import { BytesList } from "../bytes/bytes_list.ts";import { createLPS } from "./_common.ts";
export type DelimiterDisposition = | "suffix" | "prefix" | "discard" ;
export interface DelimiterStreamOptions { disposition?: DelimiterDisposition;}
export class DelimiterStream extends TransformStream<Uint8Array, Uint8Array> { #bufs = new BytesList(); #delimiter: Uint8Array; #inspectIndex = 0; #matchIndex = 0; #delimLen: number; #delimLPS: Uint8Array; #disp?: DelimiterDisposition;
constructor( delimiter: Uint8Array, options?: DelimiterStreamOptions, ) { super({ transform: (chunk, controller) => { this.#handle(chunk, controller); }, flush: (controller) => { controller.enqueue(this.#bufs.concat()); }, });
this.#delimiter = delimiter; this.#delimLen = delimiter.length; this.#delimLPS = createLPS(delimiter); this.#disp = options?.disposition ?? "discard"; }
#handle( chunk: Uint8Array, controller: TransformStreamDefaultController<Uint8Array>, ) { this.#bufs.add(chunk); let localIndex = 0; while (this.#inspectIndex < this.#bufs.size()) { if (chunk[localIndex] === this.#delimiter[this.#matchIndex]) { this.#inspectIndex++; localIndex++; this.#matchIndex++; if (this.#matchIndex === this.#delimLen) { const start = this.#inspectIndex - this.#delimLen; const end = this.#disp == "suffix" ? this.#inspectIndex : start; const copy = this.#bufs.slice(0, end); controller.enqueue(copy); const shift = this.#disp == "prefix" ? start : this.#inspectIndex; this.#bufs.shift(shift); this.#inspectIndex = this.#disp == "prefix" ? this.#delimLen : 0; this.#matchIndex = 0; } } else { if (this.#matchIndex === 0) { this.#inspectIndex++; localIndex++; } else { this.#matchIndex = this.#delimLPS[this.#matchIndex - 1]; } } } }}