Skip to content

Commit

Permalink
fix: restore compatibility with binary parsers
Browse files Browse the repository at this point in the history
Related: 5579d40
  • Loading branch information
darrachequesne committed May 19, 2021
1 parent 36d0acf commit a33e42b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
21 changes: 13 additions & 8 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 packetOpts = {
const basePacketOpts = {
preEncoded: true,
volatile: flags.volatile,
compress: flags.compress
Expand All @@ -135,15 +135,20 @@ export class Adapter extends EventEmitter {
packet.nsp = this.nsp.name;
const encodedPackets = this.encoder.encode(packet);

const firstPacketOpts = {
wsPreEncoded: "4" + encodedPackets[0], // "4" being the "message" packet type in Engine.IO
...packetOpts
};
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 => {
socket.client.writeToEngine(encodedPackets[0], firstPacketOpts);
for (let i = 1; i < encodedPackets.length; i++) {
socket.client.writeToEngine(encodedPackets[i], packetOpts);
for (let i = 0; i < encodedPackets.length; i++) {
socket.client.writeToEngine(encodedPackets[i], packetOpts[i]);
}
});
}
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ describe("socket.io-adapter", () => {
{
id,
client: {
writeToEngine() {
writeToEngine(payload, opts) {
expect(payload).to.eql("123");
expect(opts.preEncoded).to.eql(true);
expect(opts.wsPreEncoded).to.eql("4123");
ids.push(id);
}
}
Expand All @@ -77,7 +80,7 @@ describe("socket.io-adapter", () => {
server: {
encoder: {
encode() {
return [];
return ["123"];
}
}
},
Expand All @@ -103,7 +106,10 @@ describe("socket.io-adapter", () => {
{
id,
client: {
writeToEngine() {
writeToEngine(payload, opts) {
expect(payload).to.be.a(Buffer);
expect(opts.preEncoded).to.eql(true);
expect(opts.wsPreEncoded).to.be(undefined);
ids.push(id);
}
}
Expand All @@ -114,7 +120,7 @@ describe("socket.io-adapter", () => {
server: {
encoder: {
encode() {
return [];
return [Buffer.from([1, 2, 3])];
}
}
},
Expand Down

0 comments on commit a33e42b

Please sign in to comment.