From dac29f99492fda71dc79e1ee8874bee36a3b2ae6 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 5 Feb 2024 08:36:28 +0100 Subject: [PATCH] refactor: type-safe module imports / requires (#41192) --- lib/browser/init.ts | 6 ++++-- lib/common/init.ts | 5 +++-- lib/node/asar-fs-wrapper.ts | 4 +++- lib/sandboxed_renderer/init.ts | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/browser/init.ts b/lib/browser/init.ts index 47a694f5d57b..97568c5d68f0 100644 --- a/lib/browser/init.ts +++ b/lib/browser/init.ts @@ -3,6 +3,8 @@ import * as fs from 'fs'; import * as path from 'path'; import type * as defaultMenuModule from '@electron/internal/browser/default-menu'; +import type * as url from 'url'; +import type * as v8 from 'v8'; const Module = require('module') as NodeJS.ModuleInternal; @@ -132,7 +134,7 @@ if (packageJson.desktopName != null) { // Set v8 flags, deliberately lazy load so that apps that do not use this // feature do not pay the price if (packageJson.v8Flags != null) { - require('v8').setFlagsFromString(packageJson.v8Flags); + (require('v8') as typeof v8).setFlagsFromString(packageJson.v8Flags); } app.setAppPath(packagePath); @@ -199,7 +201,7 @@ if (packagePath) { // Finally load app's main.js and transfer control to C++. if ((packageJson.type === 'module' && !mainStartupScript.endsWith('.cjs')) || mainStartupScript.endsWith('.mjs')) { const { loadESM } = __non_webpack_require__('internal/process/esm_loader'); - const main = require('url').pathToFileURL(path.join(packagePath, mainStartupScript)); + const main = (require('url') as typeof url).pathToFileURL(path.join(packagePath, mainStartupScript)); loadESM(async (esmLoader: any) => { try { await esmLoader.import(main.toString(), undefined, Object.create(null)); diff --git a/lib/common/init.ts b/lib/common/init.ts index 097f8fa6418c..9032ed9994be 100644 --- a/lib/common/init.ts +++ b/lib/common/init.ts @@ -1,6 +1,7 @@ import * as util from 'util'; +import type * as stream from 'stream'; -const timers = require('timers'); +import timers = require('timers'); type AnyFn = (...args: any[]) => any @@ -62,7 +63,7 @@ if (process.type === 'browser' || if (process.platform === 'win32') { // Always returns EOF for stdin stream. - const { Readable } = require('stream'); + const { Readable } = require('stream') as typeof stream; const stdin = new Readable(); stdin.push(null); Object.defineProperty(process, 'stdin', { diff --git a/lib/node/asar-fs-wrapper.ts b/lib/node/asar-fs-wrapper.ts index eeb6e06239f8..e3dc73e9c838 100644 --- a/lib/node/asar-fs-wrapper.ts +++ b/lib/node/asar-fs-wrapper.ts @@ -2,7 +2,9 @@ import { Buffer } from 'buffer'; import { constants } from 'fs'; import * as path from 'path'; import * as util from 'util'; + import type * as Crypto from 'crypto'; +import type * as os from 'os'; const asar = process._linkedBinding('electron_common_asar'); @@ -255,7 +257,7 @@ export const wrapFsWithAsar = (fs: Record) => { if (!process.env.ELECTRON_LOG_ASAR_READS) return; if (!logFDs.has(asarPath)) { const logFilename = `${path.basename(asarPath, '.asar')}-access-log.txt`; - const logPath = path.join(require('os').tmpdir(), logFilename); + const logPath = path.join((require('os') as typeof os).tmpdir(), logFilename); logFDs.set(asarPath, fs.openSync(logPath, 'a')); } fs.writeSync(logFDs.get(asarPath), `${offset}: ${filePath}\n`); diff --git a/lib/sandboxed_renderer/init.ts b/lib/sandboxed_renderer/init.ts index fb3768b62ac5..83ddf4a1bd6d 100644 --- a/lib/sandboxed_renderer/init.ts +++ b/lib/sandboxed_renderer/init.ts @@ -1,4 +1,5 @@ import * as events from 'events'; +import { setImmediate, clearImmediate } from 'timers'; import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages'; import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils'; @@ -126,7 +127,6 @@ function runPreloadScript (preloadSrc: string) { // eval in window scope const preloadFn = binding.createPreloadScript(preloadWrapperSrc); - const { setImmediate, clearImmediate } = require('timers'); const exports = {}; preloadFn(preloadRequire, preloadProcess, Buffer, global, setImmediate, clearImmediate, exports, { exports });