From 5381426da2fef6e8e893aace08ea27b41982c2f6 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Tue, 10 Jan 2023 03:16:39 -0800 Subject: [PATCH] fix: handle null/undefined options for fs.readdir (#34764) --- lib/asar/fs-wrapper.ts | 6 +++--- spec/asar-spec.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/asar/fs-wrapper.ts b/lib/asar/fs-wrapper.ts index 61d55c9f02343b..2059dc447fe545 100644 --- a/lib/asar/fs-wrapper.ts +++ b/lib/asar/fs-wrapper.ts @@ -623,11 +623,11 @@ export const wrapFsWithAsar = (fs: Record) => { }; 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; @@ -646,7 +646,7 @@ export const wrapFsWithAsar = (fs: Record) => { return; } - if (options.withFileTypes) { + if (options?.withFileTypes) { const dirents = []; for (const file of files) { const childPath = path.join(filePath, file); diff --git a/spec/asar-spec.ts b/spec/asar-spec.ts index b96b65a4c09956..d835011c69ed4e 100644 --- a/spec/asar-spec.ts +++ b/spec/asar-spec.ts @@ -965,6 +965,32 @@ describe('asar package', function () { const err = await new Promise(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 () {