Skip to content

Commit c260f17

Browse files
authored
fix(csr): include the offset in the restored packets (#40)
The connection would be recovered, but the offset was not included in the restored packets. Related: - #39 - #39
1 parent fc03543 commit c260f17

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/adapter.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,14 @@ class RedisStreamsAdapter extends ClusterAdapterWithHeartbeat {
289289

290290
for (const entry of entries) {
291291
if (entry.message.nsp === this.nsp.name && entry.message.type === "3") {
292-
const message = RedisStreamsAdapter.decode(entry.message);
293-
294-
// @ts-ignore
295-
if (shouldIncludePacket(session.rooms, message.data.opts)) {
296-
// @ts-ignore
297-
session.missedPackets.push(message.data.packet.data);
292+
const message = RedisStreamsAdapter.decode(entry.message) as {
293+
data: any;
294+
};
295+
const { packet, opts } = message.data;
296+
297+
if (shouldIncludePacket(session.rooms, opts)) {
298+
packet.data.push(entry.id);
299+
session.missedPackets.push(packet.data);
298300
}
299301
}
300302
offset = entry.id;

test/connection-state-recovery.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Server } from "socket.io";
22
import { io as ioc } from "socket.io-client";
3-
import { setup, sleep } from "./util";
3+
import { setup } from "./util";
44
import expect = require("expect.js");
55

66
describe("connection state recovery", () => {
7-
let servers: Server[], ports: number[], cleanup;
7+
let servers: Server[], ports: number[], cleanup: () => void;
88

99
beforeEach(async () => {
1010
const testContext = await setup({
@@ -82,10 +82,14 @@ describe("connection state recovery", () => {
8282
socket.io.engine.close();
8383

8484
socket.on("connect", () => {
85+
// @ts-expect-error _lastOffset is private
86+
offsets.add(socket._lastOffset);
87+
8588
expect(socket.recovered).to.eql(true);
8689

8790
setTimeout(() => {
8891
expect(events).to.eql([1, 2, 3]);
92+
expect(offsets.size).to.eql(4);
8993

9094
socket.disconnect();
9195
done();
@@ -94,9 +98,13 @@ describe("connection state recovery", () => {
9498
});
9599

96100
const events: number[] = [];
101+
const offsets = new Set<string>();
97102

98103
socket.on("myEvent", (val) => {
99104
events.push(val);
105+
// note: the offset is updated after the callback execution
106+
// @ts-expect-error _lastOffset is private
107+
offsets.add(socket._lastOffset);
100108
});
101109
});
102110

0 commit comments

Comments
 (0)