path = require 'path'
util = require 'util'
+kEmptyBufferLength = 0
+
# Cache asar archive objects.
cachedArchives = {}
getOrCreateArchive = (p) ->
flag = options.flag || 'r'
encoding = options.encoding
- buffer = new Buffer(info.size)
- open archive.path, flag, (error, fd) ->
- return callback error if error
- fs.read fd, buffer, 0, info.size, info.offset, (error) ->
- fs.close fd, ->
- callback error, if encoding then buffer.toString encoding else buffer
+ bufferLength = info.size || kEmptyBufferLength
+ buffer = new Buffer(bufferLength)
+
+ if info.size
+ open archive.path, flag, (error, fd) ->
+ return callback error if error
+ fs.read fd, buffer, 0, info.size, info.offset, (error, bytesRead, buf) ->
+ fs.close fd, ->
+ callback error, if encoding then buf.toString encoding, 0 ,bytesRead else buf
+ else
+ callback null, buffer
openSync = fs.openSync
readFileSync = fs.readFileSync
flag = options.flag || 'r'
encoding = options.encoding
- buffer = new Buffer(info.size)
- fd = openSync archive.path, flag
- try
- fs.readSync fd, buffer, 0, info.size, info.offset
- catch e
- throw e
- finally
- fs.closeSync fd
- if encoding then buffer.toString encoding else buffer
+ bufferLength = info.size || kEmptyBufferLength
+ buffer = new Buffer(bufferLength)
+
+ if info.size
+ fd = openSync archive.path, flag
+ try
+ bytesRead = fs.readSync fd, buffer, 0, info.size, info.offset
+ catch e
+ throw e
+ finally
+ fs.closeSync fd
+ if encoding then buffer.toString encoding, 0, bytesRead else buffer
+ else
+ buffer
readdir = fs.readdir
fs.readdir = (p, callback) ->
file3 = path.join fixtures, 'asar', 'a.asar', 'file3'
assert.equal fs.readFileSync(file3).toString(), 'file3\n'
+ it 'reads from a empty file', ->
+ file = path.join fixtures, 'asar', 'empty.asar', 'file1'
+ buffer = fs.readFileSync(file)
+ assert.equal buffer.length, 0
+ assert.equal buffer.toString(), ''
+
it 'reads a linked file', ->
p = path.join fixtures, 'asar', 'a.asar', 'link1'
assert.equal fs.readFileSync(p).toString(), 'file1\n'
assert.equal String(content), 'file1\n'
done()
+ it 'reads from a empty file', (done) ->
+ p = path.join fixtures, 'asar', 'empty.asar', 'file1'
+ fs.readFile p, (err, content) ->
+ assert.equal err, null
+ assert.equal String(content), ''
+ done()
+
it 'reads a linked file', (done) ->
p = path.join fixtures, 'asar', 'a.asar', 'link1'
fs.readFile p, (err, content) ->