Skip to content

Commit

Permalink
fix: fix broadcasting volatile packets with binary attachments
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Nov 16, 2021
1 parent 83cce96 commit 88eee59
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
17 changes: 2 additions & 15 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class Adapter extends EventEmitter {
*/
public broadcast(packet: any, opts: BroadcastOptions): void {
const flags = opts.flags || {};
const basePacketOpts = {
const packetOpts = {
preEncoded: true,
volatile: flags.volatile,
compress: flags.compress
Expand All @@ -135,21 +135,8 @@ export class Adapter extends EventEmitter {
packet.nsp = this.nsp.name;
const encodedPackets = this.encoder.encode(packet);

const packetOpts = encodedPackets.map(encodedPacket => {
if (typeof encodedPacket === "string") {
return {
...basePacketOpts,
wsPreEncoded: "4" + encodedPacket // "4" being the "message" packet type in Engine.IO
};
} else {
return basePacketOpts;
}
});

this.apply(opts, socket => {
for (let i = 0; i < encodedPackets.length; i++) {
socket.client.writeToEngine(encodedPackets[i], packetOpts[i]);
}
socket.client.writeToEngine(encodedPackets, packetOpts);
});
}

Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ describe("socket.io-adapter", () => {
id,
client: {
writeToEngine(payload, opts) {
expect(payload).to.eql("123");
expect(payload).to.eql(["123"]);
expect(opts.preEncoded).to.eql(true);
expect(opts.wsPreEncoded).to.eql("4123");
ids.push(id);
}
}
Expand Down Expand Up @@ -107,7 +106,8 @@ describe("socket.io-adapter", () => {
id,
client: {
writeToEngine(payload, opts) {
expect(payload).to.be.a(Buffer);
expect(payload).to.be.a(Array);
expect(payload[0]).to.be.a(Buffer);
expect(opts.preEncoded).to.eql(true);
expect(opts.wsPreEncoded).to.be(undefined);
ids.push(id);
Expand Down

0 comments on commit 88eee59

Please sign in to comment.