forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 5
/
preload.wrapper.ts
137 lines (113 loc) · 3.49 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 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, writeFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { Script } 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 {
cachedData = readFileSync(cachePath);
} catch (error) {
// No cache - no big deal
if (error.code !== 'ENOENT') {
throw error;
}
}
let script: Script | undefined;
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);
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;
};
if (cachedData || process.env.GENERATE_PRELOAD_CACHE) {
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);
// See `ts/scripts/generate-preload-cache.ts`
if (script && process.env.GENERATE_PRELOAD_CACHE) {
writeFileSync(cachePath, script.createCachedData());
ipcRenderer.send('shutdown');
}