Support instances of `EventTarget` as a `signal` (#1001)

Co-authored-by: Richie Bendall <richiebendall@gmail.com>
This commit is contained in:
William Orr 2021-01-01 06:37:26 +01:00 committed by GitHub
parent c86886249a
commit 1f4f85e1bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 5 deletions

View File

@ -82,9 +82,8 @@ export default class Request extends Body {
signal = init.signal;
}
// eslint-disable-next-line no-eq-null, eqeqeq
if (signal != null && !isAbortSignal(signal)) {
throw new TypeError('Expected signal to be an instanceof AbortSignal');
throw new TypeError('Expected signal to be an instanceof AbortSignal or EventTarget');
}
this[INTERNALS] = {

View File

@ -74,8 +74,10 @@ export function isFormData(object) {
*/
export const isAbortSignal = object => {
return (
typeof object === 'object' &&
object[NAME] === 'AbortSignal'
typeof object === 'object' && (
object[NAME] === 'AbortSignal' ||
object[NAME] === 'EventTarget'
)
);
};

View File

@ -1021,7 +1021,7 @@ describe('node-fetch', () => {
return result;
});
it('should throw a TypeError if a signal is not of type AbortSignal', () => {
it('should throw a TypeError if a signal is not of type AbortSignal or EventTarget', () => {
return Promise.all([
expect(fetch(`${base}inspect`, {signal: {}}))
.to.be.eventually.rejected