Comparing version 0.13.0 to 0.14.0
@@ -11,2 +11,3 @@ /** | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
@@ -66,3 +67,5 @@ | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -98,3 +101,5 @@ req.body.ID = opts.id; | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -124,15 +129,9 @@ utils.options(req, opts); | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
utils.options(req, opts); | ||
this.consul._get(req, function(err, res) { | ||
if (err) return callback(err, undefined, res); | ||
if (res.body && res.body.length) { | ||
return callback(null, res.body[0], res); | ||
} | ||
callback(undefined, undefined, res); | ||
}); | ||
this.consul._get(req, utils.bodyItem, callback); | ||
}; | ||
@@ -160,3 +159,5 @@ | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -163,0 +164,0 @@ utils.options(req, opts); |
@@ -13,2 +13,3 @@ /** | ||
var AgentService = require('./agent/service').AgentService; | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
@@ -30,4 +31,4 @@ | ||
Agent.prototype.checks = function(callback) { | ||
this.check.list(callback); | ||
Agent.prototype.checks = function() { | ||
this.check.list.apply(this.check, arguments); | ||
}; | ||
@@ -39,4 +40,4 @@ | ||
Agent.prototype.services = function(callback) { | ||
this.service.list(callback); | ||
Agent.prototype.services = function() { | ||
this.service.list.apply(this.service, arguments); | ||
}; | ||
@@ -105,3 +106,3 @@ | ||
if (typeof opts.enable !== 'boolean') { | ||
return callback(this.consul._err('enable required', req)); | ||
return callback(this.consul._err(errors.Validation('enable required'), req)); | ||
} | ||
@@ -130,7 +131,6 @@ if (opts.reason) req.query.reason = opts.reason; | ||
params: { address: opts.address }, | ||
query: {}, | ||
}; | ||
if (!opts.address) { | ||
return callback(this.consul._err('address required', req)); | ||
return callback(this.consul._err(errors.Validation('address required'), req)); | ||
} | ||
@@ -161,3 +161,3 @@ | ||
if (!opts.node) { | ||
return callback(this.consul._err('node required', req)); | ||
return callback(this.consul._err(errors.Validation('node required'), req)); | ||
} | ||
@@ -164,0 +164,0 @@ |
@@ -11,2 +11,3 @@ /** | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
@@ -56,3 +57,5 @@ | ||
if (!opts.name) return callback(this.consul._err('name required', req)); | ||
if (!opts.name) { | ||
return callback(this.consul._err(errors.Validation('name required'), req)); | ||
} | ||
@@ -62,5 +65,9 @@ req.body.Name = opts.name; | ||
if (opts.serviceid) req.body.ServiceID = opts.serviceid; | ||
if (opts.hasOwnProperty('id')) req.body.Id = opts.id; | ||
if (opts.script && opts.interval) { | ||
req.body.Script = opts.script; | ||
if (opts.hasOwnProperty('id')) req.body.ID = opts.id; | ||
if ((opts.http || opts.script) && opts.interval) { | ||
if (opts.http) { | ||
req.body.HTTP = opts.http; | ||
} else { | ||
req.body.Script = opts.script; | ||
} | ||
req.body.Interval = opts.interval; | ||
@@ -70,3 +77,4 @@ } else if (opts.ttl) { | ||
} else { | ||
return callback(this.consul._err('script and interval, or ttl required', req)); | ||
return callback(this.consul._err( | ||
errors.Validation('http or script and interval, or ttl required'), req)); | ||
} | ||
@@ -97,3 +105,5 @@ if (opts.hasOwnProperty('notes')) req.body.Notes = opts.notes; | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -123,3 +133,5 @@ utils.options(req, opts); | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -151,3 +163,5 @@ if (opts.note) req.query.note = opts.note; | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -179,3 +193,5 @@ if (opts.note) req.query.note = opts.note; | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -182,0 +198,0 @@ if (opts.note) req.query.note = opts.note; |
@@ -11,2 +11,3 @@ /** | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
@@ -60,3 +61,5 @@ | ||
if (!opts.name) return callback(this.consul._err('name required', req)); | ||
if (!opts.name) { | ||
return callback(this.consul._err(errors.Validation('name required'), req)); | ||
} | ||
@@ -84,3 +87,4 @@ req.body.Name = opts.name; | ||
} else { | ||
return callback(this.consul._err('http or script and interval, or ttl required', req)); | ||
return callback(this.consul._err( | ||
errors.Validation('http or script and interval, or ttl required'), req)); | ||
} | ||
@@ -112,3 +116,5 @@ if (check.hasOwnProperty('notes')) req.body.Check.Notes = check.notes; | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -134,5 +140,7 @@ utils.options(req, opts); | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
if (typeof opts.enable !== 'boolean') { | ||
return callback(this.consul._err('enable required', req)); | ||
return callback(this.consul._err(errors.Validation('enable required'), req)); | ||
} | ||
@@ -139,0 +147,0 @@ if (opts.reason) req.query.reason = opts.reason; |
@@ -29,3 +29,8 @@ /** | ||
Catalog.prototype.datacenters = function(callback) { | ||
Catalog.prototype.datacenters = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
var req = { | ||
@@ -36,2 +41,4 @@ name: 'catalog.datacenters', | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
@@ -38,0 +45,0 @@ }; |
@@ -11,2 +11,3 @@ /** | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
@@ -39,3 +40,2 @@ | ||
path: '/catalog/nodes', | ||
query: {}, | ||
}; | ||
@@ -53,6 +53,3 @@ | ||
CatalogNode.prototype.services = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
} else if (typeof opts === 'string') { | ||
if (typeof opts === 'string') { | ||
opts = { node: opts }; | ||
@@ -67,5 +64,8 @@ } | ||
params: { node: opts.node }, | ||
query: {}, | ||
}; | ||
if (!opts.node) { | ||
return callback(this.consul._err(errors.Validation('node required'), req)); | ||
} | ||
utils.options(req, opts); | ||
@@ -72,0 +72,0 @@ |
@@ -11,2 +11,3 @@ /** | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
@@ -52,6 +53,3 @@ | ||
CatalogService.prototype.nodes = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
} else if (typeof opts === 'string') { | ||
if (typeof opts === 'string') { | ||
opts = { service: opts }; | ||
@@ -69,2 +67,5 @@ } | ||
if (!opts.service) { | ||
return callback(this.consul._err(errors.Validation('service required'), req)); | ||
} | ||
if (opts.tag) req.query.tag = opts.tag; | ||
@@ -71,0 +72,0 @@ |
@@ -25,1 +25,12 @@ /** | ||
]; | ||
/** | ||
* Time | ||
*/ | ||
var du = exports.DURATION_UNITS = { ns: 1 }; | ||
du.us = 1000 * du.ns; | ||
du.ms = 1000 * du.us; | ||
du.s = 1000 * du.ms; | ||
du.m = 60 * du.s; | ||
du.h = 60 * du.m; |
@@ -17,3 +17,3 @@ /** | ||
var Catalog = require('./catalog').Catalog; | ||
var Event_ = require('./event').Event; | ||
var Event = require('./event').Event; | ||
var Health = require('./health').Health; | ||
@@ -23,2 +23,3 @@ var Kv = require('./kv').Kv; | ||
var Status = require('./status').Status; | ||
var Watcher = require('./watcher').Watcher; | ||
@@ -49,3 +50,3 @@ /** | ||
this.catalog = new Catalog(this); | ||
this.event = new Event_(this); | ||
this.event = new Event(this); | ||
this.health = new Health(this); | ||
@@ -60,2 +61,10 @@ this.kv = new Kv(this); | ||
/** | ||
* Watch helper. | ||
*/ | ||
Consul.prototype.watch = function(opts) { | ||
return new Watcher(this, opts); | ||
}; | ||
/** | ||
* Module exports. | ||
@@ -62,0 +71,0 @@ */ |
@@ -11,2 +11,3 @@ /** | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
@@ -46,3 +47,5 @@ | ||
if (!opts.name) return callback(this.consul._err('name required', req)); | ||
if (!opts.name) { | ||
return callback(this.consul._err(errors.Validation('name required'), req)); | ||
} | ||
@@ -55,3 +58,3 @@ var buffer; | ||
} | ||
if (opts.node) req.query.name = opts.name; | ||
if (opts.node) req.query.node = opts.node; | ||
if (opts.service) req.query.service = opts.service; | ||
@@ -58,0 +61,0 @@ if (opts.tag) req.query.tag = opts.tag; |
@@ -12,2 +12,3 @@ /** | ||
var constants = require('./constants'); | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
@@ -38,5 +39,8 @@ | ||
params: { node: opts.node }, | ||
query: {}, | ||
}; | ||
if (!opts.node) { | ||
return callback(this.consul._err(errors.Validation('node required'), req)); | ||
} | ||
utils.options(req, opts); | ||
@@ -62,5 +66,8 @@ | ||
params: { service: opts.service }, | ||
query: {}, | ||
}; | ||
if (!opts.service) { | ||
return callback(this.consul._err(errors.Validation('service required'), req)); | ||
} | ||
utils.options(req, opts); | ||
@@ -89,2 +96,6 @@ | ||
if (!opts.service) { | ||
return callback(this.consul._err(errors.Validation('service required'), req)); | ||
} | ||
if (opts.tag) req.query.tag = opts.tag; | ||
@@ -110,9 +121,13 @@ if (opts.passing) req.query.passing = 'true'; | ||
var req = { | ||
name: 'health.state', | ||
path: '/health/state/{state}', | ||
params: { state: opts.state }, | ||
query: {}, | ||
}; | ||
if (!opts.state) { | ||
return callback(this.consul._err(errors.Validation('state required'), req)); | ||
} | ||
if (opts.state !== 'any' && constants.CHECK_STATE.indexOf(opts.state) < 0) { | ||
return callback(this.consul._err('Invalid state: ' + opts.state, req)); | ||
return callback(this.consul._err(errors.Validation('state invalid: ' + opts.state), req)); | ||
} | ||
@@ -119,0 +134,0 @@ |
@@ -11,2 +11,3 @@ /** | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
@@ -53,3 +54,3 @@ | ||
if (res.body && Array.isArray(res.body)) { | ||
if (res.body && Array.isArray(res.body) && res.body.length) { | ||
res.body.forEach(function(item) { | ||
@@ -59,6 +60,6 @@ if (!item.hasOwnProperty('Value')) return; | ||
}); | ||
} else { | ||
return callback(undefined, undefined, res); | ||
} | ||
if (!res.body.length) return callback(undefined, undefined, res); | ||
if (!opts.recurse) return callback(null, res.body[0], res); | ||
@@ -81,3 +82,3 @@ | ||
callback = opts; | ||
opts = { key: '' }; | ||
opts = {}; | ||
break; | ||
@@ -136,4 +137,8 @@ } | ||
if (!opts.key) return callback(this.consul._err('key required', req)); | ||
if (!opts.hasOwnProperty('value')) return callback(this.consul._err('value required', req)); | ||
if (!opts.key) { | ||
return callback(this.consul._err(errors.Validation('key required'), req)); | ||
} | ||
if (!opts.hasOwnProperty('value')) { | ||
return callback(this.consul._err(errors.Validation('value required'), req)); | ||
} | ||
@@ -140,0 +145,0 @@ if (opts.hasOwnProperty('cas')) req.query.cas = opts.cas; |
@@ -11,2 +11,3 @@ /** | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
@@ -72,3 +73,5 @@ | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -98,3 +101,5 @@ utils.options(req, opts); | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -106,3 +111,3 @@ utils.options(req, opts); | ||
if (res.body && res.body.length) { | ||
if (res.body && Array.isArray(res.body) && res.body.length) { | ||
return callback(null, res.body[0], res); | ||
@@ -132,6 +137,7 @@ } | ||
params: { node: opts.node }, | ||
query: {}, | ||
}; | ||
if (!opts.node) return callback(this.consul._err('node required', req)); | ||
if (!opts.node) { | ||
return callback(this.consul._err(errors.Validation('node required'), req)); | ||
} | ||
@@ -158,3 +164,2 @@ utils.options(req, opts); | ||
path: '/session/list', | ||
query: {}, | ||
}; | ||
@@ -182,6 +187,7 @@ | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
if (!opts.id) return callback(this.consul._err('id required', req)); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
@@ -188,0 +194,0 @@ utils.options(req, opts); |
@@ -8,2 +8,8 @@ /** | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var constants = require('./constants'); | ||
/** | ||
* Body | ||
@@ -15,6 +21,20 @@ */ | ||
next(false, null, request.res.body, request.res); | ||
next(false, undefined, request.res.body, request.res); | ||
} | ||
/** | ||
* First item in body | ||
*/ | ||
function bodyItem(request, next) { | ||
if (request.err) return next(false, request.err, undefined, request.res); | ||
if (request.res.body && request.res.body.length) { | ||
return next(false, undefined, request.res.body[0], request.res); | ||
} | ||
next(false, undefined, undefined, request.res); | ||
} | ||
/** | ||
* Empty | ||
@@ -37,7 +57,7 @@ */ | ||
if (obj) { | ||
Object.keys(obj).forEach(function(name) { | ||
for (var name in obj) { | ||
if (obj.hasOwnProperty(name)) { | ||
result[name.replace(/_/g, '').toLowerCase()] = obj[name]; | ||
} | ||
}); | ||
} | ||
} | ||
@@ -53,2 +73,4 @@ | ||
function options(req, opts) { | ||
if (!opts) opts = {}; | ||
if (!req.query) req.query = {}; | ||
@@ -79,5 +101,5 @@ | ||
function decode(value, opts) { | ||
if (value === null) return value; | ||
if (typeof value !== 'string') return value; | ||
value = new Buffer(value, 'base64'); | ||
if (!opts.buffer) value = value.toString(); | ||
if (!opts || !opts.buffer) value = value.toString(); | ||
return value; | ||
@@ -87,2 +109,46 @@ } | ||
/** | ||
* Shallow clone | ||
*/ | ||
function clone(src) { | ||
var dst = {}; | ||
for (var key in src) { | ||
if (src.hasOwnProperty(key)) { | ||
dst[key] = src[key]; | ||
} | ||
} | ||
return dst; | ||
} | ||
/** | ||
* Parse duration | ||
*/ | ||
function parseDuration(value) { | ||
if (typeof value === 'number') return value / 1e6; | ||
if (typeof value !== 'string') return; | ||
var n; | ||
var m = value.match(/^(\d*\.?\d*)$/); | ||
if (m) { | ||
n = parseFloat(m[1]); | ||
if (!isNaN(n)) return n / 1e6; | ||
} | ||
m = value.match(/^([\d\.]*)(ns|us|ms|s|m|h)$/); | ||
if (!m) return; | ||
n = parseFloat(m[1]); | ||
if (isNaN(n)) return; | ||
return n * constants.DURATION_UNITS[m[2]] / 1e6; | ||
} | ||
/** | ||
* Module exports | ||
@@ -92,2 +158,3 @@ */ | ||
exports.body = body; | ||
exports.bodyItem = bodyItem; | ||
exports.decode = decode; | ||
@@ -97,1 +164,3 @@ exports.empty = empty; | ||
exports.options = options; | ||
exports.clone = clone; | ||
exports.parseDuration = parseDuration; |
{ | ||
"name": "consul", | ||
"version": "0.13.0", | ||
"version": "0.14.0", | ||
"description": "Consul client", | ||
@@ -11,15 +11,17 @@ "main": "./lib", | ||
"async": "^0.9.0", | ||
"debug": "^2.0.0", | ||
"istanbul": "^0.3.6", | ||
"debug": "^2.1.3", | ||
"istanbul": "^0.3.8", | ||
"jscs": "^1.11.3", | ||
"jshint": "^2.5.5", | ||
"lodash": "^3.3.0", | ||
"mocha": "^2.1.0", | ||
"node-uuid": "^1.4.1", | ||
"should": "^5.0.1", | ||
"lodash": "^3.5.0", | ||
"mocha": "^2.2.1", | ||
"nock": "^1.2.0", | ||
"node-uuid": "^1.4.3", | ||
"should": "^5.2.0", | ||
"temp": "^0.8.1" | ||
}, | ||
"scripts": { | ||
"cover": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --recursive --check-leaks --timeout 15000 && open coverage/lcov-report/index.html", | ||
"test": "./node_modules/.bin/jshint lib test && ./node_modules/.bin/jscs lib test && ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --recursive --check-leaks --timeout 15000 && istanbul check-coverage --statements 86 --functions 100 --branches 64 --lines 95" | ||
"cover": "./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --recursive && open coverage/lcov-report/index.html", | ||
"test": "./node_modules/.bin/jshint lib test && ./node_modules/.bin/jscs lib test && ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --recursive --check-leaks && istanbul check-coverage --statements 100 --functions 100 --branches 100 --lines 100", | ||
"acceptance": "ACCEPTANCE=true ./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- test/acceptance --recursive --check-leaks --timeout 15000" | ||
}, | ||
@@ -26,0 +28,0 @@ "keywords": [ |
@@ -1,2 +0,2 @@ | ||
# Consul | ||
# Consul [![Build Status](https://travis-ci.org/silas/node-consul.png?branch=master)](https://travis-ci.org/silas/node-consul) | ||
@@ -6,3 +6,2 @@ This is a [Consul][consul] client. | ||
* [Documentation](#documentation) | ||
* [Development](#development) | ||
* [License](#license) | ||
@@ -27,3 +26,22 @@ | ||
* [Status](#status) | ||
* [Watch](#watch) | ||
<a name="init"/> | ||
### consul([options]) | ||
Initialize a new Consul client. | ||
Options | ||
* host (String, default: 127.0.0.1): agent address | ||
* port (String, default: 8500): agent HTTP(S) port | ||
* secure (Boolean, default: false): enable HTTPS | ||
* ca (String[], optional): array of strings or Buffers of trusted certificates in PEM format | ||
Usage | ||
``` javascript | ||
var consul = require('consul')(); | ||
``` | ||
<a name="callback"/> | ||
@@ -39,5 +57,6 @@ ### Callback | ||
<a name="common-options"/> | ||
### Common Options | ||
<a name="method-options"/> | ||
### Method Options | ||
These options will be passed along with any call, although only certain endpoints support them. See the [HTTP API][consul-docs-api] for more information. | ||
These options will be passed along with any method call, although only certain endpoints support them. See the [HTTP API][consul-docs-api] for more information. | ||
@@ -52,3 +71,3 @@ * dc (String, optional): datacenter (defaults to local for agent) | ||
These options work for all endpoints. | ||
These options work for all methods. | ||
@@ -58,20 +77,2 @@ * ctx (EventEmitter, optional): emit `cancel` to abort request | ||
<a name="init"/> | ||
### consul([options]) | ||
Initialize a new Consul client. | ||
Options | ||
* host (String, default: 127.0.0.1): agent address | ||
* port (String, default: 8500): agent HTTP(S) port | ||
* secure (Boolean, default: false): enable HTTPS | ||
* ca (String[], optional): array of strings or Buffers of trusted certificates in PEM format | ||
Usage | ||
``` javascript | ||
var consul = require('consul')(); | ||
``` | ||
<a name="acl"/> | ||
@@ -1481,4 +1482,30 @@ ### consul.acl | ||
## Development | ||
<a name="watch"/> | ||
### consul.watch(options) | ||
Watch an endpoint for changes. | ||
Options | ||
* method (Function): method to watch | ||
* options (Object): method options | ||
Usage | ||
``` javascript | ||
var watch = consul.watch({ method: consul.kv.get, options: { key: 'test' }); | ||
watch.on('change', function(data, res) { | ||
console.log('data:', data); | ||
}); | ||
watch.on('error', function(err) { | ||
console.log('error:', err); | ||
}); | ||
setTimeout(function() { watch.end(); }, 30 * 1000); | ||
``` | ||
## Acceptance Tests | ||
1. Install [Consul][download] into your `PATH`. | ||
@@ -1501,3 +1528,3 @@ 1. Attach required IPs | ||
``` console | ||
$ npm test | ||
$ npm run acceptance | ||
``` | ||
@@ -1504,0 +1531,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
84019
23
1556
1532
1
11