Skip to content

Commit 8270e00

Browse files
fix(webtransport): honor the binaryType attribute
The Node.js client will now properly receive binary data as Buffers instead of ArrayBuffers, when connecting with WebTransport. Browser clients will still receive ArrayBuffers though (or Blobs, if `socket.binaryType` is set to "blob").
1 parent d55c39e commit 8270e00

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

lib/socket.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Emitter } from "@socket.io/component-emitter";
77
import { protocol } from "engine.io-parser";
88
import type { Packet, BinaryType, PacketType, RawData } from "engine.io-parser";
99
import { CloseDetails, Transport } from "./transport.js";
10+
import { defaultBinaryType } from "./transports/websocket-constructor.js";
1011

1112
const debug = debugModule("engine.io-client:socket"); // debug()
1213

@@ -253,7 +254,7 @@ export class Socket extends Emitter<
253254
> {
254255
public id: string;
255256
public transport: Transport;
256-
public binaryType: BinaryType;
257+
public binaryType: BinaryType = defaultBinaryType;
257258
public readyState: SocketState;
258259
public writeBuffer: Packet[] = [];
259260

lib/transports/websocket.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Transport } from "../transport.js";
2-
import { encode } from "../contrib/parseqs.js";
32
import { yeast } from "../contrib/yeast.js";
43
import { pick } from "../util.js";
54
import {
6-
defaultBinaryType,
75
nextTick,
86
usingBrowserWebSocket,
97
WebSocket,
@@ -84,7 +82,7 @@ export class WS extends Transport {
8482
return this.emitReserved("error", err);
8583
}
8684

87-
this.ws.binaryType = this.socket.binaryType || defaultBinaryType;
85+
this.ws.binaryType = this.socket.binaryType;
8886

8987
this.addEventListeners();
9088
}

lib/transports/webtransport.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ export class WT extends Transport {
4343
this.transport.createBidirectionalStream().then((stream) => {
4444
const decoderStream = createPacketDecoderStream(
4545
Number.MAX_SAFE_INTEGER,
46-
// TODO expose binarytype
47-
"arraybuffer"
46+
this.socket.binaryType
4847
);
4948
const reader = stream.readable.pipeThrough(decoderStream).getReader();
5049

test/webtransport.mjs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,14 @@ describe("WebTransport", () => {
260260
});
261261
});
262262

263-
it("should send some binary data (server to client)", (done) => {
263+
it("should send some binary data (server to client) (as ArrayBuffer)", (done) => {
264264
setup({}, ({ engine, h3Server, certificate }) => {
265265
const socket = createSocket(h3Server.port, certificate, {
266266
transports: ["webtransport"],
267267
});
268268

269+
socket.binaryType = "arraybuffer";
270+
269271
engine.on("connection", (serverSocket) => {
270272
serverSocket.send(Uint8Array.from([1, 2, 3]));
271273
});
@@ -278,4 +280,23 @@ describe("WebTransport", () => {
278280
});
279281
});
280282
});
283+
284+
it("should send some binary data (server to client) (as Buffer)", (done) => {
285+
setup({}, ({ engine, h3Server, certificate }) => {
286+
const socket = createSocket(h3Server.port, certificate, {
287+
transports: ["webtransport"],
288+
});
289+
290+
engine.on("connection", (serverSocket) => {
291+
serverSocket.send(Uint8Array.from([1, 2, 3]));
292+
});
293+
294+
socket.on("message", (data) => {
295+
expect(Buffer.isBuffer(data)).to.be(true);
296+
expect(data).to.eql(Uint8Array.of(1, 2, 3));
297+
298+
success(engine, h3Server, done);
299+
});
300+
});
301+
});
281302
});

0 commit comments

Comments
 (0)