fix(asar): readdir(withFileTypes) fails on deep directory (#26865)

when using readdirSync on a deep directory within the archive, the code fails to get the stats of child paths.
This commit is contained in:
Avi Vahl 2020-12-15 04:21:49 +02:00 committed by GitHub
parent b73b343e29
commit 9b02d94e97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View file

@ -615,9 +615,10 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
if (options.withFileTypes) { if (options.withFileTypes) {
const dirents = []; const dirents = [];
for (const file of files) { for (const file of files) {
const stats = archive.stat(file); const childPath = path.join(filePath, file);
const stats = archive.stat(childPath);
if (!stats) { if (!stats) {
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath: file }); const error = createError(AsarError.NOT_FOUND, { asarPath, filePath: childPath });
nextTick(callback!, [error]); nextTick(callback!, [error]);
return; return;
} }
@ -657,9 +658,10 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
if (options && (options as any).withFileTypes) { if (options && (options as any).withFileTypes) {
const dirents = []; const dirents = [];
for (const file of files) { for (const file of files) {
const stats = archive.stat(file); const childPath = path.join(filePath, file);
const stats = archive.stat(childPath);
if (!stats) { if (!stats) {
throw createError(AsarError.NOT_FOUND, { asarPath, filePath: file }); throw createError(AsarError.NOT_FOUND, { asarPath, filePath: childPath });
} }
if (stats.isFile) { if (stats.isFile) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_FILE)); dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_FILE));

View file

@ -920,6 +920,16 @@ describe('asar package', function () {
expect(names).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']); expect(names).to.deep.equal(['dir1', 'dir2', 'dir3', 'file1', 'file2', 'file3', 'link1', 'link2', 'ping.js']);
}); });
it('supports withFileTypes for a deep directory', function () {
const p = path.join(asarDir, 'a.asar', 'dir3');
const dirs = fs.readdirSync(p, { withFileTypes: true });
for (const dir of dirs) {
expect(dir instanceof fs.Dirent).to.be.true();
}
const names = dirs.map(a => a.name);
expect(names).to.deep.equal(['file1', 'file2', 'file3']);
});
it('reads dirs from a linked dir', function () { it('reads dirs from a linked dir', function () {
const p = path.join(asarDir, 'a.asar', 'link2', 'link2'); const p = path.join(asarDir, 'a.asar', 'link2', 'link2');
const dirs = fs.readdirSync(p); const dirs = fs.readdirSync(p);