Skip to content

Commit 4b123af

Browse files
committed
[node] Update types to v20.5.0
https://github.com/nodejs/node/releases/tag/v20.5.0 - events: allow safely adding listener to abortSignal - test_runner: add shards support Also: - some classes became Disposable or AsyncDisposable - fix return type in `setInterval`
1 parent 77fd45a commit 4b123af

29 files changed

Lines changed: 1126 additions & 790 deletions

types/node/child_process.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ declare module 'child_process' {
302302
* @since v0.1.90
303303
*/
304304
kill(signal?: NodeJS.Signals | number): boolean;
305+
/**
306+
* Calls {@link ChildProcess.kill} with `'SIGTERM'`.
307+
* @since v20.5.0
308+
*/
309+
[Symbol.dispose](): void;
305310
/**
306311
* When an IPC channel has been established between the parent and child (
307312
* i.e. when using {@link fork}), the `subprocess.send()` method can

types/node/dgram.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,11 @@ declare module 'dgram' {
538538
prependOnceListener(event: 'error', listener: (err: Error) => void): this;
539539
prependOnceListener(event: 'listening', listener: () => void): this;
540540
prependOnceListener(event: 'message', listener: (msg: Buffer, rinfo: RemoteInfo) => void): this;
541+
/**
542+
* Calls `socket.close()` and returns a promise that fulfills when the socket has closed.
543+
* @since v20.5.0
544+
*/
545+
[Symbol.asyncDispose](): Promise<void>;
541546
}
542547
}
543548
declare module 'node:dgram' {

types/node/events.d.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,41 @@ declare module 'events' {
338338
* objects.
339339
*/
340340
static setMaxListeners(n?: number, ...eventTargets: Array<_DOMEventTarget | NodeJS.EventEmitter>): void;
341+
/**
342+
* Listens once to the `abort` event on the provided `signal`.
343+
*
344+
* Listening to the `abort` event on abort signals is unsafe and may
345+
* lead to resource leaks since another third party with the signal can
346+
* call `e.stopImmediatePropagation()`. Unfortunately Node.js cannot change
347+
* this since it would violate the web standard. Additionally, the original
348+
* API makes it easy to forget to remove listeners.
349+
*
350+
* This API allows safely using `AbortSignal`s in Node.js APIs by solving these
351+
* two issues by listening to the event such that `stopImmediatePropagation` does
352+
* not prevent the listener from running.
353+
*
354+
* Returns a disposable so that it may be unsubscribed from more easily.
355+
*
356+
* ```js
357+
* import { addAbortListener } from 'node:events';
358+
*
359+
* function example(signal) {
360+
* let disposable;
361+
* try {
362+
* signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
363+
* disposable = addAbortListener(signal, (e) => {
364+
* // Do something when signal is aborted.
365+
* });
366+
* } finally {
367+
* disposable?.[Symbol.dispose]();
368+
* }
369+
* }
370+
* ```
371+
* @since v20.5.0
372+
* @experimental
373+
* @return that removes the `abort` listener.
374+
*/
375+
static addAbortListener(signal: AbortSignal, resource: (event: Event) => void): Disposable;
341376
/**
342377
* This symbol shall be used to install a listener for only monitoring `'error'`events. Listeners installed using this symbol are called before the regular`'error'` listeners are called.
343378
*

types/node/fs/promises.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ declare module 'fs/promises' {
456456
* @return Fulfills with `undefined` upon success.
457457
*/
458458
close(): Promise<void>;
459+
/**
460+
* An alias for {@link FileHandle.close()}.
461+
* @since v20.4.0
462+
*/
463+
[Symbol.asyncDispose](): Promise<void>;
459464
}
460465
const constants: typeof fsConstants;
461466
/**

types/node/globals.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ declare var AbortSignal: typeof globalThis extends {onmessage: any; AbortSignal:
8484
};
8585
//#endregion borrowed
8686

87+
//#region Disposable
88+
interface SymbolConstructor {
89+
/**
90+
* A method that is used to release resources held by an object. Called by the semantics of the `using` statement.
91+
*/
92+
readonly dispose: unique symbol;
93+
94+
/**
95+
* A method that is used to asynchronously release resources held by an object. Called by the semantics of the `await using` statement.
96+
*/
97+
readonly asyncDispose: unique symbol;
98+
}
99+
100+
interface Disposable {
101+
[Symbol.dispose](): void;
102+
}
103+
104+
interface AsyncDisposable {
105+
[Symbol.asyncDispose](): PromiseLike<void>;
106+
}
107+
//#endregion Disposable
108+
87109
//#region ArrayLike.at()
88110
interface RelativeIndexable<T> {
89111
/**

types/node/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Type definitions for non-npm package Node.js 20.4
1+
// Type definitions for non-npm package Node.js 20.5
22
// Project: https://nodejs.org/
33
// Definitions by: Microsoft TypeScript <https://github.com/Microsoft>
44
// DefinitelyTyped <https://github.com/DefinitelyTyped>

types/node/net.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,11 @@ declare module 'net' {
639639
prependOnceListener(event: 'error', listener: (err: Error) => void): this;
640640
prependOnceListener(event: 'listening', listener: () => void): this;
641641
prependOnceListener(event: 'drop', listener: (data?: DropArgument) => void): this;
642+
/**
643+
* Calls {@link Server.close()} and returns a promise that fulfills when the server has closed.
644+
* @since v20.5.0
645+
*/
646+
[Symbol.asyncDispose](): Promise<void>;
642647
}
643648
type IPVersion = 'ipv4' | 'ipv6';
644649
/**

types/node/stream.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ declare module 'stream' {
477477
removeListener(event: 'resume', listener: () => void): this;
478478
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
479479
[Symbol.asyncIterator](): AsyncIterableIterator<any>;
480+
/**
481+
* Calls `readable.destroy()` with an `AbortError` and returns a promise that fulfills when the stream is finished.
482+
* @since v20.4.0
483+
*/
484+
[Symbol.asyncDispose](): Promise<void>;
480485
}
481486
import WritableOptions = internal.WritableOptions;
482487
class WritableBase extends Stream implements NodeJS.WritableStream {

types/node/test.d.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,16 @@ declare module 'node:test' {
259259
* If the test uses callbacks, the callback function is passed as an argument
260260
*/
261261
type SuiteFn = (s: SuiteContext) => void | Promise<void>;
262+
interface TestShard {
263+
/**
264+
* A positive integer between 1 and `<total>` that specifies the index of the shard to run.
265+
*/
266+
index: number;
267+
/**
268+
* A positive integer that specifies the total number of shards to split the test files to.
269+
*/
270+
total: number;
271+
}
262272
interface RunOptions {
263273
/**
264274
* If a number is provided, then that many files would run in parallel.
@@ -301,9 +311,15 @@ declare module 'node:test' {
301311
*/
302312
setup?: (root: Test) => void | Promise<void>;
303313
/**
304-
* Whether to run in watch mode or not. Default: false.
314+
* Whether to run in watch mode or not.
315+
* @default false
305316
*/
306-
watch?: boolean;
317+
watch?: boolean | undefined;
318+
/**
319+
* Running tests in a specific shard.
320+
* @default undefined
321+
*/
322+
shard?: TestShard | undefined;
307323
}
308324
class Test extends AsyncResource {
309325
concurrency: number;
@@ -1210,6 +1226,10 @@ declare module 'node:test' {
12101226
* @since v20.4.0
12111227
*/
12121228
runAll(): void;
1229+
/**
1230+
* Calls {@link MockTimers.reset()}.
1231+
*/
1232+
[Symbol.dispose](): void;
12131233
}
12141234
export { test as default, run, test, describe, it, before, after, beforeEach, afterEach, mock, skip, only, todo };
12151235
}

types/node/test/child_process.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ async function testPromisify() {
377377
_boolean = cp.kill();
378378
_boolean = cp.kill(9);
379379
_boolean = cp.kill("SIGTERM");
380+
cp[Symbol.dispose]();
380381

381382
_maybeNumber = cp.exitCode;
382383
_maybeSignal = cp.signalCode;

0 commit comments

Comments
 (0)