Skip to content

Commit

Permalink
fix: don鈥檛 clobber passed-in tls options with rediss:/ URLs (#949)
Browse files Browse the repository at this point in the history
Closes #942, #949, #940, #950, #948
  • Loading branch information
paulmelnikow authored and luin committed Aug 27, 2019
1 parent c1f0d03 commit ceefcfa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
7 changes: 7 additions & 0 deletions lib/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Redis.prototype.resetOfflineQueue = function() {

Redis.prototype.parseOptions = function() {
this.options = {};
let isTls = false;
for (var i = 0; i < arguments.length; ++i) {
var arg = arguments[i];
if (arg === null || typeof arg === "undefined") {
Expand All @@ -201,12 +202,18 @@ Redis.prototype.parseOptions = function() {
defaults(this.options, arg);
} else if (typeof arg === "string") {
defaults(this.options, parseURL(arg));
if (arg.startsWith("rediss://")) {
isTls = true;
}
} else if (typeof arg === "number") {
this.options.port = arg;
} else {
throw new Error("Invalid argument " + arg);
}
}
if (isTls) {
defaults(this.options, { tls: true });
}
defaults(this.options, Redis.defaultOptions);

if (typeof this.options.port === "string") {
Expand Down
3 changes: 0 additions & 3 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,6 @@ export function parseURL(url) {
if (parsed.port) {
result.port = parsed.port;
}
if (parsed.protocol === "rediss:") {
result.tls = true;
}
defaults(result, parsed.query);

return result;
Expand Down
8 changes: 8 additions & 0 deletions test/unit/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ describe("Redis", function() {
host: "192.168.1.1"
});
expect(option).to.have.property("port", 6380);

option = getOption("rediss://host");
expect(option).to.have.property("tls", true);

option = getOption("rediss://example.test", {
tls: { hostname: "example.test" }
});
expect(option.tls).to.deep.equal({ hostname: "example.test" });
} catch (err) {
stub.restore();
throw err;
Expand Down
1 change: 0 additions & 1 deletion test/unit/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ describe("utils", function() {
expect(
utils.parseURL("rediss://user:pass@127.0.0.1:6380/4?key=value")
).to.eql({
tls: true,
host: "127.0.0.1",
port: "6380",
db: "4",
Expand Down

0 comments on commit ceefcfa

Please sign in to comment.