Minor changes

This commit is contained in:
SukkaW
2024-01-14 00:44:46 +08:00
parent 75c9e084a9
commit 897a505c32
13 changed files with 106 additions and 113 deletions

View File

@@ -3,34 +3,34 @@ import { fetchWithRetry, defaultRequestInit } from './fetch-retry';
import { TextLineStream } from './text-line-transform-stream';
import { PolyfillTextDecoderStream } from './text-decoder-stream';
function createTextLineStreamFromStreamSource(stream: ReadableStream<Uint8Array>) {
return stream
.pipeThrough(new PolyfillTextDecoderStream())
.pipeThrough(new TextLineStream());
}
// const decoder = new TextDecoder('utf-8');
// async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
// let buf = '';
// for await (const chunk of stream) {
// const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
// for (let i = 0, len = chunkStr.length; i < len; i++) {
// const char = chunkStr[i];
// if (char === '\n') {
// yield buf;
// buf = '';
// } else {
// buf += char;
// }
// }
// }
// if (buf) {
// yield buf;
// }
// function createTextLineStreamFromStreamSource(stream: ReadableStream<Uint8Array>) {
// return stream
// .pipeThrough(new PolyfillTextDecoderStream())
// .pipeThrough(new TextLineStream());
// }
const decoder = new TextDecoder('utf-8');
async function *createTextLineAsyncGeneratorFromStreamSource(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
let buf = '';
for await (const chunk of stream) {
const chunkStr = decoder.decode(chunk).replaceAll('\r\n', '\n');
for (let i = 0, len = chunkStr.length; i < len; i++) {
const char = chunkStr[i];
if (char === '\n') {
yield buf;
buf = '';
} else {
buf += char;
}
}
}
if (buf) {
yield buf;
}
}
export function readFileByLine(file: string | URL | BunFile) {
if (typeof file === 'string') {
file = Bun.file(file);
@@ -38,7 +38,7 @@ export function readFileByLine(file: string | URL | BunFile) {
file = Bun.file(file);
}
return createTextLineStreamFromStreamSource(file.stream());
return createTextLineAsyncGeneratorFromStreamSource(file.stream());
}
export function createReadlineInterfaceFromResponse(resp: Response) {
@@ -49,7 +49,7 @@ export function createReadlineInterfaceFromResponse(resp: Response) {
throw new Error('Body has already been consumed.');
}
return createTextLineStreamFromStreamSource(resp.body);
return createTextLineAsyncGeneratorFromStreamSource(resp.body);
}
export function fetchRemoteTextByLine(url: string | URL) {