From c0cc0bfab44d5ce5fa2fdc5c9af85401c4ff0b5c Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 02:05:21 +0000 Subject: [PATCH 01/13] start of work towards v3 release --- .github/FUNDING.yml | 1 + .npmignore | 6 ++ .travis.yml | 1 - changelog.md => CHANGELOG.md | 29 +++++++ CODE_OF_CONDUCT.md | 46 ++++++++++ CONTRIBUTING.md | 1 + LICENSE | 6 +- README.md | 83 +++++------------- appveyor.yml | 1 - index.js | 143 +++++++++++-------------------- package.json | 40 ++++++--- test/auth.spec.js | 2 +- test/commands/hset.spec.js | 18 ++-- test/commands/set.spec.js | 12 ++- test/connection.spec.js | 67 +++------------ test/lib/redis-process.js | 2 +- test/multi.spec.js | 20 +++-- test/node_redis.spec.js | 158 ++--------------------------------- 18 files changed, 235 insertions(+), 401 deletions(-) create mode 100644 .github/FUNDING.yml rename changelog.md => CHANGELOG.md (97%) create mode 100644 CODE_OF_CONDUCT.md create mode 100644 CONTRIBUTING.md diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 00000000000..1893f87aadd --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +open_collective: node-redis diff --git a/.npmignore b/.npmignore index 569f3efb2b8..497486e4e0d 100644 --- a/.npmignore +++ b/.npmignore @@ -11,3 +11,9 @@ coverage/ *.rdb *.out *.yml +CHANGELOG.md +CONTRIBUTING.md +CODE_OF_CONDUCT.md +.travis.yml +appveyor.yml +package-lock.json diff --git a/.travis.yml b/.travis.yml index e0c8e7d152c..d1bd2e3cef0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,6 @@ addons: packages: - g++-4.8 node_js: - - "4" - "6" - "8" - "12" diff --git a/changelog.md b/CHANGELOG.md similarity index 97% rename from changelog.md rename to CHANGELOG.md index d650f9ba394..daea8eeefc0 100644 --- a/changelog.md +++ b/CHANGELOG.md @@ -1,5 +1,34 @@ # Changelog +## v.3.0.0 - 09 Feb, 2020 + +This version is mainly a release to distribute all the unreleased changes on master since 2017 and additionally removes +a lot of old deprecated features and old internals in preparation for an upcoming modernization refactor (v4). + +### Breaking Changes + + - Dropped support for Node.js < 6 + - Dropped support for `hiredis` (no longer required) + - Removed previously deprecated `drain` event + - Removed previously deprecated `idle` event + - Removed previously deprecated `parser` option + - Removed previously deprecated `max_delay` option + - Removed previously deprecated `max_attempts` option + - Removed previously deprecated `socket_no_delay` option + +### Bug Fixes + + - Removed development files from published package (#1370) + - Duplicate function now allows db param to be passed (#1311) + +### Features + + - Upgraded to latest `redis-commands` package + - Upgraded to latest `redis-parser` package, v3.0.0, which brings performance improvements + - Replaced `double-ended-queue` with `denque`, which brings performance improvements + - Add timestamps to debug traces + - Add `socket_initial_delay` option for `socket.setKeepAlive` (#1396) + ## v.2.8.0 - 31 Jul, 2017 Features diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000000..00aad77d088 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,46 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at `redis @ invertase.io`. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000000..cf873b4c072 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1 @@ + diff --git a/LICENSE b/LICENSE index 710407f442b..7b7ba1393b4 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ -LICENSE - "MIT License" +MIT License -Copyright (c) 2016 by NodeRedis +Copyright (c) Node Redis contributors. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation @@ -21,4 +21,4 @@ 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. \ No newline at end of file +OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 91695a8b8bc..e4f60d3ab32 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,21 @@ -redis - a node.js redis client -=========================== - -[![Build Status](https://travis-ci.org/NodeRedis/node_redis.svg?branch=master)](https://travis-ci.org/NodeRedis/node_redis) -[![Coverage Status](https://coveralls.io/repos/NodeRedis/node_redis/badge.svg?branch=)](https://coveralls.io/r/NodeRedis/node_redis?branch=) -[![Windows Tests](https://img.shields.io/appveyor/ci/BridgeAR/node-redis/master.svg?label=Windows%20Tests)](https://ci.appveyor.com/project/BridgeAR/node-redis/branch/master) -[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/NodeRedis/node_redis?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) +

+ + + +

Node Redis

+ A high performance Node.js Redis client. +

+ +

+ NPM downloads + NPM version + Build Status + Coverage Status + Windows Tests + Follow on Twitter +

+ +--- This is a complete and feature rich Redis client for node.js. __It supports all Redis commands__ and focuses on high performance. @@ -195,30 +206,11 @@ So please attach the error listener to node_redis. `client` will emit `end` when an established Redis server connection has closed. -### "drain" (deprecated) - -`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. - -If the stream is buffering `client.should_buffer` is set to true. Otherwise the -variable is always set to false. That way you can decide when to reduce your -send rate and resume sending commands when you get `drain`. - -You can also check the return value of each command as it will also return the -backpressure indicator (deprecated). If false is returned the stream had to -buffer. - ### "warning" `client` will emit `warning` when password was set but none is needed and if a deprecated option / function / similar is used. -### "idle" (deprecated) - -`client` will emit `idle` when there are no outstanding commands that are -awaiting a response. - ## redis.createClient() If you have `redis-server` running on the same machine as node, then the defaults for port and host are probably fine and you don't need to supply any @@ -242,17 +234,13 @@ __Note:__ Using `'rediss://...` for the protocol in a `redis_url` will enable a | port | 6379 | Port of the Redis server | | path | null | The UNIX socket string of the Redis server | | url | null | The URL of the Redis server. Format: `[redis[s]:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]` (More info avaliable at [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis)). | -| parser | javascript | __Deprecated__ Use either the built-in JS parser [`javascript`]() or the native [`hiredis`]() parser. __Note__ `node_redis` < 2.6 uses hiredis as default if installed. This changed in v.2.6.0. | | string_numbers | null | Set to `true`, `node_redis` will return Redis number values as Strings instead of javascript Numbers. Useful if you need to handle big numbers (above `Number.MAX_SAFE_INTEGER === 2^53`). Hiredis is incapable of this behavior, so setting this option to `true` will result in the built-in javascript parser being used no matter the value of the `parser` option. | | return_buffers | false | If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings. | | detect_buffers | false | If set to `true`, then replies will be sent to callbacks as Buffers. This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to every command on a client. __Note__: This doesn't work properly with the pubsub mode. A subscriber has to either always return Strings or Buffers. | | socket_keepalive | true | If set to `true`, the keep-alive functionality is enabled on the underlying socket. | -| socket_initialdelay | 0 | Initial Delay in milliseconds, and this will also behave the interval keep alive message sending to Redis. | +| socket_initial_delay | 0 | Initial Delay in milliseconds, and this will also behave the interval keep alive message sending to Redis. | | no_ready_check | false | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will 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 | 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 executed immediately with an error, or an error will be emitted if no callback is specified. | -| retry_max_delay | null | __Deprecated__ _Please use `retry_strategy` instead._ By default, every time the client tries to connect and fails, the reconnection delay almost doubles. This delay normally grows infinitely, but setting `retry_max_delay` limits it to the maximum value provided in milliseconds. | -| connect_timeout | 3600000 | __Deprecated__ _Please use `retry_strategy` instead._ Setting `connect_timeout` limits the total time for the client to connect and reconnect. The value is provided in milliseconds and is counted from the moment a new client is created or from the time the connection is lost. The last retry is going to happen exactly at the timeout time. Default is to try connecting until the default system socket timeout has been exceeded and to try reconnecting until 1h has elapsed. | -| max_attempts | 0 | __Deprecated__ _Please use `retry_strategy` instead._ By default, a client will try reconnecting until connected. Setting `max_attempts` limits total amount of connection attempts. Setting this to 1 will prevent any reconnect attempt. | | retry_unfulfilled_commands | false | If set to `true`, all commands that were unfulfilled while the connection is lost will be retried after the connection has been reestablished. Use this with caution if you use state altering commands (e.g. `incr`). This is especially useful if you use blocking commands. | | password | null | If set, client will run Redis auth command on connect. Alias `auth_pass` __Note__ `node_redis` < 2.5 must use `auth_pass` | | db | null | If set, client will run Redis `select` command on connect. | @@ -473,12 +461,6 @@ client.hgetall("hosts", function (err, obj) { }); ``` -Output: - -```js -{ mjr: '1', another: '23', home: '1234' } -``` - ### client.hmset(hash, obj[, callback]) Multiple values in a hash can be set by supplying an object: @@ -770,7 +752,7 @@ clients.alterer = clients.watcher.duplicate(); clients.watcher.watch('foo',function(err) { if (err) { throw err; } //if you comment out the next line, the transaction will work - clients.alterer.set('foo',Math.random(), (err) => {if (err) { throw err; }}) + clients.alterer.set('foo',Math.random(), (err) => {if (err) { throw err; }}); //using a setTimeout here to ensure that the MULTI/EXEC will come after the SET. //Normally, you would use a callback to ensure order, but I want the above SET command @@ -897,8 +879,8 @@ the second word as first parameter: ```js client.script('load', 'return 1'); -client.multi().script('load', 'return 1').exec(...); -client.multi([['script', 'load', 'return 1']]).exec(...); +client.multi().script('load', 'return 1').exec(); +client.multi([['script', 'load', 'return 1']]).exec(); ``` ## client.duplicate([options][, callback]) @@ -1082,6 +1064,7 @@ ReplyError: ERR wrong number of arguments for 'set' command ``` ## How to Contribute + - Open a pull request or an issue about what you want to implement / change. We're glad for any help! - Please be aware that we'll only accept fully tested code. @@ -1097,23 +1080,3 @@ contributed to `node_redis` too. Thanks to all of them! ## License [MIT](LICENSE) - -### Consolidation: It's time for celebration - -Right now there are two great redis clients around and both have some advantages -above each other. We speak about ioredis and node_redis. So after talking to -each other about how we could improve in working together we (that is @luin and -@BridgeAR) decided to work towards a single library on the long run. But step by -step. - -First of all, we want to split small parts of our libraries into others so that -we're both able to use the same code. Those libraries are going to be maintained -under the NodeRedis organization. This is going to reduce the maintenance -overhead, allows others to use the very same code, if they need it and it's way -easyer for others to contribute to both libraries. - -We're very happy about this step towards working together as we both want to -give you the best redis experience possible. - -If you want to join our cause by help maintaining something, please don't -hesitate to contact either one of us. diff --git a/appveyor.yml b/appveyor.yml index d9cd4f4b635..f8c16e9a4de 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -3,7 +3,6 @@ # Test against these versions of Node.js. environment: matrix: - - nodejs_version: "4" - nodejs_version: "6" - nodejs_version: "8" - nodejs_version: "10" diff --git a/index.js b/index.js index b5acf785eb7..bdbf46daf51 100644 --- a/index.js +++ b/index.js @@ -67,38 +67,16 @@ function RedisClient (options, stream) { cnx_options.family = (!options.family && net.isIP(cnx_options.host)) || (options.family === 'IPv6' ? 6 : 4); this.address = cnx_options.host + ':' + cnx_options.port; } - // Warn on misusing deprecated functions - if (typeof options.retry_strategy === 'function') { - if ('max_attempts' in options) { - self.warn('WARNING: You activated the retry_strategy and max_attempts at the same time. This is not possible and max_attempts will be ignored.'); - // Do not print deprecation warnings twice - delete options.max_attempts; - } - if ('retry_max_delay' in options) { - self.warn('WARNING: You activated the retry_strategy and retry_max_delay at the same time. This is not possible and retry_max_delay will be ignored.'); - // Do not print deprecation warnings twice - delete options.retry_max_delay; - } - } this.connection_options = cnx_options; this.connection_id = RedisClient.connection_id++; this.connected = false; this.ready = false; - if (options.socket_nodelay === undefined) { - options.socket_nodelay = true; - } else if (!options.socket_nodelay) { // Only warn users with this set to false - self.warn( - 'socket_nodelay is deprecated and will be removed in v.3.0.0.\n' + - 'Setting socket_nodelay to false likely results in a reduced throughput. Please use .batch for pipelining instead.\n' + - 'If you are sure you rely on the NAGLE-algorithm you can activate it by calling client.stream.setNoDelay(false) instead.' - ); - } if (options.socket_keepalive === undefined) { options.socket_keepalive = true; } - if (options.socket_initialdelay === undefined) { - options.socket_initialdelay = 0; + if (options.socket_initial_delay === undefined) { + options.socket_initial_delay = 0; // set default to 0, which is aligned to https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay } for (var command in options.rename_commands) { @@ -116,14 +94,6 @@ function RedisClient (options, stream) { this.handle_reply = handle_detect_buffers_reply; } this.should_buffer = false; - this.max_attempts = options.max_attempts | 0; - if ('max_attempts' in options) { - self.warn( - 'max_attempts is deprecated and will be removed in v.3.0.0.\n' + - 'To reduce the number of options and to improve the reconnection handling please use the new `retry_strategy` option instead.\n' + - 'This replaces the max_attempts and retry_max_delay option.' - ); - } 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.pipeline_queue = new Queue(); // Holds all pipelined commands @@ -131,14 +101,6 @@ function RedisClient (options, stream) { // This should be done by the retry_strategy. Instead it should only be the timeout for connecting to redis this.connect_timeout = +options.connect_timeout || 3600000; // 60 * 60 * 1000 ms this.enable_offline_queue = options.enable_offline_queue === false ? false : true; - this.retry_max_delay = +options.retry_max_delay || null; - if ('retry_max_delay' in options) { - self.warn( - 'retry_max_delay is deprecated and will be removed in v.3.0.0.\n' + - 'To reduce the amount of options and the improve the reconnection handling please use the new `retry_strategy` option instead.\n' + - 'This replaces the max_attempts and retry_max_delay option.' - ); - } this.initialize_retry_vars(); this.pub_sub_mode = 0; this.subscription_set = {}; @@ -158,17 +120,7 @@ function RedisClient (options, stream) { this.create_stream(); // The listeners will not be attached right away, so let's print the deprecation message while the listener is attached this.on('newListener', function (event) { - if (event === 'idle') { - this.warn( - 'The idle event listener is deprecated and will likely be removed in v.3.0.0.\n' + - 'If you rely on this feature please open a new ticket in node_redis with your use case' - ); - } else if (event === 'drain') { - this.warn( - 'The drain event listener is deprecated and will be removed in v.3.0.0.\n' + - 'If you want to keep on listening to this event please listen to the stream drain event directly.' - ); - } else if ((event === 'message_buffer' || event === 'pmessage_buffer' || event === 'messageBuffer' || event === 'pmessageBuffer') && !this.buffers && !this.message_buffers) { + if ((event === 'message_buffer' || event === 'pmessage_buffer' || event === 'messageBuffer' || event === 'pmessageBuffer') && !this.buffers && !this.message_buffers) { this.reply_parser.optionReturnBuffers = true; this.message_buffers = true; this.handle_reply = handle_detect_buffers_reply; @@ -264,7 +216,6 @@ RedisClient.prototype.create_stream = function () { // The buffer_from_socket.toString() has a significant impact on big chunks and therefore this should only be used if necessary debug('Net read ' + self.address + ' id ' + self.connection_id); // + ': ' + buffer_from_socket.toString()); self.reply_parser.execute(buffer_from_socket); - self.emit_idle(); }); this.stream.on('error', function (err) { @@ -283,9 +234,7 @@ RedisClient.prototype.create_stream = function () { self.drain(); }); - if (this.options.socket_nodelay) { - this.stream.setNoDelay(); - } + this.stream.setNoDelay(); // Fire the command before redis is connected to be sure it's the first fired command if (this.auth_pass !== undefined) { @@ -387,7 +336,7 @@ RedisClient.prototype.on_error = function (err) { this.connected = false; this.ready = false; - // Only emit the error if the retry_stategy option is not set + // Only emit the error if the retry_strategy option is not set if (!this.options.retry_strategy) { this.emit('error', err); } @@ -402,7 +351,7 @@ RedisClient.prototype.on_connect = function () { this.connected = true; this.ready = false; this.emitted_end = false; - this.stream.setKeepAlive(this.options.socket_keepalive, this.options.socket_initialdelay); + this.stream.setKeepAlive(this.options.socket_keepalive, this.options.socket_initial_delay); this.stream.setTimeout(0); this.emit('connect'); @@ -607,18 +556,31 @@ RedisClient.prototype.connection_gone = function (why, error) { if (this.retry_delay instanceof Error) { error = this.retry_delay; } + var errorMessage = 'Redis connection in broken state: '; + if (this.retry_totaltime >= this.connect_timeout) { + errorMessage += 'connection timeout exceeded.'; + } else { + errorMessage += 'maximum connection attempts exceeded.'; + } + this.flush_and_error({ - message: 'Stream connection ended and command aborted.', - code: 'NR_CLOSED' + message: errorMessage, + code: 'CONNECTION_BROKEN', }, { error: error }); + var retryError = new Error(errorMessage); + retryError.code = 'CONNECTION_BROKEN'; + if (error) { + retryError.origin = error; + } this.end(false); + this.emit('error', retryError); return; } } - if (this.max_attempts !== 0 && this.attempts >= this.max_attempts || this.retry_totaltime >= this.connect_timeout) { + if (this.retry_totaltime >= this.connect_timeout) { var message = 'Redis connection in broken state: '; if (this.retry_totaltime >= this.connect_timeout) { message += 'connection timeout exceeded.'; @@ -637,8 +599,8 @@ RedisClient.prototype.connection_gone = function (why, error) { if (error) { err.origin = error; } - this.emit('error', err); this.end(false); + this.emit('error', err); return; } @@ -656,15 +618,12 @@ RedisClient.prototype.connection_gone = function (why, error) { }); } - if (this.retry_max_delay !== null && this.retry_delay > this.retry_max_delay) { - this.retry_delay = this.retry_max_delay; - } else if (this.retry_totaltime + this.retry_delay > this.connect_timeout) { + if (this.retry_totaltime + this.retry_delay > this.connect_timeout) { // Do not exceed the maximum this.retry_delay = this.connect_timeout - this.retry_totaltime; } debug('Retry connection in ' + this.retry_delay + ' ms'); - this.retry_timer = setTimeout(retry_connection, this.retry_delay, this, error); }; @@ -693,16 +652,9 @@ RedisClient.prototype.return_error = function (err) { }; RedisClient.prototype.drain = function () { - this.emit('drain'); this.should_buffer = false; }; -RedisClient.prototype.emit_idle = function () { - if (this.command_queue.length === 0 && this.pub_sub_mode === 0) { - this.emit('idle'); - } -}; - function normal_reply (self, reply) { var command_obj = self.command_queue.shift(); if (typeof command_obj.callback === 'function') { @@ -752,7 +704,7 @@ function subscribe_unsubscribe (self, reply, type) { self.command_queue.shift(); if (typeof command_obj.callback === 'function') { // TODO: The current return value is pretty useless. - // Evaluate to change this in v.3 to return all subscribed / unsubscribed channels in an array including the number of channels subscribed too + // Evaluate to change this in v.4 to return all subscribed / unsubscribed channels in an array including the number of channels subscribed too command_obj.callback(null, channel); } self.sub_commands_left = 0; @@ -768,7 +720,7 @@ function subscribe_unsubscribe (self, reply, type) { function return_pub_sub (self, reply) { var type = reply[0].toString(); if (type === 'message') { // channel, message - if (!self.options.return_buffers || self.message_buffers) { // backwards compatible. Refactor this in v.3 to always return a string on the normal emitter + if (!self.options.return_buffers || self.message_buffers) { // backwards compatible. Refactor this in v.4 to always return a string on the normal emitter self.emit('message', reply[1].toString(), reply[2].toString()); self.emit('message_buffer', reply[1], reply[2]); self.emit('messageBuffer', reply[1], reply[2]); @@ -776,7 +728,7 @@ function return_pub_sub (self, reply) { self.emit('message', reply[1], reply[2]); } } else if (type === 'pmessage') { // pattern, channel, message - if (!self.options.return_buffers || self.message_buffers) { // backwards compatible. Refactor this in v.3 to always return a string on the normal emitter + if (!self.options.return_buffers || self.message_buffers) { // backwards compatible. Refactor this in v.4 to always return a string on the normal emitter self.emit('pmessage', reply[1].toString(), reply[2].toString(), reply[3].toString()); self.emit('pmessage_buffer', reply[1], reply[2], reply[3]); self.emit('pmessageBuffer', reply[1], reply[2], reply[3]); @@ -884,32 +836,39 @@ RedisClient.prototype.internal_send_command = function (command_obj) { } else if (typeof args[i] === 'object') { // Checking for object instead of Buffer.isBuffer helps us finding data types that we can't handle properly if (args[i] instanceof Date) { // Accept dates as valid input args_copy[i] = args[i].toString(); - } else if (args[i] === null) { - this.warn( - 'Deprecated: The ' + command.toUpperCase() + ' command contains a "null" argument.\n' + - 'This is converted to a "null" string now and will return an error from v.3.0 on.\n' + - 'Please handle this in your code to make sure everything works as you intended it to.' - ); - args_copy[i] = 'null'; // Backwards compatible :/ } else if (Buffer.isBuffer(args[i])) { args_copy[i] = args[i]; command_obj.buffer_args = true; big_data = true; } else { - this.warn( - 'Deprecated: The ' + command.toUpperCase() + ' command contains a argument of type ' + args[i].constructor.name + '.\n' + - 'This is converted to "' + args[i].toString() + '" by using .toString() now and will return an error from v.3.0 on.\n' + - 'Please handle this in your code to make sure everything works as you intended it to.' + var invalidArgError = new Error( + 'node_redis: The ' + command.toUpperCase() + ' command contains a invalid argument type.\n' + + 'Only strings, dates and buffers are accepted. Please update your code to use valid argument types.' ); - args_copy[i] = args[i].toString(); // Backwards compatible :/ + invalidArgError.command = command_obj.command.toUpperCase(); + if (command_obj.args && command_obj.args.length) { + invalidArgError.args = command_obj.args; + } + if (command_obj.callback) { + command_obj.callback(invalidArgError); + return false; + } + throw invalidArgError; } } else if (typeof args[i] === 'undefined') { - this.warn( - 'Deprecated: The ' + command.toUpperCase() + ' command contains a "undefined" argument.\n' + - 'This is converted to a "undefined" string now and will return an error from v.3.0 on.\n' + - 'Please handle this in your code to make sure everything works as you intended it to.' + var undefinedArgError = new Error( + 'node_redis: The ' + command.toUpperCase() + ' command contains a invalid argument type of "undefined".\n' + + 'Only strings, dates and buffers are accepted. Please update your code to use valid argument types.' ); - args_copy[i] = 'undefined'; // Backwards compatible :/ + undefinedArgError.command = command_obj.command.toUpperCase(); + if (command_obj.args && command_obj.args.length) { + undefinedArgError.args = command_obj.args; + } + if (command_obj.callback) { + command_obj.callback(undefinedArgError); + return false; + } + throw undefinedArgError; } else { // Seems like numbers are converted fast using string concatenation args_copy[i] = '' + args[i]; diff --git a/package.json b/package.json index 015b93a8c74..e322fc6ef45 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "redis", "version": "2.8.0", - "description": "Redis client library", + "description": "A high performance Redis client.", "keywords": [ "database", "redis", @@ -14,6 +14,16 @@ "backpressure" ], "author": "Matt Ranney ", + "contributors": [ + { + "name": "Mike Diarmid (Salakar)", + "url": "https://github.com/salakar" + }, + { + "name": "Ruben Bridgewater (BridgeAR)", + "url": "https://github.com/BridgeAR" + } + ], "license": "MIT", "main": "./index.js", "scripts": { @@ -31,30 +41,34 @@ "redis-parser": "^3.0.0" }, "engines": { - "node": ">=4.0.0" + "node": ">=6" }, "devDependencies": { - "bluebird": "^3.0.2", + "bluebird": "^3.7.2", "coveralls": "^2.11.2", - "eslint": "^4.2.0", + "eslint": "^6.8.0", "intercept-stdout": "~0.1.2", - "metrics": "^0.1.9", - "mocha": "^3.1.2", - "nyc": "^10.0.0", - "tcp-port-used": "^0.1.2", - "uuid": "^2.0.1", - "win-spawn": "^2.0.0" + "metrics": "^0.1.21", + "mocha": "^4.1.0", + "nyc": "^14.1.1", + "tcp-port-used": "^1.0.1", + "uuid": "^3.4.0", + "cross-spawn": "^6.0.5" }, "repository": { "type": "git", - "url": "git://github.com/NodeRedis/node_redis.git" + "url": "git://github.com/NodeRedis/node-redis.git" }, "bugs": { - "url": "https://github.com/NodeRedis/node_redis/issues" + "url": "https://github.com/NodeRedis/node-redis/issues" }, - "homepage": "https://github.com/NodeRedis/node_redis", + "homepage": "https://github.com/NodeRedis/node-redis", "directories": { "example": "examples", "test": "test" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-redis" } } diff --git a/test/auth.spec.js b/test/auth.spec.js index 8411a4b618d..67e6d0924a1 100644 --- a/test/auth.spec.js +++ b/test/auth.spec.js @@ -192,7 +192,7 @@ describe('client authentication', function () { client = redis.createClient.apply(null, args); var async = true; - client.auth(undefined, function (err, res) { + client.auth('undefined', function (err, res) { assert.strictEqual(err.message, 'ERR invalid password'); assert.strictEqual(err.command, 'AUTH'); assert.strictEqual(res, undefined); diff --git a/test/commands/hset.spec.js b/test/commands/hset.spec.js index d0ce7f5481b..c62b17713b0 100644 --- a/test/commands/hset.spec.js +++ b/test/commands/hset.spec.js @@ -45,22 +45,16 @@ describe("The 'hset' method", function () { }); }); - it('warns if someone passed a array either as field or as value', function (done) { + it('errors if someone passed a array either as field or as value', function (done) { var hash = 'test hash'; var field = 'array'; - // This would be converted to "array contents" but if you use more than one entry, - // it'll result in e.g. "array contents,second content" and this is not supported and considered harmful var value = ['array contents']; - client.on('warning', function (msg) { - assert.strictEqual( - msg, - 'Deprecated: The HMSET command contains a argument of type Array.\n' + - 'This is converted to "array contents" by using .toString() now and will return an error from v.3.0 on.\n' + - 'Please handle this in your code to make sure everything works as you intended it to.' - ); + try { + client.HMSET(hash, field, value); + } catch (error) { + assert(/node_redis: The HMSET command contains a invalid argument type./.test(error.message)); done(); - }); - client.HMSET(hash, field, value); + } }); it('does not error when a buffer and date are set as values on the same hash', function (done) { diff --git a/test/commands/set.spec.js b/test/commands/set.spec.js index 01b62443818..360cffe5f59 100644 --- a/test/commands/set.spec.js +++ b/test/commands/set.spec.js @@ -140,10 +140,14 @@ describe("The 'set' method", function () { }); }); - // TODO: This test has to be refactored from v.3.0 on to expect an error instead - it("converts null to 'null'", function (done) { - client.set('foo', null); - client.get('foo', helper.isString('null', done)); + it('errors if null value is passed', function (done) { + try { + client.set('foo', null); + assert(false); + } catch (error) { + assert(/The SET command contains a invalid argument type./.test(error.message)); + } + client.get('foo', helper.isNull(done)); }); it('emit an error with only the key set', function (done) { diff --git a/test/connection.spec.js b/test/connection.spec.js index f6dc8310cb2..7ecee698530 100644 --- a/test/connection.spec.js +++ b/test/connection.spec.js @@ -152,40 +152,6 @@ describe('connection tests', function () { describe('using ' + parser + ' and ' + ip, function () { describe('on lost connection', function () { - it('emit an error after max retry attempts and do not try to reconnect afterwards', function (done) { - var maxAttempts = 3; - var options = { - parser: parser, - maxAttempts: maxAttempts - }; - client = redis.createClient(options); - assert.strictEqual(client.retryBackoff, 1.7); - assert.strictEqual(client.retryDelay, 200); - assert.strictEqual(Object.keys(options).length, 2); - var calls = 0; - - client.once('ready', function () { - helper.killConnection(client); - }); - - client.on('reconnecting', function (params) { - calls++; - }); - - client.on('error', function (err) { - if (/Redis connection in broken state: maximum connection attempts.*?exceeded./.test(err.message)) { - process.nextTick(function () { // End is called after the error got emitted - assert.strictEqual(calls, maxAttempts - 1); - assert.strictEqual(client.emitted_end, true); - assert.strictEqual(client.connected, false); - assert.strictEqual(client.ready, false); - assert.strictEqual(client.closing, true); - done(); - }); - } - }); - }); - it('emit an error after max retry timeout and do not try to reconnect afterwards', function (done) { // TODO: Investigate why this test fails with windows. Reconnect is only triggered once if (process.platform === 'win32') this.skip(); @@ -271,16 +237,11 @@ describe('connection tests', function () { }); it('retryStrategy used to reconnect with individual error', function (done) { - var text = ''; - var unhookIntercept = intercept(function (data) { - text += data; - return ''; - }); client = redis.createClient({ retryStrategy: function (options) { if (options.totalRetryTime > 150) { client.set('foo', 'bar', function (err, res) { - assert.strictEqual(err.message, 'Stream connection ended and command aborted.'); + assert.strictEqual(err.message, 'Redis connection in broken state: maximum connection attempts exceeded.'); assert.strictEqual(err.origin.message, 'Connection timeout'); done(); }); @@ -289,18 +250,8 @@ describe('connection tests', function () { } return Math.min(options.attempt * 25, 200); }, - maxAttempts: 5, - retryMaxDelay: 123, port: 9999 }); - process.nextTick(function () { - assert.strictEqual( - text, - 'node_redis: WARNING: You activated the retry_strategy and max_attempts at the same time. This is not possible and max_attempts will be ignored.\n' + - 'node_redis: WARNING: You activated the retry_strategy and retry_max_delay at the same time. This is not possible and retry_max_delay will be ignored.\n' - ); - unhookIntercept(); - }); }); it('retry_strategy used to reconnect', function (done) { @@ -308,8 +259,8 @@ describe('connection tests', function () { retry_strategy: function (options) { if (options.total_retry_time > 150) { client.set('foo', 'bar', function (err, res) { - assert.strictEqual(err.message, 'Stream connection ended and command aborted.'); - assert.strictEqual(err.code, 'NR_CLOSED'); + assert.strictEqual(err.message, 'Redis connection in broken state: maximum connection attempts exceeded.'); + assert.strictEqual(err.code, 'CONNECTION_BROKEN'); assert.strictEqual(err.origin.code, 'ECONNREFUSED'); done(); }); @@ -337,11 +288,13 @@ describe('connection tests', function () { client.stream.destroy(); }, 50); client.on('error', function (err) { - assert.strictEqual(err.code, 'NR_CLOSED'); - assert.strictEqual(err.message, 'Stream connection ended and command aborted.'); - unhookIntercept(); - redis.debugMode = false; - done(); + if (err instanceof redis.AbortError) { + assert.strictEqual(err.message, 'Redis connection in broken state: maximum connection attempts exceeded.'); + assert.strictEqual(err.code, 'CONNECTION_BROKEN'); + unhookIntercept(); + redis.debugMode = false; + done(); + } }); }); }); diff --git a/test/lib/redis-process.js b/test/lib/redis-process.js index c46fd8d70f0..ce2f881edd6 100644 --- a/test/lib/redis-process.js +++ b/test/lib/redis-process.js @@ -4,7 +4,7 @@ var config = require('./config'); var fs = require('fs'); var path = require('path'); -var spawn = require('win-spawn'); +var spawn = require('cross-spawn'); var tcpPortUsed = require('tcp-port-used'); var bluebird = require('bluebird'); diff --git a/test/multi.spec.js b/test/multi.spec.js index 33ffdbe16ce..233ead776c1 100644 --- a/test/multi.spec.js +++ b/test/multi.spec.js @@ -219,17 +219,22 @@ describe("The 'multi' method", function () { client = redis.createClient({ host: 'somewhere', port: 6379, - max_attempts: 1 + retry_strategy: function (options) { + if (options.attempt > 1) { + // End reconnecting with built in error + return undefined; + } + } }); client.on('error', function (err) { - if (/Redis connection in broken state/.test(err.message)) { + if (/Redis connection to somewhere:6379 failed/.test(err.origin.message)) { done(); } }); client.multi([['set', 'foo', 'bar'], ['get', 'foo']]).exec(function (err, res) { - assert(/Redis connection in broken state/.test(err.message)); + assert(/Redis connection in broken state: maximum connection attempts exceeded/.test(err.message)); assert.strictEqual(err.errors.length, 2); assert.strictEqual(err.errors[0].args.length, 2); }); @@ -239,12 +244,17 @@ describe("The 'multi' method", function () { client = redis.createClient({ host: 'somewhere', port: 6379, - max_attempts: 1 + retry_strategy: function (options) { + if (options.attempt > 1) { + // End reconnecting with built in error + return undefined; + } + } }); client.on('error', function (err) { // Results in multiple done calls if test fails - if (/Redis connection in broken state/.test(err.message)) { + if (/Redis connection to somewhere:6379 failed/.test(err.origin.message)) { done(); } }); diff --git a/test/node_redis.spec.js b/test/node_redis.spec.js index d529d40da3e..3c511ec9af9 100644 --- a/test/node_redis.spec.js +++ b/test/node_redis.spec.js @@ -707,25 +707,6 @@ describe('The node_redis client', function () { }); }); - describe('idle', function () { - it('emits idle as soon as there are no outstanding commands', function (done) { - var end = helper.callFuncAfter(done, 2); - client.on('warning', function (msg) { - assert.strictEqual( - msg, - 'The idle event listener is deprecated and will likely be removed in v.3.0.0.\n' + - 'If you rely on this feature please open a new ticket in node_redis with your use case' - ); - end(); - }); - client.on('idle', function onIdle () { - client.removeListener('idle', onIdle); - client.get('foo', helper.isString('bar', end)); - }); - client.set('foo', 'bar'); - }); - }); - describe('utf8', function () { it('handles utf-8 keys', function (done) { var utf8_sample = 'ಠ_ಠ'; @@ -793,111 +774,6 @@ describe('The node_redis client', function () { // }); }); - describe('socket_nodelay', function () { - describe('true', function () { - var args = config.configureClient(parser, ip, { - socket_nodelay: true - }); - - it("fires client.on('ready')", function (done) { - client = redis.createClient.apply(null, args); - client.on('ready', function () { - assert.strictEqual(true, client.options.socket_nodelay); - client.quit(done); - }); - }); - - it('client is functional', function (done) { - client = redis.createClient.apply(null, args); - client.on('ready', function () { - assert.strictEqual(true, client.options.socket_nodelay); - client.set(['set key 1', 'set val'], helper.isString('OK')); - client.set(['set key 2', 'set val'], helper.isString('OK')); - client.get(['set key 1'], helper.isString('set val')); - client.get(['set key 2'], helper.isString('set val')); - client.quit(done); - }); - }); - }); - - describe('false', function () { - var args = config.configureClient(parser, ip, { - socket_nodelay: false - }); - - it("fires client.on('ready')", function (done) { - client = redis.createClient.apply(null, args); - client.on('ready', function () { - assert.strictEqual(false, client.options.socket_nodelay); - client.quit(done); - }); - }); - - it('client is functional', function (done) { - client = redis.createClient.apply(null, args); - client.on('ready', function () { - assert.strictEqual(false, client.options.socket_nodelay); - client.set(['set key 1', 'set val'], helper.isString('OK')); - client.set(['set key 2', 'set val'], helper.isString('OK')); - client.get(['set key 1'], helper.isString('set val')); - client.get(['set key 2'], helper.isString('set val')); - client.quit(done); - }); - }); - }); - - describe('defaults to true', function () { - - it("fires client.on('ready')", function (done) { - client = redis.createClient.apply(null, args); - client.on('ready', function () { - assert.strictEqual(true, client.options.socket_nodelay); - client.quit(done); - }); - }); - - it('client is functional', function (done) { - client = redis.createClient.apply(null, args); - client.on('ready', function () { - assert.strictEqual(true, client.options.socket_nodelay); - client.set(['set key 1', 'set val'], helper.isString('OK')); - client.set(['set key 2', 'set val'], helper.isString('OK')); - client.get(['set key 1'], helper.isString('set val')); - client.get(['set key 2'], helper.isString('set val')); - client.quit(done); - }); - }); - }); - }); - - describe('retry_max_delay', function () { - it('sets upper bound on how long client waits before reconnecting', function (done) { - var time; - var timeout = process.platform !== 'win32' ? 20 : 100; - - client = redis.createClient.apply(null, config.configureClient(parser, ip, { - retry_max_delay: 1 // ms - })); - client.on('ready', function () { - if (this.times_connected === 1) { - this.stream.end(); - time = Date.now(); - } else { - done(); - } - }); - client.on('reconnecting', function () { - time = Date.now() - time; - assert(time < timeout, 'The reconnect should not have taken longer than ' + timeout + ' but it took ' + time); - }); - client.on('error', function (err) { - // This is rare but it might be triggered. - // So let's have a robust test - assert.strictEqual(err.code, 'ECONNRESET'); - }); - }); - }); - describe('protocol error', function () { it('should gracefully recover and only fail on the already send commands', function (done) { @@ -932,33 +808,9 @@ describe('The node_redis client', function () { describe('enable_offline_queue', function () { describe('true', function () { - it('should emit drain if offline queue is flushed and nothing to buffer', function (done) { - client = redis.createClient({ - parser: parser, - no_ready_check: true - }); - var end = helper.callFuncAfter(done, 3); - client.set('foo', 'bar'); - client.get('foo', end); - client.on('warning', function (msg) { - assert.strictEqual( - msg, - 'The drain event listener is deprecated and will be removed in v.3.0.0.\n' + - 'If you want to keep on listening to this event please listen to the stream drain event directly.' - ); - end(); - }); - client.on('drain', function () { - assert(client.offline_queue.length === 0); - end(); - }); - }); it('does not return an error and enqueues operation', function (done) { - client = redis.createClient(9999, null, { - max_attempts: 0, - parser: parser - }); + client = redis.createClient(9999, null); var finished = false; client.on('error', function (e) { // ignore, b/c expecting a "can't connect" error @@ -980,8 +832,12 @@ describe('The node_redis client', function () { it('enqueues operation and keep the queue while trying to reconnect', function (done) { client = redis.createClient(9999, null, { - max_attempts: 4, - parser: parser + retry_strategy: function (options) { + if (options.attempt > 4) { + return undefined; + } + return 100; + }, }); var i = 0; From 144b7edd8562b2c3cb5046fc8c0797201ccadaaf Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 02:08:46 +0000 Subject: [PATCH 02/13] start of work towards v3 release --- README.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e4f60d3ab32..77197ed7172 100644 --- a/README.md +++ b/README.md @@ -1,34 +1,38 @@

