From a133b37bab40cffabe50897246b08ce6a1798d9b Mon Sep 17 00:00:00 2001 From: Simon Wachter Date: Thu, 4 Aug 2022 12:23:27 +0200 Subject: [PATCH] Don't freeze signal when freezing Options (#2099) --- source/core/options.ts | 1 - test/abort.ts | 13 +++++++++++++ test/normalize-arguments.ts | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/source/core/options.ts b/source/core/options.ts index 38191f38e..110e763e7 100644 --- a/source/core/options.ts +++ b/source/core/options.ts @@ -2521,6 +2521,5 @@ export default class Options { Object.freeze(options.retry.methods); Object.freeze(options.retry.statusCodes); Object.freeze(options.context); - Object.freeze(options.signal); } } diff --git a/test/abort.ts b/test/abort.ts index 3312b090f..fea2c1c67 100644 --- a/test/abort.ts +++ b/test/abort.ts @@ -271,4 +271,17 @@ if (globalThis.AbortController !== undefined) { message: 'This operation was aborted.', }); }); + + test('support setting the signal as a default option', async t => { + const controller = new AbortController(); + + const got2 = got.extend({signal: controller.signal}); + const p = got2('http://example.com', {signal: controller.signal}); + controller.abort(); + + await t.throwsAsync(p, { + code: 'ERR_ABORTED', + message: 'This operation was aborted.', + }); + }); } diff --git a/test/normalize-arguments.ts b/test/normalize-arguments.ts index dc0edcc55..12b00594a 100644 --- a/test/normalize-arguments.ts +++ b/test/normalize-arguments.ts @@ -1,4 +1,5 @@ import {URL, URLSearchParams} from 'url'; +import {EventEmitter} from 'events'; import test from 'ava'; import got, {Options} from '../source/index.js'; @@ -167,3 +168,26 @@ test('searchParams - multiple values for one key', t => { ['100', '200', '300'], ); }); + +test('signal does not get frozen', t => { + let signal: AbortSignal; + if (globalThis.AbortSignal === undefined) { + // Fake a signal using a node EventEmitter when the AbortController API is not available + class FakeSignal extends EventEmitter implements AbortSignal { + aborted = false; + throwIfAborted() {} + } + signal = new FakeSignal(); + } else { + const controller = new AbortController(); + signal = controller.signal; + } + + const options = new Options({ + url: new URL('http://localhost'), + signal, + }); + options.freeze(); + + t.is(Object.isFrozen(options.signal), false); +});