Make fs.readFile support asar package
This commit is contained in:
parent
9b755620d3
commit
8740147aa2
1 changed files with 64 additions and 2 deletions
|
@ -25,6 +25,13 @@ asarStatsToFsStats = (stats) ->
|
|||
isSocket: -> false
|
||||
}
|
||||
|
||||
# Create a ENOENT error.
|
||||
createNotFoundError = (asarPath, filePath) ->
|
||||
error = new Error("ENOENT, #{filePath} not found in #{asarPath}")
|
||||
error.code = "ENOENT"
|
||||
error.errno = -2
|
||||
error
|
||||
|
||||
# Override fs APIs.
|
||||
statSync = fs.statSync
|
||||
fs.statSync = (p) ->
|
||||
|
@ -35,7 +42,7 @@ fs.statSync = (p) ->
|
|||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
stats = archive.stat filePath
|
||||
throw new Error("#{filePath} not found in #{asarPath}") unless stats
|
||||
throw createNotFoundError(asarPath, filePath) unless stats
|
||||
|
||||
asarStatsToFsStats stats
|
||||
|
||||
|
@ -48,7 +55,7 @@ fs.stat = (p, callback) ->
|
|||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
stats = asar.createArchive(asarPath).stat filePath
|
||||
return callback new Error("#{filePath} not found in #{asarPath}") unless stats
|
||||
return callback createNotFoundError(asarPath, filePath) unless stats
|
||||
|
||||
callback undefined, asarStatsToFsStats stats
|
||||
|
||||
|
@ -62,3 +69,58 @@ fs.statSyncNoException = (p) ->
|
|||
stats = asar.createArchive(asarPath).stat filePath
|
||||
return false unless stats
|
||||
asarStatsToFsStats stats
|
||||
|
||||
exists = fs.exists
|
||||
fs.exists = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return exists p, callback unless isAsar
|
||||
|
||||
archive = asar.createArchive asarPath
|
||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
callback archive.stat(filePath) isnt false
|
||||
|
||||
existsSync = fs.existsSync
|
||||
fs.existsSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return existsSync p unless isAsar
|
||||
|
||||
archive = asar.createArchive asarPath
|
||||
return false unless archive
|
||||
|
||||
archive.stat(filePath) isnt false
|
||||
|
||||
readFile = fs.readFile
|
||||
fs.readFile = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readFile.apply this, arguments unless isAsar
|
||||
|
||||
archive = asar.createArchive asarPath
|
||||
return callback throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
info = archive.getFileInfo filePath
|
||||
return callback createNotFoundError(asarPath, filePath) unless info
|
||||
|
||||
buffer = new Buffer(info.size)
|
||||
fs.open archive.path, 'r', (error, fd) ->
|
||||
return callback error if error
|
||||
fs.read fd, buffer, 0, info.size, info.offset, (error) ->
|
||||
fs.close fd, ->
|
||||
callback error, buffer
|
||||
|
||||
readFileSync = fs.readFileSync
|
||||
fs.readFileSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readFileSync.apply this, arguments unless isAsar
|
||||
|
||||
archive = asar.createArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
info = archive.getFileInfo filePath
|
||||
throw createNotFoundError(asarPath, filePath) unless info
|
||||
|
||||
buffer = new Buffer(info.size)
|
||||
fd = fs.openSync archive.path, 'r'
|
||||
fs.readSync fd, buffer, 0, info.size, info.offset
|
||||
fs.closeSync fd
|
||||
buffer
|
||||
|
|
Loading…
Reference in a new issue