2018-09-22 12:28:50 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-01-12 01:39:11 +00:00
|
|
|
(function () {
|
2016-03-25 19:50:43 +00:00
|
|
|
const asar = process.binding('atom_common_asar')
|
2018-09-10 15:29:19 +00:00
|
|
|
const assert = require('assert')
|
2018-09-13 16:10:51 +00:00
|
|
|
const { Buffer } = require('buffer')
|
2016-05-25 23:20:49 +00:00
|
|
|
const childProcess = require('child_process')
|
2016-03-25 19:50:43 +00:00
|
|
|
const path = require('path')
|
2018-08-01 03:06:48 +00:00
|
|
|
const util = require('util')
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const envNoAsar = process.env.ELECTRON_NO_ASAR &&
|
|
|
|
process.type !== 'browser' &&
|
|
|
|
process.type !== 'renderer'
|
|
|
|
const isAsarDisabled = () => process.noAsar || envNoAsar
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
/**
|
|
|
|
* @param {!Function} functionToCall
|
|
|
|
* @param {!Array|undefined} args
|
|
|
|
*/
|
|
|
|
const nextTick = (functionToCall, args = []) => {
|
|
|
|
process.nextTick(() => functionToCall(...args))
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
// Cache asar archive objects.
|
|
|
|
const cachedArchives = new Map()
|
2016-10-07 17:59:34 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const getOrCreateArchive = archivePath => {
|
|
|
|
const isCached = cachedArchives.has(archivePath)
|
|
|
|
if (isCached) {
|
|
|
|
return cachedArchives.get(archivePath)
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const newArchive = asar.createArchive(archivePath)
|
|
|
|
if (!newArchive) return null
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
cachedArchives.set(archivePath, newArchive)
|
|
|
|
return newArchive
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Clean cache on quit.
|
2018-09-08 02:23:47 +00:00
|
|
|
process.on('exit', () => {
|
2018-09-10 15:29:19 +00:00
|
|
|
for (const archive of cachedArchives.values()) {
|
|
|
|
archive.destroy()
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2018-09-10 15:29:19 +00:00
|
|
|
cachedArchives.clear()
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const ASAR_EXTENSION = '.asar'
|
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Separate asar package's path from full path.
|
2018-09-10 15:29:19 +00:00
|
|
|
const splitPath = archivePathOrBuffer => {
|
|
|
|
// Shortcut for disabled asar.
|
2018-09-13 16:10:51 +00:00
|
|
|
if (isAsarDisabled()) return { isAsar: false }
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
// Check for a bad argument type.
|
|
|
|
let archivePath = archivePathOrBuffer
|
|
|
|
if (Buffer.isBuffer(archivePathOrBuffer)) {
|
|
|
|
archivePath = archivePathOrBuffer.toString()
|
|
|
|
}
|
2018-09-13 16:10:51 +00:00
|
|
|
if (typeof archivePath !== 'string') return { isAsar: false }
|
2018-09-10 15:29:19 +00:00
|
|
|
|
|
|
|
if (archivePath.endsWith(ASAR_EXTENSION)) {
|
2018-09-13 16:10:51 +00:00
|
|
|
return { isAsar: true, asarPath: archivePath, filePath: '' }
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
archivePath = path.normalize(archivePath)
|
|
|
|
const index = archivePath.lastIndexOf(`${ASAR_EXTENSION}${path.sep}`)
|
2018-09-13 16:10:51 +00:00
|
|
|
if (index === -1) return { isAsar: false }
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
// E.g. for "//some/path/to/archive.asar/then/internal.file"...
|
2018-09-08 02:23:47 +00:00
|
|
|
return {
|
|
|
|
isAsar: true,
|
2018-09-10 15:29:19 +00:00
|
|
|
// "//some/path/to/archive.asar"
|
|
|
|
asarPath: archivePath.substr(0, index + ASAR_EXTENSION.length),
|
|
|
|
// "then/internal.file" (with a path separator excluded)
|
|
|
|
filePath: archivePath.substr(index + ASAR_EXTENSION.length + 1)
|
2016-01-12 02:40:23 +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
|
|
|
// Convert asar archive's Stats object to fs's Stats object.
|
2016-07-25 17:07:39 +00:00
|
|
|
let nextInode = 0
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const uid = process.getuid != null ? process.getuid() : 0
|
|
|
|
const gid = process.getgid != null ? process.getgid() : 0
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const fakeTime = new Date()
|
2018-08-12 06:20:33 +00:00
|
|
|
const msec = (date) => (date || fakeTime).getTime()
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const asarStatsToFsStats = function (stats) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { Stats, constants } = require('fs')
|
2018-08-12 06:20:33 +00:00
|
|
|
|
|
|
|
let mode = constants.S_IROTH ^ constants.S_IRGRP ^ constants.S_IRUSR ^ constants.S_IWUSR
|
|
|
|
|
|
|
|
if (stats.isFile) {
|
|
|
|
mode ^= constants.S_IFREG
|
|
|
|
} else if (stats.isDirectory) {
|
|
|
|
mode ^= constants.S_IFDIR
|
|
|
|
} else if (stats.isLink) {
|
|
|
|
mode ^= constants.S_IFLNK
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2018-08-12 06:20:33 +00:00
|
|
|
|
|
|
|
return new Stats(
|
2018-09-13 16:10:51 +00:00
|
|
|
1, // dev
|
|
|
|
mode, // mode
|
|
|
|
1, // nlink
|
|
|
|
uid,
|
|
|
|
gid,
|
|
|
|
0, // rdev
|
|
|
|
undefined, // blksize
|
|
|
|
++nextInode, // ino
|
|
|
|
stats.size,
|
|
|
|
undefined, // blocks,
|
|
|
|
msec(stats.atime), // atim_msec
|
|
|
|
msec(stats.mtime), // mtim_msec
|
|
|
|
msec(stats.ctime), // ctim_msec
|
|
|
|
msec(stats.birthtime) // birthtim_msec
|
2018-08-12 06:20:33 +00:00
|
|
|
)
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const AsarError = {
|
|
|
|
NOT_FOUND: 'NOT_FOUND',
|
|
|
|
NOT_DIR: 'NOT_DIR',
|
|
|
|
NO_ACCESS: 'NO_ACCESS',
|
|
|
|
INVALID_ARCHIVE: 'INVALID_ARCHIVE'
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const createError = (errorType, { asarPath, filePath } = {}) => {
|
2018-09-08 02:23:47 +00:00
|
|
|
let error
|
|
|
|
switch (errorType) {
|
2018-09-10 15:29:19 +00:00
|
|
|
case AsarError.NOT_FOUND:
|
2018-09-08 02:23:47 +00:00
|
|
|
error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`)
|
|
|
|
error.code = 'ENOENT'
|
|
|
|
error.errno = -2
|
|
|
|
break
|
2018-09-10 15:29:19 +00:00
|
|
|
case AsarError.NOT_DIR:
|
2018-09-08 02:23:47 +00:00
|
|
|
error = new Error('ENOTDIR, not a directory')
|
|
|
|
error.code = 'ENOTDIR'
|
|
|
|
error.errno = -20
|
|
|
|
break
|
2018-09-10 15:29:19 +00:00
|
|
|
case AsarError.NO_ACCESS:
|
2018-09-08 02:23:47 +00:00
|
|
|
error = new Error(`EACCES: permission denied, access '${filePath}'`)
|
|
|
|
error.code = 'EACCES'
|
|
|
|
error.errno = -13
|
|
|
|
break
|
2018-09-10 15:29:19 +00:00
|
|
|
case AsarError.INVALID_ARCHIVE:
|
2018-09-08 02:23:47 +00:00
|
|
|
error = new Error(`Invalid package ${asarPath}`)
|
|
|
|
break
|
|
|
|
default:
|
2018-09-10 15:29:19 +00:00
|
|
|
assert.fail(`Invalid error type "${errorType}" passed to createError.`)
|
2018-09-08 02:23:47 +00:00
|
|
|
}
|
2018-09-10 15:29:19 +00:00
|
|
|
return error
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const overrideAPISync = function (module, name, pathArgumentIndex) {
|
|
|
|
if (pathArgumentIndex == null) pathArgumentIndex = 0
|
2016-07-25 17:07:39 +00:00
|
|
|
const old = module[name]
|
2016-03-25 19:50:43 +00:00
|
|
|
module[name] = function () {
|
2018-09-10 15:29:19 +00:00
|
|
|
const pathArgument = arguments[pathArgumentIndex]
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!isAsar) return old.apply(this, arguments)
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-13 16:10:51 +00:00
|
|
|
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const newPath = archive.copyFileOut(filePath)
|
2018-09-13 16:10:51 +00:00
|
|
|
if (!newPath) throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
arguments[pathArgumentIndex] = newPath
|
2016-03-25 19:50:43 +00:00
|
|
|
return old.apply(this, arguments)
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const overrideAPI = function (module, name, pathArgumentIndex) {
|
|
|
|
if (pathArgumentIndex == null) pathArgumentIndex = 0
|
2016-07-25 17:07:39 +00:00
|
|
|
const old = module[name]
|
2016-03-25 19:50:43 +00:00
|
|
|
module[name] = function () {
|
2018-09-10 15:29:19 +00:00
|
|
|
const pathArgument = arguments[pathArgumentIndex]
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!isAsar) return old.apply(this, arguments)
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const callback = arguments[arguments.length - 1]
|
2018-09-10 15:29:19 +00:00
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
return overrideAPISync(module, name, pathArgumentIndex)
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
|
|
|
const newPath = archive.copyFileOut(filePath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!newPath) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
arguments[pathArgumentIndex] = newPath
|
2016-03-25 19:50:43 +00:00
|
|
|
return old.apply(this, arguments)
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-08-01 03:06:48 +00:00
|
|
|
if (old[util.promisify.custom]) {
|
|
|
|
module[name][util.promisify.custom] = function () {
|
2018-09-10 15:29:19 +00:00
|
|
|
const pathArgument = arguments[pathArgumentIndex]
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!isAsar) return old[util.promisify.custom].apply(this, arguments)
|
2018-08-01 03:06:48 +00:00
|
|
|
|
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
return Promise.reject(createError(AsarError.INVALID_ARCHIVE, { asarPath }))
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2018-08-01 03:06:48 +00:00
|
|
|
|
|
|
|
const newPath = archive.copyFileOut(filePath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!newPath) {
|
2018-09-13 16:10:51 +00:00
|
|
|
return Promise.reject(createError(AsarError.NOT_FOUND, { asarPath, filePath }))
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2018-08-01 03:06:48 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
arguments[pathArgumentIndex] = newPath
|
2018-08-01 03:06:48 +00:00
|
|
|
return old[util.promisify.custom].apply(this, arguments)
|
|
|
|
}
|
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-01-19 18:25:03 +00:00
|
|
|
|
|
|
|
// Override fs APIs.
|
2018-09-08 02:23:47 +00:00
|
|
|
exports.wrapFsWithAsar = fs => {
|
2016-07-25 17:07:39 +00:00
|
|
|
const logFDs = {}
|
2018-09-08 02:23:47 +00:00
|
|
|
const logASARAccess = (asarPath, filePath, offset) => {
|
|
|
|
if (!process.env.ELECTRON_LOG_ASAR_READS) return
|
2015-12-22 19:20:50 +00:00
|
|
|
if (!logFDs[asarPath]) {
|
2016-03-25 19:50:43 +00:00
|
|
|
const path = require('path')
|
2018-09-08 02:23:47 +00:00
|
|
|
const logFilename = `${path.basename(asarPath, '.asar')}-access-log.txt`
|
2016-07-25 17:07:39 +00:00
|
|
|
const logPath = path.join(require('os').tmpdir(), logFilename)
|
2016-03-25 19:50:43 +00:00
|
|
|
logFDs[asarPath] = fs.openSync(logPath, 'a')
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
fs.writeSync(logFDs[asarPath], `${offset}: ${filePath}\n`)
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2015-12-22 19:20:50 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { lstatSync } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.lstatSync = pathArgument => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!isAsar) return lstatSync(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-13 16:10:51 +00:00
|
|
|
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const stats = archive.stat(filePath)
|
2018-09-13 16:10:51 +00:00
|
|
|
if (!stats) throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
return asarStatsToFsStats(stats)
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { lstat } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.lstat = (pathArgument, callback) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!isAsar) return lstat(pathArgument, callback)
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const stats = archive.stat(filePath)
|
|
|
|
if (!stats) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const fsStats = asarStatsToFsStats(stats)
|
|
|
|
nextTick(callback, [null, fsStats])
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { statSync } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.statSync = pathArgument => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!isAsar) return statSync(pathArgument)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Do not distinguish links for now.
|
2018-09-10 15:29:19 +00:00
|
|
|
return fs.lstatSync(pathArgument)
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { stat } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.stat = (pathArgument, callback) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!isAsar) return stat(pathArgument, callback)
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
// Do not distinguish links for now.
|
2018-09-10 15:29:19 +00:00
|
|
|
process.nextTick(() => fs.lstat(pathArgument, callback))
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { realpathSync } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.realpathSync = function (pathArgument) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-08-12 07:10:55 +00:00
|
|
|
if (!isAsar) return realpathSync.apply(this, arguments)
|
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const fileRealPath = archive.realpath(filePath)
|
|
|
|
if (fileRealPath === false) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
return path.join(realpathSync(asarPath), fileRealPath)
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.realpathSync.native = function (pathArgument) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-08-12 07:10:55 +00:00
|
|
|
if (!isAsar) return realpathSync.native.apply(this, arguments)
|
|
|
|
|
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const fileRealPath = archive.realpath(filePath)
|
|
|
|
if (fileRealPath === false) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
return path.join(realpathSync.native(asarPath), fileRealPath)
|
2018-08-12 07:10:55 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { realpath } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.realpath = function (pathArgument, cache, callback) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-08-12 07:10:55 +00:00
|
|
|
if (!isAsar) 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
|
2018-09-08 02:23:47 +00:00
|
|
|
cache = undefined
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const fileRealPath = archive.realpath(filePath)
|
|
|
|
if (fileRealPath === false) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
realpath(asarPath, (error, archiveRealPath) => {
|
|
|
|
if (error === null) {
|
|
|
|
const fullPath = path.join(archiveRealPath, fileRealPath)
|
|
|
|
callback(null, fullPath)
|
|
|
|
} else {
|
|
|
|
callback(error)
|
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.realpath.native = function (pathArgument, cache, callback) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-08-12 07:10:55 +00:00
|
|
|
if (!isAsar) return realpath.native.apply(this, arguments)
|
|
|
|
|
|
|
|
if (typeof cache === 'function') {
|
|
|
|
callback = cache
|
2018-09-10 15:29:19 +00:00
|
|
|
cache = undefined
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const fileRealPath = archive.realpath(filePath)
|
|
|
|
if (fileRealPath === false) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-08-12 07:10:55 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
realpath.native(asarPath, (error, archiveRealPath) => {
|
|
|
|
if (error === null) {
|
|
|
|
const fullPath = path.join(archiveRealPath, fileRealPath)
|
|
|
|
callback(null, fullPath)
|
|
|
|
} else {
|
|
|
|
callback(error)
|
|
|
|
}
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { exists } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.exists = (pathArgument, callback) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!isAsar) return exists(pathArgument, callback)
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const pathExists = (archive.stat(filePath) !== false)
|
|
|
|
nextTick(callback, [pathExists])
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.exists[util.promisify.custom] = pathArgument => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!isAsar) return exists[util.promisify.custom](pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-08-01 03:06:48 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
return Promise.reject(error)
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-08-01 03:06:48 +00:00
|
|
|
return Promise.resolve(archive.stat(filePath) !== false)
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { existsSync } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.existsSync = pathArgument => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!isAsar) return existsSync(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!archive) return false
|
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
return archive.stat(filePath) !== false
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { access } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.access = function (pathArgument, mode, callback) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!isAsar) return access.apply(this, arguments)
|
|
|
|
|
2016-07-25 17:49:20 +00:00
|
|
|
if (typeof mode === 'function') {
|
|
|
|
callback = mode
|
|
|
|
mode = fs.constants.F_OK
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:49:20 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:49:20 +00:00
|
|
|
const info = archive.getFileInfo(filePath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!info) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:49:20 +00:00
|
|
|
if (info.unpacked) {
|
|
|
|
const realPath = archive.copyFileOut(filePath)
|
|
|
|
return fs.access(realPath, mode, callback)
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const stats = archive.stat(filePath)
|
|
|
|
if (!stats) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
if (mode & fs.constants.W_OK) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NO_ACCESS, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback)
|
2016-07-25 17:49:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { accessSync } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.accessSync = function (pathArgument, mode) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!isAsar) return accessSync.apply(this, arguments)
|
2018-09-10 15:29:19 +00:00
|
|
|
|
2018-09-08 02:23:47 +00:00
|
|
|
if (mode == null) mode = fs.constants.F_OK
|
|
|
|
|
2016-07-25 18:05:18 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 18:05:18 +00:00
|
|
|
const info = archive.getFileInfo(filePath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!info) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 18:05:18 +00:00
|
|
|
if (info.unpacked) {
|
|
|
|
const realPath = archive.copyFileOut(filePath)
|
|
|
|
return fs.accessSync(realPath, mode)
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
const stats = archive.stat(filePath)
|
|
|
|
if (!stats) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mode & fs.constants.W_OK) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.NO_ACCESS, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2016-07-25 18:05:18 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { readFile } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.readFile = function (pathArgument, options, callback) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!isAsar) 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
|
2018-09-08 02:23:47 +00:00
|
|
|
options = { encoding: null }
|
2017-11-20 17:40:47 +00:00
|
|
|
} else if (typeof options === 'string') {
|
2018-09-08 02:23:47 +00:00
|
|
|
options = { encoding: options }
|
2017-11-20 16:20:20 +00:00
|
|
|
} else if (options === null || options === undefined) {
|
2018-09-08 02:23:47 +00:00
|
|
|
options = { encoding: null }
|
2017-11-20 17:40:47 +00:00
|
|
|
} else if (typeof options !== 'object') {
|
2017-07-31 01:32:45 +00:00
|
|
|
throw new TypeError('Bad arguments')
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2017-07-31 01:32:45 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { encoding } = options
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const info = archive.getFileInfo(filePath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!info) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.size === 0) {
|
|
|
|
nextTick(callback, [null, encoding ? '' : Buffer.alloc(0)])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
if (info.unpacked) {
|
2016-07-25 17:07:39 +00:00
|
|
|
const realPath = archive.copyFileOut(filePath)
|
2016-03-25 19:50:43 +00:00
|
|
|
return fs.readFile(realPath, options, callback)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2017-08-01 09:52:48 +00:00
|
|
|
|
2017-11-23 21:41:47 +00:00
|
|
|
const buffer = Buffer.alloc(info.size)
|
2016-07-25 17:07:39 +00:00
|
|
|
const fd = archive.getFd()
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!(fd >= 0)) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
logASARAccess(asarPath, filePath, info.offset)
|
2018-09-08 02:23:47 +00:00
|
|
|
fs.read(fd, buffer, 0, info.size, info.offset, error => {
|
2016-07-25 17:07:39 +00:00
|
|
|
callback(error, encoding ? buffer.toString(encoding) : buffer)
|
2016-03-25 19:50:43 +00:00
|
|
|
})
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { readFileSync } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.readFileSync = function (pathArgument, options) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!isAsar) return readFileSync.apply(this, arguments)
|
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-13 16:10:51 +00:00
|
|
|
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const info = archive.getFileInfo(filePath)
|
2018-09-13 16:10:51 +00:00
|
|
|
if (!info) throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
|
2018-09-08 02:23:47 +00:00
|
|
|
if (info.size === 0) return (options) ? '' : Buffer.alloc(0)
|
2016-01-19 18:25:03 +00:00
|
|
|
if (info.unpacked) {
|
2016-07-25 17:07:39 +00:00
|
|
|
const realPath = archive.copyFileOut(filePath)
|
2016-03-25 19:50:43 +00:00
|
|
|
return fs.readFileSync(realPath, options)
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-01-19 18:25:03 +00:00
|
|
|
if (!options) {
|
2018-09-08 02:23:47 +00:00
|
|
|
options = { encoding: null }
|
2017-11-20 17:40:47 +00:00
|
|
|
} else if (typeof options === 'string') {
|
2018-09-08 02:23:47 +00:00
|
|
|
options = { encoding: options }
|
2017-11-20 17:40:47 +00:00
|
|
|
} else if (typeof options !== 'object') {
|
2016-03-25 19:50:43 +00:00
|
|
|
throw new TypeError('Bad arguments')
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { encoding } = options
|
2017-11-23 21:41:47 +00:00
|
|
|
const buffer = Buffer.alloc(info.size)
|
2016-07-25 17:07:39 +00:00
|
|
|
const fd = archive.getFd()
|
2018-09-13 16:10:51 +00:00
|
|
|
if (!(fd >= 0)) throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
logASARAccess(asarPath, filePath, info.offset)
|
|
|
|
fs.readSync(fd, buffer, 0, info.size, info.offset)
|
2018-09-08 02:23:47 +00:00
|
|
|
return (encoding) ? buffer.toString(encoding) : buffer
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { readdir } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.readdir = function (pathArgument, callback) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!isAsar) return readdir.apply(this, arguments)
|
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const files = archive.readdir(filePath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!files) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2018-09-10 15:29:19 +00:00
|
|
|
nextTick(callback, [null, files])
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { readdirSync } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.readdirSync = function (pathArgument) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!isAsar) return readdirSync.apply(this, arguments)
|
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!archive) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.INVALID_ARCHIVE, { asarPath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const files = archive.readdir(filePath)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!files) {
|
2018-09-13 16:10:51 +00:00
|
|
|
throw createError(AsarError.NOT_FOUND, { asarPath, filePath })
|
2018-09-10 15:29:19 +00:00
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
return files
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { internalModuleReadJSON } = process.binding('fs')
|
2018-09-10 15:29:19 +00:00
|
|
|
process.binding('fs').internalModuleReadJSON = pathArgument => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!isAsar) return internalModuleReadJSON(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!archive) return
|
|
|
|
|
2016-07-25 17:07:39 +00:00
|
|
|
const info = archive.getFileInfo(filePath)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!info) return
|
|
|
|
if (info.size === 0) return ''
|
2016-01-19 18:25:03 +00:00
|
|
|
if (info.unpacked) {
|
2016-07-25 17:07:39 +00:00
|
|
|
const realPath = archive.copyFileOut(filePath)
|
2018-09-13 16:10:51 +00:00
|
|
|
return fs.readFileSync(realPath, { encoding: 'utf8' })
|
2016-01-19 18:25:03 +00:00
|
|
|
}
|
2018-09-08 02:23:47 +00:00
|
|
|
|
2017-11-23 21:41:47 +00:00
|
|
|
const buffer = Buffer.alloc(info.size)
|
2016-07-25 17:07:39 +00:00
|
|
|
const fd = archive.getFd()
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!(fd >= 0)) return
|
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
logASARAccess(asarPath, filePath, info.offset)
|
|
|
|
fs.readSync(fd, buffer, 0, info.size, info.offset)
|
|
|
|
return buffer.toString('utf8')
|
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { internalModuleStat } = process.binding('fs')
|
2018-09-10 15:29:19 +00:00
|
|
|
process.binding('fs').internalModuleStat = pathArgument => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, asarPath, filePath } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (!isAsar) return internalModuleStat(pathArgument)
|
2018-09-08 02:23:47 +00:00
|
|
|
|
|
|
|
// -ENOENT
|
2016-07-25 17:07:39 +00:00
|
|
|
const archive = getOrCreateArchive(asarPath)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!archive) return -34
|
2016-01-19 18:25:03 +00:00
|
|
|
|
|
|
|
// -ENOENT
|
2016-07-25 17:07:39 +00:00
|
|
|
const stats = archive.stat(filePath)
|
2018-09-08 02:23:47 +00:00
|
|
|
if (!stats) return -34
|
2016-01-19 18:25:03 +00:00
|
|
|
|
2018-09-08 02:23:47 +00:00
|
|
|
return (stats.isDirectory) ? 1 : 0
|
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') {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { mkdir } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.mkdir = (pathArgument, mode, callback) => {
|
|
|
|
if (typeof mode === 'function') {
|
|
|
|
callback = mode
|
|
|
|
mode = undefined
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, filePath } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (isAsar && filePath.length > 0) {
|
|
|
|
const error = createError(AsarError.NOT_DIR)
|
|
|
|
nextTick(callback, [error])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir(pathArgument, mode, callback)
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
2016-07-25 17:07:39 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { mkdirSync } = fs
|
2018-09-10 15:29:19 +00:00
|
|
|
fs.mkdirSync = function (pathArgument, mode) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const { isAsar, filePath } = splitPath(pathArgument)
|
2018-09-10 15:29:19 +00:00
|
|
|
if (isAsar && filePath.length) throw createError(AsarError.NOT_DIR)
|
|
|
|
return mkdirSync(pathArgument, mode)
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-19 15:08:08 +00:00
|
|
|
|
|
|
|
// Executing a command string containing a path to an asar
|
2016-05-25 23:20:49 +00:00
|
|
|
// archive confuses `childProcess.execFile`, which is internally
|
|
|
|
// called by `childProcess.{exec,execSync}`, causing
|
2016-05-19 15:08:08 +00:00
|
|
|
// Electron to consider the full command as a single path
|
|
|
|
// to an archive.
|
2018-09-13 16:10:51 +00:00
|
|
|
const { exec, execSync } = childProcess
|
2018-08-01 03:06:48 +00:00
|
|
|
childProcess.exec = invokeWithNoAsar(exec)
|
|
|
|
childProcess.exec[util.promisify.custom] = invokeWithNoAsar(exec[util.promisify.custom])
|
|
|
|
childProcess.execSync = invokeWithNoAsar(execSync)
|
|
|
|
|
|
|
|
function invokeWithNoAsar (func) {
|
|
|
|
return function () {
|
2016-07-25 17:07:39 +00:00
|
|
|
const processNoAsarOriginalValue = process.noAsar
|
2016-05-19 15:08:08 +00:00
|
|
|
process.noAsar = true
|
2016-09-06 20:40:25 +00:00
|
|
|
try {
|
2018-08-01 03:06:48 +00:00
|
|
|
return func.apply(this, arguments)
|
2016-09-06 20:40:25 +00:00
|
|
|
} finally {
|
|
|
|
process.noAsar = processNoAsarOriginalValue
|
|
|
|
}
|
2016-05-19 15:08:08 +00:00
|
|
|
}
|
2018-08-01 03:06:48 +00:00
|
|
|
}
|
2016-05-19 15:08:08 +00:00
|
|
|
|
2018-10-03 16:36:20 +00:00
|
|
|
// Strictly implementing the flags of fs.copyFile is hard, just do a simple
|
|
|
|
// implementation for now. Doing 2 copies won't spend much time more as OS
|
|
|
|
// has filesystem caching.
|
|
|
|
overrideAPI(fs, 'copyFile')
|
|
|
|
overrideAPISync(fs, 'copyFileSync')
|
|
|
|
|
2016-03-25 19:50:43 +00:00
|
|
|
overrideAPI(fs, 'open')
|
2016-05-25 23:20:49 +00:00
|
|
|
overrideAPI(childProcess, 'execFile')
|
2016-03-25 19:50:43 +00:00
|
|
|
overrideAPISync(process, 'dlopen', 1)
|
|
|
|
overrideAPISync(require('module')._extensions, '.node', 1)
|
|
|
|
overrideAPISync(fs, 'openSync')
|
2016-07-25 17:07:39 +00:00
|
|
|
overrideAPISync(childProcess, 'execFileSync')
|
2016-03-25 19:50:43 +00:00
|
|
|
}
|
|
|
|
})()
|