From da7962fd4dfcf83d0d671b0c295623740ed1981f Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 23 Sep 2023 06:48:34 -0400 Subject: [PATCH] lib: fix `internalBinding` typings PR-URL: https://github.com/nodejs/node/pull/49742 Reviewed-By: Chemi Atlow Reviewed-By: Geoffrey Booth Reviewed-By: Zeyu "Alex" Yang Reviewed-By: Moshe Atlow --- lib/internal/bootstrap/realm.js | 4 +- tsconfig.json | 18 +-------- typings/globals.d.ts | 48 ++++++++++++++++++++++++ typings/internalBinding/async_wrap.d.ts | 4 +- typings/internalBinding/blob.d.ts | 4 +- typings/internalBinding/config.d.ts | 4 +- typings/internalBinding/constants.d.ts | 4 +- typings/internalBinding/fs.d.ts | 8 ++-- typings/internalBinding/http_parser.d.ts | 4 +- typings/internalBinding/messaging.d.ts | 4 +- typings/internalBinding/options.d.ts | 4 +- typings/internalBinding/os.d.ts | 4 +- typings/internalBinding/serdes.d.ts | 4 +- typings/internalBinding/symbols.d.ts | 4 +- typings/internalBinding/timers.d.ts | 4 +- typings/internalBinding/types.d.ts | 4 +- typings/internalBinding/url.d.ts | 4 +- typings/internalBinding/util.d.ts | 4 +- typings/internalBinding/worker.d.ts | 6 ++- typings/primordials.d.ts | 9 +---- 20 files changed, 88 insertions(+), 61 deletions(-) diff --git a/lib/internal/bootstrap/realm.js b/lib/internal/bootstrap/realm.js index 608e3072850d45..f9d096ca963464 100644 --- a/lib/internal/bootstrap/realm.js +++ b/lib/internal/bootstrap/realm.js @@ -174,9 +174,9 @@ const experimentalModuleList = new SafeSet(); }; } -// Set up internalBinding() in the closure. /** - * @type {InternalBinding} + * Set up internalBinding() in the closure. + * @type {import('typings/globals').internalBinding} */ let internalBinding; { diff --git a/tsconfig.json b/tsconfig.json index f42ed9ad0eac67..3f5a3067ced063 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,22 +2,6 @@ "include": ["lib", "doc"], "exclude": ["src", "tools", "out"], "files": [ - "./typings/internalBinding/async_wrap.d.ts", - "./typings/internalBinding/blob.d.ts", - "./typings/internalBinding/config.d.ts", - "./typings/internalBinding/constants.d.ts", - "./typings/internalBinding/fs.d.ts", - "./typings/internalBinding/http_parser.d.ts", - "./typings/internalBinding/messaging.d.ts", - "./typings/internalBinding/options.d.ts", - "./typings/internalBinding/os.d.ts", - "./typings/internalBinding/serdes.d.ts", - "./typings/internalBinding/symbols.d.ts", - "./typings/internalBinding/timers.d.ts", - "./typings/internalBinding/types.d.ts", - "./typings/internalBinding/url.d.ts", - "./typings/internalBinding/util.d.ts", - "./typings/internalBinding/worker.d.ts", "./typings/globals.d.ts", "./typings/primordials.d.ts" ], @@ -25,7 +9,7 @@ "allowJs": true, "checkJs": false, "noEmit": true, - "lib": ["ESNext"], + "lib": ["ESNext", "DOM"], "target": "ESNext", "module": "CommonJS", "baseUrl": ".", diff --git a/typings/globals.d.ts b/typings/globals.d.ts index 182284aa342718..d72bf937bb75c9 100644 --- a/typings/globals.d.ts +++ b/typings/globals.d.ts @@ -1,3 +1,20 @@ +import {AsyncWrapBinding} from "./internalBinding/async_wrap"; +import {BlobBinding} from "./internalBinding/blob"; +import {ConfigBinding} from "./internalBinding/config"; +import {ConstantsBinding} from "./internalBinding/constants"; +import {HttpParserBinding} from "./internalBinding/http_parser"; +import {FsBinding} from "./internalBinding/fs"; +import {MessagingBinding} from "./internalBinding/messaging"; +import {OptionsBinding} from "./internalBinding/options"; +import {OSBinding} from "./internalBinding/os"; +import {SerdesBinding} from "./internalBinding/serdes"; +import {SymbolsBinding} from "./internalBinding/symbols"; +import {TimersBinding} from "./internalBinding/timers"; +import {TypesBinding} from "./internalBinding/types"; +import {URLBinding} from "./internalBinding/url"; +import {UtilBinding} from "./internalBinding/util"; +import {WorkerBinding} from "./internalBinding/worker"; + declare type TypedArray = | Uint8Array | Uint8ClampedArray @@ -10,3 +27,34 @@ declare type TypedArray = | Float64Array | BigUint64Array | BigInt64Array; + +interface InternalBindingMap { + async_wrap: AsyncWrapBinding; + blob: BlobBinding; + config: ConfigBinding; + constants: ConstantsBinding; + fs: FsBinding; + http_parser: HttpParserBinding; + messaging: MessagingBinding; + options: OptionsBinding; + os: OSBinding; + serdes: SerdesBinding; + symbols: SymbolsBinding; + timers: TimersBinding; + types: TypesBinding; + url: URLBinding; + util: UtilBinding; + worker: WorkerBinding; +} + +type InternalBindingKeys = keyof InternalBindingMap; + +declare function internalBinding(binding: T): InternalBindingMap[T] + +declare global { + namespace NodeJS { + interface Global { + internalBinding(binding: T): InternalBindingMap[T] + } + } +} diff --git a/typings/internalBinding/async_wrap.d.ts b/typings/internalBinding/async_wrap.d.ts index bfbda3d7ed890b..a8b583c56967bb 100644 --- a/typings/internalBinding/async_wrap.d.ts +++ b/typings/internalBinding/async_wrap.d.ts @@ -72,7 +72,7 @@ declare namespace InternalAsyncWrapBinding { } } -declare function InternalBinding(binding: 'async_wrap'): { +export interface AsyncWrapBinding { setupHooks(): { init: ( asyncId: number, @@ -129,4 +129,4 @@ declare function InternalBinding(binding: 'async_wrap'): { kDefaultTriggerAsyncId: 3; }; Providers: InternalAsyncWrapBinding.Providers; -}; +} diff --git a/typings/internalBinding/blob.d.ts b/typings/internalBinding/blob.d.ts index 41813d0a619607..5396c863bb5e59 100644 --- a/typings/internalBinding/blob.d.ts +++ b/typings/internalBinding/blob.d.ts @@ -10,10 +10,10 @@ declare namespace InternalBlobBinding { } } -declare function InternalBinding(binding: 'blob'): { +export interface BlobBinding { createBlob(sources: Array, length: number): InternalBlobBinding.BlobHandle; FixedSizeBlobCopyJob: typeof InternalBlobBinding.FixedSizeBlobCopyJob; getDataObject(id: string): [handle: InternalBlobBinding.BlobHandle | undefined, length: number, type: string] | undefined; storeDataObject(id: string, handle: InternalBlobBinding.BlobHandle, size: number, type: string): void; revokeDataObject(id: string): void; -}; +} diff --git a/typings/internalBinding/config.d.ts b/typings/internalBinding/config.d.ts index d9f34e1131ced5..7c93e34238915e 100644 --- a/typings/internalBinding/config.d.ts +++ b/typings/internalBinding/config.d.ts @@ -1,4 +1,4 @@ -declare function InternalBinding(binding: 'config'): { +export interface ConfigBinding { isDebugBuild: boolean; hasOpenSSL: boolean; fipsMode: boolean; @@ -9,4 +9,4 @@ declare function InternalBinding(binding: 'config'): { noBrowserGlobals: boolean; bits: number; hasDtrace: boolean; -}; +} diff --git a/typings/internalBinding/constants.d.ts b/typings/internalBinding/constants.d.ts index 68fead278cc64e..0495724e85869c 100644 --- a/typings/internalBinding/constants.d.ts +++ b/typings/internalBinding/constants.d.ts @@ -1,4 +1,4 @@ -declare function InternalBinding(binding: 'constants'): { +export interface ConstantsBinding { os: { UV_UDP_REUSEADDR: 4; dlopen: { @@ -382,4 +382,4 @@ declare function InternalBinding(binding: 'constants'): { TRACE_EVENT_PHASE_LEAVE_CONTEXT: 41; TRACE_EVENT_PHASE_LINK_IDS: 61; }; -}; +} diff --git a/typings/internalBinding/fs.d.ts b/typings/internalBinding/fs.d.ts index 4576913bf1366b..d87b326e42efc8 100644 --- a/typings/internalBinding/fs.d.ts +++ b/typings/internalBinding/fs.d.ts @@ -109,7 +109,7 @@ declare namespace InternalFSBinding { function internalModuleReadJSON(path: string): [] | [string, boolean]; function internalModuleStat(path: string): number; - + function lchown(path: string, uid: number, gid: number, req: FSReqCallback): void; function lchown(path: string, uid: number, gid: number, req: undefined, ctx: FSSyncContext): void; function lchown(path: string, uid: number, gid: number, usePromises: typeof kUsePromises): Promise; @@ -198,7 +198,7 @@ declare namespace InternalFSBinding { function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, req: FSReqCallback): void; function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, req: undefined, ctx: FSSyncContext): void; function symlink(target: StringOrBuffer, path: StringOrBuffer, type: number, usePromises: typeof kUsePromises): Promise; - + function unlink(path: string, req: FSReqCallback): void; function unlink(path: string, req: undefined, ctx: FSSyncContext): void; function unlink(path: string, usePromises: typeof kUsePromises): Promise; @@ -220,7 +220,7 @@ declare namespace InternalFSBinding { function writeString(fd: number, value: string, pos: unknown, encoding: unknown, usePromises: typeof kUsePromises): Promise; } -declare function InternalBinding(binding: 'fs'): { +export interface FsBinding { FSReqCallback: typeof InternalFSBinding.FSReqCallback; FileHandle: typeof InternalFSBinding.FileHandle; @@ -269,4 +269,4 @@ declare function InternalBinding(binding: 'fs'): { writeBuffer: typeof InternalFSBinding.writeBuffer; writeBuffers: typeof InternalFSBinding.writeBuffers; writeString: typeof InternalFSBinding.writeString; -}; +} diff --git a/typings/internalBinding/http_parser.d.ts b/typings/internalBinding/http_parser.d.ts index bd9a22e55b12e5..5ab5651b637176 100644 --- a/typings/internalBinding/http_parser.d.ts +++ b/typings/internalBinding/http_parser.d.ts @@ -39,7 +39,7 @@ declare namespace InternalHttpParserBinding { } } -declare function InternalBinding(binding: 'http_parser'): { +export interface HttpParserBinding { methods: string[]; HTTPParser: typeof InternalHttpParserBinding.HTTPParser; -}; +} diff --git a/typings/internalBinding/messaging.d.ts b/typings/internalBinding/messaging.d.ts index ca5be84848408d..93a1a050887503 100644 --- a/typings/internalBinding/messaging.d.ts +++ b/typings/internalBinding/messaging.d.ts @@ -17,7 +17,7 @@ declare namespace InternalMessagingBinding { } -declare function InternalBinding(binding: 'messaging'): { +export interface MessagingBinding { DOMException: typeof import('internal/per_context/domexception').DOMException; MessageChannel: typeof InternalMessagingBinding.MessageChannel; MessagePort: typeof InternalMessagingBinding.MessagePort; @@ -29,4 +29,4 @@ declare function InternalBinding(binding: 'messaging'): { moveMessagePortToContext(port: typeof InternalMessagingBinding.MessagePort, context: any): typeof InternalMessagingBinding.MessagePort; setDeserializerCreateObjectFunction(func: (deserializeInfo: string) => any): void; broadcastChannel(name: string): typeof InternalMessagingBinding.MessagePort; -}; +} diff --git a/typings/internalBinding/options.d.ts b/typings/internalBinding/options.d.ts index ded726f9d71127..8248997d463654 100644 --- a/typings/internalBinding/options.d.ts +++ b/typings/internalBinding/options.d.ts @@ -1,4 +1,4 @@ -declare function InternalBinding(binding: 'options'): { +export interface OptionsBinding { getOptions(): { options: Map< string, @@ -32,4 +32,4 @@ declare function InternalBinding(binding: 'options'): { kHostPort: 6; kStringList: 7; }; -}; +} diff --git a/typings/internalBinding/os.d.ts b/typings/internalBinding/os.d.ts index 161d9fd4b46836..91e907ef3c1205 100644 --- a/typings/internalBinding/os.d.ts +++ b/typings/internalBinding/os.d.ts @@ -2,7 +2,7 @@ declare namespace InternalOSBinding { type OSContext = {}; } -declare function InternalBinding(binding: 'os'): { +export interface OSBinding { getHostname(ctx: InternalOSBinding.OSContext): string | undefined; getLoadAvg(array: Float64Array): void; getUptime(): number; @@ -22,4 +22,4 @@ declare function InternalBinding(binding: 'os'): { getPriority(pid: number, ctx: InternalOSBinding.OSContext): number | undefined; getOSInformation(ctx: InternalOSBinding.OSContext): [sysname: string, version: string, release: string]; isBigEndian: boolean; -}; +} diff --git a/typings/internalBinding/serdes.d.ts b/typings/internalBinding/serdes.d.ts index b3577c658d2a9f..20b010151cb74a 100644 --- a/typings/internalBinding/serdes.d.ts +++ b/typings/internalBinding/serdes.d.ts @@ -30,7 +30,7 @@ declare namespace InternalSerdesBinding { } } -declare function InternalBinding(binding: 'serdes'): { +export interface SerdesBinding { Serializer: typeof InternalSerdesBinding.Serializer; Deserializer: typeof InternalSerdesBinding.Deserializer; -}; +} diff --git a/typings/internalBinding/symbols.d.ts b/typings/internalBinding/symbols.d.ts index ac85f9e67e8aa2..96310970d5cdee 100644 --- a/typings/internalBinding/symbols.d.ts +++ b/typings/internalBinding/symbols.d.ts @@ -10,7 +10,7 @@ export const owner_symbol: unique symbol; export const onpskexchange_symbol: unique symbol; export const trigger_async_id_symbol: unique symbol; -declare function InternalBinding(binding: 'symbols'): { +export interface SymbolsBinding { async_id_symbol: typeof async_id_symbol; handle_onclose_symbol: typeof handle_onclose_symbol; no_message_symbol: typeof no_message_symbol; @@ -22,4 +22,4 @@ declare function InternalBinding(binding: 'symbols'): { owner_symbol: typeof owner_symbol; onpskexchange_symbol: typeof onpskexchange_symbol; trigger_async_id_symbol: typeof trigger_async_id_symbol; -}; +} diff --git a/typings/internalBinding/timers.d.ts b/typings/internalBinding/timers.d.ts index fd6d40ecfee5cc..e03deeeca24008 100644 --- a/typings/internalBinding/timers.d.ts +++ b/typings/internalBinding/timers.d.ts @@ -1,8 +1,8 @@ -declare function InternalBinding(binding: 'timers'): { +export interface TimersBinding { getLibuvNow(): number; setupTimers(immediateCallback: () => void, timersCallback: (now: number) => void): void; scheduleTimer(msecs: number): void; toggleTimerRef(value: boolean): void; toggleImmediateRef(value: boolean): void; immediateInfo: Uint32Array; -}; +} diff --git a/typings/internalBinding/types.d.ts b/typings/internalBinding/types.d.ts index c8efea84b69e9d..0c0d3ec81b5aae 100644 --- a/typings/internalBinding/types.d.ts +++ b/typings/internalBinding/types.d.ts @@ -1,4 +1,4 @@ -declare function InternalBinding(binding: 'types'): { +export interface TypesBinding { isAsyncFunction(value: unknown): value is (...args: unknown[]) => Promise; isGeneratorFunction(value: unknown): value is GeneratorFunction; isAnyArrayBuffer(value: unknown): value is (ArrayBuffer | SharedArrayBuffer); @@ -23,4 +23,4 @@ declare function InternalBinding(binding: 'types'): { isNumberObject: (value: unknown) => value is Number; isBooleanObject: (value: unknown) => value is Boolean, isBigIntObject: (value: unknown) => value is BigInt; -}; +} diff --git a/typings/internalBinding/url.d.ts b/typings/internalBinding/url.d.ts index 4c8ae2ab1508f0..03d5f6d750f358 100644 --- a/typings/internalBinding/url.d.ts +++ b/typings/internalBinding/url.d.ts @@ -1,6 +1,6 @@ import type { urlUpdateActions } from 'internal/url' -declare function InternalBinding(binding: 'url'): { +export interface URLBinding { urlComponents: Uint32Array; domainToASCII(input: string): string; @@ -9,4 +9,4 @@ declare function InternalBinding(binding: 'url'): { format(input: string, fragment?: boolean, unicode?: boolean, search?: boolean, auth?: boolean): string; parse(input: string, base?: string): string | false; update(input: string, actionType: typeof urlUpdateActions, value: string): string | false; -}; +} diff --git a/typings/internalBinding/util.d.ts b/typings/internalBinding/util.d.ts index 002f8eb26d93e6..1ac9d3a39bbb31 100644 --- a/typings/internalBinding/util.d.ts +++ b/typings/internalBinding/util.d.ts @@ -7,7 +7,7 @@ declare namespace InternalUtilBinding { } } -declare function InternalBinding(binding: 'util'): { +export interface UtilBinding { // PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES, defined in src/env_properties.h arrow_message_private_symbol: 1; contextify_context_private_symbol: 2; @@ -44,4 +44,4 @@ declare function InternalBinding(binding: 'util'): { WeakReference: typeof InternalUtilBinding.WeakReference; guessHandleType(fd: number): 'TCP' | 'TTY' | 'UDP' | 'FILE' | 'PIPE' | 'UNKNOWN'; toUSVString(str: string, start: number): string; -}; +} diff --git a/typings/internalBinding/worker.d.ts b/typings/internalBinding/worker.d.ts index 52bfc53c29dc0a..10cb5fc5db6e32 100644 --- a/typings/internalBinding/worker.d.ts +++ b/typings/internalBinding/worker.d.ts @@ -1,3 +1,5 @@ +import { InternalMessagingBinding } from './messaging'; + declare namespace InternalWorkerBinding { class Worker { constructor( @@ -18,7 +20,7 @@ declare namespace InternalWorkerBinding { } } -declare function InternalBinding(binding: 'worker'): { +export interface WorkerBinding { Worker: typeof InternalWorkerBinding.Worker; getEnvMessagePort(): InternalMessagingBinding.MessagePort; threadId: number; @@ -30,4 +32,4 @@ declare function InternalBinding(binding: 'worker'): { kCodeRangeSizeMb: number; kStackSizeMb: number; kTotalResourceLimitCount: number; -}; +} diff --git a/typings/primordials.d.ts b/typings/primordials.d.ts index 8590812f4155e4..c0a4dd2348bf03 100644 --- a/typings/primordials.d.ts +++ b/typings/primordials.d.ts @@ -203,7 +203,6 @@ declare namespace primordials { export const DatePrototypeToTimeString: UncurryThis export const DatePrototypeToISOString: UncurryThis export const DatePrototypeToUTCString: UncurryThis - export const DatePrototypeToGMTString: UncurryThis export const DatePrototypeGetDate: UncurryThis export const DatePrototypeSetDate: UncurryThis export const DatePrototypeGetDay: UncurryThis @@ -238,8 +237,6 @@ declare namespace primordials { export const DatePrototypeGetUTCSeconds: UncurryThis export const DatePrototypeSetUTCSeconds: UncurryThis export const DatePrototypeValueOf: UncurryThis - export const DatePrototypeGetYear: UncurryThis - export const DatePrototypeSetYear: UncurryThis export const DatePrototypeToJSON: UncurryThis export const DatePrototypeToLocaleString: UncurryThis export const DatePrototypeToLocaleDateString: UncurryThis @@ -247,8 +244,8 @@ declare namespace primordials { export const DatePrototypeSymbolToPrimitive: UncurryMethod; export import Error = globalThis.Error; export const ErrorPrototype: typeof Error.prototype + // @ts-ignore export const ErrorCaptureStackTrace: typeof Error.captureStackTrace - export const ErrorStackTraceLimit: typeof Error.stackTraceLimit export const ErrorPrototypeToString: UncurryThis export import EvalError = globalThis.EvalError; export const EvalErrorPrototype: typeof EvalError.prototype @@ -332,11 +329,7 @@ declare namespace primordials { export const ObjectEntries: typeof Object.entries export const ObjectFromEntries: typeof Object.fromEntries export const ObjectValues: typeof Object.values - export const ObjectPrototype__defineGetter__: UncurryThis - export const ObjectPrototype__defineSetter__: UncurryThis export const ObjectPrototypeHasOwnProperty: UncurryThis - export const ObjectPrototype__lookupGetter__: UncurryThis - export const ObjectPrototype__lookupSetter__: UncurryThis export const ObjectPrototypeIsPrototypeOf: UncurryThis export const ObjectPrototypePropertyIsEnumerable: UncurryThis export const ObjectPrototypeToString: UncurryThis