Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clearing setTimeout , that controls timeout feature #362

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/fetch.ts
@@ -1,19 +1,19 @@
import type { Readable } from "node:stream";
import destr from "destr";
import type { Readable } from "node:stream";
import { withBase, withQuery } from "ufo";
import { createFetchError } from "./error";
import {
isPayloadMethod,
isJSONSerializable,
detectResponseType,
mergeFetchOptions,
} from "./utils";
import type {
$Fetch,
CreateFetchOptions,
FetchResponse,
FetchContext,
$Fetch,
FetchResponse,
} from "./types";
import {
detectResponseType,
isJSONSerializable,
isPayloadMethod,
mergeFetchOptions,
} from "./utils";

// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
const retryStatusCodes = new Set([
Expand All @@ -30,14 +30,14 @@ const retryStatusCodes = new Set([
// https://developer.mozilla.org/en-US/docs/Web/API/Response/body
const nullBodyResponses = new Set([101, 204, 205, 304]);

export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
export function createFetch (globalOptions: CreateFetchOptions = {}): $Fetch {
const {
fetch = globalThis.fetch,
Headers = globalThis.Headers,
AbortController = globalThis.AbortController,
} = globalOptions;

async function onError(context: FetchContext): Promise<FetchResponse<any>> {
async function onError (context: FetchContext): Promise<FetchResponse<any>> {
// Is Abort
// If it is an active abort, it will not retry automatically.
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException#error_names
Expand Down Expand Up @@ -85,7 +85,7 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
throw error;
}

const $fetchRaw: $Fetch["raw"] = async function $fetchRaw(
const $fetchRaw: $Fetch["raw"] = async function $fetchRaw (
_request,
_options = {}
) {
Expand Down Expand Up @@ -138,7 +138,7 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
// ReadableStream Body
("pipeTo" in (context.options.body as ReadableStream) &&
typeof (context.options.body as ReadableStream).pipeTo ===
"function") ||
"function") ||
// Node.js Stream Body
typeof (context.options.body as Readable).pipe === "function"
) {
Expand All @@ -150,9 +150,10 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
}

// TODO: Can we merge signals?
let tControl
if (!context.options.signal && context.options.timeout) {
const controller = new AbortController();
setTimeout(() => controller.abort(), context.options.timeout);
tControl = setTimeout(() => controller.abort(), context.options.timeout);
context.options.signal = controller.signal;
}

Expand All @@ -166,6 +167,7 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
if (context.options.onRequestError) {
await context.options.onRequestError(context as any);
}
if (tControl) clearTimeout(tControl)
return await onError(context);
}

Expand Down Expand Up @@ -210,13 +212,14 @@ export function createFetch(globalOptions: CreateFetchOptions = {}): $Fetch {
if (context.options.onResponseError) {
await context.options.onResponseError(context as any);
}
if (tControl) clearTimeout(tControl)
return await onError(context);
}

if (tControl) clearTimeout(tControl)
return context.response;
};

const $fetch = async function $fetch(request, options) {
const $fetch = async function $fetch (request, options) {
const r = await $fetchRaw(request, options);
return r._data;
} as $Fetch;
Expand Down