mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-10-30 16:50:47 +08:00
Properly handle event streams with readline
The stream would otherwise split longer events into multiple chunks which would fail to parse. Signed-off-by: Rūdolfs Ošiņš <rudolfs@osins.org>
This commit is contained in:
@@ -10,6 +10,7 @@ import getPort from "get-port";
|
||||
import lodash from "lodash";
|
||||
import waitOn from "wait-on";
|
||||
import { execa } from "execa";
|
||||
import * as readline from "node:readline/promises";
|
||||
|
||||
import * as Process from "./process.js";
|
||||
import { randomTag } from "@tests/support/support.js";
|
||||
@@ -292,24 +293,25 @@ export class RadiclePeer {
|
||||
resources: [`socket:${this.#socket}`],
|
||||
});
|
||||
|
||||
this.rad(["node", "events"], { cwd: this.#radHome }).stdout?.on(
|
||||
"data",
|
||||
(data: any) => {
|
||||
data
|
||||
.toString()
|
||||
.split("\n")
|
||||
.forEach((data: unknown) => {
|
||||
if (data && typeof data === "string" && data.trim() !== "") {
|
||||
try {
|
||||
const result: NodeEvent = JSON.parse(data);
|
||||
this.#eventRecords.push(result);
|
||||
} catch (e) {
|
||||
console.log("Error parsing event", data);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
const stdout = this.rad(["node", "events"], { cwd: this.#radHome }).stdout;
|
||||
|
||||
if (stdout) {
|
||||
readline
|
||||
.createInterface({
|
||||
input: stdout,
|
||||
terminal: false,
|
||||
})
|
||||
.on("line", line => {
|
||||
try {
|
||||
const result: NodeEvent = JSON.parse(line);
|
||||
this.#eventRecords.push(result);
|
||||
} catch (e) {
|
||||
console.log("Error parsing event", line);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw new Error("Could not get stdout to track events");
|
||||
}
|
||||
}
|
||||
|
||||
public async stopNode() {
|
||||
|
||||
Reference in New Issue
Block a user