From 585ba28d479ea1db20e80c318055c448cb13391b Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sat, 8 Jun 2024 18:36:43 +0200 Subject: fix(ext/websocket): correctly order messages when sending blobs (#24133) Previously the asynchronous read of the blob would not block sends that are started later. We now do this, but in such a way as to not regress performance in the common case of not using `Blob`. --- tests/unit/websocket_test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests') diff --git a/tests/unit/websocket_test.ts b/tests/unit/websocket_test.ts index 223b13404..74e85052e 100644 --- a/tests/unit/websocket_test.ts +++ b/tests/unit/websocket_test.ts @@ -706,6 +706,31 @@ Deno.test("echo arraybuffer with binaryType arraybuffer", async () => { await promise; }); +Deno.test("echo blob mixed with string", async () => { + const { promise, resolve } = Promise.withResolvers(); + const ws = new WebSocket("ws://localhost:4242"); + ws.binaryType = "arraybuffer"; + const blob = new Blob(["foo"]); + ws.onerror = () => fail(); + ws.onopen = () => { + ws.send(blob); + ws.send("bar"); + }; + const messages: (ArrayBuffer | string)[] = []; + ws.onmessage = (e) => { + messages.push(e.data); + if (messages.length === 2) { + assertEquals(messages[0], new Uint8Array([102, 111, 111]).buffer); + assertEquals(messages[1], "bar"); + ws.close(); + } + }; + ws.onclose = () => { + resolve(); + }; + await promise; +}); + Deno.test("Event Handlers order", async () => { const { promise, resolve } = Promise.withResolvers(); const ws = new WebSocket("ws://localhost:4242"); -- cgit v1.2.3