Reuse archive's fd in Node asar API

This commit is contained in:
Cheng Zhao 2015-05-11 11:10:50 +08:00
parent d8d7e5b9bb
commit d9c769fa69

View file

@ -236,21 +236,20 @@ exports.wrapFsWithAsar = (fs) ->
return fs.readFile realPath, options, callback return fs.readFile realPath, options, callback
if not options if not options
options = encoding: null, flag: 'r' options = encoding: null
else if util.isString options else if util.isString options
options = encoding: options, flag: 'r' options = encoding: options
else if not util.isObject options else if not util.isObject options
throw new TypeError('Bad arguments') throw new TypeError('Bad arguments')
flag = options.flag || 'r'
encoding = options.encoding encoding = options.encoding
buffer = new Buffer(info.size) buffer = new Buffer(info.size)
open archive.path, flag, (error, fd) -> fd = archive.getFd()
return callback error if error return notFoundError asarPath, filePath, callback unless fd >= 0
fs.read fd, buffer, 0, info.size, info.offset, (error) ->
fs.close fd, -> fs.read fd, buffer, 0, info.size, info.offset, (error) ->
callback error, if encoding then buffer.toString encoding else buffer callback error, if encoding then buffer.toString encoding else buffer
openSync = fs.openSync openSync = fs.openSync
readFileSync = fs.readFileSync readFileSync = fs.readFileSync
@ -270,23 +269,19 @@ exports.wrapFsWithAsar = (fs) ->
return fs.readFileSync realPath, options return fs.readFileSync realPath, options
if not options if not options
options = encoding: null, flag: 'r' options = encoding: null
else if util.isString options else if util.isString options
options = encoding: options, flag: 'r' options = encoding: options
else if not util.isObject options else if not util.isObject options
throw new TypeError('Bad arguments') throw new TypeError('Bad arguments')
flag = options.flag || 'r'
encoding = options.encoding encoding = options.encoding
buffer = new Buffer(info.size) buffer = new Buffer(info.size)
fd = openSync archive.path, flag fd = archive.getFd()
try notFoundError asarPath, filePath unless fd >= 0
fs.readSync fd, buffer, 0, info.size, info.offset
catch e fs.readSync fd, buffer, 0, info.size, info.offset
throw e
finally
fs.closeSync fd
if encoding then buffer.toString encoding else buffer if encoding then buffer.toString encoding else buffer
readdir = fs.readdir readdir = fs.readdir