ESM: revisit need for `_VSCODE_NODE_MODULES` (fix #226453) (#227427)

This commit is contained in:
Benjamin Pasero 2024-09-03 15:18:08 +02:00 коммит произвёл GitHub
Родитель b0a79def0b
Коммит d60295dc0e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
11 изменённых файлов: 18 добавлений и 63 удалений

9
src/bootstrap-amd.js поставляемый
Просмотреть файл

@ -20,6 +20,7 @@ import { product, pkg } from './bootstrap-meta.js';
import './bootstrap-node.js';
import * as performance from './vs/base/common/performance.js';
/** @ts-ignore */
const require = createRequire(import.meta.url);
/** @type any */
const module = { exports: {} };
@ -45,14 +46,6 @@ if (process.env['ELECTRON_RUN_AS_NODE'] || process.versions['electron']) {
}
// ESM-uncomment-end
// Store the node.js require function in a variable
// before loading our AMD loader to avoid issues
// when this file is bundled with other files.
const nodeRequire = require;
// VSCODE_GLOBALS: node_modules
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => nodeRequire(String(mod)) });
// VSCODE_GLOBALS: package/product.json
/** @type Partial<IProductConfiguration> */
// ESM-comment-begin

32
src/typings/vscode-globals-modules.d.ts поставляемый
Просмотреть файл

@ -1,32 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// AMD2ESM migration relevant
declare global {
/**
* TODO@esm @deprecated node modules that are in used in a context that
* shouldn't have access to node_modules (node-free renderer or
* shared process)
*/
var _VSCODE_NODE_MODULES: {
crypto: typeof import('crypto');
zlib: typeof import('zlib');
net: typeof import('net');
os: typeof import('os');
module: typeof import('module');
fs: typeof import('fs'),
vm: typeof import('vm'),
['native-watchdog']: typeof import('native-watchdog')
perf_hooks: typeof import('perf_hooks');
['vsda']: any
['vscode-encrypt']: any
}
}
// fake export to make global work
export { }

Просмотреть файл

@ -185,9 +185,9 @@ class AMDModuleImporter {
private async _nodeJSLoadScript(scriptSrc: string): Promise<DefineCall | undefined> {
try {
const fs = (globalThis as any)._VSCODE_NODE_MODULES['fs'];
const vm = (globalThis as any)._VSCODE_NODE_MODULES['vm'];
const module = (globalThis as any)._VSCODE_NODE_MODULES['module'];
const fs = (await import(`${'fs'}`)).default;
const vm = (await import(`${'vm'}`)).default;
const module = (await import(`${'module'}`)).default;
const filePath = URI.parse(scriptSrc).fsPath;
const content = fs.readFileSync(filePath).toString();
@ -196,7 +196,6 @@ class AMDModuleImporter {
const compileWrapper = script.runInThisContext();
compileWrapper.apply();
return this._defineCalls.pop();
} catch (error) {
throw error;
}

Просмотреть файл

@ -40,6 +40,10 @@ import { determineServerConnectionToken, requestHasValidConnectionToken as httpR
import { IServerEnvironmentService, ServerParsedArgs } from './serverEnvironmentService.js';
import { setupServerServices, SocketServer } from './serverServices.js';
import { CacheControl, serveError, serveFile, WebClientServer } from './webClientServer.js';
// ESM-uncomment-begin
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
// ESM-uncomment-end
const SHUTDOWN_TIMEOUT = 5 * 60 * 1000;
@ -768,7 +772,7 @@ export async function createServer(address: string | net.AddressInfo | null, arg
const hasVSDA = fs.existsSync(join(FileAccess.asFileUri('').fsPath, '../node_modules/vsda'));
if (hasVSDA) {
try {
return <typeof vsda>globalThis._VSCODE_NODE_MODULES['vsda'];
return require('vsda');
} catch (err) {
logService.error(err);
}

Просмотреть файл

@ -27,7 +27,7 @@ class NodeModuleRequireInterceptor extends RequireInterceptor {
protected _installInterceptor(): void {
const that = this;
const node_module = <any>globalThis._VSCODE_NODE_MODULES.module;
const node_module = require('module');
const originalLoad = node_module._load;
node_module._load = function load(request: string, parent: { filename: string }, isMain: boolean) {
request = applyAlternatives(request);

Просмотреть файл

@ -25,10 +25,13 @@ import { IHostUtils } from '../common/extHostExtensionService.js';
import { createURITransformer } from './uriTransformer.js';
import { ExtHostConnectionType, readExtHostConnection } from '../../services/extensions/common/extensionHostEnv.js';
import { ExtensionHostExitCode, IExtHostReadyMessage, IExtHostReduceGraceTimeMessage, IExtHostSocketMessage, IExtensionHostInitData, MessageType, createMessageOfType, isMessageOfType } from '../../services/extensions/common/extensionHostProtocol.js';
import { IDisposable } from '../../../base/common/lifecycle.js';
import '../common/extHost.common.services.js';
import './extHost.node.services.js';
// ESM-uncomment-begin
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
// ESM-uncomment-end
interface ParsedExtHostArgs {
transformURIs?: boolean;
@ -63,7 +66,7 @@ const args = minimist(process.argv.slice(2), {
// happening we essentially blocklist this module from getting loaded in any
// extension by patching the node require() function.
(function () {
const Module = globalThis._VSCODE_NODE_MODULES.module as any;
const Module = require('module');
const originalLoad = Module._load;
Module._load = function (request: string) {
@ -327,7 +330,7 @@ function connectToRenderer(protocol: IMessagePassingProtocol): Promise<IRenderer
// So also use the native node module to do it from a separate thread
let watchdog: typeof nativeWatchdog;
try {
watchdog = globalThis._VSCODE_NODE_MODULES['native-watchdog'];
watchdog = require('native-watchdog');
watchdog.start(initData.parentPid);
} catch (err) {
// no problem...

Просмотреть файл

@ -122,7 +122,7 @@ const modulesCache = new Map<IExtensionDescription | undefined, { http?: typeof
function configureModuleLoading(extensionService: ExtHostExtensionService, lookup: ReturnType<typeof createPatchedModules>): Promise<void> {
return extensionService.getExtensionPathIndex()
.then(extensionPaths => {
const node_module = <any>globalThis._VSCODE_NODE_MODULES.module;
const node_module = require('module');
const original = node_module._load;
node_module._load = function load(request: string, parent: { filename: string }, isMain: boolean) {
if (request === 'net') {

Просмотреть файл

@ -74,9 +74,6 @@ if (util.inspect && util.inspect['defaultOptions']) {
util.inspect['defaultOptions'].customInspect = false;
}
// VSCODE_GLOBALS: node_modules
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => (require.__$__nodeRequire ?? require)(String(mod)) });
// VSCODE_GLOBALS: package/product.json
globalThis._VSCODE_PRODUCT_JSON = (require.__$__nodeRequire ?? require)('../../../product.json');
globalThis._VSCODE_PACKAGE_JSON = (require.__$__nodeRequire ?? require)('../../../package.json');

Просмотреть файл

@ -73,9 +73,6 @@ if (util.inspect && util.inspect['defaultOptions']) {
util.inspect['defaultOptions'].customInspect = false;
}
// VSCODE_GLOBALS: node_modules
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => require(String(mod)) });
// VSCODE_GLOBALS: package/product.json
globalThis._VSCODE_PRODUCT_JSON = require('../../../product.json');
globalThis._VSCODE_PACKAGE_JSON = require('../../../package.json');

Просмотреть файл

@ -77,9 +77,6 @@ if (majorRequiredNodeVersion !== currentMajorNodeVersion) {
function main() {
// VSCODE_GLOBALS: node_modules
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => require(String(mod)) });
// VSCODE_GLOBALS: package/product.json
globalThis._VSCODE_PRODUCT_JSON = require(`${REPO_ROOT}/product.json`);
globalThis._VSCODE_PACKAGE_JSON = require(`${REPO_ROOT}/package.json`);

Просмотреть файл

@ -79,11 +79,8 @@ if (majorRequiredNodeVersion !== currentMajorNodeVersion) {
function main() {
// VSCODE_GLOBALS: node_modules
const _require = module.createRequire(import.meta.url);
globalThis._VSCODE_NODE_MODULES = new Proxy(Object.create(null), { get: (_target, mod) => _require(String(mod)) });
// VSCODE_GLOBALS: package/product.json
const _require = module.createRequire(import.meta.url);
globalThis._VSCODE_PRODUCT_JSON = _require(`${REPO_ROOT}/product.json`);
globalThis._VSCODE_PACKAGE_JSON = _require(`${REPO_ROOT}/package.json`);