Skip to content

Commit

Permalink
Don't freeze signal when freezing Options (#2099)
Browse files Browse the repository at this point in the history
  • Loading branch information
svvac committed Aug 4, 2022
1 parent e032b60 commit a133b37
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 0 additions & 1 deletion source/core/options.ts
Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions test/abort.ts
Expand Up @@ -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.',
});
});
}
24 changes: 24 additions & 0 deletions 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';

Expand Down Expand Up @@ -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);
});

0 comments on commit a133b37

Please sign in to comment.