Browse Source

fix: handle null/undefined options for fs.readdir (#36846)

* fix: handle null/undefined options for fs.readdir (#34764)

Co-authored-by: David Sanders <[email protected]>

* chore: remove optional chaining

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: David Sanders <[email protected]>
trop[bot] 2 years ago
parent
commit
b110a19358
2 changed files with 29 additions and 3 deletions
  1. 3 3
      lib/asar/fs-wrapper.ts
  2. 26 0
      spec/asar-spec.js

+ 3 - 3
lib/asar/fs-wrapper.ts

@@ -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;
@@ -646,7 +646,7 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
       return;
     }
 
-    if (options.withFileTypes) {
+    if (options && options.withFileTypes) {
       const dirents = [];
       for (const file of files) {
         const childPath = path.join(filePath, file);

+ 26 - 0
spec/asar-spec.js

@@ -1014,6 +1014,32 @@ describe('asar package', function () {
           }
         });
       });
+
+      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 () {