Perf: read most of files in one pass

This commit is contained in:
SukkaW
2024-01-15 13:32:12 +08:00
parent a3e1a85c70
commit a7fc13b355
7 changed files with 46 additions and 35 deletions

View File

@@ -3,6 +3,7 @@ import { fetchWithRetry, defaultRequestInit } from './fetch-retry';
import { TextLineStream } from './text-line-transform-stream';
import { PolyfillTextDecoderStream } from './text-decoder-stream';
import { processLine } from './process-line';
// function createTextLineStreamFromStreamSource(stream: ReadableStream<Uint8Array>) {
// return stream
// .pipeThrough(new PolyfillTextDecoderStream())
@@ -54,3 +55,14 @@ export function createReadlineInterfaceFromResponse(this: void, resp: Response)
export function fetchRemoteTextByLine(url: string | URL) {
return fetchWithRetry(url, defaultRequestInit).then(createReadlineInterfaceFromResponse);
}
export async function readFileIntoProcessedArray(file: string | URL | BunFile) {
if (typeof file === 'string') {
file = Bun.file(file);
} else if (!('writer' in file)) {
file = Bun.file(file);
}
const content = await file.text();
return content.split('\n').filter(processLine);
}