🐛 handle messages sent in multiple parts over stdin

This commit is contained in:
Nate Sesti 2024-04-15 20:20:05 -07:00
parent e9afe48db6
commit befba0e56a

View file

@ -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));
}