Include code cache for preload bundle
Co-authored-by: Scott Nonnenberg <scott@signal.org>
This commit is contained in:
parent
3c9332449f
commit
695f64a55a
13 changed files with 230 additions and 11 deletions
|
@ -29,6 +29,7 @@ ts/**/*.js
|
|||
.eslintrc.js
|
||||
webpack.config.ts
|
||||
preload.bundle.*
|
||||
preload.wrapper.*
|
||||
bundles/**
|
||||
|
||||
# Sticker Creator has its own eslint config
|
||||
|
|
2
.github/workflows/benchmark.yml
vendored
2
.github/workflows/benchmark.yml
vendored
|
@ -54,6 +54,8 @@ jobs:
|
|||
run: npm run generate
|
||||
- name: Bundle
|
||||
run: npm run build:esbuild:prod
|
||||
- name: Create preload cache
|
||||
run: xvfb-run --auto-servernum npm run build:preload-cache
|
||||
|
||||
- name: Run startup benchmarks
|
||||
run: |
|
||||
|
|
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
|
@ -165,6 +165,8 @@ jobs:
|
|||
|
||||
- name: Create bundle
|
||||
run: npm run build:esbuild:prod
|
||||
- name: Create preload cache
|
||||
run: xvfb-run --auto-servernum npm run build:preload-cache
|
||||
|
||||
- name: Build with packaging .deb file
|
||||
run: npm run build:release -- --publish=never
|
||||
|
@ -249,6 +251,8 @@ jobs:
|
|||
|
||||
- name: Create bundle
|
||||
run: npm run build:esbuild:prod
|
||||
- name: Create preload cache
|
||||
run: npm run build:preload-cache
|
||||
|
||||
- name: Build with NSIS
|
||||
run: npm run build:release
|
||||
|
@ -359,6 +363,8 @@ jobs:
|
|||
run: npm run generate
|
||||
- name: Bundle
|
||||
run: npm run build:esbuild:prod
|
||||
- name: Create preload cache
|
||||
run: xvfb-run --auto-servernum npm run build:preload-cache
|
||||
|
||||
- name: Run mock server tests
|
||||
run: |
|
||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -29,6 +29,7 @@ stylesheets/*.css
|
|||
!stylesheets/webrtc_internals.css
|
||||
/storybook-static/
|
||||
preload.bundle.*
|
||||
preload.wrapper.js
|
||||
bundles/
|
||||
ts/sql/mainWorker.bundle.js.LICENSE.txt
|
||||
build/ICUMessageParams.d.ts
|
||||
|
|
|
@ -52,6 +52,7 @@ js/calling-tools/**
|
|||
stylesheets/_intlTelInput.scss
|
||||
|
||||
preload.bundle.*
|
||||
preload.wrapper.js
|
||||
bundles/**
|
||||
|
||||
# Sticker Creator has its own prettier config
|
||||
|
|
|
@ -709,7 +709,7 @@ async function createWindow() {
|
|||
preload: join(
|
||||
__dirname,
|
||||
usePreloadBundle
|
||||
? '../preload.bundle.js'
|
||||
? '../preload.wrapper.js'
|
||||
: '../ts/windows/main/preload.js'
|
||||
),
|
||||
spellcheck: await getSpellCheckSetting(),
|
||||
|
|
|
@ -61,7 +61,7 @@
|
|||
"svgo": "svgo --multipass images/**/*.svg",
|
||||
"transpile": "run-p check:types build:esbuild",
|
||||
"check:types": "tsc --noEmit",
|
||||
"clean-transpile-once": "rimraf sticker-creator/dist app/**/*.js app/*.js ts/**/*.js ts/*.js bundles tsconfig.tsbuildinfo",
|
||||
"clean-transpile-once": "rimraf sticker-creator/dist app/**/*.js app/*.js ts/**/*.js ts/*.js bundles tsconfig.tsbuildinfo preload.bundle.js preload.bundle.cache",
|
||||
"clean-transpile": "run-s clean-transpile-once clean-transpile-once",
|
||||
"ready": "npm-run-all --print-label clean-transpile generate --parallel lint lint-deps lint-intl test-node test-electron",
|
||||
"dev": "npm run build-protobuf && cross-env SIGNAL_ENV=storybook storybook dev --port 6006",
|
||||
|
@ -74,7 +74,7 @@
|
|||
"test:storybook": "npm run build:storybook && run-p --race test:storybook:*",
|
||||
"test:storybook:serve": "http-server storybook-static --port 6006 --silent",
|
||||
"test:storybook:test": "wait-on http://127.0.0.1:6006/ --timeout 5000 && test-storybook",
|
||||
"build": "run-s --print-label generate build:esbuild:prod build:release",
|
||||
"build": "run-s --print-label generate build:esbuild:prod build:preload-cache build:release",
|
||||
"build-linux": "run-s generate build:esbuild:prod && npm run build:release -- --publish=never",
|
||||
"build:acknowledgments": "node scripts/generate-acknowledgments.js",
|
||||
"build:dns-fallback": "node ts/scripts/generate-dns-fallback.js",
|
||||
|
@ -85,6 +85,7 @@
|
|||
"build:esbuild:prod": "node scripts/esbuild.js --prod",
|
||||
"build:electron": "electron-builder --config.extraMetadata.environment=$SIGNAL_ENV",
|
||||
"build:release": "cross-env SIGNAL_ENV=production npm run build:electron -- --config.directories.output=release",
|
||||
"build:preload-cache": "electron --js-args=\"--predictable --random-seed 1\" ts/scripts/generate-preload-cache.js",
|
||||
"verify": "run-p --print-label verify:*",
|
||||
"verify:ts": "tsc --noEmit",
|
||||
"electron:install-app-deps": "electron-builder install-app-deps"
|
||||
|
@ -534,6 +535,8 @@
|
|||
"app/*",
|
||||
"!app/*.ts",
|
||||
"preload.bundle.js",
|
||||
"preload.wrapper.js",
|
||||
"preload.bundle.cache",
|
||||
"preload_utils.js",
|
||||
"main.js",
|
||||
"images/**",
|
||||
|
|
126
preload.wrapper.ts
Normal file
126
preload.wrapper.ts
Normal file
|
@ -0,0 +1,126 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
// Based on:
|
||||
// https://github.com/zertosh/v8-compile-cache/blob/b6bc035d337fbda0e6e3ec7936499048fc9deafc/v8-compile-cache.js
|
||||
|
||||
import { Module } from 'node:module';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join, dirname } from 'node:path';
|
||||
import { Script } from 'node:vm';
|
||||
|
||||
const srcPath = join(__dirname, 'preload.bundle.js');
|
||||
const cachePath = join(__dirname, 'preload.bundle.cache');
|
||||
|
||||
let cachedData: Buffer | undefined;
|
||||
try {
|
||||
cachedData = readFileSync(cachePath);
|
||||
} catch (error) {
|
||||
// No cache - no big deal
|
||||
if (error.code !== 'ENOENT') {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function compile(
|
||||
filename: string,
|
||||
content: string
|
||||
): (..._args: Array<unknown>) => void {
|
||||
// https://github.com/nodejs/node/blob/v7.5.0/lib/module.js#L511
|
||||
|
||||
// create wrapper function
|
||||
const wrapper = Module.wrap(content);
|
||||
|
||||
const script = new Script(wrapper, {
|
||||
filename,
|
||||
lineOffset: 0,
|
||||
cachedData,
|
||||
});
|
||||
|
||||
const compiledWrapper = script.runInThisContext({
|
||||
filename,
|
||||
lineOffset: 0,
|
||||
columnOffset: 0,
|
||||
displayErrors: true,
|
||||
});
|
||||
|
||||
return compiledWrapper;
|
||||
}
|
||||
|
||||
const ModuleInternals = Module as unknown as {
|
||||
prototype: {
|
||||
_compile(
|
||||
this: typeof ModuleInternals,
|
||||
content: string,
|
||||
filename: string
|
||||
): unknown;
|
||||
};
|
||||
require(id: string): unknown;
|
||||
exports: unknown;
|
||||
_resolveFilename(
|
||||
request: unknown,
|
||||
mod: unknown,
|
||||
_: false,
|
||||
options: unknown
|
||||
): unknown;
|
||||
_resolveLookupPaths(request: unknown, mod: unknown, _: true): unknown;
|
||||
_cache: unknown;
|
||||
_extensions: unknown;
|
||||
};
|
||||
|
||||
const previousModuleCompile = ModuleInternals.prototype._compile;
|
||||
ModuleInternals.prototype._compile = function _compile(
|
||||
content: string,
|
||||
filename: string
|
||||
) {
|
||||
if (filename !== srcPath) {
|
||||
throw new Error(`Unexpected filename: ${filename}`);
|
||||
}
|
||||
|
||||
// Immediately restore
|
||||
ModuleInternals.prototype._compile = previousModuleCompile;
|
||||
|
||||
const require = (id: string) => {
|
||||
return this.require(id);
|
||||
};
|
||||
|
||||
// https://github.com/nodejs/node/blob/v10.15.3/lib/internal/modules/cjs/helpers.js#L28
|
||||
const resolve = (request: unknown, options: unknown) => {
|
||||
return ModuleInternals._resolveFilename(request, this, false, options);
|
||||
};
|
||||
require.resolve = resolve;
|
||||
|
||||
resolve.paths = (request: unknown) => {
|
||||
return ModuleInternals._resolveLookupPaths(request, this, true);
|
||||
};
|
||||
|
||||
require.main = process.mainModule;
|
||||
|
||||
// Enable support to add extra extension types
|
||||
require.extensions = ModuleInternals._extensions;
|
||||
require.cache = ModuleInternals._cache;
|
||||
|
||||
const dir = dirname(filename);
|
||||
|
||||
const compiledWrapper = compile(filename, content);
|
||||
|
||||
// We skip the debugger setup because by the time we run, node has already
|
||||
// done that itself.
|
||||
|
||||
// `Buffer` is included for Electron.
|
||||
// See https://github.com/zertosh/v8-compile-cache/pull/10#issuecomment-518042543
|
||||
const args = [
|
||||
this.exports,
|
||||
require,
|
||||
this,
|
||||
filename,
|
||||
dir,
|
||||
process,
|
||||
global,
|
||||
Buffer,
|
||||
];
|
||||
return compiledWrapper.apply(this.exports, args);
|
||||
};
|
||||
|
||||
// eslint-disable-next-line import/no-dynamic-require
|
||||
require(srcPath);
|
|
@ -103,12 +103,15 @@ async function main() {
|
|||
...nodeDefaults,
|
||||
format: 'cjs',
|
||||
mainFields: ['browser', 'main'],
|
||||
entryPoints: glob
|
||||
.sync('{app,ts}/**/*.{ts,tsx}', {
|
||||
nodir: true,
|
||||
root: ROOT_DIR,
|
||||
})
|
||||
.filter(file => !file.endsWith('.d.ts')),
|
||||
entryPoints: [
|
||||
'preload.wrapper.ts',
|
||||
...glob
|
||||
.sync('{app,ts}/**/*.{ts,tsx}', {
|
||||
nodir: true,
|
||||
root: ROOT_DIR,
|
||||
})
|
||||
.filter(file => !file.endsWith('.d.ts')),
|
||||
],
|
||||
outdir: path.join(ROOT_DIR),
|
||||
},
|
||||
preloadConfig: {
|
||||
|
|
12
ts/scripts/generate-preload-cache.html
Normal file
12
ts/scripts/generate-preload-cache.html
Normal file
|
@ -0,0 +1,12 @@
|
|||
<!-- Copyright 2014 Signal Messenger, LLC -->
|
||||
<!-- SPDX-License-Identifier: AGPL-3.0-only -->
|
||||
|
||||
<!doctype html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Generating cache...</title>
|
||||
</head>
|
||||
<body>
|
||||
Generating cache...
|
||||
</body>
|
||||
</html>
|
29
ts/scripts/generate-preload-cache.preload.ts
Normal file
29
ts/scripts/generate-preload-cache.preload.ts
Normal file
|
@ -0,0 +1,29 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { Module } from 'node:module';
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
import { Script } from 'node:vm';
|
||||
import { ipcRenderer } from 'electron';
|
||||
|
||||
ipcRenderer.on('compile', async () => {
|
||||
try {
|
||||
const sourceFile = join(__dirname, '..', '..', 'preload.bundle.js');
|
||||
const outFile = sourceFile.replace(/\.js$/, '');
|
||||
|
||||
const source = await readFile(sourceFile, 'utf8');
|
||||
const script = new Script(Module.wrap(source), {
|
||||
filename: 'preload.bundle.js',
|
||||
produceCachedData: true,
|
||||
});
|
||||
if (!script.cachedDataProduced || !script.cachedData) {
|
||||
throw new Error('Cached data not produced');
|
||||
}
|
||||
|
||||
await writeFile(`${outFile}.cache`, script.cachedData);
|
||||
await ipcRenderer.invoke('done');
|
||||
} catch (error) {
|
||||
await ipcRenderer.invoke('error', error);
|
||||
}
|
||||
});
|
35
ts/scripts/generate-preload-cache.ts
Normal file
35
ts/scripts/generate-preload-cache.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { join } from 'node:path';
|
||||
import { app, BrowserWindow, ipcMain } from 'electron';
|
||||
|
||||
app.on('ready', async () => {
|
||||
ipcMain.handle('done', () => {
|
||||
app.quit();
|
||||
});
|
||||
|
||||
ipcMain.handle('error', (_event, err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
const window = new BrowserWindow({
|
||||
show: false,
|
||||
webPreferences: {
|
||||
devTools: true,
|
||||
nodeIntegration: false,
|
||||
sandbox: false,
|
||||
contextIsolation: true,
|
||||
preload: join(__dirname, 'generate-preload-cache.preload.js'),
|
||||
},
|
||||
});
|
||||
|
||||
await window.loadURL(
|
||||
pathToFileURL(join(__dirname, 'generate-preload-cache.html')).toString()
|
||||
);
|
||||
|
||||
window.webContents.openDevTools();
|
||||
window.webContents.send('compile', process.argv[2], process.argv[3]);
|
||||
});
|
|
@ -15,14 +15,14 @@ let archive: string;
|
|||
let exe: string;
|
||||
if (process.platform === 'darwin') {
|
||||
archive = join(
|
||||
'mac',
|
||||
'mac-arm64',
|
||||
`${packageJson.productName}.app`,
|
||||
'Contents',
|
||||
'Resources',
|
||||
'app.asar'
|
||||
);
|
||||
exe = join(
|
||||
'mac',
|
||||
'mac-arm64',
|
||||
`${packageJson.productName}.app`,
|
||||
'Contents',
|
||||
'MacOS',
|
||||
|
|
Loading…
Reference in a new issue