Perf: faster fetchAssets (without string and manual split)
Some checks are pending
Build / Build (push) Waiting to run
Build / Deploy to Cloudflare Pages (push) Blocked by required conditions
Build / Deploy to GitHub and GitLab (push) Blocked by required conditions

This commit is contained in:
SukkaW
2025-01-22 10:52:03 +08:00
parent d97a866352
commit 07419a7942
5 changed files with 28 additions and 40 deletions

View File

@@ -1,6 +1,9 @@
import picocolors from 'picocolors';
import { $$fetch, defaultRequestInit, ResponseError } from './fetch-retry';
import { waitWithAbort } from 'foxts/wait';
import { nullthrow } from 'foxts/guard';
import { TextLineStream } from './text-line-transform-stream';
import { ProcessLineStream } from './process-line';
// eslint-disable-next-line sukka/unicorn/custom-error-definition -- typescript is better
export class CustomAbortError extends Error {
@@ -26,7 +29,7 @@ export class CustomNoETagFallbackError extends Error {
}
}
export async function fetchAssets(url: string, fallbackUrls: null | undefined | string[] | readonly string[]) {
export async function fetchAssets(url: string, fallbackUrls: null | undefined | string[] | readonly string[], processLine = false) {
const controller = new AbortController();
const createFetchFallbackPromise = async (url: string, index: number) => {
@@ -44,14 +47,19 @@ export async function fetchAssets(url: string, fallbackUrls: null | undefined |
throw new CustomAbortError();
}
const res = await $$fetch(url, { signal: controller.signal, ...defaultRequestInit });
const text = await res.text();
if (text.length < 2) {
let stream = nullthrow(res.body).pipeThrough(new TextDecoderStream()).pipeThrough(new TextLineStream());
if (processLine) {
stream = stream.pipeThrough(new ProcessLineStream());
}
const arr = await Array.fromAsync(stream);
if (arr.length < 1) {
throw new ResponseError(res, url, 'empty response w/o 304');
}
controller.abort();
return text;
return arr;
};
if (!fallbackUrls || fallbackUrls.length === 0) {