Skip to content

Commit 88eee59

Browse files
fix: fix broadcasting volatile packets with binary attachments
The binary attachments of volatile packets were discarded (only the header packet was sent) due to a bug introduced in [1]. Note: the `wsPreEncoded` option is removed by this commit, as I wasn't able to find an elegant (read: non explosive) way to keep it. [1]: 5579d40 Related: socketio/socket.io#3919
1 parent 83cce96 commit 88eee59

File tree

2 files changed

+5
-18
lines changed

2 files changed

+5
-18
lines changed

lib/index.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class Adapter extends EventEmitter {
126126
*/
127127
public broadcast(packet: any, opts: BroadcastOptions): void {
128128
const flags = opts.flags || {};
129-
const basePacketOpts = {
129+
const packetOpts = {
130130
preEncoded: true,
131131
volatile: flags.volatile,
132132
compress: flags.compress
@@ -135,21 +135,8 @@ export class Adapter extends EventEmitter {
135135
packet.nsp = this.nsp.name;
136136
const encodedPackets = this.encoder.encode(packet);
137137

138-
const packetOpts = encodedPackets.map(encodedPacket => {
139-
if (typeof encodedPacket === "string") {
140-
return {
141-
...basePacketOpts,
142-
wsPreEncoded: "4" + encodedPacket // "4" being the "message" packet type in Engine.IO
143-
};
144-
} else {
145-
return basePacketOpts;
146-
}
147-
});
148-
149138
this.apply(opts, socket => {
150-
for (let i = 0; i < encodedPackets.length; i++) {
151-
socket.client.writeToEngine(encodedPackets[i], packetOpts[i]);
152-
}
139+
socket.client.writeToEngine(encodedPackets, packetOpts);
153140
});
154141
}
155142

test/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ describe("socket.io-adapter", () => {
6767
id,
6868
client: {
6969
writeToEngine(payload, opts) {
70-
expect(payload).to.eql("123");
70+
expect(payload).to.eql(["123"]);
7171
expect(opts.preEncoded).to.eql(true);
72-
expect(opts.wsPreEncoded).to.eql("4123");
7372
ids.push(id);
7473
}
7574
}
@@ -107,7 +106,8 @@ describe("socket.io-adapter", () => {
107106
id,
108107
client: {
109108
writeToEngine(payload, opts) {
110-
expect(payload).to.be.a(Buffer);
109+
expect(payload).to.be.a(Array);
110+
expect(payload[0]).to.be.a(Buffer);
111111
expect(opts.preEncoded).to.eql(true);
112112
expect(opts.wsPreEncoded).to.be(undefined);
113113
ids.push(id);

0 commit comments

Comments
 (0)