Skip to content

Commit

Permalink
fix: handle null/undefined options for fs.readdir (electron#34764)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored and gecko19 committed Feb 28, 2023
1 parent 1272d0d commit 3445ad3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/asar/fs-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,11 +623,11 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
};

const { readdir } = fs;
fs.readdir = function (pathArgument: string, options: { encoding?: string | null; withFileTypes?: boolean } = {}, callback?: Function) {
fs.readdir = function (pathArgument: string, options?: { encoding?: string | null; withFileTypes?: boolean } | null, callback?: Function) {
const pathInfo = splitPath(pathArgument);
if (typeof options === 'function') {
callback = options;
options = {};
options = undefined;
}
if (!pathInfo.isAsar) return readdir.apply(this, arguments);
const { asarPath, filePath } = pathInfo;
Expand All @@ -646,7 +646,7 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
return;
}

if (options.withFileTypes) {
if (options?.withFileTypes) {
const dirents = [];
for (const file of files) {
const childPath = path.join(filePath, file);
Expand Down
26 changes: 26 additions & 0 deletions spec/asar-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,32 @@ describe('asar package', function () {
const err = await new Promise<any>(resolve => fs.readdir(p, resolve));
expect(err.code).to.equal('ENOENT');
});

it('handles null for options', function (done) {
const p = path.join(asarDir, 'a.asar', 'dir1');
fs.readdir(p, null, function (err, dirs) {
try {
expect(err).to.be.null();
expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
done();
} catch (e) {
done(e);
}
});
});

it('handles undefined for options', function (done) {
const p = path.join(asarDir, 'a.asar', 'dir1');
fs.readdir(p, undefined, function (err, dirs) {
try {
expect(err).to.be.null();
expect(dirs).to.deep.equal(['file1', 'file2', 'file3', 'link1', 'link2']);
done();
} catch (e) {
done(e);
}
});
});
});

describe('fs.promises.readdir', function () {
Expand Down

0 comments on commit 3445ad3

Please sign in to comment.