mirror of
https://github.com/SukkaW/Surge.git
synced 2026-01-29 01:51:52 +08:00
Refactor: speed up reject parsing
This commit is contained in:
61
Build/lib/fetch-assets.ts
Normal file
61
Build/lib/fetch-assets.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import picocolors from 'picocolors';
|
||||
import { defaultRequestInit, fetchWithRetry } from './fetch-retry';
|
||||
|
||||
class CustomAbortError extends Error {
|
||||
public readonly name = 'AbortError';
|
||||
public readonly digest = 'AbortError';
|
||||
}
|
||||
|
||||
const sleepWithAbort = (ms: number, signal: AbortSignal) => new Promise<void>((resolve, reject) => {
|
||||
signal.throwIfAborted();
|
||||
signal.addEventListener('abort', stop);
|
||||
Bun.sleep(ms).then(done).catch(doReject);
|
||||
|
||||
function done() {
|
||||
signal.removeEventListener('abort', stop);
|
||||
resolve();
|
||||
}
|
||||
function stop(this: AbortSignal) {
|
||||
reject(this.reason);
|
||||
}
|
||||
function doReject(reason: unknown) {
|
||||
signal.removeEventListener('abort', stop);
|
||||
reject(reason);
|
||||
}
|
||||
});
|
||||
|
||||
export async function fetchAssets(url: string, fallbackUrls: string[] | readonly string[]) {
|
||||
const controller = new AbortController();
|
||||
|
||||
const fetchMainPromise = fetchWithRetry(url, { signal: controller.signal, ...defaultRequestInit })
|
||||
.then(r => r.text())
|
||||
.then(text => {
|
||||
controller.abort();
|
||||
return text;
|
||||
});
|
||||
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.
|
||||
try {
|
||||
await sleepWithAbort(500 + (index + 1) * 20, controller.signal);
|
||||
} catch {
|
||||
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));
|
||||
throw new CustomAbortError();
|
||||
}
|
||||
const res = await fetchWithRetry(url, { signal: controller.signal, ...defaultRequestInit });
|
||||
const text = await res.text();
|
||||
controller.abort();
|
||||
return text;
|
||||
};
|
||||
|
||||
return Promise.any([
|
||||
fetchMainPromise,
|
||||
...fallbackUrls.map(createFetchFallbackPromise)
|
||||
]).catch(e => {
|
||||
console.log(`Download Rule for [${url}] failed`);
|
||||
throw e;
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user