Browse Source

fixes 10128

Siyuan Liu 7 years ago
parent
commit
5a48c1feed
2 changed files with 22 additions and 14 deletions
  1. 13 14
      lib/common/asar.js
  2. 9 0
      spec/asar-spec.js

+ 13 - 14
lib/common/asar.js

@@ -457,8 +457,18 @@
       }
       if (typeof options === 'function') {
         callback = options
-        options = void 0
+        options = {
+          encoding: null
+        }
+      } else if (util.isString(options)) {
+        options = {
+          encoding: options
+        }
+      } else if (!util.isObject(options)) {
+        throw new TypeError('Bad arguments')
       }
+      const {encoding} = options
+
       const archive = getOrCreateArchive(asarPath)
       if (!archive) {
         return invalidArchiveError(asarPath, callback)
@@ -469,25 +479,14 @@
       }
       if (info.size === 0) {
         return process.nextTick(function () {
-          callback(null, new Buffer(0))
+          callback(null, encoding? '' : new Buffer(0))
         })
       }
       if (info.unpacked) {
         const realPath = archive.copyFileOut(filePath)
         return fs.readFile(realPath, options, callback)
       }
-      if (!options) {
-        options = {
-          encoding: null
-        }
-      } else if (util.isString(options)) {
-        options = {
-          encoding: options
-        }
-      } else if (!util.isObject(options)) {
-        throw new TypeError('Bad arguments')
-      }
-      const {encoding} = options
+      
       const buffer = new Buffer(info.size)
       const fd = archive.getFd()
       if (!(fd >= 0)) {

+ 9 - 0
spec/asar-spec.js

@@ -99,6 +99,15 @@ describe('asar package', function () {
         })
       })
 
+      it('reads from a empty file with encoding', function (done) {
+        var p = path.join(fixtures, 'asar', 'empty.asar', 'file1')
+        fs.readFile(p, 'utf8', function (err, content) {
+          assert.equal(err, null)
+          assert.equal(content, '')
+          done()
+        })
+      })
+
       it('reads a linked file', function (done) {
         var p = path.join(fixtures, 'asar', 'a.asar', 'link1')
         fs.readFile(p, function (err, content) {