Merge pull request #1082 from atom/graceful-asar
Make asar support work with graceful-fs
This commit is contained in:
commit
c6fabf8613
5 changed files with 233 additions and 206 deletions
|
@ -2,7 +2,7 @@ vm = require 'vm'
|
|||
|
||||
# Execute the 'fs.js' and pass the 'exports' to it.
|
||||
source = '(function (exports, require, module, __filename, __dirname) { ' +
|
||||
process.binding('natives').fs +
|
||||
process.binding('natives').originalFs +
|
||||
'\n});'
|
||||
fn = vm.runInThisContext source, { filename: 'fs.js' }
|
||||
fn exports, require, module
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
asar = process.atomBinding 'asar'
|
||||
child_process = require 'child_process'
|
||||
fs = require 'fs'
|
||||
path = require 'path'
|
||||
util = require 'util'
|
||||
|
||||
|
@ -60,203 +59,6 @@ createNotFoundError = (asarPath, filePath) ->
|
|||
error.errno = -2
|
||||
error
|
||||
|
||||
# Override fs APIs.
|
||||
lstatSync = fs.lstatSync
|
||||
fs.lstatSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return lstatSync p unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
stats = archive.stat filePath
|
||||
throw createNotFoundError(asarPath, filePath) unless stats
|
||||
|
||||
asarStatsToFsStats stats
|
||||
|
||||
lstat = fs.lstat
|
||||
fs.lstat = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return lstat p, callback unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
stats = getOrCreateArchive(asarPath).stat filePath
|
||||
return callback createNotFoundError(asarPath, filePath) unless stats
|
||||
|
||||
process.nextTick -> callback null, asarStatsToFsStats stats
|
||||
|
||||
statSync = fs.statSync
|
||||
fs.statSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return statSync p unless isAsar
|
||||
|
||||
# Do not distinguish links for now.
|
||||
fs.lstatSync p
|
||||
|
||||
stat = fs.stat
|
||||
fs.stat = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return stat p, callback unless isAsar
|
||||
|
||||
# Do not distinguish links for now.
|
||||
process.nextTick -> fs.lstat p, callback
|
||||
|
||||
statSyncNoException = fs.statSyncNoException
|
||||
fs.statSyncNoException = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return statSyncNoException p unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return false unless archive
|
||||
stats = archive.stat filePath
|
||||
return false unless stats
|
||||
asarStatsToFsStats stats
|
||||
|
||||
realpathSync = fs.realpathSync
|
||||
fs.realpathSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return realpathSync.apply this, arguments unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
real = archive.realpath filePath
|
||||
throw createNotFoundError(asarPath, filePath) if real is false
|
||||
|
||||
path.join realpathSync(asarPath), real
|
||||
|
||||
realpath = fs.realpath
|
||||
fs.realpath = (p, cache, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return realpath.apply this, arguments unless isAsar
|
||||
|
||||
if typeof cache is 'function'
|
||||
callback = cache
|
||||
cache = undefined
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
real = archive.realpath filePath
|
||||
return callback createNotFoundError(asarPath, filePath) if real is false
|
||||
|
||||
realpath asarPath, (err, p) ->
|
||||
return callback err if err
|
||||
callback null, path.join(p, real)
|
||||
|
||||
exists = fs.exists
|
||||
fs.exists = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return exists p, callback unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
process.nextTick -> callback archive.stat(filePath) isnt false
|
||||
|
||||
existsSync = fs.existsSync
|
||||
fs.existsSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return existsSync p unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return false unless archive
|
||||
|
||||
archive.stat(filePath) isnt false
|
||||
|
||||
open = fs.open
|
||||
readFile = fs.readFile
|
||||
fs.readFile = (p, options, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readFile.apply this, arguments unless isAsar
|
||||
|
||||
if typeof options is 'function'
|
||||
callback = options
|
||||
options = undefined
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
info = archive.getFileInfo filePath
|
||||
return callback createNotFoundError(asarPath, filePath) unless info
|
||||
|
||||
if not options
|
||||
options = encoding: null, flag: 'r'
|
||||
else if util.isString options
|
||||
options = encoding: options, flag: 'r'
|
||||
else if not util.isObject options
|
||||
throw new TypeError('Bad arguments')
|
||||
|
||||
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
|
||||
|
||||
openSync = fs.openSync
|
||||
readFileSync = fs.readFileSync
|
||||
fs.readFileSync = (p, options) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readFileSync.apply this, arguments unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
info = archive.getFileInfo filePath
|
||||
throw createNotFoundError(asarPath, filePath) unless info
|
||||
|
||||
if not options
|
||||
options = encoding: null, flag: 'r'
|
||||
else if util.isString options
|
||||
options = encoding: options, flag: 'r'
|
||||
else if not util.isObject options
|
||||
throw new TypeError('Bad arguments')
|
||||
|
||||
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
|
||||
|
||||
readdir = fs.readdir
|
||||
fs.readdir = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readdir.apply this, arguments unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
files = archive.readdir filePath
|
||||
return callback createNotFoundError(asarPath, filePath) unless files
|
||||
|
||||
process.nextTick -> callback null, files
|
||||
|
||||
readdirSync = fs.readdirSync
|
||||
fs.readdirSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readdirSync.apply this, arguments unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
files = archive.readdir filePath
|
||||
throw createNotFoundError(asarPath, filePath) unless files
|
||||
|
||||
files
|
||||
|
||||
# Override APIs that rely on passing file path instead of content to C++.
|
||||
overrideAPISync = (module, name, arg = 0) ->
|
||||
old = module[name]
|
||||
|
@ -293,9 +95,207 @@ overrideAPI = (module, name, arg = 0) ->
|
|||
arguments[arg] = newPath
|
||||
old.apply this, arguments
|
||||
|
||||
overrideAPI fs, 'open'
|
||||
overrideAPI child_process, 'execFile'
|
||||
overrideAPISync process, 'dlopen', 1
|
||||
overrideAPISync require('module')._extensions, '.node', 1
|
||||
overrideAPISync fs, 'openSync'
|
||||
overrideAPISync child_process, 'fork'
|
||||
# Override fs APIs.
|
||||
exports.wrapFsWithAsar = (fs) ->
|
||||
lstatSync = fs.lstatSync
|
||||
fs.lstatSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return lstatSync p unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
stats = archive.stat filePath
|
||||
throw createNotFoundError(asarPath, filePath) unless stats
|
||||
|
||||
asarStatsToFsStats stats
|
||||
|
||||
lstat = fs.lstat
|
||||
fs.lstat = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return lstat p, callback unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
stats = getOrCreateArchive(asarPath).stat filePath
|
||||
return callback createNotFoundError(asarPath, filePath) unless stats
|
||||
|
||||
process.nextTick -> callback null, asarStatsToFsStats stats
|
||||
|
||||
statSync = fs.statSync
|
||||
fs.statSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return statSync p unless isAsar
|
||||
|
||||
# Do not distinguish links for now.
|
||||
fs.lstatSync p
|
||||
|
||||
stat = fs.stat
|
||||
fs.stat = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return stat p, callback unless isAsar
|
||||
|
||||
# Do not distinguish links for now.
|
||||
process.nextTick -> fs.lstat p, callback
|
||||
|
||||
statSyncNoException = fs.statSyncNoException
|
||||
fs.statSyncNoException = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return statSyncNoException p unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return false unless archive
|
||||
stats = archive.stat filePath
|
||||
return false unless stats
|
||||
asarStatsToFsStats stats
|
||||
|
||||
realpathSync = fs.realpathSync
|
||||
fs.realpathSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return realpathSync.apply this, arguments unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
real = archive.realpath filePath
|
||||
throw createNotFoundError(asarPath, filePath) if real is false
|
||||
|
||||
path.join realpathSync(asarPath), real
|
||||
|
||||
realpath = fs.realpath
|
||||
fs.realpath = (p, cache, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return realpath.apply this, arguments unless isAsar
|
||||
|
||||
if typeof cache is 'function'
|
||||
callback = cache
|
||||
cache = undefined
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
real = archive.realpath filePath
|
||||
return callback createNotFoundError(asarPath, filePath) if real is false
|
||||
|
||||
realpath asarPath, (err, p) ->
|
||||
return callback err if err
|
||||
callback null, path.join(p, real)
|
||||
|
||||
exists = fs.exists
|
||||
fs.exists = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return exists p, callback unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
process.nextTick -> callback archive.stat(filePath) isnt false
|
||||
|
||||
existsSync = fs.existsSync
|
||||
fs.existsSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return existsSync p unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return false unless archive
|
||||
|
||||
archive.stat(filePath) isnt false
|
||||
|
||||
open = fs.open
|
||||
readFile = fs.readFile
|
||||
fs.readFile = (p, options, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readFile.apply this, arguments unless isAsar
|
||||
|
||||
if typeof options is 'function'
|
||||
callback = options
|
||||
options = undefined
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
info = archive.getFileInfo filePath
|
||||
return callback createNotFoundError(asarPath, filePath) unless info
|
||||
|
||||
if not options
|
||||
options = encoding: null, flag: 'r'
|
||||
else if util.isString options
|
||||
options = encoding: options, flag: 'r'
|
||||
else if not util.isObject options
|
||||
throw new TypeError('Bad arguments')
|
||||
|
||||
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
|
||||
|
||||
openSync = fs.openSync
|
||||
readFileSync = fs.readFileSync
|
||||
fs.readFileSync = (p, options) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readFileSync.apply this, arguments unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
info = archive.getFileInfo filePath
|
||||
throw createNotFoundError(asarPath, filePath) unless info
|
||||
|
||||
if not options
|
||||
options = encoding: null, flag: 'r'
|
||||
else if util.isString options
|
||||
options = encoding: options, flag: 'r'
|
||||
else if not util.isObject options
|
||||
throw new TypeError('Bad arguments')
|
||||
|
||||
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
|
||||
|
||||
readdir = fs.readdir
|
||||
fs.readdir = (p, callback) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readdir.apply this, arguments unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
return callback new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
files = archive.readdir filePath
|
||||
return callback createNotFoundError(asarPath, filePath) unless files
|
||||
|
||||
process.nextTick -> callback null, files
|
||||
|
||||
readdirSync = fs.readdirSync
|
||||
fs.readdirSync = (p) ->
|
||||
[isAsar, asarPath, filePath] = splitPath p
|
||||
return readdirSync.apply this, arguments unless isAsar
|
||||
|
||||
archive = getOrCreateArchive asarPath
|
||||
throw new Error("Invalid package #{asarPath}") unless archive
|
||||
|
||||
files = archive.readdir filePath
|
||||
throw createNotFoundError(asarPath, filePath) unless files
|
||||
|
||||
files
|
||||
|
||||
overrideAPI fs, 'open'
|
||||
overrideAPI child_process, 'execFile'
|
||||
overrideAPISync process, 'dlopen', 1
|
||||
overrideAPISync require('module')._extensions, '.node', 1
|
||||
overrideAPISync fs, 'openSync'
|
||||
overrideAPISync child_process, 'fork'
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
process = global.process
|
||||
fs = require 'fs'
|
||||
path = require 'path'
|
||||
timers = require 'timers'
|
||||
Module = require 'module'
|
||||
|
@ -36,4 +37,19 @@ if process.type is 'browser'
|
|||
global.setInterval = wrapWithActivateUvLoop timers.setInterval
|
||||
|
||||
# Add support for asar packages.
|
||||
require './asar'
|
||||
asar = require './asar'
|
||||
asar.wrapFsWithAsar fs
|
||||
|
||||
# Make graceful-fs work with asar.
|
||||
source = process.binding 'natives'
|
||||
source.originalFs = source.fs
|
||||
source.fs = """
|
||||
var src = '(function (exports, require, module, __filename, __dirname) { ' +
|
||||
process.binding('natives').originalFs +
|
||||
' });';
|
||||
var vm = require('vm');
|
||||
var fn = vm.runInThisContext(src, { filename: 'fs.js' });
|
||||
fn(exports, require, module);
|
||||
var asar = require(#{JSON.stringify(__dirname)} + '/asar');
|
||||
asar.wrapFsWithAsar(exports);
|
||||
"""
|
||||
|
|
|
@ -412,3 +412,13 @@ describe 'asar package', ->
|
|||
file = path.join fixtures, 'asar', 'a.asar'
|
||||
stats = originalFs.statSync file
|
||||
assert stats.isFile()
|
||||
|
||||
describe 'graceful-fs module', ->
|
||||
gfs = require 'graceful-fs'
|
||||
|
||||
it 'recognize asar archvies', ->
|
||||
p = path.join fixtures, 'asar', 'a.asar', 'link1'
|
||||
assert.equal gfs.readFileSync(p).toString(), 'file1\n'
|
||||
|
||||
it 'does not touch global fs object', ->
|
||||
assert.notEqual fs.readdir, gfs.readdir
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
"devDependencies": {
|
||||
"formidable": "1.0.16",
|
||||
"graceful-fs": "3.0.5",
|
||||
"q": "0.9.7",
|
||||
"mocha": "2.1.0",
|
||||
"runas": "2.x",
|
||||
|
|
Loading…
Add table
Reference in a new issue