Merge pull request #10160 from liusy182/liusy182-10128-asar
Return empty string when fs.readFile with 'utf8' option from asar file
This commit is contained in:
commit
72d2e176dd
2 changed files with 28 additions and 20 deletions
|
@ -457,26 +457,6 @@
|
||||||
}
|
}
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
callback = options
|
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 = {
|
options = {
|
||||||
encoding: null
|
encoding: null
|
||||||
}
|
}
|
||||||
|
@ -488,6 +468,25 @@
|
||||||
throw new TypeError('Bad arguments')
|
throw new TypeError('Bad arguments')
|
||||||
}
|
}
|
||||||
const {encoding} = options
|
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 buffer = new Buffer(info.size)
|
||||||
const fd = archive.getFd()
|
const fd = archive.getFd()
|
||||||
if (!(fd >= 0)) {
|
if (!(fd >= 0)) {
|
||||||
|
|
|
@ -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) {
|
it('reads a linked file', function (done) {
|
||||||
var p = path.join(fixtures, 'asar', 'a.asar', 'link1')
|
var p = path.join(fixtures, 'asar', 'a.asar', 'link1')
|
||||||
fs.readFile(p, function (err, content) {
|
fs.readFile(p, function (err, content) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue