From e71a56d11e5457b9dc7ff56735c97a2200a2428a Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 7 Sep 2023 00:04:25 +0200 Subject: [PATCH] refactor: const Module = require('module') as NodeJS.ModuleInternal; (#38757) Co-authored-by: Milan Burda --- lib/asar/fs-wrapper.ts | 2 +- lib/browser/init.ts | 2 +- lib/common/reset-search-paths.ts | 6 +++--- lib/renderer/init.ts | 2 +- lib/utility/init.ts | 2 +- lib/worker/init.ts | 2 +- spec/modules-spec.ts | 2 +- typings/internal-ambient.d.ts | 11 +++++++++++ 8 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/asar/fs-wrapper.ts b/lib/asar/fs-wrapper.ts index 3423d845f809..2427c9473f84 100644 --- a/lib/asar/fs-wrapper.ts +++ b/lib/asar/fs-wrapper.ts @@ -5,7 +5,7 @@ import type * as Crypto from 'crypto'; const asar = process._linkedBinding('electron_common_asar'); -const Module = require('module'); +const Module = require('module') as NodeJS.ModuleInternal; const Promise: PromiseConstructor = global.Promise; diff --git a/lib/browser/init.ts b/lib/browser/init.ts index 7f921cb8a12b..5fb42e84fec5 100644 --- a/lib/browser/init.ts +++ b/lib/browser/init.ts @@ -4,7 +4,7 @@ import * as path from 'path'; import type * as defaultMenuModule from '@electron/internal/browser/default-menu'; -const Module = require('module'); +const Module = require('module') as NodeJS.ModuleInternal; // We modified the original process.argv to let node.js load the init.js, // we need to restore it here. diff --git a/lib/common/reset-search-paths.ts b/lib/common/reset-search-paths.ts index ebb72862a8c8..4268613532d7 100644 --- a/lib/common/reset-search-paths.ts +++ b/lib/common/reset-search-paths.ts @@ -1,6 +1,6 @@ import * as path from 'path'; -const Module = require('module'); +const Module = require('module') as NodeJS.ModuleInternal; // We do not want to allow use of the VM module in the renderer process as // it conflicts with Blink's V8::Context internal logic. @@ -10,7 +10,7 @@ if (process.type === 'renderer') { if (request === 'vm') { console.warn('The vm module of Node.js is deprecated in the renderer process and will be removed.'); } - return _load.apply(this, arguments); + return _load.apply(this, arguments as any); }; } @@ -60,7 +60,7 @@ const originalResolveFilename = Module._resolveFilename; // renderer process regardless of the names, they're superficial for TypeScript // only. const electronModuleNames = new Set(['electron', 'electron/main', 'electron/renderer', 'electron/common']); -Module._resolveFilename = function (request: string, parent: NodeModule, isMain: boolean, options?: { paths: Array}) { +Module._resolveFilename = function (request, parent, isMain, options) { if (electronModuleNames.has(request)) { return 'electron'; } else { diff --git a/lib/renderer/init.ts b/lib/renderer/init.ts index 7bd00af472de..3c1a150d6b89 100644 --- a/lib/renderer/init.ts +++ b/lib/renderer/init.ts @@ -5,7 +5,7 @@ import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages'; import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal'; import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils'; -const Module = require('module'); +const Module = require('module') as NodeJS.ModuleInternal; // Make sure globals like "process" and "global" are always available in preload // scripts even after they are deleted in "loaded" script. diff --git a/lib/utility/init.ts b/lib/utility/init.ts index d360dd514e51..5cf1ee1fdef9 100644 --- a/lib/utility/init.ts +++ b/lib/utility/init.ts @@ -1,5 +1,5 @@ import { ParentPort } from '@electron/internal/utility/parent-port'; -const Module = require('module'); +const Module = require('module') as NodeJS.ModuleInternal; const v8Util = process._linkedBinding('electron_common_v8_util'); const entryScript: string = v8Util.getHiddenValue(process, '_serviceStartupScript'); diff --git a/lib/worker/init.ts b/lib/worker/init.ts index a72f643c8ac4..7c2beaa07078 100644 --- a/lib/worker/init.ts +++ b/lib/worker/init.ts @@ -1,6 +1,6 @@ import * as path from 'path'; -const Module = require('module'); +const Module = require('module') as NodeJS.ModuleInternal; // We modified the original process.argv to let node.js load the // init.js, we need to restore it here. diff --git a/spec/modules-spec.ts b/spec/modules-spec.ts index a6e8bc2e6822..22cf62804310 100644 --- a/spec/modules-spec.ts +++ b/spec/modules-spec.ts @@ -7,7 +7,7 @@ import { closeAllWindows } from './lib/window-helpers'; import * as childProcess from 'node:child_process'; import { once } from 'node:events'; -const Module = require('node:module'); +const Module = require('node:module') as NodeJS.ModuleInternal; const nativeModulesEnabled = !process.env.ELECTRON_SKIP_NATIVE_MODULE_TESTS; diff --git a/typings/internal-ambient.d.ts b/typings/internal-ambient.d.ts index 1805f58de461..9fa99fb93292 100644 --- a/typings/internal-ambient.d.ts +++ b/typings/internal-ambient.d.ts @@ -3,6 +3,17 @@ declare const BUILDFLAG: (flag: boolean) => boolean; declare const ENABLE_VIEWS_API: boolean; declare namespace NodeJS { + interface ModuleInternal extends NodeJS.Module { + new(id: string, parent?: NodeJS.Module | null): NodeJS.Module; + _load(request: string, parent?: NodeJS.Module | null, isMain?: boolean): any; + _resolveFilename(request: string, parent?: NodeJS.Module | null, isMain?: boolean, options?: { paths: string[] }): string; + _preloadModules(requests: string[]): void; + _nodeModulePaths(from: string): string[]; + _extensions: Record any>; + _cache: Record; + wrapper: [string, string]; + } + interface FeaturesBinding { isBuiltinSpellCheckerEnabled(): boolean; isPDFViewerEnabled(): boolean;