Skip to content

Commit d8143cc

Browse files
refactor: do not persist session if connection state recovery if disabled
This is a follow-up commit of [1]. Without it, adapter.persistSession() would be called even if the connection state recovery feature was disabled. [1]: 54d5ee0
1 parent b2dd7cf commit d8143cc

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

Diff for: lib/socket.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,10 @@ export class Socket<
742742
debug("closing socket - reason %s", reason);
743743
this.emitReserved("disconnecting", reason, description);
744744

745-
if (RECOVERABLE_DISCONNECT_REASONS.has(reason)) {
745+
if (
746+
this.server._opts.connectionStateRecovery &&
747+
RECOVERABLE_DISCONNECT_REASONS.has(reason)
748+
) {
746749
debug("connection state recovery is enabled for sid %s", this.id);
747750
this.adapter.persistSession({
748751
sid: this.id,

Diff for: test/connection-state-recovery.ts

+29
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Server, Socket } from "..";
22
import expect from "expect.js";
33
import { waitFor, eioHandshake, eioPush, eioPoll } from "./support/util";
44
import { createServer, Server as HttpServer } from "http";
5+
import { Adapter } from "socket.io-adapter";
56

67
async function init(httpServer: HttpServer, io: Server) {
78
// Engine.IO handshake
@@ -215,4 +216,32 @@ describe("connection state recovery", () => {
215216

216217
io.close();
217218
});
219+
220+
it("should not call adapter#persistSession or adapter#restoreSession if disabled", async () => {
221+
const httpServer = createServer().listen(0);
222+
223+
class DummyAdapter extends Adapter {
224+
override persistSession(session) {
225+
expect.fail();
226+
}
227+
228+
override restoreSession(pid, offset) {
229+
expect.fail();
230+
return Promise.reject("should not happen");
231+
}
232+
}
233+
234+
const io = new Server(httpServer, {
235+
adapter: DummyAdapter,
236+
});
237+
238+
// Engine.IO handshake
239+
const sid = await eioHandshake(httpServer);
240+
241+
await eioPush(httpServer, sid, '40{"pid":"foo","offset":"bar"}');
242+
await eioPoll(httpServer, sid);
243+
await eioPush(httpServer, sid, "1");
244+
245+
io.close();
246+
});
218247
});

0 commit comments

Comments
 (0)