From f5e2112bf54e9d710c4a8e1aa7ffc55a214bf840 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 9 Feb 2026 23:09:45 +0000 Subject: [PATCH] Decode non-string websocket client payloads Co-authored-by: Theo Browne --- apps/t3/src/runtimeApiServer.test.ts | 35 ++++++++++++++++++++++++++++ apps/t3/src/runtimeApiServer.ts | 29 +++++++++++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/apps/t3/src/runtimeApiServer.test.ts b/apps/t3/src/runtimeApiServer.test.ts index db991ce193..16b569f5f1 100644 --- a/apps/t3/src/runtimeApiServer.test.ts +++ b/apps/t3/src/runtimeApiServer.test.ts @@ -129,6 +129,41 @@ describe("runtimeApiServer", () => { client.socket.close(); }); + it("accepts buffer-encoded websocket request payloads", async () => { + const server = await startRuntimeApiServer({ + port: 0, + launchCwd: process.cwd(), + }); + servers.push(server); + + const client = await connectClient(server.wsUrl); + await client.nextMessage(); + + client.socket.send( + Buffer.from( + JSON.stringify({ + type: "request", + id: "buffer-todos-1", + method: "todos.list", + }), + ), + ); + + const response = await withTimeout( + (async (): Promise => { + const message = await client.nextMessage(); + if (message.type === "response" && message.id === "buffer-todos-1") { + return message; + } + return Promise.reject(new Error("Expected matching todos response.")); + })(), + ); + expect(response.ok).toBe(true); + expect(Array.isArray(response.result)).toBe(true); + + client.socket.close(); + }); + it("replaces an existing websocket client with a new one", async () => { const server = await startRuntimeApiServer({ port: 0, diff --git a/apps/t3/src/runtimeApiServer.ts b/apps/t3/src/runtimeApiServer.ts index 779bd444aa..66a6b0f7c7 100644 --- a/apps/t3/src/runtimeApiServer.ts +++ b/apps/t3/src/runtimeApiServer.ts @@ -2,7 +2,7 @@ import { spawn } from "node:child_process"; import os from "node:os"; import path from "node:path"; import { z } from "zod"; -import { WebSocketServer, type WebSocket } from "ws"; +import { WebSocketServer, type RawData, type WebSocket } from "ws"; import { EDITORS, @@ -121,6 +121,26 @@ function sendMessage(socket: WebSocket, message: unknown): void { socket.send(JSON.stringify(message)); } +function decodeClientMessage(raw: RawData): string | null { + if (typeof raw === "string") { + return raw; + } + + if (raw instanceof Buffer) { + return raw.toString("utf8"); + } + + if (raw instanceof ArrayBuffer) { + return Buffer.from(raw).toString("utf8"); + } + + if (Array.isArray(raw)) { + return Buffer.concat(raw).toString("utf8"); + } + + return null; +} + function openPathInFileManager(targetPath: string): void { const command = process.platform === "win32" ? "explorer" : process.platform === "darwin" ? "open" : "xdg-open"; @@ -523,9 +543,14 @@ export async function startRuntimeApiServer( }); socket.on("message", async (raw) => { + const decoded = decodeClientMessage(raw); + if (!decoded) { + return; + } + const maybeParsed = (() => { try { - return JSON.parse(raw.toString()) as unknown; + return JSON.parse(decoded) as unknown; } catch { return null; }