From 7201ae48a67aa0526f433ea6e2609b3cc29115dd Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Fri, 23 Aug 2024 10:47:31 -0600 Subject: [PATCH] feat(captp): handle `send` rejections --- packages/captp/NEWS.md | 4 ++++ packages/captp/src/captp.js | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/captp/NEWS.md b/packages/captp/NEWS.md index 1ca86d26ed..38aa1024dc 100644 --- a/packages/captp/NEWS.md +++ b/packages/captp/NEWS.md @@ -1,5 +1,9 @@ User-visible changes in `@endo/captp`: +# Unreleased + +- Relax typing of `send` to allow `async` functions, and abort the connection if the `send` function returns a rejected promise. + # v3.1.0 (2023-04-14) - Disable GC by default to work around known issues with dropping diff --git a/packages/captp/src/captp.js b/packages/captp/src/captp.js index 12f6863a29..f15989202f 100644 --- a/packages/captp/src/captp.js +++ b/packages/captp/src/captp.js @@ -66,7 +66,7 @@ const reverseSlot = slot => { * Create a CapTP connection. * * @param {string} ourId our name for the current side - * @param {(obj: Record) => void} rawSend send a JSONable packet + * @param {((obj: Record) => void) | ((obj: Record) => PromiseLike)} rawSend send a JSONable packet * @param {any} bootstrapObj the object to export to the other side * @param {CapTPOptions} opts options to the connection */ @@ -196,8 +196,10 @@ export const makeCapTP = ( return; } - // Actually send the message, in the next turn. - rawSend(obj); + // Actually send the message. + Promise.resolve(rawSend(obj)) + // eslint-disable-next-line no-use-before-define + .catch(abort); // Abort if rawSend returned a rejection. }; /** @@ -727,7 +729,7 @@ export const makeCapTP = ( quietReject(obj.reason, false); unplug = reason; // Deliver the object, even though we're unplugged. - rawSend(obj); + Promise.resolve(rawSend(obj)).catch(sink); } // We no longer wish to subscribe to object finalization. slotToImported.clearWithoutFinalizing();