Skip to content

Commit

Permalink
Merge pull request #582 from almeidx/exists-promise
Browse files Browse the repository at this point in the history
refactor(loadImage): use promise version of existsSync
  • Loading branch information
Brooooooklyn committed Jan 1, 2023
2 parents 110d92f + 8b20215 commit 13c36c0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion load-image.js
Expand Up @@ -32,7 +32,7 @@ module.exports = async function loadImage(source, options = {}) {
// if source is a string or URL instance
if (typeof source === 'string' || source instanceof URL) {
// if the source exists as a file, construct image from that file
if (fs.existsSync(source)) {
if (await exists(source)) {
return createImage(await fs.promises.readFile(source), options.alt)
} else {
// the source is a remote url here
Expand Down Expand Up @@ -101,3 +101,12 @@ function isBufferLike(src) {
src instanceof Object.getPrototypeOf(Uint8Array)
)
}

async function exists(path) {
try {
await fs.promises.access(path, fs.constants.F_OK)
return true
} catch {
return false
}
}

0 comments on commit 13c36c0

Please sign in to comment.