diff --git a/lib/asar/fs-wrapper.ts b/lib/asar/fs-wrapper.ts index e7e92d28f9f1..eeb6e06239f8 100644 --- a/lib/asar/fs-wrapper.ts +++ b/lib/asar/fs-wrapper.ts @@ -261,6 +261,14 @@ export const wrapFsWithAsar = (fs: Record) => { fs.writeSync(logFDs.get(asarPath), `${offset}: ${filePath}\n`); }; + const shouldThrowStatError = (options: any) => { + if (options && typeof options === 'object' && options.throwIfNoEntry === false) { + return false; + } + + return true; + }; + const { lstatSync } = fs; fs.lstatSync = (pathArgument: string, options: any) => { const pathInfo = splitPath(pathArgument); @@ -268,10 +276,16 @@ export const wrapFsWithAsar = (fs: Record) => { const { asarPath, filePath } = pathInfo; const archive = getOrCreateArchive(asarPath); - if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath }); + if (!archive) { + if (shouldThrowStatError(options)) throw createError(AsarError.INVALID_ARCHIVE, { asarPath }); + return null; + } const stats = archive.stat(filePath); - if (!stats) throw createError(AsarError.NOT_FOUND, { asarPath, filePath }); + if (!stats) { + if (shouldThrowStatError(options)) throw createError(AsarError.NOT_FOUND, { asarPath, filePath }); + return null; + } return asarStatsToFsStats(stats); }; diff --git a/spec/asar-spec.ts b/spec/asar-spec.ts index ed7a58753084..6caa556cdae2 100644 --- a/spec/asar-spec.ts +++ b/spec/asar-spec.ts @@ -490,6 +490,15 @@ describe('asar package', function () { }).to.throw(/ENOENT/); } }); + + itremote('returns null when can not find file with throwIfNoEntry === false', function () { + const ref2 = ['file4', 'file5', path.join('dir1', 'file4')]; + for (let j = 0, len = ref2.length; j < len; j++) { + const file = ref2[j]; + const p = path.join(asarDir, 'a.asar', file); + expect(fs.lstatSync(p, { throwIfNoEntry: false })).to.equal(null); + } + }); }); describe('fs.lstat', function () {