From 4ec5636e214b229aa792fabdfea7981e270ae3e1 Mon Sep 17 00:00:00 2001 From: Ariel Barabas <810664+knoid@users.noreply.github.com> Date: Sun, 2 Feb 2025 13:49:31 -0300 Subject: [PATCH] fix: allow disabling keepAlive in TypeScript --- lib/Redis.ts | 8 ++++++-- lib/redis/RedisOptions.ts | 2 +- test/unit/redis.ts | 3 +++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/Redis.ts b/lib/Redis.ts index 181215748..fae852e8d 100644 --- a/lib/Redis.ts +++ b/lib/Redis.ts @@ -227,12 +227,16 @@ class Redis extends Commander implements DataHandledable { // Node ignores setKeepAlive before connect, therefore we wait for the event: // https://github.com/nodejs/node/issues/31663 if (typeof options.keepAlive === "number") { + + // prevents TypeScript error when used inside arrow function + const { keepAlive } = options; + if (stream.connecting) { stream.once(CONNECT_EVENT, () => { - stream.setKeepAlive(true, options.keepAlive); + stream.setKeepAlive(true, keepAlive); }); } else { - stream.setKeepAlive(true, options.keepAlive); + stream.setKeepAlive(true, keepAlive); } } diff --git a/lib/redis/RedisOptions.ts b/lib/redis/RedisOptions.ts index 26cc90aa0..fc48a429c 100644 --- a/lib/redis/RedisOptions.ts +++ b/lib/redis/RedisOptions.ts @@ -28,7 +28,7 @@ export interface CommonRedisOptions extends CommanderOptions { * @link https://nodejs.org/api/net.html#socketsetkeepaliveenable-initialdelay * @default 0 */ - keepAlive?: number; + keepAlive?: number | null; /** * Enable/disable the use of Nagle's algorithm. diff --git a/test/unit/redis.ts b/test/unit/redis.ts index 5ede16bd2..3158ecbd5 100644 --- a/test/unit/redis.ts +++ b/test/unit/redis.ts @@ -99,6 +99,9 @@ describe("Redis", () => { option = getOption("redis://localhost?family=6"); expect(option).to.have.property("family", 6); + + option = getOption(1234, { keepAlive: null }); + expect(option).to.have.property('keepAlive', null); } catch (err) { stub.restore(); throw err;