From 6c8fa6e3765a1cdc047fd2d565ab241c05734f98 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 11 Jun 2022 20:30:51 -0500 Subject: [PATCH] lib: refactor transferable AbortSignal The use of makeTransferable on all AbortSignal instances made creating them always slow, causing bottlenecks in stuff like Web Streams. This refactors that so that AbortSignals are not transferable by default, which is actually the correct standard behavior anyway. A new transferableAbortSignal and transferableAbortController utility is provided to enable the transferable use case. Signed-off-by: James M Snell --- doc/api/util.md | 27 +++++++++++++ lib/internal/abort_controller.js | 45 +++++++++++++++++++-- lib/internal/worker/js_transferable.js | 2 + lib/util.js | 13 ++++++ test/parallel/test-abortsignal-cloneable.js | 11 +++-- 5 files changed, 91 insertions(+), 7 deletions(-) diff --git a/doc/api/util.md b/doc/api/util.md index 9d5201adfcc9a7..5683c6fb864ab7 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -1470,6 +1470,33 @@ 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()` + + + +Creates and returns an {AbortController} instance whose {AbortSignal} is marked +as transferable and can be used with `structuredClone()` or `postMessage()`. + +## `util.transferableAbortSignal(signal)` + + + +* `signal` {AbortSignal} +* Returns: {AbortSignal} + +Marks the given {AbortSignal} as transferable so that it can be used with +`structuredClone()` and `postMessage()`. + +```cjs +const signal = transferableAbortSignal(AbortSignal.timeout(100)); +const channel = new MessageChannel(); +channel.port2.postMessage(signal, [signal]); +``` + ## `util.types`