Skip to content

Commit

Permalink
lib: fix internalBinding typings
Browse files Browse the repository at this point in the history
PR-URL: nodejs#49742
Reviewed-By: Chemi Atlow <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
Reviewed-By: Zeyu "Alex" Yang <[email protected]>
Reviewed-By: Moshe Atlow <[email protected]>
  • Loading branch information
anonrig authored Sep 23, 2023
1 parent c0b4208 commit da7962f
Show file tree
Hide file tree
Showing 20 changed files with 88 additions and 61 deletions.
4 changes: 2 additions & 2 deletions lib/internal/bootstrap/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
{
Expand Down
18 changes: 1 addition & 17 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,14 @@
"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"
],
"compilerOptions": {
"allowJs": true,
"checkJs": false,
"noEmit": true,
"lib": ["ESNext"],
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "CommonJS",
"baseUrl": ".",
Expand Down
48 changes: 48 additions & 0 deletions typings/globals.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<T extends InternalBindingKeys>(binding: T): InternalBindingMap[T]

declare global {
namespace NodeJS {
interface Global {
internalBinding<T extends InternalBindingKeys>(binding: T): InternalBindingMap[T]
}
}
}
4 changes: 2 additions & 2 deletions typings/internalBinding/async_wrap.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ declare namespace InternalAsyncWrapBinding {
}
}

