Skip to content

Commit 8c0d113

Browse files
committed
Merge pull request #887 from NodeRedis/rename-commands
Add rename_commands option Closes #499
2 parents 90033bd + 972d1cd commit 8c0d113

File tree

5 files changed

+135
-5
lines changed

5 files changed

+135
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ redis - a node.js redis client
55
[![Coverage Status](https://coveralls.io/repos/NodeRedis/node_redis/badge.svg?branch=)](https://coveralls.io/r/NodeRedis/node_redis?branch=)
66
[![Windows Tests](https://img.shields.io/appveyor/ci/bcoe/node-redis/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/bcoe/node-redis)
77

8-
This is a complete Redis client for node.js. It supports all Redis commands and focuses on performance.
8+
This is a complete and feature rich Redis client for node.js. It supports all Redis commands and focuses on performance.
99

1010
Install with:
1111

@@ -204,6 +204,7 @@ limits total amount of connection tries. Setting this to 1 will prevent any reco
204204
* `auth_pass`: *null*; If set, client will run redis auth command on connect.
205205
* `family`: *IPv4*; You can force using IPv6 if you set the family to 'IPv6'. See Node.js [net](https://nodejs.org/api/net.html) or [dns](https://nodejs.org/api/dns.html) modules how to use the family type.
206206
* `disable_resubscribing`: *false*; If set to `true`, a client won't resubscribe after disconnecting
207+
* `rename_commands`: *null*; pass a object with renamed commands to use those instead of the original functions. See the [redis security topics](http://redis.io/topics/security) for more info.
207208

208209
```js
209210
var redis = require("redis"),

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Changelog
66
Features
77

88
- Added disable_resubscribing option to prevent a client from resubscribing after reconnecting (@BridgeAR)
9+
- Added rename_commands options to handle renamed commands from the redis config (@digmxl & @BridgeAR)
910

1011
Bugfixes
1112

index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ function RedisClient(stream, options) {
4242
this.connected = false;
4343
this.ready = false;
4444
this.connections = 0;
45-
if (this.options.socket_nodelay === undefined) {
46-
this.options.socket_nodelay = true;
45+
if (options.socket_nodelay === undefined) {
46+
options.socket_nodelay = true;
4747
}
48-
if (this.options.socket_keepalive === undefined) {
49-
this.options.socket_keepalive = true;
48+
if (options.socket_keepalive === undefined) {
49+
options.socket_keepalive = true;
50+
}
51+
if (options.rename_commands) {
52+
for (var command in options.rename_commands) {
53+
if (options.rename_commands.hasOwnProperty(command)) {
54+
options.rename_commands[command.toLowerCase()] = options.rename_commands[command];
55+
}
56+
}
5057
}
5158
this.should_buffer = false;
5259
this.command_queue_high_water = options.command_queue_high_water || 1000;
@@ -720,6 +727,10 @@ RedisClient.prototype.send_command = function (command, args, callback) {
720727

721728
elem_count = args.length + 1;
722729

730+
if (typeof this.options.rename_commands !== 'undefined' && this.options.rename_commands[command]) {
731+
command = this.options.rename_commands[command];
732+
}
733+
723734
// Always use 'Multi bulk commands', but if passed any Buffer args, then do multiple writes, one for each arg.
724735
// This means that using Buffers in commands is going to be slower, so use Strings if you don't already have a Buffer.
725736

test/conf/rename.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
port 6379
2+
bind ::1 127.0.0.1
3+
unixsocket /tmp/redis.sock
4+
unixsocketperm 755
5+
rename-command SET 807081f5afa96845a02816a28b7258c3
6+
rename-command GET f397808a43ceca3963e22b4e13deb672
7+
rename-command GETRANGE 9e3102b15cf231c4e9e940f284744fe0

test/rename.spec.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
'use strict';
2+
3+
var assert = require("assert");
4+
var config = require("./lib/config");
5+
var helper = require('./helper');
6+
var redis = config.redis;
7+
8+
describe("rename commands", function () {
9+
before(function (done) {
10+
helper.stopRedis(function () {
11+
helper.startRedis('./conf/rename.conf', done);
12+
});
13+
});
14+
15+
helper.allTests(function(parser, ip, args) {
16+
17+
describe("using " + parser + " and " + ip, function () {
18+
var client = null;
19+
20+
afterEach(function () {
21+
client.end();
22+
});
23+
24+
it("allows to use renamed functions", function (done) {
25+
if (helper.redisProcess().spawnFailed()) this.skip();
26+
27+
client = redis.createClient({
28+
rename_commands: {
29+
set: '807081f5afa96845a02816a28b7258c3',
30+
GETRANGE: '9e3102b15cf231c4e9e940f284744fe0'
31+
}
32+
});
33+
34+
client.set('key', 'value', function(err, reply) {
35+
assert.strictEqual(reply, 'OK');
36+
});
37+
38+
client.get('key', function(err, reply) {
39+
assert.strictEqual(err.message, "ERR unknown command 'get'");
40+
assert.strictEqual(err.command, 'GET');
41+
assert.strictEqual(reply, undefined);
42+
});
43+
44+
client.getrange('key', 1, -1, function(err, reply) {
45+
assert.strictEqual(reply, 'alue');
46+
assert.strictEqual(err, null);
47+
done();
48+
});
49+
});
50+
51+
it("should also work with multi", function (done) {
52+
if (helper.redisProcess().spawnFailed()) this.skip();
53+
54+
client = redis.createClient({
55+
rename_commands: {
56+
SET: '807081f5afa96845a02816a28b7258c3',
57+
getrange: '9e3102b15cf231c4e9e940f284744fe0'
58+
}
59+
});
60+
61+
client.multi([['set', 'key', 'value']]).exec(function (err, res) {
62+
assert.strictEqual(res[0], 'OK');
63+
});
64+
65+
var multi = client.multi();
66+
multi.getrange('key', 1, -1);
67+
multi.exec(function (err, res) {
68+
assert(!err);
69+
assert.strictEqual(res.length, 1);
70+
assert.strictEqual(res[0], 'alue');
71+
done();
72+
});
73+
});
74+
75+
it("should also work with multi and abort transaction", function (done) {
76+
if (helper.redisProcess().spawnFailed()) this.skip();
77+
78+
client = redis.createClient({
79+
rename_commands: {
80+
SET: '807081f5afa96845a02816a28b7258c3',
81+
getrange: '9e3102b15cf231c4e9e940f284744fe0'
82+
}
83+
});
84+
85+
var multi = client.multi();
86+
multi.get('key');
87+
multi.getrange('key', 1, -1, function(err, reply) {
88+
assert.strictEqual(reply, 'alue');
89+
assert.strictEqual(err, null);
90+
});
91+
multi.exec(function (err, res) {
92+
assert(err);
93+
assert.strictEqual(err.message, 'EXECABORT Transaction discarded because of previous errors.');
94+
assert.strictEqual(err.errors[0].message, "ERR unknown command 'get'");
95+
assert.strictEqual(err.errors[0].command, 'GET');
96+
assert.strictEqual(err.code, 'EXECABORT');
97+
assert.strictEqual(err.errors[0].code, 'ERR');
98+
done();
99+
});
100+
});
101+
102+
});
103+
});
104+
105+
after(function (done) {
106+
helper.stopRedis(function () {
107+
helper.startRedis('./conf/redis.conf', done);
108+
});
109+
});
110+
});

0 commit comments

Comments
 (0)