From 941223544c7517609cb6c55139eb4d1e92563cea 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 | 11 +++++++++++ 3 files changed, 24 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..548dbf0fa 100644 --- a/test/normalize-arguments.ts +++ b/test/normalize-arguments.ts @@ -167,3 +167,14 @@ test('searchParams - multiple values for one key', t => { ['100', '200', '300'], ); }); + +test('signal does not get frozen', t => { + const controller = new AbortController(); + const options = new Options({ + url: new URL('http://localhost'), + signal: controller.signal, + }); + options.freeze(); + + t.is(Object.isFrozen(options.signal), false); +});