diff --git a/packages/socket.io-client/lib/contrib/backo2.ts b/packages/socket.io-client/lib/contrib/backo2.ts index 2307eff46..544ce6611 100644 --- a/packages/socket.io-client/lib/contrib/backo2.ts +++ b/packages/socket.io-client/lib/contrib/backo2.ts @@ -12,8 +12,8 @@ export function Backoff(opts) { opts = opts || {}; - this.ms = opts.min || 100; - this.max = opts.max || 10000; + this.ms = opts.min ?? 100; + this.max = opts.max ?? 10000; this.factor = opts.factor || 2; this.jitter = opts.jitter > 0 && opts.jitter <= 1 ? opts.jitter : 0; this.attempts = 0; diff --git a/packages/socket.io-client/lib/manager.ts b/packages/socket.io-client/lib/manager.ts index b9597bf23..d366fcbec 100644 --- a/packages/socket.io-client/lib/manager.ts +++ b/packages/socket.io-client/lib/manager.ts @@ -169,9 +169,9 @@ export class Manager< this.opts = opts; installTimerFunctions(this, opts); this.reconnection(opts.reconnection !== false); - this.reconnectionAttempts(opts.reconnectionAttempts || Infinity); - this.reconnectionDelay(opts.reconnectionDelay || 1000); - this.reconnectionDelayMax(opts.reconnectionDelayMax || 5000); + this.reconnectionAttempts(opts.reconnectionAttempts ?? Infinity); + this.reconnectionDelay(opts.reconnectionDelay ?? 1000); + this.reconnectionDelayMax(opts.reconnectionDelayMax ?? 5000); this.randomizationFactor(opts.randomizationFactor ?? 0.5); this.backoff = new Backoff({ min: this.reconnectionDelay(), diff --git a/packages/socket.io-client/test/connection.ts b/packages/socket.io-client/test/connection.ts index 1cf3bf01f..1944dc36d 100644 --- a/packages/socket.io-client/test/connection.ts +++ b/packages/socket.io-client/test/connection.ts @@ -403,6 +403,45 @@ describe("connection", () => { }); }); + it("should accept 0 as the value of the reconnection options", () => { + const manager = new Manager(BASE_URL, { + autoConnect: false, + reconnectionAttempts: 0, + reconnectionDelay: 0, + reconnectionDelayMax: 0, + }); + + expect(manager.reconnectionAttempts()).to.be(0); + expect(manager.reconnectionDelay()).to.be(0); + expect(manager.reconnectionDelayMax()).to.be(0); + // @ts-ignore property 'backoff' is private + expect(manager.backoff.duration()).to.be(0); + }); + + it("should not try to reconnect when requested zero attempts", () => { + return wrap((done) => { + const manager = new Manager(BASE_URL, { + reconnection: true, + timeout: 0, + reconnectionAttempts: 0, + reconnectionDelay: 10, + }); + let socket; + + manager.on("reconnect_attempt", () => { + done(new Error("should not happen")); + }); + + manager.on("reconnect_failed", () => { + socket.close(); + manager._close(); + done(); + }); + + socket = manager.socket("/timeout"); + }); + }); + it("should fire reconnect_* events on manager", () => { return wrap((done) => { const manager = new Manager(BASE_URL, {