TextLineStream

import { TextLineStream } from "jsr:@ende/streams";

class

class TextLineStream extends TransformStream<string, string>

Transform a stream into a stream where each chunk is divided by a newline, be it \n or \r\n. \r can be enabled via the allowCR option.

If you want to split by a custom delimiter, consider using TextDelimiterStream.

Example: JSON Lines

import { TextLineStream } from "@ende/streams/text-line-stream";
import { toTransformStream } from "@ende/streams/to-transform-stream";
import { assertEquals } from "@ende/assert";

const stream = ReadableStream.from([
  '{"name": "Alice", "age": ',
  '30}\n{"name": "Bob", "age"',
  ": 25}\n",
]);

type Person = { name: string; age: number };

// Split the stream by newline and parse each line as a JSON object
const jsonStream = stream.pipeThrough(new TextLineStream())
  .pipeThrough(toTransformStream(async function* (src) {
    for await (const chunk of src) {
      if (chunk.trim().length === 0) {
        continue;
      }
      yield JSON.parse(chunk) as Person;
    }
  }));

assertEquals(
  await Array.fromAsync(jsonStream),
  [{ "name": "Alice", "age": 30 }, { "name": "Bob", "age": 25 }],
);

Example: Allow splitting by \r

import { TextLineStream } from "@ende/streams/text-line-stream";
import { assertEquals } from "@ende/assert";

const stream = ReadableStream.from([
 "CR\rLF",
 "\nCRLF\r\ndone",
]).pipeThrough(new TextLineStream({ allowCR: true }));

const lines = await Array.fromAsync(stream);

assertEquals(lines, ["CR", "LF", "CRLF", "done"]);

Constructors

constructor

new TextLineStream(options: TextLineStreamOptions = [UNSUPPORTED])

Constructs a new instance.

options: TextLineStreamOptions = [UNSUPPORTED]

Options for the stream.

Source