Skip to content

Commit

Permalink
feat: support nesting options via redis://
Browse files Browse the repository at this point in the history
Close: #940
  • Loading branch information
luin committed Aug 8, 2019
1 parent c1f0d03 commit 3aea10f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 16 deletions.
37 changes: 25 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ used in the world's biggest online commerce company [Alibaba](http://www.alibaba

0. Full-featured. It supports [Cluster](http://redis.io/topics/cluster-tutorial), [Sentinel](http://redis.io/topics/sentinel), [Pipelining](http://redis.io/topics/pipelining) and of course [Lua scripting](http://redis.io/commands/eval) & [Pub/Sub](http://redis.io/topics/pubsub) (with the support of binary messages).
1. High performance.
2. Delightful API. It works with Node callbacks and Native promises.
3. Transformation of command arguments and replies.
4. Transparent key prefixing.
5. Abstraction for Lua scripting, allowing you to define custom commands.
6. Support for binary data.
7. Support for TLS.
8. Support for offline queue and ready checking.
9. Support for ES6 types, such as `Map` and `Set`.
10. Support for GEO commands (Redis 3.2 Unstable).
11. Sophisticated error handling strategy.
12. Support for NAT mapping.
1. Delightful API. It works with Node callbacks and Native promises.
1. Transformation of command arguments and replies.
1. Transparent key prefixing.
1. Abstraction for Lua scripting, allowing you to define custom commands.
1. Support for binary data.
1. Support for TLS.
1. Support for offline queue and ready checking.
1. Support for ES6 types, such as `Map` and `Set`.
1. Support for GEO commands (Redis 3.2 Unstable).
1. Sophisticated error handling strategy.
1. Support for NAT mapping.

# Links

Expand Down Expand Up @@ -125,6 +125,19 @@ You can also specify connection options as a [`redis://` URL](http://www.iana.or
```javascript
// Connect to 127.0.0.1:6380, db 4, using password "authpassword":
new Redis("redis://:[email protected]:6380/4");

// Options can be passed via url query. Nesting support is provided.
// The following two are equal:
new Redis("redis://:[email protected]:6380/4?tls[ca]=aW9yZWRpcwo%3D");
new Redis({
host: "127.0.0.1",
port: 6380,
password: "authpassword",
db: 4,
tls: {
ca: "aW9yZWRpcwo="
}
});
```

See [API Documentation](API.md#new_Redis) for all available options.
Expand Down Expand Up @@ -948,7 +961,7 @@ However there are some differences when using transaction and pipeline in Cluste

0. All keys in a pipeline should belong to the same slot since ioredis sends all commands in a pipeline to the same node.
1. You can't use `multi` without pipeline (aka `cluster.multi({ pipeline: false })`). This is because when you call `cluster.multi({ pipeline: false })`, ioredis doesn't know which node the `multi` command should be sent to.
2. Chaining custom commands in the pipeline is not supported in Cluster mode.
1. Chaining custom commands in the pipeline is not supported in Cluster mode.

When any commands in a pipeline receives a `MOVED` or `ASK` error, ioredis will resend the whole pipeline to the specified node automatically if all of the following conditions are satisfied:

Expand Down
11 changes: 7 additions & 4 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parse as urllibParse } from "url";
import { defaults, noop, flatten } from "./lodash";
import { parse as parseQuery } from "qs";
import Debug from "./debug";

/**
Expand Down Expand Up @@ -248,11 +249,11 @@ export function parseURL(url) {
if (isInt(url)) {
return { port: url };
}
var parsed = urllibParse(url, true, true);
var parsed = urllibParse(url, false, true);

if (!parsed.slashes && url[0] !== "/") {
url = "//" + url;
parsed = urllibParse(url, true, true);
parsed = urllibParse(url, false, true);
}

var result: any = {};
Expand All @@ -274,10 +275,12 @@ export function parseURL(url) {
if (parsed.port) {
result.port = parsed.port;
}
if (parsed.protocol === "rediss:") {

defaults(result, parseQuery(parsed.query));

if (parsed.protocol === "rediss:" && !result.tls) {
result.tls = true;
}
defaults(result, parsed.query);

return result;
}
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"denque": "^1.1.0",
"lodash.defaults": "^4.2.0",
"lodash.flatten": "^4.4.0",
"qs": "^6.7.0",
"redis-commands": "1.5.0",
"redis-errors": "^1.2.0",
"redis-parser": "^3.0.0",
Expand All @@ -50,6 +51,7 @@
"@types/lodash.flatten": "^4.4.6",
"@types/mocha": "^5.2.7",
"@types/node": "^12.0.2",
"@types/qs": "^6.5.3",
"@types/redis-errors": "1.2.0",
"@types/sinon": "^7.0.13",
"@typescript-eslint/eslint-plugin": "^1.11.0",
Expand Down
3 changes: 3 additions & 0 deletions test/unit/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ describe("Redis", function() {
option = getOption("redis://127.0.0.1/");
expect(option).to.have.property("db", 0);

option = getOption("redis://127.0.0.1/4?db=5");
expect(option).to.have.property("db", 4);

option = getOption("/tmp/redis.sock");
expect(option).to.have.property("path", "/tmp/redis.sock");

Expand Down
35 changes: 35 additions & 0 deletions test/unit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,41 @@ describe("utils", function() {
password: "pass",
key: "value"
});

const ca = "aW9yZWRpcwo=";
expect(
utils.parseURL(
`rediss://user:[email protected]:6380/4?key=value&tls[ca]=${encodeURIComponent(
ca
)}`
)
).to.eql({
tls: {
ca
},
host: "127.0.0.1",
port: "6380",
db: "4",
password: "pass",
key: "value"
});

expect(
utils.parseURL(
`redis://user:[email protected]:6380/4?key=value&tls[ca]=${encodeURIComponent(
ca
)}`
)
).to.eql({
tls: {
ca
},
host: "127.0.0.1",
port: "6380",
db: "4",
password: "pass",
key: "value"
});
});
});

Expand Down

0 comments on commit 3aea10f

Please sign in to comment.