From 3f20e5b15cb41abc7e5b909fea9b410d857c88a2 Mon Sep 17 00:00:00 2001 From: flakey5 <73616808+flakey5@users.noreply.github.com> Date: Fri, 29 Jul 2022 11:31:24 -0700 Subject: [PATCH] lib: refactor transferable AbortSignal Co-authored-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/44048 Backport-PR-URL: https://github.com/nodejs/node/pull/44941 Reviewed-By: Matteo Collina Reviewed-By: Stephen Belanger Reviewed-By: Paolo Insogna Reviewed-By: Joyee Cheung Reviewed-By: Antoine du Hamel --- doc/api/util.md | 31 +++++++++++ lib/internal/abort_controller.js | 60 +++++++++++++++++---- lib/internal/worker/js_transferable.js | 2 + lib/util.js | 13 +++++ test/parallel/test-abortsignal-cloneable.js | 11 ++-- 5 files changed, 105 insertions(+), 12 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index b7801929fa5f5e..d0e134f935d2e1 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -1597,6 +1597,37 @@ Returns the `string` after replacing any surrogate code points (or equivalently, any unpaired surrogate code units) with the Unicode "replacement character" U+FFFD. +## `util.transferableAbortController()` + + + +> Stability: 1 - Experimental + +Creates and returns an {AbortController} instance whose {AbortSignal} is marked +as transferable and can be used with `structuredClone()` or `postMessage()`. + +## `util.transferableAbortSignal(signal)` + + + +> Stability: 1 - Experimental + +* `signal` {AbortSignal} +* Returns: {AbortSignal} + +Marks the given {AbortSignal} as transferable so that it can be used with +`structuredClone()` and `postMessage()`. + +```js +const signal = transferableAbortSignal(AbortSignal.timeout(100)); +const channel = new MessageChannel(); +channel.port2.postMessage(signal, [signal]); +``` + ## `util.types`