- +

Node Redis

- A high performance Node.js Redis client. +

A high performance Node.js Redis client.

+--- +

NPM downloads NPM version Build Status Coverage Status Windows Tests - Follow on Twitter + Follow on Twitter

--- -This is a complete and feature rich Redis client for node.js. __It supports all +This is a complete and feature rich Redis client for Node.js. __It supports all Redis commands__ and focuses on high performance. Install with: - npm install redis +```bash +npm install redis +``` ## Usage Example ```js -var redis = require("redis"), - client = redis.createClient(); +const redis = require("redis"); +const client = redis.createClient(); // if you'd like to select database 3, instead of 0 (default), call // client.select(3, function() { /* ... */ }); @@ -69,6 +73,7 @@ landscape. ### Promises #### Native Promises + If you are using node v8 or higher, you can promisify node_redis with [util.promisify](https://nodejs.org/api/util.html#util_util_promisify_original) as in: ```js const {promisify} = require('util'); @@ -92,6 +97,7 @@ async function myFunc() { ``` #### Bluebird Promises + You can also use node_redis with promises by promisifying node_redis with [bluebird](https://github.com/petkaantonov/bluebird) as in: From bb05064b143584f8e01bd4da3b52b93ee90ea64e Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 02:15:44 +0000 Subject: [PATCH 03/13] format docs --- .prettierrc | 11 + CODE_OF_CONDUCT.md | 20 +- README.md | 532 ++++++++++++++++++++++++--------------------- package.json | 1 + 4 files changed, 305 insertions(+), 259 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000000..951c38fd13a --- /dev/null +++ b/.prettierrc @@ -0,0 +1,11 @@ +{ + "arrowParens": "avoid", + "trailingComma": "all", + "useTabs": false, + "semi": true, + "singleQuote": true, + "bracketSpacing": true, + "jsxBracketSameLine": false, + "tabWidth": 2, + "printWidth": 100 +} diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index 00aad77d088..50b0a741084 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -8,19 +8,19 @@ In the interest of fostering an open and welcoming environment, we as contributo Examples of behavior that contributes to creating a positive environment include: -* Using welcoming and inclusive language -* Being respectful of differing viewpoints and experiences -* Gracefully accepting constructive criticism -* Focusing on what is best for the community -* Showing empathy towards other community members +- Using welcoming and inclusive language +- Being respectful of differing viewpoints and experiences +- Gracefully accepting constructive criticism +- Focusing on what is best for the community +- Showing empathy towards other community members Examples of unacceptable behavior by participants include: -* The use of sexualized language or imagery and unwelcome sexual attention or advances -* Trolling, insulting/derogatory comments, and personal or political attacks -* Public or private harassment -* Publishing others' private information, such as a physical or electronic address, without explicit permission -* Other conduct which could reasonably be considered inappropriate in a professional setting +- The use of sexualized language or imagery and unwelcome sexual attention or advances +- Trolling, insulting/derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or electronic address, without explicit permission +- Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities diff --git a/README.md b/README.md index 77197ed7172..bd124739b24 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

- +

Node Redis

A high performance Node.js Redis client.

@@ -12,15 +12,15 @@ NPM downloads NPM version Build Status - Coverage Status Windows Tests + Coverage Status Follow on Twitter

