Skip to content

Commit a133b37

Browse files
committed
Don't freeze signal when freezing Options (sindresorhus#2099)
1 parent e032b60 commit a133b37

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

source/core/options.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2521,6 +2521,5 @@ export default class Options {
25212521
Object.freeze(options.retry.methods);
25222522
Object.freeze(options.retry.statusCodes);
25232523
Object.freeze(options.context);
2524-
Object.freeze(options.signal);
25252524
}
25262525
}

test/abort.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,17 @@ if (globalThis.AbortController !== undefined) {
271271
message: 'This operation was aborted.',
272272
});
273273
});
274+
275+
test('support setting the signal as a default option', async t => {
276+
const controller = new AbortController();
277+
278+
const got2 = got.extend({signal: controller.signal});
279+
const p = got2('http://example.com', {signal: controller.signal});
280+
controller.abort();
281+
282+
await t.throwsAsync(p, {
283+
code: 'ERR_ABORTED',
284+
message: 'This operation was aborted.',
285+
});
286+
});
274287
}

test/normalize-arguments.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {URL, URLSearchParams} from 'url';
2+
import {EventEmitter} from 'events';
23
import test from 'ava';
34
import got, {Options} from '../source/index.js';
45

@@ -167,3 +168,26 @@ test('searchParams - multiple values for one key', t => {
167168
['100', '200', '300'],
168169
);
169170
});
171+
172+
test('signal does not get frozen', t => {
173+
let signal: AbortSignal;
174+
if (globalThis.AbortSignal === undefined) {
175+
// Fake a signal using a node EventEmitter when the AbortController API is not available
176+
class FakeSignal extends EventEmitter implements AbortSignal {
177+
aborted = false;
178+
throwIfAborted() {}
179+
}
180+
signal = new FakeSignal();
181+
} else {
182+
const controller = new AbortController();
183+
signal = controller.signal;
184+
}
185+
186+
const options = new Options({
187+
url: new URL('http://localhost'),
188+
signal,
189+
});
190+
options.freeze();
191+
192+
t.is(Object.isFrozen(options.signal), false);
193+
});

0 commit comments

Comments
 (0)