From ada42544737dd00542928b770ff54cb13a808b25 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 17 Aug 2022 23:57:09 +0200 Subject: [PATCH] lib: use safe `Promise` alternatives when available PR-URL: https://github.com/nodejs/node/pull/43476 Reviewed-By: Moshe Atlow --- lib/internal/debugger/inspect_repl.js | 5 ++--- lib/internal/fs/cp/cp.js | 10 ++++------ lib/internal/modules/esm/loader.js | 5 ++--- lib/internal/webstreams/adapters.js | 23 ++++++++++------------ lib/internal/webstreams/readablestream.js | 4 ++-- lib/internal/webstreams/transformstream.js | 17 +++++++--------- 6 files changed, 27 insertions(+), 37 deletions(-) diff --git a/lib/internal/debugger/inspect_repl.js b/lib/internal/debugger/inspect_repl.js index bfc11990e0ddf0..c2b741e0d5d8d6 100644 --- a/lib/internal/debugger/inspect_repl.js +++ b/lib/internal/debugger/inspect_repl.js @@ -23,7 +23,6 @@ const { ObjectKeys, ObjectValues, Promise, - PromisePrototypeCatch, PromisePrototypeThen, PromiseResolve, ReflectGetOwnPropertyDescriptor, @@ -653,8 +652,8 @@ function createRepl(inspector) { } const inspectValue = (expr) => - PromisePrototypeCatch(evalInCurrentContext(expr), - (error) => `<${error.message}>`); + PromisePrototypeThen(evalInCurrentContext(expr), undefined, + (error) => `<${error.message}>`); const lastIndex = watchedExpressions.length - 1; const values = await SafePromiseAll(watchedExpressions, inspectValue); diff --git a/lib/internal/fs/cp/cp.js b/lib/internal/fs/cp/cp.js index bcbc8aa3279a50..e0b7a74b1c799a 100644 --- a/lib/internal/fs/cp/cp.js +++ b/lib/internal/fs/cp/cp.js @@ -6,11 +6,9 @@ const { ArrayPrototypeEvery, ArrayPrototypeFilter, Boolean, - PromiseAll, - PromisePrototypeCatch, PromisePrototypeThen, PromiseReject, - SafeArrayIterator, + SafePromiseAll, StringPrototypeSplit, } = primordials; const { @@ -128,13 +126,13 @@ function getStats(src, dest, opts) { const statFunc = opts.dereference ? (file) => stat(file, { bigint: true }) : (file) => lstat(file, { bigint: true }); - return PromiseAll(new SafeArrayIterator([ + return SafePromiseAll([ statFunc(src), - PromisePrototypeCatch(statFunc(dest), (err) => { + PromisePrototypeThen(statFunc(dest), undefined, (err) => { if (err.code === 'ENOENT') return null; throw err; }), - ])); + ]); } async function checkParentDir(destStat, src, dest, opts) { diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 71c9100a7e7ed1..a63bb9247efcab 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -13,9 +13,8 @@ const { ObjectCreate, ObjectDefineProperty, ObjectSetPrototypeOf, - PromiseAll, RegExpPrototypeExec, - SafeArrayIterator, + SafePromiseAll, SafeWeakMap, StringPrototypeSlice, StringPrototypeToUpperCase, @@ -525,7 +524,7 @@ class ESMLoader { .then(({ module }) => module.getNamespace()); } - const namespaces = await PromiseAll(new SafeArrayIterator(jobs)); + const namespaces = await SafePromiseAll(jobs); if (!wasArr) { return namespaces[0]; } // We can skip the pairing below diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js index 0fd571bfe11d19..a8b13b39aac830 100644 --- a/lib/internal/webstreams/adapters.js +++ b/lib/internal/webstreams/adapters.js @@ -1,11 +1,10 @@ 'use strict'; const { - ArrayPrototypeMap, - PromiseAll, PromisePrototypeThen, - PromisePrototypeFinally, PromiseResolve, + SafePromiseAll, + SafePromisePrototypeFinally, Uint8Array, } = primordials; @@ -165,7 +164,7 @@ function newWritableStreamFromStreamWritable(streamWritable) { async write(chunk) { if (streamWritable.writableNeedDrain || !streamWritable.write(chunk)) { backpressurePromise = createDeferredPromise(); - return PromisePrototypeFinally( + return SafePromisePrototypeFinally( backpressurePromise.promise, () => { backpressurePromise = undefined; }); @@ -246,10 +245,9 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj writer.ready, () => { return PromisePrototypeThen( - PromiseAll( - ArrayPrototypeMap( - chunks, - (data) => writer.write(data.chunk))), + SafePromiseAll( + chunks, + (data) => writer.write(data.chunk)), done, done); }, @@ -652,10 +650,9 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options = writer.ready, () => { return PromisePrototypeThen( - PromiseAll( - ArrayPrototypeMap( - chunks, - (data) => writer.write(data.chunk))), + SafePromiseAll( + chunks, + (data) => writer.write(data.chunk)), done, done); }, @@ -751,7 +748,7 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options = if (!writableClosed || !readableClosed) { PromisePrototypeThen( - PromiseAll([ + SafePromiseAll([ closeWriter(), closeReader(), ]), diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index e92025cc16160e..26879ad861efe7 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -19,8 +19,8 @@ const { PromisePrototypeThen, PromiseResolve, PromiseReject, - PromiseAll, ReflectConstruct, + SafePromiseAll, Symbol, SymbolAsyncIterator, SymbolToStringTag, @@ -1319,7 +1319,7 @@ function readableStreamPipeTo( } shutdownWithAnAction( - async () => PromiseAll(actions.map((action) => action())), + () => SafePromiseAll(actions, (action) => action()), true, error); } diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js index 457c9eb8fb338a..b7366fb1ba2576 100644 --- a/lib/internal/webstreams/transformstream.js +++ b/lib/internal/webstreams/transformstream.js @@ -4,7 +4,6 @@ const { FunctionPrototypeBind, FunctionPrototypeCall, ObjectDefineProperties, - PromisePrototypeCatch, PromisePrototypeThen, PromiseResolve, ReflectConstruct, @@ -496,19 +495,17 @@ function transformStreamDefaultControllerError(controller, error) { transformStreamError(controller[kState].stream, error); } -function transformStreamDefaultControllerPerformTransform(controller, chunk) { - const transformPromise = - ensureIsPromise( +async function transformStreamDefaultControllerPerformTransform(controller, chunk) { + try { + return await ensureIsPromise( controller[kState].transformAlgorithm, controller, chunk, controller); - return PromisePrototypeCatch( - transformPromise, - (error) => { - transformStreamError(controller[kState].stream, error); - throw error; - }); + } catch (error) { + transformStreamError(controller[kState].stream, error); + throw error; + } } function transformStreamDefaultControllerTerminate(controller) {