refactor: clean up asar functionality (#14046)

This commit is contained in:
Shelley Vohr 2018-09-07 19:23:47 -07:00 committed by GitHub
parent edc6a854d4
commit 2963e377ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,26 +10,28 @@
// Cache asar archive objects. // Cache asar archive objects.
const cachedArchives = {} const cachedArchives = {}
const envNoAsar = process.env.ELECTRON_NO_ASAR && process.type !== 'browser' && process.type !== 'renderer' // asar error types
const isAsarDisabled = function () { const NOT_FOUND = 'NOT_FOUND'
return process.noAsar || envNoAsar const NOT_DIR = 'NOT_DIR'
} const NO_ACCESS = 'NO_ACCESS'
const INVALID_ARCHIVE = 'INVALID_ARCHIVE'
const getOrCreateArchive = function (p) { const envNoAsar = process.env.ELECTRON_NO_ASAR && process.type !== 'browser' && process.type !== 'renderer'
const isAsarDisabled = () => process.noAsar || envNoAsar
const getOrCreateArchive = p => {
let archive = cachedArchives[p] let 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
}
cachedArchives[p] = archive cachedArchives[p] = archive
return archive return archive
} }
// Clean cache on quit. // Clean cache on quit.
process.on('exit', function () { process.on('exit', () => {
for (let p in cachedArchives) { for (let p in cachedArchives) {
if (!hasProp.call(cachedArchives, p)) continue if (!hasProp.call(cachedArchives, p)) continue
cachedArchives[p].destroy() cachedArchives[p].destroy()
@ -37,30 +39,22 @@
}) })
// Separate asar package's path from full path. // Separate asar package's path from full path.
const splitPath = function (p) { const splitPath = p => {
// shortcut to disable asar. // shortcut to disable asar.
if (isAsarDisabled()) { if (isAsarDisabled()) return {isAsar: false}
return [false] if (Buffer.isBuffer(p)) p = p.toString()
} if (typeof p !== 'string') return {isAsar: false}
if (p.endsWith('.asar')) return {isAsar: true, asarPath: p, filePath: ''}
if (Buffer.isBuffer(p)) {
p = p.toString()
}
if (typeof p !== 'string') {
return [false]
}
if (p.substr(-5) === '.asar') {
return [true, p, '']
}
p = path.normalize(p) p = path.normalize(p)
const index = p.lastIndexOf('.asar' + path.sep) const index = p.lastIndexOf(`.asar${path.sep}`)
if (index === -1) { if (index === -1) return {isAsar: false}
return [false]
return {
isAsar: true,
asarPath: p.substr(0, index + 5),
filePath: 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.
@ -103,78 +97,47 @@
) )
} }
// Create a ENOENT error. const createError = (errorType, {asarPath, filePath, callback} = {}) => {
const notFoundError = function (asarPath, filePath, callback) { let error
const error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`) switch (errorType) {
case NOT_FOUND:
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') { break
throw error case NOT_DIR:
} error = new Error('ENOTDIR, not a directory')
process.nextTick(function () {
callback(error)
})
}
// Create a ENOTDIR error.
const notDirError = function (callback) {
const error = new Error('ENOTDIR, not a directory')
error.code = 'ENOTDIR' error.code = 'ENOTDIR'
error.errno = -20 error.errno = -20
if (typeof callback !== 'function') { break
throw error case NO_ACCESS:
} error = new Error(`EACCES: permission denied, access '${filePath}'`)
process.nextTick(function () {
callback(error)
})
}
// Create a EACCES error.
const accessError = function (asarPath, filePath, callback) {
const error = new Error(`EACCES: permission denied, access '${filePath}'`)
error.code = 'EACCES' error.code = 'EACCES'
error.errno = -13 error.errno = -13
if (typeof callback !== 'function') { break
throw error case INVALID_ARCHIVE:
error = new Error(`Invalid package ${asarPath}`)
break
default:
throw new Error('invalid error type')
} }
process.nextTick(function () { if (typeof callback !== 'function') throw error
callback(error) process.nextTick(() => callback(error))
})
} }
// Create invalid archive error.
const invalidArchiveError = function (asarPath, callback) {
const error = new Error(`Invalid package ${asarPath}`)
if (typeof callback !== 'function') {
throw error
}
process.nextTick(function () {
callback(error)
})
}
// Override APIs that rely on passing file path instead of content to C++.
const overrideAPISync = function (module, name, arg) { const overrideAPISync = function (module, name, arg) {
if (arg == null) { if (arg == null) arg = 0
arg = 0
}
const old = module[name] const old = module[name]
module[name] = function () { module[name] = function () {
const p = arguments[arg] const 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)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) createError(INVALID_ARCHIVE, {asarPath})
invalidArchiveError(asarPath)
}
const newPath = archive.copyFileOut(filePath) const newPath = archive.copyFileOut(filePath)
if (!newPath) { if (!newPath) createError(NOT_FOUND, {asarPath, filePath})
notFoundError(asarPath, filePath)
}
arguments[arg] = newPath arguments[arg] = newPath
return old.apply(this, arguments) return old.apply(this, arguments)
@ -182,52 +145,38 @@
} }
const overrideAPI = function (module, name, arg) { const overrideAPI = function (module, name, arg) {
if (arg == null) { if (arg == null) arg = 0
arg = 0
}
const old = module[name] const old = module[name]
module[name] = function () { module[name] = function () {
const p = arguments[arg] const p = arguments[arg]
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { console.log(`${isAsar}, ${asarPath}, ${filePath}`)
return old.apply(this, arguments) if (!isAsar) return old.apply(this, arguments)
}
const callback = arguments[arguments.length - 1] const callback = arguments[arguments.length - 1]
if (typeof callback !== 'function') { if (typeof callback !== 'function') return overrideAPISync(module, name, arg)
return overrideAPISync(module, name, arg)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
return invalidArchiveError(asarPath, callback)
}
const newPath = archive.copyFileOut(filePath) const newPath = archive.copyFileOut(filePath)
if (!newPath) { if (!newPath) return createError(NOT_FOUND, {asarPath, filePath, callback})
return notFoundError(asarPath, filePath, callback)
}
arguments[arg] = newPath arguments[arg] = newPath
return old.apply(this, arguments) return old.apply(this, arguments)
} }
if (old[util.promisify.custom]) { if (old[util.promisify.custom]) {
module[name][util.promisify.custom] = function () { module[name][util.promisify.custom] = function () {
const p = arguments[arg] const p = arguments[arg]
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return old[util.promisify.custom].apply(this, arguments)
return old[util.promisify.custom].apply(this, arguments)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return new Promise(() => createError(INVALID_ARCHIVE, {asarPath}))
return new Promise(() => invalidArchiveError(asarPath))
}
const newPath = archive.copyFileOut(filePath) const newPath = archive.copyFileOut(filePath)
if (!newPath) { if (!newPath) return new Promise(() => createError(NOT_FOUND, {asarPath, filePath}))
return new Promise(() => notFoundError(asarPath, filePath))
}
arguments[arg] = newPath arguments[arg] = newPath
return old[util.promisify.custom].apply(this, arguments) return old[util.promisify.custom].apply(this, arguments)
@ -236,141 +185,121 @@
} }
// Override fs APIs. // Override fs APIs.
exports.wrapFsWithAsar = function (fs) { exports.wrapFsWithAsar = fs => {
const logFDs = {} const logFDs = {}
const logASARAccess = function (asarPath, filePath, offset) { const logASARAccess = (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]) {
const path = require('path') const path = require('path')
const logFilename = path.basename(asarPath, '.asar') + '-access-log.txt' const logFilename = `${path.basename(asarPath, '.asar')}-access-log.txt`
const logPath = path.join(require('os').tmpdir(), logFilename) const 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`)
} }
const {lstatSync} = fs const {lstatSync} = fs
fs.lstatSync = function (p) { fs.lstatSync = p => {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return lstatSync(p)
return lstatSync(p)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) createError(INVALID_ARCHIVE, {asarPath})
invalidArchiveError(asarPath)
}
const stats = archive.stat(filePath) const stats = archive.stat(filePath)
if (!stats) { if (!stats) createError(NOT_FOUND, {asarPath, filePath})
notFoundError(asarPath, filePath)
}
return asarStatsToFsStats(stats) return asarStatsToFsStats(stats)
} }
const {lstat} = fs const {lstat} = fs
fs.lstat = function (p, callback) { fs.lstat = (p, callback) => {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return lstat(p, callback)
return lstat(p, callback)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
return invalidArchiveError(asarPath, callback)
}
const stats = getOrCreateArchive(asarPath).stat(filePath) const stats = getOrCreateArchive(asarPath).stat(filePath)
if (!stats) { if (!stats) return createError(NOT_FOUND, {asarPath, filePath, callback})
return notFoundError(asarPath, filePath, callback)
} process.nextTick(() => callback(null, asarStatsToFsStats(stats)))
process.nextTick(function () {
callback(null, asarStatsToFsStats(stats))
})
} }
const {statSync} = fs const {statSync} = fs
fs.statSync = function (p) { fs.statSync = 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)
} }
const {stat} = fs const {stat} = fs
fs.stat = function (p, callback) { fs.stat = (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.
process.nextTick(function () { process.nextTick(() => fs.lstat(p, callback))
fs.lstat(p, callback)
})
} }
const {statSyncNoException} = fs const {statSyncNoException} = fs
fs.statSyncNoException = function (p) { fs.statSyncNoException = p => {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return statSyncNoException(p)
return statSyncNoException(p)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return false
return false
}
const stats = archive.stat(filePath) const stats = archive.stat(filePath)
if (!stats) { if (!stats) return false
return false
}
return asarStatsToFsStats(stats) return asarStatsToFsStats(stats)
} }
const {realpathSync} = fs const {realpathSync} = fs
fs.realpathSync = function (p) { fs.realpathSync = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) return realpathSync.apply(this, arguments) if (!isAsar) return realpathSync.apply(this, arguments)
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) return invalidArchiveError(asarPath) if (!archive) return createError(INVALID_ARCHIVE, {asarPath})
const real = archive.realpath(filePath) const real = archive.realpath(filePath)
if (real === false) notFoundError(asarPath, filePath) if (real === false) createError(NOT_FOUND, {asarPath, filePath})
return path.join(realpathSync(asarPath), real) return path.join(realpathSync(asarPath), real)
} }
fs.realpathSync.native = function (p) { fs.realpathSync.native = function (p) {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) return realpathSync.native.apply(this, arguments) if (!isAsar) return realpathSync.native.apply(this, arguments)
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) return invalidArchiveError(asarPath) if (!archive) return createError(INVALID_ARCHIVE, {asarPath})
const real = archive.realpath(filePath) const real = archive.realpath(filePath)
if (real === false) notFoundError(asarPath, filePath) if (real === false) createError(NOT_FOUND, {asarPath, filePath})
return path.join(realpathSync.native(asarPath), real) return path.join(realpathSync.native(asarPath), real)
} }
const {realpath} = fs const {realpath} = fs
fs.realpath = function (p, cache, callback) { fs.realpath = function (p, cache, callback) {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) return realpath.apply(this, arguments) if (!isAsar) return realpath.apply(this, arguments)
if (typeof cache === 'function') { if (typeof cache === 'function') {
callback = cache callback = cache
cache = void 0 cache = undefined
} }
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) return invalidArchiveError(asarPath, callback) if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
const real = archive.realpath(filePath) const real = archive.realpath(filePath)
if (real === false) return notFoundError(asarPath, filePath, callback) if (real === false) return createError(NOT_FOUND, {asarPath, filePath, callback})
return realpath(asarPath, (err, p) => { return realpath(asarPath, (err, p) => {
return (err) ? callback(err) : callback(null, path.join(p, real)) return (err) ? callback(err) : callback(null, path.join(p, real))
@ -378,7 +307,7 @@
} }
fs.realpath.native = function (p, cache, callback) { fs.realpath.native = function (p, cache, callback) {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) return realpath.native.apply(this, arguments) if (!isAsar) return realpath.native.apply(this, arguments)
if (typeof cache === 'function') { if (typeof cache === 'function') {
@ -387,10 +316,10 @@
} }
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) return invalidArchiveError(asarPath, callback) if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
const real = archive.realpath(filePath) const real = archive.realpath(filePath)
if (real === false) return notFoundError(asarPath, filePath, callback) if (real === false) return createError(NOT_FOUND, {asarPath, filePath, callback})
return realpath.native(asarPath, (err, p) => { return realpath.native(asarPath, (err, p) => {
return (err) ? callback(err) : callback(null, path.join(p, real)) return (err) ? callback(err) : callback(null, path.join(p, real))
@ -398,148 +327,112 @@
} }
const {exists} = fs const {exists} = fs
fs.exists = function (p, callback) { fs.exists = (p, callback) => {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return exists(p, callback)
return exists(p, callback)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
return invalidArchiveError(asarPath, callback)
}
process.nextTick(function () {
// Disabled due to false positive in StandardJS // Disabled due to false positive in StandardJS
// eslint-disable-next-line standard/no-callback-literal // eslint-disable-next-line standard/no-callback-literal
callback(archive.stat(filePath) !== false) process.nextTick(() => callback(archive.stat(filePath) !== false))
})
} }
fs.exists[util.promisify.custom] = function (p) { fs.exists[util.promisify.custom] = p => {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return exists[util.promisify.custom](p)
return exists[util.promisify.custom](p)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return new Promise(() => createError(INVALID_ARCHIVE, {asarPath}))
return new Promise(() => invalidArchiveError(asarPath))
}
return Promise.resolve(archive.stat(filePath) !== false) return Promise.resolve(archive.stat(filePath) !== false)
} }
const {existsSync} = fs const {existsSync} = fs
fs.existsSync = function (p) { fs.existsSync = p => {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return existsSync(p)
return existsSync(p)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return false
return false
}
return archive.stat(filePath) !== false return archive.stat(filePath) !== false
} }
const {access} = fs const {access} = fs
fs.access = function (p, mode, callback) { fs.access = function (p, mode, callback) {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return access.apply(this, arguments)
return access.apply(this, arguments)
}
if (typeof mode === 'function') { if (typeof mode === 'function') {
callback = mode callback = mode
mode = fs.constants.F_OK mode = fs.constants.F_OK
} }
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
return invalidArchiveError(asarPath, callback)
}
const info = archive.getFileInfo(filePath) const info = archive.getFileInfo(filePath)
if (!info) { if (!info) return createError(NOT_FOUND, {asarPath, filePath, callback})
return notFoundError(asarPath, filePath, callback)
}
if (info.unpacked) { if (info.unpacked) {
const realPath = archive.copyFileOut(filePath) const realPath = archive.copyFileOut(filePath)
return fs.access(realPath, mode, callback) return fs.access(realPath, mode, callback)
} }
const stats = getOrCreateArchive(asarPath).stat(filePath) const stats = getOrCreateArchive(asarPath).stat(filePath)
if (!stats) { if (!stats) return createError(NOT_FOUND, {asarPath, filePath, callback})
return notFoundError(asarPath, filePath, callback)
} if (mode & fs.constants.W_OK) return createError(NO_ACCESS, {asarPath, filePath, callback})
if (mode & fs.constants.W_OK) {
return accessError(asarPath, filePath, callback) process.nextTick(() => callback())
}
process.nextTick(function () {
callback()
})
} }
const {accessSync} = fs const {accessSync} = fs
fs.accessSync = function (p, mode) { fs.accessSync = function (p, mode) {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return accessSync.apply(this, arguments)
return accessSync.apply(this, arguments) if (mode == null) mode = fs.constants.F_OK
}
if (mode == null) {
mode = fs.constants.F_OK
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) createError(INVALID_ARCHIVE, {asarPath})
invalidArchiveError(asarPath)
}
const info = archive.getFileInfo(filePath) const info = archive.getFileInfo(filePath)
if (!info) { if (!info) createError(NOT_FOUND, {asarPath, filePath})
notFoundError(asarPath, filePath)
}
if (info.unpacked) { if (info.unpacked) {
const realPath = archive.copyFileOut(filePath) const realPath = archive.copyFileOut(filePath)
return fs.accessSync(realPath, mode) return fs.accessSync(realPath, mode)
} }
const stats = getOrCreateArchive(asarPath).stat(filePath) const stats = getOrCreateArchive(asarPath).stat(filePath)
if (!stats) { if (!stats) createError(NOT_FOUND, {asarPath, filePath})
notFoundError(asarPath, filePath) if (mode & fs.constants.W_OK) createError(NO_ACCESS, {asarPath, filePath})
}
if (mode & fs.constants.W_OK) {
accessError(asarPath, filePath)
}
} }
const {readFile} = fs const {readFile} = fs
fs.readFile = function (p, options, callback) { fs.readFile = function (p, options, callback) {
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 = { options = { encoding: null }
encoding: null
}
} else if (typeof options === 'string') { } else if (typeof options === 'string') {
options = { options = { encoding: options }
encoding: options
}
} else if (options === null || options === undefined) { } else if (options === null || options === undefined) {
options = { options = { encoding: null }
encoding: null
}
} else if (typeof options !== 'object') { } else if (typeof options !== 'object') {
throw new TypeError('Bad arguments') throw new TypeError('Bad arguments')
} }
const {encoding} = options
const {encoding} = options
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
return invalidArchiveError(asarPath, callback)
}
const info = archive.getFileInfo(filePath) const info = archive.getFileInfo(filePath)
if (!info) { if (!info) return createError(NOT_FOUND, {asarPath, filePath, callback})
return notFoundError(asarPath, filePath, callback) if (info.size === 0) return process.nextTick(() => callback(null, encoding ? '' : Buffer.alloc(0)))
}
if (info.size === 0) {
return process.nextTick(function () {
callback(null, encoding ? '' : Buffer.alloc(0))
})
}
if (info.unpacked) { if (info.unpacked) {
const realPath = archive.copyFileOut(filePath) const realPath = archive.copyFileOut(filePath)
return fs.readFile(realPath, options, callback) return fs.readFile(realPath, options, callback)
@ -547,158 +440,115 @@
const buffer = Buffer.alloc(info.size) const buffer = Buffer.alloc(info.size)
const fd = archive.getFd() const fd = archive.getFd()
if (!(fd >= 0)) { if (!(fd >= 0)) return createError(NOT_FOUND, {asarPath, filePath, callback})
return notFoundError(asarPath, filePath, callback)
}
logASARAccess(asarPath, filePath, info.offset) logASARAccess(asarPath, filePath, info.offset)
fs.read(fd, buffer, 0, info.size, info.offset, function (error) { fs.read(fd, buffer, 0, info.size, info.offset, error => {
callback(error, encoding ? buffer.toString(encoding) : buffer) callback(error, encoding ? buffer.toString(encoding) : buffer)
}) })
} }
const {readFileSync} = fs const {readFileSync} = fs
fs.readFileSync = function (p, options) { fs.readFileSync = function (p, options) {
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)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) createError(INVALID_ARCHIVE, {asarPath})
invalidArchiveError(asarPath)
}
const info = archive.getFileInfo(filePath) const info = archive.getFileInfo(filePath)
if (!info) { if (!info) createError(NOT_FOUND, {asarPath, filePath})
notFoundError(asarPath, filePath) if (info.size === 0) return (options) ? '' : Buffer.alloc(0)
}
if (info.size === 0) {
if (options) {
return ''
} else {
return Buffer.alloc(0)
}
}
if (info.unpacked) { if (info.unpacked) {
const realPath = archive.copyFileOut(filePath) const 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 (typeof options === 'string') { } else if (typeof options === 'string') {
options = { options = { encoding: options }
encoding: options
}
} else if (typeof options !== 'object') { } else if (typeof options !== 'object') {
throw new TypeError('Bad arguments') throw new TypeError('Bad arguments')
} }
const {encoding} = options const {encoding} = options
const buffer = Buffer.alloc(info.size) const buffer = Buffer.alloc(info.size)
const fd = archive.getFd() const fd = archive.getFd()
if (!(fd >= 0)) { if (!(fd >= 0)) createError(NOT_FOUND, {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) { return (encoding) ? buffer.toString(encoding) : buffer
return buffer.toString(encoding)
} else {
return buffer
}
} }
const {readdir} = fs const {readdir} = fs
fs.readdir = function (p, callback) { fs.readdir = function (p, callback) {
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)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
return invalidArchiveError(asarPath, callback)
}
const files = archive.readdir(filePath) const files = archive.readdir(filePath)
if (!files) { if (!files) return createError(NOT_FOUND, {asarPath, filePath, callback})
return notFoundError(asarPath, filePath, callback)
} process.nextTick(() => callback(null, files))
process.nextTick(function () {
callback(null, files)
})
} }
const {readdirSync} = fs const {readdirSync} = fs
fs.readdirSync = function (p) { fs.readdirSync = function (p) {
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)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) createError(INVALID_ARCHIVE, {asarPath})
invalidArchiveError(asarPath)
}
const files = archive.readdir(filePath) const files = archive.readdir(filePath)
if (!files) { if (!files) createError(NOT_FOUND, {asarPath, filePath})
notFoundError(asarPath, filePath)
}
return files return files
} }
const {internalModuleReadJSON} = process.binding('fs') const {internalModuleReadJSON} = process.binding('fs')
process.binding('fs').internalModuleReadJSON = function (p) { process.binding('fs').internalModuleReadJSON = p => {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return internalModuleReadJSON(p)
return internalModuleReadJSON(p)
}
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) { if (!archive) return
return
}
const info = archive.getFileInfo(filePath) const info = archive.getFileInfo(filePath)
if (!info) { if (!info) return
return if (info.size === 0) return ''
}
if (info.size === 0) {
return ''
}
if (info.unpacked) { if (info.unpacked) {
const realPath = archive.copyFileOut(filePath) const realPath = archive.copyFileOut(filePath)
return fs.readFileSync(realPath, { return fs.readFileSync(realPath, {encoding: 'utf8'})
encoding: 'utf8'
})
} }
const buffer = Buffer.alloc(info.size) const buffer = Buffer.alloc(info.size)
const fd = archive.getFd() const fd = archive.getFd()
if (!(fd >= 0)) { if (!(fd >= 0)) return
return
}
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')
} }
const {internalModuleStat} = process.binding('fs') const {internalModuleStat} = process.binding('fs')
process.binding('fs').internalModuleStat = function (p) { process.binding('fs').internalModuleStat = p => {
const [isAsar, asarPath, filePath] = splitPath(p) const {isAsar, asarPath, filePath} = splitPath(p)
if (!isAsar) { if (!isAsar) return internalModuleStat(p)
return internalModuleStat(p)
} // -ENOENT
const archive = getOrCreateArchive(asarPath) const archive = getOrCreateArchive(asarPath)
if (!archive) return -34
// -ENOENT // -ENOENT
if (!archive) {
return -34
}
const stats = archive.stat(filePath) const stats = archive.stat(filePath)
if (!stats) return -34
// -ENOENT return (stats.isDirectory) ? 1 : 0
if (!stats) {
return -34
}
if (stats.isDirectory) {
return 1
} else {
return 0
}
} }
// Calling mkdir for directory inside asar archive should throw ENOTDIR // Calling mkdir for directory inside asar archive should throw ENOTDIR
@ -707,23 +557,19 @@
// widely used. // widely used.
if (process.platform === 'win32') { if (process.platform === 'win32') {
const {mkdir} = fs const {mkdir} = fs
fs.mkdir = function (p, mode, callback) { fs.mkdir = (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) return createError(NOT_DIR, {callback})
if (isAsar && filePath.length) {
return notDirError(callback)
}
mkdir(p, mode, callback) mkdir(p, mode, callback)
} }
const {mkdirSync} = fs const {mkdirSync} = fs
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) createError(NOT_DIR)
notDirError()
}
return mkdirSync(p, mode) return mkdirSync(p, mode)
} }
} }