standardize asar.js by hand

This commit is contained in:
Zeke Sikelianos 2016-03-25 12:50:43 -07:00 committed by Kevin Sawicki
parent c845ea8372
commit 67fa250020

View file

@ -1,70 +1,71 @@
(function () { (function () {
const asar = process.binding('atom_common_asar'); const asar = process.binding('atom_common_asar')
const child_process = require('child_process'); const child_process = require('child_process')
const path = require('path'); const path = require('path')
const util = require('util'); const util = require('util')
var hasProp = {}.hasOwnProperty; var hasProp = {}.hasOwnProperty
// Cache asar archive objects. // Cache asar archive objects.
var cachedArchives = {}; var cachedArchives = {}
var getOrCreateArchive = function(p) { var getOrCreateArchive = function (p) {
var archive; var archive
archive = cachedArchives[p]; archive = cachedArchives[p]
if (archive != null) { if (archive != null) {
return archive; return archive
} }
archive = asar.createArchive(p); archive = asar.createArchive(p)
if (!archive) { if (!archive) {
return false; return false
} }
return cachedArchives[p] = archive; cachedArchives[p] = archive
}; return archive
}
// Clean cache on quit. // Clean cache on quit.
process.on('exit', function() { process.on('exit', function () {
var archive, p; var archive, p
for (p in cachedArchives) { for (p in cachedArchives) {
if (!hasProp.call(cachedArchives, p)) continue; if (!hasProp.call(cachedArchives, p)) continue
archive = cachedArchives[p]; archive = cachedArchives[p]
archive.destroy(); archive.destroy()
} }
}); })
// Separate asar package's path from full path. // Separate asar package's path from full path.
var splitPath = function(p) { var splitPath = function (p) {
var index; var index
// shortcut to disable asar. // shortcut to disable asar.
if (process.noAsar) { if (process.noAsar) {
return [false]; return [false]
} }
if (typeof p !== 'string') { if (typeof p !== 'string') {
return [false]; return [false]
} }
if (p.substr(-5) === '.asar') { if (p.substr(-5) === '.asar') {
return [true, p, '']; return [true, p, '']
} }
p = path.normalize(p); p = path.normalize(p)
index = p.lastIndexOf(".asar" + path.sep); index = p.lastIndexOf('.asar' + path.sep)
if (index === -1) { if (index === -1) {
return [false]; return [false]
} }
return [true, p.substr(0, index + 5), p.substr(index + 6)]; return [true, p.substr(0, index + 5), p.substr(index + 6)]
}; }
// Convert asar archive's Stats object to fs's Stats object. // Convert asar archive's Stats object to fs's Stats object.
var nextInode = 0; var nextInode = 0
var uid = process.getuid != null ? process.getuid() : 0; var uid = process.getuid != null ? process.getuid() : 0
var gid = process.getgid != null ? process.getgid() : 0; var gid = process.getgid != null ? process.getgid() : 0
var fakeTime = new Date(); var fakeTime = new Date()
var asarStatsToFsStats = function(stats) { var asarStatsToFsStats = function (stats) {
return { return {
dev: 1, dev: 1,
ino: ++nextInode, ino: ++nextInode,
@ -78,521 +79,521 @@
mtime: stats.mtime || fakeTime, mtime: stats.mtime || fakeTime,
ctime: stats.ctime || fakeTime, ctime: stats.ctime || fakeTime,
size: stats.size, size: stats.size,
isFile: function() { isFile: function () {
return stats.isFile; return stats.isFile
}, },
isDirectory: function() { isDirectory: function () {
return stats.isDirectory; return stats.isDirectory
}, },
isSymbolicLink: function() { isSymbolicLink: function () {
return stats.isLink; return stats.isLink
}, },
isBlockDevice: function() { isBlockDevice: function () {
return false; return false
}, },
isCharacterDevice: function() { isCharacterDevice: function () {
return false; return false
}, },
isFIFO: function() { isFIFO: function () {
return false; return false
}, },
isSocket: function() { isSocket: function () {
return false; return false
} }
}; }
}; }
// Create a ENOENT error. // Create a ENOENT error.
var notFoundError = function(asarPath, filePath, callback) { var notFoundError = function (asarPath, filePath, callback) {
var error; var error
error = new Error("ENOENT, " + filePath + " not found in " + asarPath); error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`)
error.code = "ENOENT"; error.code = 'ENOENT'
error.errno = -2; error.errno = -2
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
throw error; throw error
} }
return process.nextTick(function() { return process.nextTick(function () {
return callback(error); return callback(error)
}); })
}; }
// Create a ENOTDIR error. // Create a ENOTDIR error.
var notDirError = function(callback) { var notDirError = function (callback) {
var error; var error
error = new Error('ENOTDIR, not a directory'); error = new Error('ENOTDIR, not a directory')
error.code = 'ENOTDIR'; error.code = 'ENOTDIR'
error.errno = -20; error.errno = -20
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
throw error; throw error
} }
return process.nextTick(function() { return process.nextTick(function () {
return callback(error); return callback(error)
}); })
}; }
// Create invalid archive error. // Create invalid archive error.
var invalidArchiveError = function(asarPath, callback) { var invalidArchiveError = function (asarPath, callback) {
var error; var error
error = new Error("Invalid package " + asarPath); error = new Error(`Invalid package ${asarPath}`)
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
throw error; throw error
} }
return process.nextTick(function() { return process.nextTick(function () {
return callback(error); return callback(error)
}); })
}; }
// Override APIs that rely on passing file path instead of content to C++. // Override APIs that rely on passing file path instead of content to C++.
var overrideAPISync = function(module, name, arg) { var overrideAPISync = function (module, name, arg) {
var old; var old
if (arg == null) { if (arg == null) {
arg = 0; arg = 0
} }
old = module[name]; old = module[name]
return module[name] = function() { module[name] = function () {
var archive, newPath, p; var archive, newPath, p
p = arguments[arg]; p = arguments[arg]
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return old.apply(this, arguments); return old.apply(this, arguments)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
invalidArchiveError(asarPath); invalidArchiveError(asarPath)
} }
newPath = archive.copyFileOut(filePath); newPath = archive.copyFileOut(filePath)
if (!newPath) { if (!newPath) {
notFoundError(asarPath, filePath); notFoundError(asarPath, filePath)
} }
arguments[arg] = newPath; arguments[arg] = newPath
return old.apply(this, arguments); return old.apply(this, arguments)
}; }
}; }
var overrideAPI = function(module, name, arg) { var overrideAPI = function (module, name, arg) {
var old; var old
if (arg == null) { if (arg == null) {
arg = 0; arg = 0
} }
old = module[name]; old = module[name]
return module[name] = function() { module[name] = function () {
var archive, callback, newPath, p; var archive, callback, newPath, p
p = arguments[arg]; p = arguments[arg]
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return old.apply(this, arguments); return old.apply(this, arguments)
} }
callback = arguments[arguments.length - 1]; callback = arguments[arguments.length - 1]
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
return overrideAPISync(module, name, arg); return overrideAPISync(module, name, arg)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
return invalidArchiveError(asarPath, callback); return invalidArchiveError(asarPath, callback)
} }
newPath = archive.copyFileOut(filePath); newPath = archive.copyFileOut(filePath)
if (!newPath) { if (!newPath) {
return notFoundError(asarPath, filePath, callback); return notFoundError(asarPath, filePath, callback)
} }
arguments[arg] = newPath; arguments[arg] = newPath
return old.apply(this, arguments); return old.apply(this, arguments)
}; }
}; }
// Override fs APIs. // Override fs APIs.
exports.wrapFsWithAsar = function(fs) { exports.wrapFsWithAsar = function (fs) {
var exists, existsSync, internalModuleReadFile, internalModuleStat, lstat, lstatSync, mkdir, mkdirSync, readFile, readFileSync, readdir, readdirSync, realpath, realpathSync, stat, statSync, statSyncNoException, logFDs, logASARAccess; var exists, existsSync, internalModuleReadFile, internalModuleStat, lstat, lstatSync, mkdir, mkdirSync, readFile, readFileSync, readdir, readdirSync, realpath, realpathSync, stat, statSync, statSyncNoException, logFDs, logASARAccess
logFDs = {}; logFDs = {}
logASARAccess = function(asarPath, filePath, offset) { logASARAccess = function (asarPath, filePath, offset) {
if (!process.env.ELECTRON_LOG_ASAR_READS) { if (!process.env.ELECTRON_LOG_ASAR_READS) {
return; return
} }
if (!logFDs[asarPath]) { if (!logFDs[asarPath]) {
var logFilename, logPath; var logFilename, logPath
const path = require('path'); const path = require('path')
logFilename = path.basename(asarPath, '.asar') + '-access-log.txt'; logFilename = path.basename(asarPath, '.asar') + '-access-log.txt'
logPath = path.join(require('os').tmpdir(), logFilename); logPath = path.join(require('os').tmpdir(), logFilename)
logFDs[asarPath] = fs.openSync(logPath, 'a'); logFDs[asarPath] = fs.openSync(logPath, 'a')
console.log('Logging ' + asarPath + ' access to ' + logPath); console.log('Logging ' + asarPath + ' access to ' + logPath)
} }
fs.writeSync(logFDs[asarPath], offset + ': ' + filePath + '\n'); fs.writeSync(logFDs[asarPath], offset + ': ' + filePath + '\n')
}; }
lstatSync = fs.lstatSync; lstatSync = fs.lstatSync
fs.lstatSync = function(p) { fs.lstatSync = function (p) {
var archive, stats; var archive, stats
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return lstatSync(p); return lstatSync(p)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
invalidArchiveError(asarPath); invalidArchiveError(asarPath)
} }
stats = archive.stat(filePath); stats = archive.stat(filePath)
if (!stats) { if (!stats) {
notFoundError(asarPath, filePath); notFoundError(asarPath, filePath)
} }
return asarStatsToFsStats(stats); return asarStatsToFsStats(stats)
}; }
lstat = fs.lstat; lstat = fs.lstat
fs.lstat = function(p, callback) { fs.lstat = function (p, callback) {
var archive, stats; var archive, stats
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return lstat(p, callback); return lstat(p, callback)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
return invalidArchiveError(asarPath, callback); return invalidArchiveError(asarPath, callback)
} }
stats = getOrCreateArchive(asarPath).stat(filePath); stats = getOrCreateArchive(asarPath).stat(filePath)
if (!stats) { if (!stats) {
return notFoundError(asarPath, filePath, callback); return notFoundError(asarPath, filePath, callback)
} }
return process.nextTick(function() { return process.nextTick(function () {
return callback(null, asarStatsToFsStats(stats)); return callback(null, asarStatsToFsStats(stats))
}); })
}; }
statSync = fs.statSync; statSync = fs.statSync
fs.statSync = function(p) { fs.statSync = function (p) {
const [isAsar] = splitPath(p); const [isAsar] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return statSync(p); return statSync(p)
} }
// Do not distinguish links for now. // Do not distinguish links for now.
return fs.lstatSync(p); return fs.lstatSync(p)
}; }
stat = fs.stat; stat = fs.stat
fs.stat = function(p, callback) { fs.stat = function (p, callback) {
const [isAsar] = splitPath(p); const [isAsar] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return stat(p, callback); return stat(p, callback)
} }
// Do not distinguish links for now. // Do not distinguish links for now.
return process.nextTick(function() { return process.nextTick(function () {
return fs.lstat(p, callback); return fs.lstat(p, callback)
}); })
}; }
statSyncNoException = fs.statSyncNoException; statSyncNoException = fs.statSyncNoException
fs.statSyncNoException = function(p) { fs.statSyncNoException = function (p) {
var archive, stats; var archive, stats
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return statSyncNoException(p); return statSyncNoException(p)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
return false; return false
} }
stats = archive.stat(filePath); stats = archive.stat(filePath)
if (!stats) { if (!stats) {
return false; return false
} }
return asarStatsToFsStats(stats); return asarStatsToFsStats(stats)
}; }
realpathSync = fs.realpathSync; realpathSync = fs.realpathSync
fs.realpathSync = function(p) { fs.realpathSync = function (p) {
var archive, real; var archive, real
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return realpathSync.apply(this, arguments); return realpathSync.apply(this, arguments)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
invalidArchiveError(asarPath); invalidArchiveError(asarPath)
} }
real = archive.realpath(filePath); real = archive.realpath(filePath)
if (real === false) { if (real === false) {
notFoundError(asarPath, filePath); notFoundError(asarPath, filePath)
} }
return path.join(realpathSync(asarPath), real); return path.join(realpathSync(asarPath), real)
}; }
realpath = fs.realpath; realpath = fs.realpath
fs.realpath = function(p, cache, callback) { fs.realpath = function (p, cache, callback) {
var archive, real; var archive, real
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return realpath.apply(this, arguments); return realpath.apply(this, arguments)
} }
if (typeof cache === 'function') { if (typeof cache === 'function') {
callback = cache; callback = cache
cache = void 0; cache = void 0
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
return invalidArchiveError(asarPath, callback); return invalidArchiveError(asarPath, callback)
} }
real = archive.realpath(filePath); real = archive.realpath(filePath)
if (real === false) { if (real === false) {
return notFoundError(asarPath, filePath, callback); return notFoundError(asarPath, filePath, callback)
} }
return realpath(asarPath, function(err, p) { return realpath(asarPath, function (err, p) {
if (err) { if (err) {
return callback(err); return callback(err)
} }
return callback(null, path.join(p, real)); return callback(null, path.join(p, real))
}); })
}; }
exists = fs.exists; exists = fs.exists
fs.exists = function(p, callback) { fs.exists = function (p, callback) {
var archive; var archive
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return exists(p, callback); return exists(p, callback)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
return invalidArchiveError(asarPath, callback); return invalidArchiveError(asarPath, callback)
} }
return process.nextTick(function() { return process.nextTick(function () {
return callback(archive.stat(filePath) !== false); return callback(archive.stat(filePath) !== false)
}); })
}; }
existsSync = fs.existsSync; existsSync = fs.existsSync
fs.existsSync = function(p) { fs.existsSync = function (p) {
var archive; var archive
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return existsSync(p); return existsSync(p)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
return false; return false
} }
return archive.stat(filePath) !== false; return archive.stat(filePath) !== false
}; }
readFile = fs.readFile; readFile = fs.readFile
fs.readFile = function(p, options, callback) { fs.readFile = function (p, options, callback) {
var archive, buffer, encoding, fd, info, realPath; var archive, buffer, encoding, fd, info, realPath
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return readFile.apply(this, arguments); return readFile.apply(this, arguments)
} }
if (typeof options === 'function') { if (typeof options === 'function') {
callback = options; callback = options
options = void 0; options = void 0
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
return invalidArchiveError(asarPath, callback); return invalidArchiveError(asarPath, callback)
} }
info = archive.getFileInfo(filePath); info = archive.getFileInfo(filePath)
if (!info) { if (!info) {
return notFoundError(asarPath, filePath, callback); return notFoundError(asarPath, filePath, callback)
} }
if (info.size === 0) { if (info.size === 0) {
return process.nextTick(function() { return process.nextTick(function () {
return callback(null, new Buffer(0)); return callback(null, new Buffer(0))
}); })
} }
if (info.unpacked) { if (info.unpacked) {
realPath = archive.copyFileOut(filePath); realPath = archive.copyFileOut(filePath)
return fs.readFile(realPath, options, callback); return fs.readFile(realPath, options, callback)
} }
if (!options) { if (!options) {
options = { options = {
encoding: null encoding: null
}; }
} else if (util.isString(options)) { } else if (util.isString(options)) {
options = { options = {
encoding: options encoding: options
}; }
} else if (!util.isObject(options)) { } else if (!util.isObject(options)) {
throw new TypeError('Bad arguments'); throw new TypeError('Bad arguments')
} }
encoding = options.encoding; encoding = options.encoding
buffer = new Buffer(info.size); buffer = new Buffer(info.size)
fd = archive.getFd(); fd = archive.getFd()
if (!(fd >= 0)) { if (!(fd >= 0)) {
return notFoundError(asarPath, filePath, callback); return notFoundError(asarPath, filePath, callback)
} }
logASARAccess(asarPath, filePath, info.offset); logASARAccess(asarPath, filePath, info.offset)
return fs.read(fd, buffer, 0, info.size, info.offset, function(error) { return fs.read(fd, buffer, 0, info.size, info.offset, function (error) {
return callback(error, encoding ? buffer.toString(encoding) : buffer); return callback(error, encoding ? buffer.toString(encoding) : buffer)
}); })
}; }
readFileSync = fs.readFileSync; readFileSync = fs.readFileSync
fs.readFileSync = function(p, opts) { fs.readFileSync = function (p, opts) {
// this allows v8 to optimize this function // this allows v8 to optimize this function
var archive, buffer, encoding, fd, info, options, realPath; var archive, buffer, encoding, fd, info, options, realPath
options = opts; options = opts
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return readFileSync.apply(this, arguments); return readFileSync.apply(this, arguments)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
invalidArchiveError(asarPath); invalidArchiveError(asarPath)
} }
info = archive.getFileInfo(filePath); info = archive.getFileInfo(filePath)
if (!info) { if (!info) {
notFoundError(asarPath, filePath); notFoundError(asarPath, filePath)
} }
if (info.size === 0) { if (info.size === 0) {
if (options) { if (options) {
return ''; return ''
} else { } else {
return new Buffer(0); return new Buffer(0)
} }
} }
if (info.unpacked) { if (info.unpacked) {
realPath = archive.copyFileOut(filePath); realPath = archive.copyFileOut(filePath)
return fs.readFileSync(realPath, options); return fs.readFileSync(realPath, options)
} }
if (!options) { if (!options) {
options = { options = {
encoding: null encoding: null
}; }
} else if (util.isString(options)) { } else if (util.isString(options)) {
options = { options = {
encoding: options encoding: options
}; }
} else if (!util.isObject(options)) { } else if (!util.isObject(options)) {
throw new TypeError('Bad arguments'); throw new TypeError('Bad arguments')
} }
encoding = options.encoding; encoding = options.encoding
buffer = new Buffer(info.size); buffer = new Buffer(info.size)
fd = archive.getFd(); fd = archive.getFd()
if (!(fd >= 0)) { if (!(fd >= 0)) {
notFoundError(asarPath, filePath); notFoundError(asarPath, filePath)
} }
logASARAccess(asarPath, filePath, info.offset); logASARAccess(asarPath, filePath, info.offset)
fs.readSync(fd, buffer, 0, info.size, info.offset); fs.readSync(fd, buffer, 0, info.size, info.offset)
if (encoding) { if (encoding) {
return buffer.toString(encoding); return buffer.toString(encoding)
} else { } else {
return buffer; return buffer
} }
}; }
readdir = fs.readdir; readdir = fs.readdir
fs.readdir = function(p, callback) { fs.readdir = function (p, callback) {
var archive, files; var archive, files
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return readdir.apply(this, arguments); return readdir.apply(this, arguments)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
return invalidArchiveError(asarPath, callback); return invalidArchiveError(asarPath, callback)
} }
files = archive.readdir(filePath); files = archive.readdir(filePath)
if (!files) { if (!files) {
return notFoundError(asarPath, filePath, callback); return notFoundError(asarPath, filePath, callback)
} }
return process.nextTick(function() { return process.nextTick(function () {
return callback(null, files); return callback(null, files)
}); })
}; }
readdirSync = fs.readdirSync; readdirSync = fs.readdirSync
fs.readdirSync = function(p) { fs.readdirSync = function (p) {
var archive, files; var archive, files
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return readdirSync.apply(this, arguments); return readdirSync.apply(this, arguments)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
invalidArchiveError(asarPath); invalidArchiveError(asarPath)
} }
files = archive.readdir(filePath); files = archive.readdir(filePath)
if (!files) { if (!files) {
notFoundError(asarPath, filePath); notFoundError(asarPath, filePath)
} }
return files; return files
}; }
internalModuleReadFile = process.binding('fs').internalModuleReadFile; internalModuleReadFile = process.binding('fs').internalModuleReadFile
process.binding('fs').internalModuleReadFile = function(p) { process.binding('fs').internalModuleReadFile = function (p) {
var archive, buffer, fd, info, realPath; var archive, buffer, fd, info, realPath
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return internalModuleReadFile(p); return internalModuleReadFile(p)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) {
return void 0; return void 0
} }
info = archive.getFileInfo(filePath); info = archive.getFileInfo(filePath)
if (!info) { if (!info) {
return void 0; return void 0
} }
if (info.size === 0) { if (info.size === 0) {
return ''; return ''
} }
if (info.unpacked) { if (info.unpacked) {
realPath = archive.copyFileOut(filePath); realPath = archive.copyFileOut(filePath)
return fs.readFileSync(realPath, { return fs.readFileSync(realPath, {
encoding: 'utf8' encoding: 'utf8'
}); })
} }
buffer = new Buffer(info.size); buffer = new Buffer(info.size)
fd = archive.getFd(); fd = archive.getFd()
if (!(fd >= 0)) { if (!(fd >= 0)) {
return void 0; return void 0
} }
logASARAccess(asarPath, filePath, info.offset); logASARAccess(asarPath, filePath, info.offset)
fs.readSync(fd, buffer, 0, info.size, info.offset); fs.readSync(fd, buffer, 0, info.size, info.offset)
return buffer.toString('utf8'); return buffer.toString('utf8')
}; }
internalModuleStat = process.binding('fs').internalModuleStat; internalModuleStat = process.binding('fs').internalModuleStat
process.binding('fs').internalModuleStat = function(p) { process.binding('fs').internalModuleStat = function (p) {
var archive, stats; var archive, stats
const [isAsar, asarPath, filePath] = splitPath(p); const [isAsar, asarPath, filePath] = splitPath(p)
if (!isAsar) { if (!isAsar) {
return internalModuleStat(p); return internalModuleStat(p)
} }
archive = getOrCreateArchive(asarPath); archive = getOrCreateArchive(asarPath)
// -ENOENT // -ENOENT
if (!archive) { if (!archive) {
return -34; return -34
} }
stats = archive.stat(filePath); stats = archive.stat(filePath)
// -ENOENT // -ENOENT
if (!stats) { if (!stats) {
return -34; return -34
} }
if (stats.isDirectory) { if (stats.isDirectory) {
return 1; return 1
} else { } else {
return 0; return 0
} }
}; }
// Calling mkdir for directory inside asar archive should throw ENOTDIR // Calling mkdir for directory inside asar archive should throw ENOTDIR
// error, but on Windows it throws ENOENT. // error, but on Windows it throws ENOENT.
// This is to work around the recursive looping bug of mkdirp since it is // This is to work around the recursive looping bug of mkdirp since it is
// widely used. // widely used.
if (process.platform === 'win32') { if (process.platform === 'win32') {
mkdir = fs.mkdir; mkdir = fs.mkdir
fs.mkdir = function(p, mode, callback) { fs.mkdir = function (p, mode, callback) {
if (typeof mode === 'function') { if (typeof mode === 'function') {
callback = mode; callback = mode
} }
const [isAsar, , filePath] = splitPath(p); const [isAsar, , filePath] = splitPath(p)
if (isAsar && filePath.length) { if (isAsar && filePath.length) {
return notDirError(callback); return notDirError(callback)
} }
return mkdir(p, mode, callback); return mkdir(p, mode, callback)
}; }
mkdirSync = fs.mkdirSync; mkdirSync = fs.mkdirSync
fs.mkdirSync = function(p, mode) { fs.mkdirSync = function (p, mode) {
const [isAsar, , filePath] = splitPath(p); const [isAsar, , filePath] = splitPath(p)
if (isAsar && filePath.length) { if (isAsar && filePath.length) {
notDirError(); notDirError()
} }
return mkdirSync(p, mode); return mkdirSync(p, mode)
}; }
} }
overrideAPI(fs, 'open'); overrideAPI(fs, 'open')
overrideAPI(child_process, 'execFile'); overrideAPI(child_process, 'execFile')
overrideAPISync(process, 'dlopen', 1); overrideAPISync(process, 'dlopen', 1)
overrideAPISync(require('module')._extensions, '.node', 1); overrideAPISync(require('module')._extensions, '.node', 1)
overrideAPISync(fs, 'openSync'); overrideAPISync(fs, 'openSync')
return overrideAPISync(child_process, 'execFileSync'); return overrideAPISync(child_process, 'execFileSync')
}; }
})(); })()