Skip to content

Commit

Permalink
EIR stage 2
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoalh committed Nov 15, 2023
1 parent cf72f7d commit 40f7763
Showing 1 changed file with 9 additions and 44 deletions.
53 changes: 9 additions & 44 deletions exfetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,46 +288,6 @@ export interface ExFetchOptions {
*/
userAgent?: string;
}
/**
* @access private
*/
interface ExFetchRequestCacheCondition {
allowRestore: boolean;
allowSave: boolean;
useFirst: boolean;
}
/**
* Assert the request whether is able to cache.
* @access private
* @param {string | URL} input
* @param {Parameters<typeof fetch>[1]} init
* @returns {ExFetchRequestCacheCondition}
*/
function assertRequestCacheCondition(input: string | URL, init?: Parameters<typeof fetch>[1]): ExFetchRequestCacheCondition {
const common: boolean = new URL(input).protocol === "https:" && (
typeof init === "undefined" ||
typeof init.method === "undefined" ||
init.method.toUpperCase() === "GET"
);
let allowRestore: boolean = common;
let allowSave: boolean = common;
let useFirst = false;
switch (init?.cache) {
case "force-cache":
useFirst = true;
break;
case "no-store":
allowRestore = false;
allowSave = false;
break;
case "reload":
allowRestore = false;
break;
default:
break;
}
return { allowRestore, allowSave, useFirst };
}
/**
* Resolve delay options.
* @access private
Expand Down Expand Up @@ -589,7 +549,12 @@ export class ExFetch {
if (new URL(input).protocol === "file:") {
return fetch(input, init);
}
const requestCacheCondition: ExFetchRequestCacheCondition = assertRequestCacheCondition(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 requestFuzzy: Request = new Request(input, {
...init,
cache: undefined,
Expand All @@ -602,11 +567,11 @@ export class ExFetch {
window: undefined
});
let responseCached: Response | undefined = undefined;
if (requestCacheCondition.allowRestore) {
if (requestCacheCommonCondition && requestCacheOption !== "no-store" && requestCacheOption !== "reload") {
await this.#cacheStorageLoad();
responseCached = await this.#cacheStorage?.match(requestFuzzy);
}
if (requestCacheCondition.useFirst && typeof responseCached !== "undefined") {
if (requestCacheOption === "force-cache" && typeof responseCached !== "undefined") {
return responseCached;
}
const responseCachedETag: string | undefined = responseCached?.headers.get("ETag") ?? undefined;
Expand Down Expand Up @@ -675,7 +640,7 @@ export class ExFetch {
await setDelay(delayTime, requestSignal);
retries += 1;
} while (retries <= this.#retry.maximum);
if (requestCacheCondition.allowSave && response.ok && response.status >= 200 && response.status < 300 && (
if (requestCacheCommonCondition && requestCacheOption !== "no-store" && response.ok && response.status >= 200 && response.status < 300 && (
response.headers.has("ETag") ||
response.headers.has("Last-Modified")
)) {
Expand Down

0 comments on commit 40f7763

Please sign in to comment.