--- -This is a complete and feature rich Redis client for Node.js. __It supports all -Redis commands__ and focuses on high performance. +This is a complete and feature rich Redis client for Node.js. **It supports all +Redis commands** and focuses on high performance. Install with: @@ -31,25 +31,25 @@ npm install redis ## Usage Example ```js -const redis = require("redis"); +const redis = require('redis'); const 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.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(); +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(); }); ``` @@ -75,24 +75,28 @@ landscape. #### Native Promises If you are using node v8 or higher, you can promisify node_redis with [util.promisify](https://nodejs.org/api/util.html#util_util_promisify_original) as in: + ```js -const {promisify} = require('util'); +const { promisify } = require('util'); const getAsync = promisify(client.get).bind(client); ``` -now *getAsync* is a promisified version of *client.get*: + +now _getAsync_ is a promisified version of _client.get_: + ```js // We expect a value 'foo': 'bar' to be present // So instead of writing client.get('foo', cb); you have to write: return getAsync('foo').then(function(res) { - console.log(res); // => 'bar' + console.log(res); // => 'bar' }); ``` or using [async await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function): + ```js async function myFunc() { - const res = await getAsync('foo'); - console.log(res); + const res = await getAsync('foo'); + console.log(res); } ``` @@ -106,20 +110,24 @@ const redis = require('redis'); bluebird.promisifyAll(redis); ``` -It'll add a *Async* to all node_redis functions (e.g. return client.getAsync().then()) +It'll add a _Async_ to all node_redis functions (e.g. return client.getAsync().then()) ```js // We expect a value 'foo': 'bar' to be present // So instead of writing client.get('foo', cb); you have to write: return client.getAsync('foo').then(function(res) { - console.log(res); // => 'bar' + console.log(res); // => 'bar' }); // Using multi with promises looks like: -return client.multi().get('foo').execAsync().then(function(res) { +return client + .multi() + .get('foo') + .execAsync() + .then(function(res) { console.log(res); // => 'bar' -}); + }); ``` ### Sending Commands @@ -130,11 +138,18 @@ a variable number of individual arguments followed by an optional callback. Examples: ```js -client.hmset(["key", "test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {}); +client.hmset(['key', 'test keys 1', 'test val 1', 'test keys 2', 'test val 2'], function( + err, + res, +) {}); // Works the same as -client.hmset("key", ["test keys 1", "test val 1", "test keys 2", "test val 2"], function (err, res) {}); +client.hmset('key', ['test keys 1', 'test val 1', 'test keys 2', 'test val 2'], function(err, res) { + // ... +}); // Or -client.hmset("key", "test keys 1", "test val 1", "test keys 2", "test val 2", function (err, res) {}); +client.hmset('key', 'test keys 1', 'test val 1', 'test keys 2', 'test val 2', function(err, res) { + // ... +}); ``` Care should be taken with user input if arrays are possible (via body-parser, query string or other method), as single arguments could be unintentionally interpreted as multiple args. @@ -142,17 +157,17 @@ Care should be taken with user input if arrays are possible (via body-parser, qu Note that in either form the `callback` is optional: ```js -client.set("some key", "some val"); -client.set(["some other key", "some val"]); +client.set('some key', 'some val'); +client.set(['some other key', 'some val']); ``` If the key is missing, reply will be null. Only if the [Redis Command Reference](http://redis.io/commands) states something else it will not be null. ```js -client.get("missingkey", function(err, reply) { - // reply is null when the key is missing - console.log(reply); +client.get('missingkey', function(err, reply) { + // reply is null when the key is missing + console.log(reply); }); ``` @@ -218,85 +233,87 @@ So please attach the error listener to node_redis. deprecated option / function / similar is used. ## redis.createClient() + If you have `redis-server` running on the same machine as node, then the defaults for port and host are probably fine and you don't need to supply any arguments. `createClient()` returns a `RedisClient` object. Otherwise, `createClient()` accepts these arguments: -* `redis.createClient([options])` -* `redis.createClient(unix_socket[, options])` -* `redis.createClient(redis_url[, options])` -* `redis.createClient(port[, host][, options])` +- `redis.createClient([options])` +- `redis.createClient(unix_socket[, options])` +- `redis.createClient(redis_url[, options])` +- `redis.createClient(port[, host][, options])` -__Tip:__ If the Redis server runs on the same machine as the client consider +**Tip:** If the Redis server runs on the same machine as the client consider using unix sockets if possible to increase throughput. -__Note:__ Using `'rediss://...` for the protocol in a `redis_url` will enable a TLS socket connection. However, additional TLS options will need to be passed in `options`, if required. +**Note:** Using `'rediss://...` for the protocol in a `redis_url` will enable a TLS socket connection. However, additional TLS options will need to be passed in `options`, if required. #### `options` object properties -| Property | Default | Description | -|-----------|-----------|-------------| -| host | 127.0.0.1 | IP address of the Redis server | -| port | 6379 | Port of the Redis server | -| path | null | The UNIX socket string of the Redis server | -| url | null | The URL of the Redis server. Format: `[redis[s]:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]` (More info avaliable at [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis)). | -| string_numbers | null | Set to `true`, `node_redis` will return Redis number values as Strings instead of javascript Numbers. Useful if you need to handle big numbers (above `Number.MAX_SAFE_INTEGER === 2^53`). Hiredis is incapable of this behavior, so setting this option to `true` will result in the built-in javascript parser being used no matter the value of the `parser` option. | -| return_buffers | false | If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings. | -| detect_buffers | false | If set to `true`, then replies will be sent to callbacks as Buffers. This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to every command on a client. __Note__: This doesn't work properly with the pubsub mode. A subscriber has to either always return Strings or Buffers. | -| socket_keepalive | true | If set to `true`, the keep-alive functionality is enabled on the underlying socket. | -| socket_initial_delay | 0 | Initial Delay in milliseconds, and this will also behave the interval keep alive message sending to Redis. | -| no_ready_check | false | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will 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 | 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 executed immediately with an error, or an error will be emitted if no callback is specified. | -| retry_unfulfilled_commands | false | If set to `true`, all commands that were unfulfilled while the connection is lost will be retried after the connection has been reestablished. Use this with caution if you use state altering commands (e.g. `incr`). This is especially useful if you use blocking commands. | -| password | null | If set, client will run Redis auth command on connect. Alias `auth_pass` __Note__ `node_redis` < 2.5 must use `auth_pass` | -| db | null | If set, client will run Redis `select` command on connect. | -| family | IPv4 | You can force using IPv6 if you set the family to 'IPv6'. See Node.js [net](https://nodejs.org/api/net.html) or [dns](https://nodejs.org/api/dns.html) modules on how to use the family type. | -| disable_resubscribing | false | If set to `true`, a client won't resubscribe after disconnecting. | -| rename_commands | null | Passing an object with renamed commands to use instead of the original functions. For example, if you renamed the command KEYS to "DO-NOT-USE" then the rename_commands object would be: `{ KEYS : "DO-NOT-USE" }` . See the [Redis security topics](http://redis.io/topics/security) for more info. | -| tls | null | An object containing options to pass to [tls.connect](http://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback) to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel). | -| prefix | null | A string used to prefix all used keys (e.g. `namespace:test`). Please be aware that the `keys` command will not be prefixed. The `keys` command has a "pattern" as argument and no key and it would be impossible to determine the existing keys in Redis if this would be prefixed. | -| retry_strategy | function | A function that receives an options object as parameter including the retry `attempt`, the `total_retry_time` indicating how much time passed since the last time connected, the `error` why the connection was lost and the number of `times_connected` in total. If you return a number from this function, the retry will happen exactly after that time in milliseconds. If you return a non-number, no further retry will happen and all offline commands are flushed with errors. Return an error to return that specific error to all offline commands. Example below. | + +| Property | Default | Description | +| -------------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| host | 127.0.0.1 | IP address of the Redis server | +| port | 6379 | Port of the Redis server | +| path | null | The UNIX socket string of the Redis server | +| url | null | The URL of the Redis server. Format: `[redis[s]:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]` (More info avaliable at [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis)). | +| string_numbers | null | Set to `true`, `node_redis` will return Redis number values as Strings instead of javascript Numbers. Useful if you need to handle big numbers (above `Number.MAX_SAFE_INTEGER === 2^53`). Hiredis is incapable of this behavior, so setting this option to `true` will result in the built-in javascript parser being used no matter the value of the `parser` option. | +| return_buffers | false | If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings. | +| detect_buffers | false | If set to `true`, then replies will be sent to callbacks as Buffers. This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to every command on a client. **Note**: This doesn't work properly with the pubsub mode. A subscriber has to either always return Strings or Buffers. | +| socket_keepalive | true | If set to `true`, the keep-alive functionality is enabled on the underlying socket. | +| socket_initial_delay | 0 | Initial Delay in milliseconds, and this will also behave the interval keep alive message sending to Redis. | +| no_ready_check | false | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will 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 | 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 executed immediately with an error, or an error will be emitted if no callback is specified. | +| retry_unfulfilled_commands | false | If set to `true`, all commands that were unfulfilled while the connection is lost will be retried after the connection has been reestablished. Use this with caution if you use state altering commands (e.g. `incr`). This is especially useful if you use blocking commands. | +| password | null | If set, client will run Redis auth command on connect. Alias `auth_pass` **Note** `node_redis` < 2.5 must use `auth_pass` | +| db | null | If set, client will run Redis `select` command on connect. | +| family | IPv4 | You can force using IPv6 if you set the family to 'IPv6'. See Node.js [net](https://nodejs.org/api/net.html) or [dns](https://nodejs.org/api/dns.html) modules on how to use the family type. | +| disable_resubscribing | false | If set to `true`, a client won't resubscribe after disconnecting. | +| rename_commands | null | Passing an object with renamed commands to use instead of the original functions. For example, if you renamed the command KEYS to "DO-NOT-USE" then the rename_commands object would be: `{ KEYS : "DO-NOT-USE" }` . See the [Redis security topics](http://redis.io/topics/security) for more info. | +| tls | null | An object containing options to pass to [tls.connect](http://nodejs.org/api/tls.html#tls_tls_connect_port_host_options_callback) to set up a TLS connection to Redis (if, for example, it is set up to be accessible via a tunnel). | +| prefix | null | A string used to prefix all used keys (e.g. `namespace:test`). Please be aware that the `keys` command will not be prefixed. The `keys` command has a "pattern" as argument and no key and it would be impossible to determine the existing keys in Redis if this would be prefixed. | +| retry_strategy | function | A function that receives an options object as parameter including the retry `attempt`, the `total_retry_time` indicating how much time passed since the last time connected, the `error` why the connection was lost and the number of `times_connected` in total. If you return a number from this function, the retry will happen exactly after that time in milliseconds. If you return a non-number, no further retry will happen and all offline commands are flushed with errors. Return an error to return that specific error to all offline commands. Example below. | ```js -var redis = require("redis"); -var client = redis.createClient({detect_buffers: true}); +var redis = require('redis'); +var client = redis.createClient({ detect_buffers: true }); -client.set("foo_rand000000000000", "OK"); +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` +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.get(new Buffer('foo_rand000000000000'), function(err, reply) { + console.log(reply.toString()); // Will print `` }); client.quit(); ``` -retry_strategy example +#### `retry_strategy` example ```js var client = redis.createClient({ - retry_strategy: function (options) { - if (options.error && options.error.code === 'ECONNREFUSED') { - // End reconnecting on a specific error and flush all commands with - // a individual error - return new Error('The server refused the connection'); - } - if (options.total_retry_time > 1000 * 60 * 60) { - // End reconnecting after a specific timeout and flush all commands - // with a individual error - return new Error('Retry time exhausted'); - } - if (options.attempt > 10) { - // End reconnecting with built in error - return undefined; - } - // reconnect after - return Math.min(options.attempt * 100, 3000); + retry_strategy: function(options) { + if (options.error && options.error.code === 'ECONNREFUSED') { + // End reconnecting on a specific error and flush all commands with + // a individual error + return new Error('The server refused the connection'); + } + if (options.total_retry_time > 1000 * 60 * 60) { + // End reconnecting after a specific timeout and flush all commands + // with a individual error + return new Error('Retry time exhausted'); + } + if (options.attempt > 10) { + // End reconnecting with built in error + return undefined; } + // reconnect after + return Math.min(options.attempt * 100, 3000); + }, }); ``` @@ -346,17 +363,17 @@ 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(); +var redis = require('redis'), + client = redis.createClient(); -client.set("foo_rand000000000000", "some fantastic value", function (err, reply) { - // This will either result in an error (flush parameter is set to true) - // or will silently fail and this callback will not be called at all (flush set to false) - console.log(err); +client.set('foo_rand000000000000', 'some fantastic value', function(err, reply) { + // This will either result in an error (flush parameter is set to true) + // or will silently fail and this callback will not be called at all (flush set to false) + console.log(err); }); client.end(true); // No further commands will be processed -client.get("foo_rand000000000000", function (err, reply) { - console.log(err); // => 'The connection has already been closed.' +client.get('foo_rand000000000000', function(err, reply) { + console.log(err); // => 'The connection has already been closed.' }); ``` @@ -366,47 +383,48 @@ client.get("foo_rand000000000000", function (err, reply) { Currently the following error subclasses exist: -* `RedisError`: _All errors_ returned by the client -* `ReplyError` subclass of `RedisError`: All errors returned by __Redis__ itself -* `AbortError` subclass of `RedisError`: All commands that could not finish due +- `RedisError`: _All errors_ returned by the client +- `ReplyError` subclass of `RedisError`: All errors returned by **Redis** itself +- `AbortError` subclass of `RedisError`: All commands that could not finish due to what ever reason -* `ParserError` subclass of `RedisError`: Returned in case of a parser error +- `ParserError` subclass of `RedisError`: Returned in case of a parser error (this should not happen) -* `AggregateError` subclass of `AbortError`: Emitted in case multiple unresolved +- `AggregateError` subclass of `AbortError`: Emitted in case multiple unresolved commands without callback got rejected in debug_mode instead of lots of `AbortError`s. All error classes are exported by the module. Example: + ```js var redis = require('./'); var assert = require('assert'); var client = redis.createClient(); -client.on('error', function (err) { - assert(err instanceof Error); - assert(err instanceof redis.AbortError); - assert(err instanceof redis.AggregateError); - // The set and get get aggregated in here - assert.strictEqual(err.errors.length, 2); - assert.strictEqual(err.code, 'NR_CLOSED'); +client.on('error', function(err) { + assert(err instanceof Error); + assert(err instanceof redis.AbortError); + assert(err instanceof redis.AggregateError); + // The set and get get aggregated in here + assert.strictEqual(err.errors.length, 2); + assert.strictEqual(err.code, 'NR_CLOSED'); }); -client.set('foo', 123, 'bar', function (err, res) { // Too many arguments - assert(err instanceof redis.ReplyError); // => true - assert.strictEqual(err.command, 'SET'); - assert.deepStrictEqual(err.args, ['foo', 123, 'bar']); - - redis.debug_mode = true; - client.set('foo', 'bar'); - client.get('foo'); - process.nextTick(function () { - // Force closing the connection while the command did not yet return - client.end(true); - redis.debug_mode = false; - }); +client.set('foo', 123, 'bar', function(err, res) { + // Too many arguments + assert(err instanceof redis.ReplyError); // => true + assert.strictEqual(err.command, 'SET'); + assert.deepStrictEqual(err.args, ['foo', 123, 'bar']); + + redis.debug_mode = true; + client.set('foo', 'bar'); + client.get('foo'); + process.nextTick(function() { + // Force closing the connection while the command did not yet return + client.end(true); + redis.debug_mode = false; + }); }); - ``` Every `ReplyError` contains the `command` name in all-caps and the arguments (`args`). @@ -414,7 +432,7 @@ Every `ReplyError` contains the `command` name in all-caps and the arguments (`a If node_redis emits a library error because of another error, the triggering error is added to the returned error as `origin` attribute. -___Error codes___ +**_Error codes_** node_redis returns a `NR_CLOSED` error code if the clients connection dropped. If a command unresolved command got rejected a `UNCERTAIN_STATE` code is @@ -428,10 +446,10 @@ 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()`. +`*SUBSCRIBE` or the blocking `BL*` commands will _NOT_ work with `.unref()`. ```js -var redis = require("redis"); +var redis = require('redis'); var client = redis.createClient(); /* @@ -440,9 +458,9 @@ var client = redis.createClient(); client-server connection is alive. */ client.unref(); -client.get("foo", function (err, value) { - if (err) throw(err); - console.log(value); +client.get('foo', function(err, value) { + if (err) throw err; + console.log(value); }); ``` @@ -461,9 +479,9 @@ syntax. Example: ```js -client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234"); -client.hgetall("hosts", function (err, obj) { - console.dir(obj); +client.hmset('hosts', 'mjr', '1', 'another', '23', 'home', '1234'); +client.hgetall('hosts', function(err, obj) { + console.dir(obj); }); ``` @@ -473,8 +491,8 @@ Multiple values in a hash can be set by supplying an object: ```js client.HMSET(key2, { - "0123456789": "abcdefghij", // NOTE: key and value will be coerced to strings - "some manner of key": "a type of value" + '0123456789': 'abcdefghij', // NOTE: key and value will be coerced to strings + 'some manner of key': 'a type of value', }); ``` @@ -486,7 +504,7 @@ Redis hash. Multiple values may also be set by supplying a list: ```js -client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value"); +client.HMSET(key1, '0123456789', 'abcdefghij', 'some manner of key', 'a type of value'); ``` ## Publish / Subscribe @@ -496,27 +514,28 @@ client connections, subscribes to a channel on one of them, and publishes to tha channel on the other: ```js -var redis = require("redis"); -var sub = redis.createClient(), pub = redis.createClient(); +var redis = require('redis'); +var sub = redis.createClient(), + pub = redis.createClient(); var msg_count = 0; -sub.on("subscribe", function (channel, count) { - pub.publish("a nice channel", "I am sending a message."); - pub.publish("a nice channel", "I am sending a second message."); - pub.publish("a nice channel", "I am sending my last message."); +sub.on('subscribe', function(channel, count) { + pub.publish('a nice channel', 'I am sending a message.'); + pub.publish('a nice channel', 'I am sending a second message.'); + pub.publish('a nice channel', 'I am sending my last message.'); }); -sub.on("message", function (channel, message) { - console.log("sub channel " + channel + ": " + message); - msg_count += 1; - if (msg_count === 3) { - sub.unsubscribe(); - sub.quit(); - pub.quit(); - } +sub.on('message', function(channel, message) { + console.log('sub channel ' + channel + ': ' + message); + msg_count += 1; + if (msg_count === 3) { + sub.unsubscribe(); + sub.quit(); + pub.quit(); + } }); -sub.subscribe("a nice channel"); +sub.subscribe('a nice channel'); ``` When a client issues a `SUBSCRIBE` or `PSUBSCRIBE`, that connection is put into @@ -591,33 +610,35 @@ further information look at [transactions](http://redis.io/topics/transactions)). ```js -var redis = require("./index"), - client = redis.createClient(), set_size = 20; +var redis = require('./index'), + client = redis.createClient(), + set_size = 20; -client.sadd("bigset", "a member"); -client.sadd("bigset", "another member"); +client.sadd('bigset', 'a member'); +client.sadd('bigset', 'another member'); while (set_size > 0) { - client.sadd("bigset", "member " + set_size); - set_size -= 1; + 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()); - }); +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]) @@ -638,20 +659,21 @@ can queue individual commands while still sending regular client command as in this example: ```js -var redis = require("redis"), - client = redis.createClient(), multi; +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); +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); +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 +multi.exec(function(err, replies) { + console.log(replies); // 101, 2 }); ``` @@ -659,16 +681,18 @@ 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(); - -client.multi([ - ["mget", "multifoo", "multibar", redis.print], - ["incr", "multifoo"], - ["incr", "multibar"] -]).exec(function (err, replies) { +var redis = require('redis'), + client = redis.createClient(); + +client + .multi([ + ['mget', 'multifoo', 'multibar', redis.print], + ['incr', 'multifoo'], + ['incr', 'multibar'], + ]) + .exec(function(err, replies) { console.log(replies); -}); + }); ``` ### Multi.exec_atomic([callback]) @@ -695,11 +719,11 @@ commands are kept in memory until they are fired. ## Optimistic Locks -Using `multi` you can make sure your modifications run as a transaction, but you -can't be sure you got there first. What if another client modified a key while +Using `multi` you can make sure your modifications run as a transaction, but you +can't be sure you got there first. What if another client modified a key while you were working with it's data? -To solve this, Redis supports the [WATCH](https://redis.io/topics/transactions) +To solve this, Redis supports the [WATCH](https://redis.io/topics/transactions) command, which is meant to be used with MULTI: ```js @@ -711,25 +735,25 @@ client.watch("foo", function( err ){ client.get("foo", function(err, result) { if(err) throw err; - + // Process result // Heavy and time consuming operation here client.multi() .set("foo", "some heavy computation") .exec(function(err, results) { - + /** - * If err is null, it means Redis successfully attempted + * If err is null, it means Redis successfully attempted * the operation. - */ + */ if(err) throw err; - + /** * If results === null, it means that a concurrent client - * changed the key while we were processing it and thus + * changed the key while we were processing it and thus * the execution of the MULTI command was not performed. - * + * * NOTICE: Failing an execution of MULTI is not considered * an error. So you will have err === null and results === null */ @@ -741,11 +765,11 @@ client.watch("foo", function( err ){ The above snippet shows the correct usage of `watch` with `multi`. Every time a watched key is changed before the execution of a `multi` command, the execution -will return `null`. On a normal situation, the execution will return an array of -values with the results of the operations. +will return `null`. On a normal situation, the execution will return an array of +values with the results of the operations. As stated in the snippet, failing the execution of a `multi` command being watched -is not considered an error. The execution may return an error if, for example, the +is not considered an error. The execution may return an error if, for example, the client cannot connect to Redis. An example where we can see the execution of a `multi` command fail is as follows: @@ -759,7 +783,7 @@ clients.watcher.watch('foo',function(err) { if (err) { throw err; } //if you comment out the next line, the transaction will work clients.alterer.set('foo',Math.random(), (err) => {if (err) { throw err; }}); - + //using a setTimeout here to ensure that the MULTI/EXEC will come after the SET. //Normally, you would use a callback to ensure order, but I want the above SET command //to be easily comment-out-able. @@ -769,7 +793,7 @@ clients.watcher.watch('foo',function(err) { .set('foo','abc') .set('bar','1234') .exec((err,results) => { - if (err) { throw err; } + if (err) { throw err; } if (results === null) { console.log('transaction aborted because results were null'); } else { @@ -784,7 +808,7 @@ clients.watcher.watch('foo',function(err) { ### WATCH limitations -Redis WATCH works only on *whole* key values. For example, with WATCH you can +Redis WATCH works only on _whole_ key values. For example, with WATCH you can watch a hash for modifications, but you cannot watch a specific field of a hash. The following example would watch the keys `foo` and `hello`, not the field `hello` @@ -797,22 +821,22 @@ var redis = require("redis"), client.hget( "foo", "hello", function(err, result){ //Do some processing with the value from this field and watch it after - + client.watch("foo", "hello", function( err ){ if(err) throw err; /** * WRONG: This is now watching the keys 'foo' and 'hello'. It is not * watching the field 'hello' of hash 'foo'. Because the key 'foo' - * refers to a hash, this command is now watching the entire hash - * for modifications. - */ + * refers to a hash, this command is now watching the entire hash + * for modifications. + */ }); } ) ``` -This limitation also applies to sets ( cannot watch individual set members ) +This limitation also applies to sets ( cannot watch individual set members ) and any other collections. ## Monitor mode @@ -829,14 +853,14 @@ arguments and the raw monitoring string. Example: ```js -var client = require("redis").createClient(); -client.monitor(function (err, res) { - console.log("Entering monitoring mode."); +var client = require('redis').createClient(); +client.monitor(function(err, res) { + console.log('Entering monitoring mode.'); }); client.set('foo', 'bar'); -client.on("monitor", function (time, args, raw_reply) { - console.log(time + ": " + args); // 1458910076.446514:['set', 'foo', 'bar'] +client.on('monitor', function(time, args, raw_reply) { + console.log(time + ': ' + args); // 1458910076.446514:['set', 'foo', 'bar'] }); ``` @@ -862,12 +886,12 @@ easy comparison. A handy callback function for displaying return values when testing. Example: ```js -var redis = require("redis"), - client = redis.createClient(); +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); +client.on('connect', function() { + client.set('foo_rand000000000000', 'some fantastic value', redis.print); + client.get('foo_rand000000000000', redis.print); }); ``` @@ -885,7 +909,10 @@ the second word as first parameter: ```js client.script('load', 'return 1'); -client.multi().script('load', 'return 1').exec(); +client + .multi() + .script('load', 'return 1') + .exec(); client.multi([['script', 'load', 'return 1']]).exec(); ``` @@ -898,33 +925,35 @@ returns it in the callback. If an error occurs in the meanwhile, that is going to return an error instead in the callback. One example of when to use duplicate() would be to accommodate the connection- -blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands +blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands are used on the same redisClient instance as non-blocking commands, the non-blocking ones may be queued up until after the blocking ones finish. ```js -var Redis=require('redis'); +var Redis = require('redis'); var client = Redis.createClient(); var clientBlocking = client.duplicate(); var get = function() { - console.log("get called"); - client.get("any_key",function() { console.log("get returned"); }); - setTimeout( get, 1000 ); + console.log('get called'); + client.get('any_key', function() { + console.log('get returned'); + }); + setTimeout(get, 1000); }; var brpop = function() { - console.log("brpop called"); - clientBlocking.brpop("nonexistent", 5, function() { - console.log("brpop return"); - setTimeout( brpop, 1000 ); - }); + console.log('brpop called'); + clientBlocking.brpop('nonexistent', 5, function() { + console.log('brpop return'); + setTimeout(brpop, 1000); + }); }; get(); brpop(); ``` Another reason to use duplicate() is when multiple DBs on the same server are -accessed via the redis SELECT command. Each DB could use its own connection. +accessed via the redis SELECT command. Each DB could use its own connection. ## client.send_command(command_name[, [args][, callback]]) @@ -960,32 +989,34 @@ commands. ### 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. +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) { +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('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 - }); + 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 + }); }); ``` @@ -1031,7 +1062,7 @@ clients: 1, NodeJS: 6.2.0, Redis: 3.2.0, parser: javascript, connected by: tcp GET 4MiB str, batch 20/1 avg/max: 101.61/118.11 2541ms total, 197 ops/sec GET 4MiB buf, 1/1 avg/max: 2.32/ 14.93 2502ms total, 430 ops/sec GET 4MiB buf, batch 20/1 avg/max: 65.01/ 84.72 2536ms total, 308 ops/sec - ``` +``` ## Debugging @@ -1045,14 +1076,17 @@ application in development mode instead (`NODE_ENV=development`). Good stack traces are only activated in development and debug mode as this results in a significant performance penalty. -___Comparison___: +**_Comparison_**: Useless stack trace: + ``` ReplyError: ERR wrong number of arguments for 'set' command at parseError (/home/ruben/repos/redis/node_modules/redis-parser/lib/parser.js:158:12) at parseType (/home/ruben/repos/redis/node_modules/redis-parser/lib/parser.js:219:14) ``` + Good stack trace: + ``` ReplyError: ERR wrong number of arguments for 'set' command at new Command (/home/ruben/repos/redis/lib/command.js:9:902) diff --git a/package.json b/package.json index e322fc6ef45..13f610f7d43 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ }, "dependencies": { "denque": "^1.4.1", + "prettier": "^1.19.1", "redis-commands": "^1.5.0", "redis-errors": "^1.2.0", "redis-parser": "^3.0.0" From 7ce08584563184640dafdfd4ab23a32b339a66d2 Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 02:28:12 +0000 Subject: [PATCH 04/13] docs --- .prettierrc | 2 +- CHANGELOG.md | 663 ++++++++++++++++++++++++++------------------------- README.md | 277 ++++++++------------- 3 files changed, 437 insertions(+), 505 deletions(-) diff --git a/.prettierrc b/.prettierrc index 951c38fd13a..1ca516a86c0 100644 --- a/.prettierrc +++ b/.prettierrc @@ -3,7 +3,7 @@ "trailingComma": "all", "useTabs": false, "semi": true, - "singleQuote": true, + "singleQuote": false, "bracketSpacing": true, "jsxBracketSameLine": false, "tabWidth": 2, diff --git a/CHANGELOG.md b/CHANGELOG.md index daea8eeefc0..49be75d7d1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,35 +1,35 @@ # Changelog -## v.3.0.0 - 09 Feb, 2020 +## v3.0.0 - 09 Feb, 2020 -This version is mainly a release to distribute all the unreleased changes on master since 2017 and additionally removes +This version is mainly a release to distribute all the unreleased changes on master since 2017 and additionally removes a lot of old deprecated features and old internals in preparation for an upcoming modernization refactor (v4). ### Breaking Changes - - Dropped support for Node.js < 6 - - Dropped support for `hiredis` (no longer required) - - Removed previously deprecated `drain` event - - Removed previously deprecated `idle` event - - Removed previously deprecated `parser` option - - Removed previously deprecated `max_delay` option - - Removed previously deprecated `max_attempts` option - - Removed previously deprecated `socket_no_delay` option +- Dropped support for Node.js < 6 +- Dropped support for `hiredis` (no longer required) +- Removed previously deprecated `drain` event +- Removed previously deprecated `idle` event +- Removed previously deprecated `parser` option +- Removed previously deprecated `max_delay` option +- Removed previously deprecated `max_attempts` option +- Removed previously deprecated `socket_no_delay` option ### Bug Fixes - - Removed development files from published package (#1370) - - Duplicate function now allows db param to be passed (#1311) +- Removed development files from published package (#1370) +- Duplicate function now allows db param to be passed (#1311) ### Features - - Upgraded to latest `redis-commands` package - - Upgraded to latest `redis-parser` package, v3.0.0, which brings performance improvements - - Replaced `double-ended-queue` with `denque`, which brings performance improvements - - Add timestamps to debug traces - - Add `socket_initial_delay` option for `socket.setKeepAlive` (#1396) - -## v.2.8.0 - 31 Jul, 2017 +- Upgraded to latest `redis-commands` package +- Upgraded to latest `redis-parser` package, v3.0.0, which brings performance improvements +- Replaced `double-ended-queue` with `denque`, which brings performance improvements +- Add timestamps to debug traces +- Add `socket_initial_delay` option for `socket.setKeepAlive` (#1396) + +## v2.8.0 - 31 Jul, 2017 Features @@ -42,13 +42,13 @@ Bugfixes - Fixed emitting internal errors while reconnecting with auth - Fixed crashing with invalid url option -## v.2.7.1 - 14 Mar, 2017 +## v2.7.1 - 14 Mar, 2017 Bugfixes - Fixed monitor mode not working in combination with IPv6 (2.6.0 regression) -## v.2.7.0 - 11 Mar, 2017 +## v2.7.0 - 11 Mar, 2017 Features @@ -59,160 +59,160 @@ Bugfixes - Fixed rename_commands not accepting `null` as value - Fixed `AbortError`s and `AggregateError`s not showing the error message in the stack trace -## v.2.6.5 - 15 Jan, 2017 +## v2.6.5 - 15 Jan, 2017 Bugfixes - Fixed parser not being reset in case the redis connection closed ASAP for overcoming of output buffer limits - Fixed parser reset if (p)message_buffer listener is attached -## v.2.6.4 - 12 Jan, 2017 +## v2.6.4 - 12 Jan, 2017 Bugfixes - Fixed monitor mode not working in combination with IPv6, sockets or lua scripts (2.6.0 regression) -## v.2.6.3 - 31 Oct, 2016 +## v2.6.3 - 31 Oct, 2016 Bugfixes - Do not change the tls setting to camel_case - Fix domain handling in combination with the offline queue (2.5.3 regression) -## v.2.6.2 - 16 Jun, 2016 +## v2.6.2 - 16 Jun, 2016 Bugfixes - Fixed individual callbacks of a transaction not being called (2.6.0 regression) -## v.2.6.1 - 02 Jun, 2016 +## v2.6.1 - 02 Jun, 2016 Bugfixes - Fixed invalid function name being exported -## v.2.6.0 - 01 Jun, 2016 +## v2.6.0 - 01 Jun, 2016 In addition to the pre-releases the following changes exist in v.2.6.0: Features -- Updated [redis-parser](https://github.com/NodeRedis/node-redis-parser) dependency ([changelog](https://github.com/NodeRedis/node-redis-parser/releases/tag/v.2.0.0)) - - The JS parser is from now on the new default as it is a lot faster than the hiredis parser - - This is no BC as there is no changed behavior for the user at all but just a performance improvement. Explicitly requireing the Hiredis parser is still possible. -- Added name property to all Redis functions (Node.js >= 4.0) -- Improved stack traces in development and debug mode +- Updated [redis-parser](https://github.com/NodeRedis/node-redis-parser) dependency ([changelog](https://github.com/NodeRedis/node-redis-parser/releases/tag/v.2.0.0)) +- The JS parser is from now on the new default as it is a lot faster than the hiredis parser +- This is no BC as there is no changed behavior for the user at all but just a performance improvement. Explicitly requireing the Hiredis parser is still possible. +- Added name property to all Redis functions (Node.js >= 4.0) +- Improved stack traces in development and debug mode Bugfixes -- Reverted support for `__proto__` (v.2.6.0-2) to prevent and breaking change +- Reverted support for `__proto__` (v.2.6.0-2) to prevent and breaking change Deprecations -- The `parser` option is deprecated and should be removed. The built-in Javascript parser is a lot faster than the hiredis parser and has more features +- The `parser` option is deprecated and should be removed. The built-in Javascript parser is a lot faster than the hiredis parser and has more features -## v.2.6.0-2 - 29 Apr, 2016 +## v2.6.0-2 - 29 Apr, 2016 Features -- Added support for the new [CLIENT REPLY ON|OFF|SKIP](http://redis.io/commands/client-reply) command (Redis v.3.2) -- Added support for camelCase - - The Node.js landscape default is to use camelCase. node_redis is a bit out of the box here - but from now on it is possible to use both, just as you prefer! - - If there's any documented variable missing as camelCased, please open a issue for it -- Improve error handling significantly - - Only emit an error if the error has not already been handled in a callback - - Improved unspecific error messages e.g. "Connection gone from end / close event" - - Added `args` to command errors to improve identification of the error - - Added origin to errors if there's e.g. a connection error - - Added ReplyError class. All Redis errors are from now on going to be of that class - - Added AbortError class. A subclass of AbortError. All unresolved and by node_redis rejected commands are from now on of that class - - Added AggregateError class. If a unresolved and by node_redis rejected command has no callback and - this applies to more than a single command, the errors for the commands without callback are aggregated - to a single error that is emitted in debug_mode in that case. -- Added `message_buffer` / `pmessage_buffer` events. That event is always going to emit a buffer - - Listening to the `message` event at the same time is always going to return the same message as string -- Added callback option to the duplicate function -- Added support for `__proto__` and other reserved keywords as hgetall field -- Updated [redis-commands](https://github.com/NodeRedis/redis-commands) dependency ([changelog](https://github.com/NodeRedis/redis-commands/releases/tag/v.1.2.0)) +- Added support for the new [CLIENT REPLY ON|OFF|SKIP](http://redis.io/commands/client-reply) command (Redis v.3.2) +- Added support for camelCase +- The Node.js landscape default is to use camelCase. node_redis is a bit out of the box here + but from now on it is possible to use both, just as you prefer! +- If there's any documented variable missing as camelCased, please open a issue for it +- Improve error handling significantly +- Only emit an error if the error has not already been handled in a callback +- Improved unspecific error messages e.g. "Connection gone from end / close event" +- Added `args` to command errors to improve identification of the error +- Added origin to errors if there's e.g. a connection error +- Added ReplyError class. All Redis errors are from now on going to be of that class +- Added AbortError class. A subclass of AbortError. All unresolved and by node_redis rejected commands are from now on of that class +- Added AggregateError class. If a unresolved and by node_redis rejected command has no callback and + this applies to more than a single command, the errors for the commands without callback are aggregated + to a single error that is emitted in debug_mode in that case. +- Added `message_buffer` / `pmessage_buffer` events. That event is always going to emit a buffer +- Listening to the `message` event at the same time is always going to return the same message as string +- Added callback option to the duplicate function +- Added support for `__proto__` and other reserved keywords as hgetall field +- Updated [redis-commands](https://github.com/NodeRedis/redis-commands) dependency ([changelog](https://github.com/NodeRedis/redis-commands/releases/tag/v.1.2.0)) Bugfixes -- Fixed v.2.5.0 auth command regression (under special circumstances a reconnect would not authenticate properly) -- Fixed v.2.6.0-0 pub sub mode and quit command regressions: - - Entering pub sub mode not working if a earlier called and still running command returned an error - - Unsubscribe callback not called if unsubscribing from all channels and resubscribing right away - - Quit command resulting in an error in some cases -- Fixed special handled functions in batch and multi context not working the same as without (e.g. select and info) - - Be aware that not all commands work in combination with transactions but they all work with batch -- Fixed address always set to 127.0.0.1:6379 in case host / port is set in the `tls` options instead of the general options +- Fixed v.2.5.0 auth command regression (under special circumstances a reconnect would not authenticate properly) +- Fixed v.2.6.0-0 pub sub mode and quit command regressions: +- Entering pub sub mode not working if a earlier called and still running command returned an error +- Unsubscribe callback not called if unsubscribing from all channels and resubscribing right away +- Quit command resulting in an error in some cases +- Fixed special handled functions in batch and multi context not working the same as without (e.g. select and info) +- Be aware that not all commands work in combination with transactions but they all work with batch +- Fixed address always set to 127.0.0.1:6379 in case host / port is set in the `tls` options instead of the general options -## v.2.6.0-1 - 01 Apr, 2016 +## v2.6.0-1 - 01 Apr, 2016 A second pre-release with further fixes. This is likely going to be released as 2.6.0 stable without further changes. Features -- Added type validations for client.send_command arguments +- Added type validations for client.send_command arguments Bugfixes -- Fixed client.send_command not working properly with every command and every option -- Fixed pub sub mode unsubscribing from all channels in combination with the new `string_numbers` option crashing -- Fixed pub sub mode unsubscribing from all channels not respected while reconnecting -- Fixed pub sub mode events in combination with the `string_numbers` option emitting the number of channels not as number +- Fixed client.send_command not working properly with every command and every option +- Fixed pub sub mode unsubscribing from all channels in combination with the new `string_numbers` option crashing +- Fixed pub sub mode unsubscribing from all channels not respected while reconnecting +- Fixed pub sub mode events in combination with the `string_numbers` option emitting the number of channels not as number -## v.2.6.0-0 - 27 Mar, 2016 +## v2.6.0-0 - 27 Mar, 2016 This is mainly a very important bug fix release with some smaller features. Features -- Monitor and pub sub mode now work together with the offline queue - - All commands that were send after a connection loss are now going to be send after reconnecting -- Activating monitor mode does now work together with arbitrary commands including pub sub mode -- Pub sub mode is completely rewritten and all known issues fixed -- Added `string_numbers` option to get back strings instead of numbers -- Quit command is from now on always going to end the connection properly +- Monitor and pub sub mode now work together with the offline queue +- All commands that were send after a connection loss are now going to be send after reconnecting +- Activating monitor mode does now work together with arbitrary commands including pub sub mode +- Pub sub mode is completely rewritten and all known issues fixed +- Added `string_numbers` option to get back strings instead of numbers +- Quit command is from now on always going to end the connection properly Bugfixes -- Fixed calling monitor command while other commands are still running -- Fixed monitor and pub sub mode not working together -- Fixed monitor mode not working in combination with the offline queue -- Fixed pub sub mode not working in combination with the offline queue -- Fixed pub sub mode resubscribing not working with non utf8 buffer channels -- Fixed pub sub mode crashing if calling unsubscribe / subscribe in various combinations -- Fixed pub sub mode emitting unsubscribe even if no channels were unsubscribed -- Fixed pub sub mode emitting a message without a message published -- Fixed quit command not ending the connection and resulting in further reconnection if called while reconnecting +- Fixed calling monitor command while other commands are still running +- Fixed monitor and pub sub mode not working together +- Fixed monitor mode not working in combination with the offline queue +- Fixed pub sub mode not working in combination with the offline queue +- Fixed pub sub mode resubscribing not working with non utf8 buffer channels +- Fixed pub sub mode crashing if calling unsubscribe / subscribe in various combinations +- Fixed pub sub mode emitting unsubscribe even if no channels were unsubscribed +- Fixed pub sub mode emitting a message without a message published +- Fixed quit command not ending the connection and resulting in further reconnection if called while reconnecting The quit command did not end connections earlier if the connection was down at that time and this could have lead to strange situations, therefor this was fixed to end the connection right away in those cases. -## v.2.5.3 - 21 Mar, 2016 +## v2.5.3 - 21 Mar, 2016 Bugfixes -- Revert throwing on invalid data types and print a warning instead +- Revert throwing on invalid data types and print a warning instead -## v.2.5.2 - 16 Mar, 2016 +## v2.5.2 - 16 Mar, 2016 Bugfixes -- Fixed breaking changes against Redis 2.4 introduced in 2.5.0 / 2.5.1 +- Fixed breaking changes against Redis 2.4 introduced in 2.5.0 / 2.5.1 -## v.2.5.1 - 15 Mar, 2016 +## v2.5.1 - 15 Mar, 2016 Bugfixes -- Fixed info command not working anymore with optional section argument +- Fixed info command not working anymore with optional section argument -## v.2.5.0 - 15 Mar, 2016 +## v2.5.0 - 15 Mar, 2016 Same changelog as the pre-release -## v.2.5.0-1 - 07 Mar, 2016 +## v2.5.0-1 - 07 Mar, 2016 This is a big release with some substantial underlining changes. Therefor this is released as a pre-release and I encourage anyone who's able to, to test this out. @@ -222,173 +222,173 @@ This release is also going to deprecate a couple things to prepare for a future Features -- The parsers moved into the [redis-parser](https://github.com/NodeRedis/node-redis-parser) module and will be maintained in there from now on - - Improve js parser speed significantly for big SUNION/SINTER/LRANGE/ZRANGE -- Improve redis-url parsing to also accept the database-number and options as query parameters as suggested in [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis) -- Added a `retry_unfulfilled_commands` option - - Setting this to 'true' results in retrying all commands that were not fulfilled on a connection loss after the reconnect. Use with caution -- Added a `db` option to select the database while connecting (this is [not recommended](https://groups.google.com/forum/#!topic/redis-db/vS5wX8X4Cjg)) -- Added a `password` option as alias for auth_pass -- The client.server_info is from now on updated while using the info command -- Gracefuly handle redis protocol errors from now on -- Added a `warning` emitter that receives node_redis warnings like auth not required and deprecation messages -- Added a `retry_strategy` option that replaces all reconnect options -- The reconnecting event from now on also receives: - - The error message why the reconnect happened (params.error) - - The amount of times the client was connected (params.times_connected) - - The total reconnecting time since the last time connected (params.total_retry_time) -- Always respect the command execution order no matter if the reply could be returned sync or not (former exceptions: [#937](https://github.com/NodeRedis/node_redis/issues/937#issuecomment-167525939)) -- redis.createClient is now checking input values stricter and detects more faulty input -- Started refactoring internals into individual modules -- Pipelining speed improvements +- The parsers moved into the [redis-parser](https://github.com/NodeRedis/node-redis-parser) module and will be maintained in there from now on +- Improve js parser speed significantly for big SUNION/SINTER/LRANGE/ZRANGE +- Improve redis-url parsing to also accept the database-number and options as query parameters as suggested in [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis) +- Added a `retry_unfulfilled_commands` option +- Setting this to 'true' results in retrying all commands that were not fulfilled on a connection loss after the reconnect. Use with caution +- Added a `db` option to select the database while connecting (this is [not recommended](https://groups.google.com/forum/#!topic/redis-db/vS5wX8X4Cjg)) +- Added a `password` option as alias for auth_pass +- The client.server_info is from now on updated while using the info command +- Gracefuly handle redis protocol errors from now on +- Added a `warning` emitter that receives node_redis warnings like auth not required and deprecation messages +- Added a `retry_strategy` option that replaces all reconnect options +- The reconnecting event from now on also receives: +- The error message why the reconnect happened (params.error) +- The amount of times the client was connected (params.times_connected) +- The total reconnecting time since the last time connected (params.total_retry_time) +- Always respect the command execution order no matter if the reply could be returned sync or not (former exceptions: [#937](https://github.com/NodeRedis/node_redis/issues/937#issuecomment-167525939)) +- redis.createClient is now checking input values stricter and detects more faulty input +- Started refactoring internals into individual modules +- Pipelining speed improvements Bugfixes -- Fixed explicit undefined as a command callback in a multi context -- Fixed hmset failing to detect the first key as buffer or date if the key is of that type -- Fixed do not run toString on an array argument and throw a "invalid data" error instead - - This is not considered as breaking change, as this is likely a error in your code and if you want to have such a behavior you should handle this beforehand - - The same applies to Map / Set and individual Object types -- Fixed redis url not accepting the protocol being omitted or protocols other than the redis protocol for convenience -- Fixed parsing the db keyspace even if the first database does not begin with a zero -- Fixed handling of errors occurring while receiving pub sub messages -- Fixed huge string pipelines crashing NodeJS (Pipeline size above 256mb) -- Fixed rename_commands and prefix option not working together -- Fixed ready being emitted to early in case a slave is still syncing / master down +- Fixed explicit undefined as a command callback in a multi context +- Fixed hmset failing to detect the first key as buffer or date if the key is of that type +- Fixed do not run toString on an array argument and throw a "invalid data" error instead +- This is not considered as breaking change, as this is likely a error in your code and if you want to have such a behavior you should handle this beforehand +- The same applies to Map / Set and individual Object types +- Fixed redis url not accepting the protocol being omitted or protocols other than the redis protocol for convenience +- Fixed parsing the db keyspace even if the first database does not begin with a zero +- Fixed handling of errors occurring while receiving pub sub messages +- Fixed huge string pipelines crashing NodeJS (Pipeline size above 256mb) +- Fixed rename_commands and prefix option not working together +- Fixed ready being emitted to early in case a slave is still syncing / master down Deprecations -- Using any command with a argument being set to null or undefined is deprecated - - From v.3.0.0 on using a command with such an argument will return an error instead - - If you want to keep the old behavior please use a precheck in your code that converts the arguments to a string. - - Using SET or SETEX with a undefined or null value will from now on also result in converting the value to "null" / "undefined" to have a consistent behavior. This is not considered as breaking change, as it returned an error earlier. -- Using .end(flush) without the flush parameter is deprecated and the flush parameter should explicitly be used - - From v.3.0.0 on using .end without flush will result in an error - - Using .end without flush means that any command that did not yet return is going to silently fail. Therefor this is considered harmful and you should explicitly silence such errors if you are sure you want this -- Depending on the return value of a command to detect the backpressure is deprecated - - From version 3.0.0 on node_redis might not return true / false as a return value anymore. Please rely on client.should_buffer instead -- The `socket_nodelay` option is deprecated and will be removed in v.3.0.0 - - If you want to buffer commands you should use [.batch or .multi](./README.md) instead. This is necessary to reduce the amount of different options and this is very likely reducing your throughput if set to false. - - If you are sure you want to activate the NAGLE algorithm you can still activate it by using client.stream.setNoDelay(false) -- The `max_attempts` option is deprecated and will be removed in v.3.0.0. Please use the `retry_strategy` instead -- The `retry_max_delay` option is deprecated and will be removed in v.3.0.0. Please use the `retry_strategy` instead -- The drain event is deprecated and will be removed in v.3.0.0. Please listen to the stream drain event instead -- The idle event is deprecated and will likely be removed in v.3.0.0. If you rely on this feature please open a new ticket in node_redis with your use case -- Redis < v. 2.6 is not officially supported anymore and might not work in all cases. Please update to a newer redis version as it is not possible to test for these old versions -- Removed non documented command syntax (adding the callback to an arguments array instead of passing it as individual argument) - -## v.2.4.2 - 27 Nov, 2015 +- Using any command with a argument being set to null or undefined is deprecated +- From v.3.0.0 on using a command with such an argument will return an error instead +- If you want to keep the old behavior please use a precheck in your code that converts the arguments to a string. +- Using SET or SETEX with a undefined or null value will from now on also result in converting the value to "null" / "undefined" to have a consistent behavior. This is not considered as breaking change, as it returned an error earlier. +- Using .end(flush) without the flush parameter is deprecated and the flush parameter should explicitly be used +- From v.3.0.0 on using .end without flush will result in an error +- Using .end without flush means that any command that did not yet return is going to silently fail. Therefor this is considered harmful and you should explicitly silence such errors if you are sure you want this +- Depending on the return value of a command to detect the backpressure is deprecated +- From version 3.0.0 on node_redis might not return true / false as a return value anymore. Please rely on client.should_buffer instead +- The `socket_nodelay` option is deprecated and will be removed in v.3.0.0 +- If you want to buffer commands you should use [.batch or .multi](./README.md) instead. This is necessary to reduce the amount of different options and this is very likely reducing your throughput if set to false. +- If you are sure you want to activate the NAGLE algorithm you can still activate it by using client.stream.setNoDelay(false) +- The `max_attempts` option is deprecated and will be removed in v.3.0.0. Please use the `retry_strategy` instead +- The `retry_max_delay` option is deprecated and will be removed in v.3.0.0. Please use the `retry_strategy` instead +- The drain event is deprecated and will be removed in v.3.0.0. Please listen to the stream drain event instead +- The idle event is deprecated and will likely be removed in v.3.0.0. If you rely on this feature please open a new ticket in node_redis with your use case +- Redis < v. 2.6 is not officially supported anymore and might not work in all cases. Please update to a newer redis version as it is not possible to test for these old versions +- Removed non documented command syntax (adding the callback to an arguments array instead of passing it as individual argument) + +## v2.4.2 - 27 Nov, 2015 Bugfixes -- Fixed not emitting ready after reconnect with disable_resubscribing ([@maxgalbu](https://github.com/maxgalbu)) +- Fixed not emitting ready after reconnect with disable_resubscribing ([@maxgalbu](https://github.com/maxgalbu)) -## v.2.4.1 - 25 Nov, 2015 +## v2.4.1 - 25 Nov, 2015 Bugfixes -- Fixed a js parser regression introduced in 2.4.0 ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed a js parser regression introduced in 2.4.0 ([@BridgeAR](https://github.com/BridgeAR)) -## v.2.4.0 - 25 Nov, 2015 +## v2.4.0 - 25 Nov, 2015 Features -- Added `tls` option to initiate a connection to a redis server behind a TLS proxy. Thanks ([@paddybyers](https://github.com/paddybyers)) -- Added `prefix` option to auto key prefix any command with the provided prefix ([@luin](https://github.com/luin) & [@BridgeAR](https://github.com/BridgeAR)) -- Added `url` option to pass the connection url with the options object ([@BridgeAR](https://github.com/BridgeAR)) -- Added `client.duplicate([options])` to duplicate the current client and return a new one with the same options ([@BridgeAR](https://github.com/BridgeAR)) -- Improve performance by up to 20% on almost all use cases ([@BridgeAR](https://github.com/BridgeAR)) +- Added `tls` option to initiate a connection to a redis server behind a TLS proxy. Thanks ([@paddybyers](https://github.com/paddybyers)) +- Added `prefix` option to auto key prefix any command with the provided prefix ([@luin](https://github.com/luin) & [@BridgeAR](https://github.com/BridgeAR)) +- Added `url` option to pass the connection url with the options object ([@BridgeAR](https://github.com/BridgeAR)) +- Added `client.duplicate([options])` to duplicate the current client and return a new one with the same options ([@BridgeAR](https://github.com/BridgeAR)) +- Improve performance by up to 20% on almost all use cases ([@BridgeAR](https://github.com/BridgeAR)) Bugfixes -- Fixed js parser handling big values slow ([@BridgeAR](https://github.com/BridgeAR)) - - The speed is now on par with the hiredis parser. +- Fixed js parser handling big values slow ([@BridgeAR](https://github.com/BridgeAR)) +- The speed is now on par with the hiredis parser. -## v.2.3.1 - 18 Nov, 2015 +## v2.3.1 - 18 Nov, 2015 Bugfixes -- Fixed saving buffers with charsets other than utf-8 while using multi ([@BridgeAR](https://github.com/BridgeAR)) -- Fixed js parser handling big values very slow ([@BridgeAR](https://github.com/BridgeAR)) - - The speed is up to ~500% faster than before but still up to ~50% slower than the hiredis parser. +- Fixed saving buffers with charsets other than utf-8 while using multi ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed js parser handling big values very slow ([@BridgeAR](https://github.com/BridgeAR)) +- The speed is up to ~500% faster than before but still up to ~50% slower than the hiredis parser. -## v.2.3.0 - 30 Oct, 2015 +## v2.3.0 - 30 Oct, 2015 Features -- Improve speed further for: ([@BridgeAR](https://github.com/BridgeAR)) - - saving big strings (up to +300%) - - using .multi / .batch (up to +50% / on Node.js 0.10.x +300%) - - saving small buffers -- Increased coverage to 99% ([@BridgeAR](https://github.com/BridgeAR)) -- Refactored manual backpressure control ([@BridgeAR](https://github.com/BridgeAR)) - - Removed the high water mark and low water mark. Such a mechanism should be implemented by a user instead - - The `drain` event is from now on only emitted if the stream really had to buffer -- Reduced the default connect_timeout to be one hour instead of 24h ([@BridgeAR](https://github.com/BridgeAR)) -- Added .path to redis.createClient(options); ([@BridgeAR](https://github.com/BridgeAR)) -- Ignore info command, if not available on server ([@ivanB1975](https://github.com/ivanB1975)) +- Improve speed further for: ([@BridgeAR](https://github.com/BridgeAR)) +- saving big strings (up to +300%) +- using .multi / .batch (up to +50% / on Node.js 0.10.x +300%) +- saving small buffers +- Increased coverage to 99% ([@BridgeAR](https://github.com/BridgeAR)) +- Refactored manual backpressure control ([@BridgeAR](https://github.com/BridgeAR)) +- Removed the high water mark and low water mark. Such a mechanism should be implemented by a user instead +- The `drain` event is from now on only emitted if the stream really had to buffer +- Reduced the default connect_timeout to be one hour instead of 24h ([@BridgeAR](https://github.com/BridgeAR)) +- Added .path to redis.createClient(options); ([@BridgeAR](https://github.com/BridgeAR)) +- Ignore info command, if not available on server ([@ivanB1975](https://github.com/ivanB1975)) Bugfixes -- Fixed a js parser error that could result in a timeout ([@BridgeAR](https://github.com/BridgeAR)) -- Fixed .multi / .batch used with Node.js 0.10.x not working properly after a reconnect ([@BridgeAR](https://github.com/BridgeAR)) -- Fixed fired but not yet returned commands not being rejected after a connection loss ([@BridgeAR](https://github.com/BridgeAR)) -- Fixed connect_timeout not respected if no connection has ever been established ([@gagle](https://github.com/gagle) & [@benjie](https://github.com/benjie)) -- Fixed return_buffers in pub sub mode ([@komachi](https://github.com/komachi)) +- Fixed a js parser error that could result in a timeout ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed .multi / .batch used with Node.js 0.10.x not working properly after a reconnect ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed fired but not yet returned commands not being rejected after a connection loss ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed connect_timeout not respected if no connection has ever been established ([@gagle](https://github.com/gagle) & [@benjie](https://github.com/benjie)) +- Fixed return_buffers in pub sub mode ([@komachi](https://github.com/komachi)) -## v.2.2.5 - 18 Oct, 2015 +## v2.2.5 - 18 Oct, 2015 Bugfixes -- Fixed undefined options passed to a new instance not accepted (possible with individual .createClient functions) ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed undefined options passed to a new instance not accepted (possible with individual .createClient functions) ([@BridgeAR](https://github.com/BridgeAR)) -## v.2.2.4 - 17 Oct, 2015 +## v2.2.4 - 17 Oct, 2015 Bugfixes -- Fixed unspecific error message for unresolvable commands ([@BridgeAR](https://github.com/BridgeAR)) -- Fixed not allowed command error in pubsub mode not being returned in a provided callback ([@BridgeAR](https://github.com/BridgeAR)) -- Fixed to many commands forbidden in pub sub mode ([@BridgeAR](https://github.com/BridgeAR)) -- Fixed mutation of the arguments array passed to .multi / .batch constructor ([@BridgeAR](https://github.com/BridgeAR)) -- Fixed mutation of the options object passed to createClient ([@BridgeAR](https://github.com/BridgeAR)) -- Fixed error callback in .multi not called if connection in broken mode ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed unspecific error message for unresolvable commands ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed not allowed command error in pubsub mode not being returned in a provided callback ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed to many commands forbidden in pub sub mode ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed mutation of the arguments array passed to .multi / .batch constructor ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed mutation of the options object passed to createClient ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed error callback in .multi not called if connection in broken mode ([@BridgeAR](https://github.com/BridgeAR)) -## v.2.2.3 - 14 Oct, 2015 +## v2.2.3 - 14 Oct, 2015 Bugfixes -- Fixed multi not being executed on Node 0.10.x if node_redis not yet ready ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed multi not being executed on Node 0.10.x if node_redis not yet ready ([@BridgeAR](https://github.com/BridgeAR)) -## v.2.2.2 - 14 Oct, 2015 +## v2.2.2 - 14 Oct, 2015 Bugfixes -- Fixed regular commands not being executed after a .multi until .exec was called ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed regular commands not being executed after a .multi until .exec was called ([@BridgeAR](https://github.com/BridgeAR)) -## v.2.2.1 - 12 Oct, 2015 +## v2.2.1 - 12 Oct, 2015 No code change -## v.2.2.0 - 12 Oct, 2015 - The peregrino falcon +## v2.2.0 - 12 Oct, 2015 - The peregrino falcon The peregrino falcon is the fasted bird on earth and this is what this release is all about: Increased performance for heavy usage by up to **400%** [sic!] and increased overall performance for any command as well. Please check the benchmarks in the [README.md](README.md) for further details. Features -- Added rename_commands options to handle renamed commands from the redis config ([@digmxl](https://github.com/digmxl) & [@BridgeAR](https://github.com/BridgeAR)) -- Added disable_resubscribing option to prevent a client from resubscribing after reconnecting ([@BridgeAR](https://github.com/BridgeAR)) -- Increased performance ([@BridgeAR](https://github.com/BridgeAR)) - - exchanging built in queue with [@petkaantonov](https://github.com/petkaantonov)'s [double-ended queue](https://github.com/petkaantonov/deque) - - prevent polymorphism - - optimize statements -- Added *.batch* command, similar to .multi but without transaction ([@BridgeAR](https://github.com/BridgeAR)) -- Improved pipelining to minimize the [RTT](http://redis.io/topics/pipelining) further ([@BridgeAR](https://github.com/BridgeAR)) +- Added rename_commands options to handle renamed commands from the redis config ([@digmxl](https://github.com/digmxl) & [@BridgeAR](https://github.com/BridgeAR)) +- Added disable_resubscribing option to prevent a client from resubscribing after reconnecting ([@BridgeAR](https://github.com/BridgeAR)) +- Increased performance ([@BridgeAR](https://github.com/BridgeAR)) +- exchanging built in queue with [@petkaantonov](https://github.com/petkaantonov)'s [double-ended queue](https://github.com/petkaantonov/deque) +- prevent polymorphism +- optimize statements +- Added _.batch_ command, similar to .multi but without transaction ([@BridgeAR](https://github.com/BridgeAR)) +- Improved pipelining to minimize the [RTT](http://redis.io/topics/pipelining) further ([@BridgeAR](https://github.com/BridgeAR)) Bugfixes -- Fixed a javascript parser regression introduced in 2.0 that could result in timeouts on high load. ([@BridgeAR](https://github.com/BridgeAR)) - - I was not able to write a regression test for this, since the error seems to only occur under heavy load with special conditions. So please have a look for timeouts with the js parser, if you use it and report all issues and switch to the hiredis parser in the meanwhile. If you're able to come up with a reproducable test case, this would be even better :) -- Fixed should_buffer boolean for .exec, .select and .auth commands not being returned and fix a couple special conditions ([@BridgeAR](https://github.com/BridgeAR)) +- Fixed a javascript parser regression introduced in 2.0 that could result in timeouts on high load. ([@BridgeAR](https://github.com/BridgeAR)) +- I was not able to write a regression test for this, since the error seems to only occur under heavy load with special conditions. So please have a look for timeouts with the js parser, if you use it and report all issues and switch to the hiredis parser in the meanwhile. If you're able to come up with a reproducable test case, this would be even better :) +- Fixed should_buffer boolean for .exec, .select and .auth commands not being returned and fix a couple special conditions ([@BridgeAR](https://github.com/BridgeAR)) If you do not rely on transactions but want to reduce the RTT you can use .batch from now on. It'll behave just the same as .multi but it does not have any transaction and therefor won't roll back any failed commands.
Both .multi and .batch are from now on going to cache the commands and release them while calling .exec. @@ -425,29 +425,29 @@ To conclude: we can proudly say that node_redis is very likely outperforming any Known issues -- The pub sub system has some flaws and those will be addressed in the next minor release +- The pub sub system has some flaws and those will be addressed in the next minor release ## v2.1.0 - Oct 02, 2015 Features: -- Addded optional flush parameter to `.end`. If set to true, commands fired after using .end are going to be rejected instead of being ignored. (@crispy1989) -- Addded: host and port can now be provided in a single options object. E.g. redis.createClient({ host: 'localhost', port: 1337, max_attempts: 5 }); (@BridgeAR) -- Speedup common cases (@BridgeAR) +- Addded optional flush parameter to `.end`. If set to true, commands fired after using .end are going to be rejected instead of being ignored. (@crispy1989) +- Addded: host and port can now be provided in a single options object. E.g. redis.createClient({ host: 'localhost', port: 1337, max_attempts: 5 }); (@BridgeAR) +- Speedup common cases (@BridgeAR) Bugfixes: -- Fix argument mutation while using the array notation with the multi constructor (@BridgeAR) -- Fix multi.hmset key not being type converted if used with an object and key not being a string (@BridgeAR) -- Fix parser errors not being catched properly (@BridgeAR) -- Fix a crash that could occur if a redis server does not return the info command as usual #541 (@BridgeAR) -- Explicitly passing undefined as a callback statement will work again. E.g. client.publish('channel', 'message', undefined); (@BridgeAR) +- Fix argument mutation while using the array notation with the multi constructor (@BridgeAR) +- Fix multi.hmset key not being type converted if used with an object and key not being a string (@BridgeAR) +- Fix parser errors not being catched properly (@BridgeAR) +- Fix a crash that could occur if a redis server does not return the info command as usual #541 (@BridgeAR) +- Explicitly passing undefined as a callback statement will work again. E.g. client.publish('channel', 'message', undefined); (@BridgeAR) ## v2.0.1 - Sep 24, 2015 Bugfixes: -- Fix argument mutation while using the array notation in combination with keys / callbacks ([#866](.)). (@BridgeAR) +- Fix argument mutation while using the array notation in combination with keys / callbacks ([#866](.)). (@BridgeAR) ## v2.0.0 - Sep 21, 2015 @@ -473,7 +473,7 @@ This is the biggest release that node_redis had since it was released in 2010. A - Added: Network errors and other stream errors will from now on include the error code as `err.code` property (@BridgeAR) - Added: Errors thrown by redis will now include the redis error code as `err.code` property. (@skeggse & @BridgeAR) - Added: Errors thrown by node_redis will now include a `err.command` property for the command used (@BridgeAR) -- Added new commands and drop support for deprecated *substr* (@BridgeAR) +- Added new commands and drop support for deprecated _substr_ (@BridgeAR) - Added new possibilities how to provide the command arguments (@BridgeAR) - The entries in the keyspace of the server_info is now an object instead of a string. (@SinisterLight & @BridgeAR) - Small speedup here and there (e.g. by not using .toLowerCase() anymore) (@BridgeAR) @@ -482,7 +482,7 @@ This is the biggest release that node_redis had since it was released in 2010. A - Remove dead code, clean up and refactor very old chunks (@BridgeAR) - Don't flush the offline queue if reconnecting (@BridgeAR) - Emit all errors insteaf of throwing sometimes and sometimes emitting them (@BridgeAR) -- *auth_pass* passwords are now checked to be a valid password (@jcppman & @BridgeAR) +- _auth_pass_ passwords are now checked to be a valid password (@jcppman & @BridgeAR) ## Bug fixes: @@ -512,11 +512,13 @@ This is the biggest release that node_redis had since it was released in 2010. A 2. Error messages have changed quite a bit. If you depend on a specific wording please check your application carfully. 3. Errors are from now on always either returned if a callback is present or emitted. They won't be thrown (neither sync, nor async). 4. The Multi error handling has changed a lot! - - All errors are from now on errors instead of strings (this only applied to the js parser). - - If an error occurs while queueing the commands an EXECABORT error will be returned including the failed commands as `.errors` property instead of an array with errors. - - If an error occurs while executing the commands and that command has a callback it'll return the error as first parameter (`err, undefined` instead of `null, undefined`). - - All the errors occuring while executing the commands will stay in the result value as error instance (if you used the js parser before they would have been strings). Be aware that the transaction won't be aborted if those error occurr! - - If `multi.exec` does not have a callback and an EXECABORT error occurrs, it'll emit that error instead. + +- All errors are from now on errors instead of strings (this only applied to the js parser). +- If an error occurs while queueing the commands an EXECABORT error will be returned including the failed commands as `.errors` property instead of an array with errors. +- If an error occurs while executing the commands and that command has a callback it'll return the error as first parameter (`err, undefined` instead of `null, undefined`). +- All the errors occuring while executing the commands will stay in the result value as error instance (if you used the js parser before they would have been strings). Be aware that the transaction won't be aborted if those error occurr! +- If `multi.exec` does not have a callback and an EXECABORT error occurrs, it'll emit that error instead. + 5. If redis can't connect to your redis server it'll give up after a certain point of failures (either max connection attempts or connection timeout exceeded). If that is the case it'll emit an CONNECTION_BROKEN error. You'll have to initiate a new client to try again afterwards. 6. The offline queue is not flushed anymore on a reconnect. It'll stay until node_redis gives up trying to reach the server or until you close the connection. 7. Before this release node_redis catched user errors and threw them async back. This is not the case anymore! No user behavior of what so ever will be tracked or catched. @@ -530,110 +532,113 @@ From now on we'll push new releases more frequently out and fix further long out ## v1.0.0 - Aug 30, 2015 -* Huge issue and pull-request cleanup. Thanks Blain! (@blainsmith) -* [#658](https://github.com/NodeRedis/node_redis/pull/658) Client now parses URL-format connection strings (e.g., redis://foo:pass@127.0.0.1:8080) (@kuwabarahiroshi) -* [#749](https://github.com/NodeRedis/node_redis/pull/749) Fix reconnection bug when client is in monitoring mode (@danielbprice) -* [#786](https://github.com/NodeRedis/node_redis/pull/786) Refactor createClient. Fixes #651 (@BridgeAR) -* [#793](https://github.com/NodeRedis/node_redis/pull/793) Refactor tests and improve test coverage (@erinspice, @bcoe) -* [#733](https://github.com/NodeRedis/node_redis/pull/733) Fixes detect_buffers functionality in the context of exec. Fixes #732, #263 (@raydog) -* [#785](https://github.com/NodeRedis/node_redis/pull/785) Tiny speedup by using 'use strict' (@BridgeAR) -* Fix extraneous error output due to pubsub tests (Mikael Kohlmyr) +- Huge issue and pull-request cleanup. Thanks Blain! (@blainsmith) +- [#658](https://github.com/NodeRedis/node_redis/pull/658) Client now parses URL-format connection strings (e.g., redis://foo:pass@127.0.0.1:8080) (@kuwabarahiroshi) +- [#749](https://github.com/NodeRedis/node_redis/pull/749) Fix reconnection bug when client is in monitoring mode (@danielbprice) +- [#786](https://github.com/NodeRedis/node_redis/pull/786) Refactor createClient. Fixes #651 (@BridgeAR) +- [#793](https://github.com/NodeRedis/node_redis/pull/793) Refactor tests and improve test coverage (@erinspice, @bcoe) +- [#733](https://github.com/NodeRedis/node_redis/pull/733) Fixes detect_buffers functionality in the context of exec. Fixes #732, #263 (@raydog) +- [#785](https://github.com/NodeRedis/node_redis/pull/785) Tiny speedup by using 'use strict' (@BridgeAR) +- Fix extraneous error output due to pubsub tests (Mikael Kohlmyr) ## v0.12.1 - Aug 10, 2014 -* Fix IPv6/IPv4 family selection in node 0.11+ (Various) + +- Fix IPv6/IPv4 family selection in node 0.11+ (Various) ## v0.12.0 - Aug 9, 2014 -* Fix unix socket support (Jack Tang) -* Improve createClient argument handling (Jack Tang) + +- 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) +- 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) +- Update command list to match Redis 2.8.9 (Charles Feng) ## v0.10.2 - May 18, 2014 -* Better binary key handling for HGETALL. (Nick Apperson) -* Fix test not resetting `error` handler. (CrypticSwarm) -* Fix SELECT error semantics. (Bryan English) +- Better binary key handling 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) +- 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) +- 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) +- 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) +- 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) +- 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) +- 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) +- 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) + +- 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) +- 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 @@ -650,20 +655,20 @@ Important bug fix for null responses (Jerry Sievert) 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) +- 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) +- [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 @@ -675,21 +680,21 @@ Very much need automated tests for reconnection and queue logic. 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) +- [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 +- 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 @@ -698,16 +703,16 @@ Some more goodies: 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, +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) +- Support SlowBuffers (Umair Siddique) +- Add Multi to exports (Louis-Philippe Perron) +- Fix for drain event calculation (Vladimir Dronnikov) Thanks! @@ -719,15 +724,15 @@ Fix bug with optional callbacks for hmset. Bugs fixed: -* authentication retry while server is loading db (danmaz74) [GH-101] -* command arguments processing issue with arrays +- 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. +- 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. @@ -735,7 +740,7 @@ See `examples/backpressure_drain.js` for more information. Add support and tests for Redis scripting through EXEC command. -Bug fix for monitor mode. (forddg) +Bug fix for monitor mode. (forddg) Auto update of new commands from redis.io (Dave Hoover) @@ -743,12 +748,12 @@ Auto update of new commands from redis.io (Dave Hoover) 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] +- 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 @@ -788,17 +793,17 @@ 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. +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. +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 +`client.server_options`. Further, the version string is decomposed into individual elements in `client.server_options.versions`. ## v0.5.4 - February 11, 2011 @@ -825,17 +830,17 @@ Fix bug where subscribe commands would not handle redis-server startup error pro Some bug fixes: -* An important bug fix in reconnection logic. Previously, reply callbacks would be invoked twice after +- 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. +- Changed error callback argument to be an actual Error object. New feature: -* Add friendly syntax for HMSET using an object. +- 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. +Remove warning about missing hiredis. You probably do want it though. ## v0.4.0 - December 5, 2010 @@ -865,9 +870,9 @@ Send a friendlier "error" event message on stream errors like connection refused 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. +- 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 diff --git a/README.md b/README.md index bd124739b24..49a21019edb 100644 --- a/README.md +++ b/README.md @@ -19,115 +19,43 @@ --- -This is a complete and feature rich Redis client for Node.js. **It supports all -Redis commands** and focuses on high performance. - -Install with: +## Installation ```bash npm install redis ``` -## Usage Example +## Usage + +#### Example ```js -const redis = require('redis'); +const redis = require("redis"); const 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.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(); -}); +client.set("string key", "string val", redis.print); +client.get("string key", redis.print); ``` -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)$ - Note that the API is entirely asynchronous. To get data back from the server, -you'll need to use a callback. From v.2.6 on the API supports camelCase and -snake_case and all options / variables / events etc. can be used either way. It -is recommended to use camelCase as this is the default for the Node.js -landscape. +you'll need to use a callback. ### Promises -#### Native Promises - -If you are using node v8 or higher, you can promisify node_redis with [util.promisify](https://nodejs.org/api/util.html#util_util_promisify_original) as in: +Node Redis currently doesn't natively support promises (this is coming in v4), however you can wrap the methods you +want to use with promises using the built-in Node.js `util.promisify` method on Node.js >= v8; ```js -const { promisify } = require('util'); +const { promisify } = require("util"); const getAsync = promisify(client.get).bind(client); -``` - -now _getAsync_ is a promisified version of _client.get_: - -```js -// We expect a value 'foo': 'bar' to be present -// So instead of writing client.get('foo', cb); you have to write: -return getAsync('foo').then(function(res) { - console.log(res); // => 'bar' -}); -``` - -or using [async await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function): - -```js -async function myFunc() { - const res = await getAsync('foo'); - console.log(res); -} -``` - -#### Bluebird Promises - -You can also use node_redis with promises by promisifying node_redis with -[bluebird](https://github.com/petkaantonov/bluebird) as in: -```js -const redis = require('redis'); -bluebird.promisifyAll(redis); -``` - -It'll add a _Async_ to all node_redis functions (e.g. return client.getAsync().then()) - -```js -// We expect a value 'foo': 'bar' to be present -// So instead of writing client.get('foo', cb); you have to write: -return client.getAsync('foo').then(function(res) { - console.log(res); // => 'bar' -}); - -// Using multi with promises looks like: - -return client - .multi() - .get('foo') - .execAsync() - .then(function(res) { - console.log(res); // => 'bar' - }); +getAsync + .then(console.log) + .catch(console.error); ``` ### Sending Commands @@ -138,16 +66,15 @@ a variable number of individual arguments followed by an optional callback. Examples: ```js -client.hmset(['key', 'test keys 1', 'test val 1', 'test keys 2', 'test val 2'], function( - err, - res, -) {}); +client.hmset(["key", "test keys 1", "test val 1", "test keys 2", "test val 2"], function(err, res) { + // ... +}); // Works the same as -client.hmset('key', ['test keys 1', 'test val 1', 'test keys 2', 'test val 2'], function(err, res) { +client.hmset("key", ["test keys 1", "test val 1", "test keys 2", "test val 2"], function(err, res) { // ... }); // Or -client.hmset('key', 'test keys 1', 'test val 1', 'test keys 2', 'test val 2', function(err, res) { +client.hmset("key", "test keys 1", "test val 1", "test keys 2", "test val 2", function(err, res) { // ... }); ``` @@ -157,15 +84,15 @@ Care should be taken with user input if arrays are possible (via body-parser, qu Note that in either form the `callback` is optional: ```js -client.set('some key', 'some val'); -client.set(['some other key', 'some val']); +client.set("some key", "some val"); +client.set(["some other key", "some val"]); ``` If the key is missing, reply will be null. Only if the [Redis Command Reference](http://redis.io/commands) states something else it will not be null. ```js -client.get('missingkey', function(err, reply) { +client.get("missingkey", function(err, reply) { // reply is null when the key is missing console.log(reply); }); @@ -189,7 +116,7 @@ Example setting key to auto expire using [SET command](https://redis.io/commands ```js // this key will expire after 10 seconds -client.set('key', 'value!', 'EX', 10); +client.set("key", "value!", "EX", 10); ``` # API @@ -275,18 +202,18 @@ using unix sockets if possible to increase throughput. | retry_strategy | function | A function that receives an options object as parameter including the retry `attempt`, the `total_retry_time` indicating how much time passed since the last time connected, the `error` why the connection was lost and the number of `times_connected` in total. If you return a number from this function, the retry will happen exactly after that time in milliseconds. If you return a non-number, no further retry will happen and all offline commands are flushed with errors. Return an error to return that specific error to all offline commands. Example below. | ```js -var redis = require('redis'); +var redis = require("redis"); var client = redis.createClient({ detect_buffers: true }); -client.set('foo_rand000000000000', 'OK'); +client.set("foo_rand000000000000", "OK"); // This will return a JavaScript String -client.get('foo_rand000000000000', function(err, reply) { +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) { +client.get(new Buffer("foo_rand000000000000"), function(err, reply) { console.log(reply.toString()); // Will print `` }); client.quit(); @@ -297,15 +224,15 @@ client.quit(); ```js var client = redis.createClient({ retry_strategy: function(options) { - if (options.error && options.error.code === 'ECONNREFUSED') { + if (options.error && options.error.code === "ECONNREFUSED") { // End reconnecting on a specific error and flush all commands with // a individual error - return new Error('The server refused the connection'); + return new Error("The server refused the connection"); } if (options.total_retry_time > 1000 * 60 * 60) { // End reconnecting after a specific timeout and flush all commands // with a individual error - return new Error('Retry time exhausted'); + return new Error("Retry time exhausted"); } if (options.attempt > 10) { // End reconnecting with built in error @@ -363,23 +290,23 @@ 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'), +var redis = require("redis"), client = redis.createClient(); -client.set('foo_rand000000000000', 'some fantastic value', function(err, reply) { +client.set("foo_rand000000000000", "some fantastic value", function(err, reply) { // This will either result in an error (flush parameter is set to true) // or will silently fail and this callback will not be called at all (flush set to false) console.log(err); }); client.end(true); // No further commands will be processed -client.get('foo_rand000000000000', function(err, reply) { +client.get("foo_rand000000000000", function(err, reply) { console.log(err); // => 'The connection has already been closed.' }); ``` `client.end()` without the flush parameter set to true should NOT be used in production! -## Error handling (>= v.2.6) +## Error handling (>= v2.6) Currently the following error subclasses exist: @@ -398,27 +325,27 @@ All error classes are exported by the module. Example: ```js -var redis = require('./'); -var assert = require('assert'); +var redis = require("./"); +var assert = require("assert"); var client = redis.createClient(); -client.on('error', function(err) { +client.on("error", function(err) { assert(err instanceof Error); assert(err instanceof redis.AbortError); assert(err instanceof redis.AggregateError); // The set and get get aggregated in here assert.strictEqual(err.errors.length, 2); - assert.strictEqual(err.code, 'NR_CLOSED'); + assert.strictEqual(err.code, "NR_CLOSED"); }); -client.set('foo', 123, 'bar', function(err, res) { +client.set("foo", 123, "bar", function(err, res) { // Too many arguments assert(err instanceof redis.ReplyError); // => true - assert.strictEqual(err.command, 'SET'); - assert.deepStrictEqual(err.args, ['foo', 123, 'bar']); + assert.strictEqual(err.command, "SET"); + assert.deepStrictEqual(err.args, ["foo", 123, "bar"]); redis.debug_mode = true; - client.set('foo', 'bar'); - client.get('foo'); + client.set("foo", "bar"); + client.get("foo"); process.nextTick(function() { // Force closing the connection while the command did not yet return client.end(true); @@ -449,7 +376,7 @@ 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 redis = require("redis"); var client = redis.createClient(); /* @@ -458,7 +385,7 @@ var client = redis.createClient(); client-server connection is alive. */ client.unref(); -client.get('foo', function(err, value) { +client.get("foo", function(err, value) { if (err) throw err; console.log(value); }); @@ -479,8 +406,8 @@ syntax. Example: ```js -client.hmset('hosts', 'mjr', '1', 'another', '23', 'home', '1234'); -client.hgetall('hosts', function(err, obj) { +client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234"); +client.hgetall("hosts", function(err, obj) { console.dir(obj); }); ``` @@ -491,8 +418,8 @@ Multiple values in a hash can be set by supplying an object: ```js client.HMSET(key2, { - '0123456789': 'abcdefghij', // NOTE: key and value will be coerced to strings - 'some manner of key': 'a type of value', + "0123456789": "abcdefghij", // NOTE: key and value will be coerced to strings + "some manner of key": "a type of value", }); ``` @@ -504,7 +431,7 @@ Redis hash. Multiple values may also be set by supplying a list: ```js -client.HMSET(key1, '0123456789', 'abcdefghij', 'some manner of key', 'a type of value'); +client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value"); ``` ## Publish / Subscribe @@ -514,19 +441,19 @@ client connections, subscribes to a channel on one of them, and publishes to tha channel on the other: ```js -var redis = require('redis'); +var redis = require("redis"); var sub = redis.createClient(), pub = redis.createClient(); var msg_count = 0; -sub.on('subscribe', function(channel, count) { - pub.publish('a nice channel', 'I am sending a message.'); - pub.publish('a nice channel', 'I am sending a second message.'); - pub.publish('a nice channel', 'I am sending my last message.'); +sub.on("subscribe", function(channel, count) { + pub.publish("a nice channel", "I am sending a message."); + pub.publish("a nice channel", "I am sending a second message."); + pub.publish("a nice channel", "I am sending my last message."); }); -sub.on('message', function(channel, message) { - console.log('sub channel ' + channel + ': ' + message); +sub.on("message", function(channel, message) { + console.log("sub channel " + channel + ": " + message); msg_count += 1; if (msg_count === 3) { sub.unsubscribe(); @@ -535,7 +462,7 @@ sub.on('message', function(channel, message) { } }); -sub.subscribe('a nice channel'); +sub.subscribe("a nice channel"); ``` When a client issues a `SUBSCRIBE` or `PSUBSCRIBE`, that connection is put into @@ -610,33 +537,33 @@ further information look at [transactions](http://redis.io/topics/transactions)). ```js -var redis = require('./index'), +var redis = require("./index"), client = redis.createClient(), set_size = 20; -client.sadd('bigset', 'a member'); -client.sadd('bigset', 'another member'); +client.sadd("bigset", "a member"); +client.sadd("bigset", "another member"); while (set_size > 0) { - client.sadd('bigset', 'member ' + set_size); + 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) { + .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'); + console.log("MULTI got " + replies.length + " replies"); replies.forEach(function(reply, index) { - console.log('Reply ' + index + ': ' + reply.toString()); + console.log("Reply " + index + ": " + reply.toString()); }); }); ``` @@ -659,17 +586,17 @@ can queue individual commands while still sending regular client command as in this example: ```js -var redis = require('redis'), +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); +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); +client.mset("incr thing", 100, "incr other thing", 1, redis.print); // drains multi queue and runs atomically multi.exec(function(err, replies) { @@ -681,14 +608,14 @@ 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'), +var redis = require("redis"), client = redis.createClient(); client .multi([ - ['mget', 'multifoo', 'multibar', redis.print], - ['incr', 'multifoo'], - ['incr', 'multibar'], + ["mget", "multifoo", "multibar", redis.print], + ["incr", "multifoo"], + ["incr", "multibar"], ]) .exec(function(err, replies) { console.log(replies); @@ -853,14 +780,14 @@ arguments and the raw monitoring string. Example: ```js -var client = require('redis').createClient(); +var client = require("redis").createClient(); client.monitor(function(err, res) { - console.log('Entering monitoring mode.'); + console.log("Entering monitoring mode."); }); -client.set('foo', 'bar'); +client.set("foo", "bar"); -client.on('monitor', function(time, args, raw_reply) { - console.log(time + ': ' + args); // 1458910076.446514:['set', 'foo', 'bar'] +client.on("monitor", function(time, args, raw_reply) { + console.log(time + ": " + args); // 1458910076.446514:['set', 'foo', 'bar'] }); ``` @@ -886,12 +813,12 @@ easy comparison. A handy callback function for displaying return values when testing. Example: ```js -var redis = require('redis'), +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); +client.on("connect", function() { + client.set("foo_rand000000000000", "some fantastic value", redis.print); + client.get("foo_rand000000000000", redis.print); }); ``` @@ -908,12 +835,12 @@ To execute redis multi-word commands like `SCRIPT LOAD` or `CLIENT LIST` pass the second word as first parameter: ```js -client.script('load', 'return 1'); +client.script("load", "return 1"); client .multi() - .script('load', 'return 1') + .script("load", "return 1") .exec(); -client.multi([['script', 'load', 'return 1']]).exec(); +client.multi([["script", "load", "return 1"]]).exec(); ``` ## client.duplicate([options][, callback]) @@ -930,21 +857,21 @@ are used on the same redisClient instance as non-blocking commands, the non-blocking ones may be queued up until after the blocking ones finish. ```js -var Redis = require('redis'); +var Redis = require("redis"); var client = Redis.createClient(); var clientBlocking = client.duplicate(); var get = function() { - console.log('get called'); - client.get('any_key', function() { - console.log('get returned'); + console.log("get called"); + client.get("any_key", function() { + console.log("get returned"); }); setTimeout(get, 1000); }; var brpop = function() { - console.log('brpop called'); - clientBlocking.brpop('nonexistent', 5, function() { - console.log('brpop return'); + console.log("brpop called"); + clientBlocking.brpop("nonexistent", 5, function() { + console.log("brpop return"); setTimeout(brpop, 1000); }); }; @@ -994,16 +921,16 @@ This applies to anything that uses an optional `[WITHSCORES]` or `[LIMIT offset Example: ```js -var args = ['myzset', 1, 'one', 2, 'two', 3, 'three', 99, 'ninety-nine']; +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.'); + console.log("added " + response + " items."); // -Infinity and +Infinity also work - var args1 = ['myzset', '+inf', '-inf']; + var args1 = ["myzset", "+inf", "-inf"]; client.zrevrangebyscore(args1, function(err, response) { if (err) throw err; - console.log('example1', response); + console.log("example1", response); // write your code here }); @@ -1011,10 +938,10 @@ client.zadd(args, function(err, response) { min = 1, offset = 1, count = 2; - var args2 = ['myzset', max, min, 'WITHSCORES', 'LIMIT', offset, count]; + var args2 = ["myzset", max, min, "WITHSCORES", "LIMIT", offset, count]; client.zrevrangebyscore(args2, function(err, response) { if (err) throw err; - console.log('example2', response); + console.log("example2", response); // write your code here }); }); From 41c8177e8def055042cedc13dca90bb0b5887523 Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 02:43:10 +0000 Subject: [PATCH 05/13] docs --- README.md | 152 +++++++++++++++++++++++++----------------------------- 1 file changed, 70 insertions(+), 82 deletions(-) diff --git a/README.md b/README.md index 49a21019edb..935338cdbd2 100644 --- a/README.md +++ b/README.md @@ -47,18 +47,18 @@ you'll need to use a callback. ### Promises Node Redis currently doesn't natively support promises (this is coming in v4), however you can wrap the methods you -want to use with promises using the built-in Node.js `util.promisify` method on Node.js >= v8; +want to use with promises using the built-in Node.js `util.promisify` method on Node.js >= v8; ```js const { promisify } = require("util"); const getAsync = promisify(client.get).bind(client); -getAsync - .then(console.log) - .catch(console.error); +getAsync.then(console.log).catch(console.error); ``` -### Sending Commands +### Commands + +This library is a 1 to 1 mapping of the [Redis commands](https://redis.io/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 @@ -98,50 +98,35 @@ client.get("missingkey", function(err, reply) { }); ``` -For a list of Redis commands, see [Redis Command Reference](http://redis.io/commands) - Minimal parsing is done on the replies. Commands that return a integer return JavaScript Numbers, arrays return JavaScript Array. `HGETALL` returns an Object keyed by the hash keys. All strings will either be returned as string or as buffer depending on your setting. Please be aware that sending null, undefined and Boolean values will result in the value coerced to a string! -# Redis Commands - -This library is a 1 to 1 mapping to [Redis commands](https://redis.io/commands). -It is not a cache library so please refer to Redis commands page for full usage -details. - -Example setting key to auto expire using [SET command](https://redis.io/commands/set) - -```js -// this key will expire after 10 seconds -client.set("key", "value!", "EX", 10); -``` - -# API +## API -## Connection and other Events +### Connection and other Events `client` will emit some events about the state of the connection to the Redis server. -### "ready" +#### "ready" `client` will emit `ready` once a connection is established. Commands issued before the `ready` event are queued, then replayed just before this event is emitted. -### "connect" +#### "connect" `client` will emit `connect` as soon as the stream is connected to the server. -### "reconnecting" +#### "reconnecting" `client` will emit `reconnecting` when trying to reconnect to the Redis server after losing the connection. Listeners are passed an object containing `delay` (in ms from the previous try) and `attempt` (the attempt #) attributes. -### "error" +#### "error" `client` will emit `error` when encountering an error connecting to the Redis server or when any other in node_redis occurs. If you use a command without @@ -150,16 +135,16 @@ listener. So please attach the error listener to node_redis. -### "end" +#### "end" `client` will emit `end` when an established Redis server connection has closed. -### "warning" +#### "warning" `client` will emit `warning` when password was set but none is needed and if a deprecated option / function / similar is used. -## redis.createClient() +### redis.createClient() If you have `redis-server` running on the same machine as node, then the defaults for port and host are probably fine and you don't need to supply any @@ -201,9 +186,11 @@ using unix sockets if possible to increase throughput. | prefix | null | A string used to prefix all used keys (e.g. `namespace:test`). Please be aware that the `keys` command will not be prefixed. The `keys` command has a "pattern" as argument and no key and it would be impossible to determine the existing keys in Redis if this would be prefixed. | | retry_strategy | function | A function that receives an options object as parameter including the retry `attempt`, the `total_retry_time` indicating how much time passed since the last time connected, the `error` why the connection was lost and the number of `times_connected` in total. If you return a number from this function, the retry will happen exactly after that time in milliseconds. If you return a non-number, no further retry will happen and all offline commands are flushed with errors. Return an error to return that specific error to all offline commands. Example below. | +**`detect_buffers` example:** + ```js -var redis = require("redis"); -var client = redis.createClient({ detect_buffers: true }); +const redis = require("redis"); +const client = redis.createClient({ detect_buffers: true }); client.set("foo_rand000000000000", "OK"); @@ -216,10 +203,9 @@ client.get("foo_rand000000000000", function(err, reply) { client.get(new Buffer("foo_rand000000000000"), function(err, reply) { console.log(reply.toString()); // Will print `` }); -client.quit(); ``` -#### `retry_strategy` example +**`retry_strategy` example:** ```js var client = redis.createClient({ @@ -244,7 +230,7 @@ var client = redis.createClient({ }); ``` -## client.auth(password[, callback]) +### client.auth(password[, callback]) When connecting to a Redis server that requires authentication, the `AUTH` command must be sent as the first command after connecting. This can be tricky @@ -256,19 +242,7 @@ 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`. -## backpressure - -### stream - -The client exposed the used [stream](https://nodejs.org/api/stream.html) in -`client.stream` and if the stream or client had to -[buffer](https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback) -the command in `client.should_buffer`. In combination this can be used to -implement backpressure by checking the buffer state before sending a command and -listening to the stream -[drain](https://nodejs.org/api/stream.html#stream_event_drain) event. - -## client.quit(callback) +### client.quit(callback) This sends the quit command to the redis server and ends cleanly right after all running commands were properly handled. If this is called while reconnecting @@ -276,7 +250,7 @@ running commands were properly handled. If this is called while reconnecting connection right away instead of resulting in further reconnections! All offline commands are going to be flushed with an error in that case. -## client.end(flush) +### client.end(flush) 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 @@ -290,25 +264,28 @@ 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(); +const redis = require("redis"); +const client = redis.createClient(); -client.set("foo_rand000000000000", "some fantastic value", function(err, reply) { +client.set("hello", "world", function(err) { // This will either result in an error (flush parameter is set to true) // or will silently fail and this callback will not be called at all (flush set to false) - console.log(err); + console.error(err); }); -client.end(true); // No further commands will be processed -client.get("foo_rand000000000000", function(err, reply) { - console.log(err); // => 'The connection has already been closed.' + +// No further commands will be processed +client.end(true); + +client.get("hello", function(err) { + console.error(err); // => 'The connection has already been closed.' }); ``` `client.end()` without the flush parameter set to true should NOT be used in production! -## Error handling (>= v2.6) +### Error Handling -Currently the following error subclasses exist: +Currently the following `Error` subclasses exist: - `RedisError`: _All errors_ returned by the client - `ReplyError` subclass of `RedisError`: All errors returned by **Redis** itself @@ -322,30 +299,36 @@ Currently the following error subclasses exist: All error classes are exported by the module. -Example: +#### Example ```js -var redis = require("./"); -var assert = require("assert"); -var client = redis.createClient(); +const assert = require("assert"); + +const redis = require("redis"); +const { AbortError, AggregateError, ReplyError } = require("redis"); +const client = redis.createClient(); client.on("error", function(err) { assert(err instanceof Error); - assert(err instanceof redis.AbortError); - assert(err instanceof redis.AggregateError); - // The set and get get aggregated in here + assert(err instanceof AbortError); + assert(err instanceof AggregateError); + + // The set and get are aggregated in here assert.strictEqual(err.errors.length, 2); assert.strictEqual(err.code, "NR_CLOSED"); }); -client.set("foo", 123, "bar", function(err, res) { + +client.set("foo", "bar", "baz", function(err, res) { // Too many arguments - assert(err instanceof redis.ReplyError); // => true + assert(err instanceof ReplyError); // => true assert.strictEqual(err.command, "SET"); assert.deepStrictEqual(err.args, ["foo", 123, "bar"]); redis.debug_mode = true; + client.set("foo", "bar"); client.get("foo"); + process.nextTick(function() { // Force closing the connection while the command did not yet return client.end(true); @@ -366,7 +349,7 @@ If a command unresolved command got rejected a `UNCERTAIN_STATE` code is returned. A `CONNECTION_BROKEN` error code is used in case node_redis gives up to reconnect. -## client.unref() +### client.unref() Call `unref()` on the underlying socket connection to the Redis server, allowing the program to exit once no more commands are pending. @@ -376,34 +359,35 @@ 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(); +const redis = require("redis"); +const 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. -*/ + * 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 +### 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, callback) +#### client.hgetall(hash, callback) -The reply from an HGETALL command will be converted into a JavaScript Object by +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: +**Example:** ```js client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234"); @@ -412,9 +396,11 @@ client.hgetall("hosts", function(err, obj) { }); ``` -### client.hmset(hash, obj[, callback]) +#### client.hmset(hash, obj[, callback]) -Multiple values in a hash can be set by supplying an object: +Multiple values in a hash can be set by supplying an object. + +**Example:** ```js client.HMSET(key2, { @@ -426,15 +412,17 @@ client.HMSET(key2, { 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]) +#### client.hmset(hash, key1, val1, ...keyN, valN, [callback]) + +Multiple values may also be set by supplying more arguments. -Multiple values may also be set by supplying a list: +**Example:** ```js client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value"); ``` -## Publish / Subscribe +### Publish / Subscribe Example of the publish / subscribe API. This program opens two client connections, subscribes to a channel on one of them, and publishes to that From 6803cf4e8fb3649b1a3d07763a33a41952febcac Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 03:07:01 +0000 Subject: [PATCH 06/13] docs --- README.md | 169 ++++++++++++++++++++++++++---------------------------- 1 file changed, 80 insertions(+), 89 deletions(-) diff --git a/README.md b/README.md index 935338cdbd2..94b70f8396b 100644 --- a/README.md +++ b/README.md @@ -33,12 +33,12 @@ npm install redis const redis = require("redis"); const client = redis.createClient(); -client.on("error", function(err) { - console.log("Error " + err); +client.on("error", function(error) { + console.error(error); }); -client.set("string key", "string val", redis.print); -client.get("string key", redis.print); +client.set("key", "value", redis.print); +client.get("key", redis.print); ``` Note that the API is entirely asynchronous. To get data back from the server, @@ -66,15 +66,17 @@ a variable number of individual arguments followed by an optional callback. Examples: ```js -client.hmset(["key", "test keys 1", "test val 1", "test keys 2", "test val 2"], function(err, res) { +client.hmset(["key", "foo", "bar"], function(err, res) { // ... }); + // Works the same as -client.hmset("key", ["test keys 1", "test val 1", "test keys 2", "test val 2"], function(err, res) { +client.hmset("key", ["foo", "bar"], function(err, res) { // ... }); + // Or -client.hmset("key", "test keys 1", "test val 1", "test keys 2", "test val 2", function(err, res) { +client.hmset("key", "foo", "bar", function(err, res) { // ... }); ``` @@ -84,15 +86,15 @@ Care should be taken with user input if arrays are possible (via body-parser, qu Note that in either form the `callback` is optional: ```js -client.set("some key", "some val"); -client.set(["some other key", "some val"]); +client.set("foo", "bar"); +client.set(["hello", "world"]); ``` If the key is missing, reply will be null. Only if the [Redis Command Reference](http://redis.io/commands) states something else it will not be null. ```js -client.get("missingkey", function(err, reply) { +client.get("missing_key", function(err, reply) { // reply is null when the key is missing console.log(reply); }); @@ -110,23 +112,23 @@ and Boolean values will result in the value coerced to a string! `client` will emit some events about the state of the connection to the Redis server. -#### "ready" +#### `"ready"` `client` will emit `ready` once a connection is established. Commands issued before the `ready` event are queued, then replayed just before this event is emitted. -#### "connect" +#### `"connect"` `client` will emit `connect` as soon as the stream is connected to the server. -#### "reconnecting" +#### `"reconnecting"` `client` will emit `reconnecting` when trying to reconnect to the Redis server after losing the connection. Listeners are passed an object containing `delay` (in ms from the previous try) and `attempt` (the attempt #) attributes. -#### "error" +#### `"error"` `client` will emit `error` when encountering an error connecting to the Redis server or when any other in node_redis occurs. If you use a command without @@ -135,11 +137,11 @@ listener. So please attach the error listener to node_redis. -#### "end" +#### `"end"` `client` will emit `end` when an established Redis server connection has closed. -#### "warning" +#### `"warning"` `client` will emit `warning` when password was set but none is needed and if a deprecated option / function / similar is used. @@ -390,184 +392,173 @@ syntax. **Example:** ```js -client.hmset("hosts", "mjr", "1", "another", "23", "home", "1234"); -client.hgetall("hosts", function(err, obj) { - console.dir(obj); +client.hmset("key", "foo", "bar", "hello", "world"); + +client.hgetall("hosts", function(err, value) { + console.log(value.foo); // > "bar" + console.log(value.hello); // > "world" }); ``` -#### client.hmset(hash, obj[, callback]) +#### client.hmset(hash, key1, val1, ...keyN, valN, [callback]) -Multiple values in a hash can be set by supplying an object. +Multiple values may also be set by supplying more arguments. **Example:** ```js -client.HMSET(key2, { - "0123456789": "abcdefghij", // NOTE: key and value will be coerced to strings - "some manner of key": "a type of value", -}); +// key +// 1) foo => bar +// 2) hello => world +client.HMSET("key", "foo", "bar", "hello", "world"); ``` -The properties and values of this Object will be set as keys and values in the -Redis hash. +### PubSub -#### client.hmset(hash, key1, val1, ...keyN, valN, [callback]) - -Multiple values may also be set by supplying more arguments. +#### Example -**Example:** +This example opens two client connections, subscribes to a channel on one of them, and publishes to that +channel on the other. ```js -client.HMSET(key1, "0123456789", "abcdefghij", "some manner of key", "a type of value"); -``` +const redis = require("redis"); -### Publish / Subscribe +const subscriber = redis.createClient(); +const publisher = redis.createClient(); -Example of the publish / subscribe API. This program opens two -client connections, subscribes to a channel on one of them, and publishes to that -channel on the other: +let messageCount = 0; -```js -var redis = require("redis"); -var sub = redis.createClient(), - pub = redis.createClient(); -var msg_count = 0; - -sub.on("subscribe", function(channel, count) { - pub.publish("a nice channel", "I am sending a message."); - pub.publish("a nice channel", "I am sending a second message."); - pub.publish("a nice channel", "I am sending my last message."); +subscriber.on("subscribe", function(channel, count) { + publisher.publish("a channel", "a message"); + publisher.publish("a channel", "another message"); }); -sub.on("message", function(channel, message) { - console.log("sub channel " + channel + ": " + message); - msg_count += 1; - if (msg_count === 3) { - sub.unsubscribe(); - sub.quit(); - pub.quit(); +subscriber.on("message", function(channel, message) { + messageCount += 1; + + console.log("Subscriber received message in channel '" + channel + "': " + message); + + if (messageCount === 2) { + subscriber.unsubscribe(); + subscriber.quit(); + publisher.quit(); } }); -sub.subscribe("a nice channel"); +subscriber.subscribe("a channel"); ``` When a client issues a `SUBSCRIBE` or `PSUBSCRIBE`, that connection is put into -a "subscriber" mode. At that point, the only valid commands are those that modify the subscription +a `"subscriber"` mode. At that point, the only valid commands are those that modify the subscription set, and quit (also ping on some redis versions). 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 with a new client (hint: use `client.duplicate()`). +open another connection with a new client (use `client.duplicate()` to quickly duplicate an existing client). -## Subscriber Events +#### Subscriber Events If a client has subscriptions active, it may emit these events: -### "message" (channel, message) +**"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 as `message`. -### "pmessage" (pattern, channel, 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 as `message`. -### "message_buffer" (channel, message) +**"message_buffer" (channel, message)**: This is the same as the `message` event with the exception, that it is always going to emit a buffer. If you listen to the `message` event at the same time as the `message_buffer`, it is always going to emit a string. -### "pmessage_buffer" (pattern, channel, message) +**"pmessage_buffer" (pattern, channel, message)**: This is the same as the `pmessage` event with the exception, that it is always going to emit a buffer. If you listen to the `pmessage` event at the same time as the `pmessage_buffer`, it is always going to emit a string. -### "subscribe" (channel, count) +**"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) +**"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) +**"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) +**"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]) +### 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()`. If any command fails to queue, all commands are rolled back and none is going to be executed (For -further information look at -[transactions](http://redis.io/topics/transactions)). +further information see the [Redis transactions](http://redis.io/topics/transactions) documentation). ```js -var redis = require("./index"), - client = redis.createClient(), - set_size = 20; +const redis = require("redis"); +const client = redis.createClient(); + +let setSize = 20; -client.sadd("bigset", "a member"); -client.sadd("bigset", "another member"); +client.sadd("key", "member1"); +client.sadd("key", "member2"); -while (set_size > 0) { - client.sadd("bigset", "member " + set_size); - set_size -= 1; +while (setSize > 0) { + client.sadd("key", "member" + setSize); + setSize -= 1; } -// multi chain with an individual callback +// chain commands 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); - }) + .scard("key") + .smembers("key") + .keys("*") .dbsize() .exec(function(err, replies) { console.log("MULTI got " + replies.length + " replies"); replies.forEach(function(reply, index) { - console.log("Reply " + index + ": " + reply.toString()); + console.log("REPLY @ index " + index + ": " + reply.toString()); }); }); ``` -### Multi.exec([callback]) +#### 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. -If your code contains an syntax error an EXECABORT error is going to be thrown +If your code contains an syntax error an `EXECABORT` error is going to be thrown and all commands are going to be aborted. That error contains a `.errors` property that contains the concrete errors. If all commands were queued successfully and an error is thrown by redis while processing the commands that error is going to be returned in the result array! -No other command is going to be aborted though than the onces failing. +No other command is going to be aborted though than the ones failing. 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 From 24824efdcc838d753f93bdec9572eca099b5d4ad Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 03:58:55 +0000 Subject: [PATCH 07/13] remove deprecated `new Buffer()` usage --- benchmarks/multi_bench.js | 6 +++--- index.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/benchmarks/multi_bench.js b/benchmarks/multi_bench.js index a79d92e83c9..86cf9329ce9 100644 --- a/benchmarks/multi_bench.js +++ b/benchmarks/multi_bench.js @@ -213,11 +213,11 @@ Test.prototype.print_stats = function () { }; small_str = '1234'; -small_buf = new Buffer(small_str); +small_buf = Buffer.from(small_str); large_str = (new Array(4096 + 1).join('-')); -large_buf = new Buffer(large_str); +large_buf = Buffer.from(large_str); very_large_str = (new Array((4 * 1024 * 1024) + 1).join('-')); -very_large_buf = new Buffer(very_large_str); +very_large_buf = Buffer.from(very_large_str); tests.push(new Test({descr: 'PING', command: 'ping', args: []})); tests.push(new Test({descr: 'PING', command: 'ping', args: [], batch: 50})); diff --git a/index.js b/index.js index bdbf46daf51..6cea8bd4c6c 100644 --- a/index.js +++ b/index.js @@ -829,7 +829,7 @@ RedisClient.prototype.internal_send_command = function (command_obj) { // 30000 seemed to be a good value to switch to buffers after testing and checking the pros and cons if (args[i].length > 30000) { big_data = true; - args_copy[i] = new Buffer(args[i], 'utf8'); + args_copy[i] = Buffer.from(args[i], 'utf8'); } else { args_copy[i] = args[i]; } From 30dd8cf8796d96c239d4324be42f689806685812 Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 03:59:02 +0000 Subject: [PATCH 08/13] docs --- README.md | 423 ++++++++++++++++++++++++++---------------------------- 1 file changed, 201 insertions(+), 222 deletions(-) diff --git a/README.md b/README.md index 94b70f8396b..236a214b5c1 100644 --- a/README.md +++ b/README.md @@ -308,6 +308,7 @@ const assert = require("assert"); const redis = require("redis"); const { AbortError, AggregateError, ReplyError } = require("redis"); + const client = redis.createClient(); client.on("error", function(err) { @@ -565,21 +566,22 @@ can queue individual commands while still sending regular client command as in this example: ```js -var redis = require("redis"), - client = redis.createClient(), - multi; +const redis = require("redis"); +const client = redis.createClient(); // start a separate multi command queue -multi = client.multi(); -multi.incr("incr thing", redis.print); -multi.incr("incr other thing", redis.print); +const multi = client.multi(); -// runs immediately -client.mset("incr thing", 100, "incr other thing", 1, redis.print); +// add some commands to the queue +multi.incr("count_cats", redis.print); +multi.incr("count_dogs", redis.print); -// drains multi queue and runs atomically +// runs a command immediately outside of the `multi` instance +client.mset("count_cats", 100, "count_dogs", 50, redis.print); + +// drains the multi queue and runs each command atomically multi.exec(function(err, replies) { - console.log(replies); // 101, 2 + console.log(replies); // 101, 51 }); ``` @@ -587,43 +589,26 @@ 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(); +const redis = require("redis"); + +const client = redis.createClient(); client .multi([ - ["mget", "multifoo", "multibar", redis.print], - ["incr", "multifoo"], - ["incr", "multibar"], + ["mget", "foo", "bar", redis.print], + ["incr", "hello"], ]) .exec(function(err, replies) { console.log(replies); }); ``` -### Multi.exec_atomic([callback]) +#### Multi.exec_atomic([callback]) Identical to Multi.exec but with the difference that executing a single command will not use transactions. -## client.batch([commands]) - -Identical to .multi without transactions. This is recommended if you want to -execute many commands at once but don't have to rely on transactions. - -`BATCH` 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 `Batch` object by calling `client.batch()`. The only difference -between .batch and .multi is that no transaction is going to be used. -Be aware that the errors are - just like in multi statements - in the result. -Otherwise both, errors and results could be returned at the same time. - -If you fire many commands at once this is going to boost the execution speed -significantly compared to firing the same commands in a loop without waiting for -the result! See the benchmarks for further comparison. Please remember that all -commands are kept in memory until they are fired. - -## Optimistic Locks +#### Optimistic Locks Using `multi` you can make sure your modifications run as a transaction, but you can't be sure you got there first. What if another client modified a key while @@ -633,39 +618,39 @@ To solve this, Redis supports the [WATCH](https://redis.io/topics/transactions) command, which is meant to be used with MULTI: ```js -var redis = require("redis"), - client = redis.createClient({ ... }); - -client.watch("foo", function( err ){ - if(err) throw err; - - client.get("foo", function(err, result) { - if(err) throw err; - - // Process result - // Heavy and time consuming operation here - - client.multi() - .set("foo", "some heavy computation") - .exec(function(err, results) { - - /** - * If err is null, it means Redis successfully attempted - * the operation. - */ - if(err) throw err; - - /** - * If results === null, it means that a concurrent client - * changed the key while we were processing it and thus - * the execution of the MULTI command was not performed. - * - * NOTICE: Failing an execution of MULTI is not considered - * an error. So you will have err === null and results === null - */ - - }); - }); +const redis = require("redis"); + +const client = redis.createClient(); + +client.watch("foo", function(watchError) { + if (watchError) throw watchError; + + client.get("foo", function(getError, result) { + if (getError) throw getError; + + // Process result + // Heavy and time consuming operation here to generate "bar" + + client + .multi() + .set("foo", "bar") + .exec(function(execError, results) { + /** + * If err is null, it means Redis successfully attempted + * the operation. + */ + if (execError) throw err; + + /** + * If results === null, it means that a concurrent client + * changed the key while we were processing it and thus + * the execution of the MULTI command was not performed. + * + * NOTICE: Failing an execution of MULTI is not considered + * an error. So you will have err === null and results === null + */ + }); + }); }); ``` @@ -681,14 +666,18 @@ client cannot connect to Redis. An example where we can see the execution of a `multi` command fail is as follows: ```js -let clients = {}; -clients.watcher = redis.createClient({ ... } ); -clients.alterer = clients.watcher.duplicate(); +const clients = { + watcher: redis.createClient(), + modifier: redis.createClient(), +}; -clients.watcher.watch('foo',function(err) { - if (err) { throw err; } - //if you comment out the next line, the transaction will work - clients.alterer.set('foo',Math.random(), (err) => {if (err) { throw err; }}); +clients.watcher.watch("foo", function(watchError) { + if (watchError) throw watchError; + + // if you comment out the next line, the transaction will work + clients.modifier.set("foo", Math.random(), setError => { + if (setError) throw err; + }); //using a setTimeout here to ensure that the MULTI/EXEC will come after the SET. //Normally, you would use a callback to ensure order, but I want the above SET command @@ -696,23 +685,25 @@ clients.watcher.watch('foo',function(err) { setTimeout(function() { clients.watcher .multi() - .set('foo','abc') - .set('bar','1234') - .exec((err,results) => { - if (err) { throw err; } + .set("foo", "bar") + .set("hello", "world") + .exec((multiExecError, results) => { + if (multiExecError) throw multiExecError; + if (results === null) { - console.log('transaction aborted because results were null'); + console.log("transaction aborted because results were null"); } else { - console.log('transaction worked and returned',results) + console.log("transaction worked and returned", results); } + clients.watcher.quit(); - clients.alterer.quit(); + clients.modifier.quit(); }); - },1000); + }, 1000); }); ``` -### WATCH limitations +#### `WATCH` limitations Redis WATCH works only on _whole_ key values. For example, with WATCH you can watch a hash for modifications, but you cannot watch a specific field of a hash. @@ -721,31 +712,49 @@ The following example would watch the keys `foo` and `hello`, not the field `hel of hash `foo`: ```js -var redis = require("redis"), - client = redis.createClient({ ... }); +const redis = require("redis"); -client.hget( "foo", "hello", function(err, result){ +const client = redis.createClient(); - //Do some processing with the value from this field and watch it after +client.hget("foo", "hello", function(hashGetError, result) { + if (hashGetError) throw hashGetError; - client.watch("foo", "hello", function( err ){ - if(err) throw err; + //Do some processing with the value from this field and watch it after - /** - * WRONG: This is now watching the keys 'foo' and 'hello'. It is not - * watching the field 'hello' of hash 'foo'. Because the key 'foo' - * refers to a hash, this command is now watching the entire hash - * for modifications. - */ - }); -} ) + client.watch("foo", "hello", function(watchError) { + if (watchError) throw watchError; + /** + * This is now watching the keys 'foo' and 'hello'. It is not + * watching the field 'hello' of hash 'foo'. Because the key 'foo' + * refers to a hash, this command is now watching the entire hash + * for modifications. + */ + }); +}); ``` -This limitation also applies to sets ( cannot watch individual set members ) +This limitation also applies to sets (you can not watch individual set members) and any other collections. -## Monitor mode +### client.batch([commands]) + +Identical to `.multi()` without transactions. This is recommended if you want to +execute many commands at once but don't need to rely on transactions. + +`BATCH` 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 `Batch` object by calling `client.batch()`. The only difference +between .batch and .multi is that no transaction is going to be used. +Be aware that the errors are - just like in multi statements - in the result. +Otherwise both, errors and results could be returned at the same time. + +If you fire many commands at once this is going to boost the execution speed +significantly compared to firing the same commands in a loop without waiting for +the result! See the benchmarks for further comparison. Please remember that all +commands are kept in memory until they are fired. + +### 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 @@ -756,25 +765,28 @@ connected to the server including the monitoring client itself. The callback for the `monitor` event takes a timestamp from the Redis server, an array of command arguments and the raw monitoring string. -Example: +#### Example: ```js -var client = require("redis").createClient(); +const redis = require("redis"); +const client = redis.createClient(); + client.monitor(function(err, res) { console.log("Entering monitoring mode."); }); + client.set("foo", "bar"); -client.on("monitor", function(time, args, raw_reply) { +client.on("monitor", function(time, args, rawReply) { console.log(time + ": " + args); // 1458910076.446514:['set', 'foo', 'bar'] }); ``` -# Extras +## Extras -Some other things you might like to know about. +Some other things you might find useful. -## client.server_info +### `client.server_info` After the ready probe completes, the results from the INFO command are saved in the `client.server_info` object. @@ -782,47 +794,45 @@ 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 ] +``` +> client.server_info.redis_version +'2.3.0' +> client.server_info.versions +[ 2, 3, 0 ] +``` -## redis.print() +### `redis.print()` A handy callback function for displaying return values when testing. Example: ```js -var redis = require("redis"), - client = redis.createClient(); +const redis = require("redis"); +const client = redis.createClient(); client.on("connect", function() { - client.set("foo_rand000000000000", "some fantastic value", redis.print); - client.get("foo_rand000000000000", redis.print); + client.set("foo", "bar", redis.print); // => "Reply: OK" + client.get("foo", redis.print); // => "Reply: bar" + client.quit(); }); ``` -This will print: - - Reply: OK - Reply: some fantastic value - -Note that this program will not exit cleanly because the client is still connected. - -## Multi-word commands +### Multi-word commands To execute redis multi-word commands like `SCRIPT LOAD` or `CLIENT LIST` pass the second word as first parameter: ```js client.script("load", "return 1"); + client .multi() .script("load", "return 1") .exec(); + client.multi([["script", "load", "return 1"]]).exec(); ``` -## client.duplicate([options][, callback]) +### `client.duplicate([options][, callback])` Duplicate all current options and return a new redisClient instance. All options passed to the duplicate function are going to replace the original option. If @@ -832,36 +842,13 @@ to return an error instead in the callback. One example of when to use duplicate() would be to accommodate the connection- blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands -are used on the same redisClient instance as non-blocking commands, the +are used on the same Redis client instance as non-blocking commands, the non-blocking ones may be queued up until after the blocking ones finish. -```js -var Redis = require("redis"); -var client = Redis.createClient(); -var clientBlocking = client.duplicate(); - -var get = function() { - console.log("get called"); - client.get("any_key", function() { - console.log("get returned"); - }); - setTimeout(get, 1000); -}; -var brpop = function() { - console.log("brpop called"); - clientBlocking.brpop("nonexistent", 5, function() { - console.log("brpop return"); - setTimeout(brpop, 1000); - }); -}; -get(); -brpop(); -``` - Another reason to use duplicate() is when multiple DBs on the same server are accessed via the redis SELECT command. Each DB could use its own connection. -## client.send_command(command_name[, [args][, callback]]) +### `client.send_command(command_name[, [args][, callback]])` All Redis commands have been added to the `client` object. However, if new commands are introduced before this library is updated or if you want to add @@ -871,23 +858,23 @@ Redis. All commands are sent as multi-bulk commands. `args` can either be an Array of arguments, or omitted / set to undefined. -## redis.add_command(command_name) +### `redis.add_command(command_name)` Calling add_command will add a new command to the prototype. The exact command name will be used when calling using this new command. Using arbitrary arguments is possible as with any other command. -## client.connected +### `client.connected` Boolean tracking the state of the connection to the Redis server. -## client.command_queue_length +### `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. -## client.offline_queue_length +### `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 @@ -897,31 +884,32 @@ commands. This applies to anything that uses an optional `[WITHSCORES]` or `[LIMIT offset count]` in the [redis.io/commands](http://redis.io/commands) documentation. -Example: +#### 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."); +const args = ["myzset", 1, "one", 2, "two", 3, "three", 99, "ninety-nine"]; + +client.zadd(args, function(addError, addResponse) { + if (addError) throw addError; + console.log("added " + addResponse + " 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 + const args1 = ["myzset", "+inf", "-inf"]; + client.zrevrangebyscore(args1, function(rangeError, rangeResponse) { + if (rangeError) throw rangeError; + console.log("response1", rangeResponse); + // ... }); - 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 + const max = 3; + const min = 1; + const offset = 1; + const count = 2; + const args2 = ["myzset", max, min, "WITHSCORES", "LIMIT", offset, count]; + client.zrevrangebyscore(args2, function(rangeError, rangeResponse) { + if (rangeError) throw rangeError; + console.log("response2", rangeResponse); + // ... }); }); ``` @@ -932,42 +920,42 @@ Much effort has been spent to make `node_redis` as fast as possible for common operations. ``` -Lenovo T450s, i7-5600U and 12gb memory -clients: 1, NodeJS: 6.2.0, Redis: 3.2.0, parser: javascript, connected by: tcp - PING, 1/1 avg/max: 0.02/ 5.26 2501ms total, 46916 ops/sec - PING, batch 50/1 avg/max: 0.06/ 4.35 2501ms total, 755178 ops/sec - SET 4B str, 1/1 avg/max: 0.02/ 4.75 2501ms total, 40856 ops/sec - SET 4B str, batch 50/1 avg/max: 0.11/ 1.51 2501ms total, 432727 ops/sec - SET 4B buf, 1/1 avg/max: 0.05/ 2.76 2501ms total, 20659 ops/sec - SET 4B buf, batch 50/1 avg/max: 0.25/ 1.76 2501ms total, 194962 ops/sec - GET 4B str, 1/1 avg/max: 0.02/ 1.55 2501ms total, 45156 ops/sec - GET 4B str, batch 50/1 avg/max: 0.09/ 3.15 2501ms total, 524110 ops/sec - GET 4B buf, 1/1 avg/max: 0.02/ 3.07 2501ms total, 44563 ops/sec - GET 4B buf, batch 50/1 avg/max: 0.10/ 3.18 2501ms total, 473171 ops/sec - SET 4KiB str, 1/1 avg/max: 0.03/ 1.54 2501ms total, 32627 ops/sec - SET 4KiB str, batch 50/1 avg/max: 0.34/ 1.89 2501ms total, 146861 ops/sec - SET 4KiB buf, 1/1 avg/max: 0.05/ 2.85 2501ms total, 20688 ops/sec - SET 4KiB buf, batch 50/1 avg/max: 0.36/ 1.83 2501ms total, 138165 ops/sec - GET 4KiB str, 1/1 avg/max: 0.02/ 1.37 2501ms total, 39389 ops/sec - GET 4KiB str, batch 50/1 avg/max: 0.24/ 1.81 2501ms total, 208157 ops/sec - GET 4KiB buf, 1/1 avg/max: 0.02/ 2.63 2501ms total, 39918 ops/sec - GET 4KiB buf, batch 50/1 avg/max: 0.31/ 8.56 2501ms total, 161575 ops/sec - INCR, 1/1 avg/max: 0.02/ 4.69 2501ms total, 45685 ops/sec - INCR, batch 50/1 avg/max: 0.09/ 3.06 2501ms total, 539964 ops/sec - LPUSH, 1/1 avg/max: 0.02/ 3.04 2501ms total, 41253 ops/sec - LPUSH, batch 50/1 avg/max: 0.12/ 1.94 2501ms total, 425090 ops/sec - LRANGE 10, 1/1 avg/max: 0.02/ 2.28 2501ms total, 39850 ops/sec - LRANGE 10, batch 50/1 avg/max: 0.25/ 1.85 2501ms total, 194302 ops/sec - LRANGE 100, 1/1 avg/max: 0.05/ 2.93 2501ms total, 21026 ops/sec - LRANGE 100, batch 50/1 avg/max: 1.52/ 2.89 2501ms total, 32767 ops/sec - SET 4MiB str, 1/1 avg/max: 5.16/ 15.55 2502ms total, 193 ops/sec - SET 4MiB str, batch 20/1 avg/max: 89.73/ 99.96 2513ms total, 223 ops/sec - SET 4MiB buf, 1/1 avg/max: 2.23/ 8.35 2501ms total, 446 ops/sec - SET 4MiB buf, batch 20/1 avg/max: 41.47/ 50.91 2530ms total, 482 ops/sec - GET 4MiB str, 1/1 avg/max: 2.79/ 10.91 2502ms total, 358 ops/sec - GET 4MiB str, batch 20/1 avg/max: 101.61/118.11 2541ms total, 197 ops/sec - GET 4MiB buf, 1/1 avg/max: 2.32/ 14.93 2502ms total, 430 ops/sec - GET 4MiB buf, batch 20/1 avg/max: 65.01/ 84.72 2536ms total, 308 ops/sec +Mac mini (2018), i7-3.2GHz and 32gb memory +clients: 1, NodeJS: 12.15.0, Redis: 5.0.6, parser: javascript, connected by: tcp + PING, 1/1 avg/max: 0.03/ 3.28 2501ms total, 31926 ops/sec + PING, batch 50/1 avg/max: 0.08/ 3.35 2501ms total, 599460 ops/sec + SET 4B str, 1/1 avg/max: 0.03/ 3.54 2501ms total, 29483 ops/sec + SET 4B str, batch 50/1 avg/max: 0.10/ 1.39 2501ms total, 477689 ops/sec + SET 4B buf, 1/1 avg/max: 0.04/ 1.52 2501ms total, 23449 ops/sec + SET 4B buf, batch 50/1 avg/max: 0.20/ 2.09 2501ms total, 244382 ops/sec + GET 4B str, 1/1 avg/max: 0.03/ 1.35 2501ms total, 32205 ops/sec + GET 4B str, batch 50/1 avg/max: 0.09/ 2.02 2501ms total, 568992 ops/sec + GET 4B buf, 1/1 avg/max: 0.03/ 2.93 2501ms total, 32802 ops/sec + GET 4B buf, batch 50/1 avg/max: 0.08/ 1.03 2501ms total, 592863 ops/sec + SET 4KiB str, 1/1 avg/max: 0.03/ 0.76 2501ms total, 29287 ops/sec + SET 4KiB str, batch 50/1 avg/max: 0.35/ 2.97 2501ms total, 143163 ops/sec + SET 4KiB buf, 1/1 avg/max: 0.04/ 1.21 2501ms total, 23070 ops/sec + SET 4KiB buf, batch 50/1 avg/max: 0.28/ 2.34 2501ms total, 176809 ops/sec + GET 4KiB str, 1/1 avg/max: 0.03/ 1.54 2501ms total, 29555 ops/sec + GET 4KiB str, batch 50/1 avg/max: 0.18/ 1.59 2501ms total, 279188 ops/sec + GET 4KiB buf, 1/1 avg/max: 0.03/ 1.80 2501ms total, 30681 ops/sec + GET 4KiB buf, batch 50/1 avg/max: 0.17/ 5.00 2501ms total, 285886 ops/sec + INCR, 1/1 avg/max: 0.03/ 1.99 2501ms total, 32757 ops/sec + INCR, batch 50/1 avg/max: 0.09/ 2.54 2501ms total, 538964 ops/sec + LPUSH, 1/1 avg/max: 0.05/ 4.85 2501ms total, 19482 ops/sec + LPUSH, batch 50/1 avg/max: 0.12/ 9.52 2501ms total, 395562 ops/sec + LRANGE 10, 1/1 avg/max: 0.06/ 9.21 2501ms total, 17062 ops/sec + LRANGE 10, batch 50/1 avg/max: 0.22/ 1.03 2501ms total, 228269 ops/sec + LRANGE 100, 1/1 avg/max: 0.05/ 1.44 2501ms total, 19051 ops/sec + LRANGE 100, batch 50/1 avg/max: 0.99/ 3.46 2501ms total, 50480 ops/sec + SET 4MiB str, 1/1 avg/max: 4.11/ 13.96 2501ms total, 243 ops/sec + SET 4MiB str, batch 20/1 avg/max: 91.16/145.01 2553ms total, 219 ops/sec + SET 4MiB buf, 1/1 avg/max: 2.81/ 11.90 2502ms total, 354 ops/sec + SET 4MiB buf, batch 20/1 avg/max: 36.21/ 70.96 2535ms total, 552 ops/sec + GET 4MiB str, 1/1 avg/max: 2.82/ 19.10 2503ms total, 354 ops/sec + GET 4MiB str, batch 20/1 avg/max: 128.57/207.86 2572ms total, 156 ops/sec + GET 4MiB buf, 1/1 avg/max: 3.13/ 23.88 2501ms total, 318 ops/sec + GET 4MiB buf, batch 20/1 avg/max: 65.91/ 87.59 2572ms total, 303 ops/sec ``` ## Debugging @@ -983,7 +971,8 @@ Good stack traces are only activated in development and debug mode as this results in a significant performance penalty. **_Comparison_**: -Useless stack trace: + +Standard stack trace: ``` ReplyError: ERR wrong number of arguments for 'set' command @@ -991,7 +980,7 @@ ReplyError: ERR wrong number of arguments for 'set' command at parseType (/home/ruben/repos/redis/node_modules/redis-parser/lib/parser.js:219:14) ``` -Good stack trace: +Debug stack trace: ``` ReplyError: ERR wrong number of arguments for 'set' command @@ -1009,20 +998,10 @@ ReplyError: ERR wrong number of arguments for 'set' command at processImmediate [as _immediateCallback] (timers.js:383:17) ``` -## How to Contribute - -- Open a pull request or an issue about what you want to implement / change. We're glad for any help! -- Please be aware that we'll only accept fully tested code. - -## Contributors - -The original author of node_redis is [Matthew Ranney](https://github.com/mranney) - -The current lead maintainer is [Ruben Bridgewater](https://github.com/BridgeAR) +## Contributing -Many [others](https://github.com/NodeRedis/node_redis/graphs/contributors) -contributed to `node_redis` too. Thanks to all of them! +Please see the [contributing guide](CONTRIBUTING.md). ## License -[MIT](LICENSE) +This repository is licensed under the "MIT" license. See [LICENSE](LICENSE). From d92665d931379f74e94e5c63ddf94d3a275b1d45 Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 04:48:48 +0000 Subject: [PATCH 09/13] docs --- .github/ISSUE_TEMPLATE.md | 39 ++++++++--- .github/PULL_REQUEST_TEMPLATE.md | 20 +++--- CONTRIBUTING.md | 107 ++++++++++++++++++++++++++++++- 3 files changed, 147 insertions(+), 19 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 42f2d3eeb61..c74eb12b803 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -1,15 +1,34 @@ -_Thanks for wanting to report an issue you've found in node_redis. Please delete +--- +title: ⚠️ Bug report +labels: needs-triage +--- + +### Issue + + + +> Describe your issue here + + +--- -* **Version**: What node_redis and what redis version is the issue happening on? -* **Platform**: What platform / version? (For example Node.js 0.10 or Node.js 5.7.0 on Windows 7 / Ubuntu 15.10 / Azure) -* **Description**: Description of your issue, stack traces from errors and code that reproduces the issue +### Environment -[gitter]: https://gitter.im/NodeRedis/node_redis?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge \ No newline at end of file + + - **Node.js Version**: `VERSION_HERE` + + + - **Redis Version**: `VERSION_HERE` + + + - **Platform**: `PLATFORM_HERE` diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 9706621c1b1..5c29bc26e23 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,14 +1,18 @@ -### Pull Request check-list +### Description -_Please make sure to review and check all of these items:_ + + + + + + +### Check List + + - [ ] Does `npm test` pass with this change (including linting)? - [ ] Is the new or changed code fully tested? - [ ] Is a documentation update included (if this change modifies existing APIs, or introduces new ones)? -_NOTE: these things are not required to open a PR and can be done -afterwards / while the PR is open._ - -### Description of change - -_Please provide a description of the change here._ \ No newline at end of file + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cf873b4c072..e82846fbee8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1 +1,106 @@ - +# Introduction + +First, thank you for considering contributing to Node Redis! It's people like you that make the open source community such a great community! 😊 + +We welcome any type of contribution, not just code. You can help with; + +- **QA**: file bug reports, the more details you can give the better (e.g. platform versions, screenshots sdk versions & logs) +- **Docs**: improve reference coverage, add more examples, fix typos or anything else you can spot. At the top of every page on our docs site you can click the `Edit` pencil to go to that pages markdown file, or view the [Docs Repo](https://github.com/invertase/react-native-firebase-docs) directly +- **Code**: take a look at the open issues. Even if you can't write code, commenting on them, showing that you care about a given issue matters. +- **Donations**: we welcome financial contributions in full transparency on our [open collective](https://opencollective.com/node-redis). + +--- + +## Project Guidelines + +As maintainers of this project, we want to ensure that the project lives and continues to grow. Not blocked by any +singular person's time. + +One of the simplest ways of doing this is by encouraging a larger set of shallow contributors. Through this we hope to +mitigate the problems of a project that needs updates but there is no-one who has the power to do so. + +### Continuous Deployment + + +Coming soon. + +### How can we help you get comfortable contributing? + +It is normal for a first pull request to be a potential fix for a problem but moving on from there to helping the +project's direction can be difficult. + +We try to help contributors cross that barrier by offering good first step issues (labelled `good-first-issue`). These +issues can be fixed without feeling like you are stepping on toes. Generally, these should be non-critical issues that +are well defined. They will be purposely avoided by mature contributors to the project, to make space for others. + +Additionally issues labelled `needs-triage` or `help-wanted` can also be picked up, these may not necessarily require +code changes but rather help with debugging and finding the cause of the issue whether it's a bug or a users incorrect +setup of the library or project. + +We aim to keep all project discussion inside GitHub issues. This is to make sure valuable discussion is accessible via +search. If you have questions about how to use the library, or how the project is running - GitHub issues are the goto +tool for this project. + +### Our expectations on you as a contributor + +You shouldn't feel bad for not contributing to open source. We want contributors like yourself to provide ideas, keep +the ship shipping and to take some of the load from others. It is non-obligatory; we’re here to get things done in an +enjoyable way. :trophy: + +We only ask that you follow the conduct guidelines set out in our [Code of Conduct](/CODE_OF_CONDUCT.md) throughout your +contribution journey. + +### What about if you have problems that cannot be discussed in public? + +You can reach out to us directly via email (`redis[AT]invertase.io`) or direct message us on +[Twitter](https://twitter.com/NodeRedis) if you'd like to discuss something privately. + +#### Project Maintainers + + - Mike Diarmid ([Salakar](https://github.com/Salakar)) @ [Invertase](https://invertase.io) + - Twitter: [@mikediarmid](https://twitter.com/mikediarmid) + - Elliot Hesp ([Ehesp](https://github.com/Ehesp)) @ [Invertase](https://invertase.io) + - Twitter: [@elliothesp](https://twitter.com/elliothesp) + - Ruben Bridgewater ([BridgeAR](https://github.com/BridgeAR)) + - Twitter: [@BridgeAR](https://twitter.com/BridgeAR) + +Huge thanks to the original author of Node Redis, [Matthew Ranney](https://github.com/mranney) and also to +[Ruben Bridgewater](https://github.com/BridgeAR) for handing over this project over to new maintainers so it could be +continuously maintained. + +--- + +## Code Guidelines + +### Your First Contribution + +Working on your first Pull Request? You can learn how from this _free_ series, +[How to Contribute to an Open Source Project on GitHub](https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github). + +### Testing Code + +Node Redis has a full test suite with coverage setup. + +To run the tests use the `npm test` command. To check detailed coverage locally run the `npm run coverage` command after +testing and open the generated `./coverage/index.html` in your browser. + +### Submitting code for review + +The bigger the pull request, the longer it will take to review and merge. Where possible try to break down large pull +requests into smaller chunks that are easier to review and merge. It is also always helpful to have some context for +your pull request. What was the purpose? Why does it matter to you? What problem are you trying to solve? Tag in any linked issues. + +To aid review we also ask that you fill out the pull request template as much as possible. + +> Use a `draft` pull request if your pull request is not complete or ready for review. + +### Code review process + +Pull Requests to the protected branches require two or more peer-review approvals and passing status checks to be able +to be merged. + +When reviewing a Pull Request please check the following steps on top of the existing automated checks: + +- Does the it provide or update the docs if docs changes are required? +- Have the tests been updated or new tests been added to test any newly implemented or changed functionality? +- Is the testing coverage ok and not worse than previously? From bf673baca70a156695ae8c860a64d06ffb65832c Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 04:50:48 +0000 Subject: [PATCH 10/13] docs --- CONTRIBUTING.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e82846fbee8..b7bc2c3b179 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -5,8 +5,8 @@ First, thank you for considering contributing to Node Redis! It's people like yo We welcome any type of contribution, not just code. You can help with; - **QA**: file bug reports, the more details you can give the better (e.g. platform versions, screenshots sdk versions & logs) -- **Docs**: improve reference coverage, add more examples, fix typos or anything else you can spot. At the top of every page on our docs site you can click the `Edit` pencil to go to that pages markdown file, or view the [Docs Repo](https://github.com/invertase/react-native-firebase-docs) directly -- **Code**: take a look at the open issues. Even if you can't write code, commenting on them, showing that you care about a given issue matters. +- **Docs**: improve reference coverage, add more examples, fix typos or anything else you can spot. +- **Code**: take a look at the open issues and help triage them. - **Donations**: we welcome financial contributions in full transparency on our [open collective](https://opencollective.com/node-redis). --- From f45850505d9ad31286c55b07edb0d31457b0be96 Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 04:54:12 +0000 Subject: [PATCH 11/13] docs --- .github/PULL_REQUEST_TEMPLATE.md | 7 ++++++- CONTRIBUTING.md | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 5c29bc26e23..98e3d312605 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,12 +1,17 @@ + + ### Description +> Description your pull request here + +--- -### Check List +### Checklist diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b7bc2c3b179..192219267a9 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -57,9 +57,9 @@ You can reach out to us directly via email (`redis[AT]invertase.io`) or direct m #### Project Maintainers - - Mike Diarmid ([Salakar](https://github.com/Salakar)) @ [Invertase](https://invertase.io) + - Mike Diarmid ([Salakar](https://github.com/Salakar)) @ [Invertase](https://github.com/invertase) - Twitter: [@mikediarmid](https://twitter.com/mikediarmid) - - Elliot Hesp ([Ehesp](https://github.com/Ehesp)) @ [Invertase](https://invertase.io) + - Elliot Hesp ([Ehesp](https://github.com/Ehesp)) @ [Invertase](https://github.com/invertase) - Twitter: [@elliothesp](https://twitter.com/elliothesp) - Ruben Bridgewater ([BridgeAR](https://github.com/BridgeAR)) - Twitter: [@BridgeAR](https://twitter.com/BridgeAR) From caa5ac70982d3cf6747cc74854f07c2f8eb288e2 Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 04:55:18 +0000 Subject: [PATCH 12/13] docs & npm ignore --- .npmignore | 1 + CONTRIBUTING.md | 51 +++++++++++++++++++++++++------------------------ README.md | 2 +- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/.npmignore b/.npmignore index 497486e4e0d..6e559f54aa4 100644 --- a/.npmignore +++ b/.npmignore @@ -17,3 +17,4 @@ CODE_OF_CONDUCT.md .travis.yml appveyor.yml package-lock.json +.prettierrc diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 192219267a9..f9cba8b4a61 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,38 +13,39 @@ We welcome any type of contribution, not just code. You can help with; ## Project Guidelines -As maintainers of this project, we want to ensure that the project lives and continues to grow. Not blocked by any +As maintainers of this project, we want to ensure that the project lives and continues to grow. Not blocked by any singular person's time. -One of the simplest ways of doing this is by encouraging a larger set of shallow contributors. Through this we hope to +One of the simplest ways of doing this is by encouraging a larger set of shallow contributors. Through this we hope to mitigate the problems of a project that needs updates but there is no-one who has the power to do so. ### Continuous Deployment + Coming soon. ### How can we help you get comfortable contributing? -It is normal for a first pull request to be a potential fix for a problem but moving on from there to helping the +It is normal for a first pull request to be a potential fix for a problem but moving on from there to helping the project's direction can be difficult. -We try to help contributors cross that barrier by offering good first step issues (labelled `good-first-issue`). These -issues can be fixed without feeling like you are stepping on toes. Generally, these should be non-critical issues that +We try to help contributors cross that barrier by offering good first step issues (labelled `good-first-issue`). These +issues can be fixed without feeling like you are stepping on toes. Generally, these should be non-critical issues that are well defined. They will be purposely avoided by mature contributors to the project, to make space for others. -Additionally issues labelled `needs-triage` or `help-wanted` can also be picked up, these may not necessarily require -code changes but rather help with debugging and finding the cause of the issue whether it's a bug or a users incorrect +Additionally issues labelled `needs-triage` or `help-wanted` can also be picked up, these may not necessarily require +code changes but rather help with debugging and finding the cause of the issue whether it's a bug or a users incorrect setup of the library or project. -We aim to keep all project discussion inside GitHub issues. This is to make sure valuable discussion is accessible via -search. If you have questions about how to use the library, or how the project is running - GitHub issues are the goto +We aim to keep all project discussion inside GitHub issues. This is to make sure valuable discussion is accessible via +search. If you have questions about how to use the library, or how the project is running - GitHub issues are the goto tool for this project. ### Our expectations on you as a contributor -You shouldn't feel bad for not contributing to open source. We want contributors like yourself to provide ideas, keep -the ship shipping and to take some of the load from others. It is non-obligatory; we’re here to get things done in an +You shouldn't feel bad for not contributing to open source. We want contributors like yourself to provide ideas, keep +the ship shipping and to take some of the load from others. It is non-obligatory; we’re here to get things done in an enjoyable way. :trophy: We only ask that you follow the conduct guidelines set out in our [Code of Conduct](/CODE_OF_CONDUCT.md) throughout your @@ -52,20 +53,20 @@ contribution journey. ### What about if you have problems that cannot be discussed in public? -You can reach out to us directly via email (`redis[AT]invertase.io`) or direct message us on +You can reach out to us directly via email (`redis[AT]invertase.io`) or direct message us on [Twitter](https://twitter.com/NodeRedis) if you'd like to discuss something privately. #### Project Maintainers - - Mike Diarmid ([Salakar](https://github.com/Salakar)) @ [Invertase](https://github.com/invertase) - - Twitter: [@mikediarmid](https://twitter.com/mikediarmid) - - Elliot Hesp ([Ehesp](https://github.com/Ehesp)) @ [Invertase](https://github.com/invertase) - - Twitter: [@elliothesp](https://twitter.com/elliothesp) - - Ruben Bridgewater ([BridgeAR](https://github.com/BridgeAR)) - - Twitter: [@BridgeAR](https://twitter.com/BridgeAR) - -Huge thanks to the original author of Node Redis, [Matthew Ranney](https://github.com/mranney) and also to -[Ruben Bridgewater](https://github.com/BridgeAR) for handing over this project over to new maintainers so it could be +- Mike Diarmid ([Salakar](https://github.com/Salakar)) @ [Invertase](https://github.com/invertase) + - Twitter: [@mikediarmid](https://twitter.com/mikediarmid) +- Elliot Hesp ([Ehesp](https://github.com/Ehesp)) @ [Invertase](https://github.com/invertase) + - Twitter: [@elliothesp](https://twitter.com/elliothesp) +- Ruben Bridgewater ([BridgeAR](https://github.com/BridgeAR)) + - Twitter: [@BridgeAR](https://twitter.com/BridgeAR) + +Huge thanks to the original author of Node Redis, [Matthew Ranney](https://github.com/mranney) and also to +[Ruben Bridgewater](https://github.com/BridgeAR) for handing over this project over to new maintainers so it could be continuously maintained. --- @@ -74,7 +75,7 @@ continuously maintained. ### Your First Contribution -Working on your first Pull Request? You can learn how from this _free_ series, +Working on your first Pull Request? You can learn how from this _free_ series, [How to Contribute to an Open Source Project on GitHub](https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github). ### Testing Code @@ -86,8 +87,8 @@ testing and open the generated `./coverage/index.html` in your browser. ### Submitting code for review -The bigger the pull request, the longer it will take to review and merge. Where possible try to break down large pull -requests into smaller chunks that are easier to review and merge. It is also always helpful to have some context for +The bigger the pull request, the longer it will take to review and merge. Where possible try to break down large pull +requests into smaller chunks that are easier to review and merge. It is also always helpful to have some context for your pull request. What was the purpose? Why does it matter to you? What problem are you trying to solve? Tag in any linked issues. To aid review we also ask that you fill out the pull request template as much as possible. @@ -96,7 +97,7 @@ To aid review we also ask that you fill out the pull request template as much as ### Code review process -Pull Requests to the protected branches require two or more peer-review approvals and passing status checks to be able +Pull Requests to the protected branches require two or more peer-review approvals and passing status checks to be able to be merged. When reviewing a Pull Request please check the following steps on top of the existing automated checks: diff --git a/README.md b/README.md index 236a214b5c1..abc9afdbeeb 100644 --- a/README.md +++ b/README.md @@ -971,7 +971,7 @@ Good stack traces are only activated in development and debug mode as this results in a significant performance penalty. **_Comparison_**: - + Standard stack trace: ``` From 5b94673fb691f48076af5c76b71bf038a48ba3cf Mon Sep 17 00:00:00 2001 From: Salakar Date: Sun, 9 Feb 2020 04:59:59 +0000 Subject: [PATCH 13/13] docs --- README.md | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index abc9afdbeeb..847dd8bd20c 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@

