pax_global_header00006660000000000000000000000064123721772420014521gustar00rootroot0000000000000052 comment=38ae7a7f507a71224e24a02d703a959f1b0fc657 node_redis-0.12.1/000077500000000000000000000000001237217724200137155ustar00rootroot00000000000000node_redis-0.12.1/.gitignore000066400000000000000000000000301237217724200156760ustar00rootroot00000000000000node_modules .tern-port node_redis-0.12.1/.npmignore000066400000000000000000000001651237217724200157160ustar00rootroot00000000000000examples/ benches/ test.js diff_multi_bench_output.js generate_commands.js multi_bench.js test-unref.js changelog.md node_redis-0.12.1/README.md000066400000000000000000000706511237217724200152050ustar00rootroot00000000000000redis - a node.js redis client =========================== This is a complete Redis client for node.js. It supports all Redis commands, including many recently added commands like EVAL from experimental Redis server branches. Install with: npm install redis Pieter Noordhuis has provided a binding to the official `hiredis` C library, which is non-blocking and fast. To use `hiredis`, do: npm install hiredis redis If `hiredis` is installed, `node_redis` will use it by default. Otherwise, a pure JavaScript parser will be used. If you use `hiredis`, be sure to rebuild it whenever you upgrade your version of node. There are mysterious failures that can happen between node and native code modules after a node upgrade. ## Usage Simple example, included as `examples/simple.js`: ```js var redis = require("redis"), client = redis.createClient(); // if you'd like to select database 3, instead of 0 (default), call // client.select(3, function() { /* ... */ }); client.on("error", function (err) { console.log("Error " + err); }); client.set("string key", "string val", redis.print); client.hset("hash key", "hashtest 1", "some value", redis.print); client.hset(["hash key", "hashtest 2", "some other value"], redis.print); client.hkeys("hash key", function (err, replies) { console.log(replies.length + " replies:"); replies.forEach(function (reply, i) { console.log(" " + i + ": " + reply); }); client.quit(); }); ``` This will display: mjr:~/work/node_redis (master)$ node example.js Reply: OK Reply: 0 Reply: 0 2 replies: 0: hashtest 1 1: hashtest 2 mjr:~/work/node_redis (master)$ ## Performance Here are typical results of `multi_bench.js` which is similar to `redis-benchmark` from the Redis distribution. It uses 50 concurrent connections with no pipelining. JavaScript parser: PING: 20000 ops 42283.30 ops/sec 0/5/1.182 SET: 20000 ops 32948.93 ops/sec 1/7/1.515 GET: 20000 ops 28694.40 ops/sec 0/9/1.740 INCR: 20000 ops 39370.08 ops/sec 0/8/1.269 LPUSH: 20000 ops 36429.87 ops/sec 0/8/1.370 LRANGE (10 elements): 20000 ops 9891.20 ops/sec 1/9/5.048 LRANGE (100 elements): 20000 ops 1384.56 ops/sec 10/91/36.072 hiredis parser: PING: 20000 ops 46189.38 ops/sec 1/4/1.082 SET: 20000 ops 41237.11 ops/sec 0/6/1.210 GET: 20000 ops 39682.54 ops/sec 1/7/1.257 INCR: 20000 ops 40080.16 ops/sec 0/8/1.242 LPUSH: 20000 ops 41152.26 ops/sec 0/3/1.212 LRANGE (10 elements): 20000 ops 36563.07 ops/sec 1/8/1.363 LRANGE (100 elements): 20000 ops 21834.06 ops/sec 0/9/2.287 The performance of `node_redis` improves dramatically with pipelining, which happens automatically in most normal programs. ### Sending Commands Each Redis command is exposed as a function on the `client` object. All functions take either an `args` Array plus optional `callback` Function or a variable number of individual arguments followed by an optional callback. Here is an example of passing an array of arguments and a callback: client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {}); Here is that same call in the second style: client.mset("test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {}); Note that in either form the `callback` is optional: client.set("some key", "some val"); client.set(["some other key", "some val"]); If the key is missing, reply will be null (probably): client.get("missingkey", function(err, reply) { // reply is null when the key is missing console.log(reply); }); For a list of Redis commands, see [Redis Command Reference](http://redis.io/commands) The commands can be specified in uppercase or lowercase for convenience. `client.get()` is the same as `client.GET()`. Minimal parsing is done on the replies. Commands that return a single line reply return JavaScript Strings, integer replies return JavaScript Numbers, "bulk" replies return node Buffers, and "multi bulk" replies return a JavaScript Array of node Buffers. `HGETALL` returns an Object with Buffers keyed by the hash keys. # API ## Connection Events `client` will emit some events about the state of the connection to the Redis server. ### "ready" `client` will emit `ready` a connection is established to the Redis server and the server reports that it is ready to receive commands. Commands issued before the `ready` event are queued, then replayed just before this event is emitted. ### "connect" `client` will emit `connect` at the same time as it emits `ready` unless `client.options.no_ready_check` is set. If this options is set, `connect` will be emitted when the stream is connected, and then you are free to try to send commands. ### "error" `client` will emit `error` when encountering an error connecting to the Redis server. Note that "error" is a special event type in node. If there are no listeners for an "error" event, node will exit. This is usually what you want, but it can lead to some cryptic error messages like this: mjr:~/work/node_redis (master)$ node example.js node.js:50 throw e; ^ Error: ECONNREFUSED, Connection refused at IOWatcher.callback (net:870:22) at node.js:607:9 Not very useful in diagnosing the problem, but if your program isn't ready to handle this, it is probably the right thing to just exit. `client` will also emit `error` if an exception is thrown inside of `node_redis` for whatever reason. It would be nice to distinguish these two cases. ### "end" `client` will emit `end` when an established Redis server connection has closed. ### "drain" `client` will emit `drain` when the TCP connection to the Redis server has been buffering, but is now writable. This event can be used to stream commands in to Redis and adapt to backpressure. Right now, you need to check `client.command_queue.length` to decide when to reduce your send rate. Then you can resume sending when you get `drain`. ### "idle" `client` will emit `idle` when there are no outstanding commands that are awaiting a response. ## redis.createClient() ### overloading * redis.createClient() = redis.createClient(6379, '127.0.0.1', {}) * redis.createClient(options) = redis.createClient(6379, '127.0.0.1', options) * redis.createClient(unix_socket, options) * redis.createClient(port, host, options) If you have `redis-server` running on the same computer as node, then the defaults for port and host are probably fine. `options` in an object with the following possible properties: * `parser`: which Redis protocol reply parser to use. Defaults to `hiredis` if that module is installed. This may also be set to `javascript`. * `return_buffers`: defaults to `false`. If set to `true`, then all replies will be sent to callbacks as node Buffer objects instead of JavaScript Strings. * `detect_buffers`: default to `false`. If set to `true`, then replies will be sent to callbacks as node Buffer objects if any of the input arguments to the original command were Buffer objects. This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to every command on a client. * `socket_nodelay`: defaults to `true`. Whether to call setNoDelay() on the TCP stream, which disables the Nagle algorithm on the underlying socket. Setting this option to `false` can result in additional throughput at the cost of more latency. Most applications will want this set to `true`. * `socket_keepalive` defaults to `true`. Whether the keep-alive functionality is enabled on the underlying socket. * `no_ready_check`: defaults to `false`. When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server not respond to any commands. To work around this, `node_redis` has a "ready check" which sends the `INFO` command to the server. The response from the `INFO` command indicates whether the server is ready for more commands. When ready, `node_redis` emits a `ready` event. Setting `no_ready_check` to `true` will inhibit this check. * `enable_offline_queue`: defaults to `true`. By default, if there is no active connection to the redis server, commands are added to a queue and are executed once the connection has been established. Setting `enable_offline_queue` to `false` will disable this feature and the callback will be execute immediately with an error, or an error will be thrown if no callback is specified. * `retry_max_delay`: defaults to `null`. By default every time the client tries to connect and fails time before reconnection (delay) almost doubles. This delay normally grows infinitely, but setting `retry_max_delay` limits delay to maximum value, provided in milliseconds. * `connect_timeout` defaults to `false`. By default client will try reconnecting until connected. Setting `connect_timeout` limits total time for client to reconnect. Value is provided in milliseconds and is counted once the disconnect occured. * `max_attempts` defaults to `null`. By default client will try reconnecting until connected. Setting `max_attempts` limits total amount of reconnects. * `auth_pass` defaults to `null`. By default client will try connecting without auth. If set, client will run redis auth command on connect. * `family` defaults to `IPv4`. The client connects in IPv4 if not specified or if the DNS resolution returns an IPv4 address. You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns modules how to use the family type. ```js var redis = require("redis"), client = redis.createClient({detect_buffers: true}); client.set("foo_rand000000000000", "OK"); // This will return a JavaScript String client.get("foo_rand000000000000", function (err, reply) { console.log(reply.toString()); // Will print `OK` }); // This will return a Buffer since original key is specified as a Buffer client.get(new Buffer("foo_rand000000000000"), function (err, reply) { console.log(reply.toString()); // Will print `` }); client.end(); ``` `createClient()` returns a `RedisClient` object that is named `client` in all of the examples here. ## client.auth(password, callback) When connecting to Redis servers that require authentication, the `AUTH` command must be sent as the first command after connecting. This can be tricky to coordinate with reconnections, the ready check, etc. To make this easier, `client.auth()` stashes `password` and will send it after each connection, including reconnections. `callback` is invoked only once, after the response to the very first `AUTH` command sent. NOTE: Your call to `client.auth()` should not be inside the ready handler. If you are doing this wrong, `client` will emit an error that looks something like this `Error: Ready check failed: ERR operation not permitted`. ## client.end() Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed. If you want to exit cleanly, call `client.quit()` to send the `QUIT` command after you have handled all replies. This example closes the connection to the Redis server before the replies have been read. You probably don't want to do this: ```js var redis = require("redis"), client = redis.createClient(); client.set("foo_rand000000000000", "some fantastic value"); client.get("foo_rand000000000000", function (err, reply) { console.log(reply.toString()); }); client.end(); ``` `client.end()` is useful for timeout cases where something is stuck or taking too long and you want to start over. ## client.unref() Call `unref()` on the underlying socket connection to the Redis server, allowing the program to exit once no more commands are pending. This is an **experimental** feature, and only supports a subset of the Redis protocol. Any commands where client state is saved on the Redis server, e.g. `*SUBSCRIBE` or the blocking `BL*` commands will *NOT* work with `.unref()`. ```js var redis = require("redis") var client = redis.createClient() /* Calling unref() will allow this program to exit immediately after the get command finishes. Otherwise the client would hang as long as the client-server connection is alive. */ client.unref() client.get("foo", function (err, value){ if (err) throw(err) console.log(value) }) ``` ## Friendlier hash commands Most Redis commands take a single String or an Array of Strings as arguments, and replies are sent back as a single String or an Array of Strings. When dealing with hash values, there are a couple of useful exceptions to this. ### client.hgetall(hash) The reply from an HGETALL command will be converted into a JavaScript Object by `node_redis`. That way you can interact with the responses using JavaScript syntax. Example: client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234"); client.hgetall("hosts", function (err, obj) { console.dir(obj); }); Output: { mjr: '1', another: '23', home: '1234' } ### client.hmset(hash, obj, [callback]) Multiple values in a hash can be set by supplying an object: client.HMSET(key2, { "0123456789": "abcdefghij", // NOTE: key and value will be coerced to strings "some manner of key": "a type of value" }); The properties and values of this Object will be set as keys and values in the Redis hash. ### client.hmset(hash, key1, val1, ... keyn, valn, [callback]) Multiple values may also be set by supplying a list: client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value"); ## Publish / Subscribe Here is a simple example of the API for publish / subscribe. This program opens two client connections, subscribes to a channel on one of them, and publishes to that channel on the other: ```js var redis = require("redis"), client1 = redis.createClient(), client2 = redis.createClient(), msg_count = 0; client1.on("subscribe", function (channel, count) { client2.publish("a nice channel", "I am sending a message."); client2.publish("a nice channel", "I am sending a second message."); client2.publish("a nice channel", "I am sending my last message."); }); client1.on("message", function (channel, message) { console.log("client1 channel " + channel + ": " + message); msg_count += 1; if (msg_count === 3) { client1.unsubscribe(); client1.end(); client2.end(); } }); client1.incr("did a thing"); client1.subscribe("a nice channel"); ``` When a client issues a `SUBSCRIBE` or `PSUBSCRIBE`, that connection is put into a "subscriber" mode. At that point, only commands that modify the subscription set are valid. When the subscription set is empty, the connection is put back into regular mode. If you need to send regular commands to Redis while in subscriber mode, just open another connection. ## Subscriber Events If a client has subscriptions active, it may emit these events: ### "message" (channel, message) Client will emit `message` for every message received that matches an active subscription. Listeners are passed the channel name as `channel` and the message Buffer as `message`. ### "pmessage" (pattern, channel, message) Client will emit `pmessage` for every message received that matches an active subscription pattern. Listeners are passed the original pattern used with `PSUBSCRIBE` as `pattern`, the sending channel name as `channel`, and the message Buffer as `message`. ### "subscribe" (channel, count) Client will emit `subscribe` in response to a `SUBSCRIBE` command. Listeners are passed the channel name as `channel` and the new count of subscriptions for this client as `count`. ### "psubscribe" (pattern, count) Client will emit `psubscribe` in response to a `PSUBSCRIBE` command. Listeners are passed the original pattern as `pattern`, and the new count of subscriptions for this client as `count`. ### "unsubscribe" (channel, count) Client will emit `unsubscribe` in response to a `UNSUBSCRIBE` command. Listeners are passed the channel name as `channel` and the new count of subscriptions for this client as `count`. When `count` is 0, this client has left subscriber mode and no more subscriber events will be emitted. ### "punsubscribe" (pattern, count) Client will emit `punsubscribe` in response to a `PUNSUBSCRIBE` command. Listeners are passed the channel name as `channel` and the new count of subscriptions for this client as `count`. When `count` is 0, this client has left subscriber mode and no more subscriber events will be emitted. ## client.multi([commands]) `MULTI` commands are queued up until an `EXEC` is issued, and then all commands are run atomically by Redis. The interface in `node_redis` is to return an individual `Multi` object by calling `client.multi()`. ```js var redis = require("./index"), client = redis.createClient(), set_size = 20; client.sadd("bigset", "a member"); client.sadd("bigset", "another member"); while (set_size > 0) { client.sadd("bigset", "member " + set_size); set_size -= 1; } // multi chain with an individual callback client.multi() .scard("bigset") .smembers("bigset") .keys("*", function (err, replies) { // NOTE: code in this callback is NOT atomic // this only happens after the the .exec call finishes. client.mget(replies, redis.print); }) .dbsize() .exec(function (err, replies) { console.log("MULTI got " + replies.length + " replies"); replies.forEach(function (reply, index) { console.log("Reply " + index + ": " + reply.toString()); }); }); ``` ### Multi.exec( callback ) `client.multi()` is a constructor that returns a `Multi` object. `Multi` objects share all of the same command methods as `client` objects do. Commands are queued up inside the `Multi` object until `Multi.exec()` is invoked. The `callback` of `.exec()` will get invoked with two arguments: * `err` **type:** `null | Array` err is either null or an array of Error Objects corresponding the the sequence the commands where chained. The last item of the array will always be an `EXECABORT` type of error originating from the `.exec()` itself. * `results` **type:** `null | Array` results is an array of responses corresponding the the sequence the commands where chained. You can either chain together `MULTI` commands as in the above example, or you can queue individual commands while still sending regular client command as in this example: ```js var redis = require("redis"), client = redis.createClient(), multi; // start a separate multi command queue multi = client.multi(); multi.incr("incr thing", redis.print); multi.incr("incr other thing", redis.print); // runs immediately client.mset("incr thing", 100, "incr other thing", 1, redis.print); // drains multi queue and runs atomically multi.exec(function (err, replies) { console.log(replies); // 101, 2 }); // you can re-run the same transaction if you like multi.exec(function (err, replies) { console.log(replies); // 102, 3 client.quit(); }); ``` In addition to adding commands to the `MULTI` queue individually, you can also pass an array of commands and arguments to the constructor: ```js var redis = require("redis"), client = redis.createClient(), multi; client.multi([ ["mget", "multifoo", "multibar", redis.print], ["incr", "multifoo"], ["incr", "multibar"] ]).exec(function (err, replies) { console.log(replies); }); ``` ## Monitor mode Redis supports the `MONITOR` command, which lets you see all commands received by the Redis server across all client connections, including from other client libraries and other computers. After you send the `MONITOR` command, no other commands are valid on that connection. `node_redis` will emit a `monitor` event for every new monitor message that comes across. The callback for the `monitor` event takes a timestamp from the Redis server and an array of command arguments. Here is a simple example: ```js var client = require("redis").createClient(), util = require("util"); client.monitor(function (err, res) { console.log("Entering monitoring mode."); }); client.on("monitor", function (time, args) { console.log(time + ": " + util.inspect(args)); }); ``` # Extras Some other things you might like to know about. ## client.server_info After the ready probe completes, the results from the INFO command are saved in the `client.server_info` object. The `versions` key contains an array of the elements of the version string for easy comparison. > client.server_info.redis_version '2.3.0' > client.server_info.versions [ 2, 3, 0 ] ## redis.print() A handy callback function for displaying return values when testing. Example: ```js var redis = require("redis"), client = redis.createClient(); client.on("connect", function () { client.set("foo_rand000000000000", "some fantastic value", redis.print); client.get("foo_rand000000000000", redis.print); }); ``` This will print: Reply: OK Reply: some fantastic value Note that this program will not exit cleanly because the client is still connected. ## redis.debug_mode Boolean to enable debug mode and protocol tracing. ```js var redis = require("redis"), client = redis.createClient(); redis.debug_mode = true; client.on("connect", function () { client.set("foo_rand000000000000", "some fantastic value"); }); ``` This will display: mjr:~/work/node_redis (master)$ node ~/example.js send command: *3 $3 SET $20 foo_rand000000000000 $20 some fantastic value on_data: +OK `send command` is data sent into Redis and `on_data` is data received from Redis. ## Multi-word commands To execute redis multi-word commands like `SCRIPT LOAD` or `CLIENT LIST` pass the second word as first parameter: client.script('load', 'return 1'); client.multi().script('load', 'return 1').exec(...); client.multi([['script', 'load', 'return 1']]).exec(...); ## client.send_command(command_name, args, callback) Used internally to send commands to Redis. For convenience, nearly all commands that are published on the Redis Wiki have been added to the `client` object. However, if I missed any, or if new commands are introduced before this library is updated, you can use `send_command()` to send arbitrary commands to Redis. All commands are sent as multi-bulk commands. `args` can either be an Array of arguments, or omitted. ## client.connected Boolean tracking the state of the connection to the Redis server. ## client.command_queue.length The number of commands that have been sent to the Redis server but not yet replied to. You can use this to enforce some kind of maximum queue depth for commands while connected. Don't mess with `client.command_queue` though unless you really know what you are doing. ## client.offline_queue.length The number of commands that have been queued up for a future connection. You can use this to enforce some kind of maximum queue depth for pre-connection commands. ## client.retry_delay Current delay in milliseconds before a connection retry will be attempted. This starts at `250`. ## client.retry_backoff Multiplier for future retry timeouts. This should be larger than 1 to add more time between retries. Defaults to 1.7. The default initial connection retry is 250, so the second retry will be 425, followed by 723.5, etc. ### Commands with Optional and Keyword arguments This applies to anything that uses an optional `[WITHSCORES]` or `[LIMIT offset count]` in the [redis.io/commands](http://redis.io/commands) documentation. Example: ```js var args = [ 'myzset', 1, 'one', 2, 'two', 3, 'three', 99, 'ninety-nine' ]; client.zadd(args, function (err, response) { if (err) throw err; console.log('added '+response+' items.'); // -Infinity and +Infinity also work var args1 = [ 'myzset', '+inf', '-inf' ]; client.zrevrangebyscore(args1, function (err, response) { if (err) throw err; console.log('example1', response); // write your code here }); var max = 3, min = 1, offset = 1, count = 2; var args2 = [ 'myzset', max, min, 'WITHSCORES', 'LIMIT', offset, count ]; client.zrevrangebyscore(args2, function (err, response) { if (err) throw err; console.log('example2', response); // write your code here }); }); ``` ## TODO Better tests for auth, disconnect/reconnect, and all combinations thereof. Stream large set/get values into and out of Redis. Otherwise the entire value must be in node's memory. Performance can be better for very large values. I think there are more performance improvements left in there for smaller values, especially for large lists of small values. ## How to Contribute - open a pull request and then wait for feedback (if [DTrejo](http://github.com/dtrejo) does not get back to you within 2 days, comment again with indignation!) ## Contributors Some people have have added features and fixed bugs in `node_redis` other than me. Ordered by date of first contribution. [Auto-generated](http://github.com/dtrejo/node-authors) on Wed Jul 25 2012 19:14:59 GMT-0700 (PDT). - [Matt Ranney aka `mranney`](https://github.com/mranney) - [Tim-Smart aka `tim-smart`](https://github.com/tim-smart) - [Tj Holowaychuk aka `visionmedia`](https://github.com/visionmedia) - [rick aka `technoweenie`](https://github.com/technoweenie) - [Orion Henry aka `orionz`](https://github.com/orionz) - [Aivo Paas aka `aivopaas`](https://github.com/aivopaas) - [Hank Sims aka `hanksims`](https://github.com/hanksims) - [Paul Carey aka `paulcarey`](https://github.com/paulcarey) - [Pieter Noordhuis aka `pietern`](https://github.com/pietern) - [nithesh aka `nithesh`](https://github.com/nithesh) - [Andy Ray aka `andy2ray`](https://github.com/andy2ray) - [unknown aka `unknowdna`](https://github.com/unknowdna) - [Dave Hoover aka `redsquirrel`](https://github.com/redsquirrel) - [Vladimir Dronnikov aka `dvv`](https://github.com/dvv) - [Umair Siddique aka `umairsiddique`](https://github.com/umairsiddique) - [Louis-Philippe Perron aka `lp`](https://github.com/lp) - [Mark Dawson aka `markdaws`](https://github.com/markdaws) - [Ian Babrou aka `bobrik`](https://github.com/bobrik) - [Felix Geisendörfer aka `felixge`](https://github.com/felixge) - [Jean-Hugues Pinson aka `undefined`](https://github.com/undefined) - [Maksim Lin aka `maks`](https://github.com/maks) - [Owen Smith aka `orls`](https://github.com/orls) - [Zachary Scott aka `zzak`](https://github.com/zzak) - [TEHEK Firefox aka `TEHEK`](https://github.com/TEHEK) - [Isaac Z. Schlueter aka `isaacs`](https://github.com/isaacs) - [David Trejo aka `DTrejo`](https://github.com/DTrejo) - [Brian Noguchi aka `bnoguchi`](https://github.com/bnoguchi) - [Philip Tellis aka `bluesmoon`](https://github.com/bluesmoon) - [Marcus Westin aka `marcuswestin2`](https://github.com/marcuswestin2) - [Jed Schmidt aka `jed`](https://github.com/jed) - [Dave Peticolas aka `jdavisp3`](https://github.com/jdavisp3) - [Trae Robrock aka `trobrock`](https://github.com/trobrock) - [Shankar Karuppiah aka `shankar0306`](https://github.com/shankar0306) - [Ignacio Burgueño aka `ignacio`](https://github.com/ignacio) Thanks. ## LICENSE - "MIT License" Copyright (c) 2010 Matthew Ranney, http://ranney.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ![spacer](http://ranney.com/1px.gif) node_redis-0.12.1/benches/000077500000000000000000000000001237217724200153245ustar00rootroot00000000000000node_redis-0.12.1/benches/buffer_bench.js000066400000000000000000000044651237217724200203030ustar00rootroot00000000000000var source = new Buffer(100), dest = new Buffer(100), i, j, k, tmp, count = 1000000, bytes = 100; for (i = 99 ; i >= 0 ; i--) { source[i] = 120; } var str = "This is a nice String.", buf = new Buffer("This is a lovely Buffer."); var start = new Date(); for (i = count * 100; i > 0 ; i--) { if (Buffer.isBuffer(str)) {} } var end = new Date(); console.log("Buffer.isBuffer(str) " + (end - start) + " ms"); var start = new Date(); for (i = count * 100; i > 0 ; i--) { if (Buffer.isBuffer(buf)) {} } var end = new Date(); console.log("Buffer.isBuffer(buf) " + (end - start) + " ms"); var start = new Date(); for (i = count * 100; i > 0 ; i--) { if (str instanceof Buffer) {} } var end = new Date(); console.log("str instanceof Buffer " + (end - start) + " ms"); var start = new Date(); for (i = count * 100; i > 0 ; i--) { if (buf instanceof Buffer) {} } var end = new Date(); console.log("buf instanceof Buffer " + (end - start) + " ms"); for (i = bytes ; i > 0 ; i --) { var start = new Date(); for (j = count ; j > 0; j--) { tmp = source.toString("ascii", 0, bytes); } var end = new Date(); console.log("toString() " + i + " bytes " + (end - start) + " ms"); } for (i = bytes ; i > 0 ; i --) { var start = new Date(); for (j = count ; j > 0; j--) { tmp = ""; for (k = 0; k <= i ; k++) { tmp += String.fromCharCode(source[k]); } } var end = new Date(); console.log("manual string " + i + " bytes " + (end - start) + " ms"); } for (i = bytes ; i > 0 ; i--) { var start = new Date(); for (j = count ; j > 0 ; j--) { for (k = i ; k > 0 ; k--) { dest[k] = source[k]; } } var end = new Date(); console.log("Manual copy " + i + " bytes " + (end - start) + " ms"); } for (i = bytes ; i > 0 ; i--) { var start = new Date(); for (j = count ; j > 0 ; j--) { for (k = i ; k > 0 ; k--) { dest[k] = 120; } } var end = new Date(); console.log("Direct assignment " + i + " bytes " + (end - start) + " ms"); } for (i = bytes ; i > 0 ; i--) { var start = new Date(); for (j = count ; j > 0 ; j--) { source.copy(dest, 0, 0, i); } var end = new Date(); console.log("Buffer.copy() " + i + " bytes " + (end - start) + " ms"); } node_redis-0.12.1/benches/hiredis_parser.js000066400000000000000000000014551237217724200206720ustar00rootroot00000000000000var Parser = require('../lib/parser/hiredis').Parser; var assert = require('assert'); /* This test makes sure that exceptions thrown inside of "reply" event handlers are not trapped and mistakenly emitted as parse errors. */ (function testExecuteDoesNotCatchReplyCallbackExceptions() { var parser = new Parser(); var replies = [{}]; parser.reader = { feed: function() {}, get: function() { return replies.shift(); } }; var emittedError = false; var caughtException = false; parser .on('error', function() { emittedError = true; }) .on('reply', function() { throw new Error('bad'); }); try { parser.execute(); } catch (err) { caughtException = true; } assert.equal(caughtException, true); assert.equal(emittedError, false); })(); node_redis-0.12.1/benches/re_sub_test.js000066400000000000000000000005371237217724200202050ustar00rootroot00000000000000var client = require('../index').createClient() , client2 = require('../index').createClient() , assert = require('assert'); client.once('subscribe', function (channel, count) { client.unsubscribe('x'); client.subscribe('x', function () { client.quit(); client2.quit(); }); client2.publish('x', 'hi'); }); client.subscribe('x'); node_redis-0.12.1/benches/reconnect_test.js000066400000000000000000000012731237217724200207040ustar00rootroot00000000000000var redis = require("../index").createClient(null, null, { // max_attempts: 4 }); redis.on("error", function (err) { console.log("Redis says: " + err); }); redis.on("ready", function () { console.log("Redis ready."); }); redis.on("reconnecting", function (arg) { console.log("Redis reconnecting: " + JSON.stringify(arg)); }); redis.on("connect", function () { console.log("Redis connected."); }); setInterval(function () { var now = Date.now(); redis.set("now", now, function (err, res) { if (err) { console.log(now + " Redis reply error: " + err); } else { console.log(now + " Redis reply: " + res); } }); }, 100); node_redis-0.12.1/benches/stress/000077500000000000000000000000001237217724200166475ustar00rootroot00000000000000node_redis-0.12.1/benches/stress/codec.js000066400000000000000000000004671237217724200202710ustar00rootroot00000000000000var json = { encode: JSON.stringify, decode: JSON.parse }; var MsgPack = require('node-msgpack'); msgpack = { encode: MsgPack.pack, decode: function(str) { return MsgPack.unpack(new Buffer(str)); } }; bison = require('bison'); module.exports = json; //module.exports = msgpack; //module.exports = bison; node_redis-0.12.1/benches/stress/pubsub/000077500000000000000000000000001237217724200201475ustar00rootroot00000000000000node_redis-0.12.1/benches/stress/pubsub/pub.js000066400000000000000000000015441237217724200212770ustar00rootroot00000000000000'use strict'; var freemem = require('os').freemem; var profiler = require('v8-profiler'); var codec = require('../codec'); var sent = 0; var pub = require('redis').createClient(null, null, { //command_queue_high_water: 5, //command_queue_low_water: 1 }) .on('ready', function() { this.emit('drain'); }) .on('drain', function() { process.nextTick(exec); }); var payload = '1'; for (var i = 0; i < 12; ++i) payload += payload; console.log('Message payload length', payload.length); function exec() { pub.publish('timeline', codec.encode({ foo: payload })); ++sent; if (!pub.should_buffer) { process.nextTick(exec); } } profiler.takeSnapshot('s_0'); exec(); setInterval(function() { profiler.takeSnapshot('s_' + sent); console.error('sent', sent, 'free', freemem(), 'cmdqlen', pub.command_queue.length, 'offqlen', pub.offline_queue.length); }, 2000); node_redis-0.12.1/benches/stress/pubsub/run000077500000000000000000000002461237217724200207030ustar00rootroot00000000000000#!/bin/sh node server.js & node server.js & node server.js & node server.js & node server.js & node server.js & node server.js & node server.js & node --debug pub.js node_redis-0.12.1/benches/stress/pubsub/server.js000066400000000000000000000007201237217724200220120ustar00rootroot00000000000000'use strict'; var freemem = require('os').freemem; var codec = require('../codec'); var id = Math.random(); var recv = 0; var sub = require('redis').createClient() .on('ready', function() { this.subscribe('timeline'); }) .on('message', function(channel, message) { var self = this; if (message) { message = codec.decode(message); ++recv; } }); setInterval(function() { console.error('id', id, 'received', recv, 'free', freemem()); }, 2000); node_redis-0.12.1/benches/stress/rpushblpop/000077500000000000000000000000001237217724200210455ustar00rootroot00000000000000node_redis-0.12.1/benches/stress/rpushblpop/pub.js000066400000000000000000000020601237217724200221670ustar00rootroot00000000000000'use strict'; var freemem = require('os').freemem; //var profiler = require('v8-profiler'); var codec = require('../codec'); var sent = 0; var pub = require('redis').createClient(null, null, { //command_queue_high_water: 5, //command_queue_low_water: 1 }) .on('ready', function() { this.del('timeline'); this.emit('drain'); }) .on('drain', function() { process.nextTick(exec); }); var payload = '1'; for (var i = 0; i < 12; ++i) payload += payload; console.log('Message payload length', payload.length); function exec() { pub.rpush('timeline', codec.encode({ foo: payload })); ++sent; if (!pub.should_buffer) { process.nextTick(exec); } } //profiler.takeSnapshot('s_0'); exec(); setInterval(function() { //var ss = profiler.takeSnapshot('s_' + sent); //console.error(ss.stringify()); pub.llen('timeline', function(err, result) { console.error('sent', sent, 'free', freemem(), 'cmdqlen', pub.command_queue.length, 'offqlen', pub.offline_queue.length, 'llen', result ); }); }, 2000); /*setTimeout(function() { process.exit(); }, 30000);*/ node_redis-0.12.1/benches/stress/rpushblpop/run000077500000000000000000000001451237217724200215770ustar00rootroot00000000000000#!/bin/sh node server.js & #node server.js & #node server.js & #node server.js & node --debug pub.js node_redis-0.12.1/benches/stress/rpushblpop/server.js000066400000000000000000000012371237217724200227140ustar00rootroot00000000000000'use strict'; var freemem = require('os').freemem; var codec = require('../codec'); var id = Math.random(); var recv = 0; var cmd = require('redis').createClient(); var sub = require('redis').createClient() .on('ready', function() { this.emit('timeline'); }) .on('timeline', function() { var self = this; this.blpop('timeline', 0, function(err, result) { var message = result[1]; if (message) { message = codec.decode(message); ++recv; } self.emit('timeline'); }); }); setInterval(function() { cmd.llen('timeline', function(err, result) { console.error('id', id, 'received', recv, 'free', freemem(), 'llen', result); }); }, 2000); node_redis-0.12.1/benches/stress/speed/000077500000000000000000000000001237217724200177475ustar00rootroot00000000000000node_redis-0.12.1/benches/stress/speed/00000066400000000000000000000006041237217724200201110ustar00rootroot00000000000000# size JSON msgpack bison 26602 2151.0170848180414 25542 ? 2842.589272665782 24835 ? ? 7280.4538397469805 6104 6985.234528557929 5045 ? 7217.461392841478 4341 ? ? 14261.406335354604 4180 15864.633685636572 4143 ? 12954.806235781925 4141 ? ? 44650.70733912719 75 114227.07313350472 40 ? 30162.440062810834 39 ? ? 119815.66013519121 node_redis-0.12.1/benches/stress/speed/plot000077500000000000000000000005721237217724200206570ustar00rootroot00000000000000#!/bin/sh gnuplot >size-rate.jpg << _EOF_ set terminal png nocrop enhanced font verdana 12 size 640,480 set logscale x set logscale y set grid set xlabel 'Serialized object size, octets' set ylabel 'decode(encode(obj)) rate, 1/sec' plot '00' using 1:2 title 'json' smooth bezier, '00' using 1:3 title 'msgpack' smooth bezier, '00' using 1:4 title 'bison' smooth bezier _EOF_ node_redis-0.12.1/benches/stress/speed/size-rate.png000066400000000000000000000150201237217724200223560ustar00rootroot00000000000000PNG  IHDR,2PLTE@Ai @0`@ԥ**@333MMMfff22U݂d"".Wp͇PErz挽k ܠ ݠݐP@Uk/@@``@@`pͷ|@ ___???RY\IDATx * EVQPtSxB n]mt\+~Z%VGjUA˷аM\ڻjzQߵ_r[ڻ)!8x0ʭ]ygy]_"kDۻiWC(@Ih#wZu7?EPT:=J񿆃eSˑeL9`gGjEyW~xX`ώ3.Bx zs&[73_ŗ^N㼌b .w$tC *ࣣK *,n4QwF[l29=: I2nl"Eݤo'SS-+(^C[nz N} }^ŬH6:+1B D#M-.0iß?sTr,`$hC4D!i B("* x ?cutM4Á~]+zuhU ^%rWt8Uѷba~,)Iyh1~,ߕޫ%mn~C>pOG0}7/{t:X}믢vl H Hu xhC4`: D!in4r@.@&,Ѐ[u xhC4X(H=R U,QwF[;D#  K"4 V@D!in4r@.@&,Ѐ[u xhC4 r@nK..RwdHU[qְ@\tr.R/j@\LEo8.8.pP`=m3QwF9 ,#Rm$br"[o цXr7{<6, 'Av.rB\r@wgX @.Gu-cLDd#Xu xhcWE5& !, s$<`qd$VZ ]p3!OZi/D r@.mj @.!@Pe"iE0? @.D2!@.F:r!ҢmPXlt^|ЇsAԭ(XKÌ h@ԭHJ0'9=qs{$6 v`7Xw *,4zE5K&+.JIa/%ԃgB)\y7$qI+hSJvn80DJ uYp7,>^,AHo =/yLTt'V _EۿݮP4V֣fo pI950*Ȓ毕h X=u T(0l1ВmۃxnmtSMj\R [%p'A^8cכM2prGp)AWF`r,G*Do/Z'?8ZQȡ 9`x.A6plcmF-\䂎p~8x9h@>q{Oa˱֧;Y7n$AʙnˍD Ѧt<5ETn̜9_)\A8¹4 mXtHWeoka܁9H]GWNI3R`T~"S@a[AAˉhSRÑ'>h?-'T ; 0!/ʱ;cV#!$H6Wa$>Koִ: ZsAj5ʼn-Ai,ϖ5DIQ9 {R?('Ts@CJ䇩h|S`2F[37H40I"=-K)G@8W`.)kK.H-S"@3vTza2DKQjRx8BGujMDl&cAԭP\! VQ $ _ 0|^70_ 0T'/Xҹ;|^7 ܏= au9`L> %H|%R 'Y&%z*@I4&s@E""ڄO TDjҾ+] 0%z[gF9`"t7S KuyVDBiT`4 4QX0ƒ HÍL?9`J"*l 4KgF +hŇE%H4Dbp` 4r䬏U hRg(Յ(hŌ $inqcP$i4,H K^.D S` 4Q8[ҿF[,/[D#J:!B%TyоFhŖz2Hu1H4#Ў" K1F ^j?F[ @&w(hĿ!@|mڻ!@Ph߷F[xSGF[@UD#2>i Ae} $ino $in_aFH? MX 0iTn4W9  @Dh@ԭb C4D!i\MX 9Q4 Hu xhr4a)@D*Ѐ[;D#M-FЄuX @n4QwF9  @Dh@ԭb C4D!i\MX 9Q4 Hu xhr4a)@D*(R+kٞ$p &R[َ:' ɉFLֵAHgtKR /BqX2[ڭӓ7c>:XÑe H[ (E&7Q9; $gc;Y 8Df#ط|L!Yƍc~">ѬwVF~ϱt?mShֻ}+qc(Jd6D%jfCo4u Ҕ^&1= ߺ ߺnfP0͆h4FGfؿ2MI~"'zLY! 6yQ}WJ4r)37NWiDwbOHG-Ѭ+7NZDi՞nn=yPPLrJ^8nȑez\ߊA>՞I j}ABv87ͣ݅>ȑN6:!VXt"f{nxpF]S_^X.kꈶcW(}5; .LLs>~nI^/~~wB [D;4)r}G!T\8] 8uN/`\tnwB cdiw_Q[Q&Ʃs (E55Kˁyj}^X/yU}!#S?xH.T}r@}0^V{.>\x"z,ȆVJQosؗS(X MnSՀaw^\t<]Qpt<_MF|YSխ\OJ;r6\9Ʌ͘>?/{(Тl ԉd%6+NIENDB`node_redis-0.12.1/benches/stress/speed/speed.js000066400000000000000000000027731237217724200214160ustar00rootroot00000000000000var msgpack = require('node-msgpack'); var bison = require('bison'); var codec = { JSON: { encode: JSON.stringify, decode: JSON.parse }, msgpack: { encode: msgpack.pack, decode: msgpack.unpack }, bison: bison }; var obj, l; var s = '0'; for (var i = 0; i < 12; ++i) s += s; obj = { foo: s, arrrrrr: [{a:1,b:false,c:null,d:1.0}, 1111, 2222, 33333333], rand: [], a: s, ccc: s, b: s + s + s }; for (i = 0; i < 100; ++i) obj.rand.push(Math.random()); forObj(obj); obj = { foo: s, arrrrrr: [{a:1,b:false,c:null,d:1.0}, 1111, 2222, 33333333], rand: [] }; for (i = 0; i < 100; ++i) obj.rand.push(Math.random()); forObj(obj); obj = { foo: s, arrrrrr: [{a:1,b:false,c:null,d:1.0}, 1111, 2222, 33333333], rand: [] }; forObj(obj); obj = { arrrrrr: [{a:1,b:false,c:null,d:1.0}, 1111, 2222, 33333333], rand: [] }; forObj(obj); function run(obj, codec) { var t1 = Date.now(); var n = 10000; for (var i = 0; i < n; ++i) { codec.decode(l = codec.encode(obj)); } var t2 = Date.now(); //console.log('DONE', n*1000/(t2-t1), 'codecs/sec, length=', l.length); return [n*1000/(t2-t1), l.length]; } function series(obj, cname, n) { var rate = 0; var len = 0; for (var i = 0; i < n; ++i) { var r = run(obj, codec[cname]); rate += r[0]; len += r[1]; } rate /= n; len /= n; console.log(cname + ' ' + rate + ' ' + len); return [rate, len]; } function forObj(obj) { var r = { JSON: series(obj, 'JSON', 20), msgpack: series(obj, 'msgpack', 20), bison: series(obj, 'bison', 20) }; return r; } node_redis-0.12.1/benches/sub_quit_test.js000066400000000000000000000007731237217724200205630ustar00rootroot00000000000000var client = require("redis").createClient(), client2 = require("redis").createClient(); client.subscribe("something"); client.on("subscribe", function (channel, count) { console.log("Got sub: " + channel); client.unsubscribe("something"); }); client.on("unsubscribe", function (channel, count) { console.log("Got unsub: " + channel + ", quitting"); client.quit(); }); // exercise unsub before sub client2.unsubscribe("something"); client2.subscribe("another thing"); client2.quit(); node_redis-0.12.1/changelog.md000066400000000000000000000254031237217724200161720ustar00rootroot00000000000000Changelog ========= ## v0.12.0 - Aug 9, 2014 * Fix unix socket support (Jack Tang) * Improve createClient argument handling (Jack Tang) ## v0.11.0 - Jul 10, 2014 * IPv6 Support. (Yann Stephan) * Revert error emitting and go back to throwing errors. (Bryce Baril) * Set socket_keepalive to prevent long-lived client timeouts. (mohit) * Correctly reset retry timer. (ouotuo) * Domains protection from bad user exit. (Jake Verbaten) * Fix reconnection socket logic to prevent misqueued entries. (Iain Proctor) ## v0.10.3 - May 22, 2014 * Update command list to match Redis 2.8.9 (Charles Feng) ## v0.10.2 - May 18, 2014 * Better binay key handlign for HGETALL. (Nick Apperson) * Fix test not resetting `error` handler. (CrypticSwarm) * Fix SELECT error semantics. (Bryan English) ## v0.10.1 - February 17, 2014 * Skip plucking redis version from the INFO stream if INFO results weren't provided. (Robert Sköld) ## v0.10.0 - December 21, 2013 * Instead of throwing errors asynchronously, emit errors on client. (Bryce Baril) ## v0.9.2 - December 15, 2013 * Regenerate commands for new 2.8.x Redis commands. (Marek Ventur) * Correctly time reconnect counts when using 'auth'. (William Hockey) ## v0.9.1 - November 23, 2013 * Allow hmset to accept numeric keys. (Alex Stokes) * Fix TypeError for multiple MULTI/EXEC errors. (Kwangsu Kim) ## v0.9.0 - October 17, 2013 * Domains support. (Forrest L Norvell) ## v0.8.6 - October 2, 2013 * If error is already an Error, don't wrap it in another Error. (Mathieu M-Gosselin) * Fix retry delay logic (Ian Babrou) * Return Errors instead of strings where Errors are expected (Ian Babrou) * Add experimental `.unref()` method to RedisClient (Bryce Baril / Olivier Lalonde) * Strengthen checking of reply to prevent conflating "message" or "pmessage" fields with pub_sub replies. (Bryce Baril) ## v0.8.5 - September 26, 2013 * Add `auth_pass` option to connect and immediately authenticate (Henrik Peinar) ## v0.8.4 - June 24, 2013 Many contributed features and fixes, including: * Ignore password set if not needed. (jbergknoff) * Improved compatibility with 0.10.X for tests and client.end() (Bryce Baril) * Protect connection retries from application exceptions. (Amos Barreto) * Better exception handling for Multi/Exec (Thanasis Polychronakis) * Renamed pubsub mode to subscriber mode (Luke Plaster) * Treat SREM like SADD when passed an array (Martin Ciparelli) * Fix empty unsub/punsub TypeError (Jeff Barczewski) * Only attempt to run a callback if it one was provided (jifeng) ## v0.8.3 - April 09, 2013 Many contributed features and fixes, including: * Fix some tests for Node.js version 0.9.x+ changes (Roman Ivanilov) * Fix error when commands submitted after idle event handler (roamm) * Bypass Redis for no-op SET/SETEX commands (jifeng) * Fix HMGET + detect_buffers (Joffrey F) * Fix CLIENT LOAD functionality (Jonas Dohse) * Add percentage outputs to diff_multi_bench_output.js (Bryce Baril) * Add retry_max_delay option (Tomasz Durka) * Fix parser off-by-one errors with nested multi-bulk replies (Bryce Baril) * Prevent parser from sinking application-side exceptions (Bryce Baril) * Fix parser incorrect buffer skip when parsing multi-bulk errors (Bryce Baril) * Reverted previous change with throwing on non-string values with HMSET (David Trejo) * Fix command queue sync issue when using pubsub (Tom Leach) * Fix compatibility with two-word Redis commands (Jonas Dohse) * Add EVAL with array syntax (dmoena) * Fix tests due to Redis reply order changes in 2.6.5+ (Bryce Baril) * Added a test for the SLOWLOG command (Nitesh Sinha) * Fix SMEMBERS order dependency in test broken by Redis changes (Garrett Johnson) * Update commands for new Redis commands (David Trejo) * Prevent exception from SELECT on subscriber reconnection (roamm) ## v0.8.2 - November 11, 2012 Another version bump because 0.8.1 didn't get applied properly for some mysterious reason. Sorry about that. Changed name of "faster" parser to "javascript". ## v0.8.1 - September 11, 2012 Important bug fix for null responses (Jerry Sievert) ## v0.8.0 - September 10, 2012 Many contributed features and fixes, including: * Pure JavaScript reply parser that is usually faster than hiredis (Jerry Sievert) * Remove hiredis as optionalDependency from package.json. It still works if you want it. * Restore client state on reconnect, including select, subscribe, and monitor. (Ignacio Burgueño) * Fix idle event (Trae Robrock) * Many documentation improvements and bug fixes (David Trejo) ## v0.7.2 - April 29, 2012 Many contributed fixes. Thank you, contributors. * [GH-190] - pub/sub mode fix (Brian Noguchi) * [GH-165] - parser selection fix (TEHEK) * numerous documentation and examples updates * auth errors emit Errors instead of Strings (David Trejo) ## v0.7.1 - November 15, 2011 Fix regression in reconnect logic. Very much need automated tests for reconnection and queue logic. ## v0.7.0 - November 14, 2011 Many contributed fixes. Thanks everybody. * [GH-127] - properly re-initialize parser on reconnect * [GH-136] - handle passing undefined as callback (Ian Babrou) * [GH-139] - properly handle exceptions thrown in pub/sub event handlers (Felix Geisendörfer) * [GH-141] - detect closing state on stream error (Felix Geisendörfer) * [GH-142] - re-select database on reconnection (Jean-Hugues Pinson) * [GH-146] - add sort example (Maksim Lin) Some more goodies: * Fix bugs with node 0.6 * Performance improvements * New version of `multi_bench.js` that tests more realistic scenarios * [GH-140] - support optional callback for subscribe commands * Properly flush and error out command queue when connection fails * Initial work on reconnection thresholds ## v0.6.7 - July 30, 2011 (accidentally skipped v0.6.6) Fix and test for [GH-123] Passing an Array as as the last argument should expand as users expect. The old behavior was to coerce the arguments into Strings, which did surprising things with Arrays. ## v0.6.5 - July 6, 2011 Contributed changes: * Support SlowBuffers (Umair Siddique) * Add Multi to exports (Louis-Philippe Perron) * Fix for drain event calculation (Vladimir Dronnikov) Thanks! ## v0.6.4 - June 30, 2011 Fix bug with optional callbacks for hmset. ## v0.6.2 - June 30, 2011 Bugs fixed: * authentication retry while server is loading db (danmaz74) [GH-101] * command arguments processing issue with arrays New features: * Auto update of new commands from redis.io (Dave Hoover) * Performance improvements and backpressure controls. * Commands now return the true/false value from the underlying socket write(s). * Implement command_queue high water and low water for more better control of queueing. See `examples/backpressure_drain.js` for more information. ## v0.6.1 - June 29, 2011 Add support and tests for Redis scripting through EXEC command. Bug fix for monitor mode. (forddg) Auto update of new commands from redis.io (Dave Hoover) ## v0.6.0 - April 21, 2011 Lots of bugs fixed. * connection error did not properly trigger reconnection logic [GH-85] * client.hmget(key, [val1, val2]) was not expanding properly [GH-66] * client.quit() while in pub/sub mode would throw an error [GH-87] * client.multi(['hmset', 'key', {foo: 'bar'}]) fails [GH-92] * unsubscribe before subscribe would make things very confused [GH-88] * Add BRPOPLPUSH [GH-79] ## v0.5.11 - April 7, 2011 Added DISCARD I originally didn't think DISCARD would do anything here because of the clever MULTI interface, but somebody pointed out to me that DISCARD can be used to flush the WATCH set. ## v0.5.10 - April 6, 2011 Added HVALS ## v0.5.9 - March 14, 2011 Fix bug with empty Array arguments - Andy Ray ## v0.5.8 - March 14, 2011 Add `MONITOR` command and special monitor command reply parsing. ## v0.5.7 - February 27, 2011 Add magical auth command. Authentication is now remembered by the client and will be automatically sent to the server on every connection, including any reconnections. ## v0.5.6 - February 22, 2011 Fix bug in ready check with `return_buffers` set to `true`. Thanks to Dean Mao and Austin Chau. ## v0.5.5 - February 16, 2011 Add probe for server readiness. When a Redis server starts up, it might take a while to load the dataset into memory. During this time, the server will accept connections, but will return errors for all non-INFO commands. Now node_redis will send an INFO command whenever it connects to a server. If the info command indicates that the server is not ready, the client will keep trying until the server is ready. Once it is ready, the client will emit a "ready" event as well as the "connect" event. The client will queue up all commands sent before the server is ready, just like it did before. When the server is ready, all offline/non-ready commands will be replayed. This should be backward compatible with previous versions. To disable this ready check behavior, set `options.no_ready_check` when creating the client. As a side effect of this change, the key/val params from the info command are available as `client.server_options`. Further, the version string is decomposed into individual elements in `client.server_options.versions`. ## v0.5.4 - February 11, 2011 Fix excess memory consumption from Queue backing store. Thanks to Gustaf Sjöberg. ## v0.5.3 - February 5, 2011 Fix multi/exec error reply callback logic. Thanks to Stella Laurenzo. ## v0.5.2 - January 18, 2011 Fix bug where unhandled error replies confuse the parser. ## v0.5.1 - January 18, 2011 Fix bug where subscribe commands would not handle redis-server startup error properly. ## v0.5.0 - December 29, 2010 Some bug fixes: * An important bug fix in reconnection logic. Previously, reply callbacks would be invoked twice after a reconnect. * Changed error callback argument to be an actual Error object. New feature: * Add friendly syntax for HMSET using an object. ## v0.4.1 - December 8, 2010 Remove warning about missing hiredis. You probably do want it though. ## v0.4.0 - December 5, 2010 Support for multiple response parsers and hiredis C library from Pieter Noordhuis. Return Strings instead of Buffers by default. Empty nested mb reply bug fix. ## v0.3.9 - November 30, 2010 Fix parser bug on failed EXECs. ## v0.3.8 - November 10, 2010 Fix for null MULTI response when WATCH condition fails. ## v0.3.7 - November 9, 2010 Add "drain" and "idle" events. ## v0.3.6 - November 3, 2010 Add all known Redis commands from Redis master, even ones that are coming in 2.2 and beyond. Send a friendlier "error" event message on stream errors like connection refused / reset. ## v0.3.5 - October 21, 2010 A few bug fixes. * Fixed bug with `nil` multi-bulk reply lengths that showed up with `BLPOP` timeouts. * Only emit `end` once when connection goes away. * Fixed bug in `test.js` where driver finished before all tests completed. ## unversioned wasteland See the git history for what happened before. node_redis-0.12.1/connection_breaker.js000066400000000000000000000032571237217724200201140ustar00rootroot00000000000000var net = require('net'); var proxyPort = 6379; var counter = 0; function breaker(conn) { conn.end(); conn.destroy(); } var server = net.createServer(function(conn) { counter++; var proxyConn = net.createConnection({ port: proxyPort }); conn.pipe(proxyConn); proxyConn.pipe(conn); proxyConn.on('end', function() { conn.end(); }); conn.on('end', function() { proxyConn.end(); }); conn.on('close', function() { proxyConn.end(); }); proxyConn.on('close', function() { conn.end(); }); proxyConn.on('error', function() { conn.end(); }); conn.on('error', function() { proxyConn.end(); }); setTimeout(breaker.bind(null, conn), Math.floor(Math.random() * 2000)); }); server.listen(6479); var redis = require('./'); var port = 6479; var client = redis.createClient(6479, 'localhost'); function iter() { var k = "k" + Math.floor(Math.random() * 10); var coinflip = Math.random() > 0.5; if (coinflip) { client.set(k, k, function(err, resp) { if (!err && resp !== "OK") { console.log("Unexpected set response " + resp); } }); } else { client.get(k, function(err, resp) { if (!err) { if (k !== resp) { console.log("Key response mismatch: " + k + " " + resp); } } }); } } function iters() { for (var i = 0; i < 100; ++i) { iter(); } setTimeout(iters, 10); } client.on("connect", function () { iters(); }); client.on("error", function (err) { console.log("Client error " + err); }); node_redis-0.12.1/diff_multi_bench_output.js000077500000000000000000000050471237217724200211650ustar00rootroot00000000000000#!/usr/bin/env node var colors = require('colors'), fs = require('fs'), _ = require('underscore'), metrics = require('metrics'), // `node diff_multi_bench_output.js before.txt after.txt` before = process.argv[2], after = process.argv[3]; if (!before || !after) { console.log('Please supply two file arguments:'); var n = __filename; n = n.substring(n.lastIndexOf('/', n.length)); console.log(' ./' + n + ' multiBenchBefore.txt multiBenchAfter.txt'); console.log('To generate multiBenchBefore.txt, run'); console.log(' node multi_bench.js > multiBenchBefore.txt'); console.log('Thank you for benchmarking responsibly.'); return; } var before_lines = fs.readFileSync(before, 'utf8').split('\n'), after_lines = fs.readFileSync(after, 'utf8').split('\n'); console.log('Comparing before,', before.green, '(', before_lines.length, 'lines)', 'to after,', after.green, '(', after_lines.length, 'lines)'); var total_ops = new metrics.Histogram.createUniformHistogram(); before_lines.forEach(function(b, i) { var a = after_lines[i]; if (!a || !b || !b.trim() || !a.trim()) { // console.log('#ignored#', '>'+a+'<', '>'+b+'<'); return; } b_words = b.split(' ').filter(is_whitespace); a_words = a.split(' ').filter(is_whitespace); var ops = [b_words, a_words] .map(function(words) { // console.log(words); return parseInt10(words.slice(-2, -1)); }).filter(function(num) { var isNaN = !num && num !== 0; return !isNaN; }); if (ops.length != 2) return var delta = ops[1] - ops[0]; var pct = ((delta / ops[0]) * 100).toPrecision(3); total_ops.update(delta); delta = humanize_diff(delta); pct = humanize_diff(pct, '%'); console.log( // name of test command_name(a_words) == command_name(b_words) ? command_name(a_words) + ':' : '404:', // results of test ops.join(' -> '), 'ops/sec (∆', delta, pct, ')'); }); console.log('Mean difference in ops/sec:', humanize_diff(total_ops.mean().toPrecision(6))); function is_whitespace(s) { return !!s.trim(); } function parseInt10(s) { return parseInt(s, 10); } // green if greater than 0, red otherwise function humanize_diff(num, unit) { unit = unit || ""; if (num > 0) { return ('+' + num + unit).green; } return ('' + num + unit).red; } function command_name(words) { var line = words.join(' '); return line.substr(0, line.indexOf(',')); } node_redis-0.12.1/examples/000077500000000000000000000000001237217724200155335ustar00rootroot00000000000000node_redis-0.12.1/examples/auth.js000066400000000000000000000002661237217724200170360ustar00rootroot00000000000000var redis = require("redis"), client = redis.createClient(); // This command is magical. Client stashes the password and will issue on every connect. client.auth("somepass"); node_redis-0.12.1/examples/backpressure_drain.js000066400000000000000000000014521237217724200217410ustar00rootroot00000000000000var redis = require("../index"), client = redis.createClient(null, null, { command_queue_high_water: 5, command_queue_low_water: 1 }), remaining_ops = 100000, paused = false; function op() { if (remaining_ops <= 0) { console.error("Finished."); process.exit(0); } remaining_ops--; if (client.hset("test hash", "val " + remaining_ops, remaining_ops) === false) { console.log("Pausing at " + remaining_ops); paused = true; } else { process.nextTick(op); } } client.on("drain", function () { if (paused) { console.log("Resuming at " + remaining_ops); paused = false; process.nextTick(op); } else { console.log("Got drain while not paused at " + remaining_ops); } }); op(); node_redis-0.12.1/examples/eval.js000066400000000000000000000004561237217724200170250ustar00rootroot00000000000000var redis = require("../index"), client = redis.createClient(); redis.debug_mode = true; client.eval("return 100.5", 0, function (err, res) { console.dir(err); console.dir(res); }); client.eval([ "return 100.5", 0 ], function (err, res) { console.dir(err); console.dir(res); }); node_redis-0.12.1/examples/extend.js000066400000000000000000000012331237217724200173570ustar00rootroot00000000000000var redis = require("redis"), client = redis.createClient(); // Extend the RedisClient prototype to add a custom method // This one converts the results from "INFO" into a JavaScript Object redis.RedisClient.prototype.parse_info = function (callback) { this.info(function (err, res) { var lines = res.toString().split("\r\n").sort(); var obj = {}; lines.forEach(function (line) { var parts = line.split(':'); if (parts[1]) { obj[parts[0]] = parts[1]; } }); callback(obj) }); }; client.parse_info(function (info) { console.dir(info); client.quit(); }); node_redis-0.12.1/examples/file.js000066400000000000000000000021221237217724200170050ustar00rootroot00000000000000// Read a file from disk, store it in Redis, then read it back from Redis. var redis = require("redis"), client = redis.createClient(), fs = require("fs"), filename = "kids_in_cart.jpg"; // Get the file I use for testing like this: // curl http://ranney.com/kids_in_cart.jpg -o kids_in_cart.jpg // or just use your own file. // Read a file from fs, store it in Redis, get it back from Redis, write it back to fs. fs.readFile(filename, function (err, data) { if (err) throw err console.log("Read " + data.length + " bytes from filesystem."); client.set(filename, data, redis.print); // set entire file client.get(filename, function (err, reply) { // get entire file if (err) { console.log("Get error: " + err); } else { fs.writeFile("duplicate_" + filename, reply, function (err) { if (err) { console.log("Error on write: " + err) } else { console.log("File written."); } client.end(); }); } }); }); node_redis-0.12.1/examples/mget.js000066400000000000000000000002331237217724200170230ustar00rootroot00000000000000var client = require("redis").createClient(); client.mget(["sessions started", "sessions started", "foo"], function (err, res) { console.dir(res); });node_redis-0.12.1/examples/monitor.js000066400000000000000000000004131237217724200175560ustar00rootroot00000000000000var client = require("../index").createClient(), util = require("util"); client.monitor(function (err, res) { console.log("Entering monitoring mode."); }); client.on("monitor", function (time, args) { console.log(time + ": " + util.inspect(args)); }); node_redis-0.12.1/examples/multi.js000066400000000000000000000023521237217724200172250ustar00rootroot00000000000000var redis = require("redis"), client = redis.createClient(), set_size = 20; client.sadd("bigset", "a member"); client.sadd("bigset", "another member"); while (set_size > 0) { client.sadd("bigset", "member " + set_size); set_size -= 1; } // multi chain with an individual callback client.multi() .scard("bigset") .smembers("bigset") .keys("*", function (err, replies) { client.mget(replies, redis.print); }) .dbsize() .exec(function (err, replies) { console.log("MULTI got " + replies.length + " replies"); replies.forEach(function (reply, index) { console.log("Reply " + index + ": " + reply.toString()); }); }); client.mset("incr thing", 100, "incr other thing", 1, redis.print); // start a separate multi command queue var multi = client.multi(); multi.incr("incr thing", redis.print); multi.incr("incr other thing", redis.print); // runs immediately client.get("incr thing", redis.print); // 100 // drains multi queue and runs atomically multi.exec(function (err, replies) { console.log(replies); // 101, 2 }); // you can re-run the same transaction if you like multi.exec(function (err, replies) { console.log(replies); // 102, 3 client.quit(); }); node_redis-0.12.1/examples/multi2.js000066400000000000000000000014101237217724200173010ustar00rootroot00000000000000var redis = require("redis"), client = redis.createClient(), multi; // start a separate command queue for multi multi = client.multi(); multi.incr("incr thing", redis.print); multi.incr("incr other thing", redis.print); // runs immediately client.mset("incr thing", 100, "incr other thing", 1, redis.print); // drains multi queue and runs atomically multi.exec(function (err, replies) { console.log(replies); // 101, 2 }); // you can re-run the same transaction if you like multi.exec(function (err, replies) { console.log(replies); // 102, 3 client.quit(); }); client.multi([ ["mget", "multifoo", "multibar", redis.print], ["incr", "multifoo"], ["incr", "multibar"] ]).exec(function (err, replies) { console.log(replies.toString()); }); node_redis-0.12.1/examples/psubscribe.js000066400000000000000000000020061237217724200202300ustar00rootroot00000000000000var redis = require("redis"), client1 = redis.createClient(), client2 = redis.createClient(), client3 = redis.createClient(), client4 = redis.createClient(), msg_count = 0; redis.debug_mode = false; client1.on("psubscribe", function (pattern, count) { console.log("client1 psubscribed to " + pattern + ", " + count + " total subscriptions"); client2.publish("channeltwo", "Me!"); client3.publish("channelthree", "Me too!"); client4.publish("channelfour", "And me too!"); }); client1.on("punsubscribe", function (pattern, count) { console.log("client1 punsubscribed from " + pattern + ", " + count + " total subscriptions"); client4.end(); client3.end(); client2.end(); client1.end(); }); client1.on("pmessage", function (pattern, channel, message) { console.log("("+ pattern +")" + " client1 received message on " + channel + ": " + message); msg_count += 1; if (msg_count === 3) { client1.punsubscribe(); } }); client1.psubscribe("channel*"); node_redis-0.12.1/examples/pub_sub.js000066400000000000000000000024701237217724200175330ustar00rootroot00000000000000var redis = require("redis"), client1 = redis.createClient(), msg_count = 0, client2 = redis.createClient(); redis.debug_mode = false; // Most clients probably don't do much on "subscribe". This example uses it to coordinate things within one program. client1.on("subscribe", function (channel, count) { console.log("client1 subscribed to " + channel + ", " + count + " total subscriptions"); if (count === 2) { client2.publish("a nice channel", "I am sending a message."); client2.publish("another one", "I am sending a second message."); client2.publish("a nice channel", "I am sending my last message."); } }); client1.on("unsubscribe", function (channel, count) { console.log("client1 unsubscribed from " + channel + ", " + count + " total subscriptions"); if (count === 0) { client2.end(); client1.end(); } }); client1.on("message", function (channel, message) { console.log("client1 channel " + channel + ": " + message); msg_count += 1; if (msg_count === 3) { client1.unsubscribe(); } }); client1.on("ready", function () { // if you need auth, do it here client1.incr("did a thing"); client1.subscribe("a nice channel", "another one"); }); client2.on("ready", function () { // if you need auth, do it here }); node_redis-0.12.1/examples/simple.js000066400000000000000000000013541237217724200173650ustar00rootroot00000000000000var redis = require("redis"), client = redis.createClient(); client.on("error", function (err) { console.log("error event - " + client.host + ":" + client.port + " - " + err); }); client.set("string key", "string val", redis.print); client.hset("hash key", "hashtest 1", "some value", redis.print); client.hset(["hash key", "hashtest 2", "some other value"], redis.print); client.hkeys("hash key", function (err, replies) { if (err) { return console.error("error response - " + err); } console.log(replies.length + " replies:"); replies.forEach(function (reply, i) { console.log(" " + i + ": " + reply); }); }); client.quit(function (err, res) { console.log("Exiting from quit command."); }); node_redis-0.12.1/examples/sort.js000066400000000000000000000006501237217724200170610ustar00rootroot00000000000000var redis = require("redis"), client = redis.createClient(); client.sadd("mylist", 1); client.sadd("mylist", 2); client.sadd("mylist", 3); client.set("weight_1", 5); client.set("weight_2", 500); client.set("weight_3", 1); client.set("object_1", "foo"); client.set("object_2", "bar"); client.set("object_3", "qux"); client.sort("mylist", "by", "weight_*", "get", "object_*", redis.print); // Prints Reply: qux,foo,barnode_redis-0.12.1/examples/subqueries.js000066400000000000000000000007101237217724200202560ustar00rootroot00000000000000// Sending commands in response to other commands. // This example runs "type" against every key in the database // var client = require("redis").createClient(); client.keys("*", function (err, keys) { keys.forEach(function (key, pos) { client.type(key, function (err, keytype) { console.log(key + " is " + keytype); if (pos === (keys.length - 1)) { client.quit(); } }); }); }); node_redis-0.12.1/examples/subquery.js000066400000000000000000000010351237217724200177470ustar00rootroot00000000000000var client = require("redis").createClient(); function print_results(obj) { console.dir(obj); } // build a map of all keys and their types client.keys("*", function (err, all_keys) { var key_types = {}; all_keys.forEach(function (key, pos) { // use second arg of forEach to get pos client.type(key, function (err, type) { key_types[key] = type; if (pos === all_keys.length - 1) { // callbacks all run in order print_results(key_types); } }); }); }); node_redis-0.12.1/examples/unix_socket.js000066400000000000000000000011711237217724200204240ustar00rootroot00000000000000var redis = require("redis"), client = redis.createClient("/tmp/redis.sock"), profiler = require("v8-profiler"); client.on("connect", function () { console.log("Got Unix socket connection.") }); client.on("error", function (err) { console.log(err.message); }); client.set("space chars", "space value"); setInterval(function () { client.get("space chars"); }, 100); function done() { client.info(function (err, reply) { console.log(reply.toString()); client.quit(); }); } setTimeout(function () { console.log("Taking snapshot."); var snap = profiler.takeSnapshot(); }, 5000); node_redis-0.12.1/examples/web_server.js000066400000000000000000000022731237217724200202400ustar00rootroot00000000000000// A simple web server that generates dyanmic content based on responses from Redis var http = require("http"), server, redis_client = require("redis").createClient(); server = http.createServer(function (request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); var redis_info, total_requests; redis_client.info(function (err, reply) { redis_info = reply; // stash response in outer scope }); redis_client.incr("requests", function (err, reply) { total_requests = reply; // stash response in outer scope }); redis_client.hincrby("ip", request.connection.remoteAddress, 1); redis_client.hgetall("ip", function (err, reply) { // This is the last reply, so all of the previous replies must have completed already response.write("This page was generated after talking to redis.\n\n" + "Redis info:\n" + redis_info + "\n" + "Total requests: " + total_requests + "\n\n" + "IP count: \n"); Object.keys(reply).forEach(function (ip) { response.write(" " + ip + ": " + reply[ip] + "\n"); }); response.end(); }); }).listen(80); node_redis-0.12.1/generate_commands.js000066400000000000000000000021231237217724200177240ustar00rootroot00000000000000var http = require("http"), fs = require("fs"); function prettyCurrentTime() { var date = new Date(); return date.toLocaleString(); } function write_file(commands, path) { var file_contents, out_commands; console.log("Writing " + Object.keys(commands).length + " commands to " + path); file_contents = "// This file was generated by ./generate_commands.js on " + prettyCurrentTime() + "\n"; out_commands = Object.keys(commands).map(function (key) { return key.toLowerCase(); }); file_contents += "module.exports = " + JSON.stringify(out_commands, null, " ") + ";\n"; fs.writeFile(path, file_contents); } http.get({host: "redis.io", path: "/commands.json"}, function (res) { var body = ""; console.log("Response from redis.io/commands.json: " + res.statusCode); res.on('data', function (chunk) { body += chunk; }); res.on('end', function () { write_file(JSON.parse(body), "lib/commands.js"); }); }).on('error', function (e) { console.log("Error fetching command list from redis.io: " + e.message); }); node_redis-0.12.1/index.js000066400000000000000000001262371237217724200153750ustar00rootroot00000000000000/*global Buffer require exports console setTimeout */ var net = require("net"), util = require("./lib/util"), Queue = require("./lib/queue"), to_array = require("./lib/to_array"), events = require("events"), crypto = require("crypto"), parsers = [], commands, connection_id = 0, default_port = 6379, default_host = "127.0.0.1"; // can set this to true to enable for all connections exports.debug_mode = false; var arraySlice = Array.prototype.slice function trace() { if (!exports.debug_mode) return; console.log.apply(null, arraySlice.call(arguments)) } // hiredis might not be installed try { require("./lib/parser/hiredis"); parsers.push(require("./lib/parser/hiredis")); } catch (err) { if (exports.debug_mode) { console.warn("hiredis parser not installed."); } } parsers.push(require("./lib/parser/javascript")); function RedisClient(stream, options) { this.stream = stream; this.options = options = options || {}; this.connection_id = ++connection_id; this.connected = false; this.ready = false; this.connections = 0; if (this.options.socket_nodelay === undefined) { this.options.socket_nodelay = true; } if (this.options.socket_keepalive === undefined) { this.options.socket_keepalive = true; } this.should_buffer = false; this.command_queue_high_water = this.options.command_queue_high_water || 1000; this.command_queue_low_water = this.options.command_queue_low_water || 0; this.max_attempts = null; if (options.max_attempts && !isNaN(options.max_attempts) && options.max_attempts > 0) { this.max_attempts = +options.max_attempts; } this.command_queue = new Queue(); // holds sent commands to de-pipeline them this.offline_queue = new Queue(); // holds commands issued but not able to be sent this.commands_sent = 0; this.connect_timeout = false; if (options.connect_timeout && !isNaN(options.connect_timeout) && options.connect_timeout > 0) { this.connect_timeout = +options.connect_timeout; } this.enable_offline_queue = true; if (typeof this.options.enable_offline_queue === "boolean") { this.enable_offline_queue = this.options.enable_offline_queue; } this.retry_max_delay = null; if (options.retry_max_delay !== undefined && !isNaN(options.retry_max_delay) && options.retry_max_delay > 0) { this.retry_max_delay = options.retry_max_delay; } this.initialize_retry_vars(); this.pub_sub_mode = false; this.subscription_set = {}; this.monitoring = false; this.closing = false; this.server_info = {}; this.auth_pass = null; if (options.auth_pass !== undefined) { this.auth_pass = options.auth_pass; } this.parser_module = null; this.selected_db = null; // save the selected db here, used when reconnecting this.old_state = null; this.install_stream_listeners(); events.EventEmitter.call(this); } util.inherits(RedisClient, events.EventEmitter); exports.RedisClient = RedisClient; RedisClient.prototype.install_stream_listeners = function() { var self = this; this.stream.on("connect", function () { self.on_connect(); }); this.stream.on("data", function (buffer_from_socket) { self.on_data(buffer_from_socket); }); this.stream.on("error", function (msg) { self.on_error(msg.message); }); this.stream.on("close", function () { self.connection_gone("close"); }); this.stream.on("end", function () { self.connection_gone("end"); }); this.stream.on("drain", function () { self.should_buffer = false; self.emit("drain"); }); }; RedisClient.prototype.initialize_retry_vars = function () { this.retry_timer = null; this.retry_totaltime = 0; this.retry_delay = 150; this.retry_backoff = 1.7; this.attempts = 1; }; RedisClient.prototype.unref = function () { trace("User requesting to unref the connection"); if (this.connected) { trace("unref'ing the socket connection"); this.stream.unref(); } else { trace("Not connected yet, will unref later"); this.once("connect", function () { this.unref(); }) } }; // flush offline_queue and command_queue, erroring any items with a callback first RedisClient.prototype.flush_and_error = function (message) { var command_obj, error; error = new Error(message); while (this.offline_queue.length > 0) { command_obj = this.offline_queue.shift(); if (typeof command_obj.callback === "function") { try { command_obj.callback(error); } catch (callback_err) { process.nextTick(function () { throw callback_err; }); } } } this.offline_queue = new Queue(); while (this.command_queue.length > 0) { command_obj = this.command_queue.shift(); if (typeof command_obj.callback === "function") { try { command_obj.callback(error); } catch (callback_err) { process.nextTick(function () { throw callback_err; }); } } } this.command_queue = new Queue(); }; RedisClient.prototype.on_error = function (msg) { var message = "Redis connection to " + this.address + " failed - " + msg; if (this.closing) { return; } if (exports.debug_mode) { console.warn(message); } this.flush_and_error(message); this.connected = false; this.ready = false; this.emit("error", new Error(message)); // "error" events get turned into exceptions if they aren't listened for. If the user handled this error // then we should try to reconnect. this.connection_gone("error"); }; RedisClient.prototype.do_auth = function () { var self = this; if (exports.debug_mode) { console.log("Sending auth to " + self.address + " id " + self.connection_id); } self.send_anyway = true; self.send_command("auth", [this.auth_pass], function (err, res) { if (err) { if (err.toString().match("LOADING")) { // if redis is still loading the db, it will not authenticate and everything else will fail console.log("Redis still loading, trying to authenticate later"); setTimeout(function () { self.do_auth(); }, 2000); // TODO - magic number alert return; } else if (err.toString().match("no password is set")) { console.log("Warning: Redis server does not require a password, but a password was supplied.") err = null; res = "OK"; } else { return self.emit("error", new Error("Auth error: " + err.message)); } } if (res.toString() !== "OK") { return self.emit("error", new Error("Auth failed: " + res.toString())); } if (exports.debug_mode) { console.log("Auth succeeded " + self.address + " id " + self.connection_id); } if (self.auth_callback) { self.auth_callback(err, res); self.auth_callback = null; } // now we are really connected self.emit("connect"); self.initialize_retry_vars(); if (self.options.no_ready_check) { self.on_ready(); } else { self.ready_check(); } }); self.send_anyway = false; }; RedisClient.prototype.on_connect = function () { if (exports.debug_mode) { console.log("Stream connected " + this.address + " id " + this.connection_id); } this.connected = true; this.ready = false; this.connections += 1; this.command_queue = new Queue(); this.emitted_end = false; if (this.options.socket_nodelay) { this.stream.setNoDelay(); } this.stream.setKeepAlive(this.options.socket_keepalive); this.stream.setTimeout(0); this.init_parser(); if (this.auth_pass) { this.do_auth(); } else { this.emit("connect"); this.initialize_retry_vars(); if (this.options.no_ready_check) { this.on_ready(); } else { this.ready_check(); } } }; RedisClient.prototype.init_parser = function () { var self = this; if (this.options.parser) { if (! parsers.some(function (parser) { if (parser.name === self.options.parser) { self.parser_module = parser; if (exports.debug_mode) { console.log("Using parser module: " + self.parser_module.name); } return true; } })) { throw new Error("Couldn't find named parser " + self.options.parser + " on this system"); } } else { if (exports.debug_mode) { console.log("Using default parser module: " + parsers[0].name); } this.parser_module = parsers[0]; } this.parser_module.debug_mode = exports.debug_mode; // return_buffers sends back Buffers from parser to callback. detect_buffers sends back Buffers from parser, but // converts to Strings if the input arguments are not Buffers. this.reply_parser = new this.parser_module.Parser({ return_buffers: self.options.return_buffers || self.options.detect_buffers || false }); // "reply error" is an error sent back by Redis this.reply_parser.on("reply error", function (reply) { if (reply instanceof Error) { self.return_error(reply); } else { self.return_error(new Error(reply)); } }); this.reply_parser.on("reply", function (reply) { self.return_reply(reply); }); // "error" is bad. Somehow the parser got confused. It'll try to reset and continue. this.reply_parser.on("error", function (err) { self.emit("error", new Error("Redis reply parser error: " + err.stack)); }); }; RedisClient.prototype.on_ready = function () { var self = this; this.ready = true; if (this.old_state !== null) { this.monitoring = this.old_state.monitoring; this.pub_sub_mode = this.old_state.pub_sub_mode; this.selected_db = this.old_state.selected_db; this.old_state = null; } // magically restore any modal commands from a previous connection if (this.selected_db !== null) { // this trick works if and only if the following send_command // never goes into the offline queue var pub_sub_mode = this.pub_sub_mode; this.pub_sub_mode = false; this.send_command('select', [this.selected_db]); this.pub_sub_mode = pub_sub_mode; } if (this.pub_sub_mode === true) { // only emit "ready" when all subscriptions were made again var callback_count = 0; var callback = function () { callback_count--; if (callback_count === 0) { self.emit("ready"); } }; Object.keys(this.subscription_set).forEach(function (key) { var parts = key.split(" "); if (exports.debug_mode) { console.warn("sending pub/sub on_ready " + parts[0] + ", " + parts[1]); } callback_count++; self.send_command(parts[0] + "scribe", [parts[1]], callback); }); return; } else if (this.monitoring) { this.send_command("monitor"); } else { this.send_offline_queue(); } this.emit("ready"); }; RedisClient.prototype.on_info_cmd = function (err, res) { var self = this, obj = {}, lines, retry_time; if (err) { return self.emit("error", new Error("Ready check failed: " + err.message)); } lines = res.toString().split("\r\n"); lines.forEach(function (line) { var parts = line.split(':'); if (parts[1]) { obj[parts[0]] = parts[1]; } }); obj.versions = []; if( obj.redis_version ){ obj.redis_version.split('.').forEach(function (num) { obj.versions.push(+num); }); } // expose info key/vals to users this.server_info = obj; if (!obj.loading || (obj.loading && obj.loading === "0")) { if (exports.debug_mode) { console.log("Redis server ready."); } this.on_ready(); } else { retry_time = obj.loading_eta_seconds * 1000; if (retry_time > 1000) { retry_time = 1000; } if (exports.debug_mode) { console.log("Redis server still loading, trying again in " + retry_time); } setTimeout(function () { self.ready_check(); }, retry_time); } }; RedisClient.prototype.ready_check = function () { var self = this; if (exports.debug_mode) { console.log("checking server ready state..."); } this.send_anyway = true; // secret flag to send_command to send something even if not "ready" this.info(function (err, res) { self.on_info_cmd(err, res); }); this.send_anyway = false; }; RedisClient.prototype.send_offline_queue = function () { var command_obj, buffered_writes = 0; while (this.offline_queue.length > 0) { command_obj = this.offline_queue.shift(); if (exports.debug_mode) { console.log("Sending offline command: " + command_obj.command); } buffered_writes += !this.send_command(command_obj.command, command_obj.args, command_obj.callback); } this.offline_queue = new Queue(); // Even though items were shifted off, Queue backing store still uses memory until next add, so just get a new Queue if (!buffered_writes) { this.should_buffer = false; this.emit("drain"); } }; RedisClient.prototype.connection_gone = function (why) { var self = this; // If a retry is already in progress, just let that happen if (this.retry_timer) { return; } if (exports.debug_mode) { console.warn("Redis connection is gone from " + why + " event."); } this.connected = false; this.ready = false; if (this.old_state === null) { var state = { monitoring: this.monitoring, pub_sub_mode: this.pub_sub_mode, selected_db: this.selected_db }; this.old_state = state; this.monitoring = false; this.pub_sub_mode = false; this.selected_db = null; } // since we are collapsing end and close, users don't expect to be called twice if (! this.emitted_end) { this.emit("end"); this.emitted_end = true; } this.flush_and_error("Redis connection gone from " + why + " event."); // If this is a requested shutdown, then don't retry if (this.closing) { this.retry_timer = null; if (exports.debug_mode) { console.warn("connection ended from quit command, not retrying."); } return; } var nextDelay = Math.floor(this.retry_delay * this.retry_backoff); if (this.retry_max_delay !== null && nextDelay > this.retry_max_delay) { this.retry_delay = this.retry_max_delay; } else { this.retry_delay = nextDelay; } if (exports.debug_mode) { console.log("Retry connection in " + this.retry_delay + " ms"); } if (this.max_attempts && this.attempts >= this.max_attempts) { this.retry_timer = null; // TODO - some people need a "Redis is Broken mode" for future commands that errors immediately, and others // want the program to exit. Right now, we just log, which doesn't really help in either case. console.error("node_redis: Couldn't get Redis connection after " + this.max_attempts + " attempts."); return; } this.attempts += 1; this.emit("reconnecting", { delay: self.retry_delay, attempt: self.attempts }); this.retry_timer = setTimeout(function () { if (exports.debug_mode) { console.log("Retrying connection..."); } self.retry_totaltime += self.retry_delay; if (self.connect_timeout && self.retry_totaltime >= self.connect_timeout) { self.retry_timer = null; // TODO - engage Redis is Broken mode for future commands, or whatever console.error("node_redis: Couldn't get Redis connection after " + self.retry_totaltime + "ms."); return; } self.stream = net.createConnection(self.connectionOption); self.install_stream_listeners(); self.retry_timer = null; }, this.retry_delay); }; RedisClient.prototype.on_data = function (data) { if (exports.debug_mode) { console.log("net read " + this.address + " id " + this.connection_id + ": " + data.toString()); } try { this.reply_parser.execute(data); } catch (err) { // This is an unexpected parser problem, an exception that came from the parser code itself. // Parser should emit "error" events if it notices things are out of whack. // Callbacks that throw exceptions will land in return_reply(), below. // TODO - it might be nice to have a different "error" event for different types of errors this.emit("error", err); } }; RedisClient.prototype.return_error = function (err) { var command_obj = this.command_queue.shift(), queue_len = this.command_queue.getLength(); if (this.pub_sub_mode === false && queue_len === 0) { this.command_queue = new Queue(); this.emit("idle"); } if (this.should_buffer && queue_len <= this.command_queue_low_water) { this.emit("drain"); this.should_buffer = false; } if (command_obj && typeof command_obj.callback === "function") { try { command_obj.callback(err); } catch (callback_err) { // if a callback throws an exception, re-throw it on a new stack so the parser can keep going process.nextTick(function () { throw callback_err; }); } } else { console.log("node_redis: no callback to send error: " + err.message); // this will probably not make it anywhere useful, but we might as well throw process.nextTick(function () { throw err; }); } }; // if a callback throws an exception, re-throw it on a new stack so the parser can keep going. // if a domain is active, emit the error on the domain, which will serve the same function. // put this try/catch in its own function because V8 doesn't optimize this well yet. function try_callback(callback, reply) { try { callback(null, reply); } catch (err) { if (process.domain) { var currDomain = process.domain; currDomain.emit('error', err); if (process.domain === currDomain) { currDomain.exit(); } } else { process.nextTick(function () { throw err; }); } } } // hgetall converts its replies to an Object. If the reply is empty, null is returned. function reply_to_object(reply) { var obj = {}, j, jl, key, val; if (reply.length === 0) { return null; } for (j = 0, jl = reply.length; j < jl; j += 2) { key = reply[j].toString('binary'); val = reply[j + 1]; obj[key] = val; } return obj; } function reply_to_strings(reply) { var i; if (Buffer.isBuffer(reply)) { return reply.toString(); } if (Array.isArray(reply)) { for (i = 0; i < reply.length; i++) { if (reply[i] !== null && reply[i] !== undefined) { reply[i] = reply[i].toString(); } } return reply; } return reply; } RedisClient.prototype.return_reply = function (reply) { var command_obj, len, type, timestamp, argindex, args, queue_len; // If the "reply" here is actually a message received asynchronously due to a // pubsub subscription, don't pop the command queue as we'll only be consuming // the head command prematurely. if (Array.isArray(reply) && reply.length > 0 && reply[0]) { type = reply[0].toString(); } if (this.pub_sub_mode && (type == 'message' || type == 'pmessage')) { trace("received pubsub message"); } else { command_obj = this.command_queue.shift(); } queue_len = this.command_queue.getLength(); if (this.pub_sub_mode === false && queue_len === 0) { this.command_queue = new Queue(); // explicitly reclaim storage from old Queue this.emit("idle"); } if (this.should_buffer && queue_len <= this.command_queue_low_water) { this.emit("drain"); this.should_buffer = false; } if (command_obj && !command_obj.sub_command) { if (typeof command_obj.callback === "function") { if (this.options.detect_buffers && command_obj.buffer_args === false) { // If detect_buffers option was specified, then the reply from the parser will be Buffers. // If this command did not use Buffer arguments, then convert the reply to Strings here. reply = reply_to_strings(reply); } // TODO - confusing and error-prone that hgetall is special cased in two places if (reply && 'hgetall' === command_obj.command.toLowerCase()) { reply = reply_to_object(reply); } try_callback(command_obj.callback, reply); } else if (exports.debug_mode) { console.log("no callback for reply: " + (reply && reply.toString && reply.toString())); } } else if (this.pub_sub_mode || (command_obj && command_obj.sub_command)) { if (Array.isArray(reply)) { type = reply[0].toString(); if (type === "message") { this.emit("message", reply[1].toString(), reply[2]); // channel, message } else if (type === "pmessage") { this.emit("pmessage", reply[1].toString(), reply[2].toString(), reply[3]); // pattern, channel, message } else if (type === "subscribe" || type === "unsubscribe" || type === "psubscribe" || type === "punsubscribe") { if (reply[2] === 0) { this.pub_sub_mode = false; if (this.debug_mode) { console.log("All subscriptions removed, exiting pub/sub mode"); } } else { this.pub_sub_mode = true; } // subscribe commands take an optional callback and also emit an event, but only the first response is included in the callback // TODO - document this or fix it so it works in a more obvious way // reply[1] can be null var reply1String = (reply[1] === null) ? null : reply[1].toString(); if (command_obj && typeof command_obj.callback === "function") { try_callback(command_obj.callback, reply1String); } this.emit(type, reply1String, reply[2]); // channel, count } else { throw new Error("subscriptions are active but got unknown reply type " + type); } } else if (! this.closing) { throw new Error("subscriptions are active but got an invalid reply: " + reply); } } else if (this.monitoring) { len = reply.indexOf(" "); timestamp = reply.slice(0, len); argindex = reply.indexOf('"'); args = reply.slice(argindex + 1, -1).split('" "').map(function (elem) { return elem.replace(/\\"/g, '"'); }); this.emit("monitor", timestamp, args); } else { throw new Error("node_redis command queue state error. If you can reproduce this, please report it."); } }; // This Command constructor is ever so slightly faster than using an object literal, but more importantly, using // a named constructor helps it show up meaningfully in the V8 CPU profiler and in heap snapshots. function Command(command, args, sub_command, buffer_args, callback) { this.command = command; this.args = args; this.sub_command = sub_command; this.buffer_args = buffer_args; this.callback = callback; } RedisClient.prototype.send_command = function (command, args, callback) { var arg, command_obj, i, il, elem_count, buffer_args, stream = this.stream, command_str = "", buffered_writes = 0, last_arg_type, lcaseCommand; if (typeof command !== "string") { throw new Error("First argument to send_command must be the command name string, not " + typeof command); } if (Array.isArray(args)) { if (typeof callback === "function") { // probably the fastest way: // client.command([arg1, arg2], cb); (straight passthrough) // send_command(command, [arg1, arg2], cb); } else if (! callback) { // most people find this variable argument length form more convenient, but it uses arguments, which is slower // client.command(arg1, arg2, cb); (wraps up arguments into an array) // send_command(command, [arg1, arg2, cb]); // client.command(arg1, arg2); (callback is optional) // send_command(command, [arg1, arg2]); // client.command(arg1, arg2, undefined); (callback is undefined) // send_command(command, [arg1, arg2, undefined]); last_arg_type = typeof args[args.length - 1]; if (last_arg_type === "function" || last_arg_type === "undefined") { callback = args.pop(); } } else { throw new Error("send_command: last argument must be a callback or undefined"); } } else { throw new Error("send_command: second argument must be an array"); } if (callback && process.domain) callback = process.domain.bind(callback); // if the last argument is an array and command is sadd or srem, expand it out: // client.sadd(arg1, [arg2, arg3, arg4], cb); // converts to: // client.sadd(arg1, arg2, arg3, arg4, cb); lcaseCommand = command.toLowerCase(); if ((lcaseCommand === 'sadd' || lcaseCommand === 'srem') && args.length > 0 && Array.isArray(args[args.length - 1])) { args = args.slice(0, -1).concat(args[args.length - 1]); } // if the value is undefined or null and command is set or setx, need not to send message to redis if (command === 'set' || command === 'setex') { if(args[args.length - 1] === undefined || args[args.length - 1] === null) { var err = new Error('send_command: ' + command + ' value must not be undefined or null'); return callback && callback(err); } } buffer_args = false; for (i = 0, il = args.length, arg; i < il; i += 1) { if (Buffer.isBuffer(args[i])) { buffer_args = true; } } command_obj = new Command(command, args, false, buffer_args, callback); if ((!this.ready && !this.send_anyway) || !stream.writable) { if (exports.debug_mode) { if (!stream.writable) { console.log("send command: stream is not writeable."); } } if (this.enable_offline_queue) { if (exports.debug_mode) { console.log("Queueing " + command + " for next server connection."); } this.offline_queue.push(command_obj); this.should_buffer = true; } else { var not_writeable_error = new Error('send_command: stream not writeable. enable_offline_queue is false'); if (command_obj.callback) { command_obj.callback(not_writeable_error); } else { throw not_writeable_error; } } return false; } if (command === "subscribe" || command === "psubscribe" || command === "unsubscribe" || command === "punsubscribe") { this.pub_sub_command(command_obj); } else if (command === "monitor") { this.monitoring = true; } else if (command === "quit") { this.closing = true; } else if (this.pub_sub_mode === true) { throw new Error("Connection in subscriber mode, only subscriber commands may be used"); } this.command_queue.push(command_obj); this.commands_sent += 1; elem_count = args.length + 1; // Always use "Multi bulk commands", but if passed any Buffer args, then do multiple writes, one for each arg. // This means that using Buffers in commands is going to be slower, so use Strings if you don't already have a Buffer. command_str = "*" + elem_count + "\r\n$" + command.length + "\r\n" + command + "\r\n"; if (! buffer_args) { // Build up a string and send entire command in one write for (i = 0, il = args.length, arg; i < il; i += 1) { arg = args[i]; if (typeof arg !== "string") { arg = String(arg); } command_str += "$" + Buffer.byteLength(arg) + "\r\n" + arg + "\r\n"; } if (exports.debug_mode) { console.log("send " + this.address + " id " + this.connection_id + ": " + command_str); } buffered_writes += !stream.write(command_str); } else { if (exports.debug_mode) { console.log("send command (" + command_str + ") has Buffer arguments"); } buffered_writes += !stream.write(command_str); for (i = 0, il = args.length, arg; i < il; i += 1) { arg = args[i]; if (!(Buffer.isBuffer(arg) || arg instanceof String)) { arg = String(arg); } if (Buffer.isBuffer(arg)) { if (arg.length === 0) { if (exports.debug_mode) { console.log("send_command: using empty string for 0 length buffer"); } buffered_writes += !stream.write("$0\r\n\r\n"); } else { buffered_writes += !stream.write("$" + arg.length + "\r\n"); buffered_writes += !stream.write(arg); buffered_writes += !stream.write("\r\n"); if (exports.debug_mode) { console.log("send_command: buffer send " + arg.length + " bytes"); } } } else { if (exports.debug_mode) { console.log("send_command: string send " + Buffer.byteLength(arg) + " bytes: " + arg); } buffered_writes += !stream.write("$" + Buffer.byteLength(arg) + "\r\n" + arg + "\r\n"); } } } if (exports.debug_mode) { console.log("send_command buffered_writes: " + buffered_writes, " should_buffer: " + this.should_buffer); } if (buffered_writes || this.command_queue.getLength() >= this.command_queue_high_water) { this.should_buffer = true; } return !this.should_buffer; }; RedisClient.prototype.pub_sub_command = function (command_obj) { var i, key, command, args; if (this.pub_sub_mode === false && exports.debug_mode) { console.log("Entering pub/sub mode from " + command_obj.command); } this.pub_sub_mode = true; command_obj.sub_command = true; command = command_obj.command; args = command_obj.args; if (command === "subscribe" || command === "psubscribe") { if (command === "subscribe") { key = "sub"; } else { key = "psub"; } for (i = 0; i < args.length; i++) { this.subscription_set[key + " " + args[i]] = true; } } else { if (command === "unsubscribe") { key = "sub"; } else { key = "psub"; } for (i = 0; i < args.length; i++) { delete this.subscription_set[key + " " + args[i]]; } } }; RedisClient.prototype.end = function () { this.stream._events = {}; //clear retry_timer if(this.retry_timer){ clearTimeout(this.retry_timer); this.retry_timer=null; } this.stream.on("error", function(){}); this.connected = false; this.ready = false; this.closing = true; return this.stream.destroySoon(); }; function Multi(client, args) { this._client = client; this.queue = [["MULTI"]]; if (Array.isArray(args)) { this.queue = this.queue.concat(args); } } exports.Multi = Multi; // take 2 arrays and return the union of their elements function set_union(seta, setb) { var obj = {}; seta.forEach(function (val) { obj[val] = true; }); setb.forEach(function (val) { obj[val] = true; }); return Object.keys(obj); } // This static list of commands is updated from time to time. ./lib/commands.js can be updated with generate_commands.js commands = set_union(["get", "set", "setnx", "setex", "append", "strlen", "del", "exists", "setbit", "getbit", "setrange", "getrange", "substr", "incr", "decr", "mget", "rpush", "lpush", "rpushx", "lpushx", "linsert", "rpop", "lpop", "brpop", "brpoplpush", "blpop", "llen", "lindex", "lset", "lrange", "ltrim", "lrem", "rpoplpush", "sadd", "srem", "smove", "sismember", "scard", "spop", "srandmember", "sinter", "sinterstore", "sunion", "sunionstore", "sdiff", "sdiffstore", "smembers", "zadd", "zincrby", "zrem", "zremrangebyscore", "zremrangebyrank", "zunionstore", "zinterstore", "zrange", "zrangebyscore", "zrevrangebyscore", "zcount", "zrevrange", "zcard", "zscore", "zrank", "zrevrank", "hset", "hsetnx", "hget", "hmset", "hmget", "hincrby", "hdel", "hlen", "hkeys", "hvals", "hgetall", "hexists", "incrby", "decrby", "getset", "mset", "msetnx", "randomkey", "select", "move", "rename", "renamenx", "expire", "expireat", "keys", "dbsize", "auth", "ping", "echo", "save", "bgsave", "bgrewriteaof", "shutdown", "lastsave", "type", "multi", "exec", "discard", "sync", "flushdb", "flushall", "sort", "info", "monitor", "ttl", "persist", "slaveof", "debug", "config", "subscribe", "unsubscribe", "psubscribe", "punsubscribe", "publish", "watch", "unwatch", "cluster", "restore", "migrate", "dump", "object", "client", "eval", "evalsha"], require("./lib/commands")); commands.forEach(function (fullCommand) { var command = fullCommand.split(' ')[0]; RedisClient.prototype[command] = function (args, callback) { if (Array.isArray(args) && typeof callback === "function") { return this.send_command(command, args, callback); } else { return this.send_command(command, to_array(arguments)); } }; RedisClient.prototype[command.toUpperCase()] = RedisClient.prototype[command]; Multi.prototype[command] = function () { this.queue.push([command].concat(to_array(arguments))); return this; }; Multi.prototype[command.toUpperCase()] = Multi.prototype[command]; }); // store db in this.select_db to restore it on reconnect RedisClient.prototype.select = function (db, callback) { var self = this; this.send_command('select', [db], function (err, res) { if (err === null) { self.selected_db = db; } if (typeof(callback) === 'function') { callback(err, res); } else if (err) { self.emit('error', err); } }); }; RedisClient.prototype.SELECT = RedisClient.prototype.select; // Stash auth for connect and reconnect. Send immediately if already connected. RedisClient.prototype.auth = function () { var args = to_array(arguments); this.auth_pass = args[0]; this.auth_callback = args[1]; if (exports.debug_mode) { console.log("Saving auth as " + this.auth_pass); } if (this.connected) { this.send_command("auth", args); } }; RedisClient.prototype.AUTH = RedisClient.prototype.auth; RedisClient.prototype.hmget = function (arg1, arg2, arg3) { if (Array.isArray(arg2) && typeof arg3 === "function") { return this.send_command("hmget", [arg1].concat(arg2), arg3); } else if (Array.isArray(arg1) && typeof arg2 === "function") { return this.send_command("hmget", arg1, arg2); } else { return this.send_command("hmget", to_array(arguments)); } }; RedisClient.prototype.HMGET = RedisClient.prototype.hmget; RedisClient.prototype.hmset = function (args, callback) { var tmp_args, tmp_keys, i, il, key; if (Array.isArray(args) && typeof callback === "function") { return this.send_command("hmset", args, callback); } args = to_array(arguments); if (typeof args[args.length - 1] === "function") { callback = args[args.length - 1]; args.length -= 1; } else { callback = null; } if (args.length === 2 && (typeof args[0] === "string" || typeof args[0] === "number") && typeof args[1] === "object") { // User does: client.hmset(key, {key1: val1, key2: val2}) // assuming key is a string, i.e. email address // if key is a number, i.e. timestamp, convert to string if (typeof args[0] === "number") { args[0] = args[0].toString(); } tmp_args = [ args[0] ]; tmp_keys = Object.keys(args[1]); for (i = 0, il = tmp_keys.length; i < il ; i++) { key = tmp_keys[i]; tmp_args.push(key); tmp_args.push(args[1][key]); } args = tmp_args; } return this.send_command("hmset", args, callback); }; RedisClient.prototype.HMSET = RedisClient.prototype.hmset; Multi.prototype.hmset = function () { var args = to_array(arguments), tmp_args; if (args.length >= 2 && typeof args[0] === "string" && typeof args[1] === "object") { tmp_args = [ "hmset", args[0] ]; Object.keys(args[1]).map(function (key) { tmp_args.push(key); tmp_args.push(args[1][key]); }); if (args[2]) { tmp_args.push(args[2]); } args = tmp_args; } else { args.unshift("hmset"); } this.queue.push(args); return this; }; Multi.prototype.HMSET = Multi.prototype.hmset; Multi.prototype.exec = function (callback) { var self = this; var errors = []; // drain queue, callback will catch "QUEUED" or error // TODO - get rid of all of these anonymous functions which are elegant but slow this.queue.forEach(function (args, index) { var command = args[0], obj; if (typeof args[args.length - 1] === "function") { args = args.slice(1, -1); } else { args = args.slice(1); } if (args.length === 1 && Array.isArray(args[0])) { args = args[0]; } if (command.toLowerCase() === 'hmset' && typeof args[1] === 'object') { obj = args.pop(); Object.keys(obj).forEach(function (key) { args.push(key); args.push(obj[key]); }); } this._client.send_command(command, args, function (err, reply) { if (err) { var cur = self.queue[index]; if (typeof cur[cur.length - 1] === "function") { cur[cur.length - 1](err); } else { errors.push(new Error(err)); } } }); }, this); // TODO - make this callback part of Multi.prototype instead of creating it each time return this._client.send_command("EXEC", [], function (err, replies) { if (err) { if (callback) { errors.push(new Error(err)); callback(errors); return; } else { throw new Error(err); } } var i, il, reply, args; if (replies) { for (i = 1, il = self.queue.length; i < il; i += 1) { reply = replies[i - 1]; args = self.queue[i]; // TODO - confusing and error-prone that hgetall is special cased in two places if (reply && args[0].toLowerCase() === "hgetall") { replies[i - 1] = reply = reply_to_object(reply); } if (typeof args[args.length - 1] === "function") { args[args.length - 1](null, reply); } } } if (callback) { callback(null, replies); } }); }; Multi.prototype.EXEC = Multi.prototype.exec; RedisClient.prototype.multi = function (args) { return new Multi(this, args); }; RedisClient.prototype.MULTI = function (args) { return new Multi(this, args); }; // stash original eval method var eval_orig = RedisClient.prototype.eval; // hook eval with an attempt to evalsha for cached scripts RedisClient.prototype.eval = RedisClient.prototype.EVAL = function () { var self = this, args = to_array(arguments), callback; if (typeof args[args.length - 1] === "function") { callback = args.pop(); } if (Array.isArray(args[0])) { args = args[0]; } // replace script source with sha value var source = args[0]; args[0] = crypto.createHash("sha1").update(source).digest("hex"); self.evalsha(args, function (err, reply) { if (err && /NOSCRIPT/.test(err.message)) { args[0] = source; eval_orig.call(self, args, callback); } else if (callback) { callback(err, reply); } }); }; exports.createClient = function(arg0, arg1, arg2){ if( arguments.length === 0 ){ // createClient() return createClient_tcp(default_port, default_host, {}); } else if( typeof arg0 === 'number' || typeof arg0 === 'string' && arg0.match(/^\d+$/) ){ // createClient( 3000, host, options) // createClient('3000', host, options) return createClient_tcp(arg0, arg1, arg2); } else if( typeof arg0 === 'string' ){ // createClient( '/tmp/redis.sock', options) return createClient_unix(arg0,arg1); } else if( arg0 !== null && typeof arg0 === 'object' ){ // createClient(options) return createClient_tcp(default_port, default_host, arg0 ); } else if( arg0 === null && arg1 === null ){ // for backward compatibility // createClient(null,null,options) return createClient_tcp(default_port, default_host, arg2); } else { throw new Error('unknown type of connection in createClient()'); } } var createClient_unix = function(path, options){ var cnxOptions = { path: path }; var net_client = net.createConnection(cnxOptions); var redis_client = new RedisClient(net_client, options || {}); redis_client.connectionOption = cnxOptions; redis_client.address = path; return redis_client; } var createClient_tcp = function (port_arg, host_arg, options) { var cnxOptions = { 'port' : port_arg || default_port, 'host' : host_arg || default_host, 'family' : (options && options.family === 'IPv6') ? 6 : 4 }; var net_client = net.createConnection(cnxOptions); var redis_client = new RedisClient(net_client, options || {}); redis_client.connectionOption = cnxOptions; redis_client.address = cnxOptions.host + ':' + cnxOptions.port; return redis_client; }; exports.print = function (err, reply) { if (err) { console.log("Error: " + err); } else { console.log("Reply: " + reply); } }; node_redis-0.12.1/lib/000077500000000000000000000000001237217724200144635ustar00rootroot00000000000000node_redis-0.12.1/lib/commands.js000066400000000000000000000047311237217724200166270ustar00rootroot00000000000000// This file was generated by ./generate_commands.js on Wed Apr 23 2014 14:51:21 GMT-0700 (PDT) module.exports = [ "append", "auth", "bgrewriteaof", "bgsave", "bitcount", "bitop", "bitpos", "blpop", "brpop", "brpoplpush", "client kill", "client list", "client getname", "client pause", "client setname", "config get", "config rewrite", "config set", "config resetstat", "dbsize", "debug object", "debug segfault", "decr", "decrby", "del", "discard", "dump", "echo", "eval", "evalsha", "exec", "exists", "expire", "expireat", "flushall", "flushdb", "get", "getbit", "getrange", "getset", "hdel", "hexists", "hget", "hgetall", "hincrby", "hincrbyfloat", "hkeys", "hlen", "hmget", "hmset", "hset", "hsetnx", "hvals", "incr", "incrby", "incrbyfloat", "info", "keys", "lastsave", "lindex", "linsert", "llen", "lpop", "lpush", "lpushx", "lrange", "lrem", "lset", "ltrim", "mget", "migrate", "monitor", "move", "mset", "msetnx", "multi", "object", "persist", "pexpire", "pexpireat", "pfadd", "pfcount", "pfmerge", "ping", "psetex", "psubscribe", "pubsub", "pttl", "publish", "punsubscribe", "quit", "randomkey", "rename", "renamenx", "restore", "rpop", "rpoplpush", "rpush", "rpushx", "sadd", "save", "scard", "script exists", "script flush", "script kill", "script load", "sdiff", "sdiffstore", "select", "set", "setbit", "setex", "setnx", "setrange", "shutdown", "sinter", "sinterstore", "sismember", "slaveof", "slowlog", "smembers", "smove", "sort", "spop", "srandmember", "srem", "strlen", "subscribe", "sunion", "sunionstore", "sync", "time", "ttl", "type", "unsubscribe", "unwatch", "watch", "zadd", "zcard", "zcount", "zincrby", "zinterstore", "zlexcount", "zrange", "zrangebylex", "zrangebyscore", "zrank", "zrem", "zremrangebylex", "zremrangebyrank", "zremrangebyscore", "zrevrange", "zrevrangebyscore", "zrevrank", "zscore", "zunionstore", "scan", "sscan", "hscan", "zscan" ]; node_redis-0.12.1/lib/parser/000077500000000000000000000000001237217724200157575ustar00rootroot00000000000000node_redis-0.12.1/lib/parser/hiredis.js000066400000000000000000000020571237217724200177500ustar00rootroot00000000000000var events = require("events"), util = require("../util"), hiredis = require("hiredis"); exports.debug_mode = false; exports.name = "hiredis"; function HiredisReplyParser(options) { this.name = exports.name; this.options = options || {}; this.reset(); events.EventEmitter.call(this); } util.inherits(HiredisReplyParser, events.EventEmitter); exports.Parser = HiredisReplyParser; HiredisReplyParser.prototype.reset = function () { this.reader = new hiredis.Reader({ return_buffers: this.options.return_buffers || false }); }; HiredisReplyParser.prototype.execute = function (data) { var reply; this.reader.feed(data); while (true) { try { reply = this.reader.get(); } catch (err) { this.emit("error", err); break; } if (reply === undefined) { break; } if (reply && reply.constructor === Error) { this.emit("reply error", reply); } else { this.emit("reply", reply); } } }; node_redis-0.12.1/lib/parser/javascript.js000066400000000000000000000176771237217724200205050ustar00rootroot00000000000000var events = require("events"), util = require("../util"); function Packet(type, size) { this.type = type; this.size = +size; } exports.name = "javascript"; exports.debug_mode = false; function ReplyParser(options) { this.name = exports.name; this.options = options || { }; this._buffer = null; this._offset = 0; this._encoding = "utf-8"; this._debug_mode = options.debug_mode; this._reply_type = null; } util.inherits(ReplyParser, events.EventEmitter); exports.Parser = ReplyParser; function IncompleteReadBuffer(message) { this.name = "IncompleteReadBuffer"; this.message = message; } util.inherits(IncompleteReadBuffer, Error); // Buffer.toString() is quite slow for small strings function small_toString(buf, start, end) { var tmp = "", i; for (i = start; i < end; i++) { tmp += String.fromCharCode(buf[i]); } return tmp; } ReplyParser.prototype._parseResult = function (type) { var start, end, offset, packetHeader; if (type === 43 || type === 45) { // + or - // up to the delimiter end = this._packetEndOffset() - 1; start = this._offset; // include the delimiter this._offset = end + 2; if (end > this._buffer.length) { this._offset = start; throw new IncompleteReadBuffer("Wait for more data."); } if (this.options.return_buffers) { return this._buffer.slice(start, end); } else { if (end - start < 65536) { // completely arbitrary return small_toString(this._buffer, start, end); } else { return this._buffer.toString(this._encoding, start, end); } } } else if (type === 58) { // : // up to the delimiter end = this._packetEndOffset() - 1; start = this._offset; // include the delimiter this._offset = end + 2; if (end > this._buffer.length) { this._offset = start; throw new IncompleteReadBuffer("Wait for more data."); } if (this.options.return_buffers) { return this._buffer.slice(start, end); } // return the coerced numeric value return +small_toString(this._buffer, start, end); } else if (type === 36) { // $ // set a rewind point, as the packet could be larger than the // buffer in memory offset = this._offset - 1; packetHeader = new Packet(type, this.parseHeader()); // packets with a size of -1 are considered null if (packetHeader.size === -1) { return undefined; } end = this._offset + packetHeader.size; start = this._offset; // set the offset to after the delimiter this._offset = end + 2; if (end > this._buffer.length) { this._offset = offset; throw new IncompleteReadBuffer("Wait for more data."); } if (this.options.return_buffers) { return this._buffer.slice(start, end); } else { return this._buffer.toString(this._encoding, start, end); } } else if (type === 42) { // * offset = this._offset; packetHeader = new Packet(type, this.parseHeader()); if (packetHeader.size < 0) { return null; } if (packetHeader.size > this._bytesRemaining()) { this._offset = offset - 1; throw new IncompleteReadBuffer("Wait for more data."); } var reply = [ ]; var ntype, i, res; offset = this._offset - 1; for (i = 0; i < packetHeader.size; i++) { ntype = this._buffer[this._offset++]; if (this._offset > this._buffer.length) { throw new IncompleteReadBuffer("Wait for more data."); } res = this._parseResult(ntype); if (res === undefined) { res = null; } reply.push(res); } return reply; } }; ReplyParser.prototype.execute = function (buffer) { this.append(buffer); var type, ret, offset; while (true) { offset = this._offset; try { // at least 4 bytes: :1\r\n if (this._bytesRemaining() < 4) { break; } type = this._buffer[this._offset++]; if (type === 43) { // + ret = this._parseResult(type); if (ret === null) { break; } this.send_reply(ret); } else if (type === 45) { // - ret = this._parseResult(type); if (ret === null) { break; } this.send_error(ret); } else if (type === 58) { // : ret = this._parseResult(type); if (ret === null) { break; } this.send_reply(ret); } else if (type === 36) { // $ ret = this._parseResult(type); if (ret === null) { break; } // check the state for what is the result of // a -1, set it back up for a null reply if (ret === undefined) { ret = null; } this.send_reply(ret); } else if (type === 42) { // * // set a rewind point. if a failure occurs, // wait for the next execute()/append() and try again offset = this._offset - 1; ret = this._parseResult(type); this.send_reply(ret); } } catch (err) { // catch the error (not enough data), rewind, and wait // for the next packet to appear if (! (err instanceof IncompleteReadBuffer)) { throw err; } this._offset = offset; break; } } }; ReplyParser.prototype.append = function (newBuffer) { if (!newBuffer) { return; } // first run if (this._buffer === null) { this._buffer = newBuffer; return; } // out of data if (this._offset >= this._buffer.length) { this._buffer = newBuffer; this._offset = 0; return; } // very large packet // check for concat, if we have it, use it if (Buffer.concat !== undefined) { this._buffer = Buffer.concat([this._buffer.slice(this._offset), newBuffer]); } else { var remaining = this._bytesRemaining(), newLength = remaining + newBuffer.length, tmpBuffer = new Buffer(newLength); this._buffer.copy(tmpBuffer, 0, this._offset); newBuffer.copy(tmpBuffer, remaining, 0); this._buffer = tmpBuffer; } this._offset = 0; }; ReplyParser.prototype.parseHeader = function () { var end = this._packetEndOffset(), value = small_toString(this._buffer, this._offset, end - 1); this._offset = end + 1; return value; }; ReplyParser.prototype._packetEndOffset = function () { var offset = this._offset; while (this._buffer[offset] !== 0x0d && this._buffer[offset + 1] !== 0x0a) { offset++; if (offset >= this._buffer.length) { throw new IncompleteReadBuffer("didn't see LF after NL reading multi bulk count (" + offset + " => " + this._buffer.length + ", " + this._offset + ")"); } } offset++; return offset; }; ReplyParser.prototype._bytesRemaining = function () { return (this._buffer.length - this._offset) < 0 ? 0 : (this._buffer.length - this._offset); }; ReplyParser.prototype.parser_error = function (message) { this.emit("error", message); }; ReplyParser.prototype.send_error = function (reply) { this.emit("reply error", reply); }; ReplyParser.prototype.send_reply = function (reply) { this.emit("reply", reply); }; node_redis-0.12.1/lib/queue.js000066400000000000000000000025421237217724200161500ustar00rootroot00000000000000// Queue class adapted from Tim Caswell's pattern library // http://github.com/creationix/pattern/blob/master/lib/pattern/queue.js function Queue() { this.tail = []; this.head = []; this.offset = 0; } Queue.prototype.shift = function () { if (this.offset === this.head.length) { var tmp = this.head; tmp.length = 0; this.head = this.tail; this.tail = tmp; this.offset = 0; if (this.head.length === 0) { return; } } return this.head[this.offset++]; // sorry, JSLint }; Queue.prototype.push = function (item) { return this.tail.push(item); }; Queue.prototype.forEach = function (fn, thisv) { var array = this.head.slice(this.offset), i, il; array.push.apply(array, this.tail); if (thisv) { for (i = 0, il = array.length; i < il; i += 1) { fn.call(thisv, array[i], i, array); } } else { for (i = 0, il = array.length; i < il; i += 1) { fn(array[i], i, array); } } return array; }; Queue.prototype.getLength = function () { return this.head.length - this.offset + this.tail.length; }; Object.defineProperty(Queue.prototype, "length", { get: function () { return this.getLength(); } }); if (typeof module !== "undefined" && module.exports) { module.exports = Queue; } node_redis-0.12.1/lib/to_array.js000066400000000000000000000003111237217724200166340ustar00rootroot00000000000000function to_array(args) { var len = args.length, arr = new Array(len), i; for (i = 0; i < len; i += 1) { arr[i] = args[i]; } return arr; } module.exports = to_array; node_redis-0.12.1/lib/util.js000066400000000000000000000003471237217724200160020ustar00rootroot00000000000000// Support for very old versions of node where the module was called "sys". At some point, we should abandon this. var util; try { util = require("util"); } catch (err) { util = require("sys"); } module.exports = util; node_redis-0.12.1/multi_bench.js000066400000000000000000000242061237217724200165500ustar00rootroot00000000000000var redis = require("./index"), metrics = require("metrics"), num_clients = parseInt(process.argv[2], 10) || 5, num_requests = 20000, tests = [], versions_logged = false, client_options = { return_buffers: false }, small_str, large_str, small_buf, large_buf; redis.debug_mode = false; function lpad(input, len, chr) { var str = input.toString(); chr = chr || " "; while (str.length < len) { str = chr + str; } return str; } metrics.Histogram.prototype.print_line = function () { var obj = this.printObj(); return lpad(obj.min, 4) + "/" + lpad(obj.max, 4) + "/" + lpad(obj.mean.toFixed(2), 7) + "/" + lpad(obj.p95.toFixed(2), 7); }; function Test(args) { this.args = args; this.callback = null; this.clients = []; this.clients_ready = 0; this.commands_sent = 0; this.commands_completed = 0; this.max_pipeline = this.args.pipeline || num_requests; this.client_options = args.client_options || client_options; this.connect_latency = new metrics.Histogram(); this.ready_latency = new metrics.Histogram(); this.command_latency = new metrics.Histogram(); } Test.prototype.run = function (callback) { var i; this.callback = callback; for (i = 0; i < num_clients ; i++) { this.new_client(i); } }; Test.prototype.new_client = function (id) { var self = this, new_client; new_client = redis.createClient(6379, "127.0.0.1", this.client_options); new_client.create_time = Date.now(); new_client.on("connect", function () { self.connect_latency.update(Date.now() - new_client.create_time); }); new_client.on("ready", function () { if (! versions_logged) { console.log("Client count: " + num_clients + ", node version: " + process.versions.node + ", server version: " + new_client.server_info.redis_version + ", parser: " + new_client.reply_parser.name); versions_logged = true; } self.ready_latency.update(Date.now() - new_client.create_time); self.clients_ready++; if (self.clients_ready === self.clients.length) { self.on_clients_ready(); } }); self.clients[id] = new_client; }; Test.prototype.on_clients_ready = function () { process.stdout.write(lpad(this.args.descr, 13) + ", " + lpad(this.args.pipeline, 5) + "/" + this.clients_ready + " "); this.test_start = Date.now(); this.fill_pipeline(); }; Test.prototype.fill_pipeline = function () { var pipeline = this.commands_sent - this.commands_completed; while (this.commands_sent < num_requests && pipeline < this.max_pipeline) { this.commands_sent++; pipeline++; this.send_next(); } if (this.commands_completed === num_requests) { this.print_stats(); this.stop_clients(); } }; Test.prototype.stop_clients = function () { var self = this; this.clients.forEach(function (client, pos) { if (pos === self.clients.length - 1) { client.quit(function (err, res) { self.callback(); }); } else { client.quit(); } }); }; Test.prototype.send_next = function () { var self = this, cur_client = this.commands_sent % this.clients.length, start = Date.now(); this.clients[cur_client][this.args.command](this.args.args, function (err, res) { if (err) { throw err; } self.commands_completed++; self.command_latency.update(Date.now() - start); self.fill_pipeline(); }); }; Test.prototype.print_stats = function () { var duration = Date.now() - this.test_start; console.log("min/max/avg/p95: " + this.command_latency.print_line() + " " + lpad(duration, 6) + "ms total, " + lpad((num_requests / (duration / 1000)).toFixed(2), 8) + " ops/sec"); }; small_str = "1234"; small_buf = new Buffer(small_str); large_str = (new Array(4097).join("-")); large_buf = new Buffer(large_str); tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 1})); tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 50})); tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 200})); tests.push(new Test({descr: "PING", command: "ping", args: [], pipeline: 20000})); tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 1})); tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 50})); tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 200})); tests.push(new Test({descr: "SET small str", command: "set", args: ["foo_rand000000000000", small_str], pipeline: 20000})); tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 1})); tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 50})); tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 200})); tests.push(new Test({descr: "SET small buf", command: "set", args: ["foo_rand000000000000", small_buf], pipeline: 20000})); tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 1})); tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 50})); tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 200})); tests.push(new Test({descr: "GET small str", command: "get", args: ["foo_rand000000000000"], pipeline: 20000})); tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 1, client_opts: { return_buffers: true} })); tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 50, client_opts: { return_buffers: true} })); tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 200, client_opts: { return_buffers: true} })); tests.push(new Test({descr: "GET small buf", command: "get", args: ["foo_rand000000000000"], pipeline: 20000, client_opts: { return_buffers: true} })); tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 1})); tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 50})); tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 200})); tests.push(new Test({descr: "SET large str", command: "set", args: ["foo_rand000000000001", large_str], pipeline: 20000})); tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 1})); tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 50})); tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 200})); tests.push(new Test({descr: "SET large buf", command: "set", args: ["foo_rand000000000001", large_buf], pipeline: 20000})); tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 1})); tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 50})); tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 200})); tests.push(new Test({descr: "GET large str", command: "get", args: ["foo_rand000000000001"], pipeline: 20000})); tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 1, client_opts: { return_buffers: true} })); tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 50, client_opts: { return_buffers: true} })); tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 200, client_opts: { return_buffers: true} })); tests.push(new Test({descr: "GET large buf", command: "get", args: ["foo_rand000000000001"], pipeline: 20000, client_opts: { return_buffers: true} })); tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 1})); tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 50})); tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 200})); tests.push(new Test({descr: "INCR", command: "incr", args: ["counter_rand000000000000"], pipeline: 20000})); tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 1})); tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 50})); tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 200})); tests.push(new Test({descr: "LPUSH", command: "lpush", args: ["mylist", small_str], pipeline: 20000})); tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 1})); tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 50})); tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 200})); tests.push(new Test({descr: "LRANGE 10", command: "lrange", args: ["mylist", "0", "9"], pipeline: 20000})); tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 1})); tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 50})); tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 200})); tests.push(new Test({descr: "LRANGE 100", command: "lrange", args: ["mylist", "0", "99"], pipeline: 20000})); function next() { var test = tests.shift(); if (test) { test.run(function () { next(); }); } else { console.log("End of tests."); process.exit(0); } } next(); node_redis-0.12.1/package.json000066400000000000000000000007061237217724200162060ustar00rootroot00000000000000{ "name": "redis", "version": "0.12.1", "description": "Redis client library", "keywords": [ "redis", "database" ], "author": "Matt Ranney ", "main": "./index.js", "scripts": { "test": "node ./test.js" }, "devDependencies": { "metrics": ">=0.1.5", "colors": "~0.6.0-1", "underscore": "~1.4.4" }, "repository": { "type": "git", "url": "git://github.com/mranney/node_redis.git" } } node_redis-0.12.1/test-unref.js000066400000000000000000000005101237217724200163430ustar00rootroot00000000000000var redis = require("./") //redis.debug_mode = true var PORT = process.argv[2] || 6379; var HOST = process.argv[3] || '127.0.0.1'; var c = redis.createClient(PORT, HOST) c.unref() c.info(function (err, reply) { if (err) process.exit(-1) if (!reply.length) process.exit(-1) process.stdout.write(reply.length.toString()) })node_redis-0.12.1/test.js000066400000000000000000002322131237217724200152350ustar00rootroot00000000000000/*global require console setTimeout process Buffer */ var PORT = 6379; var HOST = '127.0.0.1'; var redis = require("./index"), client = redis.createClient(PORT, HOST), client2 = redis.createClient(PORT, HOST), client3 = redis.createClient(PORT, HOST), bclient = redis.createClient(PORT, HOST, { return_buffers: true }), assert = require("assert"), crypto = require("crypto"), util = require("./lib/util"), fork = require("child_process").fork, test_db_num = 15, // this DB will be flushed and used for testing tests = {}, connected = false, ended = false, next, cur_start, run_next_test, all_tests, all_start, test_count; // Set this to truthy to see the wire protocol and other debugging info redis.debug_mode = process.argv[2]; function server_version_at_least(connection, desired_version) { // Return true if the server version >= desired_version var version = connection.server_info.versions; for (var i = 0; i < 3; i++) { if (version[i] > desired_version[i]) return true; if (version[i] < desired_version[i]) return false; } return true; } function buffers_to_strings(arr) { return arr.map(function (val) { return val.toString(); }); } function require_number(expected, label) { return function (err, results) { assert.strictEqual(null, err, label + " expected " + expected + ", got error: " + err); assert.strictEqual(expected, results, label + " " + expected + " !== " + results); assert.strictEqual(typeof results, "number", label); return true; }; } function require_number_any(label) { return function (err, results) { assert.strictEqual(null, err, label + " expected any number, got error: " + err); assert.strictEqual(typeof results, "number", label + " " + results + " is not a number"); return true; }; } function require_number_pos(label) { return function (err, results) { assert.strictEqual(null, err, label + " expected positive number, got error: " + err); assert.strictEqual(true, (results > 0), label + " " + results + " is not a positive number"); return true; }; } function require_string(str, label) { return function (err, results) { assert.strictEqual(null, err, label + " expected string '" + str + "', got error: " + err); assert.equal(str, results, label + " " + str + " does not match " + results); return true; }; } function require_null(label) { return function (err, results) { assert.strictEqual(null, err, label + " expected null, got error: " + err); assert.strictEqual(null, results, label + ": " + results + " is not null"); return true; }; } function require_error(label) { return function (err, results) { assert.notEqual(err, null, label + " err is null, but an error is expected here."); return true; }; } function is_empty_array(obj) { return Array.isArray(obj) && obj.length === 0; } function last(name, fn) { return function (err, results) { fn(err, results); next(name); }; } // Wraps the given callback in a timeout. If the returned function // is not called within the timeout period, we fail the named test. function with_timeout(name, cb, millis) { var timeoutId = setTimeout(function() { assert.fail("Callback timed out!", name); }, millis); return function() { clearTimeout(timeoutId); cb.apply(this, arguments); }; } next = function next(name) { console.log(" \x1b[33m" + (Date.now() - cur_start) + "\x1b[0m ms"); run_next_test(); }; // Tests are run in the order they are defined, so FLUSHDB should always be first. tests.IPV4 = function () { var ipv4Client = redis.createClient( PORT, "127.0.0.1", { "family" : "IPv4" } ); ipv4Client.once("ready", function start_tests() { console.log("Connected to " + ipv4Client.address + ", Redis server version " + ipv4Client.server_info.redis_version + "\n"); console.log("Using reply parser " + ipv4Client.reply_parser.name); ipv4Client.quit(); run_next_test(); }); ipv4Client.on('end', function () { }); // Exit immediately on connection failure, which triggers "exit", below, which fails the test ipv4Client.on("error", function (err) { console.error("client: " + err.stack); process.exit(); }); } tests.IPV6 = function () { if (!server_version_at_least(client, [2, 8, 0])) { console.log("Skipping IPV6 for old Redis server version < 2.8.0"); return run_next_test(); } var ipv6Client = redis.createClient( PORT, "::1", { "family" : "IPv6" } ); ipv6Client.once("ready", function start_tests() { console.log("Connected to " + ipv6Client.address + ", Redis server version " + ipv6Client.server_info.redis_version + "\n"); console.log("Using reply parser " + ipv6Client.reply_parser.name); ipv6Client.quit(); run_next_test(); }); ipv6Client.on('end', function () { }); // Exit immediately on connection failure, which triggers "exit", below, which fails the test ipv6Client.on("error", function (err) { console.error("client: " + err.stack); process.exit(); }); } tests.UNIX_SOCKET = function () { var unixClient = redis.createClient('/tmp/redis.sock'); // if this fails, check the permission of unix socket. // unixsocket /tmp/redis.sock // unixsocketperm 777 unixClient.once('ready', function start_tests(){ console.log("Connected to " + unixClient.address + ", Redis server version " + unixClient.server_info.redis_version + "\n"); console.log("Using reply parser " + unixClient.reply_parser.name); unixClient.quit(); run_next_test(); }); unixClient.on( 'end', function(){ }); // Exit immediately on connection failure, which triggers "exit", below, which fails the test unixClient.on("error", function (err) { console.error("client: " + err.stack); process.exit(); }); } tests.FLUSHDB = function () { var name = "FLUSHDB"; client.select(test_db_num, require_string("OK", name)); client2.select(test_db_num, require_string("OK", name)); client3.select(test_db_num, require_string("OK", name)); client.mset("flush keys 1", "flush val 1", "flush keys 2", "flush val 2", require_string("OK", name)); client.FLUSHDB(require_string("OK", name)); client.dbsize(last(name, require_number(0, name))); }; tests.INCR = function () { var name = "INCR"; if (bclient.reply_parser.name == "hiredis") { console.log("Skipping INCR buffer test with hiredis"); return next(name); } // Test incr with the maximum JavaScript number value. Since we are // returning buffers we should get back one more as a Buffer. bclient.set("seq", "9007199254740992", function (err, result) { assert.strictEqual(result.toString(), "OK"); bclient.incr("seq", function (err, result) { assert.strictEqual("9007199254740993", result.toString()); next(name); }); }); }; tests.MULTI_1 = function () { var name = "MULTI_1", multi1, multi2; // Provoke an error at queue time multi1 = client.multi(); multi1.mset("multifoo", "10", "multibar", "20", require_string("OK", name)); multi1.set("foo2", require_error(name)); multi1.incr("multifoo", require_number(11, name)); multi1.incr("multibar", require_number(21, name)); multi1.exec(function () { require_error(name); // Redis 2.6.5+ will abort transactions with errors // see: http://redis.io/topics/transactions var multibar_expected = 22; var multifoo_expected = 12; if (server_version_at_least(client, [2, 6, 5])) { multibar_expected = 1; multifoo_expected = 1; } // Confirm that the previous command, while containing an error, still worked. multi2 = client.multi(); multi2.incr("multibar", require_number(multibar_expected, name)); multi2.incr("multifoo", require_number(multifoo_expected, name)); multi2.exec(function (err, replies) { assert.strictEqual(multibar_expected, replies[0]); assert.strictEqual(multifoo_expected, replies[1]); next(name); }); }); }; tests.MULTI_2 = function () { var name = "MULTI_2"; // test nested multi-bulk replies client.multi([ ["mget", "multifoo", "multibar", function (err, res) { assert.strictEqual(2, res.length, name); assert.strictEqual("12", res[0].toString(), name); assert.strictEqual("22", res[1].toString(), name); }], ["set", "foo2", require_error(name)], ["incr", "multifoo", require_number(13, name)], ["incr", "multibar", require_number(23, name)] ]).exec(function (err, replies) { if (server_version_at_least(client, [2, 6, 5])) { assert.notEqual(err, null, name); assert.equal(replies, undefined, name); } else { assert.strictEqual(2, replies[0].length, name); assert.strictEqual("12", replies[0][0].toString(), name); assert.strictEqual("22", replies[0][1].toString(), name); assert.strictEqual("13", replies[1].toString()); assert.strictEqual("23", replies[2].toString()); } next(name); }); }; tests.MULTI_3 = function () { var name = "MULTI_3"; client.sadd("some set", "mem 1"); client.sadd("some set", "mem 2"); client.sadd("some set", "mem 3"); client.sadd("some set", "mem 4"); // make sure empty mb reply works client.del("some missing set"); client.smembers("some missing set", function (err, reply) { // make sure empty mb reply works assert.strictEqual(true, is_empty_array(reply), name); }); // test nested multi-bulk replies with empty mb elements. client.multi([ ["smembers", "some set"], ["del", "some set"], ["smembers", "some set"] ]) .scard("some set") .exec(function (err, replies) { assert.strictEqual(true, is_empty_array(replies[2]), name); next(name); }); }; tests.MULTI_4 = function () { var name = "MULTI_4"; client.multi() .mset('some', '10', 'keys', '20') .incr('some') .incr('keys') .mget('some', 'keys') .exec(function (err, replies) { assert.strictEqual(null, err); assert.equal('OK', replies[0]); assert.equal(11, replies[1]); assert.equal(21, replies[2]); assert.equal(11, replies[3][0].toString()); assert.equal(21, replies[3][1].toString()); next(name); }); }; tests.MULTI_5 = function () { var name = "MULTI_5"; // test nested multi-bulk replies with nulls. client.multi([ ["mget", ["multifoo", "some", "random value", "keys"]], ["incr", "multifoo"] ]) .exec(function (err, replies) { assert.strictEqual(replies.length, 2, name); assert.strictEqual(replies[0].length, 4, name); next(name); }); }; tests.MULTI_6 = function () { var name = "MULTI_6"; client.multi() .hmset("multihash", "a", "foo", "b", 1) .hmset("multihash", { extra: "fancy", things: "here" }) .hgetall("multihash") .exec(function (err, replies) { assert.strictEqual(null, err); assert.equal("OK", replies[0]); assert.equal(Object.keys(replies[2]).length, 4); assert.equal("foo", replies[2].a); assert.equal("1", replies[2].b); assert.equal("fancy", replies[2].extra); assert.equal("here", replies[2].things); next(name); }); }; tests.MULTI_7 = function () { var name = "MULTI_7"; if (bclient.reply_parser.name != "javascript") { console.log("Skipping wire-protocol test for 3rd-party parser"); return next(name); } var p = require("./lib/parser/javascript"); var parser = new p.Parser(false); var reply_count = 0; function check_reply(reply) { assert.deepEqual(reply, [['a']], "Expecting multi-bulk reply of [['a']]"); reply_count++; assert.notEqual(reply_count, 4, "Should only parse 3 replies"); } parser.on("reply", check_reply); parser.execute(new Buffer('*1\r\n*1\r\n$1\r\na\r\n')); parser.execute(new Buffer('*1\r\n*1\r')); parser.execute(new Buffer('\n$1\r\na\r\n')); parser.execute(new Buffer('*1\r\n*1\r\n')); parser.execute(new Buffer('$1\r\na\r\n')); next(name); }; tests.MULTI_EXCEPTION_1 = function() { var name = "MULTI_EXCEPTION_1"; if (!server_version_at_least(client, [2, 6, 5])) { console.log("Skipping " + name + " for old Redis server version < 2.6.5"); return next(name); } client.multi().set("foo").exec(function (err, reply) { assert(Array.isArray(err), "err should be an array"); assert.equal(2, err.length, "err should have 2 items"); assert(err[0].message.match(/ERR/), "First error message should contain ERR"); assert(err[1].message.match(/EXECABORT/), "First error message should contain EXECABORT"); next(name); }); }; tests.MULTI_8 = function () { var name = "MULTI_8", multi1, multi2; // Provoke an error at queue time multi1 = client.multi(); multi1.mset("multifoo_8", "10", "multibar_8", "20", require_string("OK", name)); multi1.set("foo2", require_error(name)); multi1.set("foo3", require_error(name)); multi1.incr("multifoo_8", require_number(11, name)); multi1.incr("multibar_8", require_number(21, name)); multi1.exec(function () { require_error(name); // Redis 2.6.5+ will abort transactions with errors // see: http://redis.io/topics/transactions var multibar_expected = 22; var multifoo_expected = 12; if (server_version_at_least(client, [2, 6, 5])) { multibar_expected = 1; multifoo_expected = 1; } // Confirm that the previous command, while containing an error, still worked. multi2 = client.multi(); multi2.incr("multibar_8", require_number(multibar_expected, name)); multi2.incr("multifoo_8", require_number(multifoo_expected, name)); multi2.exec(function (err, replies) { assert.strictEqual(multibar_expected, replies[0]); assert.strictEqual(multifoo_expected, replies[1]); next(name); }); }); }; tests.FWD_ERRORS_1 = function () { var name = "FWD_ERRORS_1"; var toThrow = new Error("Forced exception"); var recordedError = null; var originalHandlers = client3.listeners("error"); client3.removeAllListeners("error"); client3.once("error", function (err) { recordedError = err; }); client3.on("message", function (channel, data) { console.log("incoming"); if (channel == name) { assert.equal(data, "Some message"); throw toThrow; } }); client3.subscribe(name); client.publish(name, "Some message"); setTimeout(function () { client3.listeners("error").push(originalHandlers); assert.equal(recordedError, toThrow, "Should have caught our forced exception"); next(name); }, 150); }; tests.EVAL_1 = function () { var name = "EVAL_1"; if (!server_version_at_least(client, [2, 5, 0])) { console.log("Skipping " + name + " for old Redis server version < 2.5.x"); return next(name); } // test {EVAL - Lua integer -> Redis protocol type conversion} client.eval("return 100.5", 0, require_number(100, name)); // test {EVAL - Lua string -> Redis protocol type conversion} client.eval("return 'hello world'", 0, require_string("hello world", name)); // test {EVAL - Lua true boolean -> Redis protocol type conversion} client.eval("return true", 0, require_number(1, name)); // test {EVAL - Lua false boolean -> Redis protocol type conversion} client.eval("return false", 0, require_null(name)); // test {EVAL - Lua status code reply -> Redis protocol type conversion} client.eval("return {ok='fine'}", 0, require_string("fine", name)); // test {EVAL - Lua error reply -> Redis protocol type conversion} client.eval("return {err='this is an error'}", 0, require_error(name)); // test {EVAL - Lua table -> Redis protocol type conversion} client.eval("return {1,2,3,'ciao',{1,2}}", 0, function (err, res) { assert.strictEqual(5, res.length, name); assert.strictEqual(1, res[0], name); assert.strictEqual(2, res[1], name); assert.strictEqual(3, res[2], name); assert.strictEqual("ciao", res[3], name); assert.strictEqual(2, res[4].length, name); assert.strictEqual(1, res[4][0], name); assert.strictEqual(2, res[4][1], name); }); // test {EVAL - Are the KEYS and ARGS arrays populated correctly?} client.eval("return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "a", "b", "c", "d", function (err, res) { assert.strictEqual(4, res.length, name); assert.strictEqual("a", res[0], name); assert.strictEqual("b", res[1], name); assert.strictEqual("c", res[2], name); assert.strictEqual("d", res[3], name); }); // test {EVAL - parameters in array format gives same result} client.eval(["return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "a", "b", "c", "d"], function (err, res) { assert.strictEqual(4, res.length, name); assert.strictEqual("a", res[0], name); assert.strictEqual("b", res[1], name); assert.strictEqual("c", res[2], name); assert.strictEqual("d", res[3], name); }); // prepare sha sum for evalsha cache test var source = "return redis.call('get', 'sha test')", sha = crypto.createHash('sha1').update(source).digest('hex'); client.set("sha test", "eval get sha test", function (err, res) { if (err) throw err; // test {EVAL - is Lua able to call Redis API?} client.eval(source, 0, function (err, res) { require_string("eval get sha test", name)(err, res); // test {EVALSHA - Can we call a SHA1 if already defined?} client.evalsha(sha, 0, require_string("eval get sha test", name)); // test {EVALSHA - Do we get an error on non defined SHA1?} client.evalsha("ffffffffffffffffffffffffffffffffffffffff", 0, require_error(name)); }); }); // test {EVAL - Redis integer -> Lua type conversion} client.set("incr key", 0, function (err, reply) { if (err) throw err; client.eval("local foo = redis.call('incr','incr key')\n" + "return {type(foo),foo}", 0, function (err, res) { if (err) throw err; assert.strictEqual(2, res.length, name); assert.strictEqual("number", res[0], name); assert.strictEqual(1, res[1], name); }); }); client.set("bulk reply key", "bulk reply value", function (err, res) { // test {EVAL - Redis bulk -> Lua type conversion} client.eval("local foo = redis.call('get','bulk reply key'); return {type(foo),foo}", 0, function (err, res) { if (err) throw err; assert.strictEqual(2, res.length, name); assert.strictEqual("string", res[0], name); assert.strictEqual("bulk reply value", res[1], name); }); }); // test {EVAL - Redis multi bulk -> Lua type conversion} client.multi() .del("mylist") .rpush("mylist", "a") .rpush("mylist", "b") .rpush("mylist", "c") .exec(function (err, replies) { if (err) throw err; client.eval("local foo = redis.call('lrange','mylist',0,-1); return {type(foo),foo[1],foo[2],foo[3],# foo}", 0, function (err, res) { assert.strictEqual(5, res.length, name); assert.strictEqual("table", res[0], name); assert.strictEqual("a", res[1], name); assert.strictEqual("b", res[2], name); assert.strictEqual("c", res[3], name); assert.strictEqual(3, res[4], name); }); }); // test {EVAL - Redis status reply -> Lua type conversion} client.eval("local foo = redis.call('set','mykey','myval'); return {type(foo),foo['ok']}", 0, function (err, res) { if (err) throw err; assert.strictEqual(2, res.length, name); assert.strictEqual("table", res[0], name); assert.strictEqual("OK", res[1], name); }); // test {EVAL - Redis error reply -> Lua type conversion} client.set("error reply key", "error reply value", function (err, res) { if (err) throw err; client.eval("local foo = redis.pcall('incr','error reply key'); return {type(foo),foo['err']}", 0, function (err, res) { if (err) throw err; assert.strictEqual(2, res.length, name); assert.strictEqual("table", res[0], name); assert.strictEqual("ERR value is not an integer or out of range", res[1], name); }); }); // test {EVAL - Redis nil bulk reply -> Lua type conversion} client.del("nil reply key", function (err, res) { if (err) throw err; client.eval("local foo = redis.call('get','nil reply key'); return {type(foo),foo == false}", 0, function (err, res) { if (err) throw err; assert.strictEqual(2, res.length, name); assert.strictEqual("boolean", res[0], name); assert.strictEqual(1, res[1], name); next(name); }); }); }; tests.SCRIPT_LOAD = function() { var name = "SCRIPT_LOAD", command = "return 1", commandSha = crypto.createHash('sha1').update(command).digest('hex'); if (!server_version_at_least(client, [2, 6, 0])) { console.log("Skipping " + name + " for old Redis server version < 2.6.x"); return next(name); } bclient.script("load", command, function(err, result) { assert.strictEqual(result.toString(), commandSha); client.multi().script("load", command).exec(function(err, result) { assert.strictEqual(result[0].toString(), commandSha); client.multi([['script', 'load', command]]).exec(function(err, result) { assert.strictEqual(result[0].toString(), commandSha); next(name); }); }); }); }; tests.CLIENT_LIST = function() { var name = "CLIENT_LIST"; if (!server_version_at_least(client, [2, 4, 0])) { console.log("Skipping " + name + " for old Redis server version < 2.4.x"); return next(name); } var pattern = /^addr=/; if ( server_version_at_least(client, [2, 8, 12])) { pattern = /^id=\d+ addr=/; } function checkResult(result) { var lines = result.toString().split('\n').slice(0, -1); assert.strictEqual(lines.length, 4); assert(lines.every(function(line) { return line.match(pattern); })); } bclient.client("list", function(err, result) { console.log(result.toString()); checkResult(result); client.multi().client("list").exec(function(err, result) { console.log(result.toString()); checkResult(result); client.multi([['client', 'list']]).exec(function(err, result) { console.log(result.toString()); checkResult(result); next(name); }); }); }); }; tests.WATCH_MULTI = function () { var name = 'WATCH_MULTI', multi; if (!server_version_at_least(client, [2, 2, 0])) { console.log("Skipping " + name + " for old Redis server version < 2.2.x"); return next(name); } client.watch(name); client.incr(name); multi = client.multi(); multi.incr(name); multi.exec(last(name, require_null(name))); }; tests.WATCH_TRANSACTION = function () { var name = "WATCH_TRANSACTION"; if (!server_version_at_least(client, [2, 1, 0])) { console.log("Skipping " + name + " because server version isn't new enough."); return next(name); } // Test WATCH command aborting transactions, look for parser offset errors. client.set("unwatched", 200); client.set(name, 0); client.watch(name); client.incr(name); var multi = client.multi() .incr(name) .exec(function (err, replies) { // Failure expected because of pre-multi incr assert.strictEqual(replies, null, "Aborted transaction multi-bulk reply should be null."); client.get("unwatched", function (err, reply) { assert.equal(err, null, name); assert.equal(reply, 200, "Expected 200, got " + reply); next(name); }); }); client.set("unrelated", 100, function (err, reply) { assert.equal(err, null, name); assert.equal(reply, "OK", "Expected 'OK', got " + reply); }); }; tests.detect_buffers = function () { var name = "detect_buffers", detect_client = redis.createClient({detect_buffers: true}); detect_client.on("ready", function () { // single Buffer or String detect_client.set("string key 1", "string value"); detect_client.get("string key 1", require_string("string value", name)); detect_client.get(new Buffer("string key 1"), function (err, reply) { assert.strictEqual(null, err, name); assert.strictEqual(true, Buffer.isBuffer(reply), name); assert.strictEqual("", reply.inspect(), name); }); detect_client.hmset("hash key 2", "key 1", "val 1", "key 2", "val 2"); // array of Buffers or Strings detect_client.hmget("hash key 2", "key 1", "key 2", function (err, reply) { assert.strictEqual(null, err, name); assert.strictEqual(true, Array.isArray(reply), name); assert.strictEqual(2, reply.length, name); assert.strictEqual("val 1", reply[0], name); assert.strictEqual("val 2", reply[1], name); }); detect_client.hmget(new Buffer("hash key 2"), "key 1", "key 2", function (err, reply) { assert.strictEqual(null, err, name); assert.strictEqual(true, Array.isArray(reply)); assert.strictEqual(2, reply.length, name); assert.strictEqual(true, Buffer.isBuffer(reply[0])); assert.strictEqual(true, Buffer.isBuffer(reply[1])); assert.strictEqual("", reply[0].inspect(), name); assert.strictEqual("", reply[1].inspect(), name); }); // array of strings with undefined values (repro #344) detect_client.hmget("hash key 2", "key 3", "key 4", function(err, reply) { assert.strictEqual(null, err, name); assert.strictEqual(true, Array.isArray(reply), name); assert.strictEqual(2, reply.length, name); assert.equal(null, reply[0], name); assert.equal(null, reply[1], name); }); // Object of Buffers or Strings detect_client.hgetall("hash key 2", function (err, reply) { assert.strictEqual(null, err, name); assert.strictEqual("object", typeof reply, name); assert.strictEqual(2, Object.keys(reply).length, name); assert.strictEqual("val 1", reply["key 1"], name); assert.strictEqual("val 2", reply["key 2"], name); }); detect_client.hgetall(new Buffer("hash key 2"), function (err, reply) { assert.strictEqual(null, err, name); assert.strictEqual("object", typeof reply, name); assert.strictEqual(2, Object.keys(reply).length, name); assert.strictEqual(true, Buffer.isBuffer(reply["key 1"])); assert.strictEqual(true, Buffer.isBuffer(reply["key 2"])); assert.strictEqual("", reply["key 1"].inspect(), name); assert.strictEqual("", reply["key 2"].inspect(), name); }); detect_client.quit(function (err, res) { next(name); }); }); }; tests.socket_nodelay = function () { var name = "socket_nodelay", c1, c2, c3, ready_count = 0, quit_count = 0; c1 = redis.createClient({socket_nodelay: true}); c2 = redis.createClient({socket_nodelay: false}); c3 = redis.createClient(); function quit_check() { quit_count++; if (quit_count === 3) { next(name); } } function run() { assert.strictEqual(true, c1.options.socket_nodelay, name); assert.strictEqual(false, c2.options.socket_nodelay, name); assert.strictEqual(true, c3.options.socket_nodelay, name); c1.set(["set key 1", "set val"], require_string("OK", name)); c1.set(["set key 2", "set val"], require_string("OK", name)); c1.get(["set key 1"], require_string("set val", name)); c1.get(["set key 2"], require_string("set val", name)); c2.set(["set key 3", "set val"], require_string("OK", name)); c2.set(["set key 4", "set val"], require_string("OK", name)); c2.get(["set key 3"], require_string("set val", name)); c2.get(["set key 4"], require_string("set val", name)); c3.set(["set key 5", "set val"], require_string("OK", name)); c3.set(["set key 6", "set val"], require_string("OK", name)); c3.get(["set key 5"], require_string("set val", name)); c3.get(["set key 6"], require_string("set val", name)); c1.quit(quit_check); c2.quit(quit_check); c3.quit(quit_check); } function ready_check() { ready_count++; if (ready_count === 3) { run(); } } c1.on("ready", ready_check); c2.on("ready", ready_check); c3.on("ready", ready_check); }; tests.reconnect = function () { var name = "reconnect"; client.set("recon 1", "one"); client.set("recon 2", "two", function (err, res) { // Do not do this in normal programs. This is to simulate the server closing on us. // For orderly shutdown in normal programs, do client.quit() client.stream.destroy(); }); client.on("reconnecting", function on_recon(params) { client.on("connect", function on_connect() { client.select(test_db_num, require_string("OK", name)); client.get("recon 1", require_string("one", name)); client.get("recon 1", require_string("one", name)); client.get("recon 2", require_string("two", name)); client.get("recon 2", require_string("two", name)); client.removeListener("connect", on_connect); client.removeListener("reconnecting", on_recon); next(name); }); }); }; tests.reconnect_select_db_after_pubsub = function() { var name = "reconnect_select_db_after_pubsub"; client.select(test_db_num); client.set(name, "one"); client.subscribe('ChannelV', function (err, res) { client.stream.destroy(); }); client.on("reconnecting", function on_recon(params) { client.on("ready", function on_connect() { client.unsubscribe('ChannelV', function (err, res) { client.get(name, require_string("one", name)); client.removeListener("connect", on_connect); client.removeListener("reconnecting", on_recon); next(name); }); }); }); }; tests.select_error_emits_if_no_callback = function () { var prev = client.listeners("error")[0]; client.removeListener("error", prev); var name = "select_error_emits_if_no_callback"; var handler = with_timeout(name, function (err) { require_error(name)(err); client.removeListener('error', handler); client.on("error", prev); next(name); }, 500); client.on('error', handler); client.select(9999); }; tests.idle = function () { var name = "idle"; client.on("idle", function on_idle() { client.removeListener("idle", on_idle); next(name); }); client.set("idle", "test"); }; tests.HSET = function () { var key = "test hash", field1 = new Buffer("0123456789"), value1 = new Buffer("abcdefghij"), field2 = new Buffer(0), value2 = new Buffer(0), name = "HSET"; client.HSET(key, field1, value1, require_number(1, name)); client.HGET(key, field1, require_string(value1.toString(), name)); // Empty value client.HSET(key, field1, value2, require_number(0, name)); client.HGET([key, field1], require_string("", name)); // Empty key, empty value client.HSET([key, field2, value1], require_number(1, name)); client.HSET(key, field2, value2, last(name, require_number(0, name))); }; tests.HLEN = function () { var key = "test hash", field1 = new Buffer("0123456789"), value1 = new Buffer("abcdefghij"), field2 = new Buffer(0), value2 = new Buffer(0), name = "HSET", timeout = 1000; client.HSET(key, field1, value1, function (err, results) { client.HLEN(key, function (err, len) { assert.ok(2 === +len); next(name); }); }); }; tests.HMSET_BUFFER_AND_ARRAY = function () { // Saving a buffer and an array to the same key should not error var key = "test hash", field1 = "buffer", value1 = new Buffer("abcdefghij"), field2 = "array", value2 = ["array contents"], name = "HSET"; client.HMSET(key, field1, value1, field2, value2, last(name, require_string("OK", name))); }; // TODO - add test for HMSET with optional callbacks tests.HMGET = function () { var key1 = "test hash 1", key2 = "test hash 2", key3 = 123456789, name = "HMGET"; // redis-like hmset syntax client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value", require_string("OK", name)); // fancy hmset syntax client.HMSET(key2, { "0123456789": "abcdefghij", "some manner of key": "a type of value" }, require_string("OK", name)); // test for numeric key client.HMSET(key3, { "0123456789": "abcdefghij", "some manner of key": "a type of value" }, require_string("OK", name)); client.HMGET(key1, "0123456789", "some manner of key", function (err, reply) { assert.strictEqual("abcdefghij", reply[0].toString(), name); assert.strictEqual("a type of value", reply[1].toString(), name); }); client.HMGET(key2, "0123456789", "some manner of key", function (err, reply) { assert.strictEqual("abcdefghij", reply[0].toString(), name); assert.strictEqual("a type of value", reply[1].toString(), name); }); client.HMGET(key3, "0123456789", "some manner of key", function (err, reply) { assert.strictEqual("abcdefghij", reply[0].toString(), name); assert.strictEqual("a type of value", reply[1].toString(), name); }); client.HMGET(key1, ["0123456789"], function (err, reply) { assert.strictEqual("abcdefghij", reply[0], name); }); client.HMGET(key1, ["0123456789", "some manner of key"], function (err, reply) { assert.strictEqual("abcdefghij", reply[0], name); assert.strictEqual("a type of value", reply[1], name); }); client.HMGET(key1, "missing thing", "another missing thing", function (err, reply) { assert.strictEqual(null, reply[0], name); assert.strictEqual(null, reply[1], name); next(name); }); }; tests.HINCRBY = function () { var name = "HINCRBY"; client.hset("hash incr", "value", 10, require_number(1, name)); client.HINCRBY("hash incr", "value", 1, require_number(11, name)); client.HINCRBY("hash incr", "value 2", 1, last(name, require_number(1, name))); }; tests.SUBSCRIBE = function () { var client1 = client, msg_count = 0, name = "SUBSCRIBE"; client1.on("subscribe", function (channel, count) { if (channel === "chan1") { client2.publish("chan1", "message 1", require_number(1, name)); client2.publish("chan2", "message 2", require_number(1, name)); client2.publish("chan1", "message 3", require_number(1, name)); } }); client1.on("unsubscribe", function (channel, count) { if (count === 0) { // make sure this connection can go into and out of pub/sub mode client1.incr("did a thing", last(name, require_number(2, name))); } }); client1.on("message", function (channel, message) { msg_count += 1; assert.strictEqual("message " + msg_count, message.toString()); if (msg_count === 3) { client1.unsubscribe("chan1", "chan2"); } }); client1.set("did a thing", 1, require_string("OK", name)); client1.subscribe("chan1", "chan2", function (err, results) { assert.strictEqual(null, err, "result sent back unexpected error: " + err); assert.strictEqual("chan1", results.toString(), name); }); }; tests.UNSUB_EMPTY = function () { // test situation where unsubscribe reply[1] is null var name = "UNSUB_EMPTY"; client3.unsubscribe(); // unsubscribe from all so can test null client3.unsubscribe(); // reply[1] will be null next(name); }; tests.PUNSUB_EMPTY = function () { // test situation where punsubscribe reply[1] is null var name = "PUNSUB_EMPTY"; client3.punsubscribe(); // punsubscribe from all so can test null client3.punsubscribe(); // reply[1] will be null next(name); }; tests.UNSUB_EMPTY_CB = function () { var name = "UNSUB_EMPTY_CB"; // test hangs on older versions of redis, so skip if (!server_version_at_least(client, [2, 6, 11])) return next(name); // test situation where unsubscribe reply[1] is null client3.unsubscribe(); // unsubscribe from all so can test null client3.unsubscribe(function (err, results) { // reply[1] will be null assert.strictEqual(null, err, "unexpected error: " + err); next(name); }); }; tests.PUNSUB_EMPTY_CB = function () { var name = "PUNSUB_EMPTY_CB"; // test hangs on older versions of redis, so skip if (!server_version_at_least(client, [2, 6, 11])) return next(name); // test situation where punsubscribe reply[1] is null client3.punsubscribe(); // punsubscribe from all so can test null client3.punsubscribe(function (err, results) { // reply[1] will be null assert.strictEqual(null, err, "unexpected error: " + err); next(name); }); }; tests.SUB_UNSUB_SUB = function () { var name = "SUB_UNSUB_SUB"; // test hangs on older versions of redis, so skip if (!server_version_at_least(client, [2, 6, 11])) return next(name); client3.subscribe('chan3'); client3.unsubscribe('chan3'); client3.subscribe('chan3', function (err, results) { assert.strictEqual(null, err, "unexpected error: " + err); client2.publish('chan3', 'foo'); }); client3.on('message', function (channel, message) { assert.strictEqual(channel, 'chan3'); assert.strictEqual(message, 'foo'); client3.removeAllListeners(); next(name); }); }; tests.SUB_UNSUB_MSG_SUB = function () { var name = "SUB_UNSUB_MSG_SUB"; // test hangs on older versions of redis, so skip if (!server_version_at_least(client, [2, 6, 11])) return next(name); client3.subscribe('chan8'); client3.subscribe('chan9'); client3.unsubscribe('chan9'); client2.publish('chan8', 'something'); client3.subscribe('chan9', with_timeout(name, function (err, results) { next(name); }, 2000)); }; tests.PSUB_UNSUB_PMSG_SUB = function () { var name = "PSUB_UNSUB_PMSG_SUB"; // test hangs on older versions of redis, so skip if (!server_version_at_least(client, [2, 6, 11])) return next(name); client3.psubscribe('abc*'); client3.subscribe('xyz'); client3.unsubscribe('xyz'); client2.publish('abcd', 'something'); client3.subscribe('xyz', with_timeout(name, function (err, results) { next(name); }, 2000)); }; tests.SUBSCRIBE_QUIT = function () { var name = "SUBSCRIBE_QUIT"; client3.on("end", function () { next(name); }); client3.on("subscribe", function (channel, count) { client3.quit(); }); client3.subscribe("chan3"); }; tests.SUBSCRIBE_CLOSE_RESUBSCRIBE = function () { var name = "SUBSCRIBE_CLOSE_RESUBSCRIBE"; var c1 = redis.createClient(); var c2 = redis.createClient(); var count = 0; /* Create two clients. c1 subscribes to two channels, c2 will publish to them. c2 publishes the first message. c1 gets the message and drops its connection. It must resubscribe itself. When it resubscribes, c2 publishes the second message, on the same channel c1 gets the message and drops its connection. It must resubscribe itself, again. When it resubscribes, c2 publishes the third message, on the second channel c1 gets the message and drops its connection. When it reconnects, the test ends. */ c1.on("message", function(channel, message) { if (channel === "chan1") { assert.strictEqual(message, "hi on channel 1"); c1.stream.end(); } else if (channel === "chan2") { assert.strictEqual(message, "hi on channel 2"); c1.stream.end(); } else { c1.quit(); c2.quit(); assert.fail("test failed"); } }); c1.subscribe("chan1", "chan2"); c2.once("ready", function() { console.log("c2 is ready"); c1.on("ready", function(err, results) { console.log("c1 is ready", count); count++; if (count == 1) { c2.publish("chan1", "hi on channel 1"); return; } else if (count == 2) { c2.publish("chan2", "hi on channel 2"); } else { c1.quit(function() { c2.quit(function() { next(name); }); }); } }); c2.publish("chan1", "hi on channel 1"); }); }; tests.EXISTS = function () { var name = "EXISTS"; client.del("foo", "foo2", require_number_any(name)); client.set("foo", "bar", require_string("OK", name)); client.EXISTS("foo", require_number(1, name)); client.EXISTS("foo2", last(name, require_number(0, name))); }; tests.DEL = function () { var name = "DEL"; client.DEL("delkey", require_number_any(name)); client.set("delkey", "delvalue", require_string("OK", name)); client.DEL("delkey", require_number(1, name)); client.exists("delkey", require_number(0, name)); client.DEL("delkey", require_number(0, name)); client.mset("delkey", "delvalue", "delkey2", "delvalue2", require_string("OK", name)); client.DEL("delkey", "delkey2", last(name, require_number(2, name))); }; tests.TYPE = function () { var name = "TYPE"; client.set(["string key", "should be a string"], require_string("OK", name)); client.rpush(["list key", "should be a list"], require_number_pos(name)); client.sadd(["set key", "should be a set"], require_number_any(name)); client.zadd(["zset key", "10.0", "should be a zset"], require_number_any(name)); client.hset(["hash key", "hashtest", "should be a hash"], require_number_any(0, name)); client.TYPE(["string key"], require_string("string", name)); client.TYPE(["list key"], require_string("list", name)); client.TYPE(["set key"], require_string("set", name)); client.TYPE(["zset key"], require_string("zset", name)); client.TYPE("not here yet", require_string("none", name)); client.TYPE(["hash key"], last(name, require_string("hash", name))); }; tests.KEYS = function () { var name = "KEYS"; client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], require_string("OK", name)); client.KEYS(["test keys*"], function (err, results) { assert.strictEqual(null, err, "result sent back unexpected error: " + err); assert.strictEqual(2, results.length, name); assert.ok(~results.indexOf("test keys 1")); assert.ok(~results.indexOf("test keys 2")); next(name); }); }; tests.MULTIBULK = function() { var name = "MULTIBULK", keys_values = []; for (var i = 0; i < 200; i++) { var key_value = [ "multibulk:" + crypto.randomBytes(256).toString("hex"), // use long strings as keys to ensure generation of large packet "test val " + i ]; keys_values.push(key_value); } client.mset(keys_values.reduce(function(a, b) { return a.concat(b); }), require_string("OK", name)); client.KEYS("multibulk:*", function(err, results) { assert.strictEqual(null, err, "result sent back unexpected error: " + err); assert.deepEqual(keys_values.map(function(val) { return val[0]; }).sort(), results.sort(), name); }); next(name); }; tests.MULTIBULK_ZERO_LENGTH = function () { var name = "MULTIBULK_ZERO_LENGTH"; client.KEYS(['users:*'], function (err, results) { assert.strictEqual(null, err, 'error on empty multibulk reply'); assert.strictEqual(true, is_empty_array(results), "not an empty array"); next(name); }); }; tests.RANDOMKEY = function () { var name = "RANDOMKEY"; client.mset(["test keys 1", "test val 1", "test keys 2", "test val 2"], require_string("OK", name)); client.RANDOMKEY([], function (err, results) { assert.strictEqual(null, err, name + " result sent back unexpected error: " + err); assert.strictEqual(true, /\w+/.test(results), name); next(name); }); }; tests.RENAME = function () { var name = "RENAME"; client.set(['foo', 'bar'], require_string("OK", name)); client.RENAME(["foo", "new foo"], require_string("OK", name)); client.exists(["foo"], require_number(0, name)); client.exists(["new foo"], last(name, require_number(1, name))); }; tests.RENAMENX = function () { var name = "RENAMENX"; client.set(['foo', 'bar'], require_string("OK", name)); client.set(['foo2', 'bar2'], require_string("OK", name)); client.RENAMENX(["foo", "foo2"], require_number(0, name)); client.exists(["foo"], require_number(1, name)); client.exists(["foo2"], require_number(1, name)); client.del(["foo2"], require_number(1, name)); client.RENAMENX(["foo", "foo2"], require_number(1, name)); client.exists(["foo"], require_number(0, name)); client.exists(["foo2"], last(name, require_number(1, name))); }; tests.DBSIZE = function () { var name = "DBSIZE"; client.set(['foo', 'bar'], require_string("OK", name)); client.DBSIZE([], last(name, require_number_pos("DBSIZE"))); }; tests.GET_1 = function () { var name = "GET_1"; client.set(["get key", "get val"], require_string("OK", name)); client.GET(["get key"], last(name, require_string("get val", name))); }; tests.GET_2 = function() { var name = "GET_2"; // tests handling of non-existent keys client.GET('this_key_shouldnt_exist', last(name, require_null(name))); }; tests.SET = function () { var name = "SET"; client.SET(["set key", "set val"], require_string("OK", name)); client.get(["set key"], last(name, require_string("set val", name))); client.SET(["set key", undefined], require_error(name)); }; tests.GETSET = function () { var name = "GETSET"; client.set(["getset key", "getset val"], require_string("OK", name)); client.GETSET(["getset key", "new getset val"], require_string("getset val", name)); client.get(["getset key"], last(name, require_string("new getset val", name))); }; tests.MGET = function () { var name = "MGET"; client.mset(["mget keys 1", "mget val 1", "mget keys 2", "mget val 2", "mget keys 3", "mget val 3"], require_string("OK", name)); client.MGET("mget keys 1", "mget keys 2", "mget keys 3", function (err, results) { assert.strictEqual(null, err, "result sent back unexpected error: " + err); assert.strictEqual(3, results.length, name); assert.strictEqual("mget val 1", results[0].toString(), name); assert.strictEqual("mget val 2", results[1].toString(), name); assert.strictEqual("mget val 3", results[2].toString(), name); }); client.MGET(["mget keys 1", "mget keys 2", "mget keys 3"], function (err, results) { assert.strictEqual(null, err, "result sent back unexpected error: " + err); assert.strictEqual(3, results.length, name); assert.strictEqual("mget val 1", results[0].toString(), name); assert.strictEqual("mget val 2", results[1].toString(), name); assert.strictEqual("mget val 3", results[2].toString(), name); }); client.MGET(["mget keys 1", "some random shit", "mget keys 2", "mget keys 3"], function (err, results) { assert.strictEqual(null, err, "result sent back unexpected error: " + err); assert.strictEqual(4, results.length, name); assert.strictEqual("mget val 1", results[0].toString(), name); assert.strictEqual(null, results[1], name); assert.strictEqual("mget val 2", results[2].toString(), name); assert.strictEqual("mget val 3", results[3].toString(), name); next(name); }); }; tests.SETNX = function () { var name = "SETNX"; client.set(["setnx key", "setnx value"], require_string("OK", name)); client.SETNX(["setnx key", "new setnx value"], require_number(0, name)); client.del(["setnx key"], require_number(1, name)); client.exists(["setnx key"], require_number(0, name)); client.SETNX(["setnx key", "new setnx value"], require_number(1, name)); client.exists(["setnx key"], last(name, require_number(1, name))); }; tests.SETEX = function () { var name = "SETEX"; client.SETEX(["setex key", "100", "setex val"], require_string("OK", name)); client.exists(["setex key"], require_number(1, name)); client.ttl(["setex key"], last(name, require_number_pos(name))); client.SETEX(["setex key", "100", undefined], require_error(name)); }; tests.MSETNX = function () { var name = "MSETNX"; client.mset(["mset1", "val1", "mset2", "val2", "mset3", "val3"], require_string("OK", name)); client.MSETNX(["mset3", "val3", "mset4", "val4"], require_number(0, name)); client.del(["mset3"], require_number(1, name)); client.MSETNX(["mset3", "val3", "mset4", "val4"], require_number(1, name)); client.exists(["mset3"], require_number(1, name)); client.exists(["mset4"], last(name, require_number(1, name))); }; tests.HGETALL = function () { var name = "HGETALL"; client.hmset(["hosts", "mjr", "1", "another", "23", "home", "1234"], require_string("OK", name)); client.HGETALL(["hosts"], function (err, obj) { assert.strictEqual(null, err, name + " result sent back unexpected error: " + err); assert.strictEqual(3, Object.keys(obj).length, name); assert.strictEqual("1", obj.mjr.toString(), name); assert.strictEqual("23", obj.another.toString(), name); assert.strictEqual("1234", obj.home.toString(), name); next(name); }); }; tests.HGETALL_2 = function () { var name = "HGETALL (Binary client)"; bclient.hmset(["bhosts", "mjr", "1", "another", "23", "home", "1234", new Buffer([0xAA, 0xBB, 0x00, 0xF0]), new Buffer([0xCC, 0xDD, 0x00, 0xF0])], require_string("OK", name)); bclient.HGETALL(["bhosts"], function (err, obj) { assert.strictEqual(null, err, name + " result sent back unexpected error: " + err); assert.strictEqual(4, Object.keys(obj).length, name); assert.strictEqual("1", obj.mjr.toString(), name); assert.strictEqual("23", obj.another.toString(), name); assert.strictEqual("1234", obj.home.toString(), name); assert.strictEqual((new Buffer([0xAA, 0xBB, 0x00, 0xF0])).toString('binary'), Object.keys(obj)[3], name); assert.strictEqual((new Buffer([0xCC, 0xDD, 0x00, 0xF0])).toString('binary'), obj[(new Buffer([0xAA, 0xBB, 0x00, 0xF0])).toString('binary')].toString('binary'), name); next(name); }); }; tests.HGETALL_MESSAGE = function () { var name = "HGETALL_MESSAGE"; client.hmset("msg_test", {message: "hello"}, require_string("OK", name)); client.hgetall("msg_test", function (err, obj) { assert.strictEqual(null, err, name + " result sent back unexpected error: " + err); assert.strictEqual(1, Object.keys(obj).length, name); assert.strictEqual(obj.message, "hello") next(name); }); }; tests.HGETALL_NULL = function () { var name = "HGETALL_NULL"; client.hgetall("missing", function (err, obj) { assert.strictEqual(null, err); assert.strictEqual(null, obj); next(name); }); }; tests.UTF8 = function () { var name = "UTF8", utf8_sample = "ಠ_ಠ"; client.set(["utf8test", utf8_sample], require_string("OK", name)); client.get(["utf8test"], function (err, obj) { assert.strictEqual(null, err); assert.strictEqual(utf8_sample, obj); next(name); }); }; // Set tests were adapted from Brian Hammond's redis-node-client.js, which has a comprehensive test suite tests.SADD = function () { var name = "SADD"; client.del('set0'); client.SADD('set0', 'member0', require_number(1, name)); client.sadd('set0', 'member0', last(name, require_number(0, name))); }; tests.SADD2 = function () { var name = "SADD2"; client.del("set0"); client.sadd("set0", ["member0", "member1", "member2"], require_number(3, name)); client.smembers("set0", function (err, res) { assert.strictEqual(res.length, 3); assert.ok(~res.indexOf("member0")); assert.ok(~res.indexOf("member1")); assert.ok(~res.indexOf("member2")); }); client.SADD("set1", ["member0", "member1", "member2"], require_number(3, name)); client.smembers("set1", function (err, res) { assert.strictEqual(res.length, 3); assert.ok(~res.indexOf("member0")); assert.ok(~res.indexOf("member1")); assert.ok(~res.indexOf("member2")); next(name); }); }; tests.SISMEMBER = function () { var name = "SISMEMBER"; client.del('set0'); client.sadd('set0', 'member0', require_number(1, name)); client.sismember('set0', 'member0', require_number(1, name)); client.sismember('set0', 'member1', last(name, require_number(0, name))); }; tests.SCARD = function () { var name = "SCARD"; client.del('set0'); client.sadd('set0', 'member0', require_number(1, name)); client.scard('set0', require_number(1, name)); client.sadd('set0', 'member1', require_number(1, name)); client.scard('set0', last(name, require_number(2, name))); }; tests.SREM = function () { var name = "SREM"; client.del('set0'); client.sadd('set0', 'member0', require_number(1, name)); client.srem('set0', 'foobar', require_number(0, name)); client.srem('set0', 'member0', require_number(1, name)); client.scard('set0', last(name, require_number(0, name))); }; tests.SREM2 = function () { var name = "SREM2"; client.del("set0"); client.sadd("set0", ["member0", "member1", "member2"], require_number(3, name)); client.SREM("set0", ["member1", "member2"], require_number(2, name)); client.smembers("set0", function (err, res) { assert.strictEqual(res.length, 1); assert.ok(~res.indexOf("member0")); }); client.sadd("set0", ["member3", "member4", "member5"], require_number(3, name)); client.srem("set0", ["member0", "member6"], require_number(1, name)); client.smembers("set0", function (err, res) { assert.strictEqual(res.length, 3); assert.ok(~res.indexOf("member3")); assert.ok(~res.indexOf("member4")); assert.ok(~res.indexOf("member5")); next(name); }); }; tests.SPOP = function () { var name = "SPOP"; client.del('zzz'); client.sadd('zzz', 'member0', require_number(1, name)); client.scard('zzz', require_number(1, name)); client.spop('zzz', function (err, value) { if (err) { assert.fail(err); } assert.equal(value, 'member0', name); }); client.scard('zzz', last(name, require_number(0, name))); }; tests.SDIFF = function () { var name = "SDIFF"; client.del('foo'); client.sadd('foo', 'x', require_number(1, name)); client.sadd('foo', 'a', require_number(1, name)); client.sadd('foo', 'b', require_number(1, name)); client.sadd('foo', 'c', require_number(1, name)); client.sadd('bar', 'c', require_number(1, name)); client.sadd('baz', 'a', require_number(1, name)); client.sadd('baz', 'd', require_number(1, name)); client.sdiff('foo', 'bar', 'baz', function (err, values) { if (err) { assert.fail(err, name); } values.sort(); assert.equal(values.length, 2, name); assert.equal(values[0], 'b', name); assert.equal(values[1], 'x', name); next(name); }); }; tests.SDIFFSTORE = function () { var name = "SDIFFSTORE"; client.del('foo'); client.del('bar'); client.del('baz'); client.del('quux'); client.sadd('foo', 'x', require_number(1, name)); client.sadd('foo', 'a', require_number(1, name)); client.sadd('foo', 'b', require_number(1, name)); client.sadd('foo', 'c', require_number(1, name)); client.sadd('bar', 'c', require_number(1, name)); client.sadd('baz', 'a', require_number(1, name)); client.sadd('baz', 'd', require_number(1, name)); // NB: SDIFFSTORE returns the number of elements in the dstkey client.sdiffstore('quux', 'foo', 'bar', 'baz', require_number(2, name)); client.smembers('quux', function (err, values) { if (err) { assert.fail(err, name); } var members = buffers_to_strings(values).sort(); assert.deepEqual(members, [ 'b', 'x' ], name); next(name); }); }; tests.SMEMBERS = function () { var name = "SMEMBERS"; client.del('foo'); client.sadd('foo', 'x', require_number(1, name)); client.smembers('foo', function (err, members) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(members), [ 'x' ], name); }); client.sadd('foo', 'y', require_number(1, name)); client.smembers('foo', function (err, values) { if (err) { assert.fail(err, name); } assert.equal(values.length, 2, name); var members = buffers_to_strings(values).sort(); assert.deepEqual(members, [ 'x', 'y' ], name); next(name); }); }; tests.SMOVE = function () { var name = "SMOVE"; client.del('foo'); client.del('bar'); client.sadd('foo', 'x', require_number(1, name)); client.smove('foo', 'bar', 'x', require_number(1, name)); client.sismember('foo', 'x', require_number(0, name)); client.sismember('bar', 'x', require_number(1, name)); client.smove('foo', 'bar', 'x', last(name, require_number(0, name))); }; tests.SINTER = function () { var name = "SINTER"; client.del('sa'); client.del('sb'); client.del('sc'); client.sadd('sa', 'a', require_number(1, name)); client.sadd('sa', 'b', require_number(1, name)); client.sadd('sa', 'c', require_number(1, name)); client.sadd('sb', 'b', require_number(1, name)); client.sadd('sb', 'c', require_number(1, name)); client.sadd('sb', 'd', require_number(1, name)); client.sadd('sc', 'c', require_number(1, name)); client.sadd('sc', 'd', require_number(1, name)); client.sadd('sc', 'e', require_number(1, name)); client.sinter('sa', 'sb', function (err, intersection) { if (err) { assert.fail(err, name); } assert.equal(intersection.length, 2, name); assert.deepEqual(buffers_to_strings(intersection).sort(), [ 'b', 'c' ], name); }); client.sinter('sb', 'sc', function (err, intersection) { if (err) { assert.fail(err, name); } assert.equal(intersection.length, 2, name); assert.deepEqual(buffers_to_strings(intersection).sort(), [ 'c', 'd' ], name); }); client.sinter('sa', 'sc', function (err, intersection) { if (err) { assert.fail(err, name); } assert.equal(intersection.length, 1, name); assert.equal(intersection[0], 'c', name); }); // 3-way client.sinter('sa', 'sb', 'sc', function (err, intersection) { if (err) { assert.fail(err, name); } assert.equal(intersection.length, 1, name); assert.equal(intersection[0], 'c', name); next(name); }); }; tests.SINTERSTORE = function () { var name = "SINTERSTORE"; client.del('sa'); client.del('sb'); client.del('sc'); client.del('foo'); client.sadd('sa', 'a', require_number(1, name)); client.sadd('sa', 'b', require_number(1, name)); client.sadd('sa', 'c', require_number(1, name)); client.sadd('sb', 'b', require_number(1, name)); client.sadd('sb', 'c', require_number(1, name)); client.sadd('sb', 'd', require_number(1, name)); client.sadd('sc', 'c', require_number(1, name)); client.sadd('sc', 'd', require_number(1, name)); client.sadd('sc', 'e', require_number(1, name)); client.sinterstore('foo', 'sa', 'sb', 'sc', require_number(1, name)); client.smembers('foo', function (err, members) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(members), [ 'c' ], name); next(name); }); }; tests.SUNION = function () { var name = "SUNION"; client.del('sa'); client.del('sb'); client.del('sc'); client.sadd('sa', 'a', require_number(1, name)); client.sadd('sa', 'b', require_number(1, name)); client.sadd('sa', 'c', require_number(1, name)); client.sadd('sb', 'b', require_number(1, name)); client.sadd('sb', 'c', require_number(1, name)); client.sadd('sb', 'd', require_number(1, name)); client.sadd('sc', 'c', require_number(1, name)); client.sadd('sc', 'd', require_number(1, name)); client.sadd('sc', 'e', require_number(1, name)); client.sunion('sa', 'sb', 'sc', function (err, union) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(union).sort(), ['a', 'b', 'c', 'd', 'e'], name); next(name); }); }; tests.SUNIONSTORE = function () { var name = "SUNIONSTORE"; client.del('sa'); client.del('sb'); client.del('sc'); client.del('foo'); client.sadd('sa', 'a', require_number(1, name)); client.sadd('sa', 'b', require_number(1, name)); client.sadd('sa', 'c', require_number(1, name)); client.sadd('sb', 'b', require_number(1, name)); client.sadd('sb', 'c', require_number(1, name)); client.sadd('sb', 'd', require_number(1, name)); client.sadd('sc', 'c', require_number(1, name)); client.sadd('sc', 'd', require_number(1, name)); client.sadd('sc', 'e', require_number(1, name)); client.sunionstore('foo', 'sa', 'sb', 'sc', function (err, cardinality) { if (err) { assert.fail(err, name); } assert.equal(cardinality, 5, name); }); client.smembers('foo', function (err, members) { if (err) { assert.fail(err, name); } assert.equal(members.length, 5, name); assert.deepEqual(buffers_to_strings(members).sort(), ['a', 'b', 'c', 'd', 'e'], name); next(name); }); }; // SORT test adapted from Brian Hammond's redis-node-client.js, which has a comprehensive test suite tests.SORT = function () { var name = "SORT"; client.del('y'); client.del('x'); client.rpush('y', 'd', require_number(1, name)); client.rpush('y', 'b', require_number(2, name)); client.rpush('y', 'a', require_number(3, name)); client.rpush('y', 'c', require_number(4, name)); client.rpush('x', '3', require_number(1, name)); client.rpush('x', '9', require_number(2, name)); client.rpush('x', '2', require_number(3, name)); client.rpush('x', '4', require_number(4, name)); client.set('w3', '4', require_string("OK", name)); client.set('w9', '5', require_string("OK", name)); client.set('w2', '12', require_string("OK", name)); client.set('w4', '6', require_string("OK", name)); client.set('o2', 'buz', require_string("OK", name)); client.set('o3', 'foo', require_string("OK", name)); client.set('o4', 'baz', require_string("OK", name)); client.set('o9', 'bar', require_string("OK", name)); client.set('p2', 'qux', require_string("OK", name)); client.set('p3', 'bux', require_string("OK", name)); client.set('p4', 'lux', require_string("OK", name)); client.set('p9', 'tux', require_string("OK", name)); // Now the data has been setup, we can test. // But first, test basic sorting. // y = [ d b a c ] // sort y ascending = [ a b c d ] // sort y descending = [ d c b a ] client.sort('y', 'asc', 'alpha', function (err, sorted) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(sorted), ['a', 'b', 'c', 'd'], name); }); client.sort('y', 'desc', 'alpha', function (err, sorted) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(sorted), ['d', 'c', 'b', 'a'], name); }); // Now try sorting numbers in a list. // x = [ 3, 9, 2, 4 ] client.sort('x', 'asc', function (err, sorted) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(sorted), [2, 3, 4, 9], name); }); client.sort('x', 'desc', function (err, sorted) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(sorted), [9, 4, 3, 2], name); }); // Try sorting with a 'by' pattern. client.sort('x', 'by', 'w*', 'asc', function (err, sorted) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(sorted), [3, 9, 4, 2], name); }); // Try sorting with a 'by' pattern and 1 'get' pattern. client.sort('x', 'by', 'w*', 'asc', 'get', 'o*', function (err, sorted) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(sorted), ['foo', 'bar', 'baz', 'buz'], name); }); // Try sorting with a 'by' pattern and 2 'get' patterns. client.sort('x', 'by', 'w*', 'asc', 'get', 'o*', 'get', 'p*', function (err, sorted) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(sorted), ['foo', 'bux', 'bar', 'tux', 'baz', 'lux', 'buz', 'qux'], name); }); // Try sorting with a 'by' pattern and 2 'get' patterns. // Instead of getting back the sorted set/list, store the values to a list. // Then check that the values are there in the expected order. client.sort('x', 'by', 'w*', 'asc', 'get', 'o*', 'get', 'p*', 'store', 'bacon', function (err) { if (err) { assert.fail(err, name); } }); client.lrange('bacon', 0, -1, function (err, values) { if (err) { assert.fail(err, name); } assert.deepEqual(buffers_to_strings(values), ['foo', 'bux', 'bar', 'tux', 'baz', 'lux', 'buz', 'qux'], name); next(name); }); // TODO - sort by hash value }; tests.MONITOR = function () { var name = "MONITOR", responses = [], monitor_client; if (!server_version_at_least(client, [2, 6, 0])) { console.log("Skipping " + name + " for old Redis server version < 2.6.x"); return next(name); } monitor_client = redis.createClient(); monitor_client.monitor(function (err, res) { client.mget("some", "keys", "foo", "bar"); client.set("json", JSON.stringify({ foo: "123", bar: "sdflkdfsjk", another: false })); }); monitor_client.on("monitor", function (time, args) { // skip monitor command for Redis <= 2.4.16 if (args[0] === "monitor") return; responses.push(args); if (responses.length === 2) { assert.strictEqual(5, responses[0].length); assert.strictEqual("mget", responses[0][0]); assert.strictEqual("some", responses[0][1]); assert.strictEqual("keys", responses[0][2]); assert.strictEqual("foo", responses[0][3]); assert.strictEqual("bar", responses[0][4]); assert.strictEqual(3, responses[1].length); assert.strictEqual("set", responses[1][0]); assert.strictEqual("json", responses[1][1]); assert.strictEqual('{"foo":"123","bar":"sdflkdfsjk","another":false}', responses[1][2]); monitor_client.quit(function (err, res) { next(name); }); } }); }; tests.BLPOP = function () { var name = "BLPOP"; client.rpush("blocking list", "initial value", function (err, res) { client2.BLPOP("blocking list", 0, function (err, res) { assert.strictEqual("blocking list", res[0].toString()); assert.strictEqual("initial value", res[1].toString()); client.rpush("blocking list", "wait for this value"); }); client2.BLPOP("blocking list", 0, function (err, res) { assert.strictEqual("blocking list", res[0].toString()); assert.strictEqual("wait for this value", res[1].toString()); next(name); }); }); }; tests.BLPOP_TIMEOUT = function () { var name = "BLPOP_TIMEOUT"; // try to BLPOP the list again, which should be empty. This should timeout and return null. client2.BLPOP("blocking list", 1, function (err, res) { if (err) { throw err; } assert.strictEqual(res, null); next(name); }); }; tests.EXPIRE = function () { var name = "EXPIRE"; client.set(['expiry key', 'bar'], require_string("OK", name)); client.EXPIRE(["expiry key", "1"], require_number_pos(name)); setTimeout(function () { client.exists(["expiry key"], last(name, require_number(0, name))); }, 2000); }; tests.TTL = function () { var name = "TTL"; client.set(["ttl key", "ttl val"], require_string("OK", name)); client.expire(["ttl key", "100"], require_number_pos(name)); setTimeout(function () { client.TTL(["ttl key"], last(name, require_number_pos(0, name))); }, 500); }; tests.OPTIONAL_CALLBACK = function () { var name = "OPTIONAL_CALLBACK"; client.del("op_cb1"); client.set("op_cb1", "x"); client.get("op_cb1", last(name, require_string("x", name))); }; tests.OPTIONAL_CALLBACK_UNDEFINED = function () { var name = "OPTIONAL_CALLBACK_UNDEFINED"; client.del("op_cb2"); client.set("op_cb2", "y", undefined); client.get("op_cb2", last(name, require_string("y", name))); client.set("op_cb_undefined", undefined, undefined); }; tests.ENABLE_OFFLINE_QUEUE_TRUE = function () { var name = "ENABLE_OFFLINE_QUEUE_TRUE"; var cli = redis.createClient(9999, null, { max_attempts: 1 // default :) // enable_offline_queue: true }); cli.on('error', function(e) { // ignore, b/c expecting a "can't connect" error }); return setTimeout(function() { cli.set(name, name, function(err, result) { assert.ifError(err); }); return setTimeout(function(){ assert.strictEqual(cli.offline_queue.length, 1); return next(name); }, 25); }, 50); }; tests.ENABLE_OFFLINE_QUEUE_FALSE = function () { var name = "ENABLE_OFFLINE_QUEUE_FALSE"; var cli = redis.createClient(9999, null, { max_attempts: 1, enable_offline_queue: false }); cli.on('error', function() { // ignore, see above }); assert.throws(function () { cli.set(name, name) }) assert.doesNotThrow(function () { cli.set(name, name, function (err) { // should callback with an error assert.ok(err); setTimeout(function () { next(name); }, 50); }); }); }; tests.SLOWLOG = function () { var name = "SLOWLOG"; client.config("set", "slowlog-log-slower-than", 0, require_string("OK", name)); client.slowlog("reset", require_string("OK", name)); client.set("foo", "bar", require_string("OK", name)); client.get("foo", require_string("bar", name)); client.slowlog("get", function (err, res) { assert.equal(res.length, 3, name); assert.equal(res[0][3].length, 2, name); assert.deepEqual(res[1][3], ["set", "foo", "bar"], name); assert.deepEqual(res[2][3], ["slowlog", "reset"], name); client.config("set", "slowlog-log-slower-than", 10000, require_string("OK", name)); next(name); }); } tests.DOMAIN = function () { var name = "DOMAIN"; var domain; try { domain = require('domain').create(); } catch (err) { console.log("Skipping " + name + " because this version of node doesn't have domains."); next(name); } if (domain) { domain.run(function () { client.set('domain', 'value', function (err, res) { assert.ok(process.domain); var notFound = res.not.existing.thing; // ohhh nooooo }); }); // this is the expected and desired behavior domain.on('error', function (err) { next(name); }); } }; // TODO - need a better way to test auth, maybe auto-config a local Redis server or something. // Yes, this is the real password. Please be nice, thanks. tests.auth = function () { var name = "AUTH", client4, ready_count = 0; client4 = redis.createClient(9006, "filefish.redistogo.com"); client4.auth("664b1b6aaf134e1ec281945a8de702a9", function (err, res) { assert.strictEqual(null, err, name); assert.strictEqual("OK", res.toString(), name); }); // test auth, then kill the connection so it'll auto-reconnect and auto-re-auth client4.on("ready", function () { ready_count++; if (ready_count === 1) { client4.stream.destroy(); } else { client4.quit(function (err, res) { next(name); }); } }); }; tests.auth2 = function () { var name = "AUTH2", client4, ready_count = 0; client4 = redis.createClient(9006, "filefish.redistogo.com", {auth_pass: "664b1b6aaf134e1ec281945a8de702a9"}); // test auth, then kill the connection so it'll auto-reconnect and auto-re-auth client4.on("ready", function () { ready_count++; if (ready_count === 1) { client4.stream.destroy(); } else { client4.quit(function (err, res) { next(name); }); } }); }; tests.reconnectRetryMaxDelay = function() { var time = new Date().getTime(), name = 'reconnectRetryMaxDelay', reconnecting = false; var client = redis.createClient(PORT, HOST, { retry_max_delay: 1 }); client.on('ready', function() { if (!reconnecting) { reconnecting = true; client.retry_delay = 1000; client.retry_backoff = 1; client.stream.end(); } else { client.end(); var lasted = new Date().getTime() - time; assert.ok(lasted < 1000); next(name); } }); }; tests.unref = function () { var name = "unref"; var external = fork("./test-unref.js"); var done = false; external.on("close", function (code) { assert(code == 0, "test-unref.js failed"); done = true; }) setTimeout(function () { if (!done) { external.kill(); } assert(done, "test-unref.js didn't finish in time."); next(name); }, 500); }; all_tests = Object.keys(tests); all_start = new Date(); test_count = 0; run_next_test = function run_next_test() { var test_name = all_tests.shift(); if (typeof tests[test_name] === "function") { util.print('- \x1b[1m' + test_name.toLowerCase() + '\x1b[0m:'); cur_start = new Date(); test_count += 1; tests[test_name](); } else { console.log('\n completed \x1b[32m%d\x1b[0m tests in \x1b[33m%d\x1b[0m ms\n', test_count, new Date() - all_start); client.quit(); client2.quit(); bclient.quit(); } }; client.once("ready", function start_tests() { console.log("Connected to " + client.address + ", Redis server version " + client.server_info.redis_version + "\n"); console.log("Using reply parser " + client.reply_parser.name); run_next_test(); connected = true; }); client.on('end', function () { ended = true; }); // Exit immediately on connection failure, which triggers "exit", below, which fails the test client.on("error", function (err) { console.error("client: " + err.stack); process.exit(); }); client2.on("error", function (err) { console.error("client2: " + err.stack); process.exit(); }); client3.on("error", function (err) { console.error("client3: " + err.stack); process.exit(); }); bclient.on("error", function (err) { console.error("bclient: " + err.stack); process.exit(); }); client.on("reconnecting", function (params) { console.log("reconnecting: " + util.inspect(params)); }); process.on('uncaughtException', function (err) { console.error("Uncaught exception: " + err.stack); process.exit(1); }); process.on('exit', function (code) { assert.equal(true, connected); assert.equal(true, ended); });