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); }; /** 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. * 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 }; })); }); 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); }; /**