Skip to content

Commit ba6adbb

Browse files
committed
fix(client): bring disableClientInfo option back
It disappeared in v5 fixes redis#2958
1 parent ebd0303 commit ba6adbb

File tree

6 files changed

+153
-8
lines changed

6 files changed

+153
-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';
@@ -17,6 +17,7 @@ import { RedisLegacyClient, RedisLegacyClientType } from './legacy-mode';
1717
import { RedisPoolOptions, RedisClientPool } from './pool';
1818
import { RedisVariadicArgument, parseArgs, pushVariadicArguments } from '../commands/generic-transformers';
1919
import { BasicCommandParser, CommandParser } from './parser';
20+
import { version } from '../../package.json'
2021

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

8594
type WithCommands<
@@ -538,6 +547,36 @@ export default class RedisClient<
538547
);
539548
}
540549

550+
if (!this.#options?.disableClientInfo) {
551+
this.#queue.addCommand([
552+
'CLIENT',
553+
'SETINFO',
554+
'LIB-NAME',
555+
this.#options?.clientInfoTag
556+
? `node-redis(${this.#options.clientInfoTag})` : 'node-redis'
557+
], {
558+
chainId,
559+
asap: true
560+
}).catch(err => {
561+
// Client libraries are expected to ignore failures since they could be
562+
// connected to an older server that doesn't support them.
563+
if (err !instanceof SimpleError || !err.isUnknownCommand()) {
564+
return;
565+
}
566+
});
567+
568+
this.#queue.addCommand(['CLIENT', 'SETINFO', 'LIB-VER', version],{
569+
chainId,
570+
asap: true
571+
}).catch(err => {
572+
// Client libraries are expected to ignore failures since they could be
573+
// connected to an older server that doesn't support them.
574+
if (err !instanceof SimpleError || !err.isUnknownCommand()) {
575+
return;
576+
}
577+
});
578+
}
579+
541580
const commands = await this.#handshake(this.#selectedDB);
542581
for (let i = commands.length - 1; i >= 0; --i) {
543582
promises.push(

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

Lines changed: 91 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,94 @@ describe('CLIENT INFO', () => {
4849
}
4950
}
5051
}, GLOBAL.SERVERS.OPEN);
52+
53+
testUtils.testWithClient('client.clientInfo - disableClientInfo: false', 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.0');
57+
assert.strictEqual(reply.libVer, undefined, 'LibVer should be undefined for Redis < 7.0');
58+
}
59+
if (testUtils.isVersionGreaterThan([7])) {
60+
assert.equal(reply.libName, 'node-redis');
61+
assert.equal(reply.libVer, version);
62+
}
63+
}, GLOBAL.SERVERS.OPEN);
64+
65+
testUtils.testWithClient('client.clientInfo - disableClientInfo: true', async client => {
66+
const reply = await client.clientInfo();
67+
if (!testUtils.isVersionGreaterThan([7])) {
68+
assert.strictEqual(reply.libName, undefined, 'LibName should be undefined for Redis < 7.0');
69+
assert.strictEqual(reply.libVer, undefined, 'LibVer should be undefined for Redis < 7.0');
70+
}
71+
if (testUtils.isVersionGreaterThan([7])) {
72+
assert.equal(reply.libName, '');
73+
assert.equal(reply.libVer, '');
74+
}
75+
}, {
76+
...GLOBAL.SERVERS.OPEN,
77+
clientOptions: {
78+
disableClientInfo: true
79+
}
80+
});
81+
82+
testUtils.testWithClient('client.clientInfo - disableClientInfo: false', async client => {
83+
const reply = await client.clientInfo();
84+
if (!testUtils.isVersionGreaterThan([7])) {
85+
assert.strictEqual(reply.libName, undefined, 'LibName should be undefined for Redis < 7.0');
86+
assert.strictEqual(reply.libVer, undefined, 'LibVer should be undefined for Redis < 7.0');
87+
}
88+
if (testUtils.isVersionGreaterThan([7])) {
89+
assert.equal(reply.libName, 'node-redis');
90+
assert.equal(reply.libVer, version);
91+
}
92+
}, GLOBAL.SERVERS.OPEN);
93+
94+
testUtils.testWithClient('client.clientInfo - disableClientInfo: true - RESP 2', async client => {
95+
const reply = await client.clientInfo();
96+
if (!testUtils.isVersionGreaterThan([7])) {
97+
assert.strictEqual(reply.libName, undefined, 'LibName should be undefined for Redis < 7.0');
98+
assert.strictEqual(reply.libVer, undefined, 'LibVer should be undefined for Redis < 7.0');
99+
}
100+
if (testUtils.isVersionGreaterThan([7])) {
101+
assert.equal(reply.libName, '');
102+
assert.equal(reply.libVer, '');
103+
}
104+
}, {
105+
...GLOBAL.SERVERS.OPEN,
106+
clientOptions: {
107+
disableClientInfo: true,
108+
RESP: 2
109+
}
110+
});
111+
112+
testUtils.testWithClient('client.clientInfo - disableClientInfo: false - RESP 3', async client => {
113+
const reply = await client.clientInfo();
114+
if (!testUtils.isVersionGreaterThan([7])) {
115+
assert.strictEqual(reply.libName, undefined, 'LibName should be undefined for Redis < 7.0');
116+
assert.strictEqual(reply.libVer, undefined, 'LibVer should be undefined for Redis < 7.0');
117+
}
118+
if (testUtils.isVersionGreaterThan([7])) {
119+
assert.equal(reply.libName, 'node-redis');
120+
assert.equal(reply.libVer, version);
121+
}
122+
}, GLOBAL.SERVERS.OPEN);
123+
124+
testUtils.testWithClient('client.clientInfo - disableClientInfo: true', async client => {
125+
const reply = await client.clientInfo();
126+
if (!testUtils.isVersionGreaterThan([7])) {
127+
assert.strictEqual(reply.libName, undefined, 'LibName should be undefined for Redis < 7.0');
128+
assert.strictEqual(reply.libVer, undefined, 'LibVer should be undefined for Redis < 7.0');
129+
}
130+
if (testUtils.isVersionGreaterThan([7])) {
131+
assert.equal(reply.libName, '');
132+
assert.equal(reply.libVer, '');
133+
}
134+
}, {
135+
...GLOBAL.SERVERS.OPEN,
136+
clientOptions: {
137+
disableClientInfo: true,
138+
RESP: 3
139+
}
140+
});
141+
51142
});

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)