Skip to content

Commit

Permalink
fix: ensure compatibility with previous versions of the adapter
Browse files Browse the repository at this point in the history
Using `socket.io@4.1.0` with `socket.io-adapter@2.2.0` would lead to
the following error:

> Uncaught Error: unknown packet type NaN

Because the packet would be encoded twice, resulting in "undefined".

See also:

- socketio/socket.io-adapter@5579d40
- dc381b7

Related:

- #3922
- #3927
  • Loading branch information
darrachequesne committed May 17, 2021
1 parent 995f38f commit a2cf248
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const debug = debugModule("socket.io:client");
interface WriteOptions {
compress?: boolean;
volatile?: boolean;
preEncoded?: boolean;
wsPreEncoded?: string;
}

Expand Down Expand Up @@ -200,12 +201,14 @@ export class Client<
* @param {Object} opts
* @private
*/
_packet(packet: Packet, opts: WriteOptions = {}): void {
_packet(packet: Packet | any[], opts: WriteOptions = {}): void {
if (this.conn.readyState !== "open") {
debug("ignoring packet write %j", packet);
return;
}
const encodedPackets = this.encoder.encode(packet);
const encodedPackets = opts.preEncoded
? (packet as any[]) // previous versions of the adapter incorrectly used socket.packet() instead of writeToEngine()
: this.encoder.encode(packet as Packet);
for (const encodedPacket of encodedPackets) {
this.writeToEngine(encodedPacket, opts);
}
Expand Down

0 comments on commit a2cf248

Please sign in to comment.