Skip to content

Commit

Permalink
feat(captp): handle send rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Aug 24, 2024
1 parent 5a42f5a commit 7201ae4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/captp/NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 6 additions & 4 deletions packages/captp/src/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const reverseSlot = slot => {
* Create a CapTP connection.
*
* @param {string} ourId our name for the current side
* @param {(obj: Record<string, any>) => void} rawSend send a JSONable packet
* @param {((obj: Record<string, any>) => void) | ((obj: Record<string, any>) => PromiseLike<void>)} rawSend send a JSONable packet
* @param {any} bootstrapObj the object to export to the other side
* @param {CapTPOptions} opts options to the connection
*/
Expand Down Expand Up @@ -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.
};

/**
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 7201ae4

Please sign in to comment.