Skip to content

Commit

Permalink
Merge pull request #10160 from liusy182/liusy182-10128-asar
Browse files Browse the repository at this point in the history
Return empty string when fs.readFile with 'utf8' option from asar file
  • Loading branch information
John Kleinschmidt authored Aug 8, 2017
2 parents ccdff72 + f56abac commit 72d2e17
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/common/asar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)) {
Expand Down
9 changes: 9 additions & 0 deletions spec/asar-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 72d2e17

Please sign in to comment.