Skip to content
Myr

Fix abortions between retries (#433)

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

authored by Richie Bendall · committed by GitHub · parent 6d9f7c8 · Browse files · 8 files+87 added−41 removed
This commit

State

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

Trailers

  • Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>

Diff stats

8 files+87 added−41 removed

  • tests:+7 added−4 removed
  • other:+80 added−37 removed

8 files+87 added−41 removed

modified:source/core/Ky.ts+6 added-3 removed
3 unmodified lines
4import type {Input, InternalOptions, NormalizedOptions, Options, SearchParamsInit} from '../types/options.js';5import {ResponsePromise} from '../types/ResponsePromise.js';6import {deepMerge, mergeHeaders} from '../utils/merge.js';7import {normalizeRequestMethod, normalizeRetryOptions} from '../utils/normalize.js';8import {delay, timeout, TimeoutOptions} from '../utils/time.js';8import timeout, {TimeoutOptions} from '../utils/timeout.js';9import delay from '../utils/delay.js';10import {ObjectEntries} from '../utils/types.js';11import {12 maxSafeTimeout,13 responseTypes,
139 unmodified lines
153154 if (supportsAbortController) {155 this.abortController = new globalThis.AbortController();156 if (this._options.signal) {157 const originalSignal = this._options.signal;158159 this._options.signal.addEventListener('abort', () => {157 this.abortController!.abort();160 this.abortController!.abort(originalSignal.reason);161 });162 }163164 this._options.signal = this.abortController.signal;
84 unmodified lines
249 // eslint-disable-next-line @typescript-eslint/no-implicit-any-catch250 } catch (error) {251 const ms = Math.min(this._calculateRetryDelay(error), maxSafeTimeout);252 if (ms !== 0 && this._retryCount > 0) {250 await delay(ms);253 await delay(ms, {signal: this._options.signal});254255 for (const hook of this._options.hooks.beforeRetry) {256 // eslint-disable-next-line no-await-in-loop257 const hookResult = await hook({
98 unmodified lines
added:source/errors/DOMException.ts+8 added
added:source/utils/delay.ts+34 added
deleted:source/utils/time.ts-34 removed
added:source/utils/timeout.ts+31 added
modified:test/browser.ts+4 added-2 removed
81 unmodified lines
8283 const error = await page.evaluate(async (url: string) => {84 const controller = new AbortController();85 const request = window.ky(`${url}/test`, {signal: controller.signal}).text();86 controller.abort();86 controller.abort('🦄');87 return request.catch(error_ => error_.toString());88 }, server.url);89 t.is(error, 'AbortError: Failed to execute \'fetch\' on \'Window\': The user aborted a request.');8990 // TODO: When targeting Node.js 18, also assert that the error is a DOMException91 t.is(error.split(': ')[1], '🦄');9293 await server.close();94});9596test('should copy origin response info when using `onDownloadProgress`', withPage, async (t: ExecutionContext, page: Page) => {
427 unmodified lines
modified:test/main.ts+3 added-2 removed
550 unmodified lines
551552 await server.close();553});554555test('throws AbortError when aborted by user', async t => {555test('throws DOMException when aborted by user', async t => {556 const server = await createHttpTestServer();557 // eslint-disable-next-line @typescript-eslint/no-empty-function558 server.get('/', () => {});559
1 unmodified line
561 const {signal} = abortController;562 const response = ky(server.url, {signal});563 abortController.abort();564565 await t.throwsAsync(response, {name: 'AbortError'});565 const {name} = (await t.throwsAsync(response))!;566 t.true(['DOMException', 'Error'].includes(name), `Expected DOMException or Error, got ${name}`);567});568569test('supports Request instance as input', async t => {570 const server = await createHttpTestServer();
133 unmodified lines
modified:package.json+1 added