Skip to content

Commit

Permalink
fix: allow disabling keepAlive in TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
knoid committed Feb 2, 2025
1 parent 3a04bee commit 4ec5636
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/redis/RedisOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions test/unit/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4ec5636

Please sign in to comment.