12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271 |
- import { Buffer } from 'buffer';
- import { Dirent, constants } from 'fs';
- import * as path from 'path';
- import * as util from 'util';
- import type * as Crypto from 'crypto';
- import type * as os from 'os';
- const asar = process._linkedBinding('electron_common_asar');
- const Module = require('module') as NodeJS.ModuleInternal;
- const Promise: PromiseConstructor = global.Promise;
- const envNoAsar = process.env.ELECTRON_NO_ASAR &&
- process.type !== 'browser' &&
- process.type !== 'renderer';
- const isAsarDisabled = () => process.noAsar || envNoAsar;
- const internalBinding = process.internalBinding!;
- delete process.internalBinding;
- const nextTick = (functionToCall: Function, args: any[] = []) => {
- process.nextTick(() => functionToCall(...args));
- };
- const binding = internalBinding('fs');
- // Cache asar archive objects.
- const cachedArchives = new Map<string, NodeJS.AsarArchive>();
- const getOrCreateArchive = (archivePath: string) => {
- const isCached = cachedArchives.has(archivePath);
- if (isCached) {
- return cachedArchives.get(archivePath)!;
- }
- try {
- const newArchive = new asar.Archive(archivePath);
- cachedArchives.set(archivePath, newArchive);
- return newArchive;
- } catch {
- return null;
- }
- };
- process._getOrCreateArchive = getOrCreateArchive;
- const asarRe = /\.asar/i;
- const {
- getValidatedPath,
- getOptions,
- getDirent
- } = __non_webpack_require__('internal/fs/utils');
- const {
- validateBoolean,
- validateFunction
- } = __non_webpack_require__('internal/validators');
- // In the renderer node internals use the node global URL but we do not set that to be
- // the global URL instance. We need to do instanceof checks against the internal URL impl
- const { URL: NodeURL } = __non_webpack_require__('internal/url');
- // Separate asar package's path from full path.
- const splitPath = (archivePathOrBuffer: string | Buffer | URL) => {
- // Shortcut for disabled asar.
- if (isAsarDisabled()) return { isAsar: <const>false };
- // Check for a bad argument type.
- let archivePath = archivePathOrBuffer;
- if (Buffer.isBuffer(archivePathOrBuffer)) {
- archivePath = archivePathOrBuffer.toString();
- }
- if (archivePath instanceof NodeURL) {
- archivePath = getValidatedPath(archivePath);
- }
- if (typeof archivePath !== 'string') return { isAsar: <const>false };
- if (!asarRe.test(archivePath)) return { isAsar: <const>false };
- return asar.splitPath(path.normalize(archivePath));
- };
- // Convert asar archive's Stats object to fs's Stats object.
- let nextInode = 0;
- const uid = process.getuid?.() ?? 0;
- const gid = process.getgid?.() ?? 0;
- const fakeTime = new Date();
- function getDirents (p: string, { 0: names, 1: types }: any[][]): Dirent[] {
- for (let i = 0; i < names.length; i++) {
- let type = types[i];
- const info = splitPath(path.join(p, names[i]));
- if (info.isAsar) {
- const archive = getOrCreateArchive(info.asarPath);
- if (!archive) continue;
- const stats = archive.stat(info.filePath);
- if (!stats) continue;
- type = stats.type;
- }
- names[i] = getDirent(p, names[i], type);
- }
- return names;
- }
- enum AsarFileType {
- kFile = (constants as any).UV_DIRENT_FILE,
- kDirectory = (constants as any).UV_DIRENT_DIR,
- kLink = (constants as any).UV_DIRENT_LINK,
- }
- const fileTypeToMode = new Map<AsarFileType, number>([
- [AsarFileType.kFile, constants.S_IFREG],
- [AsarFileType.kDirectory, constants.S_IFDIR],
- [AsarFileType.kLink, constants.S_IFLNK]
- ]);
- const asarStatsToFsStats = function (stats: NodeJS.AsarFileStat) {
- const { Stats } = require('fs');
- const mode = constants.S_IROTH | constants.S_IRGRP | constants.S_IRUSR | constants.S_IWUSR | fileTypeToMode.get(stats.type)!;
- return new Stats(
- 1, // dev
- mode, // mode
- 1, // nlink
- uid,
- gid,
- 0, // rdev
- undefined, // blksize
- ++nextInode, // ino
- stats.size,
- undefined, // blocks,
- fakeTime.getTime(), // atim_msec
- fakeTime.getTime(), // mtim_msec
- fakeTime.getTime(), // ctim_msec
- fakeTime.getTime() // birthtim_msec
- );
- };
- const enum AsarError {
- NOT_FOUND = 'NOT_FOUND',
- NOT_DIR = 'NOT_DIR',
- NO_ACCESS = 'NO_ACCESS',
- INVALID_ARCHIVE = 'INVALID_ARCHIVE'
- }
- type AsarErrorObject = Error & { code?: string, errno?: number };
- const createError = (errorType: AsarError, { asarPath, filePath }: { asarPath?: string, filePath?: string } = {}) => {
- let error: AsarErrorObject;
- switch (errorType) {
- case AsarError.NOT_FOUND:
- error = new Error(`ENOENT, ${filePath} not found in ${asarPath}`);
- error.code = 'ENOENT';
- error.errno = -2;
- break;
- case AsarError.NOT_DIR:
- error = new Error('ENOTDIR, not a directory');
- error.code = 'ENOTDIR';
- error.errno = -20;
- break;
- case AsarError.NO_ACCESS:
- error = new Error(`EACCES: permission denied, access '${filePath}'`);
- error.code = 'EACCES';
- error.errno = -13;
- break;
- case AsarError.INVALID_ARCHIVE:
- error = new Error(`Invalid package ${asarPath}`);
- break;
- default:
- throw new Error(`Invalid error type "${errorType}" passed to createError.`);
- }
- return error;
- };
- const overrideAPISync = function (module: Record<string, any>, name: string, pathArgumentIndex?: number | null, fromAsync: boolean = false) {
- if (pathArgumentIndex == null) pathArgumentIndex = 0;
- const old = module[name];
- const func = function (this: any, ...args: any[]) {
- const pathArgument = args[pathArgumentIndex!];
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return old.apply(this, args);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
- const newPath = archive.copyFileOut(filePath);
- if (!newPath) throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
- args[pathArgumentIndex!] = newPath;
- return old.apply(this, args);
- };
- if (fromAsync) {
- return func;
- }
- module[name] = func;
- };
- const overrideAPI = function (module: Record<string, any>, name: string, pathArgumentIndex?: number | null) {
- if (pathArgumentIndex == null) pathArgumentIndex = 0;
- const old = module[name];
- module[name] = function (this: any, ...args: any[]) {
- const pathArgument = args[pathArgumentIndex!];
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return old.apply(this, args);
- const { asarPath, filePath } = pathInfo;
- const callback = args[args.length - 1];
- if (typeof callback !== 'function') {
- return overrideAPISync(module, name, pathArgumentIndex!, true)!.apply(this, args);
- }
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
- nextTick(callback, [error]);
- return;
- }
- const newPath = archive.copyFileOut(filePath);
- if (!newPath) {
- const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
- nextTick(callback, [error]);
- return;
- }
- args[pathArgumentIndex!] = newPath;
- return old.apply(this, args);
- };
- if (old[util.promisify.custom]) {
- module[name][util.promisify.custom] = makePromiseFunction(old[util.promisify.custom], pathArgumentIndex);
- }
- if (module.promises && module.promises[name]) {
- module.promises[name] = makePromiseFunction(module.promises[name], pathArgumentIndex);
- }
- };
- let crypto: typeof Crypto;
- function validateBufferIntegrity (buffer: Buffer, integrity: NodeJS.AsarFileInfo['integrity']) {
- if (!integrity) return;
- // Delay load crypto to improve app boot performance
- // when integrity protection is not enabled
- crypto = crypto || require('crypto');
- const actual = crypto.createHash(integrity.algorithm).update(buffer).digest('hex');
- if (actual !== integrity.hash) {
- console.error(`ASAR Integrity Violation: got a hash mismatch (${actual} vs ${integrity.hash})`);
- process.exit(1);
- }
- }
- const makePromiseFunction = function (orig: Function, pathArgumentIndex: number) {
- return function (this: any, ...args: any[]) {
- const pathArgument = args[pathArgumentIndex];
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return orig.apply(this, args);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- return Promise.reject(createError(AsarError.INVALID_ARCHIVE, { asarPath }));
- }
- const newPath = archive.copyFileOut(filePath);
- if (!newPath) {
- return Promise.reject(createError(AsarError.NOT_FOUND, { asarPath, filePath }));
- }
- args[pathArgumentIndex] = newPath;
- return orig.apply(this, args);
- };
- };
- // Override fs APIs.
- export const wrapFsWithAsar = (fs: Record<string, any>) => {
- const logFDs = new Map<string, number>();
- const logASARAccess = (asarPath: string, filePath: string, offset: number) => {
- if (!process.env.ELECTRON_LOG_ASAR_READS) return;
- if (!logFDs.has(asarPath)) {
- const logFilename = `${path.basename(asarPath, '.asar')}-access-log.txt`;
- const logPath = path.join((require('os') as typeof os).tmpdir(), logFilename);
- logFDs.set(asarPath, fs.openSync(logPath, 'a'));
- }
- fs.writeSync(logFDs.get(asarPath), `${offset}: ${filePath}\n`);
- };
- const shouldThrowStatError = (options: any) => {
- if (options && typeof options === 'object' && options.throwIfNoEntry === false) {
- return false;
- }
- return true;
- };
- const { lstatSync } = fs;
- fs.lstatSync = (pathArgument: string, options: any) => {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return lstatSync(pathArgument, options);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- if (shouldThrowStatError(options)) {
- throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
- };
- return null;
- }
- const stats = archive.stat(filePath);
- if (!stats) {
- if (shouldThrowStatError(options)) {
- throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
- };
- return null;
- }
- return asarStatsToFsStats(stats);
- };
- const { lstat } = fs;
- fs.lstat = (pathArgument: string, options: any, callback: any) => {
- const pathInfo = splitPath(pathArgument);
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
- if (!pathInfo.isAsar) return lstat(pathArgument, options, callback);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
- nextTick(callback, [error]);
- return;
- }
- const stats = archive.stat(filePath);
- if (!stats) {
- const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
- nextTick(callback, [error]);
- return;
- }
- const fsStats = asarStatsToFsStats(stats);
- nextTick(callback, [null, fsStats]);
- };
- fs.promises.lstat = util.promisify(fs.lstat);
- const { statSync } = fs;
- fs.statSync = (pathArgument: string, options: any) => {
- const { isAsar } = splitPath(pathArgument);
- if (!isAsar) return statSync(pathArgument, options);
- // Do not distinguish links for now.
- return fs.lstatSync(pathArgument, options);
- };
- const { stat } = fs;
- fs.stat = (pathArgument: string, options: any, callback: any) => {
- const { isAsar } = splitPath(pathArgument);
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
- if (!isAsar) return stat(pathArgument, options, callback);
- // Do not distinguish links for now.
- process.nextTick(() => fs.lstat(pathArgument, options, callback));
- };
- fs.promises.stat = util.promisify(fs.stat);
- const wrapRealpathSync = function (realpathSync: Function) {
- return function (this: any, pathArgument: string, options: any) {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return realpathSync.apply(this, arguments);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
- }
- const fileRealPath = archive.realpath(filePath);
- if (fileRealPath === false) {
- throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
- }
- return path.join(realpathSync(asarPath, options), fileRealPath);
- };
- };
- const { realpathSync } = fs;
- fs.realpathSync = wrapRealpathSync(realpathSync);
- fs.realpathSync.native = wrapRealpathSync(realpathSync.native);
- const wrapRealpath = function (realpath: Function) {
- return function (this: any, pathArgument: string, options: any, callback: any) {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return realpath.apply(this, arguments);
- const { asarPath, filePath } = pathInfo;
- if (arguments.length < 3) {
- callback = options;
- options = {};
- }
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
- nextTick(callback, [error]);
- return;
- }
- const fileRealPath = archive.realpath(filePath);
- if (fileRealPath === false) {
- const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
- nextTick(callback, [error]);
- return;
- }
- realpath(asarPath, options, (error: Error | null, archiveRealPath: string) => {
- if (error === null) {
- const fullPath = path.join(archiveRealPath, fileRealPath);
- callback(null, fullPath);
- } else {
- callback(error);
- }
- });
- };
- };
- const { realpath } = fs;
- fs.realpath = wrapRealpath(realpath);
- fs.realpath.native = wrapRealpath(realpath.native);
- fs.promises.realpath = util.promisify(fs.realpath.native);
- const { exists: nativeExists } = fs;
- fs.exists = function exists (pathArgument: string, callback: any) {
- let pathInfo: ReturnType<typeof splitPath>;
- try {
- pathInfo = splitPath(pathArgument);
- } catch {
- nextTick(callback, [false]);
- return;
- }
- if (!pathInfo.isAsar) return nativeExists(pathArgument, callback);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
- nextTick(callback, [error]);
- return;
- }
- const pathExists = (archive.stat(filePath) !== false);
- nextTick(callback, [pathExists]);
- };
- fs.exists[util.promisify.custom] = function exists (pathArgument: string) {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return nativeExists[util.promisify.custom](pathArgument);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
- return Promise.reject(error);
- }
- return Promise.resolve(archive.stat(filePath) !== false);
- };
- const { existsSync } = fs;
- fs.existsSync = (pathArgument: string) => {
- let pathInfo: ReturnType<typeof splitPath>;
- try {
- pathInfo = splitPath(pathArgument);
- } catch {
- return false;
- }
- if (!pathInfo.isAsar) return existsSync(pathArgument);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) return false;
- return archive.stat(filePath) !== false;
- };
- const { access } = fs;
- fs.access = function (pathArgument: string, mode: number, callback: any) {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return access.apply(this, arguments);
- const { asarPath, filePath } = pathInfo;
- if (typeof mode === 'function') {
- callback = mode;
- mode = fs.constants.F_OK;
- }
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
- nextTick(callback, [error]);
- return;
- }
- const info = archive.getFileInfo(filePath);
- if (!info) {
- const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
- nextTick(callback, [error]);
- return;
- }
- if (info.unpacked) {
- const realPath = archive.copyFileOut(filePath);
- return fs.access(realPath, mode, callback);
- }
- const stats = archive.stat(filePath);
- if (!stats) {
- const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
- nextTick(callback, [error]);
- return;
- }
- if (mode & fs.constants.W_OK) {
- const error = createError(AsarError.NO_ACCESS, { asarPath, filePath });
- nextTick(callback, [error]);
- return;
- }
- nextTick(callback);
- };
- const { access: accessPromise } = fs.promises;
- fs.promises.access = function (pathArgument: string, mode: number) {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) {
- return accessPromise.apply(this, arguments);
- }
- const p = util.promisify(fs.access);
- return p(pathArgument, mode);
- };
- const { accessSync } = fs;
- fs.accessSync = function (pathArgument: string, mode: any) {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return accessSync.apply(this, arguments);
- const { asarPath, filePath } = pathInfo;
- if (mode == null) mode = fs.constants.F_OK;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
- }
- const info = archive.getFileInfo(filePath);
- if (!info) {
- throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
- }
- if (info.unpacked) {
- const realPath = archive.copyFileOut(filePath);
- return fs.accessSync(realPath, mode);
- }
- const stats = archive.stat(filePath);
- if (!stats) {
- throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
- }
- if (mode & fs.constants.W_OK) {
- throw createError(AsarError.NO_ACCESS, { asarPath, filePath });
- }
- };
- function fsReadFileAsar (pathArgument: string, options: any, callback: any) {
- const pathInfo = splitPath(pathArgument);
- if (pathInfo.isAsar) {
- const { asarPath, filePath } = pathInfo;
- if (typeof options === 'function') {
- callback = options;
- options = { encoding: null };
- } else if (typeof options === 'string') {
- options = { encoding: options };
- } else if (options === null || options === undefined) {
- options = { encoding: null };
- } else if (typeof options !== 'object') {
- throw new TypeError('Bad arguments');
- }
- const { encoding } = options;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
- nextTick(callback, [error]);
- return;
- }
- const info = archive.getFileInfo(filePath);
- if (!info) {
- 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) {
- const realPath = archive.copyFileOut(filePath);
- return fs.readFile(realPath, options, callback);
- }
- const buffer = Buffer.alloc(info.size);
- const fd = archive.getFdAndValidateIntegrityLater();
- if (!(fd >= 0)) {
- const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
- nextTick(callback, [error]);
- return;
- }
- logASARAccess(asarPath, filePath, info.offset);
- fs.read(fd, buffer, 0, info.size, info.offset, (error: Error) => {
- validateBufferIntegrity(buffer, info.integrity);
- callback(error, encoding ? buffer.toString(encoding) : buffer);
- });
- }
- }
- const { readFile } = fs;
- fs.readFile = function (pathArgument: string, options: any, callback: any) {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) {
- return readFile.apply(this, arguments);
- }
- return fsReadFileAsar(pathArgument, options, callback);
- };
- const { readFile: readFilePromise } = fs.promises;
- fs.promises.readFile = function (pathArgument: string, options: any) {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) {
- return readFilePromise.apply(this, arguments);
- }
- const p = util.promisify(fsReadFileAsar);
- return p(pathArgument, options);
- };
- const { readFileSync } = fs;
- fs.readFileSync = function (pathArgument: string, options: any) {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return readFileSync.apply(this, arguments);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
- const info = archive.getFileInfo(filePath);
- if (!info) throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
- if (info.size === 0) return (options) ? '' : Buffer.alloc(0);
- if (info.unpacked) {
- const realPath = archive.copyFileOut(filePath);
- return fs.readFileSync(realPath, options);
- }
- if (!options) {
- options = { encoding: null };
- } else if (typeof options === 'string') {
- options = { encoding: options };
- } else if (typeof options !== 'object') {
- throw new TypeError('Bad arguments');
- }
- const { encoding } = options;
- const buffer = Buffer.alloc(info.size);
- const fd = archive.getFdAndValidateIntegrityLater();
- if (!(fd >= 0)) {
- throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
- }
- logASARAccess(asarPath, filePath, info.offset);
- fs.readSync(fd, buffer, 0, info.size, info.offset);
- validateBufferIntegrity(buffer, info.integrity);
- return (encoding) ? buffer.toString(encoding) : buffer;
- };
- type ReaddirOptions = { encoding: BufferEncoding | null; withFileTypes?: false, recursive?: false } | undefined | null;
- type ReaddirCallback = (err: NodeJS.ErrnoException | null, files?: string[]) => void;
- const processReaddirResult = (args: any) => (args.context.withFileTypes ? handleDirents(args) : handleFilePaths(args));
- function handleDirents ({ result, currentPath, context }: { result: any[], currentPath: string, context: any }) {
- const length = result[0].length;
- for (let i = 0; i < length; i++) {
- const resultPath = path.join(currentPath, result[0][i]);
- const info = splitPath(resultPath);
- let type = result[1][i];
- if (info.isAsar) {
- const archive = getOrCreateArchive(info.asarPath);
- if (!archive) return;
- const stats = archive.stat(info.filePath);
- if (!stats) continue;
- type = stats.type;
- }
- const dirent = getDirent(currentPath, result[0][i], type);
- const stat = internalBinding('fs').internalModuleStat(binding, resultPath);
- context.readdirResults.push(dirent);
- if (dirent.isDirectory() || stat === 1) {
- context.pathsQueue.push(path.join(dirent.path, dirent.name));
- }
- }
- }
- function handleFilePaths ({ result, currentPath, context }: { result: string[], currentPath: string, context: any }) {
- for (let i = 0; i < result.length; i++) {
- const resultPath = path.join(currentPath, result[i]);
- const relativeResultPath = path.relative(context.basePath, resultPath);
- const stat = internalBinding('fs').internalModuleStat(binding, resultPath);
- context.readdirResults.push(relativeResultPath);
- if (stat === 1) {
- context.pathsQueue.push(resultPath);
- }
- }
- }
- function readdirRecursive (basePath: string, options: ReaddirOptions, callback: ReaddirCallback) {
- const context = {
- withFileTypes: Boolean(options!.withFileTypes),
- encoding: options!.encoding,
- basePath,
- readdirResults: [],
- pathsQueue: [basePath]
- };
- let i = 0;
- function read (pathArg: string) {
- const req = new binding.FSReqCallback();
- req.oncomplete = (err: any, result: string) => {
- if (err) {
- callback(err);
- return;
- }
- if (result === undefined) {
- callback(null, context.readdirResults);
- return;
- }
- processReaddirResult({
- result,
- currentPath: pathArg,
- context
- });
- if (i < context.pathsQueue.length) {
- read(context.pathsQueue[i++]);
- } else {
- callback(null, context.readdirResults);
- }
- };
- const pathInfo = splitPath(pathArg);
- if (pathInfo.isAsar) {
- let readdirResult;
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
- nextTick(callback, [error]);
- return;
- }
- readdirResult = archive.readdir(filePath);
- if (!readdirResult) {
- const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
- nextTick(callback, [error]);
- return;
- }
- // If we're in an asar dir, we need to ensure the result is in the same format as the
- // native call to readdir withFileTypes i.e. an array of arrays.
- if (context.withFileTypes) {
- readdirResult = [
- [...readdirResult], readdirResult.map((p: string) => {
- return internalBinding('fs').internalModuleStat(binding, path.join(pathArg, p));
- })
- ];
- }
- processReaddirResult({
- result: readdirResult,
- currentPath: pathArg,
- context
- });
- if (i < context.pathsQueue.length) {
- read(context.pathsQueue[i++]);
- } else {
- callback(null, context.readdirResults);
- }
- } else {
- binding.readdir(
- pathArg,
- context.encoding,
- context.withFileTypes,
- req
- );
- }
- }
- read(context.pathsQueue[i++]);
- }
- const { readdir } = fs;
- fs.readdir = function (pathArgument: string, options: ReaddirOptions, callback: ReaddirCallback) {
- callback = typeof options === 'function' ? options : callback;
- validateFunction(callback, 'callback');
- options = getOptions(options);
- pathArgument = getValidatedPath(pathArgument);
- if (options?.recursive != null) {
- validateBoolean(options?.recursive, 'options.recursive');
- }
- if (options?.recursive) {
- readdirRecursive(pathArgument, options, callback);
- return;
- }
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return readdir.apply(this, arguments);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- const error = createError(AsarError.INVALID_ARCHIVE, { asarPath });
- nextTick(callback!, [error]);
- return;
- }
- const files = archive.readdir(filePath);
- if (!files) {
- const error = createError(AsarError.NOT_FOUND, { asarPath, filePath });
- nextTick(callback!, [error]);
- return;
- }
- if (options?.withFileTypes) {
- const dirents = [];
- for (const file of files) {
- const childPath = path.join(filePath, file);
- const stats = archive.stat(childPath);
- if (!stats) {
- const error = createError(AsarError.NOT_FOUND, { asarPath, filePath: childPath });
- nextTick(callback!, [error]);
- return;
- }
- dirents.push(new fs.Dirent(file, stats.type));
- }
- nextTick(callback!, [null, dirents]);
- return;
- }
- nextTick(callback!, [null, files]);
- };
- const { readdir: readdirPromise } = fs.promises;
- fs.promises.readdir = async function (pathArgument: string, options: ReaddirOptions) {
- options = getOptions(options);
- pathArgument = getValidatedPath(pathArgument);
- if (options?.recursive != null) {
- validateBoolean(options?.recursive, 'options.recursive');
- }
- if (options?.recursive) {
- return readdirRecursivePromises(pathArgument, options);
- }
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return readdirPromise(pathArgument, options);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- return Promise.reject(createError(AsarError.INVALID_ARCHIVE, { asarPath }));
- }
- const files = archive.readdir(filePath);
- if (!files) {
- return Promise.reject(createError(AsarError.NOT_FOUND, { asarPath, filePath }));
- }
- if (options?.withFileTypes) {
- const dirents = [];
- for (const file of files) {
- const childPath = path.join(filePath, file);
- const stats = archive.stat(childPath);
- if (!stats) {
- throw createError(AsarError.NOT_FOUND, { asarPath, filePath: childPath });
- }
- dirents.push(new fs.Dirent(file, stats.type));
- }
- return Promise.resolve(dirents);
- }
- return Promise.resolve(files);
- };
- const { readdirSync } = fs;
- fs.readdirSync = function (pathArgument: string, options: ReaddirOptions) {
- options = getOptions(options);
- pathArgument = getValidatedPath(pathArgument);
- if (options?.recursive != null) {
- validateBoolean(options?.recursive, 'options.recursive');
- }
- if (options?.recursive) {
- return readdirSyncRecursive(pathArgument, options);
- }
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return readdirSync.apply(this, arguments);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) {
- throw createError(AsarError.INVALID_ARCHIVE, { asarPath });
- }
- const files = archive.readdir(filePath);
- if (!files) {
- throw createError(AsarError.NOT_FOUND, { asarPath, filePath });
- }
- if (options?.withFileTypes) {
- const dirents = [];
- for (const file of files) {
- const childPath = path.join(filePath, file);
- const stats = archive.stat(childPath);
- if (!stats) {
- throw createError(AsarError.NOT_FOUND, { asarPath, filePath: childPath });
- }
- dirents.push(new fs.Dirent(file, stats.type));
- }
- return dirents;
- }
- return files;
- };
- const modBinding = internalBinding('modules');
- const { readPackageJSON } = modBinding;
- internalBinding('modules').readPackageJSON = (
- jsonPath: string,
- isESM: boolean,
- base: undefined | string,
- specifier: undefined | string
- ) => {
- const pathInfo = splitPath(jsonPath);
- if (!pathInfo.isAsar) return readPackageJSON(jsonPath, isESM, base, specifier);
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) return undefined;
- const realPath = archive.copyFileOut(filePath);
- if (!realPath) return undefined;
- return readPackageJSON(realPath, isESM, base, specifier);
- };
- const { internalModuleStat } = binding;
- internalBinding('fs').internalModuleStat = (receiver: unknown, pathArgument: string) => {
- const pathInfo = splitPath(pathArgument);
- if (!pathInfo.isAsar) return internalModuleStat(receiver, pathArgument);
- const { asarPath, filePath } = pathInfo;
- // -ENOENT
- const archive = getOrCreateArchive(asarPath);
- if (!archive) return -34;
- // -ENOENT
- const stats = archive.stat(filePath);
- if (!stats) return -34;
- return (stats.type === AsarFileType.kDirectory) ? 1 : 0;
- };
- const { kUsePromises } = binding;
- async function readdirRecursivePromises (originalPath: string, options: ReaddirOptions) {
- const result: any[] = [];
- const pathInfo = splitPath(originalPath);
- let queue: [string, string[]][] = [];
- const withFileTypes = Boolean(options?.withFileTypes);
- let initialItem = [];
- if (pathInfo.isAsar) {
- const archive = getOrCreateArchive(pathInfo.asarPath);
- if (!archive) return result;
- const files = archive.readdir(pathInfo.filePath);
- if (!files) return result;
- // If we're in an asar dir, we need to ensure the result is in the same format as the
- // native call to readdir withFileTypes i.e. an array of arrays.
- initialItem = files;
- if (withFileTypes) {
- initialItem = [
- [...initialItem], initialItem.map((p: string) => {
- return internalBinding('fs').internalModuleStat(binding, path.join(originalPath, p));
- })
- ];
- }
- } else {
- initialItem = await binding.readdir(
- path.toNamespacedPath(originalPath),
- options!.encoding,
- withFileTypes,
- kUsePromises
- );
- }
- queue = [[originalPath, initialItem]];
- if (withFileTypes) {
- while (queue.length > 0) {
- // @ts-expect-error this is a valid array destructure assignment.
- const { 0: pathArg, 1: readDir } = queue.pop();
- for (const dirent of getDirents(pathArg, readDir)) {
- result.push(dirent);
- if (dirent.isDirectory()) {
- const direntPath = path.join(pathArg, dirent.name);
- const info = splitPath(direntPath);
- let readdirResult;
- if (info.isAsar) {
- const archive = getOrCreateArchive(info.asarPath);
- if (!archive) continue;
- const files = archive.readdir(info.filePath);
- if (!files) continue;
- readdirResult = [
- [...files], files.map((p: string) => {
- return internalBinding('fs').internalModuleStat(binding, path.join(direntPath, p));
- })
- ];
- } else {
- readdirResult = await binding.readdir(
- direntPath,
- options!.encoding,
- true,
- kUsePromises
- );
- }
- queue.push([direntPath, readdirResult]);
- }
- }
- }
- } else {
- while (queue.length > 0) {
- // @ts-expect-error this is a valid array destructure assignment.
- const { 0: pathArg, 1: readDir } = queue.pop();
- for (const ent of readDir) {
- const direntPath = path.join(pathArg, ent);
- const stat = internalBinding('fs').internalModuleStat(binding, direntPath);
- result.push(path.relative(originalPath, direntPath));
- if (stat === 1) {
- const subPathInfo = splitPath(direntPath);
- let item = [];
- if (subPathInfo.isAsar) {
- const archive = getOrCreateArchive(subPathInfo.asarPath);
- if (!archive) return;
- const files = archive.readdir(subPathInfo.filePath);
- if (!files) return result;
- item = files;
- } else {
- item = await binding.readdir(
- path.toNamespacedPath(direntPath),
- options!.encoding,
- false,
- kUsePromises
- );
- }
- queue.push([direntPath, item]);
- }
- }
- }
- }
- return result;
- }
- function readdirSyncRecursive (basePath: string, options: ReaddirOptions) {
- const context = {
- withFileTypes: Boolean(options!.withFileTypes),
- encoding: options!.encoding,
- basePath,
- readdirResults: [] as any,
- pathsQueue: [basePath]
- };
- function read (pathArg: string) {
- let readdirResult;
- const pathInfo = splitPath(pathArg);
- if (pathInfo.isAsar) {
- const { asarPath, filePath } = pathInfo;
- const archive = getOrCreateArchive(asarPath);
- if (!archive) return;
- readdirResult = archive.readdir(filePath);
- if (!readdirResult) return;
- // If we're in an asar dir, we need to ensure the result is in the same format as the
- // native call to readdir withFileTypes i.e. an array of arrays.
- if (context.withFileTypes) {
- readdirResult = [
- [...readdirResult], readdirResult.map((p: string) => {
- return internalBinding('fs').internalModuleStat(binding, path.join(pathArg, p));
- })
- ];
- }
- } else {
- readdirResult = binding.readdir(
- path.toNamespacedPath(pathArg),
- context.encoding,
- context.withFileTypes
- );
- }
- if (readdirResult === undefined) {
- return;
- }
- processReaddirResult({
- result: readdirResult,
- currentPath: pathArg,
- context
- });
- }
- for (let i = 0; i < context.pathsQueue.length; i++) {
- read(context.pathsQueue[i]);
- }
- return context.readdirResults;
- }
- // Calling mkdir for directory inside asar archive should throw ENOTDIR
- // error, but on Windows it throws ENOENT.
- if (process.platform === 'win32') {
- const { mkdir } = fs;
- fs.mkdir = (pathArgument: string, options: any, callback: any) => {
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
- const pathInfo = splitPath(pathArgument);
- if (pathInfo.isAsar && pathInfo.filePath.length > 0) {
- const error = createError(AsarError.NOT_DIR);
- nextTick(callback, [error]);
- return;
- }
- mkdir(pathArgument, options, callback);
- };
- fs.promises.mkdir = util.promisify(fs.mkdir);
- const { mkdirSync } = fs;
- fs.mkdirSync = function (pathArgument: string, options: any) {
- const pathInfo = splitPath(pathArgument);
- if (pathInfo.isAsar && pathInfo.filePath.length) throw createError(AsarError.NOT_DIR);
- return mkdirSync(pathArgument, options);
- };
- }
- function invokeWithNoAsar (func: Function) {
- return function (this: any) {
- const processNoAsarOriginalValue = process.noAsar;
- process.noAsar = true;
- try {
- return func.apply(this, arguments);
- } finally {
- process.noAsar = processNoAsarOriginalValue;
- }
- };
- }
- // 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');
- overrideAPI(fs, 'open');
- overrideAPISync(process, 'dlopen', 1);
- overrideAPISync(Module._extensions, '.node', 1);
- overrideAPISync(fs, 'openSync');
- const overrideChildProcess = (childProcess: Record<string, any>) => {
- // 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.
- const { exec, execSync } = childProcess;
- childProcess.exec = invokeWithNoAsar(exec);
- childProcess.exec[util.promisify.custom] = invokeWithNoAsar(exec[util.promisify.custom]);
- childProcess.execSync = invokeWithNoAsar(execSync);
- overrideAPI(childProcess, 'execFile');
- overrideAPISync(childProcess, 'execFileSync');
- };
- const asarReady = new WeakSet();
- // 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) {
- overrideChildProcess(require('child_process'));
- } else {
- const originalModuleLoad = Module._load;
- Module._load = (request: string, ...args: any[]) => {
- const loadResult = originalModuleLoad(request, ...args);
- if (request === 'child_process' || request === 'node:child_process') {
- if (!asarReady.has(loadResult)) {
- asarReady.add(loadResult);
- // Just to make it obvious what we are dealing with here
- const childProcess = loadResult;
- overrideChildProcess(childProcess);
- }
- }
- return loadResult;
- };
- }
- };
|