Skip to content

Commit 027698c

Browse files
authored
Ensure the correct data types are sent in WebSocket messages from the client to the Worker (#8083)
* Convert message data type in websocket proxy * Updated websockets playground tests to check data types * Added changeset
1 parent bff209d commit 027698c

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

.changeset/silver-bats-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Ensure the correct data types are sent in WebSocket messages from the client to the Worker

packages/vite-plugin-cloudflare/playground/websockets/__tests__/websockets.spec.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,30 @@ test("closes WebSocket connection", async () => {
2626
});
2727
});
2828

29-
test("sends and receives WebSocket messages", async () => {
29+
test("sends and receives WebSocket string messages", async () => {
3030
await openWebSocket();
31-
const sendButton = page.getByRole("button", { name: "Send message" });
31+
const sendButton = page.getByRole("button", { name: "Send string" });
3232
const messageTextBefore = await page.textContent("p");
3333
expect(messageTextBefore).toBe("");
3434
await sendButton.click();
3535
await vi.waitFor(async () => {
3636
const messageTextAfter = await page.textContent("p");
3737
expect(messageTextAfter).toBe(
38-
`Durable Object received client message: 'Client event'.`
38+
`Durable Object received client message: 'Client event' of type 'string'.`
39+
);
40+
});
41+
});
42+
43+
test("sends and receives WebSocket ArrayBuffer messages", async () => {
44+
await openWebSocket();
45+
const sendButton = page.getByRole("button", { name: "Send ArrayBuffer" });
46+
const messageTextBefore = await page.textContent("p");
47+
expect(messageTextBefore).toBe("");
48+
await sendButton.click();
49+
await vi.waitFor(async () => {
50+
const messageTextAfter = await page.textContent("p");
51+
expect(messageTextAfter).toBe(
52+
`Durable Object received client message: '[object ArrayBuffer]' of type 'object'.`
3953
);
4054
});
4155
});

packages/vite-plugin-cloudflare/playground/websockets/src/index.html

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
<h1>WebSockets playground</h1>
1010
<button id="open" aria-label="Open WebSocket">Open</button>
1111
<button id="close" aria-label="Close WebSocket">Close</button>
12-
<button id="send" aria-label="Send message">Send</button>
12+
<button id="send-string" aria-label="Send string">Send string</button>
13+
<button id="send-array-buffer" aria-label="Send ArrayBuffer">
14+
Send ArrayBuffer
15+
</button>
1316
<h2 id="status">WebSocket closed</h2>
1417
<p id="message"></p>
1518
</main>
@@ -18,13 +21,17 @@ <h2 id="status">WebSocket closed</h2>
1821
let ws;
1922
const openButton = document.querySelector("#open");
2023
const closeButton = document.querySelector("#close");
21-
const sendButton = document.querySelector("#send");
24+
const sendStringButton = document.querySelector("#send-string");
25+
const sendArrayBufferButton = document.querySelector("#send-array-buffer");
2226
const statusText = document.querySelector("#status");
2327
const messageText = document.querySelector("#message");
2428

2529
openButton.addEventListener("click", () => open());
2630
closeButton.addEventListener("click", () => close());
27-
sendButton.addEventListener("click", () => send("Client event"));
31+
sendStringButton.addEventListener("click", () => send("Client event"));
32+
sendArrayBufferButton.addEventListener("click", () =>
33+
send(new ArrayBuffer(10))
34+
);
2835

2936
function open() {
3037
if (ws) {

packages/vite-plugin-cloudflare/playground/websockets/src/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ export class WebSocketServer extends DurableObject {
1515
}
1616

1717
override async webSocketMessage(ws: WebSocket, data: string | ArrayBuffer) {
18-
const decoder = new TextDecoder();
19-
const message = typeof data === "string" ? data : decoder.decode(data);
20-
21-
ws.send(`Durable Object received client message: '${message}'.`);
18+
ws.send(
19+
`Durable Object received client message: '${data}' of type '${typeof data}'.`
20+
);
2221
}
2322
}
2423

packages/vite-plugin-cloudflare/src/websockets.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ export function handleWebSocket(
6161
});
6262

6363
// Forward client events to Worker
64-
clientWebSocket.on("message", (event: ArrayBuffer | string) => {
65-
workerWebSocket.send(event);
64+
clientWebSocket.on("message", (data, isBinary) => {
65+
workerWebSocket.send(
66+
isBinary
67+
? Array.isArray(data)
68+
? Buffer.concat(data)
69+
: data
70+
: data.toString()
71+
);
6672
});
6773
clientWebSocket.on("error", (error) => {
6874
logger.error(`WebSocket error:\n${error.stack || error.message}`, {

0 commit comments

Comments
 (0)