Skip to content

Commit e90bf31

Browse files
fix(discord): keep gateway close reasons UTF-16 safe (#102246)
* fix(discord): keep gateway close reasons UTF-16 safe * test(discord): assert exact close reason truncation --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 1017ea5 commit e90bf31

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

extensions/discord/src/monitor/gateway-plugin.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,35 @@ describe("createDiscordGatewayPlugin", () => {
355355
expect(logs).toContain("lastErrorCode=WS_ERR_TOO_MANY_BUFFERED_PARTS");
356356
expect(logs).toContain("hint=possible ws receiver buffered-parts limit");
357357
});
358+
359+
it("keeps gateway close reason logs UTF-16 safe", () => {
360+
const socket = new EventEmitter() as EventEmitter & { binaryType?: string };
361+
const runtime = {
362+
log: vi.fn(),
363+
error: vi.fn(),
364+
exit: vi.fn(),
365+
};
366+
const plugin = createPlugin(
367+
{
368+
webSocketCtor: function WebSocketCtor() {
369+
return socket;
370+
} as unknown as NonNullable<
371+
Parameters<typeof createDiscordGatewayPlugin>[0]["testing"]
372+
>["webSocketCtor"],
373+
},
374+
{},
375+
runtime,
376+
);
377+
const createdSocket = (
378+
plugin as unknown as { createWebSocket: (url: string) => typeof socket }
379+
).createWebSocket("wss://gateway.discord.gg");
380+
381+
createdSocket.emit("close", 1008, Buffer.from(`${"A".repeat(239)}🧪 tail`));
382+
383+
const log = String(runtime.log.mock.calls.at(-1)?.[0]);
384+
expect(log).toContain("discord: gateway websocket closed");
385+
expect(log).toContain("code=1008");
386+
expect(log).toContain(`reason=${"A".repeat(239)}...`);
387+
expect(log).toContain("hint=possible ws receiver buffered-parts limit");
388+
});
358389
});

extensions/discord/src/monitor/gateway-plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from "openclaw/plugin-sdk/proxy-capture";
1212
import { danger, warn } from "openclaw/plugin-sdk/runtime-env";
1313
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
14+
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
1415
import * as ws from "ws";
1516
import * as discordGateway from "../internal/gateway.js";
1617
import { createDiscordDnsLookup } from "../network-config.js";
@@ -110,7 +111,7 @@ function formatDiscordGatewayCloseReason(reason: Buffer): string {
110111
if (text.length <= DISCORD_GATEWAY_CLOSE_REASON_LOG_MAX_CHARS) {
111112
return text;
112113
}
113-
return `${text.slice(0, DISCORD_GATEWAY_CLOSE_REASON_LOG_MAX_CHARS)}...`;
114+
return `${truncateUtf16Safe(text, DISCORD_GATEWAY_CLOSE_REASON_LOG_MAX_CHARS)}...`;
114115
}
115116

116117
function formatDiscordGatewayTransportErrorLog(params: {

0 commit comments

Comments
 (0)