From 22c42e3545e4adbc5931276c378f5d62c8b3854a Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Thu, 19 Jan 2023 09:13:04 +0100 Subject: [PATCH] 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 --- lib/index.ts | 1 + test/arraybuffer.js | 21 --------------------- test/parser.js | 15 +++++++++++++++ 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index b3035d3..6b77094 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -288,6 +288,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> { public destroy() { if (this.reconstructor) { this.reconstructor.finishedReconstruction(); + this.reconstructor = null; } } } diff --git a/test/arraybuffer.js b/test/arraybuffer.js index 7103c80..c3db78b 100644 --- a/test/arraybuffer.js +++ b/test/arraybuffer.js @@ -69,27 +69,6 @@ describe("ArrayBuffer", () => { return helpers.test_bin(packet); }); - it("cleans itself up on close", () => { - const packet = { - type: PacketType.EVENT, - data: ["a", new ArrayBuffer(2), new ArrayBuffer(3)], - id: 0, - nsp: "/", - }; - - const encodedPackets = encoder.encode(packet); - - const decoder = new Decoder(); - decoder.on("decoded", (packet) => { - throw new Error("received a packet when not all binary data was sent."); - }); - - decoder.add(encodedPackets[0]); // add metadata - decoder.add(encodedPackets[1]); // add first attachment - decoder.destroy(); // destroy before all data added - expect(decoder.reconstructor.buffers.length).to.be(0); // expect that buffer is clean - }); - it("should not modify the input packet", () => { const packet = { type: PacketType.EVENT, diff --git a/test/parser.js b/test/parser.js index 47836db..1b99166 100644 --- a/test/parser.js +++ b/test/parser.js @@ -127,4 +127,19 @@ describe("socket.io-parser", () => { /^Unknown type: 999$/ ); }); + + it("should resume decoding after calling destroy()", () => { + return new Promise((resolve) => { + const decoder = new Decoder(); + + decoder.on("decoded", (packet) => { + expect(packet.data).to.eql(["hello"]); + resolve(); + }); + + decoder.add('51-["hello"]'); + decoder.destroy(); + decoder.add('2["hello"]'); + }); + }); });