From 7c58645aca2b53d15dbf698efa43cf934a436d91 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 10 Sep 2024 23:24:56 +0200 Subject: [PATCH] lib: move `Symbol[Async]Dispose` polyfills to `internal/util` PR-URL: https://github.com/nodejs/node/pull/54853 Reviewed-By: Moshe Atlow Reviewed-By: Matteo Collina Reviewed-By: Chengzhong Wu Reviewed-By: Benjamin Gruenbaum Reviewed-By: Marco Ippolito Reviewed-By: James M Snell --- lib/_http_server.js | 2 +- lib/child_process.js | 2 +- lib/dgram.js | 4 +--- lib/events.js | 3 +-- lib/https.js | 2 +- lib/inspector.js | 5 ++++- lib/internal/child_process.js | 3 +-- lib/internal/events/abort_listener.js | 2 +- lib/internal/fs/promises.js | 2 +- lib/internal/http2/core.js | 4 ++-- lib/internal/per_context/primordials.js | 5 ----- lib/internal/process/pre_execution.js | 4 ++-- lib/internal/readline/interface.js | 3 +-- lib/internal/streams/add-abort-signal.js | 2 +- lib/internal/streams/end-of-stream.js | 2 +- lib/internal/streams/pipeline.js | 3 +-- lib/internal/streams/readable.js | 4 +++- lib/internal/streams/writable.js | 4 +++- lib/internal/test_runner/mock/mock_timers.js | 2 +- lib/internal/test_runner/test.js | 2 +- lib/internal/util.js | 8 ++++++++ lib/internal/webstreams/readablestream.js | 2 +- lib/net.js | 11 ++++++++--- lib/readline.js | 2 +- lib/readline/promises.js | 2 +- lib/timers.js | 2 +- typings/primordials.d.ts | 2 -- 27 files changed, 48 insertions(+), 41 deletions(-) diff --git a/lib/_http_server.js b/lib/_http_server.js index b6e2ba69bc0648..a9616740dd6f86 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -29,7 +29,6 @@ const { ObjectSetPrototypeOf, ReflectApply, Symbol, - SymbolAsyncDispose, SymbolFor, } = primordials; @@ -82,6 +81,7 @@ const { const { kEmptyObject, promisify, + SymbolAsyncDispose, } = require('internal/util'); const { validateInteger, diff --git a/lib/child_process.js b/lib/child_process.js index 4e15255a01007b..580a441a803bdd 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -42,7 +42,6 @@ const { StringPrototypeIncludes, StringPrototypeSlice, StringPrototypeToUpperCase, - SymbolDispose, } = primordials; const { @@ -51,6 +50,7 @@ const { getSystemErrorName, kEmptyObject, promisify, + SymbolDispose, } = require('internal/util'); const { isArrayBufferView } = require('internal/util/types'); let debug = require('internal/util/debuglog').debuglog( diff --git a/lib/dgram.js b/lib/dgram.js index 9b411246afc545..5f93526e301f43 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -30,8 +30,6 @@ const { ObjectDefineProperty, ObjectSetPrototypeOf, ReflectApply, - SymbolAsyncDispose, - SymbolDispose, } = primordials; const { @@ -63,7 +61,7 @@ const { validatePort, } = require('internal/validators'); const { Buffer } = require('buffer'); -const { deprecate, guessHandleType, promisify } = require('internal/util'); +const { deprecate, guessHandleType, promisify, SymbolAsyncDispose, SymbolDispose } = require('internal/util'); const { isArrayBufferView } = require('internal/util/types'); const EventEmitter = require('events'); const { addAbortListener } = require('internal/events/abort_listener'); diff --git a/lib/events.js b/lib/events.js index 1d9239d276049b..36217a8313c134 100644 --- a/lib/events.js +++ b/lib/events.js @@ -47,12 +47,11 @@ const { StringPrototypeSplit, Symbol, SymbolAsyncIterator, - SymbolDispose, SymbolFor, } = primordials; const kRejection = SymbolFor('nodejs.rejection'); -const { kEmptyObject } = require('internal/util'); +const { SymbolDispose, kEmptyObject } = require('internal/util'); const { inspect, diff --git a/lib/https.js b/lib/https.js index ec68296ed518c4..21b40275ecb691 100644 --- a/lib/https.js +++ b/lib/https.js @@ -33,13 +33,13 @@ const { ObjectSetPrototypeOf, ReflectApply, ReflectConstruct, - SymbolAsyncDispose, } = primordials; const { assertCrypto, kEmptyObject, promisify, + SymbolAsyncDispose, } = require('internal/util'); assertCrypto(); diff --git a/lib/inspector.js b/lib/inspector.js index b623d96b68c3f7..b1d67b84fe04d6 100644 --- a/lib/inspector.js +++ b/lib/inspector.js @@ -4,9 +4,12 @@ const { JSONParse, JSONStringify, SafeMap, - SymbolDispose, } = primordials; +const { + SymbolDispose, +} = require('internal/util'); + const { ERR_INSPECTOR_ALREADY_ACTIVATED, ERR_INSPECTOR_ALREADY_CONNECTED, diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js index 816ebe17fcf11b..c694a8f26fe425 100644 --- a/lib/internal/child_process.js +++ b/lib/internal/child_process.js @@ -12,7 +12,6 @@ const { ReflectApply, StringPrototypeSlice, Symbol, - SymbolDispose, Uint8Array, } = primordials; @@ -56,7 +55,7 @@ const { TTY } = internalBinding('tty_wrap'); const { UDP } = internalBinding('udp_wrap'); const SocketList = require('internal/socket_list'); const { owner_symbol } = require('internal/async_hooks').symbols; -const { convertToValidSignal, deprecate } = require('internal/util'); +const { convertToValidSignal, deprecate, SymbolDispose } = require('internal/util'); const { isArrayBufferView } = require('internal/util/types'); const spawn_sync = internalBinding('spawn_sync'); const { kStateSymbol } = require('internal/dgram'); diff --git a/lib/internal/events/abort_listener.js b/lib/internal/events/abort_listener.js index 033cbf7b250fe0..2803fa585d29df 100644 --- a/lib/internal/events/abort_listener.js +++ b/lib/internal/events/abort_listener.js @@ -2,7 +2,7 @@ const { SymbolDispose, -} = primordials; +} = require('internal/util'); const { validateAbortSignal, validateFunction, diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 1d1fdb8463ca8d..fd57191ef9869f 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -15,7 +15,6 @@ const { SafeArrayIterator, SafePromisePrototypeFinally, Symbol, - SymbolAsyncDispose, Uint8Array, uncurryThis, } = primordials; @@ -100,6 +99,7 @@ const { promisify, isWindows, isMacOS, + SymbolAsyncDispose, } = require('internal/util'); const EventEmitter = require('events'); const { StringDecoder } = require('string_decoder'); diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 4dc4eaf547e238..923db9ad1b5b1e 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -19,8 +19,6 @@ const { SafeMap, SafeSet, Symbol, - SymbolAsyncDispose, - SymbolDispose, Uint32Array, Uint8Array, } = primordials; @@ -30,6 +28,8 @@ const { customInspectSymbol: kInspect, kEmptyObject, promisify, + SymbolAsyncDispose, + SymbolDispose, } = require('internal/util'); assertCrypto(); diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 6a31f17fb863cb..9851f3f64f6e56 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -229,11 +229,6 @@ function copyPrototype(src, dest, prefix) { copyPrototype(original.prototype, primordials, `${name}Prototype`); }); -// Define Symbol.dispose and Symbol.asyncDispose -// Until these are defined by the environment. -// TODO(MoLow): Remove this polyfill once Symbol.dispose and Symbol.asyncDispose are available in V8. -primordials.SymbolDispose ??= primordials.SymbolFor('nodejs.dispose'); -primordials.SymbolAsyncDispose ??= primordials.SymbolFor('nodejs.asyncDispose'); // Create copies of intrinsic objects that require a valid `this` to call // static methods. diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index df0c437027201a..287ebd3e8ed30b 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -15,8 +15,6 @@ const { String, StringPrototypeStartsWith, Symbol, - SymbolAsyncDispose, - SymbolDispose, globalThis, } = primordials; @@ -31,6 +29,8 @@ const { defineReplaceableLazyAttribute, setupCoverageHooks, emitExperimentalWarning, + SymbolAsyncDispose, + SymbolDispose, } = require('internal/util'); const { diff --git a/lib/internal/readline/interface.js b/lib/internal/readline/interface.js index 74d2e91e98b145..7c1a15f3c70012 100644 --- a/lib/internal/readline/interface.js +++ b/lib/internal/readline/interface.js @@ -30,7 +30,6 @@ const { StringPrototypeTrim, Symbol, SymbolAsyncIterator, - SymbolDispose, } = primordials; const { codes: { @@ -45,7 +44,7 @@ const { validateString, validateUint32, } = require('internal/validators'); -const { kEmptyObject } = require('internal/util'); +const { SymbolDispose, kEmptyObject } = require('internal/util'); const { inspect, getStringWidth, diff --git a/lib/internal/streams/add-abort-signal.js b/lib/internal/streams/add-abort-signal.js index 2fa16d697c11b6..9d76fda29a6e48 100644 --- a/lib/internal/streams/add-abort-signal.js +++ b/lib/internal/streams/add-abort-signal.js @@ -2,7 +2,7 @@ const { SymbolDispose, -} = primordials; +} = require('internal/util'); const { AbortError, diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index c44f99e993ad5b..c234792bd33ed4 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -6,7 +6,6 @@ const { Promise, PromisePrototypeThen, - SymbolDispose, } = primordials; const { @@ -19,6 +18,7 @@ const { const { kEmptyObject, once, + SymbolDispose, } = require('internal/util'); const { validateAbortSignal, diff --git a/lib/internal/streams/pipeline.js b/lib/internal/streams/pipeline.js index 83c53d80a32f5f..c3cc742a0de14a 100644 --- a/lib/internal/streams/pipeline.js +++ b/lib/internal/streams/pipeline.js @@ -7,11 +7,10 @@ const { ArrayIsArray, Promise, SymbolAsyncIterator, - SymbolDispose, } = primordials; const eos = require('internal/streams/end-of-stream'); -const { once } = require('internal/util'); +const { SymbolDispose, once } = require('internal/util'); const destroyImpl = require('internal/streams/destroy'); const Duplex = require('internal/streams/duplex'); const { diff --git a/lib/internal/streams/readable.js b/lib/internal/streams/readable.js index fb587622550452..17f8e53ad56339 100644 --- a/lib/internal/streams/readable.js +++ b/lib/internal/streams/readable.js @@ -32,7 +32,6 @@ const { Promise, SafeSet, Symbol, - SymbolAsyncDispose, SymbolAsyncIterator, SymbolSpecies, TypedArrayPrototypeSet, @@ -44,6 +43,9 @@ Readable.ReadableState = ReadableState; const EE = require('events'); const { Stream, prependListener } = require('internal/streams/legacy'); const { Buffer } = require('buffer'); +const { + SymbolAsyncDispose, +} = require('internal/util'); const { addAbortSignal, diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index ea89abf97c7604..83178c2429c791 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -35,7 +35,6 @@ const { Promise, StringPrototypeToLowerCase, Symbol, - SymbolAsyncDispose, SymbolHasInstance, } = primordials; @@ -47,6 +46,9 @@ const Stream = require('internal/streams/legacy').Stream; const { Buffer } = require('buffer'); const destroyImpl = require('internal/streams/destroy'); const eos = require('internal/streams/end-of-stream'); +const { + SymbolAsyncDispose, +} = require('internal/util'); const { addAbortSignal, diff --git a/lib/internal/test_runner/mock/mock_timers.js b/lib/internal/test_runner/mock/mock_timers.js index 400ca5e37f026e..dc77fc429935b6 100644 --- a/lib/internal/test_runner/mock/mock_timers.js +++ b/lib/internal/test_runner/mock/mock_timers.js @@ -17,7 +17,6 @@ const { Promise, Symbol, SymbolAsyncIterator, - SymbolDispose, globalThis, } = primordials; @@ -29,6 +28,7 @@ const { const { emitExperimentalWarning, + SymbolDispose, } = require('internal/util'); const { AbortError, diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 09387f89c36c34..26a65eda67dd69 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -26,7 +26,6 @@ const { StringPrototypeStartsWith, StringPrototypeTrim, Symbol, - SymbolDispose, } = primordials; const { getCallerLocation } = internalBinding('util'); const { exitCodes: { kGenericUserError } } = internalBinding('errors'); @@ -52,6 +51,7 @@ const { createDeferredPromise, kEmptyObject, once: runOnce, + SymbolDispose, } = require('internal/util'); const { isPromise } = require('internal/util/types'); const { diff --git a/lib/internal/util.js b/lib/internal/util.js index f9de6cd77c71f4..37d6ef7a93e477 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -936,6 +936,14 @@ module.exports = { setupCoverageHooks, removeColors, + // Define Symbol.dispose and Symbol.asyncDispose + // Until these are defined by the environment. + // TODO(MoLow): Remove this polyfill once Symbol.dispose and Symbol.asyncDispose are available in primordials. + // eslint-disable-next-line node-core/prefer-primordials + SymbolDispose: Symbol.dispose || SymbolFor('nodejs.dispose'), + // eslint-disable-next-line node-core/prefer-primordials + SymbolAsyncDispose: Symbol.asyncDispose || SymbolFor('nodejs.asyncDispose'), + // Symbol used to customize promisify conversion customPromisifyArgs: kCustomPromisifyArgsSymbol, diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index f37f5174918c6d..68ef663c530fe6 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -20,7 +20,6 @@ const { SafePromiseAll, Symbol, SymbolAsyncIterator, - SymbolDispose, SymbolToStringTag, TypedArrayPrototypeGetLength, Uint8Array, @@ -54,6 +53,7 @@ const { kEmptyObject, kEnumerableProperty, SideEffectFreeRegExpPrototypeSymbolReplace, + SymbolDispose, } = require('internal/util'); const { diff --git a/lib/net.js b/lib/net.js index be21c566610286..4591e3b977cc51 100644 --- a/lib/net.js +++ b/lib/net.js @@ -36,8 +36,6 @@ const { ObjectDefineProperty, ObjectSetPrototypeOf, Symbol, - SymbolAsyncDispose, - SymbolDispose, } = primordials; const EventEmitter = require('events'); @@ -116,7 +114,14 @@ const { } = require('internal/errors'); const { isUint8Array } = require('internal/util/types'); const { queueMicrotask } = require('internal/process/task_queues'); -const { kEmptyObject, guessHandleType, promisify, isWindows } = require('internal/util'); +const { + guessHandleType, + isWindows, + kEmptyObject, + promisify, + SymbolAsyncDispose, + SymbolDispose, +} = require('internal/util'); const { validateAbortSignal, validateBoolean, diff --git a/lib/readline.js b/lib/readline.js index 509c7022ee95cc..abc5b25d2e00e6 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -30,7 +30,6 @@ const { Promise, PromiseReject, StringPrototypeSlice, - SymbolDispose, } = primordials; const { @@ -51,6 +50,7 @@ const { const { kEmptyObject, promisify, + SymbolDispose, } = require('internal/util'); const { validateAbortSignal } = require('internal/validators'); diff --git a/lib/readline/promises.js b/lib/readline/promises.js index ccd0745a33f941..286222bfab9a61 100644 --- a/lib/readline/promises.js +++ b/lib/readline/promises.js @@ -2,7 +2,6 @@ const { Promise, - SymbolDispose, } = primordials; const { @@ -22,6 +21,7 @@ const { validateAbortSignal } = require('internal/validators'); const { kEmptyObject, + SymbolDispose, } = require('internal/util'); let addAbortListener; diff --git a/lib/timers.js b/lib/timers.js index 9de6b8887dd943..b594f79d120e40 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -26,7 +26,6 @@ const { MathTrunc, ObjectDefineProperties, ObjectDefineProperty, - SymbolDispose, SymbolToPrimitive, } = primordials; @@ -58,6 +57,7 @@ const { const { promisify: { custom: customPromisify }, deprecate, + SymbolDispose, } = require('internal/util'); let debug = require('internal/util/debuglog').debuglog('timer', (fn) => { debug = fn; diff --git a/typings/primordials.d.ts b/typings/primordials.d.ts index e5426b85b5c29b..987239c01245a7 100644 --- a/typings/primordials.d.ts +++ b/typings/primordials.d.ts @@ -429,8 +429,6 @@ declare namespace primordials { export const SymbolFor: typeof Symbol.for export const SymbolKeyFor: typeof Symbol.keyFor export const SymbolAsyncIterator: typeof Symbol.asyncIterator - export const SymbolDispose: typeof Symbol.dispose - export const SymbolAsyncDispose: typeof Symbol.asyncDispose export const SymbolHasInstance: typeof Symbol.hasInstance export const SymbolIsConcatSpreadable: typeof Symbol.isConcatSpreadable export const SymbolIterator: typeof Symbol.iterator