NPM downloads NPM version - Build Status + Build Status Windows Tests - Coverage Status + Coverage Status Follow on Twitter

@@ -131,11 +131,11 @@ after losing the connection. Listeners are passed an object containing `delay` #### `"error"` `client` will emit `error` when encountering an error connecting to the Redis -server or when any other in node_redis occurs. If you use a command without +server or when any other in Node Redis occurs. If you use a command without callback and encounter a ReplyError it is going to be emitted to the error listener. -So please attach the error listener to node_redis. +So please attach the error listener to Node Redis. #### `"end"` @@ -171,15 +171,15 @@ using unix sockets if possible to increase throughput. | port | 6379 | Port of the Redis server | | path | null | The UNIX socket string of the Redis server | | url | null | The URL of the Redis server. Format: `[redis[s]:]//[[user][:password@]][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]]` (More info avaliable at [IANA](http://www.iana.org/assignments/uri-schemes/prov/redis)). | -| string_numbers | null | Set to `true`, `node_redis` will return Redis number values as Strings instead of javascript Numbers. Useful if you need to handle big numbers (above `Number.MAX_SAFE_INTEGER === 2^53`). Hiredis is incapable of this behavior, so setting this option to `true` will result in the built-in javascript parser being used no matter the value of the `parser` option. | +| string_numbers | null | Set to `true`, Node Redis will return Redis number values as Strings instead of javascript Numbers. Useful if you need to handle big numbers (above `Number.MAX_SAFE_INTEGER === 2^53`). Hiredis is incapable of this behavior, so setting this option to `true` will result in the built-in javascript parser being used no matter the value of the `parser` option. | | return_buffers | false | If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings. | | detect_buffers | false | If set to `true`, then replies will be sent to callbacks as Buffers. This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to every command on a client. **Note**: This doesn't work properly with the pubsub mode. A subscriber has to either always return Strings or Buffers. | | socket_keepalive | true | If set to `true`, the keep-alive functionality is enabled on the underlying socket. | | socket_initial_delay | 0 | Initial Delay in milliseconds, and this will also behave the interval keep alive message sending to Redis. | -| no_ready_check | false | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will 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. | +| no_ready_check | false | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will 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 | 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 executed immediately with an error, or an error will be emitted if no callback is specified. | | retry_unfulfilled_commands | false | If set to `true`, all commands that were unfulfilled while the connection is lost will be retried after the connection has been reestablished. Use this with caution if you use state altering commands (e.g. `incr`). This is especially useful if you use blocking commands. | -| password | null | If set, client will run Redis auth command on connect. Alias `auth_pass` **Note** `node_redis` < 2.5 must use `auth_pass` | +| password | null | If set, client will run Redis auth command on connect. Alias `auth_pass` **Note** Node Redis < 2.5 must use `auth_pass` | | db | null | If set, client will run Redis `select` command on connect. | | family | IPv4 | You can force using IPv6 if you set the family to 'IPv6'. See Node.js [net](https://nodejs.org/api/net.html) or [dns](https://nodejs.org/api/dns.html) modules on how to use the family type. | | disable_resubscribing | false | If set to `true`, a client won't resubscribe after disconnecting. | @@ -342,14 +342,14 @@ client.set("foo", "bar", "baz", function(err, res) { Every `ReplyError` contains the `command` name in all-caps and the arguments (`args`). -If node_redis emits a library error because of another error, the triggering +If Node Redis emits a library error because of another error, the triggering error is added to the returned error as `origin` attribute. **_Error codes_** -node_redis returns a `NR_CLOSED` error code if the clients connection dropped. +Node Redis returns a `NR_CLOSED` error code if the clients connection dropped. If a command unresolved command got rejected a `UNCERTAIN_STATE` code is -returned. A `CONNECTION_BROKEN` error code is used in case node_redis gives up +returned. A `CONNECTION_BROKEN` error code is used in case Node Redis gives up to reconnect. ### client.unref() @@ -386,9 +386,8 @@ dealing with hash values, there are a couple of useful exceptions to this. #### client.hgetall(hash, callback) -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. +The reply from an `HGETALL` command will be converted into a JavaScript Object. That way you can interact with the +responses using JavaScript syntax. **Example:** @@ -514,7 +513,7 @@ 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 +are run atomically by Redis. The interface returns an individual `Multi` object by calling `client.multi()`. If any command fails to queue, all commands are rolled back and none is going to be executed (For further information see the [Redis transactions](http://redis.io/topics/transactions) documentation). @@ -743,7 +742,7 @@ Identical to `.multi()` without transactions. This is recommended if you want to execute many commands at once but don't need to rely on transactions. `BATCH` 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 +are run atomically by Redis. The interface returns an individual `Batch` object by calling `client.batch()`. The only difference between .batch and .multi is that no transaction is going to be used. Be aware that the errors are - just like in multi statements - in the result. @@ -916,8 +915,7 @@ client.zadd(args, function(addError, addResponse) { ## Performance -Much effort has been spent to make `node_redis` as fast as possible for common -operations. +Much effort has been spent to make Node Redis as fast as possible for common operations. ``` Mac mini (2018), i7-3.2GHz and 32gb memory @@ -960,7 +958,7 @@ clients: 1, NodeJS: 12.15.0, Redis: 5.0.6, parser: javascript, connected by: tcp ## Debugging -To get debug output run your `node_redis` application with `NODE_DEBUG=redis`. +To get debug output run your Node Redis application with `NODE_DEBUG=redis`. This is also going to result in good stack traces opposed to useless ones otherwise for any async operation.