Skip to content

Commit 865133e

Browse files
committed
fix(client): bring disableClientInfo option back
It disappeared in v5 fixes #2958
1 parent 6f961bd commit 865133e

File tree

6 files changed

+148
-8
lines changed

6 files changed

+148
-8
lines changed

packages/client/lib/client/index.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BasicAuth, CredentialsError, CredentialsProvider, StreamingCredentialsP
44
import RedisCommandsQueue, { CommandOptions } from './commands-queue';
55
import { EventEmitter } from 'node:events';
66
import { attachConfig, functionArgumentsPrefix, getTransformReply, scriptArgumentsPrefix } from '../commander';
7-
import { ClientClosedError, ClientOfflineError, DisconnectsClientError, WatchError } from '../errors';
7+
import { ClientClosedError, ClientOfflineError, DisconnectsClientError, SimpleError, WatchError } from '../errors';
88
import { URL } from 'node:url';
99
import { TcpSocketConnectOpts } from 'node:net';
1010
import { PUBSUB_TYPE, PubSubType, PubSubListener, PubSubTypeListeners, ChannelListeners } from './pub-sub';
@@ -18,6 +18,7 @@ import { RedisPoolOptions, RedisClientPool } from './pool';
1818
import { RedisVariadicArgument, parseArgs, pushVariadicArguments } from '../commands/generic-transformers';
1919
import { BasicCommandParser, CommandParser } from './parser';
2020
import SingleEntryCache from '../single-entry-cache';
21+
import { version } from '../../package.json'
2122

2223
export interface RedisClientOptions<
2324
M extends RedisModules = RedisModules,
@@ -81,6 +82,14 @@ export interface RedisClientOptions<
8182
* TODO
8283
*/
8384
commandOptions?: CommandOptions<TYPE_MAPPING>;
85+
/**
86+
* If set to true, disables sending client identifier (user-agent like message) to the redis server
87+
*/
88+
disableClientInfo?: boolean;
89+
/**
90+
* Tag to append to library name that is sent to the Redis server
91+
*/
92+
clientInfoTag?: string;
8493
}
8594

8695
type WithCommands<
@@ -548,6 +557,36 @@ export default class RedisClient<
548557
);
549558
}
550559

560+
if (!this.#options?.disableClientInfo) {
561+
this.#queue.addCommand([
562+
'CLIENT',
563+
'SETINFO',
564+
'LIB-NAME',
565+
this.#options?.clientInfoTag
566+
? `node-redis(${this.#options.clientInfoTag})` : 'node-redis'
567+
], {
568+
chainId,
569+
asap: true
570+
}).catch(err => {
571+
// Client libraries are expected to ignore failures since they could be
572+
// connected to an older server that doesn't support them.
573+
if (err !instanceof SimpleError || !err.isUnknownCommand()) {
574+
return;
575+
}
576+
});
577+
578+
this.#queue.addCommand(['CLIENT', 'SETINFO', 'LIB-VER', version],{
579+
chainId,
580+
asap: true
581+
}).catch(err => {
582+
// Client libraries are expected to ignore failures since they could be
583+
// connected to an older server that doesn't support them.
584+
if (err !instanceof SimpleError || !err.isUnknownCommand()) {
585+
return;
586+
}
587+
});
588+
}
589+
551590
const commands = await this.#handshake(this.#selectedDB);
552591
for (let i = commands.length - 1; i >= 0; --i) {
553592
promises.push(

packages/client/lib/commands/CLIENT_INFO.spec.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { strict as assert } from 'node:assert';
22
import CLIENT_INFO from './CLIENT_INFO';
33
import testUtils, { GLOBAL } from '../test-utils';
44
import { parseArgs } from './generic-transformers';
5+
import { version } from '../../package.json';
56

67
describe('CLIENT INFO', () => {
78
testUtils.isVersionGreaterThanHook([6, 2]);
@@ -48,4 +49,89 @@ describe('CLIENT INFO', () => {
4849
}
4950
}
5051
}, GLOBAL.SERVERS.OPEN);
52+
53+
testUtils.testWithClient('client.clientInfo Redis < 7', async client => {
54+
const reply = await client.clientInfo();
55+
if (!testUtils.isVersionGreaterThan([7])) {
56+
assert.strictEqual(reply.libName, undefined, 'LibName should be undefined for Redis < 7');
57+
assert.strictEqual(reply.libVer, undefined, 'LibVer should be undefined for Redis < 7');
58+
}
59+
}, GLOBAL.SERVERS.OPEN);
60+
61+
testUtils.testWithClientIfVersionWithinRange([[7], 'LATEST'], 'client.clientInfo Redis>=7 info disabled', async client => {
62+
const reply = await client.clientInfo();
63+
assert.equal(reply.libName, '');
64+
assert.equal(reply.libVer, '');
65+
}, {
66+
...GLOBAL.SERVERS.OPEN,
67+
clientOptions: {
68+
disableClientInfo: true
69+
}
70+
});
71+
72+
testUtils.testWithClientIfVersionWithinRange([[7], 'LATEST'], 'client.clientInfo Redis>=7 resp unset, info enabled, tag set', async client => {
73+
const reply = await client.clientInfo();
74+
assert.equal(reply.libName, 'node-redis(client1)');
75+
assert.equal(reply.libVer, version);
76+
}, {
77+
...GLOBAL.SERVERS.OPEN,
78+
clientOptions: {
79+
clientInfoTag: 'client1'
80+
}
81+
});
82+
83+
testUtils.testWithClientIfVersionWithinRange([[7], 'LATEST'], 'client.clientInfo Redis>=7 resp unset, info enabled, tag unset', async client => {
84+
const reply = await client.clientInfo();
85+
assert.equal(reply.libName, 'node-redis');
86+
assert.equal(reply.libVer, version);
87+
}, GLOBAL.SERVERS.OPEN);
88+
89+
testUtils.testWithClientIfVersionWithinRange([[7], 'LATEST'], 'client.clientInfo Redis>=7 resp2 info enabled', async client => {
90+
const reply = await client.clientInfo();
91+
assert.equal(reply.libName, 'node-redis(client1)');
92+
assert.equal(reply.libVer, version);
93+
}, {
94+
...GLOBAL.SERVERS.OPEN,
95+
clientOptions: {
96+
RESP: 2,
97+
clientInfoTag: 'client1'
98+
}
99+
});
100+
101+
testUtils.testWithClientIfVersionWithinRange([[7], 'LATEST'], 'client.clientInfo Redis>=7 resp2 info disabled', async client => {
102+
const reply = await client.clientInfo();
103+
assert.equal(reply.libName, '');
104+
assert.equal(reply.libVer, '');
105+
}, {
106+
...GLOBAL.SERVERS.OPEN,
107+
clientOptions: {
108+
disableClientInfo: true,
109+
RESP: 2
110+
}
111+
});
112+
113+
testUtils.testWithClientIfVersionWithinRange([[7], 'LATEST'], 'client.clientInfo Redis>=7 resp3 info enabled', async client => {
114+
const reply = await client.clientInfo();
115+
assert.equal(reply.libName, 'node-redis(client1)');
116+
assert.equal(reply.libVer, version);
117+
}, {
118+
...GLOBAL.SERVERS.OPEN,
119+
clientOptions: {
120+
RESP: 3,
121+
clientInfoTag: 'client1'
122+
}
123+
});
124+
125+
testUtils.testWithClientIfVersionWithinRange([[7], 'LATEST'], 'client.clientInfo Redis>=7 resp3 info disabled', async client => {
126+
const reply = await client.clientInfo();
127+
assert.equal(reply.libName, '');
128+
assert.equal(reply.libVer, '');
129+
}, {
130+
...GLOBAL.SERVERS.OPEN,
131+
clientOptions: {
132+
disableClientInfo: true,
133+
RESP: 3
134+
}
135+
});
136+
51137
});

