From befba0e56a90bce02da29150ab7c31db6f3a81ba Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Mon, 15 Apr 2024 20:20:05 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20handle=20messages=20sent=20in=20?= =?UTF-8?q?multiple=20parts=20over=20stdin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- binary/src/messenger.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/binary/src/messenger.ts b/binary/src/messenger.ts index f8f02a7f9..1e2d3a745 100644 --- a/binary/src/messenger.ts +++ b/binary/src/messenger.ts @@ -80,14 +80,31 @@ export class IpcMessenger { // Call handler which is waiting for the response, nothing to return this.idListeners.get(msg.messageId)?.(msg); } catch (e) { - console.error("Error parsing line: ", line, e); + let truncatedLine = line; + if (line.length > 200) { + truncatedLine = + line.substring(0, 100) + "..." + line.substring(line.length - 100); + } + console.error("Error parsing line: ", truncatedLine, e); return; } } + private _unfinishedLine: string | undefined = undefined; private _handleData(data: Buffer) { const d = data.toString(); - const lines = d.split(/\r\n|\r|\n/).filter((line) => line.trim() !== ""); + const lines = d.split(/\r\n/).filter((line) => line.trim() !== ""); + if (lines.length === 0) { + return; + } + + if (this._unfinishedLine) { + lines[0] = this._unfinishedLine + lines[0]; + this._unfinishedLine = undefined; + } + if (!d.endsWith("\r\n")) { + this._unfinishedLine = lines.pop(); + } lines.forEach((line) => this._handleLine(line)); }