Enable wrap arbitrary fs object

This commit is contained in:
Cheng Zhao 2015-01-31 23:31:14 -08:00
parent b1bb7bd8f3
commit 0a393eaa1c
2 changed files with 207 additions and 205 deletions

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,5 @@ if process.type is 'browser'
global.setInterval = wrapWithActivateUvLoop timers.setInterval
# Add support for asar packages.
require './asar'
asar = require './asar'
asar.wrapFsWithAsar fs