Skip to content

Commit 22c42e3

Browse files
committedJan 19, 2023
fix: calling destroy() should clear all internal state
If a client was in the process of receiving some binary attachments when the connection was abruptly closed, then the manager would call `decoder.destroy()` ([1]) but was then stuck in a "parse error" loop upon reconnection (since it expected a binary attachment and not a CONNECT packet). [1]: https://github.com/socketio/socket.io-client/blob/a1c528b089773d7810a03befaeb982f7e01c3e11/lib/manager.ts#L520
1 parent ae8dd88 commit 22c42e3

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed
 

‎lib/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
288288
public destroy() {
289289
if (this.reconstructor) {
290290
this.reconstructor.finishedReconstruction();
291+
this.reconstructor = null;
291292
}
292293
}
293294
}

‎test/arraybuffer.js

-21
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,6 @@ describe("ArrayBuffer", () => {
6969
return helpers.test_bin(packet);
7070
});
7171

72-
it("cleans itself up on close", () => {
73-
const packet = {
74-
type: PacketType.EVENT,
75-
data: ["a", new ArrayBuffer(2), new ArrayBuffer(3)],
76-
id: 0,
77-
nsp: "/",
78-
};
79-
80-
const encodedPackets = encoder.encode(packet);
81-
82-
const decoder = new Decoder();
83-
decoder.on("decoded", (packet) => {
84-
throw new Error("received a packet when not all binary data was sent.");
85-
});
86-
87-
decoder.add(encodedPackets[0]); // add metadata
88-
decoder.add(encodedPackets[1]); // add first attachment
89-
decoder.destroy(); // destroy before all data added
90-
expect(decoder.reconstructor.buffers.length).to.be(0); // expect that buffer is clean
91-
});
92-
9372
it("should not modify the input packet", () => {
9473
const packet = {
9574
type: PacketType.EVENT,

‎test/parser.js

+15
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,19 @@ describe("socket.io-parser", () => {
127127
/^Unknown type: 999$/
128128
);
129129
});
130+
131+
it("should resume decoding after calling destroy()", () => {
132+
return new Promise((resolve) => {
133+
const decoder = new Decoder();
134+
135+
decoder.on("decoded", (packet) => {
136+
expect(packet.data).to.eql(["hello"]);
137+
resolve();
138+
});
139+
140+
decoder.add('51-["hello"]');
141+
decoder.destroy();
142+
decoder.add('2["hello"]');
143+
});
144+
});
130145
});

0 commit comments

Comments
 (0)