fix: ensure compatibility with previous versions of the adapter

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:

- 5579d40c24
- dc381b72c6

Related:

- https://github.com/socketio/socket.io/issues/3922
- https://github.com/socketio/socket.io/issues/3927
This commit is contained in:
Damien Arrachequesne
2021-05-17 22:36:27 +02:00
parent 995f38f4cc
commit a2cf2486c3

View File

@@ -13,6 +13,7 @@ const debug = debugModule("socket.io:client");
interface WriteOptions {
compress?: boolean;
volatile?: boolean;
preEncoded?: boolean;
wsPreEncoded?: string;
}
@@ -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);
}