DelimiterStream

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

class

class DelimiterStream extends TransformStream<Uint8Array, Uint8Array>

Divide a stream into chunks delimited by a given byte sequence.

If you are working with a stream of string, consider using TextDelimiterStream.

Example: Divide a CSV stream by commas, discarding the commas:

import { DelimiterStream } from "@ende/streams/delimiter-stream";
import { assertEquals } from "@ende/assert";

const inputStream = ReadableStream.from(["foo,bar", ",baz"]);

const transformed = inputStream.pipeThrough(new TextEncoderStream())
  .pipeThrough(new DelimiterStream(new TextEncoder().encode(",")))
  .pipeThrough(new TextDecoderStream());

assertEquals(await Array.fromAsync(transformed), ["foo", "bar", "baz"]);

Example: Divide a stream after semi-colons, keeping the semicolons in the output:

import { DelimiterStream } from "@ende/streams/delimiter-stream";
import { assertEquals } from "@ende/assert";

const inputStream = ReadableStream.from(["foo;", "bar;baz", ";"]);

const transformed = inputStream.pipeThrough(new TextEncoderStream())
  .pipeThrough(
    new DelimiterStream(new TextEncoder().encode(";"), {
      disposition: "suffix",
    }),
  ).pipeThrough(new TextDecoderStream());

assertEquals(await Array.fromAsync(transformed), ["foo;", "bar;", "baz;"]);

Constructors

constructor

new DelimiterStream(delimiter: Uint8Array, options: DelimiterStreamOptions = [UNSUPPORTED])

Constructs a new instance.

delimiter: Uint8Array

A delimiter to split the stream by.

options: DelimiterStreamOptions = [UNSUPPORTED]

Options for the delimiter stream.

Source