electron/lib/common/asar.js

617 lines
17 KiB
JavaScript
Raw Normal View History

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