update electron to v43
This commit is contained in:
parent
68ac0beedf
commit
fb6c8b6ee9
5385 changed files with 513060 additions and 123058 deletions
480
electron/node_modules/app-builder-lib/out/asar/asarUtil.js
generated
vendored
480
electron/node_modules/app-builder-lib/out/asar/asarUtil.js
generated
vendored
|
|
@ -2,240 +2,284 @@
|
|||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AsarPackager = void 0;
|
||||
const builder_util_1 = require("builder-util");
|
||||
const dynamicImport_1 = require("../util/dynamicImport");
|
||||
const fs_1 = require("builder-util/out/fs");
|
||||
const fs_2 = require("fs");
|
||||
const promises_1 = require("fs/promises");
|
||||
const fs = require("fs-extra");
|
||||
const fs_extra_1 = require("fs-extra");
|
||||
const path = require("path");
|
||||
const appFileCopier_1 = require("../util/appFileCopier");
|
||||
const asar_1 = require("./asar");
|
||||
const integrity_1 = require("./integrity");
|
||||
const unpackDetector_1 = require("./unpackDetector");
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const pickle = require("chromium-pickle-js");
|
||||
const stream_1 = require("stream");
|
||||
const os = require("os");
|
||||
const resolvePath = async (file) => (file && (await (0, fs_1.exists)(file)) ? fs.realpath(file).catch(() => path.resolve(file)) : undefined);
|
||||
const resolvePaths = async (filepaths) => {
|
||||
return Promise.all(filepaths.map(resolvePath)).then(paths => paths.filter((it) => it != null));
|
||||
};
|
||||
const DENYLIST = resolvePaths([
|
||||
"/usr",
|
||||
"/lib",
|
||||
"/bin",
|
||||
"/sbin",
|
||||
"/etc",
|
||||
"/tmp",
|
||||
"/var", // block whole /var by default. If $HOME is under /var, it's explicitly in ALLOWLIST - https://github.com/electron-userland/electron-builder/issues/9025#issuecomment-3575380041
|
||||
// macOS system directories
|
||||
"/System",
|
||||
"/Library",
|
||||
"/private",
|
||||
// Windows system directories
|
||||
process.env.SystemRoot,
|
||||
process.env.WINDIR,
|
||||
]);
|
||||
const ALLOWLIST = resolvePaths([
|
||||
os.tmpdir(), // always allow temp dir
|
||||
os.homedir(), // always allow home dir
|
||||
]);
|
||||
/** @internal */
|
||||
class AsarPackager {
|
||||
constructor(src, destination, options, unpackPattern) {
|
||||
this.src = src;
|
||||
this.destination = destination;
|
||||
this.options = options;
|
||||
this.unpackPattern = unpackPattern;
|
||||
this.fs = new asar_1.AsarFilesystem(this.src);
|
||||
this.outFile = path.join(destination, "app.asar");
|
||||
this.unpackedDest = `${this.outFile}.unpacked`;
|
||||
constructor(packager, config) {
|
||||
this.packager = packager;
|
||||
this.config = config;
|
||||
this.outFile = path.join(config.resourcePath, `app.asar`);
|
||||
}
|
||||
// sort files to minimize file change (i.e. asar file is not changed dramatically on small change)
|
||||
async pack(fileSets, packager) {
|
||||
if (this.options.ordering != null) {
|
||||
// ordering doesn't support transformed files, but ordering is not used functionality - wait user report to fix it
|
||||
await order(fileSets[0].files, this.options.ordering, fileSets[0].src);
|
||||
}
|
||||
await promises_1.mkdir(path.dirname(this.outFile), { recursive: true });
|
||||
const unpackedFileIndexMap = new Map();
|
||||
for (const fileSet of fileSets) {
|
||||
unpackedFileIndexMap.set(fileSet, await this.createPackageFromFiles(fileSet, packager.info));
|
||||
}
|
||||
await this.writeAsarFile(fileSets, unpackedFileIndexMap);
|
||||
async pack(fileSets) {
|
||||
const orderedFileSets = [
|
||||
// Write dependencies first to minimize offset changes to asar header
|
||||
...fileSets.slice(1),
|
||||
// Finish with the app files that change most often
|
||||
fileSets[0],
|
||||
].map(set => this.orderFileSet(set));
|
||||
const streams = await this.processFileSets(orderedFileSets);
|
||||
await this.executeElectronAsar(streams);
|
||||
}
|
||||
async createPackageFromFiles(fileSet, packager) {
|
||||
const metadata = fileSet.metadata;
|
||||
// search auto unpacked dir
|
||||
const unpackedDirs = new Set();
|
||||
const rootForAppFilesWithoutAsar = path.join(this.destination, "app");
|
||||
if (this.options.smartUnpack !== false) {
|
||||
await unpackDetector_1.detectUnpackedDirs(fileSet, unpackedDirs, this.unpackedDest, rootForAppFilesWithoutAsar);
|
||||
}
|
||||
const dirToCreateForUnpackedFiles = new Set(unpackedDirs);
|
||||
const correctDirNodeUnpackedFlag = async (filePathInArchive, dirNode) => {
|
||||
for (const dir of unpackedDirs) {
|
||||
if (filePathInArchive.length > dir.length + 2 && filePathInArchive[dir.length] === path.sep && filePathInArchive.startsWith(dir)) {
|
||||
dirNode.unpacked = true;
|
||||
unpackedDirs.add(filePathInArchive);
|
||||
// not all dirs marked as unpacked after first iteration - because node module dir can be marked as unpacked after processing node module dir content
|
||||
// e.g. node-notifier/example/advanced.js processed, but only on process vendor/terminal-notifier.app module will be marked as unpacked
|
||||
await promises_1.mkdir(path.join(this.unpackedDest, filePathInArchive), { recursive: true });
|
||||
break;
|
||||
}
|
||||
async executeElectronAsar(streams) {
|
||||
// override logger temporarily to clean up console (electron/asar does some internal logging that blogs up the default electron-builder logs)
|
||||
const consoleLogger = console.log;
|
||||
console.log = (...args) => {
|
||||
if (args[0] === "Ordering file has 100% coverage.") {
|
||||
return; // no need to log, this means our ordering logic is working correctly
|
||||
}
|
||||
builder_util_1.log.info({ args }, "logging @electron/asar");
|
||||
};
|
||||
const transformedFiles = fileSet.transformedFiles;
|
||||
const taskManager = new builder_util_1.AsyncTaskManager(packager.cancellationToken);
|
||||
const fileCopier = new fs_1.FileCopier();
|
||||
let currentDirNode = null;
|
||||
let currentDirPath = null;
|
||||
const unpackedFileIndexSet = new Set();
|
||||
for (let i = 0, n = fileSet.files.length; i < n; i++) {
|
||||
const file = fileSet.files[i];
|
||||
const stat = metadata.get(file);
|
||||
if (stat == null) {
|
||||
continue;
|
||||
}
|
||||
const pathInArchive = path.relative(rootForAppFilesWithoutAsar, appFileCopier_1.getDestinationPath(file, fileSet));
|
||||
if (stat.isSymbolicLink()) {
|
||||
const s = stat;
|
||||
this.fs.getOrCreateNode(pathInArchive).link = s.relativeLink;
|
||||
s.pathInArchive = pathInArchive;
|
||||
unpackedFileIndexSet.add(i);
|
||||
continue;
|
||||
}
|
||||
let fileParent = path.dirname(pathInArchive);
|
||||
if (fileParent === ".") {
|
||||
fileParent = "";
|
||||
}
|
||||
if (currentDirPath !== fileParent) {
|
||||
if (fileParent.startsWith("..")) {
|
||||
throw new Error(`Internal error: path must not start with "..": ${fileParent}`);
|
||||
}
|
||||
currentDirPath = fileParent;
|
||||
currentDirNode = this.fs.getOrCreateNode(fileParent);
|
||||
// do not check for root
|
||||
if (fileParent !== "" && !currentDirNode.unpacked) {
|
||||
if (unpackedDirs.has(fileParent)) {
|
||||
currentDirNode.unpacked = true;
|
||||
}
|
||||
else {
|
||||
await correctDirNodeUnpackedFlag(fileParent, currentDirNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
const dirNode = currentDirNode;
|
||||
const newData = transformedFiles == null ? undefined : transformedFiles.get(i);
|
||||
const isUnpacked = dirNode.unpacked || (this.unpackPattern != null && this.unpackPattern(file, stat));
|
||||
const integrity = newData === undefined ? await integrity_1.hashFile(file) : integrity_1.hashFileContents(newData);
|
||||
this.fs.addFileNode(file, dirNode, newData == undefined ? stat.size : Buffer.byteLength(newData), isUnpacked, stat, integrity);
|
||||
if (isUnpacked) {
|
||||
if (!dirNode.unpacked && !dirToCreateForUnpackedFiles.has(fileParent)) {
|
||||
dirToCreateForUnpackedFiles.add(fileParent);
|
||||
await promises_1.mkdir(path.join(this.unpackedDest, fileParent), { recursive: true });
|
||||
}
|
||||
const unpackedFile = path.join(this.unpackedDest, pathInArchive);
|
||||
taskManager.addTask(copyFileOrData(fileCopier, newData, file, unpackedFile, stat));
|
||||
if (taskManager.tasks.length > fs_1.MAX_FILE_REQUESTS) {
|
||||
await taskManager.awaitTasks();
|
||||
}
|
||||
unpackedFileIndexSet.add(i);
|
||||
}
|
||||
}
|
||||
if (taskManager.tasks.length > 0) {
|
||||
await taskManager.awaitTasks();
|
||||
}
|
||||
return unpackedFileIndexSet;
|
||||
const { createPackageFromStreams } = await (0, dynamicImport_1.dynamicImport)("@electron/asar");
|
||||
await createPackageFromStreams(this.outFile, streams);
|
||||
console.log = consoleLogger;
|
||||
}
|
||||
writeAsarFile(fileSets, unpackedFileIndexMap) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const headerPickle = pickle.createEmpty();
|
||||
headerPickle.writeString(JSON.stringify(this.fs.header));
|
||||
const headerBuf = headerPickle.toBuffer();
|
||||
const sizePickle = pickle.createEmpty();
|
||||
sizePickle.writeUInt32(headerBuf.length);
|
||||
const sizeBuf = sizePickle.toBuffer();
|
||||
const writeStream = fs_2.createWriteStream(this.outFile);
|
||||
writeStream.on("error", reject);
|
||||
writeStream.on("close", resolve);
|
||||
writeStream.write(sizeBuf);
|
||||
let fileSetIndex = 0;
|
||||
let files = fileSets[0].files;
|
||||
let metadata = fileSets[0].metadata;
|
||||
let transformedFiles = fileSets[0].transformedFiles;
|
||||
let unpackedFileIndexSet = unpackedFileIndexMap.get(fileSets[0]);
|
||||
const w = (index) => {
|
||||
while (true) {
|
||||
if (index >= files.length) {
|
||||
if (++fileSetIndex >= fileSets.length) {
|
||||
writeStream.end();
|
||||
return;
|
||||
}
|
||||
else {
|
||||
files = fileSets[fileSetIndex].files;
|
||||
metadata = fileSets[fileSetIndex].metadata;
|
||||
transformedFiles = fileSets[fileSetIndex].transformedFiles;
|
||||
unpackedFileIndexSet = unpackedFileIndexMap.get(fileSets[fileSetIndex]);
|
||||
index = 0;
|
||||
}
|
||||
}
|
||||
if (!unpackedFileIndexSet.has(index)) {
|
||||
break;
|
||||
}
|
||||
else {
|
||||
const stat = metadata.get(files[index]);
|
||||
if (stat != null && stat.isSymbolicLink()) {
|
||||
fs_2.symlink(stat.linkRelativeToFile, path.join(this.unpackedDest, stat.pathInArchive), () => w(index + 1));
|
||||
return;
|
||||
}
|
||||
}
|
||||
index++;
|
||||
async processFileSets(fileSets) {
|
||||
var _a;
|
||||
const unpackedPaths = new Set();
|
||||
if (this.config.options.smartUnpack !== false) {
|
||||
for (const fileSet of fileSets) {
|
||||
(0, unpackDetector_1.detectUnpackedDirs)(fileSet, unpackedPaths);
|
||||
}
|
||||
}
|
||||
const resultsMap = new Map();
|
||||
const streamOrdering = [];
|
||||
const normalizedUnpackedPaths = Array.from(unpackedPaths).map(p => path.normalize(p));
|
||||
// Check whether a file or directory should be unpacked, using pre-normalized unpacked paths and early returns
|
||||
const isUnpacked = (dir, file, stat) => {
|
||||
var _a, _b;
|
||||
const normalizedDir = path.normalize(dir);
|
||||
// Check file pattern first (most specific)
|
||||
if (!(0, builder_util_1.isEmptyOrSpaces)(file) && stat && ((_b = (_a = this.config).unpackPattern) === null || _b === void 0 ? void 0 : _b.call(_a, file, stat))) {
|
||||
return true;
|
||||
}
|
||||
// Check if path is within any unpacked directory
|
||||
for (const unpackedPath of normalizedUnpackedPaths) {
|
||||
if (normalizedDir === unpackedPath || normalizedDir.startsWith(unpackedPath + path.sep)) {
|
||||
return true;
|
||||
}
|
||||
const data = transformedFiles == null ? null : transformedFiles.get(index);
|
||||
const file = files[index];
|
||||
if (data !== null && data !== undefined) {
|
||||
writeStream.write(data, () => w(index + 1));
|
||||
return;
|
||||
}
|
||||
// https://github.com/yarnpkg/yarn/pull/3539
|
||||
const stat = metadata.get(file);
|
||||
if (stat != null && stat.size < 2 * 1024 * 1024) {
|
||||
promises_1.readFile(file)
|
||||
.then(it => {
|
||||
writeStream.write(it, () => w(index + 1));
|
||||
})
|
||||
.catch(e => reject(`Cannot read file ${file}: ${e.stack || e}`));
|
||||
}
|
||||
else {
|
||||
const readStream = fs_2.createReadStream(file);
|
||||
readStream.on("error", reject);
|
||||
readStream.once("end", () => w(index + 1));
|
||||
readStream.on("open", () => {
|
||||
readStream.pipe(writeStream, {
|
||||
end: false,
|
||||
});
|
||||
});
|
||||
}
|
||||
return false;
|
||||
};
|
||||
// First pass: process all files in order, ensuring parent directories exist
|
||||
for (const fileSet of fileSets) {
|
||||
// Don't use Promise.all, we need to retain order of execution/iteration through the already-ordered fileset
|
||||
for (const [index, file] of fileSet.files.entries()) {
|
||||
const transformedData = (_a = fileSet.transformedFiles) === null || _a === void 0 ? void 0 : _a.get(index);
|
||||
const stat = fileSet.metadata.get(file);
|
||||
const destination = path.relative(this.config.defaultDestination, (0, appFileCopier_1.getDestinationPath)(file, fileSet));
|
||||
// Ensure parent directories exist before processing file
|
||||
this.ensureParentDirectories(destination, resultsMap, streamOrdering);
|
||||
const result = await this.processFileOrSymlink({
|
||||
file,
|
||||
destination,
|
||||
fileSet,
|
||||
transformedData,
|
||||
stat,
|
||||
isUnpacked,
|
||||
});
|
||||
if (result && !resultsMap.has(result.path)) {
|
||||
resultsMap.set(result.path, result);
|
||||
streamOrdering.push(result.path);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Second pass: propagate unpacked flag to parent directories
|
||||
for (const entry of resultsMap.values()) {
|
||||
if (entry.unpacked) {
|
||||
this.markParentDirectoriesAsUnpacked(entry.path, resultsMap, isUnpacked);
|
||||
}
|
||||
}
|
||||
// Build final results array maintaining processing order
|
||||
return streamOrdering.reduce((streams, path) => {
|
||||
const stream = resultsMap.has(path) ? resultsMap.get(path) : null;
|
||||
if (stream != null) {
|
||||
streams.push(stream);
|
||||
}
|
||||
return streams;
|
||||
}, []);
|
||||
}
|
||||
ensureParentDirectories(destination, resultsMap, streamOrdering) {
|
||||
const parents = [];
|
||||
let current = path.dirname(path.normalize(destination));
|
||||
// Collect all parent directories from deepest to root
|
||||
while (current !== ".") {
|
||||
parents.unshift(current);
|
||||
current = path.dirname(current);
|
||||
}
|
||||
// Add parent directories in order (root to deepest)
|
||||
for (const parentPath of parents) {
|
||||
if (!resultsMap.has(parentPath)) {
|
||||
const dir = {
|
||||
type: "directory",
|
||||
path: parentPath,
|
||||
unpacked: false, // Updated in second pass if needed
|
||||
};
|
||||
resultsMap.set(parentPath, dir);
|
||||
streamOrdering.push(parentPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
markParentDirectoriesAsUnpacked(destination, resultsMap, isUnpacked) {
|
||||
let current = path.dirname(path.normalize(destination));
|
||||
while (current !== ".") {
|
||||
const entry = resultsMap.get(current);
|
||||
if (entry && isUnpacked(current)) {
|
||||
entry.unpacked = true;
|
||||
}
|
||||
current = path.dirname(current);
|
||||
}
|
||||
}
|
||||
async processFileOrSymlink(options) {
|
||||
const { isUnpacked, transformedData, file, destination, stat } = options;
|
||||
const unpacked = isUnpacked(destination, file, stat);
|
||||
// Handle directories
|
||||
if (!stat.isFile() && !stat.isSymbolicLink()) {
|
||||
return { path: destination, unpacked, type: "directory" };
|
||||
}
|
||||
// Handle transformed data (pre-processed content)
|
||||
if (transformedData != null) {
|
||||
const size = Buffer.byteLength(transformedData);
|
||||
return {
|
||||
path: destination,
|
||||
streamGenerator: () => new stream_1.Readable({
|
||||
read() {
|
||||
this.push(transformedData);
|
||||
this.push(null);
|
||||
},
|
||||
}),
|
||||
unpacked,
|
||||
type: "file",
|
||||
stat: { mode: stat.mode, size },
|
||||
};
|
||||
writeStream.write(headerBuf, () => w(0));
|
||||
}
|
||||
// verify that the file is not a direct link or symlinked to access/copy a system file
|
||||
await this.protectSystemAndUnsafePaths(file, await this.packager.info.getWorkspaceRoot());
|
||||
const baseConfig = {
|
||||
path: destination,
|
||||
streamGenerator: () => fs.createReadStream(file),
|
||||
unpacked,
|
||||
stat,
|
||||
};
|
||||
// Handle regular files
|
||||
if (!stat.isSymbolicLink()) {
|
||||
return { ...baseConfig, type: "file" };
|
||||
}
|
||||
// Handle symlinks - make relative to source location
|
||||
let link = await (0, fs_extra_1.readlink)(file);
|
||||
if (path.isAbsolute(link)) {
|
||||
link = path.relative(path.dirname(file), link);
|
||||
}
|
||||
return {
|
||||
...baseConfig,
|
||||
type: "link",
|
||||
symlink: link,
|
||||
};
|
||||
}
|
||||
orderFileSet(fileSet) {
|
||||
const sortedFileEntries = Array.from(fileSet.files.entries());
|
||||
sortedFileEntries.sort(([, a], [, b]) => {
|
||||
if (a === b) {
|
||||
return 0;
|
||||
}
|
||||
// Place addons last because their signature changes per build
|
||||
const isAAddon = a.endsWith(".node");
|
||||
const isBAddon = b.endsWith(".node");
|
||||
if (isAAddon && !isBAddon) {
|
||||
return 1;
|
||||
}
|
||||
if (isBAddon && !isAAddon) {
|
||||
return -1;
|
||||
}
|
||||
// Otherwise order by name
|
||||
return a < b ? -1 : 1;
|
||||
});
|
||||
let transformedFiles;
|
||||
if (fileSet.transformedFiles) {
|
||||
transformedFiles = new Map();
|
||||
const indexMap = new Map();
|
||||
for (const [newIndex, [oldIndex]] of sortedFileEntries.entries()) {
|
||||
indexMap.set(oldIndex, newIndex);
|
||||
}
|
||||
for (const [oldIndex, value] of fileSet.transformedFiles) {
|
||||
const newIndex = indexMap.get(oldIndex);
|
||||
if (newIndex === undefined) {
|
||||
throw new Error(`Internal error: ${fileSet.files[oldIndex]} was lost while ordering asar`);
|
||||
}
|
||||
transformedFiles.set(newIndex, value);
|
||||
}
|
||||
}
|
||||
return {
|
||||
src: fileSet.src,
|
||||
destination: fileSet.destination,
|
||||
metadata: fileSet.metadata,
|
||||
files: sortedFileEntries.map(([, file]) => file),
|
||||
transformedFiles,
|
||||
};
|
||||
}
|
||||
async checkAgainstRoots(target, allowRoots) {
|
||||
const resolved = await resolvePath(target);
|
||||
if (resolved == null || (0, builder_util_1.isEmptyOrSpaces)(resolved)) {
|
||||
return false;
|
||||
}
|
||||
for (const root of allowRoots) {
|
||||
if (resolved === root || resolved.startsWith(root + path.sep)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
async protectSystemAndUnsafePaths(file, workspaceRoot) {
|
||||
const resolved = await resolvePath(file);
|
||||
const logFields = { source: file, realPath: resolved };
|
||||
const workspace = await resolvePath(workspaceRoot);
|
||||
// If in workspace, always safe
|
||||
if (workspace && (resolved === null || resolved === void 0 ? void 0 : resolved.startsWith(workspace))) {
|
||||
return;
|
||||
}
|
||||
// Check allowlist (priority)
|
||||
if (await this.checkAgainstRoots(file, await ALLOWLIST)) {
|
||||
return;
|
||||
}
|
||||
// Check denylist
|
||||
if (await this.checkAgainstRoots(file, await DENYLIST)) {
|
||||
builder_util_1.log.error(logFields, `denied access to system or unsafe path`);
|
||||
throw new Error(`Cannot copy file [${file}] symlinked to file [${resolved}] outside the package to a system or unsafe path`);
|
||||
}
|
||||
// Default: outside explicit paths but not explicitly denied
|
||||
builder_util_1.log.debug(logFields, `path is outside of explicit safe paths, defaulting to safe`);
|
||||
}
|
||||
}
|
||||
exports.AsarPackager = AsarPackager;
|
||||
async function order(filenames, orderingFile, src) {
|
||||
const orderingFiles = (await promises_1.readFile(orderingFile, "utf8")).split("\n").map(line => {
|
||||
if (line.indexOf(":") !== -1) {
|
||||
line = line.split(":").pop();
|
||||
}
|
||||
line = line.trim();
|
||||
if (line[0] === "/") {
|
||||
line = line.slice(1);
|
||||
}
|
||||
return line;
|
||||
});
|
||||
const ordering = [];
|
||||
for (const file of orderingFiles) {
|
||||
const pathComponents = file.split(path.sep);
|
||||
for (const pathComponent of pathComponents) {
|
||||
ordering.push(path.join(src, pathComponent));
|
||||
}
|
||||
}
|
||||
const sortedFiles = [];
|
||||
let missing = 0;
|
||||
const total = filenames.length;
|
||||
for (const file of ordering) {
|
||||
if (!sortedFiles.includes(file) && filenames.includes(file)) {
|
||||
sortedFiles.push(file);
|
||||
}
|
||||
}
|
||||
for (const file of filenames) {
|
||||
if (!sortedFiles.includes(file)) {
|
||||
sortedFiles.push(file);
|
||||
missing += 1;
|
||||
}
|
||||
}
|
||||
builder_util_1.log.info({ coverage: ((total - missing) / total) * 100 }, "ordering files in ASAR archive");
|
||||
return sortedFiles;
|
||||
}
|
||||
function copyFileOrData(fileCopier, data, source, destination, stats) {
|
||||
if (data == null) {
|
||||
return fileCopier.copy(source, destination, stats);
|
||||
}
|
||||
else {
|
||||
return promises_1.writeFile(destination, data);
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=asarUtil.js.map
|
||||
Loading…
Add table
Add a link
Reference in a new issue