Skip to content

Commit

Permalink
fix: support the throwIfNoEntry option to statSync and lstatSync in a…
Browse files Browse the repository at this point in the history
…sar files (#40224)

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
  • Loading branch information
trop[bot] and MarshallOfSound committed Oct 18, 2023
1 parent a98d66d commit 9ddd08b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/asar/fs-wrapper.ts
Expand Up @@ -261,17 +261,31 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
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);
if (!pathInfo.isAsar) return lstatSync(pathArgument, options);
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);
};
Expand Down
9 changes: 9 additions & 0 deletions spec/asar-spec.ts
Expand Up @@ -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 () {
Expand Down

0 comments on commit 9ddd08b

Please sign in to comment.