perf(providers): assemble fragmented responses once per line (#911)
This commit is contained in:
@@ -3,6 +3,55 @@ import { describe, expect, it } from "vitest";
|
||||
import { GhidraResponseBuffer } from "./GhidraResponseBuffer.js";
|
||||
|
||||
describe("Ghidra response buffer", () => {
|
||||
it("preserves fragmented lines, whitespace and a trailing partial response", () => {
|
||||
const lines: string[] = [];
|
||||
const buffer = new GhidraResponseBuffer({
|
||||
onLine: (line) => lines.push(line),
|
||||
});
|
||||
buffer.push(' {"result":"雪');
|
||||
buffer.push('🦊"}\r');
|
||||
buffer.push('\n\n {"result":2}\n{"res');
|
||||
expect(lines).toEqual(['{"result":"雪🦊"}', '{"result":2}']);
|
||||
buffer.push('ult":3}');
|
||||
expect(lines).toHaveLength(2);
|
||||
buffer.push("\n");
|
||||
expect(lines).toEqual([
|
||||
'{"result":"雪🦊"}',
|
||||
'{"result":2}',
|
||||
'{"result":3}',
|
||||
]);
|
||||
});
|
||||
|
||||
it("drops both pending fragments and remaining lines when reset during delivery", () => {
|
||||
const lines: string[] = [];
|
||||
const buffer = new GhidraResponseBuffer({
|
||||
onLine: (line) => {
|
||||
lines.push(line);
|
||||
buffer.reset();
|
||||
},
|
||||
});
|
||||
buffer.push("discarded partial");
|
||||
buffer.reset();
|
||||
buffer.push("first\nsecond\npartial");
|
||||
buffer.push("discarded");
|
||||
buffer.reset();
|
||||
buffer.push("next\n");
|
||||
expect(lines).toEqual(["first", "next"]);
|
||||
});
|
||||
|
||||
it("accepts a large response arriving in many small socket fragments", () => {
|
||||
const lines: string[] = [];
|
||||
const buffer = new GhidraResponseBuffer({
|
||||
onLine: (line) => lines.push(line),
|
||||
});
|
||||
const response = `{"result":"${"x".repeat(8 * 1024 * 1024)}"}`;
|
||||
for (let offset = 0; offset < response.length; offset += 4096)
|
||||
buffer.push(response.slice(offset, offset + 4096));
|
||||
expect(lines).toEqual([]);
|
||||
buffer.push("\n");
|
||||
expect(lines).toEqual([response]);
|
||||
});
|
||||
|
||||
it("accepts complete responses larger than the former 64 MiB ceiling", () => {
|
||||
const lines: string[] = [];
|
||||
const buffer = new GhidraResponseBuffer({
|
||||
|
||||
@@ -7,6 +7,7 @@ export interface GhidraResponseBufferOptions {
|
||||
export class GhidraResponseBuffer {
|
||||
readonly #options: GhidraResponseBufferOptions;
|
||||
#buffer = "";
|
||||
#fragments: string[] = [];
|
||||
|
||||
constructor(options: GhidraResponseBufferOptions) {
|
||||
this.#options = options;
|
||||
@@ -14,7 +15,10 @@ export class GhidraResponseBuffer {
|
||||
|
||||
/** Consume one decoded socket chunk. */
|
||||
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 encodedLine = this.#buffer.slice(0, newline);
|
||||
@@ -28,5 +32,6 @@ export class GhidraResponseBuffer {
|
||||
/** Drop any incomplete line when a session closes. */
|
||||
reset(): void {
|
||||
this.#buffer = "";
|
||||
this.#fragments = [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user