From c69b907d985b4e789945e4cecdb4dbe7f4620477 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 7 Sep 2024 13:16:42 -0400 Subject: [PATCH] lib: refactor project to use `Promise.withResolvers` --- lib/child_process.js | 4 +-- lib/internal/abort_controller.js | 4 +-- lib/internal/blob.js | 6 ++--- lib/internal/fs/watchers.js | 6 ++--- lib/internal/per_context/primordials.js | 13 ++++++++++ lib/internal/streams/duplexify.js | 6 ++--- lib/internal/test_runner/harness.js | 4 +-- lib/internal/test_runner/runner.js | 4 +-- lib/internal/test_runner/test.js | 6 ++--- lib/internal/test_runner/utils.js | 4 +-- lib/internal/util.js | 12 --------- lib/internal/webstreams/adapters.js | 12 ++++----- lib/internal/webstreams/readablestream.js | 26 +++++++++---------- lib/internal/webstreams/transfer.js | 9 +++---- lib/internal/webstreams/transformstream.js | 12 ++++----- lib/internal/webstreams/writablestream.js | 22 ++++++++-------- test/parallel/test-runner-run-watch.mjs | 6 ++--- .../test-runner-watch-mode-complex.mjs | 6 ++--- test/parallel/test-runner-watch-mode.mjs | 6 ++--- typings/primordials.d.ts | 1 + 20 files changed, 81 insertions(+), 88 deletions(-) diff --git a/lib/child_process.js b/lib/child_process.js index 4e15255a01007b..d99cdf4ec33051 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -37,6 +37,7 @@ const { ObjectAssign, ObjectDefineProperty, ObjectPrototypeHasOwnProperty, + PromiseWithResolvers, RegExpPrototypeExec, SafeSet, StringPrototypeIncludes, @@ -47,7 +48,6 @@ const { const { convertToValidSignal, - createDeferredPromise, getSystemErrorName, kEmptyObject, promisify, @@ -237,7 +237,7 @@ function exec(command, options, callback) { const customPromiseExecFunction = (orig) => { return (...args) => { - const { promise, resolve, reject } = createDeferredPromise(); + const { promise, resolve, reject } = PromiseWithResolvers(); promise.child = orig(...args, (err, stdout, stderr) => { if (err !== null) { diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index 2fba1e3fdbe81b..3631764afb07de 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -8,6 +8,7 @@ const { ObjectDefineProperties, ObjectDefineProperty, PromiseResolve, + PromiseWithResolvers, SafeFinalizationRegistry, SafeSet, Symbol, @@ -26,7 +27,6 @@ const { kWeakHandler, } = require('internal/event_target'); const { - createDeferredPromise, customInspectSymbol, kEmptyObject, kEnumerableProperty, @@ -449,7 +449,7 @@ async function aborted(signal, resource) { validateObject(resource, 'resource', kValidateObjectAllowObjects); if (signal.aborted) return PromiseResolve(); - const abortPromise = createDeferredPromise(); + const abortPromise = PromiseWithResolvers(); const opts = { __proto__: null, [kWeakHandler]: resource, once: true, [kResistStopPropagation]: true }; signal.addEventListener('abort', abortPromise.resolve, opts); return abortPromise.promise; diff --git a/lib/internal/blob.js b/lib/internal/blob.js index 4ff2b0e1e7051b..750c0eb7c3b90e 100644 --- a/lib/internal/blob.js +++ b/lib/internal/blob.js @@ -9,6 +9,7 @@ const { ObjectSetPrototypeOf, PromisePrototypeThen, PromiseReject, + PromiseWithResolvers, RegExpPrototypeExec, RegExpPrototypeSymbolReplace, StringPrototypeSplit, @@ -47,7 +48,6 @@ const { } = require('internal/util/types'); const { - createDeferredPromise, customInspectSymbol: kInspect, kEmptyObject, kEnumerableProperty, @@ -273,7 +273,7 @@ class Blob { if (!isBlob(this)) return PromiseReject(new ERR_INVALID_THIS('Blob')); - const { promise, resolve, reject } = createDeferredPromise(); + const { promise, resolve, reject } = PromiseWithResolvers(); const reader = this[kHandle].getReader(); const buffers = []; const readNext = () => { @@ -340,7 +340,7 @@ class Blob { this.pendingPulls = []; }, pull(c) { - const { promise, resolve, reject } = createDeferredPromise(); + const { promise, resolve, reject } = PromiseWithResolvers(); this.pendingPulls.push({ resolve, reject }); const readNext = () => { reader.pull((status, buffer) => { diff --git a/lib/internal/fs/watchers.js b/lib/internal/fs/watchers.js index f5ecc15159f457..efa8dd85c64bfb 100644 --- a/lib/internal/fs/watchers.js +++ b/lib/internal/fs/watchers.js @@ -4,6 +4,7 @@ const { FunctionPrototypeCall, ObjectDefineProperty, ObjectSetPrototypeOf, + PromiseWithResolvers, Symbol, } = primordials; @@ -15,7 +16,6 @@ const { }, } = require('internal/errors'); const { - createDeferredPromise, kEmptyObject, } = require('internal/util'); @@ -325,7 +325,7 @@ async function* watch(filename, options = kEmptyObject) { throw new AbortError(undefined, { cause: signal?.reason }); const handle = new FSEvent(); - let { promise, resolve, reject } = createDeferredPromise(); + let { promise, resolve, reject } = PromiseWithResolvers(); const oncancel = () => { handle.close(); reject(new AbortError(undefined, { cause: signal?.reason })); @@ -368,7 +368,7 @@ async function* watch(filename, options = kEmptyObject) { while (!signal?.aborted) { yield await promise; - ({ promise, resolve, reject } = createDeferredPromise()); + ({ promise, resolve, reject } = PromiseWithResolvers()); } throw new AbortError(undefined, { cause: signal?.reason }); } finally { diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 6a31f17fb863cb..d611dc1337f134 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -744,5 +744,18 @@ primordials.SafeStringPrototypeSearch = (str, regexp) => { return match ? match.index : -1; }; +// TODO(@anonrig): Remove this when V8 removes the flag to disable Promise.withResolvers() +// eslint-disable-next-line node-core/prefer-primordials +primordials.PromiseWithResolvers = Promise.withResolvers || (() => { + let resolve; + let reject; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + + return { promise, resolve, reject }; +}); + ObjectSetPrototypeOf(primordials, null); ObjectFreeze(primordials); diff --git a/lib/internal/streams/duplexify.js b/lib/internal/streams/duplexify.js index 4498f0f3905be2..3e026352f20432 100644 --- a/lib/internal/streams/duplexify.js +++ b/lib/internal/streams/duplexify.js @@ -2,6 +2,7 @@ const { FunctionPrototypeCall, + PromiseWithResolvers, } = primordials; const { @@ -27,7 +28,6 @@ const { destroyer } = require('internal/streams/destroy'); const Duplex = require('internal/streams/duplex'); const Readable = require('internal/streams/readable'); const Writable = require('internal/streams/writable'); -const { createDeferredPromise } = require('internal/util'); const from = require('internal/streams/from'); const { @@ -209,7 +209,7 @@ module.exports = function duplexify(body, name) { }; function fromAsyncGen(fn) { - let { promise, resolve } = createDeferredPromise(); + let { promise, resolve } = PromiseWithResolvers(); const ac = new AbortController(); const signal = ac.signal; const value = fn(async function*() { @@ -221,7 +221,7 @@ function fromAsyncGen(fn) { if (done) return; if (signal.aborted) throw new AbortError(undefined, { cause: signal.reason }); - ({ promise, resolve } = createDeferredPromise()); + ({ promise, resolve } = PromiseWithResolvers()); yield chunk; } }(), { signal }); diff --git a/lib/internal/test_runner/harness.js b/lib/internal/test_runner/harness.js index 1bc6cddabd41a0..15589786835e68 100644 --- a/lib/internal/test_runner/harness.js +++ b/lib/internal/test_runner/harness.js @@ -4,6 +4,7 @@ const { ArrayPrototypePush, FunctionPrototypeBind, PromiseResolve, + PromiseWithResolvers, SafeMap, SafePromiseAllReturnVoid, } = primordials; @@ -26,7 +27,6 @@ const { shouldColorizeTestFiles, } = require('internal/test_runner/utils'); const { queueMicrotask } = require('internal/process/task_queues'); -const { createDeferredPromise } = require('internal/util'); const { bigint: hrtime } = process.hrtime; const resolvedPromise = PromiseResolve(); const testResources = new SafeMap(); @@ -35,7 +35,7 @@ let globalRoot; testResources.set(reporterScope.asyncId(), reporterScope); function createTestTree(rootTestOptions, globalOptions) { - const buildPhaseDeferred = createDeferredPromise(); + const buildPhaseDeferred = PromiseWithResolvers(); const harness = { __proto__: null, buildPromise: buildPhaseDeferred.promise, diff --git a/lib/internal/test_runner/runner.js b/lib/internal/test_runner/runner.js index 9590ef8dcf75bf..532a3b602d9057 100644 --- a/lib/internal/test_runner/runner.js +++ b/lib/internal/test_runner/runner.js @@ -17,6 +17,7 @@ const { ObjectAssign, PromisePrototypeThen, PromiseResolve, + PromiseWithResolvers, SafeMap, SafePromiseAll, SafePromiseAllReturnVoid, @@ -59,7 +60,6 @@ const { getInspectPort, isUsingInspector, isInspectorMessage } = require('intern const { isRegExp } = require('internal/util/types'); const { pathToFileURL } = require('internal/url'); const { - createDeferredPromise, getCWDURL, kEmptyObject, } = require('internal/util'); @@ -688,7 +688,7 @@ function run(options = kEmptyObject) { }; } else { runFiles = async () => { - const { promise, resolve: finishBootstrap } = createDeferredPromise(); + const { promise, resolve: finishBootstrap } = PromiseWithResolvers(); await root.runInAsyncScope(async () => { const parentURL = getCWDURL().href; diff --git a/lib/internal/test_runner/test.js b/lib/internal/test_runner/test.js index 09387f89c36c34..e44543adcf4af5 100644 --- a/lib/internal/test_runner/test.js +++ b/lib/internal/test_runner/test.js @@ -16,6 +16,7 @@ const { ObjectSeal, PromisePrototypeThen, PromiseResolve, + PromiseWithResolvers, ReflectApply, RegExpPrototypeExec, SafeMap, @@ -49,7 +50,6 @@ const { isTestFailureError, } = require('internal/test_runner/utils'); const { - createDeferredPromise, kEmptyObject, once: runOnce, } = require('internal/util'); @@ -133,7 +133,7 @@ function lazyAssertObject(harness) { } function stopTest(timeout, signal) { - const deferred = createDeferredPromise(); + const deferred = PromiseWithResolvers(); const abortListener = addAbortListener(signal, deferred.resolve); let timer; let disposeFunction; @@ -775,7 +775,7 @@ class Test extends AsyncResource { // pending for later execution. this.reporter.enqueue(this.nesting, this.loc, this.name); if (this.root.harness.buildPromise || !this.parent.hasConcurrency()) { - const deferred = createDeferredPromise(); + const deferred = PromiseWithResolvers(); deferred.test = this; this.parent.addPendingSubtest(deferred); diff --git a/lib/internal/test_runner/utils.js b/lib/internal/test_runner/utils.js index 0d6fab11d10d82..3aeb5e37f481b7 100644 --- a/lib/internal/test_runner/utils.js +++ b/lib/internal/test_runner/utils.js @@ -12,6 +12,7 @@ const { NumberParseInt, NumberPrototypeToFixed, ObjectGetOwnPropertyDescriptor, + PromiseWithResolvers, RegExp, RegExpPrototypeExec, SafeMap, @@ -27,7 +28,6 @@ const { AsyncResource } = require('async_hooks'); const { relative } = require('path'); const { createWriteStream } = require('fs'); const { pathToFileURL } = require('internal/url'); -const { createDeferredPromise } = require('internal/util'); const { getOptionValue } = require('internal/options'); const { green, yellow, red, white, shouldColorize } = require('internal/util/colors'); @@ -57,7 +57,7 @@ const kDefaultPattern = `**/{${ArrayPrototypeJoin(kPatterns, ',')}}.?(c|m)js`; function createDeferredCallback() { let calledCount = 0; - const { promise, resolve, reject } = createDeferredPromise(); + const { promise, resolve, reject } = PromiseWithResolvers(); const cb = (err) => { calledCount++; diff --git a/lib/internal/util.js b/lib/internal/util.js index f9de6cd77c71f4..db5a2731f2d566 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -559,17 +559,6 @@ function sleep(msec) { _sleep(msec); } -function createDeferredPromise() { - let resolve; - let reject; - const promise = new Promise((res, rej) => { - resolve = res; - reject = rej; - }); - - return { promise, resolve, reject }; -} - // https://heycam.github.io/webidl/#define-the-operations function defineOperation(target, name, method) { ObjectDefineProperty(target, name, { @@ -896,7 +885,6 @@ module.exports = { cachedResult, convertToValidSignal, createClassWrapper, - createDeferredPromise, decorateErrorStack, defineOperation, defineLazyProperties, diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 307c3f537ec63c..b17efccc838c53 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -7,6 +7,7 @@ const { ObjectEntries, PromisePrototypeThen, PromiseResolve, + PromiseWithResolvers, SafePromiseAll, SafePromisePrototypeFinally, SafeSet, @@ -64,7 +65,6 @@ const { } = require('internal/errors'); const { - createDeferredPromise, kEmptyObject, normalizeEncoding, } = require('internal/util'); @@ -213,7 +213,7 @@ function newWritableStreamFromStreamWritable(streamWritable) { async write(chunk) { if (streamWritable.writableNeedDrain || !streamWritable.write(chunk)) { - backpressurePromise = createDeferredPromise(); + backpressurePromise = PromiseWithResolvers(); return SafePromisePrototypeFinally( backpressurePromise.promise, () => { backpressurePromise = undefined; @@ -227,7 +227,7 @@ function newWritableStreamFromStreamWritable(streamWritable) { close() { if (closed === undefined && !isWritableEnded(streamWritable)) { - closed = createDeferredPromise(); + closed = PromiseWithResolvers(); streamWritable.end(); return closed.promise; } @@ -900,7 +900,7 @@ function newWritableStreamFromStreamBase(streamBase, strategy) { } function doWrite(chunk, controller) { - const promise = createDeferredPromise(); + const promise = PromiseWithResolvers(); let ret; let req; try { @@ -933,7 +933,7 @@ function newWritableStreamFromStreamBase(streamBase, strategy) { }, close() { - const promise = createDeferredPromise(); + const promise = PromiseWithResolvers(); const req = new ShutdownWrap(); req.oncomplete = () => promise.resolve(); const err = streamBase.shutdown(req); @@ -1000,7 +1000,7 @@ function newReadableStreamFromStreamBase(streamBase, strategy, options = kEmptyO }, cancel() { - const promise = createDeferredPromise(); + const promise = PromiseWithResolvers(); try { ondone(); } catch (error) { diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index f37f5174918c6d..79c1df29d70a5a 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -17,6 +17,7 @@ const { PromisePrototypeThen, PromiseReject, PromiseResolve, + PromiseWithResolvers, SafePromiseAll, Symbol, SymbolAsyncIterator, @@ -48,7 +49,6 @@ const { } = require('internal/util/types'); const { - createDeferredPromise, customInspectSymbol: kInspect, isArrayBufferDetached, kEmptyObject, @@ -254,7 +254,7 @@ class ReadableStream { validateObject(strategy, 'strategy', kValidateObjectAllowObjectsAndNull); this[kState] = createReadableStreamState(); - this[kIsClosedPromise] = createDeferredPromise(); + this[kIsClosedPromise] = PromiseWithResolvers(); this[kControllerErrorFunction] = () => {}; // The spec requires handling of the strategy first @@ -488,7 +488,7 @@ class ReadableStream { new ERR_INVALID_STATE.TypeError( 'The reader is not bound to a ReadableStream')); } - const promise = createDeferredPromise(); + const promise = PromiseWithResolvers(); // eslint-disable-next-line no-use-before-define readableStreamDefaultReaderRead(reader, new ReadableStreamAsyncIteratorReadRequest(reader, state, promise)); @@ -644,7 +644,7 @@ function InternalTransferredReadableStream() { this[kType] = 'ReadableStream'; this[kState] = createReadableStreamState(); - this[kIsClosedPromise] = createDeferredPromise(); + this[kIsClosedPromise] = PromiseWithResolvers(); } ObjectSetPrototypeOf(InternalTransferredReadableStream.prototype, ReadableStream.prototype); @@ -787,7 +787,7 @@ class ReadableStreamAsyncIteratorReadRequest { class DefaultReadRequest { constructor() { - this[kState] = createDeferredPromise(); + this[kState] = PromiseWithResolvers(); } [kChunk](value) { @@ -807,7 +807,7 @@ class DefaultReadRequest { class ReadIntoRequest { constructor() { - this[kState] = createDeferredPromise(); + this[kState] = PromiseWithResolvers(); } [kChunk](value) { @@ -1230,7 +1230,7 @@ function InternalReadableStream(start, pull, cancel, highWaterMark, size) { markTransferMode(this, false, true); this[kType] = 'ReadableStream'; this[kState] = createReadableStreamState(); - this[kIsClosedPromise] = createDeferredPromise(); + this[kIsClosedPromise] = PromiseWithResolvers(); const controller = new ReadableStreamDefaultController(kSkipThrow); setupReadableStreamDefaultController( this, @@ -1257,7 +1257,7 @@ function InternalReadableByteStream(start, pull, cancel) { markTransferMode(this, false, true); this[kType] = 'ReadableStream'; this[kState] = createReadableStreamState(); - this[kIsClosedPromise] = createDeferredPromise(); + this[kIsClosedPromise] = PromiseWithResolvers(); const controller = new ReadableByteStreamController(kSkipThrow); setupReadableByteStreamController( this, @@ -1391,7 +1391,7 @@ function readableStreamPipeTo( } } - const promise = createDeferredPromise(); + const promise = PromiseWithResolvers(); const state = { currentWrite: PromiseResolve(), @@ -1508,7 +1508,7 @@ function readableStreamPipeTo( await writer[kState].ready.promise; - const promise = createDeferredPromise(); + const promise = PromiseWithResolvers(); // eslint-disable-next-line no-use-before-define readableStreamDefaultReaderRead(reader, new PipeToReadableStreamReadRequest(writer, state, promise)); @@ -1612,7 +1612,7 @@ function readableStreamDefaultTee(stream, cloneForBranch2) { let reason2; let branch1; let branch2; - const cancelPromise = createDeferredPromise(); + const cancelPromise = PromiseWithResolvers(); async function pullAlgorithm() { if (reading) return; @@ -1711,7 +1711,7 @@ function readableByteStreamTee(stream) { let reason2; let branch1; let branch2; - const cancelDeferred = createDeferredPromise(); + const cancelDeferred = PromiseWithResolvers(); function forwardReaderError(thisReader) { PromisePrototypeThen( @@ -2158,7 +2158,7 @@ function readableStreamReaderGenericInitialize(reader, stream) { stream[kState].reader = reader; switch (stream[kState].state) { case 'readable': - reader[kState].close = createDeferredPromise(); + reader[kState].close = PromiseWithResolvers(); break; case 'closed': reader[kState].close = { diff --git a/lib/internal/webstreams/transfer.js b/lib/internal/webstreams/transfer.js index 9835e6ab272c98..9ce3f249ffd2bd 100644 --- a/lib/internal/webstreams/transfer.js +++ b/lib/internal/webstreams/transfer.js @@ -3,6 +3,7 @@ const { ObjectDefineProperties, PromiseResolve, + PromiseWithResolvers, ReflectConstruct, } = primordials; @@ -28,10 +29,6 @@ const { writableStreamDefaultControllerErrorIfNeeded, } = require('internal/webstreams/writablestream'); -const { - createDeferredPromise, -} = require('internal/util'); - const assert = require('internal/assert'); const { @@ -181,7 +178,7 @@ class CrossRealmTransformWritableSink { this[kState] = { port, controller: undefined, - backpressurePromise: createDeferredPromise(), + backpressurePromise: PromiseWithResolvers(), unref, }; @@ -239,7 +236,7 @@ class CrossRealmTransformWritableSink { }; } await this[kState].backpressurePromise.promise; - this[kState].backpressurePromise = createDeferredPromise(); + this[kState].backpressurePromise = PromiseWithResolvers(); try { this[kState].port.postMessage({ type: 'chunk', value: chunk }); } catch (error) { diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js index 68b1e7aa83d9fc..1decc70e75f236 100644 --- a/lib/internal/webstreams/transformstream.js +++ b/lib/internal/webstreams/transformstream.js @@ -5,6 +5,7 @@ const { ObjectDefineProperties, ObjectSetPrototypeOf, PromisePrototypeThen, + PromiseWithResolvers, Symbol, SymbolToStringTag, } = primordials; @@ -23,7 +24,6 @@ const { } = internalBinding('messaging'); const { - createDeferredPromise, customInspectSymbol: kInspect, kEmptyObject, kEnumerableProperty, @@ -161,7 +161,7 @@ class TransformStream { extractHighWaterMark(writableHighWaterMark, 1); const actualWritableSize = extractSizeAlgorithm(writableSize); - const startPromise = createDeferredPromise(); + const startPromise = PromiseWithResolvers(); initializeTransformStream( this, @@ -438,7 +438,7 @@ function transformStreamSetBackpressure(stream, backpressure) { assert(stream[kState].backpressure !== backpressure); if (stream[kState].backpressureChange.promise !== undefined) stream[kState].backpressureChange.resolve?.(); - stream[kState].backpressureChange = createDeferredPromise(); + stream[kState].backpressureChange = PromiseWithResolvers(); stream[kState].backpressure = backpressure; } @@ -582,7 +582,7 @@ async function transformStreamDefaultSinkAbortAlgorithm(stream, reason) { return controller[kState].finishPromise; } - const { promise, resolve, reject } = createDeferredPromise(); + const { promise, resolve, reject } = PromiseWithResolvers(); controller[kState].finishPromise = promise; const cancelPromise = controller[kState].cancelAlgorithm(reason); transformStreamDefaultControllerClearAlgorithms(controller); @@ -615,7 +615,7 @@ function transformStreamDefaultSinkCloseAlgorithm(stream) { if (controller[kState].finishPromise !== undefined) { return controller[kState].finishPromise; } - const { promise, resolve, reject } = createDeferredPromise(); + const { promise, resolve, reject } = PromiseWithResolvers(); controller[kState].finishPromise = promise; const flushPromise = controller[kState].flushAlgorithm(controller); transformStreamDefaultControllerClearAlgorithms(controller); @@ -653,7 +653,7 @@ function transformStreamDefaultSourceCancelAlgorithm(stream, reason) { return controller[kState].finishPromise; } - const { promise, resolve, reject } = createDeferredPromise(); + const { promise, resolve, reject } = PromiseWithResolvers(); controller[kState].finishPromise = promise; const cancelPromise = controller[kState].cancelAlgorithm(reason); transformStreamDefaultControllerClearAlgorithms(controller); diff --git a/lib/internal/webstreams/writablestream.js b/lib/internal/webstreams/writablestream.js index 43f7a5121b1e97..2d30190e346f13 100644 --- a/lib/internal/webstreams/writablestream.js +++ b/lib/internal/webstreams/writablestream.js @@ -11,6 +11,7 @@ const { PromisePrototypeThen, PromiseReject, PromiseResolve, + PromiseWithResolvers, Symbol, SymbolToStringTag, } = primordials; @@ -30,7 +31,6 @@ const { } = internalBinding('messaging'); const { - createDeferredPromise, customInspectSymbol: kInspect, kEmptyObject, kEnumerableProperty, @@ -170,7 +170,7 @@ class WritableStream { this[kState] = createWritableStreamState(); - this[kIsClosedPromise] = createDeferredPromise(); + this[kIsClosedPromise] = PromiseWithResolvers(); this[kControllerErrorFunction] = () => {}; const size = extractSizeAlgorithm(strategy?.size); @@ -304,7 +304,7 @@ function InternalTransferredWritableStream() { this[kType] = 'WritableStream'; this[kState] = createWritableStreamState(); - this[kIsClosedPromise] = createDeferredPromise(); + this[kIsClosedPromise] = PromiseWithResolvers(); } ObjectSetPrototypeOf(InternalTransferredWritableStream.prototype, WritableStream.prototype); @@ -519,7 +519,7 @@ function InternalWritableStream(start, write, close, abort, highWaterMark, size) markTransferMode(this, false, true); this[kType] = 'WritableStream'; this[kState] = createWritableStreamState(); - this[kIsClosedPromise] = createDeferredPromise(); + this[kIsClosedPromise] = PromiseWithResolvers(); const controller = new WritableStreamDefaultController(kSkipThrow); setupWritableStreamDefaultController( @@ -555,7 +555,7 @@ const isWritableStreamDefaultController = function createWritableStreamState() { return { __proto__: null, - close: createDeferredPromise(), + close: PromiseWithResolvers(), closeRequest: { __proto__: null, promise: undefined, @@ -614,7 +614,7 @@ function setupWritableStreamDefaultWriter(writer, stream) { case 'writable': if (!writableStreamCloseQueuedOrInFlight(stream) && stream[kState].backpressure) { - writer[kState].ready = createDeferredPromise(); + writer[kState].ready = PromiseWithResolvers(); } else { writer[kState].ready = { promise: PromiseResolve(), @@ -661,7 +661,7 @@ function setupWritableStreamDefaultWriter(writer, stream) { } function setClosedPromiseToNewPromise() { - writer[kState].close = createDeferredPromise(); + writer[kState].close = PromiseWithResolvers(); } } @@ -686,7 +686,7 @@ function writableStreamAbort(stream, reason) { reason = undefined; } - const abort = createDeferredPromise(); + const abort = PromiseWithResolvers(); stream[kState].pendingAbortRequest = { abort, @@ -713,7 +713,7 @@ function writableStreamClose(stream) { } assert(state === 'writable' || state === 'erroring'); assert(!writableStreamCloseQueuedOrInFlight(stream)); - stream[kState].closeRequest = createDeferredPromise(); + stream[kState].closeRequest = PromiseWithResolvers(); const { promise } = stream[kState].closeRequest; if (writer !== undefined && backpressure && state === 'writable') writer[kState].ready.resolve?.(); @@ -729,7 +729,7 @@ function writableStreamUpdateBackpressure(stream, backpressure) { } = stream[kState]; if (writer !== undefined && stream[kState].backpressure !== backpressure) { if (backpressure) { - writer[kState].ready = createDeferredPromise(); + writer[kState].ready = PromiseWithResolvers(); } else { writer[kState].ready.resolve?.(); } @@ -959,7 +959,7 @@ function writableStreamAddWriteRequest(stream) { promise, resolve, reject, - } = createDeferredPromise(); + } = PromiseWithResolvers(); ArrayPrototypePush( stream[kState].writeRequests, { diff --git a/test/parallel/test-runner-run-watch.mjs b/test/parallel/test-runner-run-watch.mjs index 09ca389f60c4db..55d2df5f0b5f93 100644 --- a/test/parallel/test-runner-run-watch.mjs +++ b/test/parallel/test-runner-run-watch.mjs @@ -1,11 +1,9 @@ -// Flags: --expose-internals import * as common from '../common/index.mjs'; import { describe, it, beforeEach } from 'node:test'; import assert from 'node:assert'; import { spawn } from 'node:child_process'; import { once } from 'node:events'; import { writeFileSync, renameSync, unlinkSync, existsSync } from 'node:fs'; -import util from 'internal/util'; import tmpdir from '../common/tmpdir.js'; import { join } from 'node:path'; @@ -42,8 +40,8 @@ function refresh() { const runner = join(import.meta.dirname, '..', 'fixtures', 'test-runner-watch.mjs'); async function testWatch({ fileToUpdate, file, action = 'update', cwd = tmpdir.path, fileToCreate }) { - const ran1 = util.createDeferredPromise(); - const ran2 = util.createDeferredPromise(); + const ran1 = Promise.withResolvers(); + const ran2 = Promise.withResolvers(); const args = [runner]; if (file) args.push('--file', file); const child = spawn(process.execPath, diff --git a/test/parallel/test-runner-watch-mode-complex.mjs b/test/parallel/test-runner-watch-mode-complex.mjs index 0ec9a225c3501c..f17fccbc3fc8c1 100644 --- a/test/parallel/test-runner-watch-mode-complex.mjs +++ b/test/parallel/test-runner-watch-mode-complex.mjs @@ -1,10 +1,8 @@ -// Flags: --expose-internals import * as common from '../common/index.mjs'; import { describe, it } from 'node:test'; import assert from 'node:assert'; import { spawn } from 'node:child_process'; import { writeFileSync, unlinkSync } from 'node:fs'; -import util from 'internal/util'; import tmpdir from '../common/tmpdir.js'; if (common.isIBMi) @@ -51,8 +49,8 @@ Object.entries(fixtureContent) describe('test runner watch mode with more complex setup', () => { it('should run tests when a dependency changed after a watched test file being deleted', async () => { - const ran1 = util.createDeferredPromise(); - const ran2 = util.createDeferredPromise(); + const ran1 = Promise.withResolvers(); + const ran2 = Promise.withResolvers(); const child = spawn(process.execPath, ['--watch', '--test'], { encoding: 'utf8', stdio: 'pipe', cwd: tmpdir.path }); diff --git a/test/parallel/test-runner-watch-mode.mjs b/test/parallel/test-runner-watch-mode.mjs index d48230ea6c4e98..61c054b9c30ac9 100644 --- a/test/parallel/test-runner-watch-mode.mjs +++ b/test/parallel/test-runner-watch-mode.mjs @@ -1,11 +1,9 @@ -// Flags: --expose-internals import * as common from '../common/index.mjs'; import { describe, it, beforeEach } from 'node:test'; import { once } from 'node:events'; import assert from 'node:assert'; import { spawn } from 'node:child_process'; import { writeFileSync, renameSync, unlinkSync, existsSync } from 'node:fs'; -import util from 'internal/util'; import tmpdir from '../common/tmpdir.js'; if (common.isIBMi) @@ -43,8 +41,8 @@ async function testWatch({ action = 'update', fileToCreate, }) { - const ran1 = util.createDeferredPromise(); - const ran2 = util.createDeferredPromise(); + const ran1 = Promise.withResolvers(); + const ran2 = Promise.withResolvers(); const child = spawn(process.execPath, ['--watch', '--test', '--test-reporter=spec', file ? fixturePaths[file] : undefined].filter(Boolean), diff --git a/typings/primordials.d.ts b/typings/primordials.d.ts index e5426b85b5c29b..883b2509e20964 100644 --- a/typings/primordials.d.ts +++ b/typings/primordials.d.ts @@ -528,6 +528,7 @@ declare namespace primordials { export const PromisePrototypeThen: UncurryThis export const PromisePrototypeCatch: UncurryThis export const PromisePrototypeFinally: UncurryThis + export const PromiseWithResolvers: typeof Promise.withResolvers export import Proxy = globalThis.Proxy import _globalThis = globalThis export { _globalThis as globalThis }