mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-14 02:00:37 +08:00
Refactor: simplify parallel cached fetching
This commit is contained in:
parent
36d2b1102d
commit
a92a0f6f1a
@ -305,49 +305,19 @@ export class Cache<S = string> {
|
|||||||
|
|
||||||
const previouslyCached = this.get(cachedKey);
|
const previouslyCached = this.get(cachedKey);
|
||||||
|
|
||||||
const primaryETag = this.get(getETagKey(primaryUrl));
|
|
||||||
const fetchMainPromise = fetchWithRetry(
|
|
||||||
primaryUrl,
|
|
||||||
{
|
|
||||||
signal: controller.signal,
|
|
||||||
...defaultRequestInit,
|
|
||||||
headers: (typeof primaryETag === 'string' && primaryETag.length > 0)
|
|
||||||
? mergeHeaders(
|
|
||||||
defaultRequestInit.headers,
|
|
||||||
{ 'If-None-Match': primaryETag }
|
|
||||||
)
|
|
||||||
: defaultRequestInit.headers
|
|
||||||
}
|
|
||||||
).then(r => {
|
|
||||||
if (r.headers.has('etag')) {
|
|
||||||
this.set(getETagKey(primaryUrl), r.headers.get('etag')!, TTL.ONE_WEEK_STATIC);
|
|
||||||
|
|
||||||
// If we do not have a cached value, we ignore 304
|
|
||||||
if (r.status === 304 && typeof previouslyCached === 'string') {
|
|
||||||
controller.abort();
|
|
||||||
throw new Custom304NotModifiedError(primaryUrl, previouslyCached);
|
|
||||||
}
|
|
||||||
} else if (!primaryETag && typeof previouslyCached === 'string') {
|
|
||||||
throw new CustomNoETagFallbackError(previouslyCached);
|
|
||||||
}
|
|
||||||
|
|
||||||
return r.text();
|
|
||||||
}).then(text => {
|
|
||||||
controller.abort();
|
|
||||||
return text;
|
|
||||||
});
|
|
||||||
|
|
||||||
const createFetchFallbackPromise = async (url: string, index: number) => {
|
const createFetchFallbackPromise = async (url: string, index: number) => {
|
||||||
// Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 500ms before downloading from the fallback URL.
|
// Most assets can be downloaded within 250ms. To avoid wasting bandwidth, we will wait for 500ms before downloading from the fallback URL.
|
||||||
try {
|
if (index > 0) {
|
||||||
await sleepWithAbort(300 + (index + 1) * 10, controller.signal);
|
try {
|
||||||
} catch {
|
await sleepWithAbort(300 + (index + 1) * 10, controller.signal);
|
||||||
console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
|
} catch {
|
||||||
throw new CustomAbortError();
|
console.log(picocolors.gray('[fetch cancelled early]'), picocolors.gray(url));
|
||||||
}
|
throw new CustomAbortError();
|
||||||
if (controller.signal.aborted) {
|
}
|
||||||
console.log(picocolors.gray('[fetch cancelled]'), picocolors.gray(url));
|
if (controller.signal.aborted) {
|
||||||
throw new CustomAbortError();
|
console.log(picocolors.gray('[fetch cancelled]'), picocolors.gray(url));
|
||||||
|
throw new CustomAbortError();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const etag = this.get(getETagKey(url));
|
const etag = this.get(getETagKey(url));
|
||||||
@ -373,11 +343,13 @@ export class Cache<S = string> {
|
|||||||
controller.abort();
|
controller.abort();
|
||||||
throw new Custom304NotModifiedError(url, previouslyCached);
|
throw new Custom304NotModifiedError(url, previouslyCached);
|
||||||
}
|
}
|
||||||
} else if (!primaryETag && typeof previouslyCached === 'string') {
|
} else if (!this.get(getETagKey(primaryUrl)) && typeof previouslyCached === 'string') {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
throw new CustomNoETagFallbackError(previouslyCached);
|
throw new CustomNoETagFallbackError(previouslyCached);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// either no etag and not cached
|
||||||
|
// or has etag but not 304
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
controller.abort();
|
controller.abort();
|
||||||
return text;
|
return text;
|
||||||
@ -385,7 +357,7 @@ export class Cache<S = string> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const text = await Promise.any([
|
const text = await Promise.any([
|
||||||
fetchMainPromise,
|
createFetchFallbackPromise(primaryUrl, -1),
|
||||||
...mirrorUrls.map(createFetchFallbackPromise)
|
...mirrorUrls.map(createFetchFallbackPromise)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -405,7 +377,7 @@ export class Cache<S = string> {
|
|||||||
if (error instanceof Custom304NotModifiedError) {
|
if (error instanceof Custom304NotModifiedError) {
|
||||||
console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl));
|
console.log(picocolors.green('[cache] http 304'), picocolors.gray(primaryUrl));
|
||||||
this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC);
|
this.updateTtl(cachedKey, TTL.ONE_WEEK_STATIC);
|
||||||
return deserializer(previouslyCached);
|
return deserializer(error.data);
|
||||||
}
|
}
|
||||||
if (error instanceof CustomNoETagFallbackError) {
|
if (error instanceof CustomNoETagFallbackError) {
|
||||||
console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl));
|
console.log(picocolors.green('[cache] hit'), picocolors.gray(primaryUrl));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user