mirror of
https://github.com/socketio/socket.io.git
synced 2026-01-10 07:28:06 -05:00
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:
@@ -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[] = [];
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user