packages/client/lib/commands/CLIENT_INFO.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ export interface ClientInfoReply {
5252
* available since 7.0
5353
*/
5454
resp?: number;
55+
/**
56+
* available since 7.0
57+
*/
58+
libName?: string;
59+
/**
60+
* available since 7.0
61+
*/
62+
libVer?: string;
5563
}
5664

5765
const CLIENT_INFO_REGEX = /([^\s=]+)=([^\s]*)/g;
@@ -67,7 +75,6 @@ export default {
6775
for (const item of rawReply.toString().matchAll(CLIENT_INFO_REGEX)) {
6876
map[item[1]] = item[2];
6977
}
70-
7178
const reply: ClientInfoReply = {
7279
id: Number(map.id),
7380
addr: map.addr,
@@ -89,7 +96,9 @@ export default {
8996
totMem: Number(map['tot-mem']),
9097
events: map.events,
9198
cmd: map.cmd,
92-
user: map.user
99+
user: map.user,
100+
libName: map['lib-name'],
101+
libVer: map['lib-ver']
93102
};
94103

95104
if (map.laddr !== undefined) {

packages/client/lib/errors.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ export class ErrorReply extends Error {
6464
}
6565
}
6666

67-
export class SimpleError extends ErrorReply {}
67+
export class SimpleError extends ErrorReply {
68+
isUnknownCommand(): boolean {
69+
return this.message.indexOf('ERR unknown command') !== -1;
70+
}
71+
}
6872

6973
export class BlobError extends ErrorReply {}
7074

packages/client/tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"extends": "../../tsconfig.base.json",
33
"compilerOptions": {
4-
"outDir": "./dist"
4+
"outDir": "./dist",
55
},
66
"include": [
77
"./index.ts",
8-
"./lib/**/*.ts"
8+
"./lib/**/*.ts",
9+
"./package.json"
910
],
1011
"exclude": [
1112
"./lib/test-utils.ts",
@@ -18,6 +19,6 @@
1819
"./lib"
1920
],
2021
"entryPointStrategy": "expand",
21-
"out": "../../documentation/client"
22+
"out": "../../documentation/client",
2223
}
2324
}

tsconfig.base.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"sourceMap": true,
1616
"declaration": true,
1717
"declarationMap": true,
18-
"allowJs": true
18+
"allowJs": true,
19+
"resolveJsonModule": true
1920
}
2021
}

0 commit comments

Comments
 (0)