diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 00000000000..34bfad29479 --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,9 @@ +version = 1 +exclude_patterns = ["examples/**"] + +[[analyzers]] +name = "javascript" +enabled = true + + [analyzers.meta] + environment = ["nodejs"] diff --git a/.eslintrc b/.eslintrc index 75c73341b06..81e5e9b9cfd 100644 --- a/.eslintrc +++ b/.eslintrc @@ -102,6 +102,7 @@ rules: globals: it: true describe: true + xdescribe: true before: true after: true beforeEach: true 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/.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..98e3d312605 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -1,14 +1,23 @@ -### Pull Request check-list + -_Please make sure to review and check all of these items:_ +### Description + + + + + +> Description your pull request here + + +--- + +### Checklist + + - [ ] 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/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 00000000000..3ec398bb627 --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,38 @@ +name: Benchmarking + +on: [pull_request] + +jobs: + benchmark: + name: Benchmark + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + node-version: [10.x, 12.x, 14.x, 15.x] + redis-version: [5.x, 6.x] + + steps: + - uses: actions/checkout@v2.3.4 + with: + fetch-depth: 1 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2.1.5 + with: + node-version: ${{ matrix.node-version }} + + - name: Setup Redis + uses: shogo82148/actions-setup-redis@v1.9.7 + with: + redis-version: ${{ matrix.redis-version }} + auto-start: "true" + + - run: npm i --no-audit --prefer-offline + - name: Run Benchmark + run: npm run benchmark > benchmark-output.txt && cat benchmark-output.txt + - name: Upload Benchmark Result + uses: actions/upload-artifact@v2.2.2 + with: + name: benchmark-output.txt + path: benchmark-output.txt diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 00000000000..98c615d0c55 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,67 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ master ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ master ] + schedule: + - cron: '35 0 * * 4' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + language: [ 'javascript' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] + # Learn more: + # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + # ℹ️ Command-line programs to run using the OS shell. + # 📚 https://git.io/JvXDl + + # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml new file mode 100644 index 00000000000..d110707ee09 --- /dev/null +++ b/.github/workflows/linting.yml @@ -0,0 +1,31 @@ +name: Linting + +on: [pull_request] + +jobs: + eslint: + name: ESLint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2.3.4 + with: + fetch-depth: 1 + - uses: actions/setup-node@v2.1.5 + with: + node-version: 12 + - run: npm i --no-audit --prefer-offline + - name: Test Code Linting + run: npm run lint + - name: Save Code Linting Report JSON + run: npm run lint:report + continue-on-error: true + - name: Annotate Code Linting Results + uses: ataylorme/eslint-annotate-action@1.1.2 + with: + repo-token: "${{ secrets.GITHUB_TOKEN }}" + report-json: "eslint-report.json" + - name: Upload ESLint report + uses: actions/upload-artifact@v2.2.2 + with: + name: eslint-report.json + path: eslint-report.json diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 00000000000..06b0e57ec3e --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,52 @@ +name: Tests + +on: [push] + +jobs: + testing: + name: Test + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + node-version: [10.x, 12.x, 14.x, 15.x] + redis-version: [4.x, 5.x, 6.x] + + steps: + - uses: actions/checkout@v2.3.4 + with: + fetch-depth: 1 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2.1.5 + with: + node-version: ${{ matrix.node-version }} + + - name: Setup Redis + uses: shogo82148/actions-setup-redis@v1.9.7 + with: + redis-version: ${{ matrix.redis-version }} + auto-start: "false" + + - name: Disable IPv6 + run: sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'; + + - name: Setup Stunnel + run: sudo apt-get install stunnel4 + + - name: Install Packages + run: npm i --no-audit --prefer-offline + + - name: Run Tests + run: npm test + + - name: Submit Coverage + run: npm run coveralls + env: + COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_TOKEN }} + + - name: Upload Coverage Report + uses: actions/upload-artifact@v2.2.2 + with: + name: coverage + path: coverage diff --git a/.github/workflows/tests_windows.yml b/.github/workflows/tests_windows.yml new file mode 100644 index 00000000000..7a2e00a9c93 --- /dev/null +++ b/.github/workflows/tests_windows.yml @@ -0,0 +1,49 @@ +name: Tests Windows + +on: [push] + +jobs: + testing-windows: + name: Test Windows + runs-on: windows-latest + strategy: + fail-fast: false + matrix: + node-version: [10.x, 12.x, 14.x, 15.x] + steps: + - uses: actions/checkout@v2.3.4 + with: + fetch-depth: 1 + + - name: Install Redis + uses: crazy-max/ghaction-chocolatey@v1.4.0 + with: + args: install redis-64 --version=3.0.503 --no-progress + + - name: Start Redis + run: | + redis-server --service-install + redis-server --service-start + redis-cli config set stop-writes-on-bgsave-error no + + - name: Setup Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2.1.5 + with: + node-version: ${{ matrix.node-version }} + + - name: Install Packages + run: npm i --no-audit --prefer-offline + + - name: Run Tests + run: npm test + + - name: Submit Coverage + run: npm run coveralls + env: + COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_TOKEN }} + + - name: Upload Coverage Report + uses: actions/upload-artifact@v2.2.2 + with: + name: coverage + path: coverage diff --git a/.gitignore b/.gitignore index 2290a8ec51d..64b4143dc6f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,11 @@ coverage stunnel.conf stunnel.pid *.out +package-lock.json + +# IntelliJ IDEs +.idea +# VisualStudioCode IDEs +.vscode +.vs +eslint-report.json diff --git a/.npmignore b/.npmignore index b0238e058cb..f1cf466f08d 100644 --- a/.npmignore +++ b/.npmignore @@ -3,8 +3,20 @@ benchmarks/ test/ .nyc_output/ coverage/ +.github/ +.eslintignore +.eslintrc .tern-port *.log *.rdb *.out *.yml +.vscode +.idea +CONTRIBUTING.md +CODE_OF_CONDUCT.md +.travis.yml +appveyor.yml +package-lock.json +.prettierrc +eslint-report.json diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000000..1ca516a86c0 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,11 @@ +{ + "arrowParens": "avoid", + "trailingComma": "all", + "useTabs": false, + "semi": true, + "singleQuote": false, + "bracketSpacing": true, + "jsxBracketSameLine": false, + "tabWidth": 2, + "printWidth": 100 +} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index eefd2354065..00000000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: node_js -sudo: false -env: - - CXX=g++-4.8 TRAVIS=true -addons: - apt: - sources: - - ubuntu-toolchain-r-test - packages: - - g++-4.8 -node_js: - - "0.10" - - "0.12" - - "4" - - "6" - - "7" -after_success: npm run coveralls diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000000..186da332a45 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,880 @@ +# Changelog + +## 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 +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) +- Add support for `rediss` protocol in url (#1282) + +## v2.8.0 - 31 Jul, 2017 + +Features + +- Accept UPPER_CASE commands in send_command +- Add arbitrary commands to the prototype by using `Redis.addCommand(name)` + +Bugfixes + +- Fixed not always copying subscribe unsubscribe arguments +- Fixed emitting internal errors while reconnecting with auth +- Fixed crashing with invalid url option + +## v2.7.1 - 14 Mar, 2017 + +Bugfixes + +- Fixed monitor mode not working in combination with IPv6 (2.6.0 regression) + +## v2.7.0 - 11 Mar, 2017 + +Features + +- All returned errors are from now a subclass of `RedisError`. + +Bugfixes + +- Fixed rename_commands not accepting `null` as value +- Fixed `AbortError`s and `AggregateError`s not showing the error message in the stack trace + +## 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 + +## v2.6.4 - 12 Jan, 2017 + +Bugfixes + +- Fixed monitor mode not working in combination with IPv6, sockets or lua scripts (2.6.0 regression) + +## 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) + +## v2.6.2 - 16 Jun, 2016 + +Bugfixes + +- Fixed individual callbacks of a transaction not being called (2.6.0 regression) + +## v2.6.1 - 02 Jun, 2016 + +Bugfixes + +- Fixed invalid function name being exported + +## 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 + +Bugfixes + +- 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 + +## 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)) + +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 + +## 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 + +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 + +## 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 + +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 + +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. + +## v2.5.3 - 21 Mar, 2016 + +Bugfixes + +- Revert throwing on invalid data types and print a warning instead + +## v2.5.2 - 16 Mar, 2016 + +Bugfixes + +- Fixed breaking changes against Redis 2.4 introduced in 2.5.0 / 2.5.1 + +## v2.5.1 - 15 Mar, 2016 + +Bugfixes + +- Fixed info command not working anymore with optional section argument + +## v2.5.0 - 15 Mar, 2016 + +Same changelog as the pre-release + +## 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. + +It took way to long to release this one and the next release cycles will be shorter again. + +This release is also going to deprecate a couple things to prepare for a future v.3 (it'll still take a while to v.3). + +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 + +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 + +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) + +## v2.4.2 - 27 Nov, 2015 + +Bugfixes + +- Fixed not emitting ready after reconnect with disable_resubscribing ([@maxgalbu](https://github.com/maxgalbu)) + +## v2.4.1 - 25 Nov, 2015 + +Bugfixes + +- Fixed a js parser regression introduced in 2.4.0 ([@BridgeAR](https://github.com/BridgeAR)) + +## 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)) + +Bugfixes + +- Fixed js parser handling big values slow ([@BridgeAR](https://github.com/BridgeAR)) +- The speed is now on par with the hiredis parser. + +## 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. + +## 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)) + +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)) + +## 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)) + +## 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)) + +## 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)) + +## 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)) + +## v2.2.1 - 12 Oct, 2015 + +No code change + +## 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)) + +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)) + +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. + +Please consider using .batch instead of looping through a lot of commands one by one. This will significantly improve your performance. + +Here are some stats compared to ioredis 1.9.1 (Lenovo T450s i7-5600U): + + simple set + 82,496 op/s » ioredis + 112,617 op/s » node_redis + + simple get + 82,015 op/s » ioredis + 105,701 op/s » node_redis + + simple get with pipeline + 10,233 op/s » ioredis + 26,541 op/s » node_redis (using .batch) + + lrange 100 + 7,321 op/s » ioredis + 26,155 op/s » node_redis + + publish + 90,524 op/s » ioredis + 112,823 op/s » node_redis + + subscribe + 43,783 op/s » ioredis + 61,889 op/s » node_redis + +To conclude: we can proudly say that node_redis is very likely outperforming any other node redis client. + +Known issues + +- 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) + +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) + +## v2.0.1 - Sep 24, 2015 + +Bugfixes: + +- Fix argument mutation while using the array notation in combination with keys / callbacks ([#866](.)). (@BridgeAR) + +## v2.0.0 - Sep 21, 2015 + +This is the biggest release that node_redis had since it was released in 2010. A long list of outstanding bugs has been fixed, so we are very happy to present you redis 2.0 and we highly recommend updating as soon as possible. + +# What's new in 2.0 + +- Implemented a "connection is broken" mode if no connection could be established +- node_redis no longer throws under any circumstances, preventing it from terminating applications. +- Multi error handling is now working properly +- Consistent command behavior including multi +- Windows support +- Improved performance +- A lot of code cleanup +- Many bug fixes +- Better user support! + +## Features: + +- Added a "redis connection is broken" mode after reaching max connection attempts / exceeding connection timeout. (@BridgeAR) +- Added NODE_DEBUG=redis env to activate the debug_mode (@BridgeAR) +- Added a default connection timeout of 24h instead of never timing out as a default (@BridgeAR) +- 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 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) +- Full windows support (@bcoe) +- Increased coverage by 10% and add a lot of tests to make sure everything works as it should. We now reached 97% :-) (@BridgeAR) +- 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) + +## Bug fixes: + +- Don't kill the app anymore by randomly throwing errors sync instead of emitting them (@BridgeAR) +- Don't catch user errors anymore occuring in callbacks (no try callback anymore & more fixes for the parser) (@BridgeAR) +- Early garbage collection of queued items (@dohse) +- Fix js parser returning errors as strings (@BridgeAR) +- Do not wrap errors into other errors (@BridgeAR) +- Authentication failures are now returned in the callback instead of being emitted (@BridgeAR) +- Fix a memory leak on reconnect (@rahar) +- Using `send_command` directly may now also be called without the args as stated in the [README.md](./README.md) (@BridgeAR) +- Fix the multi.exec error handling (@BridgeAR) +- Fix commands being inconsistent and behaving wrong (@BridgeAR) +- Channel names with spaces are now properly resubscribed after a reconnection (@pbihler) +- Do not try to reconnect after the connection timeout has been exceeded (@BridgeAR) +- Ensure the execution order is observed if using .eval (@BridgeAR) +- Fix commands not being rejected after calling .quit (@BridgeAR) +- Fix .auth calling the callback twice if already connected (@BridgeAR) +- Fix detect_buffers not working in pub sub mode and while monitoring (@BridgeAR) +- Fix channel names always being strings instead of buffers while return_buffers is true (@BridgeAR) +- Don't print any debug statements if not asked for (@BridgeAR) +- Fix a couple small other bugs + +## Breaking changes: + +1. redis.send_command commands have to be lower case from now on. This does only apply if you use `.send_command` directly instead of the convenient methods like `redis.command`. +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. + +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. +8. The keyspace of `redis.server_info` (db0...) is from now on an object instead of an string. + +NodeRedis also thanks @qdb, @tobek, @cvibhagool, @frewsxcv, @davidbanham, @serv, @vitaliylag, @chrishamant, @GamingCoder and all other contributors that I may have missed for their contributions! + +From now on we'll push new releases more frequently out and fix further long outstanding things and implement new features. + +
+ +## 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) + +## v0.12.1 - Aug 10, 2014 + +- 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) + +## v0.11.0 - Jul 10, 2014 + +- IPv6 Support. (Yann Stephan) +- Revert error emitting and go back to throwing errors. (Bryce Baril) +- Set socket_keepalive to prevent long-lived client timeouts. (mohit) +- Correctly reset retry timer. (ouotuo) +- Domains protection from bad user exit. (Jake Verbaten) +- Fix reconnection socket logic to prevent misqueued entries. (Iain Proctor) + +## v0.10.3 - May 22, 2014 + +- Update command list to match Redis 2.8.9 (Charles Feng) + +## v0.10.2 - May 18, 2014 + +- Better 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) + +## v0.10.0 - December 21, 2013 + +- Instead of throwing errors asynchronously, emit errors on client. (Bryce Baril) + +## v0.9.2 - December 15, 2013 + +- Regenerate commands for new 2.8.x Redis commands. (Marek Ventur) +- Correctly time reconnect counts when using 'auth'. (William Hockey) + +## v0.9.1 - November 23, 2013 + +- Allow hmset to accept numeric keys. (Alex Stokes) +- Fix TypeError for multiple MULTI/EXEC errors. (Kwangsu Kim) + +## v0.9.0 - October 17, 2013 + +- Domains support. (Forrest L Norvell) + +## v0.8.6 - October 2, 2013 + +- If error is already an Error, don't wrap it in another Error. (Mathieu M-Gosselin) +- Fix retry delay logic (Ian Babrou) +- Return Errors instead of strings where Errors are expected (Ian Babrou) +- Add experimental `.unref()` method to RedisClient (Bryce Baril / Olivier Lalonde) +- Strengthen checking of reply to prevent conflating "message" or "pmessage" fields with pub_sub replies. (Bryce Baril) + +## v0.8.5 - September 26, 2013 + +- Add `auth_pass` option to connect and immediately authenticate (Henrik Peinar) + +## v0.8.4 - June 24, 2013 + +Many contributed features and fixes, including: + +- Ignore password set if not needed. (jbergknoff) +- Improved compatibility with 0.10.X for tests and client.end() (Bryce Baril) +- Protect connection retries from application exceptions. (Amos Barreto) +- Better exception handling for Multi/Exec (Thanasis Polychronakis) +- Renamed pubsub mode to subscriber mode (Luke Plaster) +- Treat SREM like SADD when passed an array (Martin Ciparelli) +- Fix empty unsub/punsub TypeError (Jeff Barczewski) +- Only attempt to run a callback if it one was provided (jifeng) + +## v0.8.3 - April 09, 2013 + +Many contributed features and fixes, including: + +- Fix some tests for Node.js version 0.9.x+ changes (Roman Ivanilov) +- Fix error when commands submitted after idle event handler (roamm) +- Bypass Redis for no-op SET/SETEX commands (jifeng) +- Fix HMGET + detect_buffers (Joffrey F) +- Fix CLIENT LOAD functionality (Jonas Dohse) +- Add percentage outputs to diff_multi_bench_output.js (Bryce Baril) +- Add retry_max_delay option (Tomasz Durka) +- Fix parser off-by-one errors with nested multi-bulk replies (Bryce Baril) +- Prevent parser from sinking application-side exceptions (Bryce Baril) +- Fix parser incorrect buffer skip when parsing multi-bulk errors (Bryce Baril) +- Reverted previous change with throwing on non-string values with HMSET (David Trejo) +- Fix command queue sync issue when using pubsub (Tom Leach) +- Fix compatibility with two-word Redis commands (Jonas Dohse) +- Add EVAL with array syntax (dmoena) +- Fix tests due to Redis reply order changes in 2.6.5+ (Bryce Baril) +- Added a test for the SLOWLOG command (Nitesh Sinha) +- Fix SMEMBERS order dependency in test broken by Redis changes (Garrett Johnson) +- Update commands for new Redis commands (David Trejo) +- Prevent exception from SELECT on subscriber reconnection (roamm) + +## v0.8.2 - November 11, 2012 + +Another version bump because 0.8.1 didn't get applied properly for some mysterious reason. +Sorry about that. + +Changed name of "faster" parser to "javascript". + +## v0.8.1 - September 11, 2012 + +Important bug fix for null responses (Jerry Sievert) + +## v0.8.0 - September 10, 2012 + +Many contributed features and fixes, including: + +- Pure JavaScript reply parser that is usually faster than hiredis (Jerry Sievert) +- Remove hiredis as optionalDependency from package.json. It still works if you want it. +- Restore client state on reconnect, including select, subscribe, and monitor. (Ignacio Burgueño) +- Fix idle event (Trae Robrock) +- Many documentation improvements and bug fixes (David Trejo) + +## v0.7.2 - April 29, 2012 + +Many contributed fixes. Thank you, contributors. + +- [GH-190] - pub/sub mode fix (Brian Noguchi) +- [GH-165] - parser selection fix (TEHEK) +- numerous documentation and examples updates +- auth errors emit Errors instead of Strings (David Trejo) + +## v0.7.1 - November 15, 2011 + +Fix regression in reconnect logic. + +Very much need automated tests for reconnection and queue logic. + +## v0.7.0 - November 14, 2011 + +Many contributed fixes. Thanks everybody. + +- [GH-127] - properly re-initialize parser on reconnect +- [GH-136] - handle passing undefined as callback (Ian Babrou) +- [GH-139] - properly handle exceptions thrown in pub/sub event handlers (Felix Geisendörfer) +- [GH-141] - detect closing state on stream error (Felix Geisendörfer) +- [GH-142] - re-select database on reconnection (Jean-Hugues Pinson) +- [GH-146] - add sort example (Maksim Lin) + +Some more goodies: + +- Fix bugs with node 0.6 +- Performance improvements +- New version of `multi_bench.js` that tests more realistic scenarios +- [GH-140] - support optional callback for subscribe commands +- Properly flush and error out command queue when connection fails +- Initial work on reconnection thresholds + +## v0.6.7 - July 30, 2011 + +(accidentally skipped v0.6.6) + +Fix and test for [GH-123] + +Passing an Array as as the last argument should expand as users +expect. The old behavior was to coerce the arguments into Strings, +which did surprising things with Arrays. + +## v0.6.5 - July 6, 2011 + +Contributed changes: + +- Support SlowBuffers (Umair Siddique) +- Add Multi to exports (Louis-Philippe Perron) +- Fix for drain event calculation (Vladimir Dronnikov) + +Thanks! + +## v0.6.4 - June 30, 2011 + +Fix bug with optional callbacks for hmset. + +## v0.6.2 - June 30, 2011 + +Bugs fixed: + +- authentication retry while server is loading db (danmaz74) [GH-101] +- command arguments processing issue with arrays + +New features: + +- Auto update of new commands from redis.io (Dave Hoover) +- Performance improvements and backpressure controls. +- Commands now return the true/false value from the underlying socket write(s). +- Implement command_queue high water and low water for more better control of queueing. + +See `examples/backpressure_drain.js` for more information. + +## v0.6.1 - June 29, 2011 + +Add support and tests for Redis scripting through EXEC command. + +Bug fix for monitor mode. (forddg) + +Auto update of new commands from redis.io (Dave Hoover) + +## v0.6.0 - April 21, 2011 + +Lots of bugs fixed. + +- connection error did not properly trigger reconnection logic [GH-85] +- client.hmget(key, [val1, val2]) was not expanding properly [GH-66] +- client.quit() while in pub/sub mode would throw an error [GH-87] +- client.multi(['hmset', 'key', {foo: 'bar'}]) fails [GH-92] +- unsubscribe before subscribe would make things very confused [GH-88] +- Add BRPOPLPUSH [GH-79] + +## v0.5.11 - April 7, 2011 + +Added DISCARD + +I originally didn't think DISCARD would do anything here because of the clever MULTI interface, but somebody +pointed out to me that DISCARD can be used to flush the WATCH set. + +## v0.5.10 - April 6, 2011 + +Added HVALS + +## v0.5.9 - March 14, 2011 + +Fix bug with empty Array arguments - Andy Ray + +## v0.5.8 - March 14, 2011 + +Add `MONITOR` command and special monitor command reply parsing. + +## v0.5.7 - February 27, 2011 + +Add magical auth command. + +Authentication is now remembered by the client and will be automatically sent to the server +on every connection, including any reconnections. + +## v0.5.6 - February 22, 2011 + +Fix bug in ready check with `return_buffers` set to `true`. + +Thanks to Dean Mao and Austin Chau. + +## v0.5.5 - February 16, 2011 + +Add probe for server readiness. + +When a Redis server starts up, it might take a while to load the dataset into memory. +During this time, the server will accept connections, but will return errors for all non-INFO +commands. Now node_redis will send an INFO command whenever it connects to a server. +If the info command indicates that the server is not ready, the client will keep trying until +the server is ready. Once it is ready, the client will emit a "ready" event as well as the +"connect" event. The client will queue up all commands sent before the server is ready, just +like it did before. When the server is ready, all offline/non-ready commands will be replayed. +This should be backward compatible with previous versions. + +To disable this ready check behavior, set `options.no_ready_check` when creating the client. + +As a side effect of this change, the key/val params from the info command are available as +`client.server_options`. Further, the version string is decomposed into individual elements +in `client.server_options.versions`. + +## v0.5.4 - February 11, 2011 + +Fix excess memory consumption from Queue backing store. + +Thanks to Gustaf Sjöberg. + +## v0.5.3 - February 5, 2011 + +Fix multi/exec error reply callback logic. + +Thanks to Stella Laurenzo. + +## v0.5.2 - January 18, 2011 + +Fix bug where unhandled error replies confuse the parser. + +## v0.5.1 - January 18, 2011 + +Fix bug where subscribe commands would not handle redis-server startup error properly. + +## v0.5.0 - December 29, 2010 + +Some bug fixes: + +- An important bug fix in reconnection logic. Previously, reply callbacks would be invoked twice after + a reconnect. +- Changed error callback argument to be an actual Error object. + +New feature: + +- Add friendly syntax for HMSET using an object. + +## v0.4.1 - December 8, 2010 + +Remove warning about missing hiredis. You probably do want it though. + +## v0.4.0 - December 5, 2010 + +Support for multiple response parsers and hiredis C library from Pieter Noordhuis. +Return Strings instead of Buffers by default. +Empty nested mb reply bug fix. + +## v0.3.9 - November 30, 2010 + +Fix parser bug on failed EXECs. + +## v0.3.8 - November 10, 2010 + +Fix for null MULTI response when WATCH condition fails. + +## v0.3.7 - November 9, 2010 + +Add "drain" and "idle" events. + +## v0.3.6 - November 3, 2010 + +Add all known Redis commands from Redis master, even ones that are coming in 2.2 and beyond. + +Send a friendlier "error" event message on stream errors like connection refused / reset. + +## v0.3.5 - October 21, 2010 + +A few bug fixes. + +- Fixed bug with `nil` multi-bulk reply lengths that showed up with `BLPOP` timeouts. +- Only emit `end` once when connection goes away. +- Fixed bug in `test.js` where driver finished before all tests completed. + +## unversioned wasteland + +See the git history for what happened before. diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000000..2adee6acb34 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,60 @@ +# 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[AT]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..d9fac6a450c --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,111 @@ +# 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. +- **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). + +--- + +## 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://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. + +--- + +## 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, run `npm install` to install all dependencies, and then run `npm test`. To check detailed coverage locally run the `npm run coverage` command after +testing and open the generated `./coverage/index.html` in your browser. + +Note that the test suite assumes that a few tools are installed in your environment, such as: +- redis (make sure redis-server is not running when starting the tests, it's part of the test-suite to start it and you'll end up with a "port already in use" error) +- stunnel (for TLS tests) + +### 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? diff --git a/LICENSE b/LICENSE index 710407f442b..db86cc4de7f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ -LICENSE - "MIT License" +MIT License -Copyright (c) 2016 by NodeRedis +Copyright (c) 2016-present 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 2b889d32712..10f0c044886 100644 --- a/README.md +++ b/README.md @@ -1,84 +1,66 @@ -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) - -This is a complete and feature rich Redis client for node.js. __It supports all Redis commands__ and focuses on high performance. - -Install with: +

