Decode non-string websocket client payloads
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
8ba5a22d45
commit
f5e2112bf5
2 files changed
+62
-2
No files matched your search
@@ -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<WsResponseMessage> => {
|
||||
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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user