chore: clean up asar stuff (#14505)
* chore: reformat code * refactor: getOrCreateArchive() for ASARs - store cached archive in a Map - return `null` instead of `false` on failures * refactor: splitPath() for ASARs - store custom extension in a constant - remove magic numbers - add comments * refactor: explicitly use assert() for a developer error * chore: remove console.log() calls * refactor: replace "p" arguments with "pathArgument" "path" would be a better name, but it is already taken but the "path" Node module. * refactor: createError() for ASARs - return an `Error` instance - use enum for error types - minor improvements * refactor: use more meaningful name for an arg than just "arg"
This commit is contained in:
parent
2d2d1d2090
commit
382afc03ae
1 changed files with 341 additions and 191 deletions
|
@ -1,59 +1,77 @@
|
||||||
(function () {
|
(function () {
|
||||||
const asar = process.binding('atom_common_asar')
|
const asar = process.binding('atom_common_asar')
|
||||||
|
const assert = require('assert')
|
||||||
const {Buffer} = require('buffer')
|
const {Buffer} = require('buffer')
|
||||||
const childProcess = require('child_process')
|
const childProcess = require('child_process')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const util = require('util')
|
const util = require('util')
|
||||||
|
|
||||||
const hasProp = {}.hasOwnProperty
|
const envNoAsar = process.env.ELECTRON_NO_ASAR &&
|
||||||
|
process.type !== 'browser' &&
|
||||||
// Cache asar archive objects.
|
process.type !== 'renderer'
|
||||||
const cachedArchives = {}
|
|
||||||
|
|
||||||
// asar error types
|
|
||||||
const NOT_FOUND = 'NOT_FOUND'
|
|
||||||
const NOT_DIR = 'NOT_DIR'
|
|
||||||
const NO_ACCESS = 'NO_ACCESS'
|
|
||||||
const INVALID_ARCHIVE = 'INVALID_ARCHIVE'
|
|
||||||
|
|
||||||
const envNoAsar = process.env.ELECTRON_NO_ASAR && process.type !== 'browser' && process.type !== 'renderer'
|
|
||||||
const isAsarDisabled = () => process.noAsar || envNoAsar
|
const isAsarDisabled = () => process.noAsar || envNoAsar
|
||||||
|
|
||||||
const getOrCreateArchive = p => {
|
/**
|
||||||
let archive = cachedArchives[p]
|
* @param {!Function} functionToCall
|
||||||
if (archive != null) return archive
|
* @param {!Array|undefined} args
|
||||||
|
*/
|
||||||
|
const nextTick = (functionToCall, args = []) => {
|
||||||
|
process.nextTick(() => functionToCall(...args))
|
||||||
|
}
|
||||||
|
|
||||||
archive = asar.createArchive(p)
|
// Cache asar archive objects.
|
||||||
if (!archive) return false
|
const cachedArchives = new Map()
|
||||||
|
|
||||||
cachedArchives[p] = archive
|
const getOrCreateArchive = archivePath => {
|
||||||
return archive
|
const isCached = cachedArchives.has(archivePath)
|
||||||
|
if (isCached) {
|
||||||
|
return cachedArchives.get(archivePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
const newArchive = asar.createArchive(archivePath)
|
||||||
|
if (!newArchive) return null
|
||||||
|
|
||||||
|
cachedArchives.set(archivePath, newArchive)
|
||||||
|
return newArchive
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean cache on quit.
|
// Clean cache on quit.
|
||||||
process.on('exit', () => {
|
process.on('exit', () => {
|
||||||
for (let p in cachedArchives) {
|
for (const archive of cachedArchives.values()) {
|
||||||
if (!hasProp.call(cachedArchives, p)) continue
|
archive.destroy()
|
||||||
cachedArchives[p].destroy()
|
|
||||||
}
|
}
|
||||||
|
cachedArchives.clear()
|
||||||
})
|
})
|
||||||
|
|
||||||
// Separate asar package's path from full path.
|
const ASAR_EXTENSION = '.asar'
|
||||||
const splitPath = p => {
|
|
||||||
// shortcut to disable asar.
|
|
||||||
if (isAsarDisabled()) return {isAsar: 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: ''}
|
|
||||||
|
|
||||||
p = path.normalize(p)
|
// Separate asar package's path from full path.
|
||||||
const index = p.lastIndexOf(`.asar${path.sep}`)
|
const splitPath = archivePathOrBuffer => {
|
||||||
|
// Shortcut for disabled asar.
|
||||||
|
if (isAsarDisabled()) return {isAsar: false}
|
||||||
|
|
||||||
|
// Check for a bad argument type.
|
||||||
|
let archivePath = archivePathOrBuffer
|
||||||
|
if (Buffer.isBuffer(archivePathOrBuffer)) {
|
||||||
|
archivePath = archivePathOrBuffer.toString()
|
||||||
|
}
|
||||||
|
if (typeof archivePath !== 'string') return {isAsar: false}
|
||||||
|
|
||||||
|
if (archivePath.endsWith(ASAR_EXTENSION)) {
|
||||||
|
return {isAsar: true, asarPath: archivePath, filePath: ''}
|
||||||
|
}
|
||||||
|
|
||||||
|
archivePath = path.normalize(archivePath)
|
||||||
|
const index = archivePath.lastIndexOf(`${ASAR_EXTENSION}${path.sep}`)
|
||||||
if (index === -1) return {isAsar: false}
|
if (index === -1) return {isAsar: false}
|
||||||
|
|
||||||
|
// E.g. for "//some/path/to/archive.asar/then/internal.file"...
|
||||||
return {
|
return {
|
||||||
isAsar: true,
|
isAsar: true,
|
||||||
asarPath: p.substr(0, index + 5),
|
// "//some/path/to/archive.asar"
|
||||||
filePath: p.substr(index + 6)
|
asarPath: archivePath.substr(0, index + ASAR_EXTENSION.length),
|
||||||
|
// "then/internal.file" (with a path separator excluded)
|
||||||
|
filePath: archivePath.substr(index + ASAR_EXTENSION.length + 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,88 +115,107 @@
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const createError = (errorType, {asarPath, filePath, callback} = {}) => {
|
const AsarError = {
|
||||||
|
NOT_FOUND: 'NOT_FOUND',
|
||||||
|
NOT_DIR: 'NOT_DIR',
|
||||||
|
NO_ACCESS: 'NO_ACCESS',
|
||||||
|
INVALID_ARCHIVE: 'INVALID_ARCHIVE'
|
||||||
|
}
|
||||||
|
|
||||||
|
const createError = (errorType, {asarPath, filePath} = {}) => {
|
||||||
let error
|
let error
|
||||||
switch (errorType) {
|
switch (errorType) {
|
||||||
case NOT_FOUND:
|
case AsarError.NOT_FOUND:
|
||||||
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
|
||||||
break
|
break
|
||||||
case NOT_DIR:
|
case AsarError.NOT_DIR:
|
||||||
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
|
||||||
break
|
break
|
||||||
case NO_ACCESS:
|
case AsarError.NO_ACCESS:
|
||||||
error = new Error(`EACCES: permission denied, access '${filePath}'`)
|
error = new Error(`EACCES: permission denied, access '${filePath}'`)
|
||||||
error.code = 'EACCES'
|
error.code = 'EACCES'
|
||||||
error.errno = -13
|
error.errno = -13
|
||||||
break
|
break
|
||||||
case INVALID_ARCHIVE:
|
case AsarError.INVALID_ARCHIVE:
|
||||||
error = new Error(`Invalid package ${asarPath}`)
|
error = new Error(`Invalid package ${asarPath}`)
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
throw new Error('invalid error type')
|
assert.fail(`Invalid error type "${errorType}" passed to createError.`)
|
||||||
}
|
}
|
||||||
if (typeof callback !== 'function') throw error
|
return error
|
||||||
process.nextTick(() => callback(error))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const overrideAPISync = function (module, name, arg) {
|
const overrideAPISync = function (module, name, pathArgumentIndex) {
|
||||||
if (arg == null) arg = 0
|
if (pathArgumentIndex == null) pathArgumentIndex = 0
|
||||||
const old = module[name]
|
const old = module[name]
|
||||||
module[name] = function () {
|
module[name] = function () {
|
||||||
const p = arguments[arg]
|
const pathArgument = arguments[pathArgumentIndex]
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return old.apply(this, arguments)
|
if (!isAsar) return old.apply(this, arguments)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) createError(INVALID_ARCHIVE, {asarPath})
|
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
|
||||||
const newPath = archive.copyFileOut(filePath)
|
const newPath = archive.copyFileOut(filePath)
|
||||||
if (!newPath) createError(NOT_FOUND, {asarPath, filePath})
|
if (!newPath) throw createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
|
||||||
arguments[arg] = newPath
|
arguments[pathArgumentIndex] = newPath
|
||||||
return old.apply(this, arguments)
|
return old.apply(this, arguments)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const overrideAPI = function (module, name, arg) {
|
const overrideAPI = function (module, name, pathArgumentIndex) {
|
||||||
if (arg == null) arg = 0
|
if (pathArgumentIndex == null) pathArgumentIndex = 0
|
||||||
const old = module[name]
|
const old = module[name]
|
||||||
module[name] = function () {
|
module[name] = function () {
|
||||||
const p = arguments[arg]
|
const pathArgument = arguments[pathArgumentIndex]
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
console.log(`${isAsar}, ${asarPath}, ${filePath}`)
|
|
||||||
if (!isAsar) 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') return overrideAPISync(module, name, arg)
|
if (typeof callback !== 'function') {
|
||||||
|
return overrideAPISync(module, name, pathArgumentIndex)
|
||||||
|
}
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
|
if (!archive) {
|
||||||
|
const error = createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const newPath = archive.copyFileOut(filePath)
|
const newPath = archive.copyFileOut(filePath)
|
||||||
if (!newPath) return createError(NOT_FOUND, {asarPath, filePath, callback})
|
if (!newPath) {
|
||||||
|
const error = createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
arguments[arg] = newPath
|
arguments[pathArgumentIndex] = 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 pathArgument = arguments[pathArgumentIndex]
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return old[util.promisify.custom].apply(this, arguments)
|
if (!isAsar) return old[util.promisify.custom].apply(this, arguments)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return new Promise(() => createError(INVALID_ARCHIVE, {asarPath}))
|
if (!archive) {
|
||||||
|
return Promise.reject(createError(AsarError.INVALID_ARCHIVE, {asarPath}))
|
||||||
|
}
|
||||||
|
|
||||||
const newPath = archive.copyFileOut(filePath)
|
const newPath = archive.copyFileOut(filePath)
|
||||||
if (!newPath) return new Promise(() => createError(NOT_FOUND, {asarPath, filePath}))
|
if (!newPath) {
|
||||||
|
return Promise.reject(createError(AsarError.NOT_FOUND, {asarPath, filePath}))
|
||||||
|
}
|
||||||
|
|
||||||
arguments[arg] = newPath
|
arguments[pathArgumentIndex] = newPath
|
||||||
return old[util.promisify.custom].apply(this, arguments)
|
return old[util.promisify.custom].apply(this, arguments)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -194,60 +231,69 @@
|
||||||
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}`)
|
|
||||||
}
|
}
|
||||||
fs.writeSync(logFDs[asarPath], `${offset}: ${filePath}\n`)
|
fs.writeSync(logFDs[asarPath], `${offset}: ${filePath}\n`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {lstatSync} = fs
|
const {lstatSync} = fs
|
||||||
fs.lstatSync = p => {
|
fs.lstatSync = pathArgument => {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return lstatSync(p)
|
if (!isAsar) return lstatSync(pathArgument)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) createError(INVALID_ARCHIVE, {asarPath})
|
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
|
||||||
const stats = archive.stat(filePath)
|
const stats = archive.stat(filePath)
|
||||||
if (!stats) createError(NOT_FOUND, {asarPath, filePath})
|
if (!stats) throw createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
|
||||||
return asarStatsToFsStats(stats)
|
return asarStatsToFsStats(stats)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {lstat} = fs
|
const {lstat} = fs
|
||||||
fs.lstat = (p, callback) => {
|
fs.lstat = (pathArgument, callback) => {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return lstat(p, callback)
|
if (!isAsar) return lstat(pathArgument, callback)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
|
if (!archive) {
|
||||||
|
const error = createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const stats = getOrCreateArchive(asarPath).stat(filePath)
|
const stats = archive.stat(filePath)
|
||||||
if (!stats) return createError(NOT_FOUND, {asarPath, filePath, callback})
|
if (!stats) {
|
||||||
|
const error = createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
process.nextTick(() => callback(null, asarStatsToFsStats(stats)))
|
const fsStats = asarStatsToFsStats(stats)
|
||||||
|
nextTick(callback, [null, fsStats])
|
||||||
}
|
}
|
||||||
|
|
||||||
const {statSync} = fs
|
const {statSync} = fs
|
||||||
fs.statSync = p => {
|
fs.statSync = pathArgument => {
|
||||||
const {isAsar} = splitPath(p)
|
const {isAsar} = splitPath(pathArgument)
|
||||||
if (!isAsar) return statSync(p)
|
if (!isAsar) return statSync(pathArgument)
|
||||||
|
|
||||||
// Do not distinguish links for now.
|
// Do not distinguish links for now.
|
||||||
return fs.lstatSync(p)
|
return fs.lstatSync(pathArgument)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {stat} = fs
|
const {stat} = fs
|
||||||
fs.stat = (p, callback) => {
|
fs.stat = (pathArgument, callback) => {
|
||||||
const {isAsar} = splitPath(p)
|
const {isAsar} = splitPath(pathArgument)
|
||||||
if (!isAsar) return stat(p, callback)
|
if (!isAsar) return stat(pathArgument, callback)
|
||||||
|
|
||||||
// Do not distinguish links for now.
|
// Do not distinguish links for now.
|
||||||
process.nextTick(() => fs.lstat(p, callback))
|
process.nextTick(() => fs.lstat(pathArgument, callback))
|
||||||
}
|
}
|
||||||
|
|
||||||
const {statSyncNoException} = fs
|
const {statSyncNoException} = fs
|
||||||
fs.statSyncNoException = p => {
|
fs.statSyncNoException = pathArgument => {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return statSyncNoException(p)
|
if (!isAsar) return statSyncNoException(pathArgument)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return false
|
if (!archive) return false
|
||||||
|
@ -259,35 +305,43 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const {realpathSync} = fs
|
const {realpathSync} = fs
|
||||||
fs.realpathSync = function (p) {
|
fs.realpathSync = function (pathArgument) {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
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 createError(INVALID_ARCHIVE, {asarPath})
|
if (!archive) {
|
||||||
|
throw createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
const real = archive.realpath(filePath)
|
|
||||||
if (real === false) createError(NOT_FOUND, {asarPath, filePath})
|
|
||||||
|
|
||||||
return path.join(realpathSync(asarPath), real)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.realpathSync.native = function (p) {
|
const fileRealPath = archive.realpath(filePath)
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
if (fileRealPath === false) {
|
||||||
|
throw createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
}
|
||||||
|
|
||||||
|
return path.join(realpathSync(asarPath), fileRealPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.realpathSync.native = function (pathArgument) {
|
||||||
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
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 createError(INVALID_ARCHIVE, {asarPath})
|
if (!archive) {
|
||||||
|
throw createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
}
|
||||||
|
|
||||||
const real = archive.realpath(filePath)
|
const fileRealPath = archive.realpath(filePath)
|
||||||
if (real === false) createError(NOT_FOUND, {asarPath, filePath})
|
if (fileRealPath === false) {
|
||||||
|
throw createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
}
|
||||||
|
|
||||||
return path.join(realpathSync.native(asarPath), real)
|
return path.join(realpathSync.native(asarPath), fileRealPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {realpath} = fs
|
const {realpath} = fs
|
||||||
fs.realpath = function (p, cache, callback) {
|
fs.realpath = function (pathArgument, cache, callback) {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return realpath.apply(this, arguments)
|
if (!isAsar) return realpath.apply(this, arguments)
|
||||||
|
|
||||||
if (typeof cache === 'function') {
|
if (typeof cache === 'function') {
|
||||||
|
@ -296,63 +350,95 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
|
if (!archive) {
|
||||||
|
const error = createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const real = archive.realpath(filePath)
|
const fileRealPath = archive.realpath(filePath)
|
||||||
if (real === false) return createError(NOT_FOUND, {asarPath, filePath, callback})
|
if (fileRealPath === false) {
|
||||||
|
const error = createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return realpath(asarPath, (err, p) => {
|
realpath(asarPath, (error, archiveRealPath) => {
|
||||||
return (err) ? callback(err) : callback(null, path.join(p, real))
|
if (error === null) {
|
||||||
|
const fullPath = path.join(archiveRealPath, fileRealPath)
|
||||||
|
callback(null, fullPath)
|
||||||
|
} else {
|
||||||
|
callback(error)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.realpath.native = function (p, cache, callback) {
|
fs.realpath.native = function (pathArgument, cache, callback) {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return realpath.native.apply(this, arguments)
|
if (!isAsar) return realpath.native.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 createError(INVALID_ARCHIVE, {asarPath, callback})
|
if (!archive) {
|
||||||
|
const error = createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const real = archive.realpath(filePath)
|
const fileRealPath = archive.realpath(filePath)
|
||||||
if (real === false) return createError(NOT_FOUND, {asarPath, filePath, callback})
|
if (fileRealPath === false) {
|
||||||
|
const error = createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return realpath.native(asarPath, (err, p) => {
|
realpath.native(asarPath, (error, archiveRealPath) => {
|
||||||
return (err) ? callback(err) : callback(null, path.join(p, real))
|
if (error === null) {
|
||||||
|
const fullPath = path.join(archiveRealPath, fileRealPath)
|
||||||
|
callback(null, fullPath)
|
||||||
|
} else {
|
||||||
|
callback(error)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const {exists} = fs
|
const {exists} = fs
|
||||||
fs.exists = (p, callback) => {
|
fs.exists = (pathArgument, callback) => {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return exists(p, callback)
|
if (!isAsar) return exists(pathArgument, callback)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
|
if (!archive) {
|
||||||
|
const error = createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
// Disabled due to false positive in StandardJS
|
nextTick(callback, [error])
|
||||||
// eslint-disable-next-line standard/no-callback-literal
|
return
|
||||||
process.nextTick(() => callback(archive.stat(filePath) !== false))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.exists[util.promisify.custom] = p => {
|
const pathExists = (archive.stat(filePath) !== false)
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
nextTick(callback, [pathExists])
|
||||||
if (!isAsar) return exists[util.promisify.custom](p)
|
}
|
||||||
|
|
||||||
|
fs.exists[util.promisify.custom] = pathArgument => {
|
||||||
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
|
if (!isAsar) return exists[util.promisify.custom](pathArgument)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return new Promise(() => createError(INVALID_ARCHIVE, {asarPath}))
|
if (!archive) {
|
||||||
|
const error = createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
return Promise.reject(error)
|
||||||
|
}
|
||||||
|
|
||||||
return Promise.resolve(archive.stat(filePath) !== false)
|
return Promise.resolve(archive.stat(filePath) !== false)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {existsSync} = fs
|
const {existsSync} = fs
|
||||||
fs.existsSync = p => {
|
fs.existsSync = pathArgument => {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return existsSync(p)
|
if (!isAsar) return existsSync(pathArgument)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return false
|
if (!archive) return false
|
||||||
|
@ -361,8 +447,8 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const {access} = fs
|
const {access} = fs
|
||||||
fs.access = function (p, mode, callback) {
|
fs.access = function (pathArgument, mode, callback) {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return access.apply(this, arguments)
|
if (!isAsar) return access.apply(this, arguments)
|
||||||
|
|
||||||
if (typeof mode === 'function') {
|
if (typeof mode === 'function') {
|
||||||
|
@ -371,48 +457,75 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
|
if (!archive) {
|
||||||
|
const error = createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const info = archive.getFileInfo(filePath)
|
const info = archive.getFileInfo(filePath)
|
||||||
if (!info) return createError(NOT_FOUND, {asarPath, filePath, callback})
|
if (!info) {
|
||||||
|
const error = createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
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 = archive.stat(filePath)
|
||||||
if (!stats) return createError(NOT_FOUND, {asarPath, filePath, callback})
|
if (!stats) {
|
||||||
|
const error = createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (mode & fs.constants.W_OK) return createError(NO_ACCESS, {asarPath, filePath, callback})
|
if (mode & fs.constants.W_OK) {
|
||||||
|
const error = createError(AsarError.NO_ACCESS, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
process.nextTick(() => callback())
|
nextTick(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {accessSync} = fs
|
const {accessSync} = fs
|
||||||
fs.accessSync = function (p, mode) {
|
fs.accessSync = function (pathArgument, mode) {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return accessSync.apply(this, arguments)
|
if (!isAsar) 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) createError(INVALID_ARCHIVE, {asarPath})
|
if (!archive) {
|
||||||
|
throw createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
}
|
||||||
|
|
||||||
const info = archive.getFileInfo(filePath)
|
const info = archive.getFileInfo(filePath)
|
||||||
if (!info) createError(NOT_FOUND, {asarPath, filePath})
|
if (!info) {
|
||||||
|
throw createError(AsarError.NOT_FOUND, {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 = archive.stat(filePath)
|
||||||
if (!stats) createError(NOT_FOUND, {asarPath, filePath})
|
if (!stats) {
|
||||||
if (mode & fs.constants.W_OK) createError(NO_ACCESS, {asarPath, filePath})
|
throw createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mode & fs.constants.W_OK) {
|
||||||
|
throw createError(AsarError.NO_ACCESS, {asarPath, filePath})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const {readFile} = fs
|
const {readFile} = fs
|
||||||
fs.readFile = function (p, options, callback) {
|
fs.readFile = function (pathArgument, options, callback) {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return readFile.apply(this, arguments)
|
if (!isAsar) return readFile.apply(this, arguments)
|
||||||
|
|
||||||
if (typeof options === 'function') {
|
if (typeof options === 'function') {
|
||||||
|
@ -428,11 +541,24 @@
|
||||||
|
|
||||||
const {encoding} = options
|
const {encoding} = options
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
|
if (!archive) {
|
||||||
|
const error = createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const info = archive.getFileInfo(filePath)
|
const info = archive.getFileInfo(filePath)
|
||||||
if (!info) return createError(NOT_FOUND, {asarPath, filePath, callback})
|
if (!info) {
|
||||||
if (info.size === 0) return process.nextTick(() => callback(null, encoding ? '' : Buffer.alloc(0)))
|
const error = createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (info.size === 0) {
|
||||||
|
nextTick(callback, [null, encoding ? '' : Buffer.alloc(0)])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
@ -440,7 +566,11 @@
|
||||||
|
|
||||||
const buffer = Buffer.alloc(info.size)
|
const buffer = Buffer.alloc(info.size)
|
||||||
const fd = archive.getFd()
|
const fd = archive.getFd()
|
||||||
if (!(fd >= 0)) return createError(NOT_FOUND, {asarPath, filePath, callback})
|
if (!(fd >= 0)) {
|
||||||
|
const error = createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
logASARAccess(asarPath, filePath, info.offset)
|
logASARAccess(asarPath, filePath, info.offset)
|
||||||
fs.read(fd, buffer, 0, info.size, info.offset, error => {
|
fs.read(fd, buffer, 0, info.size, info.offset, error => {
|
||||||
|
@ -449,15 +579,16 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const {readFileSync} = fs
|
const {readFileSync} = fs
|
||||||
fs.readFileSync = function (p, options) {
|
fs.readFileSync = function (pathArgument, options) {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return readFileSync.apply(this, arguments)
|
if (!isAsar) return readFileSync.apply(this, arguments)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) createError(INVALID_ARCHIVE, {asarPath})
|
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
|
||||||
const info = archive.getFileInfo(filePath)
|
const info = archive.getFileInfo(filePath)
|
||||||
if (!info) createError(NOT_FOUND, {asarPath, filePath})
|
if (!info) throw createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
|
||||||
if (info.size === 0) return (options) ? '' : Buffer.alloc(0)
|
if (info.size === 0) return (options) ? '' : Buffer.alloc(0)
|
||||||
if (info.unpacked) {
|
if (info.unpacked) {
|
||||||
const realPath = archive.copyFileOut(filePath)
|
const realPath = archive.copyFileOut(filePath)
|
||||||
|
@ -475,7 +606,7 @@
|
||||||
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)) createError(NOT_FOUND, {asarPath, filePath})
|
if (!(fd >= 0)) throw createError(AsarError.NOT_FOUND, {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)
|
||||||
|
@ -483,37 +614,49 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const {readdir} = fs
|
const {readdir} = fs
|
||||||
fs.readdir = function (p, callback) {
|
fs.readdir = function (pathArgument, callback) {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return readdir.apply(this, arguments)
|
if (!isAsar) return readdir.apply(this, arguments)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return createError(INVALID_ARCHIVE, {asarPath, callback})
|
if (!archive) {
|
||||||
|
const error = createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const files = archive.readdir(filePath)
|
const files = archive.readdir(filePath)
|
||||||
if (!files) return createError(NOT_FOUND, {asarPath, filePath, callback})
|
if (!files) {
|
||||||
|
const error = createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
process.nextTick(() => callback(null, files))
|
nextTick(callback, [null, files])
|
||||||
}
|
}
|
||||||
|
|
||||||
const {readdirSync} = fs
|
const {readdirSync} = fs
|
||||||
fs.readdirSync = function (p) {
|
fs.readdirSync = function (pathArgument) {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return readdirSync.apply(this, arguments)
|
if (!isAsar) return readdirSync.apply(this, arguments)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) createError(INVALID_ARCHIVE, {asarPath})
|
if (!archive) {
|
||||||
|
throw createError(AsarError.INVALID_ARCHIVE, {asarPath})
|
||||||
|
}
|
||||||
|
|
||||||
const files = archive.readdir(filePath)
|
const files = archive.readdir(filePath)
|
||||||
if (!files) createError(NOT_FOUND, {asarPath, filePath})
|
if (!files) {
|
||||||
|
throw createError(AsarError.NOT_FOUND, {asarPath, filePath})
|
||||||
|
}
|
||||||
|
|
||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
const {internalModuleReadJSON} = process.binding('fs')
|
const {internalModuleReadJSON} = process.binding('fs')
|
||||||
process.binding('fs').internalModuleReadJSON = p => {
|
process.binding('fs').internalModuleReadJSON = pathArgument => {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return internalModuleReadJSON(p)
|
if (!isAsar) return internalModuleReadJSON(pathArgument)
|
||||||
|
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
if (!archive) return
|
if (!archive) return
|
||||||
|
@ -536,9 +679,9 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
const {internalModuleStat} = process.binding('fs')
|
const {internalModuleStat} = process.binding('fs')
|
||||||
process.binding('fs').internalModuleStat = p => {
|
process.binding('fs').internalModuleStat = pathArgument => {
|
||||||
const {isAsar, asarPath, filePath} = splitPath(p)
|
const {isAsar, asarPath, filePath} = splitPath(pathArgument)
|
||||||
if (!isAsar) return internalModuleStat(p)
|
if (!isAsar) return internalModuleStat(pathArgument)
|
||||||
|
|
||||||
// -ENOENT
|
// -ENOENT
|
||||||
const archive = getOrCreateArchive(asarPath)
|
const archive = getOrCreateArchive(asarPath)
|
||||||
|
@ -557,20 +700,27 @@
|
||||||
// widely used.
|
// widely used.
|
||||||
if (process.platform === 'win32') {
|
if (process.platform === 'win32') {
|
||||||
const {mkdir} = fs
|
const {mkdir} = fs
|
||||||
fs.mkdir = (p, mode, callback) => {
|
fs.mkdir = (pathArgument, mode, callback) => {
|
||||||
if (typeof mode === 'function') callback = mode
|
if (typeof mode === 'function') {
|
||||||
|
callback = mode
|
||||||
|
mode = undefined
|
||||||
|
}
|
||||||
|
|
||||||
const {isAsar, filePath} = splitPath(p)
|
const {isAsar, filePath} = splitPath(pathArgument)
|
||||||
if (isAsar && filePath.length) return createError(NOT_DIR, {callback})
|
if (isAsar && filePath.length > 0) {
|
||||||
|
const error = createError(AsarError.NOT_DIR)
|
||||||
|
nextTick(callback, [error])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
mkdir(p, mode, callback)
|
mkdir(pathArgument, mode, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {mkdirSync} = fs
|
const {mkdirSync} = fs
|
||||||
fs.mkdirSync = function (p, mode) {
|
fs.mkdirSync = function (pathArgument, mode) {
|
||||||
const {isAsar, filePath} = splitPath(p)
|
const {isAsar, filePath} = splitPath(pathArgument)
|
||||||
if (isAsar && filePath.length) createError(NOT_DIR)
|
if (isAsar && filePath.length) throw createError(AsarError.NOT_DIR)
|
||||||
return mkdirSync(p, mode)
|
return mkdirSync(pathArgument, mode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue