diff --git a/.changeset/unlucky-lobsters-try.md b/.changeset/unlucky-lobsters-try.md new file mode 100644 index 00000000000..f27b1ad758d --- /dev/null +++ b/.changeset/unlucky-lobsters-try.md @@ -0,0 +1,5 @@ +--- +'@whatwg-node/fetch': patch +--- + +Node ponyfill requests must have an abort signal diff --git a/packages/fetch/dist/create-node-ponyfill.js b/packages/fetch/dist/create-node-ponyfill.js index 034cca2403d..54734cc2e5c 100644 --- a/packages/fetch/dist/create-node-ponyfill.js +++ b/packages/fetch/dist/create-node-ponyfill.js @@ -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; diff --git a/packages/server/test/request-listener.spec.ts b/packages/server/test/request-listener.spec.ts index 707f4f14b4a..ed5ac0a9abd 100644 --- a/packages/server/test/request-listener.spec.ts +++ b/packages/server/test/request-listener.spec.ts @@ -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(); + }); }); });