+ + + +

Node Redis

+

A high performance Node.js Redis client.

+

+ +--- + +

+ NPM downloads + NPM version + Build Status + Windows Build Status + Coverage Status + + Coverage Status + +

+ +--- + +## Installation + +```bash +npm install redis +``` - npm install redis +## Usage -## Usage Example +#### Example ```js -var redis = require("redis"), - client = redis.createClient(); - -// if you'd like to select database 3, instead of 0 (default), call -// client.select(3, function() { /* ... */ }); +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.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("key", "value", redis.print); +client.get("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. +Note that the API is entirely asynchronous. To get data back from the server, +you'll need to use a callback. ### Promises -You can also use node_redis with promises by promisifying node_redis with [bluebird](https://github.com/petkaantonov/bluebird) 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 -var redis = require('redis'); -bluebird.promisifyAll(redis.RedisClient.prototype); -bluebird.promisifyAll(redis.Multi.prototype); -``` - -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: +const { promisify } = require("util"); +const getAsync = promisify(client.get).bind(client); -return client.multi().get('foo').execAsync().then(function(res) { - console.log(res); // => 'bar' -}); +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 @@ -86,722 +68,922 @@ 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) { + // ... +}); ``` +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. + 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. +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("missing_key", function(err, reply) { + // reply is null when the key is missing + console.log(reply); }); ``` -For a list of Redis commands, see [Redis Command Reference](http://redis.io/commands) +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! -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! +## 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. +`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) and `attempt` (the attempt #) attributes. +`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 callback and encounter a ReplyError it is going to be emitted to the error listener. +`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 +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" +#### `"end"` `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. +#### `"warning"` -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`. +`client` will emit `warning` when password was set but none is needed and if a +deprecated option / function / similar is used. -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. +### redis.createClient() -### "warning" +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: -`client` will emit `warning` when password was set but none is needed and if a deprecated option / function / similar is used. +- `redis.createClient([options])` +- `redis.createClient(unix_socket[, options])` +- `redis.createClient(redis_url[, options])` +- `redis.createClient(port[, host][, options])` -### "idle" (deprecated) +**Tip:** If the Redis server runs on the same machine as the client consider +using unix sockets if possible to increase throughput. -`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 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])` - -__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. #### `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:]//[[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. | -| 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. | -| 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` | +| user | null | The ACL user (only valid when `password` is set) | +| 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. | +| connect_timeout | 3600000 | In milliseconds. This should only be the timeout for connecting to redis, but for now it interferes with `retry_strategy` and stops it from reconnecting after this timeout. | + +**`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"); // 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.times_connected > 10) { - // End reconnecting with built in error - return undefined; - } - // reconnect after - return Math.min(options.attempt * 100, 3000); +const 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); + }, }); ``` -## 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 to coordinate with reconnections, the ready check, -etc. To make this easier, `client.auth()` stashes `password` and will send it after each connection, -including reconnections. `callback` is invoked only once, after the response to the very first -`AUTH` command sent. +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 +to coordinate with reconnections, the ready check, etc. To make this easier, +`client.auth()` stashes `password` and will send it after each connection, +including reconnections. `callback` is invoked only once, after the response to +the very first `AUTH` command sent. NOTE: Your call to `client.auth()` should not be inside the ready handler. If you are doing this wrong, `client` will emit an error that looks something like this `Error: Ready check failed: ERR operation not permitted`. -## 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() +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 +(and therefore no connection to the redis server exists) it is going to end the +connection right away instead of resulting in further reconnections! All offline +commands are going to be flushed with an error in that case. -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 (and therefor no connection to the redis server exists) it is going to end the 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 +`client.quit()` as mentioned above. -Forcibly close the connection to the Redis server. Note that this does not wait until all replies have been parsed. -If you want to exit cleanly, call `client.quit()` as mentioned above. +You should set flush to true, if you are not absolutely sure you do not care +about any other commands. If you set flush to false all still running commands +will silently fail. -You should set flush to true, if you are not absolutely sure you do not care about any other commands. -If you set flush to false all still running commands will silently fail. - -This example closes the connection to the Redis server before the replies have been read. You probably don't -want to do this: +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) { - // 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("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.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 (>= v.2.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 -* `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 (this should not happen) -* `AggregateError` subclass of `AbortError`: Emitted in case multiple unresolved commands without callback got rejected in debug_mode instead of lots of `AbortError`s. +- `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 + (this should not happen) +- `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: +#### 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); - assert.strictEqual(err.errors.length, 2); // The set and get got aggregated in here - assert.strictEqual(err.code, 'NR_CLOSED'); -}); -client.set('foo', 123, 'bar', function (err, res) { // To 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 () { - client.end(true); // Force closing the connection while the command did not yet return - redis.debug_mode = false; - }); +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 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", "bar", "baz", function(err, res) { + // Too many arguments + 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); + redis.debug_mode = false; + }); +}); ``` 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 error is added to the returned error as `origin` attribute. +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 `UNERCTAIN_STATE` code is returned. -A `CONNECTION_BROKEN` error code is used in case node_redis gives up to reconnect. +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 +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. +Call `unref()` on the underlying socket connection to the Redis server, allowing +the program to exit once no more commands are pending. -This is an **experimental** feature, and only supports a subset of the Redis protocol. Any commands where client state is saved on the Redis server, e.g. `*SUBSCRIBE` or the blocking `BL*` commands will *NOT* work with `.unref()`. +This is an **experimental** feature, and only supports a subset of the Redis +protocol. Any commands where client state is saved on the Redis server, e.g. +`*SUBSCRIBE` or the blocking `BL*` commands will _NOT_ work with `.unref()`. ```js -var redis = require("redis") -var client = redis.createClient() +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. -*/ -client.unref() -client.get("foo", function (err, value){ - if (err) throw(err) - console.log(value) -}) + * 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. +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 `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: +**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("key", function(err, value) { + console.log(value.foo); // > "bar" + console.log(value.hello); // > "world" }); ``` -Output: +#### client.hmset(hash, key1, val1, ...keyN, valN, [callback]) -```js -{ mjr: '1', another: '23', home: '1234' } -``` +Multiple values may also be set by supplying more arguments. -### client.hmset(hash, obj[, callback]) - -Multiple values in a hash can be set by supplying an object: +**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]) +#### Example -Multiple values may also be set by supplying a list: +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, only commands that modify the subscription set are valid and quit (and depending on the redis version ping as well). When the subscription -set is empty, the connection is put back into regular mode. +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 +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()`). +If you need to send regular commands to Redis while in subscriber mode, just +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`. +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. +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. +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`. +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`. +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. +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 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)). +`MULTI` commands are queued up until an `EXEC` is issued, and then all commands +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). ```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 -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()); - }); +// chain commands +client + .multi() + .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 " + 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. +`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 and all commands are going to be aborted. That error contains a `.errors` property that contains the concret 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. +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 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 this example: +You can either chain together `MULTI` commands as in the above example, or you +can queue individual commands while still sending regular client command as in +this example: ```js -var redis = require("redis"), - client = redis.createClient(), multi; +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 -multi.exec(function (err, replies) { - console.log(replies); // 101, 2 +// 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, 51 }); ``` -In addition to adding commands to the `MULTI` queue individually, you can also pass an array -of commands and arguments to the constructor: +In addition to adding commands to the `MULTI` queue individually, you can also +pass an array of commands and arguments to the constructor: ```js -var redis = require("redis"), - client = redis.createClient(), multi; - -client.multi([ - ["mget", "multifoo", "multibar", redis.print], - ["incr", "multifoo"], - ["incr", "multibar"] -]).exec(function (err, replies) { +const redis = require("redis"); + +const client = redis.createClient(); + +client + .multi([ + ["mget", "foo", "bar", redis.print], + ["incr", "hello"], + ]) + .exec(function(err, replies) { console.log(replies); -}); + }); ``` -### Multi.exec_atomic([callback]) - -Identical to Multi.exec but with the difference that executing a single command will not use transactions. +#### Multi.exec_atomic([callback]) -## client.batch([commands]) +Identical to Multi.exec but with the difference that executing a single command +will not use transactions. -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. +#### Optimistic Locks -`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. +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? -If you fire many commands at once this is going to boost the execution speed significantly compared to fireing 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. +To solve this, Redis supports the [WATCH](https://redis.io/topics/transactions) +command, which is meant to be used with MULTI: -## Monitor mode +```js +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 execError; + + /** + * 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 + */ + }); + }); +}); +``` -Redis supports the `MONITOR` command, which lets you see all commands received by the Redis server -across all client connections, including from other client libraries and other computers. +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. -A `monitor` event is going to be emitted for every command fired from any client 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. +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 +client cannot connect to Redis. -Example: +An example where we can see the execution of a `multi` command fail is as follows: ```js -var client = require("redis").createClient(); -client.monitor(function (err, res) { - console.log("Entering monitoring mode."); +const clients = { + watcher: redis.createClient(), + modifier: redis.createClient(), +}; + +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 setError; + }); + + // 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. + setTimeout(function() { + clients.watcher + .multi() + .set("foo", "bar") + .set("hello", "world") + .exec((multiExecError, results) => { + if (multiExecError) throw multiExecError; + + if (results === null) { + console.log("transaction aborted because results were null"); + } else { + console.log("transaction worked and returned", results); + } + + clients.watcher.quit(); + clients.modifier.quit(); + }); + }, 1000); }); -client.set('foo', 'bar'); +``` + +#### `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. + +The following example would watch the keys `foo` and `hello`, not the field `hello` +of hash `foo`: + +```js +const redis = require("redis"); + +const client = redis.createClient(); + +client.hget("foo", "hello", function(hashGetError, result) { + if (hashGetError) throw hashGetError; + + //Do some processing with the value from this field and watch it after -client.on("monitor", function (time, args, raw_reply) { - console.log(time + ": " + args); // 1458910076.446514:['set', 'foo', 'bar'] + 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. + */ + }); }); ``` -# Extras +This limitation also applies to sets (you can not watch individual set members) +and any other collections. -Some other things you might like to know about. +### client.batch([commands]) -## client.server_info +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. -After the ready probe completes, the results from the INFO command are saved in the `client.server_info` -object. +`BATCH` commands are queued up until an `EXEC` is issued, and then all commands +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. +Otherwise both, errors and results could be returned at the same time. -The `versions` key contains an array of the elements of the version string for easy comparison. +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. - > client.server_info.redis_version - '2.3.0' - > client.server_info.versions - [ 2, 3, 0 ] +### Monitor mode -## redis.print() +Redis supports the `MONITOR` command, which lets you see all commands received +by the Redis server across all client connections, including from other client +libraries and other computers. -A handy callback function for displaying return values when testing. Example: +A `monitor` event is going to be emitted for every command fired from any client +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: ```js -var redis = require("redis"), - client = 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("connect", function () { - client.set("foo_rand000000000000", "some fantastic value", redis.print); - client.get("foo_rand000000000000", redis.print); +client.on("monitor", function(time, args, rawReply) { + console.log(time + ": " + args); // 1458910076.446514:['set', 'foo', 'bar'] }); ``` -This will print: +## Extras + +Some other things you might find useful. + +### `client.server_info` - Reply: OK - Reply: some fantastic value +After the ready probe completes, the results from the INFO command are saved in +the `client.server_info` object. -Note that this program will not exit cleanly because the client is still connected. +The `versions` key contains an array of the elements of the version string for +easy comparison. -## Multi-word commands +``` +> client.server_info.redis_version +'2.3.0' +> client.server_info.versions +[ 2, 3, 0 ] +``` + +### `redis.print()` + +A handy callback function for displaying return values when testing. Example: + +```js +const redis = require("redis"); +const client = redis.createClient(); + +client.on("connect", function() { + client.set("foo", "bar", redis.print); // => "Reply: OK" + client.get("foo", redis.print); // => "Reply: bar" + client.quit(); +}); +``` + +### Multi-word commands To execute redis multi-word commands like `SCRIPT LOAD` or `CLIENT LIST` pass the second word as first parameter: - client.script('load', 'return 1'); - client.multi().script('load', 'return 1').exec(...); - client.multi([['script', 'load', 'return 1']]).exec(...); +```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 you pass a callback, duplicate is going to wait until the client is ready and returns it in the callback. If an error occurs in the meanwhile, that is going to return an error instead in the 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 +you pass a callback, duplicate is going to wait until the client is ready and +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 accomodate the connection- -blocking redis commands BRPOP, BLPOP, and BRPOPLPUSH. If these commands -are used on the same redisClient instance as non-blocking commands, the +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 Redis client instance as non-blocking commands, the non-blocking ones may be queued up until after the blocking ones finish. - 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]]) - -All Redis commands have been added to the `client` object. However, if new commands are introduced before this library is updated, -you can use `send_command()` to send arbitrary commands to Redis. -The command_name has to be lower case. - -All commands are sent as multi-bulk commands. `args` can either be an Array of arguments, or omitted / set to undefined. - -## client.connected +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.sendCommand(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 +individual commands you can use `sendCommand()` to send arbitrary commands to +Redis. + +All commands are sent as multi-bulk commands. `args` can either be an Array of +arguments, or omitted / set to undefined. + +### `redis.addCommand(command_name)` + +Calling addCommand 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` 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. +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 commands. +The number of commands that have been queued up for a future connection. You can +use this to enforce some kind of maximum queue depth for pre-connection +commands. ### Commands with Optional and Keyword arguments This applies to anything that uses an optional `[WITHSCORES]` or `[LIMIT offset count]` in the [redis.io/commands](http://redis.io/commands) documentation. -Example: -```js -var args = [ 'myzset', 1, 'one', 2, 'two', 3, 'three', 99, 'ninety-nine' ]; -client.zadd(args, function (err, response) { - if (err) throw err; - console.log('added '+response+' items.'); - - // -Infinity and +Infinity also work - var args1 = [ 'myzset', '+inf', '-inf' ]; - client.zrevrangebyscore(args1, function (err, response) { - if (err) throw err; - console.log('example1', response); - // write your code here - }); +#### Example - 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 - }); +```js +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 + const args1 = ["myzset", "+inf", "-inf"]; + client.zrevrangebyscore(args1, function(rangeError, rangeResponse) { + if (rangeError) throw rangeError; + console.log("response1", rangeResponse); + // ... + }); + + 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); + // ... + }); }); ``` ## Performance -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 - ``` +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 +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 -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. -If you only want to have good stack traces but not the debug output run your application in development mode instead (`NODE_ENV=development`). +This is also going to result in good stack traces opposed to useless ones +otherwise for any async operation. +If you only want to have good stack traces but not the debug output run your +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. +Good stack traces are only activated in development and debug mode as this +results in a significant performance penalty. + +**_Comparison_**: + +Standard stack trace: -___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: + +Debug stack trace: + ``` ReplyError: ERR wrong number of arguments for 'set' command at new Command (/home/ruben/repos/redis/lib/command.js:9:902) @@ -818,28 +1000,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. +## Contributing -## 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) - -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) - -### 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 maintance 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. +This repository is licensed under the "MIT" license. See [LICENSE](LICENSE). diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 9e79508e4c7..00000000000 --- a/appveyor.yml +++ /dev/null @@ -1,44 +0,0 @@ -# http://www.appveyor.com/docs/appveyor-yml - -# Test against these versions of Node.js. -environment: - matrix: - - nodejs_version: "0.10" - - nodejs_version: "0.12" - - nodejs_version: "4" - - nodejs_version: "6" - -pull_requests: - do_not_increment_build_number: true - -platform: Any CPU -shallow_clone: true - -# Install scripts. (runs after repo cloning) -install: - # Install the Redis - - nuget install redis-64 -excludeversion - - redis-64\tools\redis-server.exe --service-install - - redis-64\tools\redis-server.exe --service-start - - '@ECHO Redis Started' - # Get the required Node version - - ps: Install-Product node $env:nodejs_version - # Typical npm stuff - - npm install - -# Post-install test scripts. -test_script: - # Output useful info for debugging. - - node --version - - npm --version - - cmd: npm t - -os: - - Default Azure - - Windows Server 2012 R2 - -# Don't actually build using MSBuild -build: off - -# Set build version format here instead of in the admin panel. -version: "{build}" diff --git a/benchmarks/diff_multi_bench_output.js b/benchmarks/diff_multi_bench_output.js index dc0d3c227d9..c3ed47a5fb5 100755 --- a/benchmarks/diff_multi_bench_output.js +++ b/benchmarks/diff_multi_bench_output.js @@ -2,7 +2,7 @@ var fs = require('fs'); var metrics = require('metrics'); - // `node diff_multi_bench_output.js beforeBench.txt afterBench.txt` +// `node diff_multi_bench_output.js beforeBench.txt afterBench.txt` var file1 = process.argv[2]; var file2 = process.argv[3]; 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/changelog.md b/changelog.md deleted file mode 100644 index 6ec0e3c663f..00000000000 --- a/changelog.md +++ /dev/null @@ -1,833 +0,0 @@ -Changelog -========= - -## v.2.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 - -Features - -- All returned errors are from now a subclass of `RedisError`. - -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 - -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 - -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 - -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 - -Bugfixes - -- Fixed individual callbacks of a transaction not being called (2.6.0 regression) - -## v.2.6.1 - 02 Jun, 2016 - -Bugfixes - -- Fixed invalid function name being exported - -## v.2.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 - -Bugfixes - -- 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 - -## v.2.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)) - -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 - -## v.2.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 - -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 - -## v.2.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 completly 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 - -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 - -Bugfixes - -- Revert throwing on invalid data types and print a warning instead - -## v.2.5.2 - 16 Mar, 2016 - -Bugfixes - -- Fixed breaking changes against Redis 2.4 introduced in 2.5.0 / 2.5.1 - -## v.2.5.1 - 15 Mar, 2016 - -Bugfixes - -- Fixed info command not working anymore with optional section argument - -## v.2.5.0 - 15 Mar, 2016 - -Same changelog as the pre-release - -## v.2.5.0-1 - 07 Mar, 2016 - -This is a big release with some substaintual underlining changes. Therefor this is released as a pre-release and I encourage anyone who's able to, to test this out. - -It took way to long to release this one and the next release cycles will be shorter again. - -This release is also going to deprecate a couple things to prepare for a future v.3 (it'll still take a while to v.3). - -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 happend (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 convienence -- Fixed parsing the db keyspace even if the first database does not begin with a zero -- Fixed handling of errors occuring 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 harmfull 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 - -Bugfixes - -- Fixed not emitting ready after reconnect with disable_resubscribing ([@maxgalbu](https://github.com/maxgalbu)) - -## v.2.4.1 - 25 Nov, 2015 - -Bugfixes - -- Fixed a js parser regression introduced in 2.4.0 ([@BridgeAR](https://github.com/BridgeAR)) - -## v.2.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)) - -Bugfixes - -- 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 - -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. - -## v.2.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)) - -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)) - -## v.2.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)) - -## v.2.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)) - -## v.2.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)) - -## v.2.2.2 - 14 Oct, 2015 - -Bugfixes - -- 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 - -No code change - -## v.2.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)) - -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)) - -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. - -Please consider using .batch instead of looping through a lot of commands one by one. This will significantly improve your performance. - -Here are some stats compared to ioredis 1.9.1 (Lenovo T450s i7-5600U): - - simple set - 82,496 op/s » ioredis - 112,617 op/s » node_redis - - simple get - 82,015 op/s » ioredis - 105,701 op/s » node_redis - - simple get with pipeline - 10,233 op/s » ioredis - 26,541 op/s » node_redis (using .batch) - - lrange 100 - 7,321 op/s » ioredis - 26,155 op/s » node_redis - - publish - 90,524 op/s » ioredis - 112,823 op/s » node_redis - - subscribe - 43,783 op/s » ioredis - 61,889 op/s » node_redis - -To conclude: we can proudly say that node_redis is very likely outperforming any other node redis client. - -Known issues - -- 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) - -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) - -## v2.0.1 - Sep 24, 2015 - -Bugfixes: - -- Fix argument mutation while using the array notation in combination with keys / callbacks ([#866](.)). (@BridgeAR) - -## v2.0.0 - Sep 21, 2015 - -This is the biggest release that node_redis had since it was released in 2010. A long list of outstanding bugs has been fixed, so we are very happy to present you redis 2.0 and we highly recommend updating as soon as possible. - -# What's new in 2.0 - -- Implemented a "connection is broken" mode if no connection could be established -- node_redis no longer throws under any circumstances, preventing it from terminating applications. -- Multi error handling is now working properly -- Consistent command behavior including multi -- Windows support -- Improved performance -- A lot of code cleanup -- Many bug fixes -- Better user support! - -## Features: - -- Added a "redis connection is broken" mode after reaching max connection attempts / exceeding connection timeout. (@BridgeAR) -- Added NODE_DEBUG=redis env to activate the debug_mode (@BridgeAR) -- Added a default connection timeout of 24h instead of never timing out as a default (@BridgeAR) -- 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 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) -- Full windows support (@bcoe) -- Increased coverage by 10% and add a lot of tests to make sure everything works as it should. We now reached 97% :-) (@BridgeAR) -- 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) - -## Bug fixes: - -- Don't kill the app anymore by randomly throwing errors sync instead of emitting them (@BridgeAR) -- Don't catch user errors anymore occuring in callbacks (no try callback anymore & more fixes for the parser) (@BridgeAR) -- Early garbage collection of queued items (@dohse) -- Fix js parser returning errors as strings (@BridgeAR) -- Do not wrap errors into other errors (@BridgeAR) -- Authentication failures are now returned in the callback instead of being emitted (@BridgeAR) -- Fix a memory leak on reconnect (@rahar) -- Using `send_command` directly may now also be called without the args as stated in the [README.md](./README.md) (@BridgeAR) -- Fix the multi.exec error handling (@BridgeAR) -- Fix commands being inconsistent and behaving wrong (@BridgeAR) -- Channel names with spaces are now properly resubscribed after a reconnection (@pbihler) -- Do not try to reconnect after the connection timeout has been exceeded (@BridgeAR) -- Ensure the execution order is observed if using .eval (@BridgeAR) -- Fix commands not being rejected after calling .quit (@BridgeAR) -- Fix .auth calling the callback twice if already connected (@BridgeAR) -- Fix detect_buffers not working in pub sub mode and while monitoring (@BridgeAR) -- Fix channel names always being strings instead of buffers while return_buffers is true (@BridgeAR) -- Don't print any debug statements if not asked for (@BridgeAR) -- Fix a couple small other bugs - -## Breaking changes: - -1. redis.send_command commands have to be lower case from now on. This does only apply if you use `.send_command` directly instead of the convenient methods like `redis.command`. -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. -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. -8. The keyspace of `redis.server_info` (db0...) is from now on an object instead of an string. - -NodeRedis also thanks @qdb, @tobek, @cvibhagool, @frewsxcv, @davidbanham, @serv, @vitaliylag, @chrishamant, @GamingCoder and all other contributors that I may have missed for their contributions! - -From now on we'll push new releases more frequently out and fix further long outstanding things and implement new features. - -
- -## 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) - -## v0.12.1 - Aug 10, 2014 -* 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) - -## v0.11.0 - Jul 10, 2014 - -* IPv6 Support. (Yann Stephan) -* Revert error emitting and go back to throwing errors. (Bryce Baril) -* Set socket_keepalive to prevent long-lived client timeouts. (mohit) -* Correctly reset retry timer. (ouotuo) -* Domains protection from bad user exit. (Jake Verbaten) -* Fix reconnection socket logic to prevent misqueued entries. (Iain Proctor) - -## v0.10.3 - May 22, 2014 - -* Update command list to match Redis 2.8.9 (Charles Feng) - -## v0.10.2 - May 18, 2014 - -* Better 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) - -## v0.10.0 - December 21, 2013 - -* Instead of throwing errors asynchronously, emit errors on client. (Bryce Baril) - -## v0.9.2 - December 15, 2013 - -* Regenerate commands for new 2.8.x Redis commands. (Marek Ventur) -* Correctly time reconnect counts when using 'auth'. (William Hockey) - -## v0.9.1 - November 23, 2013 - -* Allow hmset to accept numeric keys. (Alex Stokes) -* Fix TypeError for multiple MULTI/EXEC errors. (Kwangsu Kim) - -## v0.9.0 - October 17, 2013 - -* Domains support. (Forrest L Norvell) - -## v0.8.6 - October 2, 2013 - -* If error is already an Error, don't wrap it in another Error. (Mathieu M-Gosselin) -* Fix retry delay logic (Ian Babrou) -* Return Errors instead of strings where Errors are expected (Ian Babrou) -* Add experimental `.unref()` method to RedisClient (Bryce Baril / Olivier Lalonde) -* Strengthen checking of reply to prevent conflating "message" or "pmessage" fields with pub_sub replies. (Bryce Baril) - -## v0.8.5 - September 26, 2013 - -* Add `auth_pass` option to connect and immediately authenticate (Henrik Peinar) - -## v0.8.4 - June 24, 2013 - -Many contributed features and fixes, including: -* Ignore password set if not needed. (jbergknoff) -* Improved compatibility with 0.10.X for tests and client.end() (Bryce Baril) -* Protect connection retries from application exceptions. (Amos Barreto) -* Better exception handling for Multi/Exec (Thanasis Polychronakis) -* Renamed pubsub mode to subscriber mode (Luke Plaster) -* Treat SREM like SADD when passed an array (Martin Ciparelli) -* Fix empty unsub/punsub TypeError (Jeff Barczewski) -* Only attempt to run a callback if it one was provided (jifeng) - -## v0.8.3 - April 09, 2013 - -Many contributed features and fixes, including: -* Fix some tests for Node.js version 0.9.x+ changes (Roman Ivanilov) -* Fix error when commands submitted after idle event handler (roamm) -* Bypass Redis for no-op SET/SETEX commands (jifeng) -* Fix HMGET + detect_buffers (Joffrey F) -* Fix CLIENT LOAD functionality (Jonas Dohse) -* Add percentage outputs to diff_multi_bench_output.js (Bryce Baril) -* Add retry_max_delay option (Tomasz Durka) -* Fix parser off-by-one errors with nested multi-bulk replies (Bryce Baril) -* Prevent parser from sinking application-side exceptions (Bryce Baril) -* Fix parser incorrect buffer skip when parsing multi-bulk errors (Bryce Baril) -* Reverted previous change with throwing on non-string values with HMSET (David Trejo) -* Fix command queue sync issue when using pubsub (Tom Leach) -* Fix compatibility with two-word Redis commands (Jonas Dohse) -* Add EVAL with array syntax (dmoena) -* Fix tests due to Redis reply order changes in 2.6.5+ (Bryce Baril) -* Added a test for the SLOWLOG command (Nitesh Sinha) -* Fix SMEMBERS order dependency in test broken by Redis changes (Garrett Johnson) -* Update commands for new Redis commands (David Trejo) -* Prevent exception from SELECT on subscriber reconnection (roamm) - - -## v0.8.2 - November 11, 2012 - -Another version bump because 0.8.1 didn't get applied properly for some mysterious reason. -Sorry about that. - -Changed name of "faster" parser to "javascript". - -## v0.8.1 - September 11, 2012 - -Important bug fix for null responses (Jerry Sievert) - -## v0.8.0 - September 10, 2012 - -Many contributed features and fixes, including: - -* Pure JavaScript reply parser that is usually faster than hiredis (Jerry Sievert) -* Remove hiredis as optionalDependency from package.json. It still works if you want it. -* Restore client state on reconnect, including select, subscribe, and monitor. (Ignacio Burgueño) -* Fix idle event (Trae Robrock) -* Many documentation improvements and bug fixes (David Trejo) - -## v0.7.2 - April 29, 2012 - -Many contributed fixes. Thank you, contributors. - -* [GH-190] - pub/sub mode fix (Brian Noguchi) -* [GH-165] - parser selection fix (TEHEK) -* numerous documentation and examples updates -* auth errors emit Errors instead of Strings (David Trejo) - -## v0.7.1 - November 15, 2011 - -Fix regression in reconnect logic. - -Very much need automated tests for reconnection and queue logic. - -## v0.7.0 - November 14, 2011 - -Many contributed fixes. Thanks everybody. - -* [GH-127] - properly re-initialize parser on reconnect -* [GH-136] - handle passing undefined as callback (Ian Babrou) -* [GH-139] - properly handle exceptions thrown in pub/sub event handlers (Felix Geisendörfer) -* [GH-141] - detect closing state on stream error (Felix Geisendörfer) -* [GH-142] - re-select database on reconnection (Jean-Hugues Pinson) -* [GH-146] - add sort example (Maksim Lin) - -Some more goodies: - -* Fix bugs with node 0.6 -* Performance improvements -* New version of `multi_bench.js` that tests more realistic scenarios -* [GH-140] - support optional callback for subscribe commands -* Properly flush and error out command queue when connection fails -* Initial work on reconnection thresholds - -## v0.6.7 - July 30, 2011 - -(accidentally skipped v0.6.6) - -Fix and test for [GH-123] - -Passing an Array as as the last argument should expand as users -expect. The old behavior was to coerce the arguments into Strings, -which did surprising things with Arrays. - -## v0.6.5 - July 6, 2011 - -Contributed changes: - -* Support SlowBuffers (Umair Siddique) -* Add Multi to exports (Louis-Philippe Perron) -* Fix for drain event calculation (Vladimir Dronnikov) - -Thanks! - -## v0.6.4 - June 30, 2011 - -Fix bug with optional callbacks for hmset. - -## v0.6.2 - June 30, 2011 - -Bugs fixed: - -* authentication retry while server is loading db (danmaz74) [GH-101] -* command arguments processing issue with arrays - -New features: - -* Auto update of new commands from redis.io (Dave Hoover) -* Performance improvements and backpressure controls. -* Commands now return the true/false value from the underlying socket write(s). -* Implement command_queue high water and low water for more better control of queueing. - -See `examples/backpressure_drain.js` for more information. - -## v0.6.1 - June 29, 2011 - -Add support and tests for Redis scripting through EXEC command. - -Bug fix for monitor mode. (forddg) - -Auto update of new commands from redis.io (Dave Hoover) - -## v0.6.0 - April 21, 2011 - -Lots of bugs fixed. - -* connection error did not properly trigger reconnection logic [GH-85] -* client.hmget(key, [val1, val2]) was not expanding properly [GH-66] -* client.quit() while in pub/sub mode would throw an error [GH-87] -* client.multi(['hmset', 'key', {foo: 'bar'}]) fails [GH-92] -* unsubscribe before subscribe would make things very confused [GH-88] -* Add BRPOPLPUSH [GH-79] - -## v0.5.11 - April 7, 2011 - -Added DISCARD - -I originally didn't think DISCARD would do anything here because of the clever MULTI interface, but somebody -pointed out to me that DISCARD can be used to flush the WATCH set. - -## v0.5.10 - April 6, 2011 - -Added HVALS - -## v0.5.9 - March 14, 2011 - -Fix bug with empty Array arguments - Andy Ray - -## v0.5.8 - March 14, 2011 - -Add `MONITOR` command and special monitor command reply parsing. - -## v0.5.7 - February 27, 2011 - -Add magical auth command. - -Authentication is now remembered by the client and will be automatically sent to the server -on every connection, including any reconnections. - -## v0.5.6 - February 22, 2011 - -Fix bug in ready check with `return_buffers` set to `true`. - -Thanks to Dean Mao and Austin Chau. - -## v0.5.5 - February 16, 2011 - -Add probe for server readiness. - -When a Redis server starts up, it might take a while to load the dataset into memory. -During this time, the server will accept connections, but will return errors for all non-INFO -commands. Now node_redis will send an INFO command whenever it connects to a server. -If the info command indicates that the server is not ready, the client will keep trying until -the server is ready. Once it is ready, the client will emit a "ready" event as well as the -"connect" event. The client will queue up all commands sent before the server is ready, just -like it did before. When the server is ready, all offline/non-ready commands will be replayed. -This should be backward compatible with previous versions. - -To disable this ready check behavior, set `options.no_ready_check` when creating the client. - -As a side effect of this change, the key/val params from the info command are available as -`client.server_options`. Further, the version string is decomposed into individual elements -in `client.server_options.versions`. - -## v0.5.4 - February 11, 2011 - -Fix excess memory consumption from Queue backing store. - -Thanks to Gustaf Sjöberg. - -## v0.5.3 - February 5, 2011 - -Fix multi/exec error reply callback logic. - -Thanks to Stella Laurenzo. - -## v0.5.2 - January 18, 2011 - -Fix bug where unhandled error replies confuse the parser. - -## v0.5.1 - January 18, 2011 - -Fix bug where subscribe commands would not handle redis-server startup error properly. - -## v0.5.0 - December 29, 2010 - -Some bug fixes: - -* An important bug fix in reconnection logic. Previously, reply callbacks would be invoked twice after - a reconnect. -* Changed error callback argument to be an actual Error object. - -New feature: - -* Add friendly syntax for HMSET using an object. - -## v0.4.1 - December 8, 2010 - -Remove warning about missing hiredis. You probably do want it though. - -## v0.4.0 - December 5, 2010 - -Support for multiple response parsers and hiredis C library from Pieter Noordhuis. -Return Strings instead of Buffers by default. -Empty nested mb reply bug fix. - -## v0.3.9 - November 30, 2010 - -Fix parser bug on failed EXECs. - -## v0.3.8 - November 10, 2010 - -Fix for null MULTI response when WATCH condition fails. - -## v0.3.7 - November 9, 2010 - -Add "drain" and "idle" events. - -## v0.3.6 - November 3, 2010 - -Add all known Redis commands from Redis master, even ones that are coming in 2.2 and beyond. - -Send a friendlier "error" event message on stream errors like connection refused / reset. - -## v0.3.5 - October 21, 2010 - -A few bug fixes. - -* Fixed bug with `nil` multi-bulk reply lengths that showed up with `BLPOP` timeouts. -* Only emit `end` once when connection goes away. -* Fixed bug in `test.js` where driver finished before all tests completed. - -## unversioned wasteland - -See the git history for what happened before. diff --git a/examples/streams.js b/examples/streams.js new file mode 100644 index 00000000000..726e4adf920 --- /dev/null +++ b/examples/streams.js @@ -0,0 +1,47 @@ +'use strict'; + +var redis = require('redis'); +var client1 = redis.createClient(); +var client2 = redis.createClient(); +var client3 = redis.createClient(); + +client1.xadd('mystream', '*', 'field1', 'm1', function (err) { + if (err) { + return console.error(err); + } + client1.xgroup('CREATE', 'mystream', 'mygroup', '$', function (err) { + if (err) { + return console.error(err); + } + }); + + client2.xreadgroup('GROUP', 'mygroup', 'consumer', 'Block', 1000, 'NOACK', + 'STREAMS', 'mystream', '>', function (err, stream) { + if (err) { + return console.error(err); + } + console.log('client2 ' + stream); + }); + + client3.xreadgroup('GROUP', 'mygroup', 'consumer', 'Block', 1000, 'NOACK', + 'STREAMS', 'mystream', '>', function (err, stream) { + if (err) { + return console.error(err); + } + console.log('client3 ' + stream); + }); + + + client1.xadd('mystream', '*', 'field1', 'm2', function (err) { + if (err) { + return console.error(err); + } + }); + + client1.xadd('mystream', '*', 'field1', 'm3', function (err) { + if (err) { + return console.error(err); + } + }); + +}); diff --git a/index.js b/index.js index 909ebb1a919..fe79c5f3935 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,11 @@ var tls = require('tls'); var util = require('util'); var utils = require('./lib/utils'); var Command = require('./lib/command'); -var Queue = require('double-ended-queue'); +var Queue = require('denque'); var errorClasses = require('./lib/customErrors'); var EventEmitter = require('events'); var Parser = require('redis-parser'); +var RedisErrors = require('redis-errors'); var commands = require('redis-commands'); var debug = require('./lib/debug'); var unifyOptions = require('./lib/createClient'); @@ -19,11 +20,6 @@ var SUBSCRIBE_COMMANDS = { punsubscribe: true }; -// Newer Node.js versions > 0.10 return the EventEmitter right away and using .EventEmitter was deprecated -if (typeof EventEmitter !== 'function') { - EventEmitter = EventEmitter.EventEmitter; -} - function noop () {} function handle_detect_buffers_reply (reply, command, buffer_args) { @@ -71,36 +67,18 @@ 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_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) { options.rename_commands[command.toLowerCase()] = options.rename_commands[command]; } @@ -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 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.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 = {}; @@ -147,8 +109,8 @@ function RedisClient (options, stream) { this.closing = false; this.server_info = {}; this.auth_pass = options.auth_pass || options.password; + this.auth_user = options.auth_user || options.user; this.selected_db = options.db; // Save the selected db here, used when reconnecting - this.old_state = null; this.fire_strings = true; // Determine if strings or buffers should be written to the stream this.pipeline = false; this.sub_commands_left = 0; @@ -159,23 +121,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 (this.reply_parser.name !== 'javascript') { - return this.warn( - 'You attached the "' + event + '" listener without the returnBuffers option set to true.\n' + - 'Please use the JavaScript parser or set the returnBuffers option to true to return 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; @@ -201,7 +147,7 @@ function create_parser (self) { err.message += '. Please report this.'; self.ready = false; self.flush_and_error({ - message: 'Fatal error encountert. Command aborted.', + message: 'Fatal error encountered. Command aborted.', code: 'NR_FATAL' }, { error: err, @@ -211,7 +157,6 @@ function create_parser (self) { self.create_stream(); }, returnBuffers: self.buffers || self.message_buffers, - name: self.options.parser || 'javascript', stringNumbers: self.options.string_numbers || false }); } @@ -272,19 +217,12 @@ 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) { self.on_error(err); }); - /* istanbul ignore next: difficult to test and not important as long as we keep this listener */ - this.stream.on('clientError', function (err) { - debug('clientError occured'); - self.on_error(err); - }); - this.stream.once('close', function (hadError) { self.connection_gone('close'); }); @@ -297,14 +235,17 @@ 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) { this.ready = true; - this.auth(this.auth_pass); + // Fail silently as we might not be able to connect + this.auth(this.auth_pass, this.auth_user, function (err) { + if (err && err.code !== 'UNCERTAIN_STATE') { + self.emit('error', err); + } + }); this.ready = false; } }; @@ -396,7 +337,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); } @@ -411,7 +352,7 @@ RedisClient.prototype.on_connect = function () { this.connected = true; this.ready = false; this.emitted_end = false; - this.stream.setKeepAlive(this.options.socket_keepalive); + this.stream.setKeepAlive(this.options.socket_keepalive, this.options.socket_initial_delay); this.stream.setTimeout(0); this.emit('connect'); @@ -616,25 +557,28 @@ RedisClient.prototype.connection_gone = function (why, error) { if (this.retry_delay instanceof Error) { error = this.retry_delay; } + + var errorMessage = 'Redis connection in broken state: retry aborted.'; + 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) { - var message = 'Redis connection in broken state: '; - if (this.retry_totaltime >= this.connect_timeout) { - message += 'connection timeout exceeded.'; - } else { - message += 'maximum connection attempts exceeded.'; - } - + if (this.retry_totaltime >= this.connect_timeout) { + var message = 'Redis connection in broken state: connection timeout exceeded.'; this.flush_and_error({ message: message, code: 'CONNECTION_BROKEN', @@ -646,8 +590,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; } @@ -665,15 +609,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); }; @@ -702,16 +643,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') { @@ -761,7 +695,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; @@ -777,7 +711,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]); @@ -785,7 +719,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]); @@ -886,39 +820,44 @@ 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]; } } 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; + } + // there is always a callback in this scenario + command_obj.callback(undefinedArgError); + return false; } else { // Seems like numbers are converted fast using string concatenation args_copy[i] = '' + args[i]; @@ -1087,12 +1026,14 @@ exports.RedisClient = RedisClient; exports.print = utils.print; exports.Multi = require('./lib/multi'); exports.AbortError = errorClasses.AbortError; -exports.RedisError = Parser.RedisError; -exports.ParserError = Parser.ParserError; -exports.ReplyError = Parser.ReplyError; +exports.RedisError = RedisErrors.RedisError; +exports.ParserError = RedisErrors.ParserError; +exports.ReplyError = RedisErrors.ReplyError; exports.AggregateError = errorClasses.AggregateError; // Add all redis commands / node_redis api to the client require('./lib/individualCommands'); require('./lib/extendedApi'); -require('./lib/commands'); + +//enables adding new commands (for modules and new commands) +exports.addCommand = exports.add_command = require('./lib/commands'); diff --git a/lib/commands.js b/lib/commands.js index 6ca01df2c79..a3b5189698b 100644 --- a/lib/commands.js +++ b/lib/commands.js @@ -4,24 +4,8 @@ var commands = require('redis-commands'); var Multi = require('./multi'); var RedisClient = require('../').RedisClient; var Command = require('./command'); -// Feature detect if a function may change it's name -var changeFunctionName = (function () { - var fn = function abc () {}; - try { - Object.defineProperty(fn, 'name', { - value: 'foobar' - }); - return true; - } catch (e) { - return false; - } -}()); - -// TODO: Rewrite this including the invidual commands into a Commands class -// that provided a functionality to add new commands to the client - -commands.list.forEach(function (command) { +var addCommand = function (command) { // Some rare Redis commands use special characters in their command name // Convert those to a underscore to prevent using invalid function names var commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1'); @@ -61,11 +45,13 @@ commands.list.forEach(function (command) { } return this.internal_send_command(new Command(command, arr, callback)); }; - if (changeFunctionName) { - Object.defineProperty(RedisClient.prototype[command], 'name', { - value: commandName - }); + // Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run) + if (commandName !== command) { + RedisClient.prototype[commandName.toUpperCase()] = RedisClient.prototype[commandName] = RedisClient.prototype[command]; } + Object.defineProperty(RedisClient.prototype[command], 'name', { + value: commandName + }); } // Do not override existing functions @@ -104,10 +90,16 @@ commands.list.forEach(function (command) { this.queue.push(new Command(command, arr, callback)); return this; }; - if (changeFunctionName) { - Object.defineProperty(Multi.prototype[command], 'name', { - value: commandName - }); + // Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run) + if (commandName !== command) { + Multi.prototype[commandName.toUpperCase()] = Multi.prototype[commandName] = Multi.prototype[command]; } + Object.defineProperty(Multi.prototype[command], 'name', { + value: commandName + }); } -}); +}; + +commands.list.forEach(addCommand); + +module.exports = addCommand; diff --git a/lib/createClient.js b/lib/createClient.js index a019fc7a383..b03bb575399 100644 --- a/lib/createClient.js +++ b/lib/createClient.js @@ -23,15 +23,24 @@ module.exports = function createClient (port_arg, host_arg, options) { } else if (typeof port_arg === 'string' || port_arg && port_arg.url) { options = utils.clone(port_arg.url ? port_arg : host_arg || options); - var parsed = URL.parse(port_arg.url || port_arg, true, true); + var url = port_arg.url || port_arg; + var parsed = URL.parse(url, true, true); // [redis:]//[[user][:password]@][host][:port][/db-number][?db=db-number[&password=bar[&option=value]]] if (parsed.slashes) { // We require slashes if (parsed.auth) { - options.password = parsed.auth.split(':')[1]; + var columnIndex = parsed.auth.indexOf(':'); + options.password = parsed.auth.slice(columnIndex + 1); + if (columnIndex > 0) { + options.user = parsed.auth.slice(0, columnIndex); + } } - if (parsed.protocol && parsed.protocol !== 'redis:') { - console.warn('node_redis: WARNING: You passed "' + parsed.protocol.substring(0, parsed.protocol.length - 1) + '" as protocol instead of the "redis" protocol!'); + if (parsed.protocol) { + if (parsed.protocol === 'rediss:') { + options.tls = options.tls || {}; + } else if (parsed.protocol !== 'redis:') { + console.warn('node_redis: WARNING: You passed "' + parsed.protocol.substring(0, parsed.protocol.length - 1) + '" as protocol instead of the "redis" protocol!'); + } } if (parsed.pathname && parsed.pathname !== '/') { options.db = parsed.pathname.substr(1); @@ -59,7 +68,7 @@ module.exports = function createClient (port_arg, host_arg, options) { } else if (parsed.hostname) { throw new RangeError('The redis url must begin with slashes "//" or contain slashes after the redis protocol'); } else { - options.path = port_arg; + options.path = url; } } else if (typeof port_arg === 'object' || port_arg === undefined) { @@ -67,7 +76,7 @@ module.exports = function createClient (port_arg, host_arg, options) { options.host = options.host || host_arg; if (port_arg && arguments.length !== 1) { - throw new TypeError('To many arguments passed to createClient. Please only pass the options object'); + throw new TypeError('Too many arguments passed to createClient. Please only pass the options object'); } } diff --git a/lib/customErrors.js b/lib/customErrors.js index d9b34421a71..2483db0d8d4 100644 --- a/lib/customErrors.js +++ b/lib/customErrors.js @@ -2,14 +2,13 @@ var util = require('util'); var assert = require('assert'); -var RedisError = require('redis-parser').RedisError; +var RedisError = require('redis-errors').RedisError; var ADD_STACKTRACE = false; function AbortError (obj, stack) { assert(obj, 'The options argument is required'); assert.strictEqual(typeof obj, 'object', 'The options argument has to be of type object'); - RedisError.call(this, obj.message, ADD_STACKTRACE); Object.defineProperty(this, 'message', { value: obj.message || '', configurable: true, diff --git a/lib/debug.js b/lib/debug.js index 0e6333f2ec3..d69c1ea6246 100644 --- a/lib/debug.js +++ b/lib/debug.js @@ -4,7 +4,9 @@ var index = require('../'); function debug () { if (index.debug_mode) { - console.error.apply(null, arguments); + var data = Array.prototype.slice.call(arguments); + data.unshift(new Date().toISOString()); + console.error.apply(null, data); } } diff --git a/lib/extendedApi.js b/lib/extendedApi.js index 0e9589775be..27ed4215d6a 100644 --- a/lib/extendedApi.js +++ b/lib/extendedApi.js @@ -16,6 +16,7 @@ RedisClient.prototype.send_command = RedisClient.prototype.sendCommand = functio if (typeof command !== 'string') { throw new TypeError('Wrong input type "' + (command !== null && command !== undefined ? command.constructor.name : command) + '" for command name'); } + command = command.toLowerCase(); if (!Array.isArray(args)) { if (args === undefined || args === null) { args = []; @@ -32,9 +33,9 @@ RedisClient.prototype.send_command = RedisClient.prototype.sendCommand = functio // Using the raw multi command is only possible with this function // If the command is not yet added to the client, the internal function should be called right away - // Otherwise we need to redirect the calls to make sure the interal functions don't get skipped + // Otherwise we need to redirect the calls to make sure the internal functions don't get skipped // The internal functions could actually be used for any non hooked function - // but this might change from time to time and at the moment there's no good way to distinguishe them + // but this might change from time to time and at the moment there's no good way to distinguish them // from each other, so let's just do it do it this way for the time being if (command === 'multi' || typeof this[command] !== 'function') { return this.internal_send_command(new Command(command, args, callback)); @@ -94,7 +95,7 @@ RedisClient.prototype.duplicate = function (options, callback) { existing_options[elem] = options[elem]; } var client = new RedisClient(existing_options); - client.selected_db = this.selected_db; + client.selected_db = options.db || this.selected_db; if (typeof callback === 'function') { var ready_listener = function () { callback(null, client); diff --git a/lib/individualCommands.js b/lib/individualCommands.js index 88fca126887..c3ea3da0df3 100644 --- a/lib/individualCommands.js +++ b/lib/individualCommands.js @@ -4,7 +4,7 @@ var utils = require('./utils'); var debug = require('./debug'); var Multi = require('./multi'); var Command = require('./command'); -var no_password_is_set = /no password is set/; +var no_password_is_set = /no password is set|called without any password configured/; var loading = /LOADING/; var RedisClient = require('../').RedisClient; @@ -180,7 +180,7 @@ Multi.prototype.info = Multi.prototype.INFO = function info (section, callback) return this; }; -function auth_callback (self, pass, callback) { +function auth_callback (self, pass, user, callback) { return function (err, res) { if (err) { if (no_password_is_set.test(err.message)) { @@ -191,7 +191,7 @@ function auth_callback (self, pass, callback) { // If redis is still loading the db, it will not authenticate and everything else will fail debug('Redis still loading, trying to authenticate later'); setTimeout(function () { - self.auth(pass, callback); + self.auth(pass, user, callback); }, 100); return; } @@ -200,25 +200,37 @@ function auth_callback (self, pass, callback) { }; } -RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, callback) { +RedisClient.prototype.auth = RedisClient.prototype.AUTH = function auth (pass, user, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); + // Backward compatibility support for auth with password only + if (user instanceof Function) { + callback = user; + user = null; + } // Stash auth for connect and reconnect. this.auth_pass = pass; + this.auth_user = user; var ready = this.ready; this.ready = ready || this.offline_queue.length === 0; - var tmp = this.internal_send_command(new Command('auth', [pass], auth_callback(this, pass, callback))); + var tmp = this.internal_send_command(new Command('auth', user ? [user, pass] : [pass], auth_callback(this, pass, user, callback))); this.ready = ready; return tmp; }; // Only works with batch, not in a transaction -Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, callback) { +Multi.prototype.auth = Multi.prototype.AUTH = function auth (pass, user, callback) { debug('Sending auth to ' + this.address + ' id ' + this.connection_id); + // Backward compatibility support for auth with password only + if (user instanceof Function) { + callback = user; + user = null; + } // Stash auth for connect and reconnect. this.auth_pass = pass; - this.queue.push(new Command('auth', [pass], auth_callback(this._client, callback))); + this.auth_user = user; + this.queue.push(new Command('auth', user ? [user, pass] : [pass], auth_callback(this._client, pass, user, callback))); return this; }; @@ -398,7 +410,7 @@ RedisClient.prototype.subscribe = RedisClient.prototype.SUBSCRIBE = function sub callback, i = 0; if (Array.isArray(arguments[0])) { - arr = arguments[0]; + arr = arguments[0].slice(0); callback = arguments[1]; } else { len = arguments.length; @@ -425,7 +437,7 @@ Multi.prototype.subscribe = Multi.prototype.SUBSCRIBE = function subscribe () { callback, i = 0; if (Array.isArray(arguments[0])) { - arr = arguments[0]; + arr = arguments[0].slice(0); callback = arguments[1]; } else { len = arguments.length; @@ -453,7 +465,7 @@ RedisClient.prototype.unsubscribe = RedisClient.prototype.UNSUBSCRIBE = function callback, i = 0; if (Array.isArray(arguments[0])) { - arr = arguments[0]; + arr = arguments[0].slice(0); callback = arguments[1]; } else { len = arguments.length; @@ -481,7 +493,7 @@ Multi.prototype.unsubscribe = Multi.prototype.UNSUBSCRIBE = function unsubscribe callback, i = 0; if (Array.isArray(arguments[0])) { - arr = arguments[0]; + arr = arguments[0].slice(0); callback = arguments[1]; } else { len = arguments.length; @@ -510,7 +522,7 @@ RedisClient.prototype.psubscribe = RedisClient.prototype.PSUBSCRIBE = function p callback, i = 0; if (Array.isArray(arguments[0])) { - arr = arguments[0]; + arr = arguments[0].slice(0); callback = arguments[1]; } else { len = arguments.length; @@ -537,7 +549,7 @@ Multi.prototype.psubscribe = Multi.prototype.PSUBSCRIBE = function psubscribe () callback, i = 0; if (Array.isArray(arguments[0])) { - arr = arguments[0]; + arr = arguments[0].slice(0); callback = arguments[1]; } else { len = arguments.length; @@ -565,7 +577,7 @@ RedisClient.prototype.punsubscribe = RedisClient.prototype.PUNSUBSCRIBE = functi callback, i = 0; if (Array.isArray(arguments[0])) { - arr = arguments[0]; + arr = arguments[0].slice(0); callback = arguments[1]; } else { len = arguments.length; @@ -593,7 +605,7 @@ Multi.prototype.punsubscribe = Multi.prototype.PUNSUBSCRIBE = function punsubscr callback, i = 0; if (Array.isArray(arguments[0])) { - arr = arguments[0]; + arr = arguments[0].slice(0); callback = arguments[1]; } else { len = arguments.length; diff --git a/lib/multi.js b/lib/multi.js index 63f5d210856..d89cffbbd48 100644 --- a/lib/multi.js +++ b/lib/multi.js @@ -1,6 +1,6 @@ 'use strict'; -var Queue = require('double-ended-queue'); +var Queue = require('denque'); var utils = require('./utils'); var Command = require('./command'); diff --git a/lib/utils.js b/lib/utils.js index 52e58ecfa6c..d0336ae9c1d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -127,7 +127,7 @@ module.exports = { reply_to_object: replyToObject, print: print, err_code: /^([A-Z]+)\s+(.+)$/, - monitor_regex: /^[0-9]{10,11}\.[0-9]+ \[[0-9]+ .+\]( ".+?")+$/, + monitor_regex: /^[0-9]{10,11}\.[0-9]+ \[[0-9]+ .+\].*"$/, clone: convenienceClone, callback_or_emit: callbackOrEmit, reply_in_order: replyInOrder diff --git a/package.json b/package.json index 417a9c486cf..762a798c34e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "redis", - "version": "2.7.1", - "description": "Redis client library", + "version": "3.1.1", + "description": "A high performance Redis client.", "keywords": [ "database", "redis", @@ -14,46 +14,64 @@ "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": { "coveralls": "nyc report --reporter=text-lcov | coveralls", "coverage": "nyc report --reporter=html", "benchmark": "node benchmarks/multi_bench.js", - "test": "nyc --cache mocha ./test/*.js ./test/commands/*.js --timeout=8000", - "lint": "eslint . --fix && npm run coverage", + "test": "nyc --cache mocha ./test/*.spec.js ./test/commands/*.spec.js --timeout=8000 && npm run coverage", + "lint": "eslint .", + "lint:fix": "eslint . --fix", + "lint:report": "eslint --output-file=eslint-report.json --format=json .", "compare": "node benchmarks/diff_multi_bench_output.js beforeBench.txt afterBench.txt" }, "dependencies": { - "double-ended-queue": "^2.1.0-0", - "redis-commands": "^1.2.0", - "redis-parser": "^2.5.0" + "denque": "^1.5.0", + "redis-commands": "^1.7.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0" }, "engines": { - "node": ">=0.10.0" + "node": ">=10" }, "devDependencies": { - "bluebird": "^3.0.2", - "coveralls": "^2.11.2", + "bluebird": "^3.7.2", + "coveralls": "^3.1.0", + "cross-spawn": "^7.0.3", + "eslint": "^7.21.0", "intercept-stdout": "~0.1.2", - "eslint": "^3.5.0", - "metrics": "^0.1.9", - "mocha": "^3.1.2", - "nyc": "^8.3.0", - "tcp-port-used": "^0.1.2", - "uuid": "^2.0.1", - "win-spawn": "^2.0.0" + "metrics": "^0.1.21", + "mocha": "^8.3.0", + "nyc": "^15.1.0", + "prettier": "^2.2.1", + "tcp-port-used": "^1.0.1", + "uuid": "^8.3.2" }, "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..d1b596e5ae3 100644 --- a/test/auth.spec.js +++ b/test/auth.spec.js @@ -3,6 +3,7 @@ var assert = require('assert'); var config = require('./lib/config'); var helper = require('./helper'); +var errors = require('./errors'); var redis = config.redis; if (process.platform === 'win32') { @@ -19,9 +20,9 @@ describe('client authentication', function () { helper.allTests({ allConnections: true - }, function (parser, ip, args) { + }, function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var auth = 'porkchopsandwiches'; var client = null; @@ -60,7 +61,7 @@ describe('client authentication', function () { }); var tmp = client.command_queue.get(0).callback; client.command_queue.get(0).callback = function (err, res) { - client.auth = function (pass, callback) { + client.auth = function (pass, user, callback) { callback(null, 'retry worked'); }; tmp(new Error('ERR redis is still LOADING')); @@ -70,11 +71,13 @@ describe('client authentication', function () { it('emits error when auth is bad without callback', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - client = redis.createClient.apply(null, args); + client = redis.createClient.apply(null, config.configureClient(ip, { + no_ready_check: true + })); client.once('error', function (err) { assert.strictEqual(err.command, 'AUTH'); - assert.ok(/ERR invalid password/.test(err.message)); + assert.ok(errors.invalidPassword.test(err.message)); return done(); }); @@ -84,11 +87,13 @@ describe('client authentication', function () { it('returns an error when auth is bad (empty string) with a callback', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - client = redis.createClient.apply(null, args); + client = redis.createClient.apply(null, config.configureClient(ip, { + no_ready_check: true + })); client.auth('', function (err, res) { assert.strictEqual(err.command, 'AUTH'); - assert.ok(/ERR invalid password/.test(err.message)); + assert.ok(errors.invalidPassword.test(err.message)); done(); }); }); @@ -133,7 +138,7 @@ describe('client authentication', function () { it('allows auth to be provided as config option for client', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - var args = config.configureClient(parser, ip, { + var args = config.configureClient(ip, { auth_pass: auth }); client = redis.createClient.apply(null, args); @@ -143,7 +148,7 @@ describe('client authentication', function () { it('allows auth and no_ready_check to be provided as config option for client', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - var args = config.configureClient(parser, ip, { + var args = config.configureClient(ip, { password: auth, no_ready_check: true }); @@ -154,7 +159,7 @@ describe('client authentication', function () { it('allows auth to be provided post-hoc with auth method', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - var args = config.configureClient(parser, ip); + var args = config.configureClient(ip); client = redis.createClient.apply(null, args); client.auth(auth); client.on('ready', done); @@ -190,10 +195,12 @@ describe('client authentication', function () { it('should return an error if the password is not correct and a callback has been provided', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - client = redis.createClient.apply(null, args); + client = redis.createClient.apply(null, config.configureClient(ip, { + no_ready_check: true + })); var async = true; - client.auth(undefined, function (err, res) { - assert.strictEqual(err.message, 'ERR invalid password'); + client.auth('undefined', function (err, res) { + assert.ok(errors.invalidPassword.test(err.message)); assert.strictEqual(err.command, 'AUTH'); assert.strictEqual(res, undefined); async = false; @@ -205,9 +212,11 @@ describe('client authentication', function () { it('should emit an error if the password is not correct and no callback has been provided', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - client = redis.createClient.apply(null, args); + client = redis.createClient.apply(null, config.configureClient(ip, { + no_ready_check: true + })); client.on('error', function (err) { - assert.strictEqual(err.message, 'ERR invalid password'); + assert.ok(errors.invalidPassword.test(err.message)); assert.strictEqual(err.command, 'AUTH'); done(); }); @@ -217,7 +226,7 @@ describe('client authentication', function () { it('allows auth to be provided post-hoc with auth method again', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - var args = config.configureClient(parser, ip, { + var args = config.configureClient(ip, { auth_pass: auth }); client = redis.createClient.apply(null, args); @@ -229,13 +238,13 @@ describe('client authentication', function () { it('does not allow any commands to be processed if not authenticated using no_ready_check true', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - var args = config.configureClient(parser, ip, { + var args = config.configureClient(ip, { no_ready_check: true }); client = redis.createClient.apply(null, args); client.on('ready', function () { client.set('foo', 'bar', function (err, res) { - assert.equal(err.message, 'NOAUTH Authentication required.'); + assert.ok(/^NOAUTH Authentication required\.(\r\n)?$/.test(err.message)); assert.equal(err.code, 'NOAUTH'); assert.equal(err.command, 'SET'); done(); @@ -248,7 +257,7 @@ describe('client authentication', function () { client = redis.createClient.apply(null, args); client.on('error', function (err) { assert.equal(err.code, 'NOAUTH'); - assert.equal(err.message, 'Ready check failed: NOAUTH Authentication required.'); + assert.ok(/^Ready check failed: NOAUTH Authentication required\.(\r\n)?$/.test(err.message)); assert.equal(err.command, 'INFO'); done(); }); @@ -258,10 +267,10 @@ describe('client authentication', function () { if (helper.redisProcess().spawnFailed()) this.skip(); client = redis.createClient({ password: 'wrong_password', - parser: parser + no_ready_check: true }); client.once('error', function (err) { - assert.strictEqual(err.message, 'ERR invalid password'); + assert.ok(errors.invalidPassword.test(err.message)); done(); }); }); @@ -269,7 +278,7 @@ describe('client authentication', function () { it('pubsub working with auth', function (done) { if (helper.redisProcess().spawnFailed()) this.skip(); - var args = config.configureClient(parser, ip, { + var args = config.configureClient(ip, { password: auth }); client = redis.createClient.apply(null, args); @@ -278,7 +287,7 @@ describe('client authentication', function () { client.once('ready', function () { assert.strictEqual(client.pub_sub_mode, 1); client.get('foo', function (err, res) { - assert(/ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message)); + assert.ok(errors.subscribeUnsubscribeOnly.test(err.message)); done(); }); }); @@ -299,7 +308,7 @@ describe('client authentication', function () { // returning the manipulated [error, result] from the callback. // There should be a better solution though - var args = config.configureClient(parser, 'localhost', { + var args = config.configureClient('localhost', { noReadyCheck: true }); client = redis.createClient.apply(null, args); diff --git a/test/batch.spec.js b/test/batch.spec.js index 762b2f70491..05cee80681a 100644 --- a/test/batch.spec.js +++ b/test/batch.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'batch' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { describe('when not connected', function () { var client; @@ -187,12 +187,12 @@ describe("The 'batch' method", function () { ['del', 'some set'], ['smembers', 'some set', undefined] // The explicit undefined is handled as a callback that is undefined ]) - .scard('some set') - .exec(function (err, replies) { - assert.strictEqual(4, replies[0].length); - assert.strictEqual(0, replies[2].length); - return done(); - }); + .scard('some set') + .exec(function (err, replies) { + assert.strictEqual(4, replies[0].length); + assert.strictEqual(0, replies[2].length); + return done(); + }); }); it('allows multiple operations to be performed using constructor with all kinds of syntax', function (done) { @@ -213,29 +213,29 @@ describe("The 'batch' method", function () { ['HMSET', 'batchhmset', ['batchbar', 'batchbaz']], ['hmset', 'batchhmset', ['batchbar', 'batchbaz'], helper.isString('OK')], ]) - .hmget(now, 123456789, 'otherTypes') - .hmget('key2', arr2, function noop () {}) - .hmget(['batchhmset2', 'some manner of key', 'batchbar3']) - .mget('batchfoo2', ['batchfoo3', 'batchfoo'], function (err, res) { - assert.strictEqual(res[0], 'batchbar2'); - assert.strictEqual(res[1], 'batchbar3'); - assert.strictEqual(res[2], null); - }) - .exec(function (err, replies) { - assert.equal(arr.length, 3); - assert.equal(arr2.length, 2); - assert.equal(arr3.length, 3); - assert.equal(arr4.length, 3); - assert.strictEqual(null, err); - assert.equal(replies[10][1], '555'); - assert.equal(replies[11][0], 'a type of value'); - assert.strictEqual(replies[12][0], null); - assert.equal(replies[12][1], 'test'); - assert.equal(replies[13][0], 'batchbar2'); - assert.equal(replies[13].length, 3); - assert.equal(replies.length, 14); - return done(); - }); + .hmget(now, 123456789, 'otherTypes') + .hmget('key2', arr2, function noop () {}) + .hmget(['batchhmset2', 'some manner of key', 'batchbar3']) + .mget('batchfoo2', ['batchfoo3', 'batchfoo'], function (err, res) { + assert.strictEqual(res[0], 'batchbar2'); + assert.strictEqual(res[1], 'batchbar3'); + assert.strictEqual(res[2], null); + }) + .exec(function (err, replies) { + assert.equal(arr.length, 3); + assert.equal(arr2.length, 2); + assert.equal(arr3.length, 3); + assert.equal(arr4.length, 3); + assert.strictEqual(null, err); + assert.equal(replies[10][1], '555'); + assert.equal(replies[11][0], 'a type of value'); + assert.strictEqual(replies[12][0], null); + assert.equal(replies[12][1], 'test'); + assert.equal(replies[13][0], 'batchbar2'); + assert.equal(replies[13].length, 3); + assert.equal(replies.length, 14); + return done(); + }); }); it('converts a non string key to a string', function (done) { @@ -316,11 +316,11 @@ describe("The 'batch' method", function () { ['mget', ['batchfoo', 'some', 'random value', 'keys']], ['incr', 'batchfoo'] ]) - .exec(function (err, replies) { - assert.strictEqual(replies.length, 2); - assert.strictEqual(replies[0].length, 4); - return done(); - }); + .exec(function (err, replies) { + assert.strictEqual(replies.length, 2); + assert.strictEqual(replies[0].length, 4); + return done(); + }); }); it('allows multiple operations to be performed on a hash', function (done) { diff --git a/test/commands/blpop.spec.js b/test/commands/blpop.spec.js index d68521e6b5e..64aedf81eae 100644 --- a/test/commands/blpop.spec.js +++ b/test/commands/blpop.spec.js @@ -8,9 +8,9 @@ var intercept = require('intercept-stdout'); describe("The 'blpop' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; var bclient; @@ -31,7 +31,7 @@ describe("The 'blpop' method", function () { }); client.rpush('blocking list', 'initial value', helper.isNumber(1)); unhookIntercept(); - assert(/^Send 127\.0\.0\.1:6379 id [0-9]+: \*3\r\n\$5\r\nrpush\r\n\$13\r\nblocking list\r\n\$13\r\ninitial value\r\n\n$/.test(text)); + assert(/Send 127\.0\.0\.1:6379 id [0-9]+: \*3\r\n\$5\r\nrpush\r\n\$13\r\nblocking list\r\n\$13\r\ninitial value\r\n\n$/.test(text)); redis.debug_mode = false; bclient.blpop('blocking list', 0, function (err, value) { assert.strictEqual(value[0], 'blocking list'); diff --git a/test/commands/client.spec.js b/test/commands/client.spec.js index 7ac32ae41ac..3214243107c 100644 --- a/test/commands/client.spec.js +++ b/test/commands/client.spec.js @@ -7,10 +7,10 @@ var redis = config.redis; describe("The 'client' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { var pattern = /addr=/; - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { @@ -57,7 +57,7 @@ describe("The 'client' method", function () { it('off', function (done) { helper.serverVersionAtLeast.call(this, client, [3, 2, 0]); assert.strictEqual(client.reply, 'ON'); - client.client(new Buffer('REPLY'), 'OFF', helper.isUndefined()); + client.client(Buffer.from('REPLY'), 'OFF', helper.isUndefined()); assert.strictEqual(client.reply, 'OFF'); client.set('foo', 'bar', helper.isUndefined(done)); }); @@ -65,7 +65,7 @@ describe("The 'client' method", function () { it('skip', function (done) { helper.serverVersionAtLeast.call(this, client, [3, 2, 0]); assert.strictEqual(client.reply, 'ON'); - client.client('REPLY', new Buffer('SKIP'), helper.isUndefined()); + client.client('REPLY', Buffer.from('SKIP'), helper.isUndefined()); assert.strictEqual(client.reply, 'SKIP_ONE_MORE'); client.set('foo', 'bar', helper.isUndefined()); client.get('foo', helper.isString('bar', done)); @@ -91,7 +91,7 @@ describe("The 'client' method", function () { var batch = client.batch(); assert.strictEqual(client.reply, 'ON'); batch.set('hello', 'world'); - batch.client(new Buffer('REPLY'), new Buffer('OFF'), helper.isUndefined()); + batch.client(Buffer.from('REPLY'), Buffer.from('OFF'), helper.isUndefined()); batch.set('foo', 'bar', helper.isUndefined()); batch.exec(function (err, res) { assert.strictEqual(client.reply, 'OFF'); diff --git a/test/commands/dbsize.spec.js b/test/commands/dbsize.spec.js index 4fdf80010d1..bd8b1467898 100644 --- a/test/commands/dbsize.spec.js +++ b/test/commands/dbsize.spec.js @@ -8,9 +8,9 @@ var uuid = require('uuid'); describe("The 'dbsize' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var key, value; beforeEach(function () { diff --git a/test/commands/del.spec.js b/test/commands/del.spec.js index 970d0886c43..86c1f4bb3af 100644 --- a/test/commands/del.spec.js +++ b/test/commands/del.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'del' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/eval.spec.js b/test/commands/eval.spec.js index 731d656d541..db74372db4d 100644 --- a/test/commands/eval.spec.js +++ b/test/commands/eval.spec.js @@ -8,9 +8,9 @@ var redis = config.redis; describe("The 'eval' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; var source = "return redis.call('set', 'sha', 'test')"; diff --git a/test/commands/exists.spec.js b/test/commands/exists.spec.js index 01a1b6891d9..399a0382f49 100644 --- a/test/commands/exists.spec.js +++ b/test/commands/exists.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'exists' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/expire.spec.js b/test/commands/expire.spec.js index f9041b71091..2891890edc0 100644 --- a/test/commands/expire.spec.js +++ b/test/commands/expire.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'expire' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/flushdb.spec.js b/test/commands/flushdb.spec.js index 61535e2e714..a4f761d3753 100644 --- a/test/commands/flushdb.spec.js +++ b/test/commands/flushdb.spec.js @@ -8,9 +8,9 @@ var uuid = require('uuid'); describe("The 'flushdb' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var key, key2; beforeEach(function () { diff --git a/test/commands/geoadd.spec.js b/test/commands/geoadd.spec.js index 3614910322d..b45df7c83a9 100644 --- a/test/commands/geoadd.spec.js +++ b/test/commands/geoadd.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'geoadd' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/get.spec.js b/test/commands/get.spec.js index e2b9a7db071..acbfc0d10db 100644 --- a/test/commands/get.spec.js +++ b/test/commands/get.spec.js @@ -8,9 +8,9 @@ var uuid = require('uuid'); describe("The 'get' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var key, value; beforeEach(function () { diff --git a/test/commands/getset.spec.js b/test/commands/getset.spec.js index e5da8573119..48dd7e9d739 100644 --- a/test/commands/getset.spec.js +++ b/test/commands/getset.spec.js @@ -8,9 +8,9 @@ var uuid = require('uuid'); describe("The 'getset' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var key, value, value2; beforeEach(function () { diff --git a/test/commands/hgetall.spec.js b/test/commands/hgetall.spec.js index 6c00e3ed099..5bfa609d0bc 100644 --- a/test/commands/hgetall.spec.js +++ b/test/commands/hgetall.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'hgetall' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; describe('regular client', function () { @@ -50,8 +50,7 @@ describe("The 'hgetall' method", function () { }); describe('binary client', function () { - var client; - var args = config.configureClient(parser, ip, { + var args = config.configureClient(ip, { return_buffers: true }); @@ -63,14 +62,14 @@ describe("The 'hgetall' method", function () { }); it('returns binary results', function (done) { - client.hmset(['bhosts', 'mjr', '1', 'another', '23', 'home', '1234', new Buffer([0xAA, 0xBB, 0x00, 0xF0]), new Buffer([0xCC, 0xDD, 0x00, 0xF0])], helper.isString('OK')); + client.hmset(['bhosts', 'mjr', '1', 'another', '23', 'home', '1234', Buffer.from([0xAA, 0xBB, 0x00, 0xF0]), Buffer.from([0xCC, 0xDD, 0x00, 0xF0])], helper.isString('OK')); client.HGETALL('bhosts', function (err, obj) { assert.strictEqual(4, Object.keys(obj).length); assert.strictEqual('1', obj.mjr.toString()); assert.strictEqual('23', obj.another.toString()); assert.strictEqual('1234', obj.home.toString()); - assert.strictEqual((new Buffer([0xAA, 0xBB, 0x00, 0xF0])).toString('binary'), Object.keys(obj)[3]); - assert.strictEqual((new Buffer([0xCC, 0xDD, 0x00, 0xF0])).toString('binary'), obj[(new Buffer([0xAA, 0xBB, 0x00, 0xF0])).toString('binary')].toString('binary')); + assert.strictEqual((Buffer.from([0xAA, 0xBB, 0x00, 0xF0])).toString('binary'), Object.keys(obj)[3]); + assert.strictEqual((Buffer.from([0xCC, 0xDD, 0x00, 0xF0])).toString('binary'), obj[(Buffer.from([0xAA, 0xBB, 0x00, 0xF0])).toString('binary')].toString('binary')); return done(err); }); }); diff --git a/test/commands/hincrby.spec.js b/test/commands/hincrby.spec.js index e94232d9a0e..10b4523b3f7 100644 --- a/test/commands/hincrby.spec.js +++ b/test/commands/hincrby.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'hincrby' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; var hash = 'test hash'; diff --git a/test/commands/hlen.spec.js b/test/commands/hlen.spec.js index 5dcab6ce8e2..874cb2970a1 100644 --- a/test/commands/hlen.spec.js +++ b/test/commands/hlen.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'hlen' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { @@ -20,10 +20,10 @@ describe("The 'hlen' method", function () { it('reports the count of keys', function (done) { var hash = 'test hash'; - var field1 = new Buffer('0123456789'); - var value1 = new Buffer('abcdefghij'); - var field2 = new Buffer(0); - var value2 = new Buffer(0); + var field1 = Buffer.from('0123456789'); + var value1 = Buffer.from('abcdefghij'); + var field2 = Buffer.alloc(0); + var value2 = Buffer.alloc(0); client.HSET(hash, field1, value1, helper.isNumber(1)); client.HSET(hash, field2, value2, helper.isNumber(1)); diff --git a/test/commands/hmget.spec.js b/test/commands/hmget.spec.js index 09bb89f258e..3676b5b7312 100644 --- a/test/commands/hmget.spec.js +++ b/test/commands/hmget.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'hmget' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; var hash = 'test hash'; diff --git a/test/commands/hmset.spec.js b/test/commands/hmset.spec.js index 93514cc2295..8ba54ecc3f2 100644 --- a/test/commands/hmset.spec.js +++ b/test/commands/hmset.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'hmset' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; var hash = 'test hash'; diff --git a/test/commands/hset.spec.js b/test/commands/hset.spec.js index d0ce7f5481b..746176f3d8c 100644 --- a/test/commands/hset.spec.js +++ b/test/commands/hset.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'hset' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; var hash = 'test hash'; @@ -21,52 +21,46 @@ describe("The 'hset' method", function () { }); it('allows a value to be set in a hash', function (done) { - var field = new Buffer('0123456789'); - var value = new Buffer('abcdefghij'); + var field = Buffer.from('0123456789'); + var value = Buffer.from('abcdefghij'); client.hset(hash, field, value, helper.isNumber(1)); client.HGET(hash, field, helper.isString(value.toString(), done)); }); it('handles an empty value', function (done) { - var field = new Buffer('0123456789'); - var value = new Buffer(0); + var field = Buffer.from('0123456789'); + var value = Buffer.alloc(0); client.HSET(hash, field, value, helper.isNumber(1)); client.HGET([hash, field], helper.isString('', done)); }); it('handles empty key and value', function (done) { - var field = new Buffer(0); - var value = new Buffer(0); + var field = Buffer.alloc(0); + var value = Buffer.alloc(0); client.HSET([hash, field, value], function (err, res) { assert.strictEqual(res, 1); client.HSET(hash, field, value, helper.isNumber(0, done)); }); }); - 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) { var hash = 'test hash'; var field1 = 'buffer'; - var value1 = new Buffer('abcdefghij'); + var value1 = Buffer.from('abcdefghij'); var field2 = 'date'; var value2 = new Date(); @@ -76,7 +70,7 @@ describe("The 'hset' method", function () { it('does not error when a buffer and date are set as fields on the same hash', function (done) { var hash = 'test hash'; var value1 = 'buffer'; - var field1 = new Buffer('abcdefghij'); + var field1 = Buffer.from('abcdefghij'); var value2 = 'date'; var field2 = new Date(); diff --git a/test/commands/incr.spec.js b/test/commands/incr.spec.js index f1f1e78ee8c..0caab84859a 100644 --- a/test/commands/incr.spec.js +++ b/test/commands/incr.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'incr' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { describe('when connected and a value in Redis', function () { diff --git a/test/commands/info.spec.js b/test/commands/info.spec.js index 39a9e9f5cce..4e5bb481fc9 100644 --- a/test/commands/info.spec.js +++ b/test/commands/info.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'info' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/keys.spec.js b/test/commands/keys.spec.js index 1aff0cea146..6ce45790b6d 100644 --- a/test/commands/keys.spec.js +++ b/test/commands/keys.spec.js @@ -8,9 +8,9 @@ var redis = config.redis; describe("The 'keys' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/mget.spec.js b/test/commands/mget.spec.js index 83d69f837f9..a2c671f683c 100644 --- a/test/commands/mget.spec.js +++ b/test/commands/mget.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'mget' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/monitor.spec.js b/test/commands/monitor.spec.js index a6c98747309..679277ffcac 100644 --- a/test/commands/monitor.spec.js +++ b/test/commands/monitor.spec.js @@ -8,7 +8,7 @@ var redis = config.redis; describe("The 'monitor' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { var client; @@ -62,7 +62,7 @@ describe("The 'monitor' method", function () { end(err); }); monitorClient.subscribe('foo', 'baz', function (err, res) { - // The return value might change in v.3 + // The return value might change in v.4 // assert.strictEqual(res, 'baz'); // TODO: Fix the return value of subscribe calls end(err); @@ -90,8 +90,8 @@ describe("The 'monitor' method", function () { monitorClient.MONITOR(function (err, res) { assert.strictEqual(monitorClient.monitoring, true); - assert.strictEqual(res.inspect(), new Buffer('OK').inspect()); - monitorClient.mget('hello', new Buffer('world')); + assert.strictEqual(res.inspect(), Buffer.from('OK').inspect()); + monitorClient.mget('hello', Buffer.from('world')); }); monitorClient.on('monitor', function (time, args, rawOutput) { diff --git a/test/commands/mset.spec.js b/test/commands/mset.spec.js index 9fac90728cf..b60f383134b 100644 --- a/test/commands/mset.spec.js +++ b/test/commands/mset.spec.js @@ -8,9 +8,9 @@ var uuid = require('uuid'); describe("The 'mset' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var key, value, key2, value2; beforeEach(function () { diff --git a/test/commands/msetnx.spec.js b/test/commands/msetnx.spec.js index a2f2ab17249..179f33744e6 100644 --- a/test/commands/msetnx.spec.js +++ b/test/commands/msetnx.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'msetnx' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/randomkey.test.js b/test/commands/randomkey.test.js index cf453e12e4b..226194f9214 100644 --- a/test/commands/randomkey.test.js +++ b/test/commands/randomkey.test.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'randomkey' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/rename.spec.js b/test/commands/rename.spec.js index cb3d7c31688..284fba310ed 100644 --- a/test/commands/rename.spec.js +++ b/test/commands/rename.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'rename' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/renamenx.spec.js b/test/commands/renamenx.spec.js index 3da640a0d8d..b56b0a1a5c9 100644 --- a/test/commands/renamenx.spec.js +++ b/test/commands/renamenx.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'renamenx' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/rpush.spec.js b/test/commands/rpush.spec.js index d137dc6473d..793d5d2d804 100644 --- a/test/commands/rpush.spec.js +++ b/test/commands/rpush.spec.js @@ -7,9 +7,9 @@ var assert = require('assert'); describe("The 'rpush' command", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/sadd.spec.js b/test/commands/sadd.spec.js index c2316739b76..442f391b9de 100644 --- a/test/commands/sadd.spec.js +++ b/test/commands/sadd.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'sadd' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/scard.spec.js b/test/commands/scard.spec.js index 5cf9b4eb73c..e327eb282a2 100644 --- a/test/commands/scard.spec.js +++ b/test/commands/scard.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'scard' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/script.spec.js b/test/commands/script.spec.js index 2e29ad553ab..c374f5b5e17 100644 --- a/test/commands/script.spec.js +++ b/test/commands/script.spec.js @@ -8,11 +8,11 @@ var redis = config.redis; describe("The 'script' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { var command = 'return 99'; var commandSha = crypto.createHash('sha1').update(command).digest('hex'); - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/sdiff.spec.js b/test/commands/sdiff.spec.js index e3963ecdf0c..95f81f09bd0 100644 --- a/test/commands/sdiff.spec.js +++ b/test/commands/sdiff.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'sdiff' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/sdiffstore.spec.js b/test/commands/sdiffstore.spec.js index 1797ca9e435..fe822b561b5 100644 --- a/test/commands/sdiffstore.spec.js +++ b/test/commands/sdiffstore.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'sdiffstore' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/select.spec.js b/test/commands/select.spec.js index 4297dca7f34..053496e337f 100644 --- a/test/commands/select.spec.js +++ b/test/commands/select.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'select' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { describe('when not connected', function () { var client; @@ -72,7 +72,7 @@ describe("The 'select' method", function () { assert.strictEqual(client.selected_db, undefined, 'default db should be undefined'); client.select(9999, function (err) { assert.equal(err.code, 'ERR'); - assert.equal(err.message, 'ERR invalid DB index'); + assert((err.message === 'ERR DB index is out of range' || err.message === 'ERR invalid DB index')); done(); }); }); @@ -97,7 +97,7 @@ describe("The 'select' method", function () { client.on('error', function (err) { assert.strictEqual(err.command, 'SELECT'); - assert.equal(err.message, 'ERR invalid DB index'); + assert((err.message === 'ERR DB index is out of range' || err.message === 'ERR invalid DB index')); done(); }); diff --git a/test/commands/set.spec.js b/test/commands/set.spec.js index 01b62443818..33a8bfa22c0 100644 --- a/test/commands/set.spec.js +++ b/test/commands/set.spec.js @@ -8,9 +8,9 @@ var uuid = require('uuid'); describe("The 'set' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var key, value; beforeEach(function () { @@ -140,10 +140,18 @@ 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('calls callback with error if null value is passed', function (done) { + client.set('foo', null, helper.isError(done)); }); it('emit an error with only the key set', function (done) { diff --git a/test/commands/setex.spec.js b/test/commands/setex.spec.js index 9a3bb53f0b1..a2126e6dbb6 100644 --- a/test/commands/setex.spec.js +++ b/test/commands/setex.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'setex' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/setnx.spec.js b/test/commands/setnx.spec.js index 2bce9f0a0ba..4b4688c0a68 100644 --- a/test/commands/setnx.spec.js +++ b/test/commands/setnx.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'setnx' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/sinter.spec.js b/test/commands/sinter.spec.js index b614a41ea4c..c4fc7759556 100644 --- a/test/commands/sinter.spec.js +++ b/test/commands/sinter.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'sinter' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/sinterstore.spec.js b/test/commands/sinterstore.spec.js index de7de87e10f..1ea4c4b109b 100644 --- a/test/commands/sinterstore.spec.js +++ b/test/commands/sinterstore.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'sinterstore' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/sismember.spec.js b/test/commands/sismember.spec.js index 69482b3b564..37ac1c466aa 100644 --- a/test/commands/sismember.spec.js +++ b/test/commands/sismember.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'sismember' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/slowlog.spec.js b/test/commands/slowlog.spec.js index 7f5417dbd8f..21f3f8007ac 100644 --- a/test/commands/slowlog.spec.js +++ b/test/commands/slowlog.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'slowlog' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/smembers.spec.js b/test/commands/smembers.spec.js index 8fbdcbba5ed..0bc8143719f 100644 --- a/test/commands/smembers.spec.js +++ b/test/commands/smembers.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'smembers' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/smove.spec.js b/test/commands/smove.spec.js index eb8eae10545..969c264b756 100644 --- a/test/commands/smove.spec.js +++ b/test/commands/smove.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'smove' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/sort.spec.js b/test/commands/sort.spec.js index 63b933eaef6..2ee08c44e21 100644 --- a/test/commands/sort.spec.js +++ b/test/commands/sort.spec.js @@ -34,9 +34,9 @@ function setupData (client, done) { describe("The 'sort' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/spop.spec.js b/test/commands/spop.spec.js index 6e0697c02a5..ec3e93fda3f 100644 --- a/test/commands/spop.spec.js +++ b/test/commands/spop.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'spop' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/srem.spec.js b/test/commands/srem.spec.js index 97c40d9ce2d..d325cb57151 100644 --- a/test/commands/srem.spec.js +++ b/test/commands/srem.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'srem' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/sunion.spec.js b/test/commands/sunion.spec.js index 44f5f247206..cc8eb624758 100644 --- a/test/commands/sunion.spec.js +++ b/test/commands/sunion.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'sunion' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/sunionstore.spec.js b/test/commands/sunionstore.spec.js index c9869bdf881..bd64c6f6b79 100644 --- a/test/commands/sunionstore.spec.js +++ b/test/commands/sunionstore.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'sunionstore' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/ttl.spec.js b/test/commands/ttl.spec.js index 65212f710c2..e176d41cb84 100644 --- a/test/commands/ttl.spec.js +++ b/test/commands/ttl.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'ttl' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/type.spec.js b/test/commands/type.spec.js index 4a0c4d2897c..f70d79e9ef6 100644 --- a/test/commands/type.spec.js +++ b/test/commands/type.spec.js @@ -6,9 +6,9 @@ var redis = config.redis; describe("The 'type' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/watch.spec.js b/test/commands/watch.spec.js index cf908646750..52a9b26f751 100644 --- a/test/commands/watch.spec.js +++ b/test/commands/watch.spec.js @@ -7,11 +7,11 @@ var redis = config.redis; describe("The 'watch' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { var watched = 'foobar'; - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/zadd.spec.js b/test/commands/zadd.spec.js index e0153ba11bb..f22963416c9 100644 --- a/test/commands/zadd.spec.js +++ b/test/commands/zadd.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'zadd' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { @@ -34,7 +34,11 @@ describe("The 'zadd' method", function () { client.zrange('infinity', 0, -1, 'WITHSCORES', function (err, res) { assert.equal(res[5], 'inf'); assert.equal(res[1], '-inf'); - assert.equal(res[3], '9.9999999999999992e+22'); + if (process.platform !== 'win32') { + assert.equal(res[3], '9.9999999999999992e+22'); + } else { + assert.equal(res[3], '9.9999999999999992e+022'); + } done(); }); }); diff --git a/test/commands/zscan.spec.js b/test/commands/zscan.spec.js index 6d4a1a60c8a..eb8acf44dbf 100644 --- a/test/commands/zscan.spec.js +++ b/test/commands/zscan.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'zscan' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/commands/zscore.spec.js b/test/commands/zscore.spec.js index 9d24d7a7cf0..8b95e527641 100644 --- a/test/commands/zscore.spec.js +++ b/test/commands/zscore.spec.js @@ -7,9 +7,9 @@ var redis = config.redis; describe("The 'zscore' method", function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; beforeEach(function (done) { diff --git a/test/conf/password.conf b/test/conf/password.conf index c2b8feb4478..3b3c02f346e 100644 --- a/test/conf/password.conf +++ b/test/conf/password.conf @@ -3,3 +3,4 @@ port 6379 bind ::1 127.0.0.1 unixsocket /tmp/redis.sock unixsocketperm 700 +save "" diff --git a/test/conf/redis.conf b/test/conf/redis.conf index fad47a5fc63..9bf706c6543 100644 --- a/test/conf/redis.conf +++ b/test/conf/redis.conf @@ -2,3 +2,4 @@ port 6379 bind ::1 127.0.0.1 unixsocket /tmp/redis.sock unixsocketperm 700 +save "" \ No newline at end of file diff --git a/test/conf/rename.conf b/test/conf/rename.conf index ef839236558..207fe156221 100644 --- a/test/conf/rename.conf +++ b/test/conf/rename.conf @@ -2,6 +2,7 @@ port 6379 bind ::1 127.0.0.1 unixsocket /tmp/redis.sock unixsocketperm 700 +save "" rename-command SET 807081f5afa96845a02816a28b7258c3 rename-command GET f397808a43ceca3963e22b4e13deb672 rename-command GETRANGE 9e3102b15cf231c4e9e940f284744fe0 diff --git a/test/conf/slave.conf b/test/conf/slave.conf index 31cd801c269..f5632bbffcb 100644 --- a/test/conf/slave.conf +++ b/test/conf/slave.conf @@ -4,3 +4,4 @@ unixsocket /tmp/redis6381.sock unixsocketperm 700 slaveof localhost 6379 masterauth porkchopsandwiches +save "" diff --git a/test/connection.spec.js b/test/connection.spec.js index f92dca1949b..827ff69c8aa 100644 --- a/test/connection.spec.js +++ b/test/connection.spec.js @@ -14,6 +14,8 @@ describe('connection tests', function () { client = null; }); afterEach(function () { + if (!client) return; + client.end(true); }); @@ -147,52 +149,17 @@ describe('connection tests', function () { }); - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + 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(); var connect_timeout = 600; // in ms client = redis.createClient({ - parser: parser, connect_timeout: connect_timeout }); var time = 0; @@ -223,7 +190,6 @@ describe('connection tests', function () { it('end connection while retry is still ongoing', function (done) { var connect_timeout = 1000; // in ms client = redis.createClient({ - parser: parser, connect_timeout: connect_timeout }); @@ -271,16 +237,12 @@ 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.'); + client.set('foo', 'bar'); + client.once('error', function (err) { + assert.strictEqual(err.message, 'Redis connection in broken state: retry aborted.'); assert.strictEqual(err.origin.message, 'Connection timeout'); done(); }); @@ -289,27 +251,18 @@ 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) { client = redis.createClient({ 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'); + client.set('foo', 'bar'); + client.once('error', function (err) { + assert.strictEqual(err.message, 'Redis connection in broken state: retry aborted.'); + assert.strictEqual(err.code, 'CONNECTION_BROKEN'); assert.strictEqual(err.origin.code, 'ECONNREFUSED'); done(); }); @@ -337,11 +290,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: retry aborted.'); + assert.strictEqual(err.code, 'CONNECTION_BROKEN'); + unhookIntercept(); + redis.debugMode = false; + done(); + } }); }); }); @@ -351,7 +306,6 @@ describe('connection tests', function () { it('emit an error after the socket timeout exceeded the connect_timeout time', function (done) { var connect_timeout = 500; // in ms client = redis.createClient({ - parser: parser, // Auto detect ipv4 and use non routable ip to trigger the timeout host: '10.255.255.1', connect_timeout: connect_timeout @@ -376,7 +330,7 @@ describe('connection tests', function () { var add = process.platform !== 'win32' ? 15 : 200; var now = Date.now(); assert(now - time < connect_timeout + add, 'The real timeout time should be below ' + (connect_timeout + add) + 'ms but is: ' + (now - time)); - // Timers sometimes trigger early (e.g. 1ms to early) + // Timers sometimes trigger early (e.g. 1ms to early) assert(now - time >= connect_timeout - 5, 'The real timeout time should be above ' + connect_timeout + 'ms, but it is: ' + (now - time)); done(); }); @@ -384,10 +338,10 @@ describe('connection tests', function () { it('use the system socket timeout if the connect_timeout has not been provided', function (done) { client = redis.createClient({ - parser: parser, - host: '2001:db8::ff00:42:8329' // auto detect ip v6 + host: '0:0:0:0:0:0:0:1', // auto detect ip v6 + no_ready_check: true }); - assert.strictEqual(client.address, '2001:db8::ff00:42:8329:6379'); + assert.strictEqual(client.address, '0:0:0:0:0:0:0:1:6379'); assert.strictEqual(client.connection_options.family, 6); process.nextTick(function () { assert.strictEqual(client.stream.listeners('timeout').length, 0); @@ -397,14 +351,25 @@ describe('connection tests', function () { it('clears the socket timeout after a connection has been established', function (done) { client = redis.createClient({ - parser: parser, connect_timeout: 1000 }); process.nextTick(function () { - assert.strictEqual(client.stream._idleTimeout, 1000); + // node > 6 + var timeout = client.stream.timeout; + // node <= 6 + if (timeout === undefined) timeout = client.stream._idleTimeout; + assert.strictEqual(timeout, 1000); }); client.on('connect', function () { - assert.strictEqual(client.stream._idleTimeout, -1); + // node > 6 + var expected = 0; + var timeout = client.stream.timeout; + // node <= 6 + if (timeout === undefined) { + timeout = client.stream._idleTimeout; + expected = -1; + } + assert.strictEqual(timeout, expected); assert.strictEqual(client.stream.listeners('timeout').length, 0); client.on('ready', done); }); @@ -414,7 +379,6 @@ describe('connection tests', function () { client = redis.createClient({ host: 'localhost', port: '6379', - parser: parser, connect_timeout: 1000 }); @@ -427,7 +391,6 @@ describe('connection tests', function () { } client = redis.createClient({ path: '/tmp/redis.sock', - parser: parser, connect_timeout: 1000 }); @@ -580,7 +543,7 @@ describe('connection tests', function () { it('allows connecting with the redis url in the options object and works with protocols other than the redis protocol (e.g. http)', function (done) { client = redis.createClient({ - url: 'http://foo:porkchopsandwiches@' + config.HOST[ip] + '/3' + url: 'http://:porkchopsandwiches@' + config.HOST[ip] + '/3' }); assert.strictEqual(client.auth_pass, 'porkchopsandwiches'); assert.strictEqual(+client.selected_db, 3); diff --git a/test/detect_buffers.spec.js b/test/detect_buffers.spec.js index 2e2f7f1ed02..faa63efb1f1 100644 --- a/test/detect_buffers.spec.js +++ b/test/detect_buffers.spec.js @@ -8,7 +8,7 @@ var redis = config.redis; describe('detect_buffers', function () { var client; - var args = config.configureClient('javascript', 'localhost', { + var args = config.configureClient('localhost', { detect_buffers: true }); @@ -43,7 +43,7 @@ describe('detect_buffers', function () { describe('first argument is a buffer', function () { it('returns a buffer', function (done) { - client.get(new Buffer('string key 1'), function (err, reply) { + client.get(Buffer.from('string key 1'), function (err, reply) { assert.strictEqual(true, Buffer.isBuffer(reply)); assert.strictEqual('', reply.inspect()); return done(err); @@ -51,7 +51,7 @@ describe('detect_buffers', function () { }); it('returns a bufffer when executed as part of transaction', function (done) { - client.multi().get(new Buffer('string key 1')).exec(function (err, reply) { + client.multi().get(Buffer.from('string key 1')).exec(function (err, reply) { assert.strictEqual(1, reply.length); assert.strictEqual(true, Buffer.isBuffer(reply[0])); assert.strictEqual('', reply[0].inspect()); @@ -65,8 +65,8 @@ describe('detect_buffers', function () { it('can interleave string and buffer results', function (done) { client.multi() .hget('hash key 2', 'key 1') - .hget(new Buffer('hash key 2'), 'key 1') - .hget('hash key 2', new Buffer('key 2')) + .hget(Buffer.from('hash key 2'), 'key 1') + .hget('hash key 2', Buffer.from('key 2')) .hget('hash key 2', 'key 2') .exec(function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); @@ -86,8 +86,8 @@ describe('detect_buffers', function () { it('can interleave string and buffer results', function (done) { client.batch() .hget('hash key 2', 'key 1') - .hget(new Buffer('hash key 2'), 'key 1') - .hget('hash key 2', new Buffer('key 2')) + .hget(Buffer.from('hash key 2'), 'key 1') + .hget('hash key 2', Buffer.from('key 2')) .hget('hash key 2', 'key 2') .exec(function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); @@ -150,7 +150,7 @@ describe('detect_buffers', function () { describe('first argument is a buffer', function () { it('returns buffers for keys requested', function (done) { - client.hmget(new Buffer('hash key 2'), 'key 1', 'key 2', function (err, reply) { + client.hmget(Buffer.from('hash key 2'), 'key 1', 'key 2', function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); assert.strictEqual(2, reply.length); assert.strictEqual(true, Buffer.isBuffer(reply[0])); @@ -162,7 +162,7 @@ describe('detect_buffers', function () { }); it('returns buffers for keys requested in transaction', function (done) { - client.multi().hmget(new Buffer('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) { + client.multi().hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); assert.strictEqual(1, reply.length); assert.strictEqual(2, reply[0].length); @@ -175,7 +175,7 @@ describe('detect_buffers', function () { }); it('returns buffers for keys requested in .batch', function (done) { - client.batch().hmget(new Buffer('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) { + client.batch().hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); assert.strictEqual(1, reply.length); assert.strictEqual(2, reply[0].length); @@ -226,7 +226,7 @@ describe('detect_buffers', function () { describe('first argument is a buffer', function () { it('returns buffer values', function (done) { - client.hgetall(new Buffer('hash key 2'), function (err, reply) { + client.hgetall(Buffer.from('hash key 2'), function (err, reply) { assert.strictEqual(null, err); assert.strictEqual('object', typeof reply); assert.strictEqual(2, Object.keys(reply).length); @@ -239,7 +239,7 @@ describe('detect_buffers', function () { }); it('returns buffer values when executed in transaction', function (done) { - client.multi().hgetall(new Buffer('hash key 2')).exec(function (err, reply) { + client.multi().hgetall(Buffer.from('hash key 2')).exec(function (err, reply) { assert.strictEqual(1, reply.length); assert.strictEqual('object', typeof reply[0]); assert.strictEqual(2, Object.keys(reply[0]).length); @@ -252,7 +252,7 @@ describe('detect_buffers', function () { }); it('returns buffer values when executed in .batch', function (done) { - client.batch().hgetall(new Buffer('hash key 2')).exec(function (err, reply) { + client.batch().hgetall(Buffer.from('hash key 2')).exec(function (err, reply) { assert.strictEqual(1, reply.length); assert.strictEqual('object', typeof reply[0]); assert.strictEqual(2, Object.keys(reply[0]).length); diff --git a/test/errors.js b/test/errors.js new file mode 100644 index 00000000000..060afab585a --- /dev/null +++ b/test/errors.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = { + invalidPassword: /^(ERR invalid password|WRONGPASS invalid username-password pair)/, + subscribeUnsubscribeOnly: /^ERR( Can't execute 'get':)? only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/ +}; diff --git a/test/helper.js b/test/helper.js index e851b7f7b35..3e1437d14ab 100644 --- a/test/helper.js +++ b/test/helper.js @@ -161,13 +161,7 @@ module.exports = { cb = opts; opts = {}; } - var parsers = ['javascript']; var protocols = ['IPv4']; - // The js parser works the same as the hiredis parser, just activate this if you want to be on the safe side - // try { - // require('hiredis'); - // parsers.push('hiredis'); - // } catch (e) {/* ignore eslint */} if (process.platform !== 'win32') { protocols.push('IPv6', '/tmp/redis.sock'); } @@ -185,13 +179,11 @@ module.exports = { } } describe('using options: ' + strOptions, function () { - parsers.forEach(function (parser) { - protocols.forEach(function (ip, i) { - if (i !== 0 && !opts.allConnections) { - return; - } - cb(parser, ip, config.configureClient(parser, ip, options)); - }); + protocols.forEach(function (ip, i) { + if (i !== 0 && !opts.allConnections) { + return; + } + cb(ip, config.configureClient(ip, options)); }); }); }); diff --git a/test/lib/config.js b/test/lib/config.js index 0420b032ce3..d363f83e04b 100644 --- a/test/lib/config.js +++ b/test/lib/config.js @@ -16,7 +16,7 @@ var config = { IPv4: '127.0.0.1', IPv6: '::1' }, - configureClient: function (parser, ip, opts) { + configureClient: function (ip, opts) { var args = []; // Do not manipulate the opts => copy them each time opts = opts ? JSON.parse(JSON.stringify(opts)) : {}; @@ -28,8 +28,6 @@ var config = { args.push(config.HOST[ip]); opts.family = ip; } - - opts.parser = parser; args.push(opts); return args; diff --git a/test/lib/redis-process.js b/test/lib/redis-process.js index e98f7fbffcd..23ff2e18153 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'); @@ -17,7 +17,7 @@ function waitForRedis (available, cb, port) { var running = false; var socket = '/tmp/redis.sock'; if (port) { - // We have to distinguishe the redis sockets if we have more than a single redis instance running + // We have to distinguish the redis sockets if we have more than a single redis instance running socket = '/tmp/redis' + port + '.sock'; } port = port || config.PORT; @@ -27,20 +27,21 @@ function waitForRedis (available, cb, port) { bluebird.join( tcpPortUsed.check(port, '127.0.0.1'), tcpPortUsed.check(port, '::1'), - function (ipV4, ipV6) { - if (ipV6 === available && ipV4 === available) { - if (fs.existsSync(socket) === available) { - clearInterval(id); - return cb(); + function (ipV4, ipV6) { + if (ipV6 === available && ipV4 === available) { + if (fs.existsSync(socket) === available) { + clearInterval(id); + return cb(); + } + // The same message applies for can't stop but we ignore that case + throw new Error('Port ' + port + ' is already in use. Tests can\'t start.\n'); } - // The same message applies for can't stop but we ignore that case - throw new Error('Port ' + port + ' is already in use. Tests can\'t start.\n'); - } - if (Date.now() - time > 6000) { - throw new Error('Redis could not start on port ' + (port || config.PORT) + '\n'); + if (Date.now() - time > 6000) { + throw new Error('Redis could not start on port ' + (port || config.PORT) + '\n'); + } + running = false; } - running = false; - }).catch(function (err) { + ).catch(function (err) { console.error('\x1b[31m' + err.stack + '\x1b[0m\n'); process.exit(1); }); @@ -50,6 +51,14 @@ function waitForRedis (available, cb, port) { module.exports = { start: function (done, conf, port) { var spawnFailed = false; + if (process.platform === 'win32') return done(null, { + spawnFailed: function () { + return spawnFailed; + }, + stop: function (done) { + return done(); + } + }); // spawn redis with our testing configuration. var confFile = conf || path.resolve(__dirname, '../conf/redis.conf'); var rp = spawn('redis-server', [confFile], {}); @@ -57,7 +66,10 @@ module.exports = { // capture a failure booting redis, and give // the user running the test some directions. rp.once('exit', function (code) { - if (code !== 0) spawnFailed = true; + if (code !== 0) { + spawnFailed = true; + throw new Error('TESTS: Redis Spawn Failed'); + } }); // wait for redis to become available, by diff --git a/test/multi.spec.js b/test/multi.spec.js index e654375dc8d..5b0e801c875 100644 --- a/test/multi.spec.js +++ b/test/multi.spec.js @@ -46,7 +46,7 @@ describe("The 'multi' method", function () { } var json = JSON.stringify(test_arr); - zlib.deflate(new Buffer(json), function (err, buffer) { + zlib.deflate(Buffer.from(json), function (err, buffer) { if (err) { done(err); return; @@ -93,9 +93,9 @@ describe("The 'multi' method", function () { }); - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { describe('when not connected', function () { @@ -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: retry aborted/.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(); } }); @@ -379,12 +389,12 @@ describe("The 'multi' method", function () { ['del', 'some set'], ['smembers', 'some set'] ]) - .scard('some set') - .exec(function (err, replies) { - assert.strictEqual(4, replies[0].length); - assert.strictEqual(0, replies[2].length); - return done(); - }); + .scard('some set') + .exec(function (err, replies) { + assert.strictEqual(4, replies[0].length); + assert.strictEqual(0, replies[2].length); + return done(); + }); }); it('allows multiple operations to be performed using constructor with all kinds of syntax', function (done) { @@ -406,30 +416,30 @@ describe("The 'multi' method", function () { ['HMSET', 'multihmset', ['multibar', 'multibaz'], undefined], // undefined is used as a explicit not set callback variable ['hmset', 'multihmset', ['multibar', 'multibaz'], helper.isString('OK')], ]) - .hmget(now, 123456789, 'otherTypes') - .hmget('key2', arr2, function noop () {}) - .hmget(['multihmset2', 'some manner of key', 'multibar3']) - .mget('multifoo2', ['multifoo3', 'multifoo'], function (err, res) { - assert(res[0], 'multifoo3'); - assert(res[1], 'multifoo'); - called = true; - }) - .exec(function (err, replies) { - assert(called); - assert.equal(arr.length, 3); - assert.equal(arr2.length, 2); - assert.equal(arr3.length, 3); - assert.equal(arr4.length, 3); - assert.strictEqual(null, err); - assert.equal(replies[10][1], '555'); - assert.equal(replies[11][0], 'a type of value'); - assert.strictEqual(replies[12][0], null); - assert.equal(replies[12][1], 'test'); - assert.equal(replies[13][0], 'multibar2'); - assert.equal(replies[13].length, 3); - assert.equal(replies.length, 14); - return done(); - }); + .hmget(now, 123456789, 'otherTypes') + .hmget('key2', arr2, function noop () {}) + .hmget(['multihmset2', 'some manner of key', 'multibar3']) + .mget('multifoo2', ['multifoo3', 'multifoo'], function (err, res) { + assert(res[0], 'multifoo3'); + assert(res[1], 'multifoo'); + called = true; + }) + .exec(function (err, replies) { + assert(called); + assert.equal(arr.length, 3); + assert.equal(arr2.length, 2); + assert.equal(arr3.length, 3); + assert.equal(arr4.length, 3); + assert.strictEqual(null, err); + assert.equal(replies[10][1], '555'); + assert.equal(replies[11][0], 'a type of value'); + assert.strictEqual(replies[12][0], null); + assert.equal(replies[12][1], 'test'); + assert.equal(replies[13][0], 'multibar2'); + assert.equal(replies[13].length, 3); + assert.equal(replies.length, 14); + return done(); + }); }); it('converts a non string key to a string', function (done) { @@ -505,11 +515,11 @@ describe("The 'multi' method", function () { ['mget', ['multifoo', 'some', 'random value', 'keys']], ['incr', 'multifoo'] ]) - .exec(function (err, replies) { - assert.strictEqual(replies.length, 2); - assert.strictEqual(replies[0].length, 4); - return done(); - }); + .exec(function (err, replies) { + assert.strictEqual(replies.length, 2); + assert.strictEqual(replies[0].length, 4); + return done(); + }); }); it('allows multiple operations to be performed on a hash', function (done) { diff --git a/test/node_redis.spec.js b/test/node_redis.spec.js index d9fbc097284..12ad70d5ef6 100644 --- a/test/node_redis.spec.js +++ b/test/node_redis.spec.js @@ -2,6 +2,7 @@ var assert = require('assert'); var fs = require('fs'); +var util = require('util'); var path = require('path'); var intercept = require('intercept-stdout'); var config = require('./lib/config'); @@ -10,8 +11,43 @@ var fork = require('child_process').fork; var redis = config.redis; var client; +// Currently GitHub Actions on Windows (and event travis) builds hang after completing if +// any processes are still running, we shutdown redis-server after all tests complete (can't do this in a +// `after_script` hook as it hangs before the `after` life cycles) to workaround the issue. +after(function (done) { + if (process.platform !== 'win32' || !process.env.GITHUB_ACTION) { + return done(); + } + setTimeout(function () { + require('cross-spawn').sync('redis-server', ['--service-stop'], {}); + done(); + }, 2000); +}); + describe('The node_redis client', function () { + describe("The 'add_command' method", function () { + + var Redis = redis.RedisClient; + + it('camel case and snakeCase version exists', function () { + assert.strictEqual(typeof redis.addCommand, 'function'); + assert.strictEqual(typeof redis.add_command, 'function'); + }); + + it('converts special characters in functions names to lowercase', function () { + var command = 'really-new.command'; + assert.strictEqual(Redis.prototype[command], undefined); + redis.addCommand(command); + if (Redis.prototype[command].name !== '') { + assert.strictEqual(Redis.prototype[command].name, 'really_new_command'); + assert.strictEqual(Redis.prototype[command.toUpperCase()].name, 'really_new_command'); + assert.strictEqual(Redis.prototype.really_new_command.name, 'really_new_command'); + assert.strictEqual(Redis.prototype.REALLY_NEW_COMMAND.name, 'really_new_command'); + } + }); + }); + it('individual commands sanity check', function (done) { // All commands should work the same in multi context or without // Therefor individual commands always have to be handled in both cases @@ -47,18 +83,19 @@ describe('The node_redis client', function () { client.once('reconnecting', function () { process.nextTick(function () { assert.strictEqual(client.reply_parser.buffer, null); + client.end(true); done(); }); }); - var partialInput = new Buffer('$100\r\nabcdef'); + var partialInput = Buffer.from('$100\r\nabcdef'); client.reply_parser.execute(partialInput); assert.strictEqual(client.reply_parser.buffer.inspect(), partialInput.inspect()); client.stream.destroy(); }); - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { afterEach(function () { client.end(true); @@ -98,11 +135,14 @@ describe('The node_redis client', function () { }); it('check if all new options replaced the old ones', function (done) { + client.selected_db = 1; var client2 = client.duplicate({ + db: 2, no_ready_check: true }); assert(client.connected); assert(!client2.connected); + assert.notEqual(client.selected_db, client2.selected_db); assert.strictEqual(client.options.no_ready_check, undefined); assert.strictEqual(client2.options.no_ready_check, true); assert.notDeepEqual(client.options, client2.options); @@ -334,7 +374,11 @@ describe('The node_redis client', function () { it('send_command with callback as args', function (done) { client.send_command('abcdef', function (err, res) { - assert.strictEqual(err.message, "ERR unknown command 'abcdef'"); + if (process.platform === 'win32') { + assert.strictEqual(err.message, "ERR unknown command 'abcdef'"); + } else { + assert.strictEqual(err.message, 'ERR unknown command `abcdef`, with args beginning with: '); + } done(); }); }); @@ -345,7 +389,6 @@ describe('The node_redis client', function () { it('should retry all commands instead of returning an error if a command did not yet return after a connection loss', function (done) { var bclient = redis.createClient({ - parser: parser, retry_unfulfilled_commands: true }); bclient.blpop('blocking list 2', 5, function (err, value) { @@ -364,7 +407,6 @@ describe('The node_redis client', function () { it('should retry all commands even if the offline queue is disabled', function (done) { var bclient = redis.createClient({ - parser: parser, enableOfflineQueue: false, retryUnfulfilledCommands: true }); @@ -490,11 +532,11 @@ describe('The node_redis client', function () { // TODO: Investigate why this test is failing hard and killing mocha if using '/tmp/redis.sock'. // Seems like something is wrong with nyc while passing a socket connection to create client! - client = redis.createClient(); - client.quit(function () { - client.get('foo', function (err, res) { + var client2 = redis.createClient(); + client2.quit(function () { + client2.get('foo', function (err, res) { assert.strictEqual(err.message, 'Stream connection ended and command aborted. It might have been processed.'); - assert.strictEqual(client.offline_queue.length, 0); + assert.strictEqual(client2.offline_queue.length, 0); done(); }); }); @@ -650,15 +692,22 @@ describe('The node_redis client', function () { done(); }); }); - require('domain').create(); }); it('catches all errors from within the domain', function (done) { var domain = require('domain').create(); domain.run(function () { - // Trigger an error within the domain + if (process.versions.node.split('.')[0] >= 13) { + // Node >= 13 + // Recreate client in domain so error handlers run this domain + // Changed in: "error handler runs outside of its domain" + // https://github.com/nodejs/node/pull/26211 + client.end(true); // make sure to close current client + client = redis.createClient(); + } client.end(true); + // Trigger an error within the domain client.set('domain', 'value'); }); @@ -671,25 +720,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 = 'ಠ_ಠ'; @@ -734,7 +764,7 @@ describe('The node_redis client', function () { }); }); - // TODO: consider allowing loading commands in v.3 + // TODO: consider allowing loading commands in v.4 // it('should fire early', function (done) { // client = redis.createClient.apply(null, args); // var fired = false; @@ -757,111 +787,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) { @@ -879,7 +804,7 @@ describe('The node_redis client', function () { }); client.once('ready', function () { client.set('foo', 'bar', function (err, res) { - assert.strictEqual(err.message, 'Fatal error encountert. Command aborted. It might have been processed.'); + assert.strictEqual(err.message, 'Fatal error encountered. Command aborted. It might have been processed.'); assert.strictEqual(err.code, 'NR_FATAL'); assert(err instanceof redis.AbortError); error = err.origin; @@ -888,7 +813,7 @@ describe('The node_redis client', function () { // ready is called in a reply process.nextTick(function () { // Fail the set answer. Has no corresponding command obj and will therefore land in the error handler and set - client.reply_parser.execute(new Buffer('a*1\r*1\r$1`zasd\r\na')); + client.reply_parser.execute(Buffer.from('a*1\r*1\r$1`zasd\r\na')); }); }); }); @@ -896,33 +821,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 @@ -944,8 +845,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; @@ -961,7 +866,14 @@ describe('The node_redis client', function () { } } else { assert.equal(err.code, 'ECONNREFUSED'); - assert.equal(err.errno, 'ECONNREFUSED'); + + if (typeof err.errno === 'number') { + // >= Node 13 + assert.equal(util.getSystemErrorName(err.errno), 'ECONNREFUSED'); + } else { + // < Node 13 + assert.equal(err.errno, 'ECONNREFUSED'); + } assert.equal(err.syscall, 'connect'); } }); @@ -985,9 +897,7 @@ describe('The node_redis client', function () { }); it('flushes the command queue if connection is lost', function (done) { - client = redis.createClient({ - parser: parser - }); + client = redis.createClient(); client.once('ready', function () { var multi = client.multi(); @@ -1021,7 +931,13 @@ describe('The node_redis client', function () { end(); } else { assert.equal(err.code, 'ECONNREFUSED'); - assert.equal(err.errno, 'ECONNREFUSED'); + if (typeof err.errno === 'number') { + // >= Node 13 + assert.equal(util.getSystemErrorName(err.errno), 'ECONNREFUSED'); + } else { + // < Node 13 + assert.equal(err.errno, 'ECONNREFUSED'); + } assert.equal(err.syscall, 'connect'); end(); } @@ -1033,7 +949,6 @@ describe('The node_redis client', function () { it('stream not writable', function (done) { client = redis.createClient({ - parser: parser, enable_offline_queue: false }); client.on('ready', function () { @@ -1047,7 +962,6 @@ describe('The node_redis client', function () { it('emit an error and does not enqueues operation', function (done) { client = redis.createClient(9999, null, { - parser: parser, max_attempts: 0, enable_offline_queue: false }); @@ -1072,7 +986,6 @@ describe('The node_redis client', function () { it('flushes the command queue if connection is lost', function (done) { client = redis.createClient({ - parser: parser, enable_offline_queue: false }); @@ -1107,7 +1020,13 @@ describe('The node_redis client', function () { end(); } else { assert.equal(err.code, 'ECONNREFUSED'); - assert.equal(err.errno, 'ECONNREFUSED'); + if (typeof err.errno === 'number') { + // >= Node 13 + assert.equal(util.getSystemErrorName(err.errno), 'ECONNREFUSED'); + } else { + // < Node 13 + assert.equal(err.errno, 'ECONNREFUSED'); + } assert.equal(err.syscall, 'connect'); redis.debug_mode = false; client.end(true); diff --git a/test/prefix.spec.js b/test/prefix.spec.js index e34805a7a9b..52fd39c1ccd 100644 --- a/test/prefix.spec.js +++ b/test/prefix.spec.js @@ -7,14 +7,13 @@ var redis = config.redis; describe('prefix key names', function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client = null; beforeEach(function (done) { client = redis.createClient({ - parser: parser, prefix: 'test:prefix:' }); client.on('ready', function () { diff --git a/test/pubsub.spec.js b/test/pubsub.spec.js index 94b0fc1880c..34e93f37f2c 100644 --- a/test/pubsub.spec.js +++ b/test/pubsub.spec.js @@ -3,13 +3,14 @@ var assert = require('assert'); var config = require('./lib/config'); var helper = require('./helper'); +var errors = require('./errors'); var redis = config.redis; describe('publish/subscribe', function () { - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var pub = null; var sub = null; var channel = 'test channel'; @@ -124,7 +125,7 @@ describe('publish/subscribe', function () { detect_buffers: true }); sub.on('subscribe', function (chnl, count) { - if (chnl.inspect() === new Buffer([0xAA, 0xBB, 0x00, 0xF0]).inspect()) { + if (chnl.inspect() === Buffer.from([0xAA, 0xBB, 0x00, 0xF0]).inspect()) { assert.equal(1, count); if (a) { return done(); @@ -137,7 +138,7 @@ describe('publish/subscribe', function () { a = true; }); - sub.subscribe(new Buffer([0xAA, 0xBB, 0x00, 0xF0]), channel2); + sub.subscribe(Buffer.from([0xAA, 0xBB, 0x00, 0xF0]), channel2); }); it('receives messages on subscribed channel', function (done) { @@ -262,13 +263,13 @@ describe('publish/subscribe', function () { }); it('handles multiple channels with the same channel name properly, even with buffers', function (done) { - var channels = ['a', 'b', 'a', new Buffer('a'), 'c', 'b']; + var channels = ['a', 'b', 'a', Buffer.from('a'), 'c', 'b']; var subscribed_channels = [1, 2, 2, 2, 3, 3]; var i = 0; sub.subscribe(channels); sub.on('subscribe', function (channel, count) { if (Buffer.isBuffer(channel)) { - assert.strictEqual(channel.inspect(), new Buffer(channels[i]).inspect()); + assert.strictEqual(channel.inspect(), Buffer.from(channels[i]).inspect()); } else { assert.strictEqual(channel, channels[i].toString()); } @@ -420,7 +421,7 @@ describe('publish/subscribe', function () { }); subscribe(['prefix:*', 'prefix:3'], function () { - pub.publish('prefix:1', new Buffer('test'), function () { + pub.publish('prefix:1', Buffer.from('test'), function () { subscribe(['prefix:2']); subscribe(['5', 'test:a', 'bla'], function () { assert(all); @@ -494,9 +495,9 @@ describe('publish/subscribe', function () { sub2.batch().psubscribe('*', helper.isString('*')).exec(); sub2.subscribe('/foo'); sub2.on('pmessage', function (pattern, channel, message) { - assert.strictEqual(pattern.inspect(), new Buffer('*').inspect()); - assert.strictEqual(channel.inspect(), new Buffer('/foo').inspect()); - assert.strictEqual(message.inspect(), new Buffer('hello world').inspect()); + assert.strictEqual(pattern.inspect(), Buffer.from('*').inspect()); + assert.strictEqual(channel.inspect(), Buffer.from('/foo').inspect()); + assert.strictEqual(message.inspect(), Buffer.from('hello world').inspect()); sub2.quit(done); }); pub.pubsub('numsub', '/foo', function (err, res) { @@ -515,19 +516,17 @@ describe('publish/subscribe', function () { sub.stream.once('data', function () { assert.strictEqual(sub.message_buffers, false); assert.strictEqual(sub.shouldBuffer, false); - sub.on('pmessageBuffer', function (pattern, channel, message) { - if (parser !== 'javascript' && typeof pattern === 'string') { - pattern = new Buffer(pattern); - channel = new Buffer(channel); + sub.on('pmessageBuffer', function (pattern, channel) { + if (typeof pattern === 'string') { + pattern = Buffer.from(pattern); + channel = Buffer.from(channel); } - assert.strictEqual(pattern.inspect(), new Buffer('*').inspect()); - assert.strictEqual(channel.inspect(), new Buffer('/foo').inspect()); + assert.strictEqual(pattern.inspect(), Buffer.from('*').inspect()); + assert.strictEqual(channel.inspect(), Buffer.from('/foo').inspect()); sub.quit(end); }); - if (parser === 'javascript') { - assert.notStrictEqual(sub.message_buffers, sub.buffers); - } - + // Either message_buffers or buffers has to be true, but not both at the same time + assert.notStrictEqual(sub.message_buffers, sub.buffers); }); var batch = sub.batch(); batch.psubscribe('*'); @@ -589,7 +588,7 @@ describe('publish/subscribe', function () { }); // Get is forbidden sub.get('foo', function (err, res) { - assert(/^ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message)); + assert.ok(errors.subscribeUnsubscribeOnly.test(err.message)); assert.strictEqual(err.command, 'GET'); }); // Quit is allowed @@ -599,7 +598,7 @@ describe('publish/subscribe', function () { it('emit error if only pub sub commands are allowed without callback', function (done) { sub.subscribe('channel'); sub.on('error', function (err) { - assert(/^ERR only \(P\)SUBSCRIBE \/ \(P\)UNSUBSCRIBE/.test(err.message)); + assert.ok(errors.subscribeUnsubscribeOnly.test(err.message)); assert.strictEqual(err.command, 'GET'); done(); }); diff --git a/test/rename.spec.js b/test/rename.spec.js index b98661e9f5f..68adc5699f0 100644 --- a/test/rename.spec.js +++ b/test/rename.spec.js @@ -10,16 +10,18 @@ if (process.platform === 'win32') { return; } -describe('rename commands', function () { +// TODO these tests are causing flakey tests - looks like redis-server is not +// being started with new configuration after or before these tests +xdescribe('rename commands', function () { before(function (done) { helper.stopRedis(function () { helper.startRedis('./conf/rename.conf', done); }); }); - helper.allTests(function (parser, ip, args) { + helper.allTests(function (ip, args) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client = null; beforeEach(function (done) { @@ -29,7 +31,6 @@ describe('rename commands', function () { set: '807081f5afa96845a02816a28b7258c3', GETRANGE: '9e3102b15cf231c4e9e940f284744fe0' }, - parser: parser }); client.on('ready', function () { @@ -50,7 +51,7 @@ describe('rename commands', function () { }); client.get('key', function (err, reply) { - assert.strictEqual(err.message, "ERR unknown command 'get'"); + assert.strictEqual(err.message, 'ERR unknown command `get`, with args beginning with: `key`, '); assert.strictEqual(err.command, 'GET'); assert.strictEqual(reply, undefined); }); @@ -108,7 +109,7 @@ describe('rename commands', function () { multi.exec(function (err, res) { assert(err); assert.strictEqual(err.message, 'EXECABORT Transaction discarded because of previous errors.'); - assert.strictEqual(err.errors[0].message, "ERR unknown command 'get'"); + assert.strictEqual(err.errors[0].message, 'ERR unknown command `get`, with args beginning with: `key`, '); assert.strictEqual(err.errors[0].command, 'GET'); assert.strictEqual(err.code, 'EXECABORT'); assert.strictEqual(err.errors[0].code, 'ERR'); @@ -124,7 +125,6 @@ describe('rename commands', function () { rename_commands: { set: '807081f5afa96845a02816a28b7258c3' }, - parser: parser, prefix: 'baz' }); client.set('foo', 'bar'); diff --git a/test/return_buffers.spec.js b/test/return_buffers.spec.js index eb18d79393f..22efb31a04f 100644 --- a/test/return_buffers.spec.js +++ b/test/return_buffers.spec.js @@ -7,11 +7,11 @@ var redis = config.redis; describe('return_buffers', function () { - helper.allTests(function (parser, ip, basicArgs) { + helper.allTests(function (ip, basicArgs) { - describe('using ' + parser + ' and ' + ip, function () { + describe('using ' + ip, function () { var client; - var args = config.configureClient(parser, ip, { + var args = config.configureClient(ip, { return_buffers: true, detect_buffers: true }); @@ -40,6 +40,10 @@ describe('return_buffers', function () { }); }); + afterEach(function () { + client.end(true); + }); + describe('get', function () { describe('first argument is a string', function () { it('returns a buffer', function (done) { @@ -65,8 +69,8 @@ describe('return_buffers', function () { it('returns buffers', function (done) { client.multi() .hget('hash key 2', 'key 1') - .hget(new Buffer('hash key 2'), 'key 1') - .hget('hash key 2', new Buffer('key 2')) + .hget(Buffer.from('hash key 2'), 'key 1') + .hget('hash key 2', Buffer.from('key 2')) .hget('hash key 2', 'key 2') .exec(function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); @@ -87,8 +91,8 @@ describe('return_buffers', function () { it('returns buffers', function (done) { client.batch() .hget('hash key 2', 'key 1') - .hget(new Buffer('hash key 2'), 'key 1') - .hget('hash key 2', new Buffer('key 2')) + .hget(Buffer.from('hash key 2'), 'key 1') + .hget('hash key 2', Buffer.from('key 2')) .hget('hash key 2', 'key 2') .exec(function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); @@ -121,7 +125,7 @@ describe('return_buffers', function () { describe('first argument is a buffer', function () { it('returns buffers for keys requested', function (done) { - client.hmget(new Buffer('hash key 2'), 'key 1', 'key 2', function (err, reply) { + client.hmget(Buffer.from('hash key 2'), 'key 1', 'key 2', function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); assert.strictEqual(2, reply.length); assert.strictEqual(true, Buffer.isBuffer(reply[0])); @@ -133,7 +137,7 @@ describe('return_buffers', function () { }); it('returns buffers for keys requested in transaction', function (done) { - client.multi().hmget(new Buffer('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) { + client.multi().hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); assert.strictEqual(1, reply.length); assert.strictEqual(2, reply[0].length); @@ -146,7 +150,7 @@ describe('return_buffers', function () { }); it('returns buffers for keys requested in .batch', function (done) { - client.batch().hmget(new Buffer('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) { + client.batch().hmget(Buffer.from('hash key 2'), 'key 1', 'key 2').exec(function (err, reply) { assert.strictEqual(true, Array.isArray(reply)); assert.strictEqual(1, reply.length); assert.strictEqual(2, reply[0].length); @@ -197,7 +201,7 @@ describe('return_buffers', function () { describe('first argument is a buffer', function () { it('returns buffer values', function (done) { - client.hgetall(new Buffer('hash key 2'), function (err, reply) { + client.hgetall(Buffer.from('hash key 2'), function (err, reply) { assert.strictEqual(null, err); assert.strictEqual('object', typeof reply); assert.strictEqual(2, Object.keys(reply).length); @@ -210,7 +214,7 @@ describe('return_buffers', function () { }); it('returns buffer values when executed in transaction', function (done) { - client.multi().hgetall(new Buffer('hash key 2')).exec(function (err, reply) { + client.multi().hgetall(Buffer.from('hash key 2')).exec(function (err, reply) { assert.strictEqual(1, reply.length); assert.strictEqual('object', typeof reply[0]); assert.strictEqual(2, Object.keys(reply[0]).length); @@ -223,7 +227,7 @@ describe('return_buffers', function () { }); it('returns buffer values when executed in .batch', function (done) { - client.batch().hgetall(new Buffer('hash key 2')).exec(function (err, reply) { + client.batch().hgetall(Buffer.from('hash key 2')).exec(function (err, reply) { assert.strictEqual(1, reply.length); assert.strictEqual('object', typeof reply[0]); assert.strictEqual(2, Object.keys(reply[0]).length); @@ -241,9 +245,9 @@ describe('return_buffers', function () { var pub; var sub; var channel = 'test channel'; - var message = new Buffer('test message'); + var message = Buffer.from('test message'); - var args = config.configureClient(parser, ip, { + var args = config.configureClient(ip, { return_buffers: true }); diff --git a/test/tls.spec.js b/test/tls.spec.js index d977ee7d9ac..127a2cfb8de 100644 --- a/test/tls.spec.js +++ b/test/tls.spec.js @@ -7,6 +7,7 @@ var helper = require('./helper'); var path = require('path'); var redis = config.redis; var utils = require('../lib/utils'); +var tls = require('tls'); var tls_options = { servername: 'redis.js.org', @@ -18,9 +19,6 @@ var tls_port = 6380; // Use skip instead of returning to indicate what tests really got skipped var skip = false; -// Wait until stunnel4 is in the travis whitelist -// Check: https://github.com/travis-ci/apt-package-whitelist/issues/403 -// If this is merged, remove the travis env checks describe('TLS connection tests', function () { before(function (done) { @@ -28,9 +26,6 @@ describe('TLS connection tests', function () { if (process.platform === 'win32') { skip = true; console.warn('\nStunnel tests do not work on windows atm. If you think you can fix that, it would be warmly welcome.\n'); - } else if (process.env.TRAVIS === 'true') { - skip = true; - console.warn('\nTravis does not support stunnel right now. Skipping tests.\nCheck: https://github.com/travis-ci/apt-package-whitelist/issues/403\n'); } if (skip) return done(); helper.stopStunnel(function () { @@ -57,7 +52,7 @@ describe('TLS connection tests', function () { client = redis.createClient({ connect_timeout: connect_timeout, port: tls_port, - tls: tls_options + tls: utils.clone(tls_options) }); var time = 0; assert.strictEqual(client.address, '127.0.0.1:' + tls_port); @@ -90,12 +85,12 @@ describe('TLS connection tests', function () { it('connect with host and port provided in the tls object', function (done) { if (skip) this.skip(); - var tls = utils.clone(tls_options); - tls.port = tls_port; - tls.host = 'localhost'; + var tls_opts = utils.clone(tls_options); + tls_opts.port = tls_port; + tls_opts.host = 'localhost'; client = redis.createClient({ connect_timeout: 1000, - tls: tls + tls: tls_opts }); // verify connection is using TCP, not UNIX socket @@ -108,6 +103,30 @@ describe('TLS connection tests', function () { client.get('foo', helper.isString('bar', done)); }); + describe('using rediss as url protocol', function () { + var tls_connect = tls.connect; + beforeEach(function () { + tls.connect = function (options) { + options = utils.clone(options); + options.ca = tls_options.ca; + options.servername = 'redis.js.org'; + options.rejectUnauthorized = true; + return tls_connect.call(tls, options); + }; + }); + afterEach(function () { + tls.connect = tls_connect; + }); + it('connect with tls when rediss is used as the protocol', function (done) { + if (skip) this.skip(); + client = redis.createClient('rediss://localhost:' + tls_port); + // verify connection is using TCP, not UNIX socket + assert(client.stream.encrypted); + client.set('foo', 'bar'); + client.get('foo', helper.isString('bar', done)); + }); + }); + it('fails to connect because the cert is not correct', function (done) { if (skip) this.skip(); var faulty_cert = utils.clone(tls_options); diff --git a/test/unify_options.spec.js b/test/unify_options.spec.js index eb441d81a66..dcdd46d330b 100644 --- a/test/unify_options.spec.js +++ b/test/unify_options.spec.js @@ -209,7 +209,7 @@ describe('createClient options', function () { }, undefined); throw new Error('failed'); } catch (err) { - assert.strictEqual(err.message, 'To many arguments passed to createClient. Please only pass the options object'); + assert.strictEqual(err.message, 'Too many arguments passed to createClient. Please only pass the options object'); } }); @@ -218,7 +218,7 @@ describe('createClient options', function () { option: [1, 2, 3], url: '//hm:abc@localhost:123/3' }); - assert.strictEqual(Object.keys(options).length, 6); + assert.strictEqual(Object.keys(options).length, 7); assert.strictEqual(options.option.length, 3); assert.strictEqual(options.host, 'localhost'); assert.strictEqual(options.port, '123'); diff --git a/test/utils.spec.js b/test/utils.spec.js index e036d84167c..592600fb6d8 100644 --- a/test/utils.spec.js +++ b/test/utils.spec.js @@ -1,7 +1,7 @@ 'use strict'; var assert = require('assert'); -var Queue = require('double-ended-queue'); +var Queue = require('denque'); var utils = require('../lib/utils'); var intercept = require('intercept-stdout'); @@ -131,17 +131,22 @@ describe('utils.js', function () { }); it('elements in the offline queue. Reply after the offline queue is empty and respect the command_obj callback', function (done) { - clientMock.offline_queue.push(create_command_obj(), create_command_obj()); + clientMock.offline_queue.push(create_command_obj()); + clientMock.offline_queue.push(create_command_obj()); utils.reply_in_order(clientMock, function () { assert.strictEqual(clientMock.offline_queue.length, 0); assert.strictEqual(res_count, 2); done(); }, null, null); - while (clientMock.offline_queue.length) clientMock.offline_queue.shift().callback(null, 'foo'); + while (clientMock.offline_queue.length) { + clientMock.offline_queue.shift().callback(null, 'foo'); + } }); it('elements in the offline queue. Reply after the offline queue is empty and respect the command_obj error emit', function (done) { - clientMock.command_queue.push({}, create_command_obj(), {}); + clientMock.command_queue.push({}); + clientMock.command_queue.push(create_command_obj()); + clientMock.command_queue.push({}); utils.reply_in_order(clientMock, function () { assert.strictEqual(clientMock.command_queue.length, 0); assert(emitted); @@ -158,8 +163,10 @@ describe('utils.js', function () { }); it('elements in the offline queue and the command_queue. Reply all other commands got handled respect the command_obj', function (done) { - clientMock.command_queue.push(create_command_obj(), create_command_obj()); - clientMock.offline_queue.push(create_command_obj(), {}); + clientMock.command_queue.push(create_command_obj()); + clientMock.command_queue.push(create_command_obj()); + clientMock.command_queue.push(create_command_obj()); + clientMock.offline_queue.push({}); utils.reply_in_order(clientMock, function (err, res) { assert.strictEqual(clientMock.command_queue.length, 0); assert.strictEqual(clientMock.offline_queue.length, 0);