Skip to content

Commit

Permalink
fix: calling destroy() should clear all internal state
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Jan 19, 2023
1 parent ae8dd88 commit 22c42e3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/index.ts
Expand Up @@ -288,6 +288,7 @@ export class Decoder extends Emitter<{}, {}, DecoderReservedEvents> {
public destroy() {
if (this.reconstructor) {
this.reconstructor.finishedReconstruction();
this.reconstructor = null;
}
}
}
Expand Down
21 changes: 0 additions & 21 deletions test/arraybuffer.js
Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions test/parser.js
Expand Up @@ -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"]');
});
});
});

0 comments on commit 22c42e3

Please sign in to comment.