declare function InternalBinding(binding: 'async_wrap'): {
export interface AsyncWrapBinding {
setupHooks(): {
init: (
asyncId: number,
Expand Down Expand Up @@ -129,4 +129,4 @@ declare function InternalBinding(binding: 'async_wrap'): {
kDefaultTriggerAsyncId: 3;
};
Providers: InternalAsyncWrapBinding.Providers;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/blob.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ declare namespace InternalBlobBinding {
}
}

declare function InternalBinding(binding: 'blob'): {
export interface BlobBinding {
createBlob(sources: Array<Uint8Array | InternalBlobBinding.BlobHandle>, 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;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare function InternalBinding(binding: 'config'): {
export interface ConfigBinding {
isDebugBuild: boolean;
hasOpenSSL: boolean;
fipsMode: boolean;
Expand All @@ -9,4 +9,4 @@ declare function InternalBinding(binding: 'config'): {
noBrowserGlobals: boolean;
bits: number;
hasDtrace: boolean;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/constants.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare function InternalBinding(binding: 'constants'): {
export interface ConstantsBinding {
os: {
UV_UDP_REUSEADDR: 4;
dlopen: {
Expand Down Expand Up @@ -382,4 +382,4 @@ declare function InternalBinding(binding: 'constants'): {
TRACE_EVENT_PHASE_LEAVE_CONTEXT: 41;
TRACE_EVENT_PHASE_LINK_IDS: 61;
};
};
}
8 changes: 4 additions & 4 deletions typings/internalBinding/fs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
Expand Down Expand Up @@ -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<void>;

function unlink(path: string, req: FSReqCallback): void;
function unlink(path: string, req: undefined, ctx: FSSyncContext): void;
function unlink(path: string, usePromises: typeof kUsePromises): Promise<void>;
Expand All @@ -220,7 +220,7 @@ declare namespace InternalFSBinding {
function writeString(fd: number, value: string, pos: unknown, encoding: unknown, usePromises: typeof kUsePromises): Promise<number>;
}

declare function InternalBinding(binding: 'fs'): {
export interface FsBinding {
FSReqCallback: typeof InternalFSBinding.FSReqCallback;

FileHandle: typeof InternalFSBinding.FileHandle;
Expand Down Expand Up @@ -269,4 +269,4 @@ declare function InternalBinding(binding: 'fs'): {
writeBuffer: typeof InternalFSBinding.writeBuffer;
writeBuffers: typeof InternalFSBinding.writeBuffers;
writeString: typeof InternalFSBinding.writeString;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/http_parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ declare namespace InternalHttpParserBinding {
}
}

declare function InternalBinding(binding: 'http_parser'): {
export interface HttpParserBinding {
methods: string[];
HTTPParser: typeof InternalHttpParserBinding.HTTPParser;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/messaging.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/options.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare function InternalBinding(binding: 'options'): {
export interface OptionsBinding {
getOptions(): {
options: Map<
string,
Expand Down Expand Up @@ -32,4 +32,4 @@ declare function InternalBinding(binding: 'options'): {
kHostPort: 6;
kStringList: 7;
};
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/os.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/serdes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ declare namespace InternalSerdesBinding {
}
}

declare function InternalBinding(binding: 'serdes'): {
export interface SerdesBinding {
Serializer: typeof InternalSerdesBinding.Serializer;
Deserializer: typeof InternalSerdesBinding.Deserializer;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/symbols.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/timers.d.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare function InternalBinding(binding: 'types'): {
export interface TypesBinding {
isAsyncFunction(value: unknown): value is (...args: unknown[]) => Promise<unknown>;
isGeneratorFunction(value: unknown): value is GeneratorFunction;
isAnyArrayBuffer(value: unknown): value is (ArrayBuffer | SharedArrayBuffer);
Expand All @@ -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;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/url.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { urlUpdateActions } from 'internal/url'

declare function InternalBinding(binding: 'url'): {
export interface URLBinding {
urlComponents: Uint32Array;

domainToASCII(input: string): string;
Expand All @@ -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;
};
}
4 changes: 2 additions & 2 deletions typings/internalBinding/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
};
}
6 changes: 4 additions & 2 deletions typings/internalBinding/worker.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { InternalMessagingBinding } from './messaging';

declare namespace InternalWorkerBinding {
class Worker {
constructor(
Expand All @@ -18,7 +20,7 @@ declare namespace InternalWorkerBinding {
}
}

declare function InternalBinding(binding: 'worker'): {
export interface WorkerBinding {
Worker: typeof InternalWorkerBinding.Worker;
getEnvMessagePort(): InternalMessagingBinding.MessagePort;
threadId: number;
Expand All @@ -30,4 +32,4 @@ declare function InternalBinding(binding: 'worker'): {
kCodeRangeSizeMb: number;
kStackSizeMb: number;
kTotalResourceLimitCount: number;
};
}
9 changes: 1 addition & 8 deletions typings/primordials.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ declare namespace primordials {
export const DatePrototypeToTimeString: UncurryThis<typeof Date.prototype.toTimeString>
export const DatePrototypeToISOString: UncurryThis<typeof Date.prototype.toISOString>
export const DatePrototypeToUTCString: UncurryThis<typeof Date.prototype.toUTCString>
export const DatePrototypeToGMTString: UncurryThis<typeof Date.prototype.toGMTString>
export const DatePrototypeGetDate: UncurryThis<typeof Date.prototype.getDate>
export const DatePrototypeSetDate: UncurryThis<typeof Date.prototype.setDate>
export const DatePrototypeGetDay: UncurryThis<typeof Date.prototype.getDay>
Expand Down Expand Up @@ -238,17 +237,15 @@ declare namespace primordials {
export const DatePrototypeGetUTCSeconds: UncurryThis<typeof Date.prototype.getUTCSeconds>
export const DatePrototypeSetUTCSeconds: UncurryThis<typeof Date.prototype.setUTCSeconds>
export const DatePrototypeValueOf: UncurryThis<typeof Date.prototype.valueOf>
export const DatePrototypeGetYear: UncurryThis<typeof Date.prototype.getYear>
export const DatePrototypeSetYear: UncurryThis<typeof Date.prototype.setYear>
export const DatePrototypeToJSON: UncurryThis<typeof Date.prototype.toJSON>
export const DatePrototypeToLocaleString: UncurryThis<typeof Date.prototype.toLocaleString>
export const DatePrototypeToLocaleDateString: UncurryThis<typeof Date.prototype.toLocaleDateString>
export const DatePrototypeToLocaleTimeString: UncurryThis<typeof Date.prototype.toLocaleTimeString>
export const DatePrototypeSymbolToPrimitive: UncurryMethod<typeof Date.prototype, typeof Symbol.toPrimitive>;
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<typeof Error.prototype.toString>
export import EvalError = globalThis.EvalError;
export const EvalErrorPrototype: typeof EvalError.prototype
Expand Down Expand Up @@ -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<typeof Object.prototype.__defineGetter__>
export const ObjectPrototype__defineSetter__: UncurryThis<typeof Object.prototype.__defineSetter__>
export const ObjectPrototypeHasOwnProperty: UncurryThis<typeof Object.prototype.hasOwnProperty>
export const ObjectPrototype__lookupGetter__: UncurryThis<typeof Object.prototype.__lookupGetter__>
export const ObjectPrototype__lookupSetter__: UncurryThis<typeof Object.prototype.__lookupSetter__>
export const ObjectPrototypeIsPrototypeOf: UncurryThis<typeof Object.prototype.isPrototypeOf>
export const ObjectPrototypePropertyIsEnumerable: UncurryThis<typeof Object.prototype.propertyIsEnumerable>
export const ObjectPrototypeToString: UncurryThis<typeof Object.prototype.toString>
Expand Down

0 comments on commit da7962f

Please sign in to comment.