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").
This commit is contained in:
Damien Arrachequesne
2023-08-02 01:32:38 +02:00
parent d55c39e0ed
commit 8270e00d5b
4 changed files with 26 additions and 7 deletions

View File

@@ -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()
@@ -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[] = [];

View File

@@ -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,
@@ -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();
}

View File

@@ -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();

View File

@@ -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]));
});
@@ -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);
});
});
});
});