electron/lib/common/asar.js

753 lines
24 KiB
JavaScript
Raw Normal View History

'use strict';
(function () {
2020-03-20 20:28:31 +00:00
const asar = process._linkedBinding('electron_common_asar');
const v8Util = process._linkedBinding('electron_common_v8_util');
const { Buffer } = require('buffer');
const Module = require('module');
const path = require('path');
const util = require('util');
2016-01-12 02:40:23 +00:00
2020-03-20 20:28:31 +00:00
const Promise = global.Promise;
const envNoAsar = process.env.ELECTRON_NO_ASAR &&
process.type !== 'browser' &&
2020-03-20 20:28:31 +00:00
process.type !== 'renderer';
const isAsarDisabled = () => process.noAsar || envNoAsar;
2016-01-12 02:40:23 +00:00
2020-03-20 20:28:31 +00:00
const internalBinding = process.internalBinding;
delete process.internalBinding;
/**
* @param {!Function} functionToCall
* @param {!Array|undefined} args
*/
const nextTick = (functionToCall, args = []) => {
2020-03-20 20:28:31 +00:00
process.nextTick(() => functionToCall(...args));
};
// Cache asar archive objects.
2020-03-20 20:28:31 +00:00
const cachedArchives = new Map();
const getOrCreateArchive = archivePath => {
2020-03-20 20:28:31 +00:00
const isCached = cachedArchives.has(archivePath);
if (isCached) {
2020-03-20 20:28:31 +00:00
return cachedArchives.get(archivePath);
}
2020-03-20 20:28:31 +00:00
const newArchive = asar.createArchive(archivePath);
if (!newArchive) return null;
2020-03-20 20:28:31 +00:00
cachedArchives.set(archivePath, newArchive);
return newArchive;
};
2016-01-12 02:40:23 +00:00
2016-01-19 18:25:03 +00:00
// Separate asar package's path from full path.
const splitPath = archivePathOrBuffer => {
// Shortcut for disabled asar.
2020-03-20 20:28:31 +00:00
if (isAsarDisabled()) return { isAsar: false };
2016-07-25 17:07:39 +00:00
// Check for a bad argument type.
2020-03-20 20:28:31 +00:00
let archivePath = archivePathOrBuffer;
if (Buffer.isBuffer(archivePathOrBuffer)) {
2020-03-20 20:28:31 +00:00
archivePath = archivePathOrBuffer.toString();
}
2020-03-20 20:28:31 +00:00
if (typeof archivePath !== 'string') return { isAsar: false };
2020-03-20 20:28:31 +00:00
return asar.splitPath(path.normalize(archivePath));
};
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.
2020-03-20 20:28:31 +00:00
let nextInode = 0;
2016-01-19 18:25:03 +00:00
2020-03-20 20:28:31 +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
2020-03-20 20:28:31 +00:00
const fakeTime = new Date();
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) {
2020-03-20 20:28:31 +00:00
const { Stats, constants } = require('fs');
2020-03-20 20:28:31 +00:00
let mode = constants.S_IROTH ^ constants.S_IRGRP ^ constants.S_IRUSR ^ constants.S_IWUSR;
if (stats.isFile) {
2020-03-20 20:28:31 +00:00
mode ^= constants.S_IFREG;
} else if (stats.isDirectory) {
2020-03-20 20:28:31 +00:00
mode ^= constants.S_IFDIR;
} else if (stats.isLink) {
2020-03-20 20:28:31 +00:00
mode ^= constants.S_IFLNK;
2016-03-25 19:50:43 +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
2020-03-20 20:28:31 +00:00
);
};
2016-01-19 18:25:03 +00:00
const AsarError = {
NOT_FOUND: 'NOT_FOUND',
NOT_DIR: 'NOT_DIR',
NO_ACCESS: 'NO_ACCESS',
INVALID_ARCHIVE: 'INVALID_ARCHIVE'
2020-03-20 20:28:31 +00:00
};
2018-09-13 16:10:51 +00:00
const createError = (errorType, { asarPath, filePath } = {}) => {
2020-03-20 20:28:31 +00:00
let error;
switch (errorType) {
case AsarError.NOT_FOUND:
2020-03-20 20:28:31 +00:00
error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`);
error.code = 'ENOENT';
error.errno = -2;
break;
case AsarError.NOT_DIR:
2020-03-20 20:28:31 +00:00
error = new Error('ENOTDIR, not a directory');
error.code = 'ENOTDIR';
error.errno = -20;
break;
case AsarError.NO_ACCESS:
2020-03-20 20:28:31 +00:00
error = new Error(`EACCES: permission denied, access '${filePath}'`);
error.code = 'EACCES';
error.errno = -13;
break;
case AsarError.INVALID_ARCHIVE:
2020-03-20 20:28:31 +00:00
error = new Error(`Invalid package ${asarPath}`);
break;
default:
2020-03-20 20:28:31 +00:00
throw new Error(`Invalid error type "${errorType}" passed to createError.`);
}
2020-03-20 20:28:31 +00:00
return error;
};
2016-01-12 02:40:23 +00:00
const overrideAPISync = function (module, name, pathArgumentIndex, fromAsync) {
2020-03-20 20:28:31 +00:00
if (pathArgumentIndex == null) pathArgumentIndex = 0;
const old = module[name];
const func = function () {
2020-03-20 20:28:31 +00:00
const pathArgument = arguments[pathArgumentIndex];
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return old.apply(this, arguments);
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const newPath = archive.copyFileOut(filePath);
if (!newPath) throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
arguments[pathArgumentIndex] = newPath;
return old.apply(this, arguments);
};
if (fromAsync) {
2020-03-20 20:28:31 +00:00
return func;
}
2020-03-20 20:28:31 +00:00
module[name] = func;
};
2016-01-19 18:25:03 +00:00
const overrideAPI = function (module, name, pathArgumentIndex) {
2020-03-20 20:28:31 +00:00
if (pathArgumentIndex == null) pathArgumentIndex = 0;
const old = module[name];
2016-03-25 19:50:43 +00:00
module[name] = function () {
2020-03-20 20:28:31 +00:00
const pathArgument = arguments[pathArgumentIndex];
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return old.apply(this, arguments);
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const callback = arguments[arguments.length - 1];
if (typeof callback !== 'function') {
2020-03-20 20:28:31 +00:00
return overrideAPISync(module, name, pathArgumentIndex, true).apply(this, arguments);
}
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
nextTick(callback, [error]);
return;
}
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const newPath = archive.copyFileOut(filePath);
if (!newPath) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
nextTick(callback, [error]);
return;
}
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
arguments[pathArgumentIndex] = newPath;
return old.apply(this, arguments);
};
if (old[util.promisify.custom]) {
2020-03-20 20:28:31 +00:00
module[name][util.promisify.custom] = makePromiseFunction(old[util.promisify.custom], pathArgumentIndex);
}
if (module.promises && module.promises[name]) {
2020-03-20 20:28:31 +00:00
module.promises[name] = makePromiseFunction(module.promises[name], pathArgumentIndex);
}
2020-03-20 20:28:31 +00:00
};
const makePromiseFunction = function (orig, pathArgumentIndex) {
return function (...args) {
2020-03-20 20:28:31 +00:00
const pathArgument = args[pathArgumentIndex];
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return orig.apply(this, args);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
return Promise.reject(createError(AsarError.INVALID_ARCHIVE, { asarPath }));
}
2020-03-20 20:28:31 +00:00
const newPath = archive.copyFileOut(filePath);
if (!newPath) {
2020-03-20 20:28:31 +00:00
return Promise.reject(createError(AsarError.NOT_FOUND, { asarPath, filePath }));
}
2020-03-20 20:28:31 +00:00
args[pathArgumentIndex] = newPath;
return orig.apply(this, args);
};
};
2016-01-19 18:25:03 +00:00
// Override fs APIs.
exports.wrapFsWithAsar = fs => {
2020-03-20 20:28:31 +00:00
const logFDs = {};
const logASARAccess = (asarPath, filePath, offset) => {
2020-03-20 20:28:31 +00:00
if (!process.env.ELECTRON_LOG_ASAR_READS) return;
if (!logFDs[asarPath]) {
2020-03-20 20:28:31 +00:00
const path = require('path');
const logFilename = `${path.basename(asarPath, '.asar')}-access-log.txt`;
const logPath = path.join(require('os').tmpdir(), logFilename);
logFDs[asarPath] = fs.openSync(logPath, 'a');
2016-03-25 19:50:43 +00:00
}
2020-03-20 20:28:31 +00:00
fs.writeSync(logFDs[asarPath], `${offset}: ${filePath}\n`);
};
2020-03-20 20:28:31 +00:00
const { lstatSync } = fs;
fs.lstatSync = (pathArgument, options) => {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return lstatSync(pathArgument, options);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
2020-03-20 20:28:31 +00:00
const stats = archive.stat(filePath);
if (!stats) throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
2020-03-20 20:28:31 +00:00
return asarStatsToFsStats(stats);
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const { lstat } = fs;
fs.lstat = function (pathArgument, options, callback) {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (typeof options === 'function') {
2020-03-20 20:28:31 +00:00
callback = options;
options = {};
}
2020-03-20 20:28:31 +00:00
if (!isAsar) return lstat(pathArgument, options, callback);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
const stats = archive.stat(filePath);
if (!stats) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
const fsStats = asarStatsToFsStats(stats);
nextTick(callback, [null, fsStats]);
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
fs.promises.lstat = util.promisify(fs.lstat);
2020-03-20 20:28:31 +00:00
const { statSync } = fs;
fs.statSync = (pathArgument, options) => {
2020-03-20 20:28:31 +00:00
const { isAsar } = splitPath(pathArgument);
if (!isAsar) return statSync(pathArgument, options);
2016-01-12 02:40:23 +00:00
2016-01-19 18:25:03 +00:00
// Do not distinguish links for now.
2020-03-20 20:28:31 +00:00
return fs.lstatSync(pathArgument, options);
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const { stat } = fs;
fs.stat = (pathArgument, options, callback) => {
2020-03-20 20:28:31 +00:00
const { isAsar } = splitPath(pathArgument);
if (typeof options === 'function') {
2020-03-20 20:28:31 +00:00
callback = options;
options = {};
}
2020-03-20 20:28:31 +00:00
if (!isAsar) return stat(pathArgument, options, callback);
2016-01-12 02:40:23 +00:00
2016-01-19 18:25:03 +00:00
// Do not distinguish links for now.
2020-03-20 20:28:31 +00:00
process.nextTick(() => fs.lstat(pathArgument, options, callback));
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
fs.promises.stat = util.promisify(fs.stat);
const wrapRealpathSync = function (realpathSync) {
return function (pathArgument, options) {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return realpathSync.apply(this, arguments);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
}
2020-03-20 20:28:31 +00:00
const fileRealPath = archive.realpath(filePath);
if (fileRealPath === false) {
2020-03-20 20:28:31 +00:00
throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
}
2020-03-20 20:28:31 +00:00
return path.join(realpathSync(asarPath, options), fileRealPath);
};
};
2020-03-20 20:28:31 +00:00
const { realpathSync } = fs;
fs.realpathSync = wrapRealpathSync(realpathSync);
fs.realpathSync.native = wrapRealpathSync(realpathSync.native);
const wrapRealpath = function (realpath) {
return function (pathArgument, options, callback) {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return realpath.apply(this, arguments);
if (arguments.length < 3) {
2020-03-20 20:28:31 +00:00
callback = options;
options = {};
}
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
const fileRealPath = archive.realpath(filePath);
if (fileRealPath === false) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
nextTick(callback, [error]);
return;
}
realpath(asarPath, options, (error, archiveRealPath) => {
if (error === null) {
2020-03-20 20:28:31 +00:00
const fullPath = path.join(archiveRealPath, fileRealPath);
callback(null, fullPath);
} else {
2020-03-20 20:28:31 +00:00
callback(error);
}
2020-03-20 20:28:31 +00:00
});
};
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const { realpath } = fs;
fs.realpath = wrapRealpath(realpath);
fs.realpath.native = wrapRealpath(realpath.native);
2020-03-20 20:28:31 +00:00
fs.promises.realpath = util.promisify(fs.realpath.native);
2020-03-20 20:28:31 +00:00
const { exists } = fs;
fs.exists = (pathArgument, callback) => {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return exists(pathArgument, callback);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
const pathExists = (archive.stat(filePath) !== false);
nextTick(callback, [pathExists]);
};
2016-07-25 17:07:39 +00:00
fs.exists[util.promisify.custom] = pathArgument => {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return exists[util.promisify.custom](pathArgument);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
return Promise.reject(error);
}
2020-03-20 20:28:31 +00:00
return Promise.resolve(archive.stat(filePath) !== false);
};
2020-03-20 20:28:31 +00:00
const { existsSync } = fs;
fs.existsSync = pathArgument => {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return existsSync(pathArgument);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) return false;
2020-03-20 20:28:31 +00:00
return archive.stat(filePath) !== false;
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const { access } = fs;
fs.access = function (pathArgument, mode, callback) {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return access.apply(this, arguments);
if (typeof mode === 'function') {
2020-03-20 20:28:31 +00:00
callback = mode;
mode = fs.constants.F_OK;
}
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
const info = archive.getFileInfo(filePath);
if (!info) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
nextTick(callback, [error]);
return;
}
if (info.unpacked) {
2020-03-20 20:28:31 +00:00
const realPath = archive.copyFileOut(filePath);
return fs.access(realPath, mode, callback);
}
2020-03-20 20:28:31 +00:00
const stats = archive.stat(filePath);
if (!stats) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
nextTick(callback, [error]);
return;
}
if (mode & fs.constants.W_OK) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NO_ACCESS, { asarPath, filePath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
nextTick(callback);
};
2020-03-20 20:28:31 +00:00
fs.promises.access = util.promisify(fs.access);
2020-03-20 20:28:31 +00:00
const { accessSync } = fs;
fs.accessSync = function (pathArgument, mode) {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return accessSync.apply(this, arguments);
2020-03-20 20:28:31 +00:00
if (mode == null) mode = fs.constants.F_OK;
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
}
2020-03-20 20:28:31 +00:00
const info = archive.getFileInfo(filePath);
if (!info) {
2020-03-20 20:28:31 +00:00
throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
}
if (info.unpacked) {
2020-03-20 20:28:31 +00:00
const realPath = archive.copyFileOut(filePath);
return fs.accessSync(realPath, mode);
}
2020-03-20 20:28:31 +00:00
const stats = archive.stat(filePath);
if (!stats) {
2020-03-20 20:28:31 +00:00
throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
}
if (mode & fs.constants.W_OK) {
2020-03-20 20:28:31 +00:00
throw createError(AsarError.NO_ACCESS, { asarPath, filePath });
}
2020-03-20 20:28:31 +00:00
};
2020-03-20 20:28:31 +00:00
const { readFile } = fs;
fs.readFile = function (pathArgument, options, callback) {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return readFile.apply(this, arguments);
2016-01-19 18:25:03 +00:00
if (typeof options === 'function') {
2020-03-20 20:28:31 +00:00
callback = options;
options = { encoding: null };
} else if (typeof options === 'string') {
2020-03-20 20:28:31 +00:00
options = { encoding: options };
} else if (options === null || options === undefined) {
2020-03-20 20:28:31 +00:00
options = { encoding: null };
} else if (typeof options !== 'object') {
2020-03-20 20:28:31 +00:00
throw new TypeError('Bad arguments');
2016-01-19 18:25:03 +00:00
}
2017-07-31 01:32:45 +00:00
2020-03-20 20:28:31 +00:00
const { encoding } = options;
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
const info = archive.getFileInfo(filePath);
if (!info) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
nextTick(callback, [error]);
return;
}
if (info.size === 0) {
2020-03-20 20:28:31 +00:00
nextTick(callback, [null, encoding ? '' : Buffer.alloc(0)]);
return;
}
2016-01-19 18:25:03 +00:00
if (info.unpacked) {
2020-03-20 20:28:31 +00:00
const realPath = archive.copyFileOut(filePath);
return fs.readFile(realPath, options, callback);
2016-01-19 18:25:03 +00:00
}
2017-08-01 09:52:48 +00:00
2020-03-20 20:28:31 +00:00
const buffer = Buffer.alloc(info.size);
const fd = archive.getFd();
if (!(fd >= 0)) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
logASARAccess(asarPath, filePath, info.offset);
fs.read(fd, buffer, 0, info.size, info.offset, error => {
2020-03-20 20:28:31 +00:00
callback(error, encoding ? buffer.toString(encoding) : buffer);
});
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
fs.promises.readFile = util.promisify(fs.readFile);
2020-03-20 20:28:31 +00:00
const { readFileSync } = fs;
fs.readFileSync = function (pathArgument, options) {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return readFileSync.apply(this, arguments);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
2020-03-20 20:28:31 +00:00
const info = archive.getFileInfo(filePath);
if (!info) throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
2020-03-20 20:28:31 +00:00
if (info.size === 0) return (options) ? '' : Buffer.alloc(0);
2016-01-19 18:25:03 +00:00
if (info.unpacked) {
2020-03-20 20:28:31 +00:00
const realPath = archive.copyFileOut(filePath);
return fs.readFileSync(realPath, options);
2016-01-19 18:25:03 +00:00
}
2016-01-19 18:25:03 +00:00
if (!options) {
2020-03-20 20:28:31 +00:00
options = { encoding: null };
} else if (typeof options === 'string') {
2020-03-20 20:28:31 +00:00
options = { encoding: options };
} else if (typeof options !== 'object') {
2020-03-20 20:28:31 +00:00
throw new TypeError('Bad arguments');
2016-01-19 18:25:03 +00:00
}
2020-03-20 20:28:31 +00:00
const { encoding } = options;
const buffer = Buffer.alloc(info.size);
const fd = archive.getFd();
if (!(fd >= 0)) throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
2020-03-20 20:28:31 +00:00
logASARAccess(asarPath, filePath, info.offset);
fs.readSync(fd, buffer, 0, info.size, info.offset);
return (encoding) ? buffer.toString(encoding) : buffer;
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const { readdir } = fs;
fs.readdir = function (pathArgument, options, callback) {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (typeof options === 'function') {
2020-03-20 20:28:31 +00:00
callback = options;
options = {};
}
2020-03-20 20:28:31 +00:00
if (!isAsar) return readdir.apply(this, arguments);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
const files = archive.readdir(filePath);
if (!files) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
nextTick(callback, [null, files]);
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
fs.promises.readdir = util.promisify(fs.readdir);
2020-03-20 20:28:31 +00:00
const { readdirSync } = fs;
fs.readdirSync = function (pathArgument, options) {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return readdirSync.apply(this, arguments);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) {
2020-03-20 20:28:31 +00:00
throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
}
2020-03-20 20:28:31 +00:00
const files = archive.readdir(filePath);
if (!files) {
2020-03-20 20:28:31 +00:00
throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
}
2020-03-20 20:28:31 +00:00
return files;
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
const { internalModuleReadJSON } = internalBinding('fs');
internalBinding('fs').internalModuleReadJSON = pathArgument => {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return internalModuleReadJSON(pathArgument);
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) return;
2020-03-20 20:28:31 +00:00
const info = archive.getFileInfo(filePath);
if (!info) return;
if (info.size === 0) return '';
2016-01-19 18:25:03 +00:00
if (info.unpacked) {
2020-03-20 20:28:31 +00:00
const realPath = archive.copyFileOut(filePath);
return fs.readFileSync(realPath, { encoding: 'utf8' });
2016-01-19 18:25:03 +00:00
}
2020-03-20 20:28:31 +00:00
const buffer = Buffer.alloc(info.size);
const fd = archive.getFd();
if (!(fd >= 0)) return;
2020-03-20 20:28:31 +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
2020-03-20 20:28:31 +00:00
const { internalModuleStat } = internalBinding('fs');
internalBinding('fs').internalModuleStat = pathArgument => {
2020-03-20 20:28:31 +00:00
const { isAsar, asarPath, filePath } = splitPath(pathArgument);
if (!isAsar) return internalModuleStat(pathArgument);
// -ENOENT
2020-03-20 20:28:31 +00:00
const archive = getOrCreateArchive(asarPath);
if (!archive) return -34;
2016-01-19 18:25:03 +00:00
// -ENOENT
2020-03-20 20:28:31 +00:00
const stats = archive.stat(filePath);
if (!stats) return -34;
2016-01-19 18:25:03 +00:00
2020-03-20 20:28:31 +00:00
return (stats.isDirectory) ? 1 : 0;
};
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.
if (process.platform === 'win32') {
2020-03-20 20:28:31 +00:00
const { mkdir } = fs;
fs.mkdir = (pathArgument, options, callback) => {
if (typeof options === 'function') {
2020-03-20 20:28:31 +00:00
callback = options;
options = {};
}
2020-03-20 20:28:31 +00:00
const { isAsar, filePath } = splitPath(pathArgument);
if (isAsar && filePath.length > 0) {
2020-03-20 20:28:31 +00:00
const error = createError(AsarError.NOT_DIR);
nextTick(callback, [error]);
return;
}
2020-03-20 20:28:31 +00:00
mkdir(pathArgument, options, callback);
};
2016-07-25 17:07:39 +00:00
2020-03-20 20:28:31 +00:00
fs.promises.mkdir = util.promisify(fs.mkdir);
2020-03-20 20:28:31 +00:00
const { mkdirSync } = fs;
fs.mkdirSync = function (pathArgument, options) {
2020-03-20 20:28:31 +00:00
const { isAsar, filePath } = splitPath(pathArgument);
if (isAsar && filePath.length) throw createError(AsarError.NOT_DIR);
return mkdirSync(pathArgument, options);
};
2016-03-25 19:50:43 +00:00
}
function invokeWithNoAsar (func) {
return function () {
2020-03-20 20:28:31 +00:00
const processNoAsarOriginalValue = process.noAsar;
process.noAsar = true;
2016-09-06 20:40:25 +00:00
try {
2020-03-20 20:28:31 +00:00
return func.apply(this, arguments);
2016-09-06 20:40:25 +00:00
} finally {
2020-03-20 20:28:31 +00:00
process.noAsar = processNoAsarOriginalValue;
2016-09-06 20:40:25 +00:00
}
2020-03-20 20:28:31 +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.
2020-03-20 20:28:31 +00:00
overrideAPI(fs, 'copyFile');
overrideAPISync(fs, 'copyFileSync');
2020-03-20 20:28:31 +00:00
overrideAPI(fs, 'open');
overrideAPISync(process, 'dlopen', 1);
overrideAPISync(Module._extensions, '.node', 1);
overrideAPISync(fs, 'openSync');
const overrideChildProcess = (childProcess) => {
// Executing a command string containing a path to an asar archive
// confuses `childProcess.execFile`, which is internally called by
// `childProcess.{exec,execSync}`, causing Electron to consider the full
// command as a single path to an archive.
2020-03-20 20:28:31 +00:00
const { exec, execSync } = childProcess;
childProcess.exec = invokeWithNoAsar(exec);
childProcess.exec[util.promisify.custom] = invokeWithNoAsar(exec[util.promisify.custom]);
childProcess.execSync = invokeWithNoAsar(execSync);
2020-03-20 20:28:31 +00:00
overrideAPI(childProcess, 'execFile');
overrideAPISync(childProcess, 'execFileSync');
};
// Lazily override the child_process APIs only when child_process is
// fetched the first time. We will eagerly override the child_process APIs
// when this env var is set so that stack traces generated inside node unit
// tests will match. This env var will only slow things down in users apps
// and should not be used.
if (process.env.ELECTRON_EAGER_ASAR_HOOK_FOR_TESTING) {
2020-03-20 20:28:31 +00:00
overrideChildProcess(require('child_process'));
} else {
2020-03-20 20:28:31 +00:00
const originalModuleLoad = Module._load;
Module._load = (request, ...args) => {
2020-03-20 20:28:31 +00:00
const loadResult = originalModuleLoad(request, ...args);
if (request === 'child_process') {
if (!v8Util.getHiddenValue(loadResult, 'asar-ready')) {
2020-03-20 20:28:31 +00:00
v8Util.setHiddenValue(loadResult, 'asar-ready', true);
// Just to make it obvious what we are dealing with here
2020-03-20 20:28:31 +00:00
const childProcess = loadResult;
2020-03-20 20:28:31 +00:00
overrideChildProcess(childProcess);
}
}
2020-03-20 20:28:31 +00:00
return loadResult;
};
}
2020-03-20 20:28:31 +00:00
};
})();