Merge pull request #1082 from atom/graceful-asar

Make asar support work with graceful-fs
This commit is contained in:
Cheng Zhao 2015-02-02 15:01:20 -08:00
commit c6fabf8613
5 changed files with 233 additions and 206 deletions

View file

@ -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

View file

@ -1,6 +1,5 @@
asar = process.atomBinding 'asar'
child_process = require 'child_process'
fs = require 'fs'
path = require 'path'
util = require 'util'
@ -60,7 +59,44 @@ createNotFoundError = (asarPath, filePath) ->
error.errno = -2
error
# Override APIs that rely on passing file path instead of content to C++.
overrideAPISync = (module, name, arg = 0) ->
old = module[name]
module[name] = ->
p = arguments[arg]
[isAsar, asarPath, filePath] = splitPath p
return old.apply this, arguments unless isAsar
archive = getOrCreateArchive asarPath
throw new Error("Invalid package #{asarPath}") unless archive
newPath = archive.copyFileOut filePath
throw createNotFoundError(asarPath, filePath) unless newPath
arguments[arg] = newPath
old.apply this, arguments
overrideAPI = (module, name, arg = 0) ->
old = module[name]
module[name] = ->
p = arguments[arg]
[isAsar, asarPath, filePath] = splitPath p
return old.apply this, arguments unless isAsar
callback = arguments[arguments.length - 1]
return overrideAPISync module, name, arg unless typeof callback is 'function'
archive = getOrCreateArchive asarPath
return callback new Error("Invalid package #{asarPath}") unless archive
newPath = archive.copyFileOut filePath
return callback createNotFoundError(asarPath, filePath) unless newPath
arguments[arg] = newPath
old.apply this, arguments
# Override fs APIs.
exports.wrapFsWithAsar = (fs) ->
lstatSync = fs.lstatSync
fs.lstatSync = (p) ->
[isAsar, asarPath, filePath] = splitPath p
@ -257,42 +293,6 @@ fs.readdirSync = (p) ->
files
# Override APIs that rely on passing file path instead of content to C++.
overrideAPISync = (module, name, arg = 0) ->
old = module[name]
module[name] = ->
p = arguments[arg]
[isAsar, asarPath, filePath] = splitPath p
return old.apply this, arguments unless isAsar
archive = getOrCreateArchive asarPath
throw new Error("Invalid package #{asarPath}") unless archive
newPath = archive.copyFileOut filePath
throw createNotFoundError(asarPath, filePath) unless newPath
arguments[arg] = newPath
old.apply this, arguments
overrideAPI = (module, name, arg = 0) ->
old = module[name]
module[name] = ->
p = arguments[arg]
[isAsar, asarPath, filePath] = splitPath p
return old.apply this, arguments unless isAsar
callback = arguments[arguments.length - 1]
return overrideAPISync module, name, arg unless typeof callback is 'function'
archive = getOrCreateArchive asarPath
return callback new Error("Invalid package #{asarPath}") unless archive
newPath = archive.copyFileOut filePath
return callback createNotFoundError(asarPath, filePath) unless newPath
arguments[arg] = newPath
old.apply this, arguments
overrideAPI fs, 'open'
overrideAPI child_process, 'execFile'
overrideAPISync process, 'dlopen', 1

View file

@ -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);
"""

View file

@ -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

View file

@ -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",