Skip to content

Commit

Permalink
feat(stream): improve SplitStringStream handling incomplete chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
yume-chan committed Mar 2, 2025
1 parent 0f29501 commit a335c14
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions libraries/stream-extra/src/split-string.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import { TransformStream } from "./stream.js";

function* split(
input: string,
separator: string,
): Generator<string, void, void> {
let start = 0;

while (true) {
const index = input.indexOf(separator, start);
if (index === -1) {
return;
}

const part = input.substring(start, index);
yield part;

start = index + 1;
}
}

export class SplitStringStream extends TransformStream<string, string> {
constructor(separator: string) {
let remaining: string | undefined = undefined;

super({
transform(chunk, controller) {
for (const part of split(chunk, separator)) {
controller.enqueue(part);
if (remaining) {
chunk = remaining + chunk;
remaining = undefined;
}

let start = 0;
while (start < chunk.length) {
const index = chunk.indexOf(separator, start);
if (index === -1) {
remaining = chunk.substring(start);
break;
}

controller.enqueue(chunk.substring(start, index));
start = index + 1;
}
},
flush(controller) {
if (remaining) {
controller.enqueue(remaining);
}
},
});
Expand Down

0 comments on commit a335c14

Please sign in to comment.