2023-01-03 10:47:44 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
2022-11-29 13:55:38 +00:00
|
|
|
|
|
|
|
import { createLPS } from "./_common.ts";
|
|
|
|
|
2023-03-29 22:21:00 +00:00
|
|
|
import type {
|
|
|
|
DelimiterDisposition,
|
|
|
|
DelimiterStreamOptions,
|
|
|
|
} from "./delimiter_stream.ts";
|
|
|
|
|
2022-11-29 13:55:38 +00:00
|
|
|
/** Transform a stream into a stream where each chunk is divided by a given delimiter.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* import { TextDelimiterStream } from "https://deno.land/std@$STD_VERSION/streams/text_delimiter_stream.ts";
|
|
|
|
* const res = await fetch("https://example.com");
|
|
|
|
* const parts = res.body!
|
|
|
|
* .pipeThrough(new TextDecoderStream())
|
|
|
|
* .pipeThrough(new TextDelimiterStream("foo"));
|
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
export class TextDelimiterStream extends TransformStream<string, string> {
|
|
|
|
#buf = "";
|
|
|
|
#delimiter: string;
|
|
|
|
#inspectIndex = 0;
|
|
|
|
#matchIndex = 0;
|
|
|
|
#delimLPS: Uint8Array;
|
2023-03-29 22:21:00 +00:00
|
|
|
#disp: DelimiterDisposition;
|
2022-11-29 13:55:38 +00:00
|
|
|
|
2023-03-29 22:21:00 +00:00
|
|
|
constructor(delimiter: string, options?: DelimiterStreamOptions) {
|
2022-11-29 13:55:38 +00:00
|
|
|
super({
|
|
|
|
transform: (chunk, controller) => {
|
|
|
|
this.#handle(chunk, controller);
|
|
|
|
},
|
|
|
|
flush: (controller) => {
|
|
|
|
controller.enqueue(this.#buf);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this.#delimiter = delimiter;
|
|
|
|
this.#delimLPS = createLPS(new TextEncoder().encode(delimiter));
|
2023-03-29 22:21:00 +00:00
|
|
|
this.#disp = options?.disposition ?? "discard";
|
2022-11-29 13:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#handle(
|
|
|
|
chunk: string,
|
|
|
|
controller: TransformStreamDefaultController<string>,
|
|
|
|
) {
|
|
|
|
this.#buf += chunk;
|
|
|
|
let localIndex = 0;
|
|
|
|
while (this.#inspectIndex < this.#buf.length) {
|
|
|
|
if (chunk[localIndex] === this.#delimiter[this.#matchIndex]) {
|
|
|
|
this.#inspectIndex++;
|
|
|
|
localIndex++;
|
|
|
|
this.#matchIndex++;
|
|
|
|
if (this.#matchIndex === this.#delimiter.length) {
|
|
|
|
// Full match
|
2023-03-29 22:21:00 +00:00
|
|
|
const start = this.#inspectIndex - this.#delimiter.length;
|
|
|
|
const end = this.#disp === "suffix" ? this.#inspectIndex : start;
|
|
|
|
const copy = this.#buf.slice(0, end);
|
|
|
|
controller.enqueue(copy);
|
|
|
|
const shift = this.#disp == "prefix" ? start : this.#inspectIndex;
|
|
|
|
this.#buf = this.#buf.slice(shift);
|
|
|
|
this.#inspectIndex = this.#disp == "prefix"
|
|
|
|
? this.#delimiter.length
|
|
|
|
: 0;
|
2022-11-29 13:55:38 +00:00
|
|
|
this.#matchIndex = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.#matchIndex === 0) {
|
|
|
|
this.#inspectIndex++;
|
|
|
|
localIndex++;
|
|
|
|
} else {
|
|
|
|
this.#matchIndex = this.#delimLPS[this.#matchIndex - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|