From c7af2cf323948eb431841d9f93c0ff779af8d789 Mon Sep 17 00:00:00 2001 From: Jen Andre Date: Fri, 11 Jul 2014 08:21:05 -0400 Subject: [PATCH 1/4] Do not try to base64 decode empty values It is possible for consul kv items to be empty, e.g. you might get something like {"CreateIndex":915,"ModifyIndex":915,"LockIndex":0,"Key":"some/key/prefix","Flags":4,"Value":null} In this case, node-consul will attempt to base64 decode this, which results in an exception being thrown, e.g.: TypeError: Cannot read property 'length' of null at new Buffer (buffer.js:184:31) at /proj/node_modules/consul-node/lib/kv.js:49:16 ... This prevents that from happening. --- lib/kv.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/kv.js b/lib/kv.js index 114eb7e..95481ed 100644 --- a/lib/kv.js +++ b/lib/kv.js @@ -40,12 +40,14 @@ KV.prototype.get = function (key, opts, done) { if (!items) return done(null, items); done(null, items.map(function (item) { + // do not try to base64 decode empty items e.g. nulls + var val = (item['Value']) ? new Buffer(item['Value'], 'base64').toString('utf8') : item['Value']; return { createIndex: item['CreateIndex'], modifyIndex: item['ModifyIndex'], key: item['Key'], flags: item['Flags'], - value: new Buffer(item['Value'], 'base64').toString('utf8') + value: val }; })); }); From c53459de6227da9a4125d00b5d51e27caf13a19e Mon Sep 17 00:00:00 2001 From: Jen Andre Date: Sun, 13 Jul 2014 09:58:37 -0400 Subject: [PATCH 2/4] Provide an api for the /v1/catalog/services route Provide an api that allows us to query the /v1/catalog/services endpoint. --- lib/catalog.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/catalog.js b/lib/catalog.js index c63b09b..f13f92d 100644 --- a/lib/catalog.js +++ b/lib/catalog.js @@ -14,7 +14,7 @@ module.exports = Catalog; /** * Catalog constructor. * - * @param {Consul} consol + * @param {Consul} consul * @constructor */ @@ -22,6 +22,18 @@ function Catalog (consul) { this.requestor = new Requestor('catalog', consul); } +/** + * Lists the services in a catalog. + * + * @param {Object} [opts] + * @param {Function} done + */ +Catalog.prototype.services = function(opts, done) { + if ('function' == typeof opts) done = opts, opts = null; + + this.requestor.get('services', opts, done); +}; + /** * Lists the nodes in a given service. * From 052af6aed5a2ffb4308848a7ebbe821c38012f54 Mon Sep 17 00:00:00 2001 From: Jen Andre Date: Sun, 13 Jul 2014 11:19:51 -0400 Subject: [PATCH 3/4] Fixes an issue where if requestor.put is supplied with an object and not a string or other type, it errors out. We need to make sure that JS objects are correctly serialized as JSON in the request body. To do this, we set req.json instead of req.body upstream to the underlying request library. --- lib/requestor.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/requestor.js b/lib/requestor.js index 2f96663..6c160d2 100644 --- a/lib/requestor.js +++ b/lib/requestor.js @@ -125,13 +125,19 @@ Requestor.prototype.get = function (path, opts, done) { */ Requestor.prototype.put = function (path, data, opts, done) { - this.request({ + var req = { method: 'PUT', url: this.urlFor(path), qs: opts, - body: data, encoding: 'utf8' - }, done); + }; + + if (data && typeof data == 'object') + req.json = data; + else + req.body = data; + + this.request(req, done); }; /** From 049c2811a30c6c1cac05715c9699fc3df9d65c6b Mon Sep 17 00:00:00 2001 From: Jen Andre Date: Sun, 13 Jul 2014 11:25:16 -0400 Subject: [PATCH 4/4] registerService (v1/service/register) should be a PUT request, not a GET request. --- lib/agent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/agent.js b/lib/agent.js index 34d7c0d..5b29ced 100644 --- a/lib/agent.js +++ b/lib/agent.js @@ -214,7 +214,7 @@ Agent.prototype.failCheck = function (checkId, opts, done) { Agent.prototype.registerService = function (service, opts, done) { if ('function' == typeof opts) done = opts, opts = null; - this.requestor.get('service/register', service, opts, done); + this.requestor.put('service/register', service, opts, done); }; /**