Enable wrap arbitrary fs object
This commit is contained in:
parent
b1bb7bd8f3
commit
0a393eaa1c
2 changed files with 207 additions and 205 deletions
|
@ -1,6 +1,5 @@
|
||||||
asar = process.atomBinding 'asar'
|
asar = process.atomBinding 'asar'
|
||||||
child_process = require 'child_process'
|
child_process = require 'child_process'
|
||||||
fs = require 'fs'
|
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
util = require 'util'
|
util = require 'util'
|
||||||
|
|
||||||
|
@ -60,7 +59,44 @@ createNotFoundError = (asarPath, filePath) ->
|
||||||
error.errno = -2
|
error.errno = -2
|
||||||
error
|
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.
|
# Override fs APIs.
|
||||||
|
exports.wrapFsWithAsar = (fs) ->
|
||||||
lstatSync = fs.lstatSync
|
lstatSync = fs.lstatSync
|
||||||
fs.lstatSync = (p) ->
|
fs.lstatSync = (p) ->
|
||||||
[isAsar, asarPath, filePath] = splitPath p
|
[isAsar, asarPath, filePath] = splitPath p
|
||||||
|
@ -257,42 +293,6 @@ fs.readdirSync = (p) ->
|
||||||
|
|
||||||
files
|
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 fs, 'open'
|
||||||
overrideAPI child_process, 'execFile'
|
overrideAPI child_process, 'execFile'
|
||||||
overrideAPISync process, 'dlopen', 1
|
overrideAPISync process, 'dlopen', 1
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
process = global.process
|
process = global.process
|
||||||
|
fs = require 'fs'
|
||||||
path = require 'path'
|
path = require 'path'
|
||||||
timers = require 'timers'
|
timers = require 'timers'
|
||||||
Module = require 'module'
|
Module = require 'module'
|
||||||
|
@ -36,4 +37,5 @@ if process.type is 'browser'
|
||||||
global.setInterval = wrapWithActivateUvLoop timers.setInterval
|
global.setInterval = wrapWithActivateUvLoop timers.setInterval
|
||||||
|
|
||||||
# Add support for asar packages.
|
# Add support for asar packages.
|
||||||
require './asar'
|
asar = require './asar'
|
||||||
|
asar.wrapFsWithAsar fs
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue