Skip to content

Commit

Permalink
fix(fetch): signal should not be nullable (#81)
Browse files Browse the repository at this point in the history
* feat: signal should not be nullable

https://fetch.spec.whatwg.org/#ref-for-map-exists%E2%91%A0%E2%91%A3

* chore: fix changeset

---------

Co-authored-by: Jacob Ebey <[email protected]>
  • Loading branch information
MichaelDeBoey and jacob-ebey authored Aug 28, 2023
1 parent c97e2b8 commit dff75d9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-fans-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@web-std/fetch": patch
---

Make `Request` signal handling follow spec: https://fetch.spec.whatwg.org/#ref-for-map-exists%E2%91%A0%E2%91%A3
2 changes: 1 addition & 1 deletion packages/fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@
"@types/mocha": "^9.1.0",
"@types/chai-as-promised": "^7.1.5",
"@types/chai-string": "^1.4.2",
"abort-controller": "^3.0.0",
"abortcontroller-polyfill": "^1.7.1",
"busboy": "^0.3.1",
"c8": "^7.3.0",
Expand All @@ -100,6 +99,7 @@
"@web-std/blob": "^3.0.3",
"@web-std/form-data": "^3.0.2",
"@web-std/stream": "^1.0.1",
"abort-controller": "^3.0.0",
"data-uri-to-buffer": "^3.0.1",
"mrmime": "^1.0.0",
"@web3-storage/multipart-parser": "^1.0.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/fetch/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async function fetch(url, options_ = {}) {
// Note: We can not use `request.body` because send would have
// consumed it already.
body: options_.body,
signal: request.signal,
signal: signal,
size: request.size
};

Expand Down
10 changes: 10 additions & 0 deletions packages/fetch/src/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import {format as formatUrl} from 'url';
import {AbortController as AbortControllerPolyfill} from 'abort-controller';
import Headers from './headers.js';
import Body, {clone, extractContentType, getTotalBytes} from './body.js';
import {isAbortSignal} from './utils/is.js';
Expand Down Expand Up @@ -128,6 +129,15 @@ export default class Request extends Body {
if (signal != null && !isAbortSignal(signal)) {
throw new TypeError('Expected signal to be an instanceof AbortSignal or EventTarget');
}

if (!signal) {
let AbortControllerConstructor = typeof AbortController != "undefined"
? AbortController
: AbortControllerPolyfill;
/** @type {any} */
let newSignal = new AbortControllerConstructor().signal;
signal = newSignal;
}

/** @type {RequestState} */
this[INTERNALS] = {
Expand Down
5 changes: 3 additions & 2 deletions packages/fetch/test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe('Request', () => {
expect(derivedRequest.signal).to.equal(derivedAbortController.signal);
});

it('should allow removing signal on derived Request instances', () => {
it('should allow overriding signal on derived Request instances', () => {
const parentAbortController = new AbortController();
const parentRequest = new Request(`${base}hello`, {
signal: parentAbortController.signal
Expand All @@ -161,7 +161,8 @@ describe('Request', () => {
signal: null
});
expect(parentRequest.signal).to.equal(parentAbortController.signal);
expect(derivedRequest.signal).to.equal(null);
expect(derivedRequest.signal).to.not.equal(null);
expect(derivedRequest.signal).to.not.equal(parentAbortController.signal);
});

it('should default to "same-origin" as credentials', () => {
Expand Down

0 comments on commit dff75d9

Please sign in to comment.