From 5a48c1feedf931159086d6c70fe8aaf7ddf1b15b Mon Sep 17 00:00:00 2001 From: Siyuan Liu Date: Mon, 31 Jul 2017 09:32:45 +0800 Subject: [PATCH] fixes 10128 --- lib/common/asar.js | 39 +++++++++++++++++++-------------------- spec/asar-spec.js | 9 +++++++++ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/common/asar.js b/lib/common/asar.js index 8132e1bb7248..86306f792733 100644 --- a/lib/common/asar.js +++ b/lib/common/asar.js @@ -457,26 +457,6 @@ } if (typeof options === 'function') { callback = options - options = void 0 - } - const archive = getOrCreateArchive(asarPath) - if (!archive) { - return invalidArchiveError(asarPath, callback) - } - const info = archive.getFileInfo(filePath) - if (!info) { - return notFoundError(asarPath, filePath, callback) - } - if (info.size === 0) { - return process.nextTick(function () { - callback(null, new Buffer(0)) - }) - } - if (info.unpacked) { - const realPath = archive.copyFileOut(filePath) - return fs.readFile(realPath, options, callback) - } - if (!options) { options = { encoding: null } @@ -488,6 +468,25 @@ throw new TypeError('Bad arguments') } const {encoding} = options + + const archive = getOrCreateArchive(asarPath) + if (!archive) { + return invalidArchiveError(asarPath, callback) + } + const info = archive.getFileInfo(filePath) + if (!info) { + return notFoundError(asarPath, filePath, callback) + } + if (info.size === 0) { + return process.nextTick(function () { + callback(null, encoding? '' : new Buffer(0)) + }) + } + if (info.unpacked) { + const realPath = archive.copyFileOut(filePath) + return fs.readFile(realPath, options, callback) + } + const buffer = new Buffer(info.size) const fd = archive.getFd() if (!(fd >= 0)) { diff --git a/spec/asar-spec.js b/spec/asar-spec.js index 9c91b6089828..391558a2a149 100644 --- a/spec/asar-spec.js +++ b/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) {