Skip to content

Commit

Permalink
EIR stage 3
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoalh committed Nov 15, 2023
1 parent 40f7763 commit 84c5bd9
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions exfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ interface ExFetchRedirectOptionsInternal {
delay: ExFetchDelayOptionsInternal;
/**
* Maximum amount of redirects to allow.
* @default 5
* @default Infinity
*/
maximum: number;
/**
Expand Down Expand Up @@ -270,7 +270,7 @@ export interface ExFetchOptions {
*/
paginate?: ExFetchPaginateOptions;
/**
* Redirect options. This only apply when define property `redirect` as `"manual"` in the request.
* Redirect options. This only apply when define property `redirect` as `"follow"` in the request, and define property `maximum` in this option.
*/
redirect?: ExFetchRedirectOptions;
/**
Expand Down Expand Up @@ -421,7 +421,7 @@ export class ExFetch {
maximum: 0,
minimum: 0
},
maximum: 5,
maximum: Infinity,
onEvent: undefined
};
#retry: ExFetchRetryOptionsInternal = {
Expand Down Expand Up @@ -549,12 +549,12 @@ export class ExFetch {
if (new URL(input).protocol === "file:") {
return fetch(input, init);
}
const requestCacheOption: RequestCache | undefined = init?.cache;
const requestCacheCommonCondition: boolean = new URL(input).protocol === "https:" && (
typeof init === "undefined" ||
typeof init.method === "undefined" ||
init.method.toUpperCase() === "GET"
);
const requestCacheOption: RequestCache | undefined = init?.cache;
const requestFuzzy: Request = new Request(input, {
...init,
cache: undefined,
Expand Down Expand Up @@ -589,6 +589,7 @@ export class ExFetch {
if (!requestHeaders.has("User-Agent") && this.#userAgent.length > 0) {
requestHeaders.set("User-Agent", this.#userAgent);
}
const requestRedirectControl: boolean = init?.redirect === "follow" && this.#redirect.maximum !== Infinity;
let requestSignal: AbortSignal | undefined = init?.signal ?? undefined;
if (typeof requestSignal === "undefined" && this.#timeout !== Infinity) {
requestSignal = AbortSignal.timeout(this.#timeout);
Expand All @@ -597,22 +598,47 @@ export class ExFetch {
const requestFetchInit: RequestInit = {
...init,
headers: requestHeaders,
redirect: requestRedirectControl ? "manual" : init?.redirect,
signal: requestSignal
};
let redirects = 0;
let retries = 0;
let response: Response;
do {
response = await fetch(requestFetchInput, requestFetchInit);
if (typeof responseCached !== "undefined" && responseCachedIsValid && response.status === 304) {
return responseCached;
if (response.status === 304) {
if (typeof responseCached !== "undefined" && responseCachedIsValid) {
return responseCached;
}
break;
}
if (httpStatusCodesRedirectable.includes(response.status)) {
if (requestRedirectControl && httpStatusCodesRedirectable.includes(response.status)) {
if (redirects >= this.#redirect.maximum) {
break;
}
const redirectURL: string | null = response.headers.get("Location");
if (redirectURL === null) {
break;
}
redirects += 1;
try {

requestFetchInput = new URL(redirectURL, input);
} catch {

break;
}
const delayTime: number = resolveDelayTime(this.#redirect.delay);
if (typeof this.#redirect.onEvent !== "undefined") {
void this.#redirect.onEvent({
countCurrent: redirects,
countMaximum: this.#redirect.maximum,
redirectAfter: delayTime,
redirectURL: new URL(requestFetchInput),
statusCode: response.status,
statusText: response.statusText
});
}
await setDelay(delayTime, requestSignal);
continue;
}
if (
response.ok ||
Expand All @@ -621,6 +647,7 @@ export class ExFetch {
) {
break;
}
retries += 1;
let delayTime: number;
try {
delayTime = new HTTPHeaderRetryAfter(response).getRemainTimeMilliseconds();
Expand All @@ -629,7 +656,7 @@ export class ExFetch {
}
if (typeof this.#retry.onEvent !== "undefined") {
void this.#retry.onEvent({
countCurrent: retries + 1,
countCurrent: retries,
countMaximum: this.#retry.maximum,
retryAfter: delayTime,
retryURL: new URL(requestFetchInput),
Expand All @@ -638,9 +665,8 @@ export class ExFetch {
});
}
await setDelay(delayTime, requestSignal);
retries += 1;
} while (retries <= this.#retry.maximum);
if (requestCacheCommonCondition && requestCacheOption !== "no-store" && response.ok && response.status >= 200 && response.status < 300 && (
if (requestCacheCommonCondition && requestCacheOption !== "no-store" && response.ok && (
response.headers.has("ETag") ||
response.headers.has("Last-Modified")
)) {
Expand Down

0 comments on commit 84c5bd9

Please sign in to comment.