Skip to content

fix: allow disabling keepAlive in TypeScript #1951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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