From 077d903ca1e3135d4d2662af066200694fae1d47 Mon Sep 17 00:00:00 2001 From: Simon Wachter Date: Thu, 4 Aug 2022 12:23:27 +0200 Subject: [PATCH 1/2] Don't freeze signal when freezing Options (#2099) --- source/core/options.ts | 1 - test/abort.ts | 13 +++++++++++++ test/normalize-arguments.ts | 16 ++++++++++++++++ 3 files changed, 29 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..04b11b333 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,18 @@ test('searchParams - multiple values for one key', t => { ['100', '200', '300'], ); }); + +if (globalThis.AbortSignal !== undefined) { + test('signal does not get frozen', t => { + const controller = new AbortController(); + const {signal} = controller; + + const options = new Options({ + url: new URL('http://localhost'), + signal, + }); + options.freeze(); + + t.is(Object.isFrozen(options.signal), false); + }); +} From 5dee8b55c56cab7e47758eb0380c928d2e405d44 Mon Sep 17 00:00:00 2001 From: Simon Wachter Date: Thu, 4 Aug 2022 14:51:01 +0200 Subject: [PATCH 2/2] Update test/normalize-arguments.ts Co-authored-by: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> --- test/normalize-arguments.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/normalize-arguments.ts b/test/normalize-arguments.ts index 04b11b333..ca25c67e4 100644 --- a/test/normalize-arguments.ts +++ b/test/normalize-arguments.ts @@ -1,5 +1,4 @@ import {URL, URLSearchParams} from 'url'; -import {EventEmitter} from 'events'; import test from 'ava'; import got, {Options} from '../source/index.js';