fix(nodes): preserve falsy event payloads

This commit is contained in:
Vincent Koc
2026-06-02 06:30:53 +02:00
parent 55467f0b94
commit 58de2b689f
5 changed files with 52 additions and 6 deletions

View File

@@ -493,8 +493,16 @@ describe("gateway/node-registry", () => {
const frames: string[] = [];
registry.register(makeClient("conn-1", "node-1", frames), {});
const payload = serializeEventPayload({ foo: "bar" });
const nullPayload = serializeEventPayload(null);
const falsePayload = serializeEventPayload(false);
const zeroPayload = serializeEventPayload(0);
const emptyStringPayload = serializeEventPayload("");
expect(registry.sendEventRaw("node-1", "chat", payload)).toBe(true);
expect(registry.sendEventRaw("node-1", "nullish", nullPayload)).toBe(true);
expect(registry.sendEventRaw("node-1", "flag", falsePayload)).toBe(true);
expect(registry.sendEventRaw("node-1", "count", zeroPayload)).toBe(true);
expect(registry.sendEventRaw("node-1", "empty", emptyStringPayload)).toBe(true);
expect(registry.sendEventRaw("missing-node", "chat", payload)).toBe(false);
expect(registry.sendEventRaw("node-1", "heartbeat", null)).toBe(true);
expect(
@@ -514,6 +522,10 @@ describe("gateway/node-registry", () => {
expect(frames).toEqual([
'{"type":"event","event":"chat","payload":{"foo":"bar"}}',
'{"type":"event","event":"nullish","payload":null}',
'{"type":"event","event":"flag","payload":false}',
'{"type":"event","event":"count","payload":0}',
'{"type":"event","event":"empty","payload":""}',
'{"type":"event","event":"heartbeat"}',
]);
});

View File

@@ -88,7 +88,7 @@ export type SerializedEventPayload = {
};
export function serializeEventPayload(payload: unknown): SerializedEventPayload | null {
if (!payload) {
if (payload === undefined) {
return null;
}
const json = JSON.stringify(payload);

View File

@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
import { parseWindowsCodePage } from "../infra/windows-encoding.js";
import { withEnv } from "../test-utils/env.js";
import { decodeCapturedOutputBuffer, sanitizeEnv } from "./invoke.js";
import { buildNodeInvokeResultParams } from "./runner.js";
import { buildNodeEventParams, buildNodeInvokeResultParams } from "./runner.js";
describe("node-host sanitizeEnv", () => {
it("ignores PATH overrides", () => {
@@ -121,3 +121,28 @@ describe("buildNodeInvokeResultParams", () => {
expect(params.payload).toEqual({ reason: "bad" });
});
});
describe("buildNodeEventParams", () => {
it("serializes explicit falsy event payloads", () => {
expect(buildNodeEventParams("node.test", undefined)).toEqual({
event: "node.test",
payloadJSON: null,
});
expect(buildNodeEventParams("node.test", null)).toEqual({
event: "node.test",
payloadJSON: "null",
});
expect(buildNodeEventParams("node.test", false)).toEqual({
event: "node.test",
payloadJSON: "false",
});
expect(buildNodeEventParams("node.test", 0)).toEqual({
event: "node.test",
payloadJSON: "0",
});
expect(buildNodeEventParams("node.test", "")).toEqual({
event: "node.test",
payloadJSON: '""',
});
});
});

View File

@@ -618,12 +618,20 @@ export function buildNodeInvokeResultParams(
return params;
}
export function buildNodeEventParams(
event: string,
payload: unknown,
): { event: string; payloadJSON: string | null } {
const payloadJSON = payload === undefined ? undefined : JSON.stringify(payload);
return {
event,
payloadJSON: typeof payloadJSON === "string" ? payloadJSON : null,
};
}
async function sendNodeEvent(client: GatewayClient, event: string, payload: unknown) {
try {
await client.request("node.event", {
event,
payloadJSON: payload ? JSON.stringify(payload) : null,
});
await client.request("node.event", buildNodeEventParams(event, payload));
} catch {
// ignore: node events are best-effort
}

View File

@@ -28,6 +28,7 @@ import {
} from "./plugin-node-host.js";
export { buildNodeInvokeResultParams };
export { buildNodeEventParams } from "./invoke.js";
type NodeHostRunOptions = {
gatewayHost: string;