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,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'

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