Skip to content

Commit ae8dd88

Browse files
fix: do not modify the input packet upon encoding
Note: this issue has existed since Socket.IO v1.0 (see [1]), because the `deconstructPacket()` method also mutates its input argument. This also explains why some adapters (like [2]) need to use `process.nextTick()` when extending the `broadcast()` method, because `Adapter.broadcast()` calls `Encoder.encode()` ([3]). Related: - socketio/socket.io#4374 - socketio/socket.io-mongo-adapter#10 [1]: 299849b [2]: https://github.com/socketio/socket.io-postgres-adapter/blob/0.3.0/lib/index.ts#L587-L590 [3]: https://github.com/socketio/socket.io-adapter/blob/2.4.0/lib/index.ts#L148
1 parent 9143aa4 commit ae8dd88

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

lib/binary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function _deconstructPacket(data, buffers) {
5353

5454
export function reconstructPacket(packet, buffers) {
5555
packet.data = _reconstructPacket(packet.data, buffers);
56-
packet.attachments = undefined; // no longer useful
56+
delete packet.attachments; // no longer useful
5757
return packet;
5858
}
5959

lib/index.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ export class Encoder {
5353

5454
if (obj.type === PacketType.EVENT || obj.type === PacketType.ACK) {
5555
if (hasBinary(obj)) {
56-
obj.type =
57-
obj.type === PacketType.EVENT
58-
? PacketType.BINARY_EVENT
59-
: PacketType.BINARY_ACK;
60-
return this.encodeAsBinary(obj);
56+
return this.encodeAsBinary({
57+
type:
58+
obj.type === PacketType.EVENT
59+
? PacketType.BINARY_EVENT
60+
: PacketType.BINARY_ACK,
61+
nsp: obj.nsp,
62+
data: obj.data,
63+
id: obj.id,
64+
});
6165
}
6266
}
6367
return [this.encodeAsString(obj)];
@@ -149,10 +153,9 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
149153
throw new Error("got plaintext data when reconstructing a packet");
150154
}
151155
packet = this.decodeString(obj);
152-
if (
153-
packet.type === PacketType.BINARY_EVENT ||
154-
packet.type === PacketType.BINARY_ACK
155-
) {
156+
const isBinaryEvent = packet.type === PacketType.BINARY_EVENT;
157+
if (isBinaryEvent || packet.type === PacketType.BINARY_ACK) {
158+
packet.type = isBinaryEvent ? PacketType.EVENT : PacketType.ACK;
156159
// binary packet's json
157160
this.reconstructor = new BinaryReconstructor(packet);
158161

test/arraybuffer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,20 @@ describe("ArrayBuffer", () => {
8989
decoder.destroy(); // destroy before all data added
9090
expect(decoder.reconstructor.buffers.length).to.be(0); // expect that buffer is clean
9191
});
92+
93+
it("should not modify the input packet", () => {
94+
const packet = {
95+
type: PacketType.EVENT,
96+
nsp: "/",
97+
data: ["a", Uint8Array.of(1, 2, 3), Uint8Array.of(4, 5, 6)],
98+
};
99+
100+
encoder.encode(packet);
101+
102+
expect(packet).to.eql({
103+
type: PacketType.EVENT,
104+
nsp: "/",
105+
data: ["a", Uint8Array.of(1, 2, 3), Uint8Array.of(4, 5, 6)],
106+
});
107+
});
92108
});

test/helpers.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ module.exports.test = (obj) => {
2020
// tests encoding of binary packets
2121
module.exports.test_bin = (obj) => {
2222
return new Promise((resolve) => {
23-
const originalData = obj.data;
2423
const encodedPackets = encoder.encode(obj);
2524

2625
const decoder = new parser.Decoder();
2726
decoder.on("decoded", (packet) => {
28-
obj.data = originalData;
29-
obj.attachments = undefined;
3027
expect(obj).to.eql(packet);
3128
resolve();
3229
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy