diff --git a/packages/internal/src/node/utils.js b/packages/internal/src/node/utils.js new file mode 100644 index 00000000000..ba4995c3798 --- /dev/null +++ b/packages/internal/src/node/utils.js @@ -0,0 +1,46 @@ +// @ts-check +// @jessie-check + +// These tools seem cross platform, but they rely on AggregateError in the error +// handling path, which is currently not available in xsnap +import 'node:process'; + +/** + * @template T + * @param {readonly (T | PromiseLike)[]} items + * @returns {Promise} + */ +export const PromiseAllOrErrors = async items => { + return Promise.allSettled(items).then(results => { + const errors = /** @type {PromiseRejectedResult[]} */ ( + results.filter(({ status }) => status === 'rejected') + ).map(result => result.reason); + if (!errors.length) { + return /** @type {PromiseFulfilledResult[]} */ (results).map( + result => result.value, + ); + } else if (errors.length === 1) { + throw errors[0]; + } else { + throw AggregateError(errors); + } + }); +}; + +/** + * @type {( + * trier: () => Promise, + * finalizer: (error?: unknown) => Promise, + * ) => Promise} + */ +export const aggregateTryFinally = async (trier, finalizer) => + trier().then( + async result => finalizer().then(() => result), + async tryError => + finalizer(tryError) + .then( + () => tryError, + finalizeError => AggregateError([tryError, finalizeError]), + ) + .then(error => Promise.reject(error)), + ); diff --git a/packages/internal/src/utils.js b/packages/internal/src/utils.js index 478b87ec44c..be3cf6dc8a9 100644 --- a/packages/internal/src/utils.js +++ b/packages/internal/src/utils.js @@ -74,46 +74,6 @@ export const makeMeasureSeconds = currentTimeMillisec => { return measureSeconds; }; -/** - * @template T - * @param {readonly (T | PromiseLike)[]} items - * @returns {Promise} - */ -export const PromiseAllOrErrors = async items => { - return Promise.allSettled(items).then(results => { - const errors = /** @type {PromiseRejectedResult[]} */ ( - results.filter(({ status }) => status === 'rejected') - ).map(result => result.reason); - if (!errors.length) { - return /** @type {PromiseFulfilledResult[]} */ (results).map( - result => result.value, - ); - } else if (errors.length === 1) { - throw errors[0]; - } else { - throw AggregateError(errors); - } - }); -}; - -/** - * @type {( - * trier: () => Promise, - * finalizer: (error?: unknown) => Promise, - * ) => Promise} - */ -export const aggregateTryFinally = async (trier, finalizer) => - trier().then( - async result => finalizer().then(() => result), - async tryError => - finalizer(tryError) - .then( - () => tryError, - finalizeError => AggregateError([tryError, finalizeError]), - ) - .then(error => Promise.reject(error)), - ); - /** * @template {Record} T * @typedef {{ [P in keyof T]: Exclude }} AllDefined diff --git a/packages/swing-store/src/snapStore.js b/packages/swing-store/src/snapStore.js index 3d67ad503b8..60ae903d899 100644 --- a/packages/swing-store/src/snapStore.js +++ b/packages/swing-store/src/snapStore.js @@ -4,7 +4,10 @@ import { finished as finishedCallback, PassThrough, Readable } from 'stream'; import { promisify } from 'util'; import { createGzip, createGunzip } from 'zlib'; import { Fail, q } from '@agoric/assert'; -import { aggregateTryFinally, PromiseAllOrErrors } from '@agoric/internal'; +import { + aggregateTryFinally, + PromiseAllOrErrors, +} from '@agoric/internal/src/node/utils.js'; import { buffer } from './util.js'; /** diff --git a/packages/telemetry/src/make-slog-sender.js b/packages/telemetry/src/make-slog-sender.js index 94693661087..5eb56aa65c5 100644 --- a/packages/telemetry/src/make-slog-sender.js +++ b/packages/telemetry/src/make-slog-sender.js @@ -1,6 +1,6 @@ import path from 'path'; import tmp from 'tmp'; -import { PromiseAllOrErrors } from '@agoric/internal'; +import { PromiseAllOrErrors } from '@agoric/internal/src/node/utils.js'; import { serializeSlogObj } from './serialize-slog-obj.js'; export const DEFAULT_SLOGSENDER_MODULE =