Skip to content

Commit

Permalink
refactor: do not persist session if connection state recovery if disa…
Browse files Browse the repository at this point in the history
…bled

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
  • Loading branch information
darrachequesne committed Feb 6, 2023
1 parent b2dd7cf commit d8143cc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,10 @@ export class Socket<
debug("closing socket - reason %s", reason);
this.emitReserved("disconnecting", reason, description);

if (RECOVERABLE_DISCONNECT_REASONS.has(reason)) {
if (
this.server._opts.connectionStateRecovery &&
RECOVERABLE_DISCONNECT_REASONS.has(reason)
) {
debug("connection state recovery is enabled for sid %s", this.id);
this.adapter.persistSession({
sid: this.id,
Expand Down
29 changes: 29 additions & 0 deletions test/connection-state-recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Server, Socket } from "..";
import expect from "expect.js";
import { waitFor, eioHandshake, eioPush, eioPoll } from "./support/util";
import { createServer, Server as HttpServer } from "http";
import { Adapter } from "socket.io-adapter";

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

io.close();
});

it("should not call adapter#persistSession or adapter#restoreSession if disabled", async () => {
const httpServer = createServer().listen(0);

class DummyAdapter extends Adapter {
override persistSession(session) {
expect.fail();
}

override restoreSession(pid, offset) {
expect.fail();
return Promise.reject("should not happen");
}
}

const io = new Server(httpServer, {
adapter: DummyAdapter,
});

// Engine.IO handshake
const sid = await eioHandshake(httpServer);

await eioPush(httpServer, sid, '40{"pid":"foo","offset":"bar"}');
await eioPoll(httpServer, sid);
await eioPush(httpServer, sid, "1");

io.close();
});
});

0 comments on commit d8143cc

Please sign in to comment.