Skip to content

Commit

Permalink
fix(webtransport): honor the binaryType attribute
Browse files Browse the repository at this point in the history
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").
  • Loading branch information
darrachequesne committed Aug 1, 2023
1 parent d55c39e commit 8270e00
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Emitter } from "@socket.io/component-emitter";
import { protocol } from "engine.io-parser";
import type { Packet, BinaryType, PacketType, RawData } from "engine.io-parser";
import { CloseDetails, Transport } from "./transport.js";
import { defaultBinaryType } from "./transports/websocket-constructor.js";

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

Expand Down Expand Up @@ -253,7 +254,7 @@ export class Socket extends Emitter<
> {
public id: string;
public transport: Transport;
public binaryType: BinaryType;
public binaryType: BinaryType = defaultBinaryType;
public readyState: SocketState;
public writeBuffer: Packet[] = [];

Expand Down
4 changes: 1 addition & 3 deletions lib/transports/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Transport } from "../transport.js";
import { encode } from "../contrib/parseqs.js";
import { yeast } from "../contrib/yeast.js";
import { pick } from "../util.js";
import {
defaultBinaryType,
nextTick,
usingBrowserWebSocket,
WebSocket,
Expand Down Expand Up @@ -84,7 +82,7 @@ export class WS extends Transport {
return this.emitReserved("error", err);
}

this.ws.binaryType = this.socket.binaryType || defaultBinaryType;
this.ws.binaryType = this.socket.binaryType;

this.addEventListeners();
}
Expand Down
3 changes: 1 addition & 2 deletions lib/transports/webtransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export class WT extends Transport {
this.transport.createBidirectionalStream().then((stream) => {
const decoderStream = createPacketDecoderStream(
Number.MAX_SAFE_INTEGER,
// TODO expose binarytype
"arraybuffer"
this.socket.binaryType
);
const reader = stream.readable.pipeThrough(decoderStream).getReader();

Expand Down
23 changes: 22 additions & 1 deletion test/webtransport.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,14 @@ describe("WebTransport", () => {
});
});

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

socket.binaryType = "arraybuffer";

engine.on("connection", (serverSocket) => {
serverSocket.send(Uint8Array.from([1, 2, 3]));
});
Expand All @@ -278,4 +280,23 @@ describe("WebTransport", () => {
});
});
});

it("should send some binary data (server to client) (as Buffer)", (done) => {
setup({}, ({ engine, h3Server, certificate }) => {
const socket = createSocket(h3Server.port, certificate, {
transports: ["webtransport"],
});

engine.on("connection", (serverSocket) => {
serverSocket.send(Uint8Array.from([1, 2, 3]));
});

socket.on("message", (data) => {
expect(Buffer.isBuffer(data)).to.be(true);
expect(data).to.eql(Uint8Array.of(1, 2, 3));

success(engine, h3Server, done);
});
});
});
});

0 comments on commit 8270e00

Please sign in to comment.