From f22d69fb509eaf02aec9fdcecd1365edc068453f Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 00:17:56 +0530 Subject: [PATCH 01/24] feat: add abort signal to body.dump() --- lib/api/readable.js | 13 +++++++++++-- test/client-request.js | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 9c184d14e1c..97122c2ac99 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -146,15 +146,24 @@ module.exports = class BodyReadable extends Readable { async dump (opts) { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 + const signal = opts && opts.signal && opts.signal instanceof AbortSignal ? opts.signal : null + if (signal?.aborted) { + throw new RequestAbortedError() + } try { for await (const chunk of this) { + if (signal && signal?.aborted) { + throw new RequestAbortedError() + } limit -= Buffer.byteLength(chunk) if (limit < 0) { return } } - } catch { - // Do nothing... + } catch (err) { + if (err instanceof RequestAbortedError) { + throw err + } } } } diff --git a/test/client-request.js b/test/client-request.js index ff564604d42..758c0b378ab 100644 --- a/test/client-request.js +++ b/test/client-request.js @@ -41,6 +41,33 @@ test('request dump', (t) => { }) }) +test('request dump with abort signal', (t) => { + t.plan(1) + const server = createServer((req, res) => { + res.write('hello') + }) + t.teardown(server.close.bind(server)) + + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`) + t.teardown(client.destroy.bind(client)) + + client.request({ + path: '/', + method: 'GET' + }, (err, { body }) => { + t.error(err) + const ac = new AbortController() + const { signal } = ac + body.dump({ signal }).catch((err) => { + t.type(err, errors.RequestAbortedError) + server.close() + }) + ac.abort() + }) + }) +}) + test('request abort before headers', (t) => { t.plan(6) From 7365982cde591f9b9bbde3ebd618403af38d9b13 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 10:36:04 +0530 Subject: [PATCH 02/24] fixup --- lib/api/readable.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 97122c2ac99..2d700188ab6 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -147,12 +147,12 @@ module.exports = class BodyReadable extends Readable { async dump (opts) { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 const signal = opts && opts.signal && opts.signal instanceof AbortSignal ? opts.signal : null - if (signal?.aborted) { + if (signal && signal.aborted) { throw new RequestAbortedError() } try { for await (const chunk of this) { - if (signal && signal?.aborted) { + if (signal && signal.aborted) { throw new RequestAbortedError() } limit -= Buffer.byteLength(chunk) From 6b3aadc774605908c05f73d4cc3d346afc26cf63 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 13:48:13 +0530 Subject: [PATCH 03/24] fixup --- test/client-request.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/client-request.js b/test/client-request.js index 758c0b378ab..4f0b04fe7df 100644 --- a/test/client-request.js +++ b/test/client-request.js @@ -1,3 +1,5 @@ +/* globals AbortController */ + 'use strict' const { test } = require('tap') @@ -42,7 +44,7 @@ test('request dump', (t) => { }) test('request dump with abort signal', (t) => { - t.plan(1) + t.plan(2) const server = createServer((req, res) => { res.write('hello') }) From d68c87cfe006216e27faa5aea4bd9367121f2818 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 14:40:02 +0530 Subject: [PATCH 04/24] fixup! use throwIfAborted --- lib/api/readable.js | 10 +++++----- test/client-request.js | 5 ++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 2d700188ab6..1ead2c1001d 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -147,13 +147,13 @@ module.exports = class BodyReadable extends Readable { async dump (opts) { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 const signal = opts && opts.signal && opts.signal instanceof AbortSignal ? opts.signal : null - if (signal && signal.aborted) { - throw new RequestAbortedError() + if (signal) { + signal.throwIfAborted() } try { for await (const chunk of this) { - if (signal && signal.aborted) { - throw new RequestAbortedError() + if (signal) { + signal.throwIfAborted() } limit -= Buffer.byteLength(chunk) if (limit < 0) { @@ -161,7 +161,7 @@ module.exports = class BodyReadable extends Readable { } } } catch (err) { - if (err instanceof RequestAbortedError) { + if (err.name === 'AbortError') { throw err } } diff --git a/test/client-request.js b/test/client-request.js index 4f0b04fe7df..db54090325e 100644 --- a/test/client-request.js +++ b/test/client-request.js @@ -60,9 +60,8 @@ test('request dump with abort signal', (t) => { }, (err, { body }) => { t.error(err) const ac = new AbortController() - const { signal } = ac - body.dump({ signal }).catch((err) => { - t.type(err, errors.RequestAbortedError) + body.dump({ signal: ac.signal }).catch((err) => { + t.equal(err.name, 'AbortError') server.close() }) ac.abort() From d9f67a19dfdbdbacd86d8a67ea27e861637a97c6 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 14:54:06 +0530 Subject: [PATCH 05/24] fixup! destroy on abort --- lib/api/readable.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 1ead2c1001d..24fec7b4bdc 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -148,13 +148,12 @@ module.exports = class BodyReadable extends Readable { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 const signal = opts && opts.signal && opts.signal instanceof AbortSignal ? opts.signal : null if (signal) { - signal.throwIfAborted() + signal.addEventListener('abort', () => { + this.destroy() + }) } try { for await (const chunk of this) { - if (signal) { - signal.throwIfAborted() - } limit -= Buffer.byteLength(chunk) if (limit < 0) { return From e6f6ef027824737078b6aba476bc0b78fccbd266 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 15:14:04 +0530 Subject: [PATCH 06/24] Update lib/api/readable.js Co-authored-by: Robert Nagy --- lib/api/readable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 24fec7b4bdc..8b97929a438 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -146,7 +146,7 @@ module.exports = class BodyReadable extends Readable { async dump (opts) { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 - const signal = opts && opts.signal && opts.signal instanceof AbortSignal ? opts.signal : null + const signal = opts && opts.signal if (signal) { signal.addEventListener('abort', () => { this.destroy() From 1e5904edd6c1e695f20d38671d7e22792420a2d2 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 15:20:01 +0530 Subject: [PATCH 07/24] fixup! add validation for abort signal --- lib/api/readable.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 8b97929a438..617a65b9885 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -4,7 +4,7 @@ const assert = require('assert') const { Readable } = require('stream') -const { RequestAbortedError, NotSupportedError } = require('../core/errors') +const { RequestAbortedError, NotSupportedError, InvalidArgumentError } = require('../core/errors') const util = require('../core/util') const { ReadableStreamFrom, toUSVString } = require('../core/util') @@ -146,8 +146,12 @@ module.exports = class BodyReadable extends Readable { async dump (opts) { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 - const signal = opts && opts.signal + const signal = opts && opts.signal if (signal) { + if (typeof signal !== 'object' || !('aborted' in signal)) { + throw new InvalidArgumentError('signal must be an AbortSignal'); + } + signal.throwIfAborted() signal.addEventListener('abort', () => { this.destroy() }) From 9fdda1103029eb3613425c0280dc4cb781041c10 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 15:20:53 +0530 Subject: [PATCH 08/24] Update lib/api/readable.js Co-authored-by: Robert Nagy --- lib/api/readable.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/api/readable.js b/lib/api/readable.js index 617a65b9885..2674ac7c5ea 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -148,6 +148,7 @@ module.exports = class BodyReadable extends Readable { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 const signal = opts && opts.signal if (signal) { + signal.throwIfAborted() if (typeof signal !== 'object' || !('aborted' in signal)) { throw new InvalidArgumentError('signal must be an AbortSignal'); } From 7761f8b8c5b429abefd28127c3795f16d08c84df Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 15:21:00 +0530 Subject: [PATCH 09/24] Update lib/api/readable.js Co-authored-by: Robert Nagy --- lib/api/readable.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/api/readable.js b/lib/api/readable.js index 2674ac7c5ea..5927c89a201 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -165,6 +165,7 @@ module.exports = class BodyReadable extends Readable { } } } catch (err) { + signal.throwIfAborted() if (err.name === 'AbortError') { throw err } From d9157e1f63d694558f1edd5d6c07bd9345110c5f Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 15:21:07 +0530 Subject: [PATCH 10/24] Update lib/api/readable.js Co-authored-by: Robert Nagy --- lib/api/readable.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/api/readable.js b/lib/api/readable.js index 5927c89a201..5bf93e01c49 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -159,6 +159,7 @@ module.exports = class BodyReadable extends Readable { } try { for await (const chunk of this) { + signal.throwIfAborted() limit -= Buffer.byteLength(chunk) if (limit < 0) { return From 7ad89f66a92a33f6808030a776c05ab497ca8051 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 15:22:17 +0530 Subject: [PATCH 11/24] fixup --- lib/api/readable.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 5bf93e01c49..7cc860a7f0d 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -148,7 +148,6 @@ module.exports = class BodyReadable extends Readable { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 const signal = opts && opts.signal if (signal) { - signal.throwIfAborted() if (typeof signal !== 'object' || !('aborted' in signal)) { throw new InvalidArgumentError('signal must be an AbortSignal'); } From 4f43953d44118252d4ef1d7c25a276639d31dd65 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 15:24:08 +0530 Subject: [PATCH 12/24] Update lib/api/readable.js Co-authored-by: Robert Nagy --- lib/api/readable.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 7cc860a7f0d..793af5964d7 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -166,9 +166,7 @@ module.exports = class BodyReadable extends Readable { } } catch (err) { signal.throwIfAborted() - if (err.name === 'AbortError') { throw err - } } } } From f950347e75e79f2258a94a54dd01928e61e3a06e Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 15:25:12 +0530 Subject: [PATCH 13/24] lint --- lib/api/readable.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 793af5964d7..256490f6a7c 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -149,7 +149,7 @@ module.exports = class BodyReadable extends Readable { const signal = opts && opts.signal if (signal) { if (typeof signal !== 'object' || !('aborted' in signal)) { - throw new InvalidArgumentError('signal must be an AbortSignal'); + throw new InvalidArgumentError('signal must be an AbortSignal') } signal.throwIfAborted() signal.addEventListener('abort', () => { @@ -166,7 +166,7 @@ module.exports = class BodyReadable extends Readable { } } catch (err) { signal.throwIfAborted() - throw err + throw err } } } From 9964137e8d45b81422f2b881fb28f6d8b328163f Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 15:36:53 +0530 Subject: [PATCH 14/24] fixup! null check --- lib/api/readable.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 256490f6a7c..01a2bf32769 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -151,6 +151,7 @@ module.exports = class BodyReadable extends Readable { if (typeof signal !== 'object' || !('aborted' in signal)) { throw new InvalidArgumentError('signal must be an AbortSignal') } + console.log(signal.throwIfAborted) signal.throwIfAborted() signal.addEventListener('abort', () => { this.destroy() @@ -158,14 +159,18 @@ module.exports = class BodyReadable extends Readable { } try { for await (const chunk of this) { - signal.throwIfAborted() + if (signal) { + signal.throwIfAborted() + } limit -= Buffer.byteLength(chunk) if (limit < 0) { return } } } catch (err) { - signal.throwIfAborted() + if (signal) { + signal.throwIfAborted() + } throw err } } From 823f6886f7eeb94db432c2cde7bb090db72b098f Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 16:12:10 +0530 Subject: [PATCH 15/24] Update lib/api/readable.js Co-authored-by: Robert Nagy --- lib/api/readable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 01a2bf32769..9c2959ccaad 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -167,7 +167,7 @@ module.exports = class BodyReadable extends Readable { return } } - } catch (err) { + } catch { if (signal) { signal.throwIfAborted() } From 9dd400a4baff1c80af62ff06e6241fc231cd8005 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 16:12:32 +0530 Subject: [PATCH 16/24] Update lib/api/readable.js Co-authored-by: Robert Nagy --- lib/api/readable.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 9c2959ccaad..fd665c5304c 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -171,7 +171,6 @@ module.exports = class BodyReadable extends Readable { if (signal) { signal.throwIfAborted() } - throw err } } } From b0c58cd5f342abca2b5238047933794ccf423873 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Tue, 7 Mar 2023 16:13:05 +0530 Subject: [PATCH 17/24] Update lib/api/readable.js Co-authored-by: Robert Nagy --- lib/api/readable.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index fd665c5304c..787cb94e35e 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -151,7 +151,6 @@ module.exports = class BodyReadable extends Readable { if (typeof signal !== 'object' || !('aborted' in signal)) { throw new InvalidArgumentError('signal must be an AbortSignal') } - console.log(signal.throwIfAborted) signal.throwIfAborted() signal.addEventListener('abort', () => { this.destroy() From e57948eb34ae3b8aa07e04860efb42e308e0ec1c Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Thu, 9 Mar 2023 00:30:39 +0530 Subject: [PATCH 18/24] fixup! use fallback throwOnError --- lib/api/readable.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/api/readable.js b/lib/api/readable.js index 787cb94e35e..2c56a9f9cdb 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -151,6 +151,17 @@ module.exports = class BodyReadable extends Readable { if (typeof signal !== 'object' || !('aborted' in signal)) { throw new InvalidArgumentError('signal must be an AbortSignal') } + if (!signal.throwIfAborted) { + // add in a throwIfAborted method to the signal + signal.throwIfAborted = function () { + if (signal.aborted) { + // DOMException not available < v17.0.0 + const err = new Error('The operation was aborted') + err.name = 'AbortError' + throw err + } + } + } signal.throwIfAborted() signal.addEventListener('abort', () => { this.destroy() From 072db990639b41602badd6e1d67301d8c02bec99 Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Thu, 9 Mar 2023 00:35:37 +0530 Subject: [PATCH 19/24] fixup! possible test fix --- test/client-request.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/client-request.js b/test/client-request.js index db54090325e..2d73e9892ee 100644 --- a/test/client-request.js +++ b/test/client-request.js @@ -59,7 +59,13 @@ test('request dump with abort signal', (t) => { method: 'GET' }, (err, { body }) => { t.error(err) - const ac = new AbortController() + let ac + if (!global.AbortController) { + const { AbortController } = require('abort-controller') + ac = new AbortController() + } else { + ac = new AbortController() + } body.dump({ signal: ac.signal }).catch((err) => { t.equal(err.name, 'AbortError') server.close() From 7ca1bf310d02475d5b1b98a3a1046ed4d277d13a Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Thu, 9 Mar 2023 01:08:37 +0530 Subject: [PATCH 20/24] fixup! use as utility --- lib/api/readable.js | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 2c56a9f9cdb..11c4a3535d2 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -147,40 +147,38 @@ module.exports = class BodyReadable extends Readable { async dump (opts) { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 const signal = opts && opts.signal + function throwIfAborted () { + if (!signal) { return } + if (signal.throwIfAborted) { + signal.throwIfAborted() + } else { + if (signal.aborted) { + // DOMException not available < v17.0.0 + const err = new Error('The operation was aborted') + err.name = 'AbortError' + throw err + } + } + } if (signal) { if (typeof signal !== 'object' || !('aborted' in signal)) { throw new InvalidArgumentError('signal must be an AbortSignal') } - if (!signal.throwIfAborted) { - // add in a throwIfAborted method to the signal - signal.throwIfAborted = function () { - if (signal.aborted) { - // DOMException not available < v17.0.0 - const err = new Error('The operation was aborted') - err.name = 'AbortError' - throw err - } - } - } - signal.throwIfAborted() + throwIfAborted() signal.addEventListener('abort', () => { this.destroy() }) } try { for await (const chunk of this) { - if (signal) { - signal.throwIfAborted() - } + throwIfAborted() limit -= Buffer.byteLength(chunk) if (limit < 0) { return } } } catch { - if (signal) { - signal.throwIfAborted() - } + throwIfAborted() } } } From 7a72cca89f6c99191fdd42e496e74638ec27143b Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Thu, 9 Mar 2023 13:36:48 +0530 Subject: [PATCH 21/24] fixup! use listener once --- lib/api/readable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 11c4a3535d2..8fb46a218d6 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -167,7 +167,7 @@ module.exports = class BodyReadable extends Readable { throwIfAborted() signal.addEventListener('abort', () => { this.destroy() - }) + }, { once: true }) } try { for await (const chunk of this) { From ae2d7120324cae1e17cc4ea328ccf288ba4d503f Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Thu, 9 Mar 2023 13:42:48 +0530 Subject: [PATCH 22/24] fixup! move to util --- lib/api/readable.js | 19 +++---------------- lib/core/util.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index 8fb46a218d6..cb3c1f2ba34 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -147,38 +147,25 @@ module.exports = class BodyReadable extends Readable { async dump (opts) { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 const signal = opts && opts.signal - function throwIfAborted () { - if (!signal) { return } - if (signal.throwIfAborted) { - signal.throwIfAborted() - } else { - if (signal.aborted) { - // DOMException not available < v17.0.0 - const err = new Error('The operation was aborted') - err.name = 'AbortError' - throw err - } - } - } if (signal) { if (typeof signal !== 'object' || !('aborted' in signal)) { throw new InvalidArgumentError('signal must be an AbortSignal') } - throwIfAborted() + util.throwIfAborted(signal) signal.addEventListener('abort', () => { this.destroy() }, { once: true }) } try { for await (const chunk of this) { - throwIfAborted() + util.throwIfAborted(signal) limit -= Buffer.byteLength(chunk) if (limit < 0) { return } } } catch { - throwIfAborted() + util.throwIfAborted(signal) } } } diff --git a/lib/core/util.js b/lib/core/util.js index 334bc26c776..3241fbdcb0d 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -389,6 +389,20 @@ function isFormDataLike (object) { ) } +function throwIfAborted (signal) { + if (!signal) { return } + if (signal.throwIfAborted) { + signal.throwIfAborted() + } else { + if (signal.aborted) { + // DOMException not available < v17.0.0 + const err = new Error('The operation was aborted') + err.name = 'AbortError' + throw err + } + } +} + const kEnumerableProperty = Object.create(null) kEnumerableProperty.enumerable = true @@ -420,6 +434,7 @@ module.exports = { getSocketInfo, isFormDataLike, buildURL, + throwIfAborted, nodeMajor, nodeMinor, nodeHasAutoSelectFamily: nodeMajor > 18 || (nodeMajor === 18 && nodeMinor >= 13) From e5de0754c2c574f6432372a2b3c677a942687d5a Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Thu, 9 Mar 2023 14:18:23 +0530 Subject: [PATCH 23/24] Update lib/core/util.js Co-authored-by: Robert Nagy --- lib/core/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/util.js b/lib/core/util.js index 3241fbdcb0d..b3f8a546198 100644 --- a/lib/core/util.js +++ b/lib/core/util.js @@ -391,7 +391,7 @@ function isFormDataLike (object) { function throwIfAborted (signal) { if (!signal) { return } - if (signal.throwIfAborted) { + if (typeof signal.throwIfAborted === 'function') { signal.throwIfAborted() } else { if (signal.aborted) { From 5018f29a956fce75089883946e4720b32b6dee8f Mon Sep 17 00:00:00 2001 From: Debadree Chatterjee Date: Thu, 9 Mar 2023 14:20:12 +0530 Subject: [PATCH 24/24] fixup! also remove on finally --- lib/api/readable.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/api/readable.js b/lib/api/readable.js index cb3c1f2ba34..a184e8eb51b 100644 --- a/lib/api/readable.js +++ b/lib/api/readable.js @@ -147,14 +147,15 @@ module.exports = class BodyReadable extends Readable { async dump (opts) { let limit = opts && Number.isFinite(opts.limit) ? opts.limit : 262144 const signal = opts && opts.signal + const abortFn = () => { + this.destroy() + } if (signal) { if (typeof signal !== 'object' || !('aborted' in signal)) { throw new InvalidArgumentError('signal must be an AbortSignal') } util.throwIfAborted(signal) - signal.addEventListener('abort', () => { - this.destroy() - }, { once: true }) + signal.addEventListener('abort', abortFn, { once: true }) } try { for await (const chunk of this) { @@ -166,6 +167,10 @@ module.exports = class BodyReadable extends Readable { } } catch { util.throwIfAborted(signal) + } finally { + if (signal) { + signal.removeEventListener('abort', abortFn) + } } } }