Skip to content

Commit 0824db3

Browse files
committed
* add global for node_modules access
* remove most usages of require.__$__nodeRequire * stop using require.nodeRequire
1 parent f675b2b commit 0824db3

File tree

12 files changed

+82
-12
lines changed

12 files changed

+82
-12
lines changed

src/bootstrap-fork.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ if (process.env['VSCODE_PARENT_PID']) {
3737
terminateWhenParentTerminates();
3838
}
3939

40+
// VSCODE_GLOBALS: node_modules
41+
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), {
42+
get(target, mod) {
43+
if (!target[mod] && typeof mod === 'string') {
44+
target[mod] = require(mod);
45+
}
46+
return target[mod];
47+
}
48+
});
49+
50+
4051
// Load AMD entry point
4152
require('./bootstrap-amd').load(process.env['VSCODE_AMD_ENTRYPOINT']);
4253

src/bootstrap-window.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@
112112

113113
window['MonacoEnvironment'] = {};
114114

115+
// VSCODE_GLOBALS: node_modules
116+
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), {
117+
get(target, mod) {
118+
if (!target[mod] && typeof mod === 'string') {
119+
target[mod] = (require.__$__nodeRequire ?? require)(mod);
120+
}
121+
return target[mod];
122+
}
123+
});
124+
115125
const loaderConfig = {
116126
baseUrl: `${bootstrapLib.fileUriFromPath(configuration.appRoot, { isWindows: safeProcess.platform === 'win32', scheme: 'vscode-file', fallbackAuthority: 'vscode-app' })}/out`,
117127
'vs/nls': nlsConfig,

src/main.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ function startup(codeCachePath, nlsConfig) {
141141
process.env['VSCODE_NLS_CONFIG'] = JSON.stringify(nlsConfig);
142142
process.env['VSCODE_CODE_CACHE_PATH'] = codeCachePath || '';
143143

144+
// VSCODE_GLOBALS: node_modules
145+
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), {
146+
get(target, mod) {
147+
if (!target[mod] && typeof mod === 'string') {
148+
target[mod] = require(mod);
149+
}
150+
return target[mod];
151+
}
152+
});
153+
144154
// Load main in AMD
145155
perf.mark('code/willLoadMainBundle');
146156
require('./bootstrap-amd').load('vs/code/electron-main/main', () => {
@@ -318,6 +328,7 @@ function getArgvConfigPath() {
318328
dataFolderName = `${dataFolderName}-dev`;
319329
}
320330

331+
// @ts-ignore
321332
return path.join(os.homedir(), dataFolderName, 'argv.json');
322333
}
323334

src/typings/vscode-globals.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
declare global {
7+
8+
/**
9+
* @deprecated node modules that are in used in a context that
10+
* shouldn't have access to node_modules (node-free renderer or
11+
* shared process)
12+
*/
13+
var _VSCODE_NODE_MODULES: {
14+
crypto: typeof import('crypto');
15+
zlib: typeof import('zlib');
16+
net: typeof import('net');
17+
os: typeof import('os');
18+
module: typeof import('module');
19+
['native-watchdog']: typeof import('native-watchdog')
20+
perf_hooks: typeof import('perf_hooks');
21+
22+
['vsda']: any
23+
['vscode-encrypt']: any
24+
}
25+
}
26+
27+
// fake export to make global work
28+
export { }

src/vs/base/common/performance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
} else if (typeof process === 'object') {
7979
// node.js: use the normal polyfill but add the timeOrigin
8080
// from the node perf_hooks API as very first mark
81-
const timeOrigin = Math.round((require.nodeRequire || require)('perf_hooks').performance.timeOrigin);
81+
const timeOrigin = Math.round((require.__$__nodeRequire || require)('perf_hooks').performance.timeOrigin);
8282
return _definePolyfillMarks(timeOrigin);
8383

8484
} else {

src/vs/base/parts/ipc/node/ipc.net.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import { ChunkStream, Client, ISocket, Protocol, SocketCloseEvent, SocketCloseEv
2020
// TODO@bpasero remove me once electron utility process has landed
2121
function getNodeDependencies() {
2222
return {
23-
crypto: (require.__$__nodeRequire('crypto') as any) as typeof import('crypto'),
24-
zlib: (require.__$__nodeRequire('zlib') as any) as typeof import('zlib'),
25-
net: (require.__$__nodeRequire('net') as any) as typeof import('net'),
26-
os: (require.__$__nodeRequire('os') as any) as typeof import('os')
23+
crypto: globalThis._VSCODE_NODE_MODULES.crypto,
24+
zlib: globalThis._VSCODE_NODE_MODULES.zlib,
25+
net: globalThis._VSCODE_NODE_MODULES.net,
26+
os: globalThis._VSCODE_NODE_MODULES.os,
2727
};
2828
}
2929

src/vs/platform/environment/test/node/nativeModules.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ flakySuite('Native Modules (all platforms)', () => {
5858

5959
test('vscode-encrypt', async () => {
6060
try {
61-
const vscodeEncrypt: Encryption = require.__$__nodeRequire('vscode-encrypt');
61+
const vscodeEncrypt: Encryption = globalThis._VSCODE_NODE_MODULES['vscode-encrypt'];
6262
const encrypted = await vscodeEncrypt.encrypt('salt', 'value');
6363
const decrypted = await vscodeEncrypt.decrypt('salt', encrypted);
6464

@@ -73,7 +73,7 @@ flakySuite('Native Modules (all platforms)', () => {
7373

7474
test('vsda', async () => {
7575
try {
76-
const vsda: any = require.__$__nodeRequire('vsda');
76+
const vsda: any = globalThis._VSCODE_NODE_MODULES['vsda'];
7777
const signer = new vsda.signer();
7878
const signed = await signer.sign('value');
7979
assert.ok(typeof signed === 'string', testErrorMessage('vsda'));

src/vs/server/node/remoteExtensionHostAgentServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ export async function createServer(address: string | net.AddressInfo | null, arg
727727
const hasVSDA = fs.existsSync(join(FileAccess.asFileUri('').fsPath, '../node_modules/vsda'));
728728
if (hasVSDA) {
729729
try {
730-
return <typeof vsda>require.__$__nodeRequire('vsda');
730+
return <typeof vsda>globalThis._VSCODE_NODE_MODULES['vsda'];
731731
} catch (err) {
732732
logService.error(err);
733733
}

src/vs/workbench/api/node/extHostExtensionService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class NodeModuleRequireInterceptor extends RequireInterceptor {
2222

2323
protected _installInterceptor(): void {
2424
const that = this;
25-
const node_module = <any>require.__$__nodeRequire('module');
25+
const node_module = <any>globalThis._VSCODE_NODE_MODULES.module;
2626
const originalLoad = node_module._load;
2727
node_module._load = function load(request: string, parent: { filename: string }, isMain: boolean) {
2828
request = applyAlternatives(request);

src/vs/workbench/api/node/extensionHostProcess.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const args = minimist(process.argv.slice(2), {
8181
// happening we essentially blocklist this module from getting loaded in any
8282
// extension by patching the node require() function.
8383
(function () {
84-
const Module = require.__$__nodeRequire('module') as any;
84+
const Module = globalThis._VSCODE_NODE_MODULES.module as any;
8585
const originalLoad = Module._load;
8686

8787
Module._load = function (request: string) {
@@ -325,7 +325,7 @@ function connectToRenderer(protocol: IMessagePassingProtocol): Promise<IRenderer
325325
// So also use the native node module to do it from a separate thread
326326
let watchdog: typeof nativeWatchdog;
327327
try {
328-
watchdog = require.__$__nodeRequire('native-watchdog');
328+
watchdog = globalThis._VSCODE_NODE_MODULES['native-watchdog'];
329329
watchdog.start(initData.parentPid);
330330
} catch (err) {
331331
// no problem...

0 commit comments

Comments
 (0)