-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathpreload.wrapper.ts
77 lines (66 loc) · 1.99 KB
/
preload.wrapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { readFileSync, writeFileSync } from 'node:fs';
import { join, dirname, basename } from 'node:path';
import { Script, constants } from 'node:vm';
import { ipcRenderer } from 'electron';
const srcPath = join(__dirname, 'preload.bundle.js');
const cachePath = join(__dirname, 'preload.bundle.cache');
let cachedData: Buffer | undefined;
try {
if (!process.env.GENERATE_PRELOAD_CACHE) {
cachedData = readFileSync(cachePath);
}
} catch (error) {
// No cache - no big deal
if (error.code !== 'ENOENT') {
throw error;
}
}
const source = readFileSync(srcPath);
window.preloadCompileStartTime = Date.now();
// `filename` is used for:
//
// - Annotating stack traces
// - Resolving dynamic `import()` calls
//
// When it is not an absolute path `import()` is resolved relative to the `cwd`.
//
// Since filename gets written into the `preload.bundle.cache` we need to use a
// path-independent value for reproducibility, and otherwise use full absolute
// path in the packaged app.
const filename = process.env.GENERATE_PRELOAD_CACHE
? 'preload.bundle.js'
: srcPath;
const script = new Script(
`(function(require, __dirname){${source.toString()}})`,
{
filename,
lineOffset: 0,
cachedData,
importModuleDynamically: constants.USE_MAIN_CONTEXT_DEFAULT_LOADER,
}
);
const { cachedDataRejected } = script;
const fn = script.runInThisContext({
filename,
lineOffset: 0,
columnOffset: 0,
displayErrors: true,
importModuleDynamically: constants.USE_MAIN_CONTEXT_DEFAULT_LOADER,
});
// See `ts/scripts/generate-preload-cache.ts`
if (process.env.GENERATE_PRELOAD_CACHE) {
writeFileSync(cachePath, script.createCachedData());
ipcRenderer.send('shutdown');
} else {
fn(require, __dirname);
window.SignalCI?.setPreloadCacheHit(
cachedData != null && !cachedDataRejected
);
}
if (cachedDataRejected) {
console.log('preload cache rejected');
} else {
console.log('preload cache hit');
}