Skip to content
Myr

Fix handling of a signal that is already aborted before the request begins (#663)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: throw early if signal is aborted before request is triggered * fix: abort if the original signal also aborted early * fix: linter issues * fix: pass abort reason to early abort * fix: check if server receives request after abort * fix: lint issues

authored by Kadhirravan R · committed by GitHub · parent 1d92c20 · Browse files · 2 files+24 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−1 removed

  • tests:+20 added−0 removed
  • other:+4 added−1 removed

2 files+24 added−1 removed

modified:source/core/Ky.ts+4 added-1 removed
161 unmodified lines
162163 if (supportsAbortController) {164 this.abortController = new globalThis.AbortController();165 const originalSignal = this._options.signal ?? (this._input as Request).signal;166 if (originalSignal?.aborted) {167 this.abortController.abort(originalSignal?.reason);168 }169170 originalSignal?.addEventListener('abort', () => {171 this.abortController!.abort(originalSignal.reason);172 });169173 this._options.signal = this.abortController.signal;174 }175176 if (supportsRequestStreams) {
194 unmodified lines
modified:test/main.ts+20 added
711 unmodified lines
712 t.true(['DOMException', 'Error'].includes(error.constructor.name), `Expected DOMException or Error, got ${error.constructor.name}`);713 t.is(error.name, 'AbortError', `Expected AbortError, got ${error.name}`);714});715716test('throws AbortError when signal was aborted before request', async t => {717 const server = await createHttpTestServer();718 let requestCount = 0;719 server.get('/', () => {720 requestCount += 1;721 });722723 const abortController = new AbortController();724 const {signal} = abortController;725 const request = new Request(server.url, {signal});726 abortController.abort();727 const response = ky(request);728729 const error = (await t.throwsAsync(response))!;730731 t.true(['DOMException', 'Error'].includes(error.constructor.name), `Expected DOMException or Error, got ${error.constructor.name}`);732 t.is(error.name, 'AbortError', `Expected AbortError, got ${error.name}`);733 t.is(requestCount, 0, 'Request count is more than 0, server received request.');734});735736test('throws AbortError when aborted via Request', async t => {737 const server = await createHttpTestServer();738 // eslint-disable-next-line @typescript-eslint/no-empty-function739 server.get('/', () => {});
170 unmodified lines