From 977d4dba2b97811eea374e0dd9b21e24c2f2441f Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 30 Sep 2015 02:35:11 +0200 Subject: [PATCH] Add host and port to options object --- changelog.md | 3 ++- index.js | 6 ++++-- test/connection.spec.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index 782afc2437..3a34697586 100644 --- a/changelog.md +++ b/changelog.md @@ -5,7 +5,8 @@ Changelog Features: -- Add 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 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) Bugfixes: diff --git a/index.js b/index.js index dc41a42c7a..80e1c23c55 100644 --- a/index.js +++ b/index.js @@ -1107,8 +1107,10 @@ var createClient_tcp = function (port_arg, host_arg, options) { exports.createClient = function(port_arg, host_arg, options) { if (typeof port_arg === 'object' || port_arg === undefined) { - options = port_arg || options; - return createClient_tcp(default_port, default_host, options); + options = port_arg || options || {}; + var host = options.host || default_host; + var port = +options.port || default_port; + return createClient_tcp(port, host, options); } if (typeof port_arg === 'number' || typeof port_arg === 'string' && /^\d+$/.test(port_arg)) { return createClient_tcp(port_arg, host_arg, options); diff --git a/test/connection.spec.js b/test/connection.spec.js index a59ff8deaf..042eabb7ee 100644 --- a/test/connection.spec.js +++ b/test/connection.spec.js @@ -79,6 +79,34 @@ describe("on lost connection", function () { }); }); + it("can not connect with wrong host / port in the options object", function (done) { + var client = redis.createClient({ + host: 'somewhere', + port: 6379, + max_attempts: 1 + }); + var end = helper.callFuncAfter(done, 2); + + client.on('error', function (err) { + assert(/CONNECTION_BROKEN|ENOTFOUND/.test(err.code)); + end(); + }); + + }); + + it("connect with host and port provided in the options object", function (done) { + var client = redis.createClient({ + host: 'localhost', + port: '6379', + parser: parser, + connect_timeout: 1000 + }); + + client.once('ready', function() { + done(); + }); + }); + }); }); });