Chore: process line stream

This commit is contained in:
SukkaW
2024-11-26 21:22:14 +08:00
parent e2920de2fa
commit f64fa201e9
12 changed files with 67 additions and 43 deletions

View File

@@ -6,7 +6,7 @@ import readline from 'node:readline';
import { TextLineStream } from './text-line-transform-stream';
import type { ReadableStream } from 'node:stream/web';
import { TextDecoderStream } from 'node:stream/web';
import { processLine } from './process-line';
import { processLine, ProcessLineStream } from './process-line';
import { $fetch } from './make-fetch-happen';
import type { NodeFetchResponse } from './make-fetch-happen';
import type { UndiciResponseData } from './fetch-retry';
@@ -40,7 +40,7 @@ function ensureResponseBody<T extends NodeFetchResponse | UndiciResponseData | U
return resp.body;
}
export const createReadlineInterfaceFromResponse: ((resp: NodeFetchResponse | UndiciResponseData | UnidiciWebResponse) => AsyncIterable<string>) = (resp) => {
export const createReadlineInterfaceFromResponse: ((resp: NodeFetchResponse | UndiciResponseData | UnidiciWebResponse, processLine?: boolean) => ReadableStream<string>) = (resp, processLine = false) => {
const stream = ensureResponseBody(resp);
const webStream: ReadableStream<Uint8Array> = 'getReader' in stream
@@ -51,13 +51,18 @@ export const createReadlineInterfaceFromResponse: ((resp: NodeFetchResponse | Un
: Readable.toWeb(new Readable().wrap(stream))
);
return webStream
const resultStream = webStream
.pipeThrough(new TextDecoderStream())
.pipeThrough(new TextLineStream());
if (processLine) {
return resultStream.pipeThrough(new ProcessLineStream());
}
return resultStream;
};
export function fetchRemoteTextByLine(url: string) {
return $fetch(url).then(createReadlineInterfaceFromResponse);
export function fetchRemoteTextByLine(url: string, processLine = false): Promise<AsyncIterable<string>> {
return $fetch(url).then(resp => createReadlineInterfaceFromResponse(resp, processLine));
}
export async function readFileIntoProcessedArray(file: string /* | FileHandle */) {