From 17b4e9bc5f33ac24d476e5d7039b079f957b530a Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 8 Feb 2023 14:28:59 +0100 Subject: [PATCH] stream: refactor to use `validateAbortSignal` PR-URL: https://github.com/nodejs/node/pull/46520 Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/internal/abort_controller.js | 1 - lib/internal/webstreams/readablestream.js | 27 +++++++++++------------ 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/internal/abort_controller.js b/lib/internal/abort_controller.js index 3522836d80439d..c66f2a8367f82d 100644 --- a/lib/internal/abort_controller.js +++ b/lib/internal/abort_controller.js @@ -406,7 +406,6 @@ ObjectDefineProperty(AbortController.prototype, SymbolToStringTag, { }); module.exports = { - kAborted, AbortController, AbortSignal, ClonedAbortSignal, diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 709cc95972f9c9..377eb02a348eaf 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -58,14 +58,11 @@ const { } = require('internal/util'); const { + validateAbortSignal, validateBuffer, validateObject, } = require('internal/validators'); -const { - kAborted, -} = require('internal/abort_controller'); - const { MessageChannel, } = require('internal/worker/io'); @@ -381,8 +378,9 @@ class ReadableStream { const preventClose = options?.preventClose; const signal = options?.signal; - if (signal !== undefined && signal?.[kAborted] === undefined) - throw new ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal); + if (signal !== undefined) { + validateAbortSignal(signal, 'options.signal'); + } if (isReadableStreamLocked(this)) throw new ERR_INVALID_STATE.TypeError('The ReadableStream is locked'); @@ -422,8 +420,9 @@ class ReadableStream { const preventClose = options?.preventClose; const signal = options?.signal; - if (signal !== undefined && signal?.[kAborted] === undefined) - throw new ERR_INVALID_ARG_TYPE('options.signal', 'AbortSignal', signal); + if (signal !== undefined) { + validateAbortSignal(signal, 'options.signal'); + } if (isReadableStreamLocked(this)) throw new ERR_INVALID_STATE.TypeError('The ReadableStream is locked'); @@ -1269,12 +1268,12 @@ function readableStreamPipeTo( let shuttingDown = false; - if (signal !== undefined && signal?.[kAborted] === undefined) { - return PromiseReject( - new ERR_INVALID_ARG_TYPE( - 'options.signal', - 'AbortSignal', - signal)); + if (signal !== undefined) { + try { + validateAbortSignal(signal, 'options.signal'); + } catch (error) { + return PromiseReject(error); + } } const promise = createDeferredPromise();