Skip to content

Commit

Permalink
Node ponyfill requests must have an abort signal (#258)
Browse files Browse the repository at this point in the history
* failing test

* not only

* requests must have an abort signal

* changeset
  • Loading branch information
enisdenjo authored Jan 11, 2023
1 parent 8d61381 commit 802cb96
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-lobsters-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/fetch': patch
---

Node ponyfill requests must have an abort signal
7 changes: 7 additions & 0 deletions packages/fetch/dist/create-node-ponyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ module.exports = function createNodePonyfill(opts = {}) {
super(requestOrUrl);
}
this.formData = getFormDataMethod(formDataModule.File, opts.formDataLimits);
this.requestSignal = this.signal;
this.optionsSignal = options?.signal || (new ponyfills.AbortController()).signal;
}
get signal() {
// node-fetch does not have a Request.signal
// https://github.com/node-fetch/node-fetch/issues/1439
return this.requestSignal || this.optionsSignal
}
}
ponyfills.Request = Request;
Expand Down
9 changes: 9 additions & 0 deletions packages/server/test/request-listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,14 @@ describe('Request Listener', () => {
});
});
});

it('should have the abort signal on the request', async () => {
const handler = jest.fn((_request: Request) => new fetchAPI.Response());
const adapter = createServerAdapter(handler, fetchAPI.Request);

await adapter.fetch('http://localhost');

expect(handler.mock.lastCall?.[0].signal).toBeTruthy();
});
});
});

0 comments on commit 802cb96

Please sign in to comment.