Skip to content

Commit

Permalink
lib: use safe Promise alternatives when available
Browse files Browse the repository at this point in the history
PR-URL: #43476
Reviewed-By: Moshe Atlow <[email protected]>
  • Loading branch information
aduh95 authored and targos committed Sep 16, 2022
1 parent 6d74ea3 commit 112f17b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 37 deletions.
5 changes: 2 additions & 3 deletions lib/internal/debugger/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const {
ObjectKeys,
ObjectValues,
Promise,
PromisePrototypeCatch,
PromisePrototypeThen,
PromiseResolve,
ReflectGetOwnPropertyDescriptor,
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions lib/internal/fs/cp/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ const {
ArrayPrototypeEvery,
ArrayPrototypeFilter,
Boolean,
PromiseAll,
PromisePrototypeCatch,
PromisePrototypeThen,
PromiseReject,
SafeArrayIterator,
SafePromiseAll,
StringPrototypeSplit,
} = primordials;
const {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ const {
ObjectCreate,
ObjectDefineProperty,
ObjectSetPrototypeOf,
PromiseAll,
RegExpPrototypeExec,
SafeArrayIterator,
SafePromiseAll,
SafeWeakMap,
StringPrototypeSlice,
StringPrototypeToUpperCase,
Expand Down Expand Up @@ -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

Expand Down
23 changes: 10 additions & 13 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict';

const {
ArrayPrototypeMap,
PromiseAll,
PromisePrototypeThen,
PromisePrototypeFinally,
PromiseResolve,
SafePromiseAll,
SafePromisePrototypeFinally,
Uint8Array,
} = primordials;

Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -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);
},
Expand Down Expand Up @@ -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);
},
Expand Down Expand Up @@ -751,7 +748,7 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =

if (!writableClosed || !readableClosed) {
PromisePrototypeThen(
PromiseAll([
SafePromiseAll([
closeWriter(),
closeReader(),
]),
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const {
PromisePrototypeThen,
PromiseResolve,
PromiseReject,
PromiseAll,
ReflectConstruct,
SafePromiseAll,
Symbol,
SymbolAsyncIterator,
SymbolToStringTag,
Expand Down Expand Up @@ -1319,7 +1319,7 @@ function readableStreamPipeTo(
}

shutdownWithAnAction(
async () => PromiseAll(actions.map((action) => action())),
() => SafePromiseAll(actions, (action) => action()),
true,
error);
}
Expand Down
17 changes: 7 additions & 10 deletions lib/internal/webstreams/transformstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
FunctionPrototypeBind,
FunctionPrototypeCall,
ObjectDefineProperties,
PromisePrototypeCatch,
PromisePrototypeThen,
PromiseResolve,
ReflectConstruct,
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 112f17b

Please sign in to comment.