perf(providers): assemble fragmented responses once per line (#911)

This commit is contained in:
morluto
2026-10-07 17:07:00 +08:00
committed by GitHub
parent e52fe9719f
commit df7151ba30
4 changed files with 135 additions and 2 deletions
+6 -1
View File
@@ -13,11 +13,15 @@ export interface HopperResponseStreamOptions {
/** Incrementally validates Hopper NDJSON response frames. */
export class HopperResponseStream {
#buffer = "";
#fragments: string[] = [];
constructor(readonly options: HopperResponseStreamOptions) {}
push(chunk: string): void {
this.#buffer += chunk;
this.#fragments.push(chunk);
if (!chunk.includes("\n")) return;
this.#buffer += this.#fragments.join("");
this.#fragments = [];
let newline = this.#buffer.indexOf("\n");
while (newline >= 0) {
const line = this.#buffer.slice(0, newline).trim();
@@ -29,6 +33,7 @@ export class HopperResponseStream {
reset(): void {
this.#buffer = "";
this.#fragments = [];
}
#acceptLine(line: string): boolean {