From ecff3ffc2fca0339da16c0a424e21746cf1c3767 Mon Sep 17 00:00:00 2001 From: Mark Russell <39885340+mark1russell7@users.noreply.github.com> Date: Fri, 13 Nov 2026 03:34:17 -0500 Subject: [PATCH] fix(sio-client): set recovered to false when the server does not use connection state recovery Without connection state recovery, the CONNECT packet of the server has no "pid" attribute, so socket.recovered was undefined instead of false, although its type is boolean. --- packages/socket.io-client/lib/socket.ts | 2 +- .../test/connection-state-recovery.ts | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/socket.io-client/lib/socket.ts b/packages/socket.io-client/lib/socket.ts index 13c0ed97ce..67059f5379 100644 --- a/packages/socket.io-client/lib/socket.ts +++ b/packages/socket.io-client/lib/socket.ts @@ -833,7 +833,7 @@ export class Socket< private onconnect(id: string, pid: string) { debug("socket connected with id %s", id); this.id = id; - this.recovered = pid && this._pid === pid; + this.recovered = !!pid && this._pid === pid; this._pid = pid; // defined only if connection state recovery is enabled this.connected = true; this.emitBuffered(); diff --git a/packages/socket.io-client/test/connection-state-recovery.ts b/packages/socket.io-client/test/connection-state-recovery.ts index 42c826b0ea..2e33100b8e 100644 --- a/packages/socket.io-client/test/connection-state-recovery.ts +++ b/packages/socket.io-client/test/connection-state-recovery.ts @@ -1,5 +1,6 @@ import expect = require("expect.js"); import { io } from ".."; +import { PacketType } from "socket.io-parser"; import { wrap, BASE_URL, success } from "./support/util"; describe("connection state recovery", () => { @@ -27,4 +28,19 @@ describe("connection state recovery", () => { }); }); }); + + it("should not be recovered when the server does not use connection state recovery", () => { + const socket = io(BASE_URL, { forceNew: true, autoConnect: false }); + + // the CONNECT packet of a server without connection state recovery has no "pid" attribute + // @ts-ignore method 'onpacket' is private + socket.onpacket({ + type: PacketType.CONNECT, + nsp: "/", + data: { sid: "abc" }, + }); + + expect(socket.connected).to.be(true); + expect(socket.recovered).to.be(false); + }); });