From ead75ea7afb217fd0868d1837bbf77ed8d0a0a6a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 6 Sep 2025 11:50:39 +0000 Subject: [PATCH 1/2] Initial plan From cf93a2c43fb54b5cfc7a8c76ccaf601cf992cbb2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 6 Sep 2025 11:56:41 +0000 Subject: [PATCH 2/2] Fix NodeJsMessageStream to properly handle long messages spanning multiple chunks Co-authored-by: hediet <2931520+hediet@users.noreply.github.com> --- json-rpc-node/src/index.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/json-rpc-node/src/index.ts b/json-rpc-node/src/index.ts index e946a6d..73892d2 100644 --- a/json-rpc-node/src/index.ts +++ b/json-rpc-node/src/index.ts @@ -22,6 +22,8 @@ export class NodeJsMessageStream extends BaseMessageStream { return new NodeJsMessageStream(process.stdin!, process.stdout!); } + private buffer: string = ""; + constructor( private readonly _writeStream: NodeJS.WritableStream, private readonly _readStream: NodeJS.ReadableStream, @@ -32,10 +34,22 @@ export class NodeJsMessageStream extends BaseMessageStream { this._readStream.on("data", (chunk: any) => { const str = chunk.toString("utf8"); - const parts = str.trim().split("\n"); // todo improve - for (const p of parts) { - const obj = JSON.parse(p) as Message; - this.onMessage(obj); + this.buffer += str; + + // Process complete messages (terminated by newlines) + let newlineIndex: number; + while ((newlineIndex = this.buffer.indexOf("\n")) !== -1) { + const messageStr = this.buffer.substring(0, newlineIndex).trim(); + this.buffer = this.buffer.substring(newlineIndex + 1); + + if (messageStr.length > 0) { + try { + const obj = JSON.parse(messageStr) as Message; + this.onMessage(obj); + } catch (error) { + console.error(`Failed to parse JSON message: ${messageStr}`, error); + } + } } });