export interface TextLineStreamOptions { allowCR?: boolean;}
export class TextLineStream extends TransformStream<string, string> { #currentLine = "";
constructor(options: TextLineStreamOptions = { allowCR: false }) { super({ transform: (chars, controller) => { chars = this.#currentLine + chars;
while (true) { const lfIndex = chars.indexOf("\n"); const crIndex = options.allowCR ? chars.indexOf("\r") : -1;
if ( crIndex !== -1 && crIndex !== (chars.length - 1) && (lfIndex === -1 || (lfIndex - 1) > crIndex) ) { controller.enqueue(chars.slice(0, crIndex)); chars = chars.slice(crIndex + 1); continue; }
if (lfIndex === -1) break;
const endIndex = chars[lfIndex - 1] === "\r" ? lfIndex - 1 : lfIndex; controller.enqueue(chars.slice(0, endIndex)); chars = chars.slice(lfIndex + 1); }
this.#currentLine = chars; }, flush: (controller) => { if (this.#currentLine === "") return; const currentLine = options.allowCR && this.#currentLine.endsWith("\r") ? this.#currentLine.slice(0, -1) : this.#currentLine; controller.enqueue(currentLine); }, }); }}