Skip to content

Commit

Permalink
Proper URL Validation for bibliography resolution and fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
halafy-ds committed Oct 17, 2024
1 parent bcd598e commit d254cc4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
16 changes: 13 additions & 3 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,23 @@ const rehypeCitationGenerator = (Cite) => {

for (let i = 0; i < bibliography.length; i++) {
if (isValidHttpUrl(bibliography[i])) {
const response = await fetch(bibliography[i])
bibtexFile.push(await response.text())
try {
const response = await fetch(bibliography[i])
bibtexFile.push(await response.text())
} catch (error) {
throw new Error(`Cannot fetch bibliography URL: ${error}.`)
}
} else {
if (isNode) {
bibtexFile.push(await readFile(bibliography[i]))
} else {
throw new Error(`Cannot read non valid bibliography URL in node env.`)
if (isNode) {
try {
bibtexFile.push(await readFile(bibliography[i]))
} catch (error) {
throw new Error(`Cannot read non valid bibliography URL in node env.`)
}
}
}
}
}
Expand Down
32 changes: 20 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ export const isNode = typeof window === 'undefined'

export const readFile = async (path) => {
if (isValidHttpUrl(path)) {
return fetch(path)
.then((response) => response.text())
.then((data) => data)
try {
const response = await fetch(path)
return await response.text()
} catch (error) {
throw new Error(`Cannot fetch bibliography URL: ${error}.`)
}
} else {
if (isNode) {
return import('fs').then((fs) => fs.readFileSync(path, 'utf8'))
} else {
throw new Error(`Cannot read non valid URL in node env.`)
try {
return import('fs').then((fs) => fs.readFileSync(path, 'utf8'))
} catch (error) {
throw new Error(`Cannot read non valid URL in node env.`)
}
}
}
}
Expand All @@ -23,11 +28,11 @@ export const readFile = async (path) => {
* @param {string} str
* @return {boolean}
*/
export const isValidHttpUrl = (str) => {
export const isValidHttpUrl = (str, base = "") => {
let url

try {
url = new URL(str)
url = base ? new URL(str, base): new URL(str)
} catch (_) {
return false
}
Expand Down Expand Up @@ -56,15 +61,18 @@ export const getBibliography = async (options, file) => {
}
// If local path, get absolute path
for (let i = 0; i < bibliography.length; i++) {
if (!isValidHttpUrl(bibliography[i])) {
if (!isValidHttpUrl(bibliography[i], options.path)) {
if (options.path){
throw new Error(`Cannot read non valid bibliography URL.`)
}
if (isNode) {
bibliography[i] = await import('path').then((path) =>
path.join(options.path || file.cwd, bibliography[i])
path.join(file.cwd, bibliography[i])
)
} else {
throw new Error(`Cannot read non valid bibliography URL in node env.`)
throw new Error(`Non valid bibliography URL: Provide a full valid path for biblio ${bibliography[i]} or set an appropriate "options.path"`)
}
}
}
}

return bibliography
Expand Down

0 comments on commit d254cc4

Please sign in to comment.