Comparing version 0.40.0 to 1.0.0
264
lib/acl.js
@@ -1,245 +0,49 @@ | ||
/** | ||
* ACL manipulation | ||
*/ | ||
const AclLegacy = require("./acl/legacy").AclLegacy; | ||
const utils = require("./utils"); | ||
'use strict'; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
/** | ||
* Initialize a new `Acl` client. | ||
*/ | ||
function Acl(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Creates one-time management token if not configured | ||
*/ | ||
Acl.prototype.bootstrap = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
class Acl { | ||
constructor(consul) { | ||
this.consul = consul; | ||
this.legacy = new Acl.Legacy(consul); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Creates one-time management token if not configured | ||
*/ | ||
async bootstrap(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'acl.bootstrap', | ||
path: '/acl/bootstrap', | ||
type: 'json', | ||
}; | ||
const req = { | ||
name: "acl.bootstrap", | ||
path: "/acl/bootstrap", | ||
type: "json", | ||
}; | ||
utils.options(req, opts); | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.body, callback); | ||
}; | ||
/** | ||
* Creates a new token with policy | ||
*/ | ||
Acl.prototype.create = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._put(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Check ACL replication | ||
*/ | ||
async replication(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'acl.create', | ||
path: '/acl/create', | ||
query: {}, | ||
type: 'json', | ||
body: {}, | ||
}; | ||
const req = { | ||
name: "acl.replication", | ||
path: "/acl/replication", | ||
query: {}, | ||
}; | ||
if (opts.name) req.body.Name = opts.name; | ||
if (opts.type) req.body.Type = opts.type; | ||
if (opts.rules) req.body.Rules = opts.rules; | ||
utils.options(req, opts); | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.body, callback); | ||
}; | ||
/** | ||
* Update the policy of a token | ||
*/ | ||
Acl.prototype.update = function(opts, callback) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'acl.update', | ||
path: '/acl/update', | ||
query: {}, | ||
type: 'json', | ||
body: {}, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
return await this.consul._get(req, utils.body); | ||
} | ||
} | ||
req.body.ID = opts.id; | ||
Acl.Legacy = AclLegacy; | ||
if (opts.name) req.body.Name = opts.name; | ||
if (opts.type) req.body.Type = opts.type; | ||
if (opts.rules) req.body.Rules = opts.rules; | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
/** | ||
* Destroys a given token | ||
*/ | ||
Acl.prototype.destroy = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'acl.destroy', | ||
path: '/acl/destroy/{id}', | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
/** | ||
* Queries the policy of a given token | ||
*/ | ||
Acl.prototype.info = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'acl.info', | ||
path: '/acl/info/{id}', | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.bodyItem, callback); | ||
}; | ||
Acl.prototype.get = Acl.prototype.info; | ||
/** | ||
* Creates a new token by cloning an existing token | ||
*/ | ||
Acl.prototype.clone = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'acl.clone', | ||
path: '/acl/clone/{id}', | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.body, callback); | ||
}; | ||
/** | ||
* Lists all the active tokens | ||
*/ | ||
Acl.prototype.list = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'acl.list', | ||
path: '/acl/list', | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Check ACL replication | ||
*/ | ||
Acl.prototype.replication = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'acl.replication', | ||
path: '/acl/replication', | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Acl = Acl; |
279
lib/agent.js
@@ -1,201 +0,162 @@ | ||
/** | ||
* Agent control | ||
*/ | ||
const AgentCheck = require("./agent/check").AgentCheck; | ||
const AgentService = require("./agent/service").AgentService; | ||
const errors = require("./errors"); | ||
const utils = require("./utils"); | ||
'use strict'; | ||
class Agent { | ||
constructor(consul) { | ||
this.consul = consul; | ||
this.check = new Agent.Check(consul); | ||
this.service = new Agent.Service(consul); | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Returns the checks the local agent is managing | ||
*/ | ||
checks() { | ||
return this.check.list.apply(this.check, arguments); | ||
} | ||
var AgentCheck = require('./agent/check').AgentCheck; | ||
var AgentService = require('./agent/service').AgentService; | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
/** | ||
* Returns the services local agent is managing | ||
*/ | ||
services() { | ||
return this.service.list.apply(this.service, arguments); | ||
} | ||
/** | ||
* Initialize a new `Agent` client. | ||
*/ | ||
/** | ||
* Returns the members as seen by the local consul agent | ||
*/ | ||
async members(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
function Agent(consul) { | ||
this.consul = consul; | ||
this.check = new Agent.Check(consul); | ||
this.service = new Agent.Service(consul); | ||
} | ||
const req = { | ||
name: "agent.members", | ||
path: "/agent/members", | ||
query: {}, | ||
}; | ||
Agent.Check = AgentCheck; | ||
Agent.Service = AgentService; | ||
utils.options(req, opts); | ||
/** | ||
* Returns the checks the local agent is managing | ||
*/ | ||
Agent.prototype.checks = function() { | ||
this.check.list.apply(this.check, arguments); | ||
}; | ||
/** | ||
* Returns the services local agent is managing | ||
*/ | ||
Agent.prototype.services = function() { | ||
this.service.list.apply(this.service, arguments); | ||
}; | ||
/** | ||
* Returns the members as seen by the local consul agent | ||
*/ | ||
Agent.prototype.members = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Reload agent configuration | ||
*/ | ||
async reload(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'agent.members', | ||
path: '/agent/members', | ||
query: {}, | ||
}; | ||
const req = { | ||
name: "agent.reload", | ||
path: "/agent/reload", | ||
}; | ||
utils.options(req, opts); | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Reload agent configuration | ||
*/ | ||
Agent.prototype.reload = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Returns the local node configuration | ||
*/ | ||
async self(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'agent.reload', | ||
path: '/agent/reload', | ||
}; | ||
const req = { | ||
name: "agent.self", | ||
path: "/agent/self", | ||
}; | ||
utils.options(req, opts); | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
/** | ||
* Returns the local node configuration | ||
*/ | ||
Agent.prototype.self = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Manages node maintenance mode | ||
*/ | ||
async maintenance(opts) { | ||
if (typeof opts === "boolean") { | ||
opts = { enable: opts }; | ||
} | ||
var req = { | ||
name: 'agent.self', | ||
path: '/agent/self', | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
const req = { | ||
name: "agent.maintenance", | ||
path: "/agent/maintenance", | ||
query: { enable: opts.enable }, | ||
}; | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
if (typeof opts.enable !== "boolean") { | ||
throw this.consul._err(errors.Validation("enable required"), req); | ||
} | ||
if (opts.reason) req.query.reason = opts.reason; | ||
/** | ||
* Manages node maintenance mode | ||
*/ | ||
utils.options(req, opts); | ||
Agent.prototype.maintenance = function(opts, callback) { | ||
if (typeof opts === 'boolean') { | ||
opts = { enable: opts }; | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Trigger local agent to join a node | ||
*/ | ||
async join(opts) { | ||
if (typeof opts === "string") { | ||
opts = { address: opts }; | ||
} | ||
var req = { | ||
name: 'agent.maintenance', | ||
path: '/agent/maintenance', | ||
query: { enable: opts.enable }, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (typeof opts.enable !== 'boolean') { | ||
return callback(this.consul._err(errors.Validation('enable required'), req)); | ||
} | ||
if (opts.reason) req.query.reason = opts.reason; | ||
const req = { | ||
name: "agent.join", | ||
path: "/agent/join/{address}", | ||
params: { address: opts.address }, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.address) { | ||
throw this.consul._err(errors.Validation("address required"), req); | ||
} | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Trigger local agent to join a node | ||
*/ | ||
Agent.prototype.join = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { address: opts }; | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Force remove node | ||
*/ | ||
async forceLeave(opts) { | ||
if (typeof opts === "string") { | ||
opts = { node: opts }; | ||
} | ||
var req = { | ||
name: 'agent.join', | ||
path: '/agent/join/{address}', | ||
params: { address: opts.address }, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.address) { | ||
return callback(this.consul._err(errors.Validation('address required'), req)); | ||
} | ||
const req = { | ||
name: "agent.forceLeave", | ||
path: "/agent/force-leave/{node}", | ||
params: { node: opts.node }, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.node) { | ||
throw this.consul._err(errors.Validation("node required"), req); | ||
} | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Force remove node | ||
*/ | ||
Agent.prototype.forceLeave = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { node: opts }; | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
Agent.Check = AgentCheck; | ||
Agent.Service = AgentService; | ||
var req = { | ||
name: 'agent.forceLeave', | ||
path: '/agent/force-leave/{node}', | ||
params: { node: opts.node }, | ||
}; | ||
if (!opts.node) { | ||
return callback(this.consul._err(errors.Validation('node required'), req)); | ||
} | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Agent = Agent; |
@@ -1,191 +0,164 @@ | ||
/** | ||
* Agent check | ||
*/ | ||
const errors = require("../errors"); | ||
const utils = require("../utils"); | ||
'use strict'; | ||
class AgentCheck { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Returns the checks the local agent is managing | ||
*/ | ||
async list(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
const req = { | ||
name: "agent.check.list", | ||
path: "/agent/checks", | ||
}; | ||
/** | ||
* Initialize a new `AgentCheck` client. | ||
*/ | ||
utils.options(req, opts); | ||
function AgentCheck(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Returns the checks the local agent is managing | ||
*/ | ||
AgentCheck.prototype.list = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Registers a new local check | ||
*/ | ||
async register(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'agent.check.list', | ||
path: '/agent/checks', | ||
}; | ||
const req = { | ||
name: "agent.check.register", | ||
path: "/agent/check/register", | ||
type: "json", | ||
}; | ||
utils.options(req, opts); | ||
try { | ||
req.body = utils.createCheck(opts); | ||
} catch (err) { | ||
throw this.consul._err(errors.Validation(err.message), req); | ||
} | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Registers a new local check | ||
*/ | ||
AgentCheck.prototype.register = function(opts, callback) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'agent.check.register', | ||
path: '/agent/check/register', | ||
type: 'json', | ||
}; | ||
try { | ||
req.body = utils.createCheck(opts); | ||
} catch (err) { | ||
return callback(this.consul._err(errors.Validation(err.message), req)); | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
utils.options(req, opts); | ||
/** | ||
* Deregister a local check | ||
*/ | ||
async deregister(opts) { | ||
if (typeof opts === "string") { | ||
opts = { id: opts }; | ||
} | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Deregister a local check | ||
*/ | ||
const req = { | ||
name: "agent.check.deregister", | ||
path: "/agent/check/deregister/{id}", | ||
params: { id: opts.id }, | ||
}; | ||
AgentCheck.prototype.deregister = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
} | ||
if (!opts.id) { | ||
throw this.consul._err(errors.Validation("id required"), req); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
var req = { | ||
name: 'agent.check.deregister', | ||
path: '/agent/check/deregister/{id}', | ||
params: { id: opts.id }, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
utils.options(req, opts); | ||
/** | ||
* Mark a local test as passing | ||
*/ | ||
async pass(opts) { | ||
if (typeof opts === "string") { | ||
opts = { id: opts }; | ||
} | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Mark a local test as passing | ||
*/ | ||
const req = { | ||
name: "agent.check.pass", | ||
path: "/agent/check/pass/{id}", | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
AgentCheck.prototype.pass = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
} | ||
if (!opts.id) { | ||
throw this.consul._err(errors.Validation("id required"), req); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (opts.note) req.query.note = opts.note; | ||
var req = { | ||
name: 'agent.check.pass', | ||
path: '/agent/check/pass/{id}', | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
if (opts.note) req.query.note = opts.note; | ||
/** | ||
* Mark a local test as warning | ||
*/ | ||
async warn(opts) { | ||
if (typeof opts === "string") { | ||
opts = { id: opts }; | ||
} | ||
utils.options(req, opts); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
const req = { | ||
name: "agent.check.warn", | ||
path: "/agent/check/warn/{id}", | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
/** | ||
* Mark a local test as warning | ||
*/ | ||
if (!opts.id) { | ||
throw this.consul._err(errors.Validation("id required"), req); | ||
} | ||
AgentCheck.prototype.warn = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
} | ||
if (opts.note) req.query.note = opts.note; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
var req = { | ||
name: 'agent.check.warn', | ||
path: '/agent/check/warn/{id}', | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
if (opts.note) req.query.note = opts.note; | ||
/** | ||
* Mark a local test as critical | ||
*/ | ||
async fail(opts) { | ||
if (typeof opts === "string") { | ||
opts = { id: opts }; | ||
} | ||
utils.options(req, opts); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
const req = { | ||
name: "agent.check.fail", | ||
path: "/agent/check/fail/{id}", | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
/** | ||
* Mark a local test as critical | ||
*/ | ||
if (!opts.id) { | ||
throw this.consul._err(errors.Validation("id required"), req); | ||
} | ||
AgentCheck.prototype.fail = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
} | ||
if (opts.note) req.query.note = opts.note; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
var req = { | ||
name: 'agent.check.fail', | ||
path: '/agent/check/fail/{id}', | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
} | ||
if (opts.note) req.query.note = opts.note; | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
/** | ||
* Module Exports. | ||
*/ | ||
exports.AgentCheck = AgentCheck; |
@@ -1,138 +0,113 @@ | ||
/** | ||
* Agent service | ||
*/ | ||
const errors = require("../errors"); | ||
const utils = require("../utils"); | ||
'use strict'; | ||
class AgentService { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Returns the services local agent is managing | ||
*/ | ||
async list(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
const req = { | ||
name: "agent.service.list", | ||
path: "/agent/services", | ||
}; | ||
/** | ||
* Initialize a new `AgentService` client. | ||
*/ | ||
utils.options(req, opts); | ||
function AgentService(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Returns the services local agent is managing | ||
*/ | ||
AgentService.prototype.list = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Registers a new local service | ||
*/ | ||
async register(opts) { | ||
if (typeof opts === "string") { | ||
opts = { name: opts }; | ||
} | ||
var req = { | ||
name: 'agent.service.list', | ||
path: '/agent/services', | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
const req = { | ||
name: "agent.service.register", | ||
path: "/agent/service/register", | ||
type: "json", | ||
body: {}, | ||
}; | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
if (!opts.name) { | ||
throw this.consul._err(errors.Validation("name required"), req); | ||
} | ||
/** | ||
* Registers a new local service | ||
*/ | ||
try { | ||
req.body = utils.createService(opts); | ||
} catch (err) { | ||
throw this.consul._err(errors.Validation(err.message), req); | ||
} | ||
AgentService.prototype.register = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { name: opts }; | ||
} | ||
utils.options(req, opts); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'agent.service.register', | ||
path: '/agent/service/register', | ||
type: 'json', | ||
body: {}, | ||
}; | ||
if (!opts.name) { | ||
return callback(this.consul._err(errors.Validation('name required'), req)); | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
try { | ||
req.body = utils.createService(opts); | ||
} catch (err) { | ||
return callback(this.consul._err(errors.Validation(err.message), req)); | ||
} | ||
/** | ||
* Deregister a local service | ||
*/ | ||
async deregister(opts) { | ||
if (typeof opts === "string") { | ||
opts = { id: opts }; | ||
} | ||
utils.options(req, opts); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
const req = { | ||
name: "agent.service.deregister", | ||
path: "/agent/service/deregister/{id}", | ||
params: { id: opts.id }, | ||
}; | ||
/** | ||
* Deregister a local service | ||
*/ | ||
if (!opts.id) { | ||
throw this.consul._err(errors.Validation("id required"), req); | ||
} | ||
AgentService.prototype.deregister = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
} | ||
utils.options(req, opts); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'agent.service.deregister', | ||
path: '/agent/service/deregister/{id}', | ||
params: { id: opts.id }, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
utils.options(req, opts); | ||
/** | ||
* Manages node maintenance mode | ||
*/ | ||
async maintenance(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
const req = { | ||
name: "agent.service.maintenance", | ||
path: "/agent/service/maintenance/{id}", | ||
params: { id: opts.id }, | ||
query: { enable: opts.enable }, | ||
}; | ||
/** | ||
* Manages node maintenance mode | ||
*/ | ||
if (!opts.id) { | ||
throw this.consul._err(errors.Validation("id required"), req); | ||
} | ||
if (typeof opts.enable !== "boolean") { | ||
throw this.consul._err(errors.Validation("enable required"), req); | ||
} | ||
if (opts.reason) req.query.reason = opts.reason; | ||
AgentService.prototype.maintenance = function(opts, callback) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
var req = { | ||
name: 'agent.service.maintenance', | ||
path: '/agent/service/maintenance/{id}', | ||
params: { id: opts.id }, | ||
query: { enable: opts.enable }, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
if (typeof opts.enable !== 'boolean') { | ||
return callback(this.consul._err(errors.Validation('enable required'), req)); | ||
} | ||
if (opts.reason) req.query.reason = opts.reason; | ||
} | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
/** | ||
* Module Exports. | ||
*/ | ||
exports.AgentService = AgentService; |
@@ -1,25 +0,45 @@ | ||
/** | ||
* Manage catalog | ||
*/ | ||
const CatalogConnect = require("./catalog/connect").CatalogConnect; | ||
const CatalogNode = require("./catalog/node").CatalogNode; | ||
const CatalogService = require("./catalog/service").CatalogService; | ||
const utils = require("./utils"); | ||
'use strict'; | ||
class Catalog { | ||
constructor(consul) { | ||
this.consul = consul; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
this.connect = new Catalog.Connect(consul); | ||
this.node = new Catalog.Node(consul); | ||
this.service = new Catalog.Service(consul); | ||
} | ||
var CatalogNode = require('./catalog/node').CatalogNode; | ||
var CatalogService = require('./catalog/service').CatalogService; | ||
var CatalogConnect = require('./catalog/connect').CatalogConnect; | ||
var utils = require('./utils'); | ||
/** | ||
* Lists known datacenters | ||
*/ | ||
async datacenters(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Initialize a new `Catalog` client. | ||
*/ | ||
const req = { | ||
name: "catalog.datacenters", | ||
path: "/catalog/datacenters", | ||
}; | ||
function Catalog(consul) { | ||
this.consul = consul; | ||
this.connect = new Catalog.Connect(consul); | ||
this.node = new Catalog.Node(consul); | ||
this.service = new Catalog.Service(consul); | ||
utils.options(req, opts); | ||
return await this.consul._get(req, utils.body); | ||
} | ||
/** | ||
* Lists nodes in a given DC | ||
*/ | ||
nodes(...args) { | ||
return this.node.list(...args); | ||
} | ||
/** | ||
* Lists services in a given DC | ||
*/ | ||
services(...args) { | ||
return this.service.list(...args); | ||
} | ||
} | ||
@@ -31,45 +51,2 @@ | ||
/** | ||
* Lists known datacenters | ||
*/ | ||
Catalog.prototype.datacenters = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'catalog.datacenters', | ||
path: '/catalog/datacenters', | ||
}; | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Lists nodes in a given DC | ||
*/ | ||
Catalog.prototype.nodes = function() { | ||
this.node.list.apply(this.node, arguments); | ||
}; | ||
/** | ||
* Lists services in a given DC | ||
*/ | ||
Catalog.prototype.services = function() { | ||
this.service.list.apply(this.service, arguments); | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Catalog = Catalog; |
@@ -1,54 +0,37 @@ | ||
/** | ||
* Catalog service | ||
*/ | ||
const errors = require("../errors"); | ||
const utils = require("../utils"); | ||
'use strict'; | ||
class CatalogConnect { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Lists the nodes in a given Connect-capable service | ||
*/ | ||
async nodes(opts) { | ||
if (typeof opts === "string") { | ||
opts = { service: opts }; | ||
} | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Initialize a new `CatalogService` client. | ||
*/ | ||
const req = { | ||
name: "catalog.connect.nodes", | ||
path: "/catalog/connect/{service}", | ||
params: { service: opts.service }, | ||
query: {}, | ||
}; | ||
function CatalogConnect(consul) { | ||
this.consul = consul; | ||
} | ||
if (!opts.service) { | ||
throw this.consul._err(errors.Validation("service required"), req); | ||
} | ||
/** | ||
* Lists the nodes in a given Connect-capable service | ||
*/ | ||
utils.options(req, opts); | ||
CatalogConnect.prototype.nodes = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { service: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'catalog.connect.nodes', | ||
path: '/catalog/connect/{service}', | ||
params: { service: opts.service }, | ||
query: {}, | ||
}; | ||
if (!opts.service) { | ||
return callback(this.consul._err(errors.Validation('service required'), req)); | ||
} | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Module Exports. | ||
*/ | ||
exports.CatalogConnect = CatalogConnect; |
@@ -1,78 +0,57 @@ | ||
/** | ||
* Catalog node | ||
*/ | ||
const errors = require("../errors"); | ||
const utils = require("../utils"); | ||
'use strict'; | ||
class CatalogNode { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Lists nodes in a given DC | ||
*/ | ||
async list(opts) { | ||
if (typeof opts === "string") { | ||
opts = { dc: opts }; | ||
} | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Initialize a new `CatalogNode` client. | ||
*/ | ||
const req = { | ||
name: "catalog.node.list", | ||
path: "/catalog/nodes", | ||
}; | ||
function CatalogNode(consul) { | ||
this.consul = consul; | ||
} | ||
utils.options(req, opts); | ||
/** | ||
* Lists nodes in a given DC | ||
*/ | ||
CatalogNode.prototype.list = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
} else if (typeof opts === 'string') { | ||
opts = { dc: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Lists the services provided by a node | ||
*/ | ||
async services(opts) { | ||
if (typeof opts === "string") { | ||
opts = { node: opts }; | ||
} | ||
var req = { | ||
name: 'catalog.node.list', | ||
path: '/catalog/nodes', | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
const req = { | ||
name: "catalog.node.services", | ||
path: "/catalog/node/{node}", | ||
params: { node: opts.node }, | ||
}; | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
if (!opts.node) { | ||
throw this.consul._err(errors.Validation("node required"), req); | ||
} | ||
/** | ||
* Lists the services provided by a node | ||
*/ | ||
utils.options(req, opts); | ||
CatalogNode.prototype.services = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { node: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'catalog.node.services', | ||
path: '/catalog/node/{node}', | ||
params: { node: opts.node }, | ||
}; | ||
if (!opts.node) { | ||
return callback(this.consul._err(errors.Validation('node required'), req)); | ||
} | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Module Exports. | ||
*/ | ||
exports.CatalogNode = CatalogNode; |
@@ -1,81 +0,61 @@ | ||
/** | ||
* Catalog service | ||
*/ | ||
const errors = require("../errors"); | ||
const utils = require("../utils"); | ||
'use strict'; | ||
class CatalogService { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Lists services in a given DC | ||
*/ | ||
var errors = require('../errors'); | ||
var utils = require('../utils'); | ||
async list(opts) { | ||
if (typeof opts === "string") { | ||
opts = { dc: opts }; | ||
} | ||
/** | ||
* Initialize a new `CatalogService` client. | ||
*/ | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
function CatalogService(consul) { | ||
this.consul = consul; | ||
} | ||
const req = { | ||
name: "catalog.service.list", | ||
path: "/catalog/services", | ||
query: {}, | ||
}; | ||
/** | ||
* Lists services in a given DC | ||
*/ | ||
utils.options(req, opts); | ||
CatalogService.prototype.list = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
} else if (typeof opts === 'string') { | ||
opts = { dc: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Lists the nodes in a given service | ||
*/ | ||
async nodes(opts) { | ||
if (typeof opts === "string") { | ||
opts = { service: opts }; | ||
} | ||
var req = { | ||
name: 'catalog.service.list', | ||
path: '/catalog/services', | ||
query: {}, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
const req = { | ||
name: "catalog.service.nodes", | ||
path: "/catalog/service/{service}", | ||
params: { service: opts.service }, | ||
query: {}, | ||
}; | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
if (!opts.service) { | ||
throw this.consul._err(errors.Validation("service required"), req); | ||
} | ||
if (opts.tag) req.query.tag = opts.tag; | ||
/** | ||
* Lists the nodes in a given service | ||
*/ | ||
utils.options(req, opts); | ||
CatalogService.prototype.nodes = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { service: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'catalog.service.nodes', | ||
path: '/catalog/service/{service}', | ||
params: { service: opts.service }, | ||
query: {}, | ||
}; | ||
if (!opts.service) { | ||
return callback(this.consul._err(errors.Validation('service required'), req)); | ||
} | ||
if (opts.tag) req.query.tag = opts.tag; | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Module Exports. | ||
*/ | ||
exports.CatalogService = CatalogService; |
@@ -1,45 +0,16 @@ | ||
/** | ||
* Constants | ||
*/ | ||
'use strict'; | ||
/** | ||
* Default options | ||
*/ | ||
exports.DEFAULT_OPTIONS = [ | ||
'consistent', | ||
'dc', | ||
'stale', | ||
'timeout', | ||
'token', | ||
'wait', | ||
'wan', | ||
"consistent", | ||
"dc", | ||
"stale", | ||
"timeout", | ||
"token", | ||
"wait", | ||
"wan", | ||
]; | ||
/** | ||
* Values | ||
*/ | ||
exports.AGENT_STATUS = ["none", "alive", "leaving", "left", "failed"]; | ||
exports.AGENT_STATUS = [ | ||
'none', | ||
'alive', | ||
'leaving', | ||
'left', | ||
'failed', | ||
]; | ||
exports.CHECK_STATE = ["unknown", "passing", "warning", "critical"]; | ||
exports.CHECK_STATE = [ | ||
'unknown', | ||
'passing', | ||
'warning', | ||
'critical', | ||
]; | ||
/** | ||
* Time | ||
*/ | ||
var du = exports.DURATION_UNITS = { ns: 1 }; | ||
const du = (exports.DURATION_UNITS = { ns: 1 }); | ||
du.us = 1000 * du.ns; | ||
@@ -46,0 +17,0 @@ du.ms = 1000 * du.us; |
@@ -1,82 +0,77 @@ | ||
/** | ||
* Consul client | ||
*/ | ||
const papi = require("papi"); | ||
'use strict'; | ||
const Acl = require("./acl").Acl; | ||
const Agent = require("./agent").Agent; | ||
const Catalog = require("./catalog").Catalog; | ||
const Event = require("./event").Event; | ||
const Health = require("./health").Health; | ||
const Kv = require("./kv").Kv; | ||
const Query = require("./query").Query; | ||
const Session = require("./session").Session; | ||
const Status = require("./status").Status; | ||
const Watch = require("./watch").Watch; | ||
const Transaction = require("./transaction").Transaction; | ||
const utils = require("./utils"); | ||
/** | ||
* Module dependencies. | ||
*/ | ||
class Consul extends papi.Client { | ||
constructor(opts) { | ||
opts = utils.defaults({}, opts); | ||
var papi = require('papi'); | ||
var util = require('util'); | ||
if (!opts.baseUrl) { | ||
opts.baseUrl = | ||
(opts.secure ? "https:" : "http:") + | ||
"//" + | ||
(opts.host || "127.0.0.1") + | ||
":" + | ||
(opts.port || 8500) + | ||
"/v1"; | ||
} | ||
opts.name = "consul"; | ||
opts.type = "json"; | ||
var Acl = require('./acl').Acl; | ||
var Agent = require('./agent').Agent; | ||
var Catalog = require('./catalog').Catalog; | ||
var Event = require('./event').Event; | ||
var Health = require('./health').Health; | ||
var Kv = require('./kv').Kv; | ||
var Lock = require('./lock').Lock; | ||
var Query = require('./query').Query; | ||
var Session = require('./session').Session; | ||
var Status = require('./status').Status; | ||
var Watch = require('./watch').Watch; | ||
var Transaction = require('./transaction').Transaction; | ||
var utils = require('./utils'); | ||
let agent; | ||
if (!opts.agent) { | ||
agent = utils.getAgent(opts.baseUrl); | ||
if (agent) { | ||
opts.agent = agent; | ||
} | ||
} | ||
/** | ||
* Initialize a new `Consul` client. | ||
*/ | ||
let defaults; | ||
if (opts.defaults) { | ||
defaults = utils.defaultCommonOptions(opts.defaults); | ||
} | ||
delete opts.defaults; | ||
function Consul(opts) { | ||
if (!(this instanceof Consul)) { | ||
return new Consul(opts); | ||
} | ||
super(opts); | ||
opts = utils.defaults({}, opts); | ||
if (defaults) this._defaults = defaults; | ||
if (!opts.baseUrl) { | ||
opts.baseUrl = (opts.secure ? 'https:' : 'http:') + '//' + | ||
(opts.host || '127.0.0.1') + ':' + | ||
(opts.port || 8500) + '/v1'; | ||
this.acl = new Consul.Acl(this); | ||
this.agent = new Consul.Agent(this); | ||
this.catalog = new Consul.Catalog(this); | ||
this.event = new Consul.Event(this); | ||
this.health = new Consul.Health(this); | ||
this.kv = new Consul.Kv(this); | ||
this.query = new Consul.Query(this); | ||
this.session = new Consul.Session(this); | ||
this.status = new Consul.Status(this); | ||
this.transaction = new Consul.Transaction(this); | ||
} | ||
opts.name = 'consul'; | ||
opts.type = 'json'; | ||
if (opts.defaults) { | ||
var defaults = utils.defaultCommonOptions(opts.defaults); | ||
if (defaults) this._defaults = defaults; | ||
destroy() { | ||
if (this._opts.agent && this._opts.agent.destroy) { | ||
this._opts.agent.destroy(); | ||
} | ||
} | ||
delete opts.defaults; | ||
papi.Client.call(this, opts); | ||
watch(opts) { | ||
return new Consul.Watch(this, opts); | ||
} | ||
this.acl = new Consul.Acl(this); | ||
this.agent = new Consul.Agent(this); | ||
this.catalog = new Consul.Catalog(this); | ||
this.event = new Consul.Event(this); | ||
this.health = new Consul.Health(this); | ||
this.kv = new Consul.Kv(this); | ||
this.query = new Consul.Query(this); | ||
this.session = new Consul.Session(this); | ||
this.status = new Consul.Status(this); | ||
this.transaction = new Consul.Transaction(this); | ||
try { | ||
if (opts.promisify) { | ||
if (typeof opts.promisify === 'function') { | ||
papi.tools.promisify(this, opts.promisify); | ||
} else { | ||
papi.tools.promisify(this); | ||
} | ||
} | ||
} catch (err) { | ||
err.message = 'promisify: ' + err.message; | ||
throw err; | ||
static parseQueryMeta(res) { | ||
return utils.parseQueryMeta(res); | ||
} | ||
} | ||
util.inherits(Consul, papi.Client); | ||
Consul.Acl = Acl; | ||
@@ -88,3 +83,2 @@ Consul.Agent = Agent; | ||
Consul.Kv = Kv; | ||
Consul.Lock = Lock; | ||
Consul.Query = Query; | ||
@@ -96,52 +90,2 @@ Consul.Session = Session; | ||
/** | ||
* Object meta | ||
*/ | ||
Consul.meta = {}; | ||
/** | ||
* Lock helper. | ||
*/ | ||
Consul.meta.lock = { type: 'eventemitter' }; | ||
Consul.prototype.lock = function(opts) { | ||
return new Consul.Lock(this, opts); | ||
}; | ||
/** | ||
* Watch helper. | ||
*/ | ||
Consul.meta.watch = { type: 'eventemitter' }; | ||
Consul.prototype.watch = function(opts) { | ||
return new Consul.Watch(this, opts); | ||
}; | ||
/** | ||
* Walk methods | ||
*/ | ||
Consul.meta.walk = { type: 'sync' }; | ||
Consul.walk = Consul.prototype.walk = function() { | ||
return papi.tools.walk(Consul); | ||
}; | ||
/** | ||
* Parse query meta | ||
*/ | ||
Consul.meta.parseQueryMeta = { type: 'sync' }; | ||
Consul.parseQueryMeta = Consul.prototype.parseQueryMeta = function(res) { | ||
return utils.parseQueryMeta(res); | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Consul = Consul; |
@@ -1,15 +0,11 @@ | ||
/** | ||
* Errors | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
/** | ||
* Create | ||
* Create error | ||
*/ | ||
function create(message) { | ||
var error = message instanceof Error ? | ||
message : | ||
new Error(message ? message : undefined); | ||
const error = | ||
message instanceof Error | ||
? message | ||
: new Error(message ? message : undefined); | ||
@@ -22,7 +18,6 @@ error.isConsul = true; | ||
/** | ||
* Validation | ||
* Create validation error | ||
*/ | ||
function validation(message) { | ||
var error = create(message); | ||
const error = create(message); | ||
@@ -34,7 +29,3 @@ error.isValidation = true; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Consul = create; | ||
exports.Validation = validation; |
167
lib/event.js
@@ -1,118 +0,91 @@ | ||
/** | ||
* Events | ||
*/ | ||
const errors = require("./errors"); | ||
const utils = require("./utils"); | ||
'use strict'; | ||
class Event { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Fires a new user event | ||
*/ | ||
async fire(opts) { | ||
let options; | ||
if (arguments.length === 2) { | ||
options = { | ||
name: arguments[0], | ||
payload: arguments[1], | ||
}; | ||
} else if (typeof opts === "string") { | ||
options = { name: opts }; | ||
} else { | ||
options = opts; | ||
} | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
options = utils.normalizeKeys(options); | ||
options = utils.defaults(options, this.consul._defaults); | ||
/** | ||
* Initialize a new `Event` client. | ||
*/ | ||
function Event(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Fires a new user event | ||
*/ | ||
Event.prototype.fire = function(opts, callback) { | ||
var options; | ||
if (arguments.length === 3) { | ||
options = { | ||
name: arguments[0], | ||
payload: arguments[1], | ||
const req = { | ||
name: "event.fire", | ||
path: "/event/fire/{name}", | ||
params: { name: options.name }, | ||
query: {}, | ||
}; | ||
callback = arguments[2]; | ||
} else if (typeof opts === 'string') { | ||
options = { name: opts }; | ||
} else { | ||
options = opts; | ||
} | ||
options = utils.normalizeKeys(options); | ||
options = utils.defaults(options, this.consul._defaults); | ||
if (!options.name) { | ||
throw this.consul._err(errors.Validation("name required"), req); | ||
} | ||
var req = { | ||
name: 'event.fire', | ||
path: '/event/fire/{name}', | ||
params: { name: options.name }, | ||
query: {}, | ||
}; | ||
let buffer; | ||
if (!options.name) { | ||
return callback(this.consul._err(errors.Validation('name required'), req)); | ||
} | ||
if (options.hasOwnProperty("payload")) { | ||
buffer = Buffer.isBuffer(options.payload); | ||
req.body = buffer ? options.payload : Buffer.from(options.payload); | ||
} | ||
if (options.node) req.query.node = options.node; | ||
if (options.service) req.query.service = options.service; | ||
if (options.tag) req.query.tag = options.tag; | ||
var buffer; | ||
utils.options(req, options); | ||
if (options.hasOwnProperty('payload')) { | ||
buffer = Buffer.isBuffer(options.payload); | ||
req.body = buffer ? options.payload : Buffer.from(options.payload); | ||
return await this.consul._put(req, utils.body).then((data) => { | ||
if (data.hasOwnProperty("Payload")) { | ||
data.Payload = utils.decode(data.Payload, { buffer: buffer }); | ||
} | ||
return data; | ||
}); | ||
} | ||
if (options.node) req.query.node = options.node; | ||
if (options.service) req.query.service = options.service; | ||
if (options.tag) req.query.tag = options.tag; | ||
utils.options(req, options); | ||
this.consul._put(req, utils.body, function(err, data, res) { | ||
if (err) return callback(err, undefined, res); | ||
if (data.hasOwnProperty('Payload')) { | ||
data.Payload = utils.decode(data.Payload, { buffer: buffer }); | ||
/** | ||
* Lists the most recent events an agent has seen | ||
*/ | ||
async list(opts) { | ||
if (typeof opts === "string") { | ||
opts = { name: opts }; | ||
} | ||
callback(null, data, res); | ||
}); | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Lists the most recent events an agent has seen | ||
*/ | ||
const req = { | ||
name: "event.list", | ||
path: "/event/list", | ||
query: {}, | ||
}; | ||
Event.prototype.list = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { name: opts }; | ||
} else if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
} | ||
if (opts.name) req.query.name = opts.name; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
var req = { | ||
name: 'event.list', | ||
path: '/event/list', | ||
query: {}, | ||
}; | ||
if (opts.name) req.query.name = opts.name; | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, function(err, data, res) { | ||
if (err) return callback(err, undefined, res); | ||
data.forEach(function(item) { | ||
if (!item.hasOwnProperty('Payload')) return; | ||
item.Payload = utils.decode(item.Payload, opts); | ||
return await this.consul._get(req, utils.body).then((data) => { | ||
data.forEach((item) => { | ||
if (item.hasOwnProperty("Payload")) { | ||
item.Payload = utils.decode(item.Payload, opts); | ||
} | ||
}); | ||
return data; | ||
}); | ||
} | ||
} | ||
callback(null, data, res); | ||
}); | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Event = Event; |
@@ -1,143 +0,126 @@ | ||
/** | ||
* Health information | ||
*/ | ||
const constants = require("./constants"); | ||
const errors = require("./errors"); | ||
const utils = require("./utils"); | ||
'use strict'; | ||
class Health { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Returns the health info of a node | ||
*/ | ||
async node(opts) { | ||
if (typeof opts === "string") { | ||
opts = { node: opts }; | ||
} | ||
var constants = require('./constants'); | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Initialize a new `Health` client. | ||
*/ | ||
const req = { | ||
name: "health.node", | ||
path: "/health/node/{node}", | ||
params: { node: opts.node }, | ||
}; | ||
function Health(consul) { | ||
this.consul = consul; | ||
} | ||
if (!opts.node) { | ||
throw this.consul._err(errors.Validation("node required"), req); | ||
} | ||
/** | ||
* Returns the health info of a node | ||
*/ | ||
utils.options(req, opts); | ||
Health.prototype.node = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { node: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Returns the checks of a service | ||
*/ | ||
async checks(opts) { | ||
if (typeof opts === "string") { | ||
opts = { service: opts }; | ||
} | ||
var req = { | ||
name: 'health.node', | ||
path: '/health/node/{node}', | ||
params: { node: opts.node }, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.node) { | ||
return callback(this.consul._err(errors.Validation('node required'), req)); | ||
} | ||
const req = { | ||
name: "health.checks", | ||
path: "/health/checks/{service}", | ||
params: { service: opts.service }, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.service) { | ||
throw this.consul._err(errors.Validation("service required"), req); | ||
} | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Returns the checks of a service | ||
*/ | ||
Health.prototype.checks = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { service: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Returns the nodes and health info of a service | ||
*/ | ||
async service(opts) { | ||
if (typeof opts === "string") { | ||
opts = { service: opts }; | ||
} | ||
var req = { | ||
name: 'health.checks', | ||
path: '/health/checks/{service}', | ||
params: { service: opts.service }, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.service) { | ||
return callback(this.consul._err(errors.Validation('service required'), req)); | ||
} | ||
const req = { | ||
name: "health.service", | ||
path: "/health/service/{service}", | ||
params: { service: opts.service }, | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.service) { | ||
throw this.consul._err(errors.Validation("service required"), req); | ||
} | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
if (opts.tag) req.query.tag = opts.tag; | ||
if (opts.passing) req.query.passing = "true"; | ||
/** | ||
* Returns the nodes and health info of a service | ||
*/ | ||
utils.options(req, opts); | ||
Health.prototype.service = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { service: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Returns the checks in a given state | ||
*/ | ||
async state(opts) { | ||
if (typeof opts === "string") { | ||
opts = { state: opts }; | ||
} | ||
var req = { | ||
name: 'health.service', | ||
path: '/health/service/{service}', | ||
params: { service: opts.service }, | ||
query: {}, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.service) { | ||
return callback(this.consul._err(errors.Validation('service required'), req)); | ||
} | ||
const req = { | ||
name: "health.state", | ||
path: "/health/state/{state}", | ||
params: { state: opts.state }, | ||
}; | ||
if (opts.tag) req.query.tag = opts.tag; | ||
if (opts.passing) req.query.passing = 'true'; | ||
if (!opts.state) { | ||
throw this.consul._err(errors.Validation("state required"), req); | ||
} | ||
utils.options(req, opts); | ||
if (opts.state !== "any" && constants.CHECK_STATE.indexOf(opts.state) < 0) { | ||
throw this.consul._err( | ||
errors.Validation("state invalid: " + opts.state), | ||
req | ||
); | ||
} | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Returns the checks in a given state | ||
*/ | ||
Health.prototype.state = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { state: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'health.state', | ||
path: '/health/state/{state}', | ||
params: { state: opts.state }, | ||
}; | ||
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(errors.Validation('state invalid: ' + opts.state), req)); | ||
} | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Health = Health; |
@@ -1,17 +0,3 @@ | ||
/** | ||
* Module index | ||
*/ | ||
const { Consul } = require("./consul"); | ||
'use strict'; | ||
/** | ||
* Module dependencies. | ||
*/ | ||
var Consul = require('./consul').Consul; | ||
/** | ||
* Module exports. | ||
*/ | ||
module.exports = Consul; |
302
lib/kv.js
@@ -1,198 +0,180 @@ | ||
/** | ||
* Key/Value store | ||
*/ | ||
const errors = require("./errors"); | ||
const utils = require("./utils"); | ||
'use strict'; | ||
class Kv { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Get | ||
*/ | ||
async get(opts) { | ||
if (typeof opts === "string") { | ||
opts = { key: opts }; | ||
} | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Initialize a new `Session` client. | ||
*/ | ||
const req = { | ||
name: "kv.get", | ||
path: "/kv/{key}", | ||
params: { key: opts.key || "" }, | ||
query: {}, | ||
}; | ||
function Kv(consul) { | ||
this.consul = consul; | ||
} | ||
if (opts.recurse) req.query.recurse = "true"; | ||
if (opts.raw) { | ||
req.query.raw = "true"; | ||
req.buffer = true; | ||
} | ||
/** | ||
* Object meta | ||
*/ | ||
utils.options(req, opts); | ||
Kv.meta = {}; | ||
return await this.consul._get(req, function (request, next) { | ||
const res = request.res; | ||
/** | ||
* Get | ||
*/ | ||
if (res && res.statusCode === 404) { | ||
return next(false, undefined, utils.responseResult(request)); | ||
} | ||
if (request.err) { | ||
utils.applyErrorResponse(request); | ||
return next(request.err); | ||
} | ||
if (opts.raw) { | ||
return next(false, undefined, utils.responseResult(request, res.body)); | ||
} | ||
Kv.prototype.get = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { key: opts }; | ||
} | ||
if (res.body && Array.isArray(res.body) && res.body.length) { | ||
res.body.forEach((item) => { | ||
if (item.hasOwnProperty("Value")) { | ||
item.Value = utils.decode(item.Value, opts); | ||
} | ||
}); | ||
} else { | ||
return next(false, undefined, utils.responseResult(request)); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.recurse) { | ||
return next( | ||
false, | ||
undefined, | ||
utils.responseResult(request, res.body[0]) | ||
); | ||
} | ||
var req = { | ||
name: 'kv.get', | ||
path: '/kv/{key}', | ||
params: { key: (opts.key || '') }, | ||
query: {}, | ||
}; | ||
if (opts.recurse) req.query.recurse = 'true'; | ||
if (opts.raw) { | ||
req.query.raw = 'true'; | ||
req.buffer = true; | ||
next(false, undefined, utils.responseResult(request, res.body)); | ||
}); | ||
} | ||
utils.options(req, opts); | ||
/** | ||
* Keys | ||
*/ | ||
async keys(opts) { | ||
if (typeof opts === "string") { | ||
opts = { key: opts }; | ||
} | ||
this.consul._get(req, function(err, res) { | ||
if (res && res.statusCode === 404) return callback(undefined, undefined, res); | ||
if (err) return callback(err, undefined, res); | ||
if (opts.raw) return callback(null, res.body, res); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (res.body && Array.isArray(res.body) && res.body.length) { | ||
res.body.forEach(function(item) { | ||
if (!item.hasOwnProperty('Value')) return; | ||
item.Value = utils.decode(item.Value, opts); | ||
}); | ||
} else { | ||
return callback(undefined, undefined, res); | ||
} | ||
const req = { | ||
name: "kv.keys", | ||
path: "/kv/{key}", | ||
params: { key: opts.key || "" }, | ||
query: { keys: true }, | ||
}; | ||
if (!opts.recurse) return callback(null, res.body[0], res); | ||
if (opts.separator) req.query.separator = opts.separator; | ||
callback(null, res.body, res); | ||
}); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Keys | ||
*/ | ||
Kv.prototype.keys = function(opts, callback) { | ||
switch (typeof opts) { | ||
case 'string': | ||
opts = { key: opts }; | ||
break; | ||
case 'function': | ||
callback = opts; | ||
opts = {}; | ||
break; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Set | ||
*/ | ||
async set(opts) { | ||
let options; | ||
switch (arguments.length) { | ||
case 3: | ||
// set(key, value, opts) | ||
options = arguments[2]; | ||
options.key = arguments[0]; | ||
options.value = arguments[1]; | ||
break; | ||
case 2: | ||
// set(key, value) | ||
options = { | ||
key: arguments[0], | ||
value: arguments[1], | ||
}; | ||
break; | ||
default: | ||
options = opts; | ||
} | ||
var req = { | ||
name: 'kv.keys', | ||
path: '/kv/{key}', | ||
params: { key: (opts.key || '') }, | ||
query: { keys: true }, | ||
}; | ||
options = utils.normalizeKeys(options); | ||
options = utils.defaults(options, this.consul._defaults); | ||
if (opts.separator) req.query.separator = opts.separator; | ||
const req = { | ||
name: "kv.set", | ||
path: "/kv/{key}", | ||
params: { key: options.key }, | ||
query: {}, | ||
type: "text", | ||
body: options.value || "", | ||
}; | ||
utils.options(req, opts); | ||
if (!options.key) { | ||
throw this.consul._err(errors.Validation("key required"), req); | ||
} | ||
if (!options.hasOwnProperty("value")) { | ||
throw this.consul._err(errors.Validation("value required"), req); | ||
} | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
if (options.hasOwnProperty("cas")) req.query.cas = options.cas; | ||
if (options.hasOwnProperty("flags")) req.query.flags = options.flags; | ||
if (options.hasOwnProperty("acquire")) req.query.acquire = options.acquire; | ||
if (options.hasOwnProperty("release")) req.query.release = options.release; | ||
/** | ||
* Set | ||
*/ | ||
utils.options(req, options); | ||
Kv.prototype.set = function(opts, callback) { | ||
var options; | ||
switch (arguments.length) { | ||
case 4: | ||
// set(key, value, opts, callback) | ||
options = arguments[2]; | ||
options.key = arguments[0]; | ||
options.value = arguments[1]; | ||
callback = arguments[3]; | ||
break; | ||
case 3: | ||
// set(key, value, callback) | ||
options = { | ||
key: arguments[0], | ||
value: arguments[1], | ||
}; | ||
callback = arguments[2]; | ||
break; | ||
default: | ||
options = opts; | ||
return await this.consul._put(req, utils.body); | ||
} | ||
options = utils.normalizeKeys(options); | ||
options = utils.defaults(options, this.consul._defaults); | ||
/** | ||
* Delete | ||
*/ | ||
async del(opts) { | ||
if (typeof opts === "string") { | ||
opts = { key: opts }; | ||
} | ||
var req = { | ||
name: 'kv.set', | ||
path: '/kv/{key}', | ||
params: { key: options.key }, | ||
query: {}, | ||
type: 'text', | ||
body: options.value || '', | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!options.key) { | ||
return callback(this.consul._err(errors.Validation('key required'), req)); | ||
} | ||
if (!options.hasOwnProperty('value')) { | ||
return callback(this.consul._err(errors.Validation('value required'), req)); | ||
} | ||
const req = { | ||
name: "kv.del", | ||
path: "/kv/{key}", | ||
params: { key: opts.key || "" }, | ||
query: {}, | ||
}; | ||
if (options.hasOwnProperty('cas')) req.query.cas = options.cas; | ||
if (options.hasOwnProperty('flags')) req.query.flags = options.flags; | ||
if (options.hasOwnProperty('acquire')) req.query.acquire = options.acquire; | ||
if (options.hasOwnProperty('release')) req.query.release = options.release; | ||
if (opts.recurse) req.query.recurse = "true"; | ||
utils.options(req, options); | ||
if (opts.hasOwnProperty("cas")) req.query.cas = opts.cas; | ||
this.consul._put(req, utils.body, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Delete | ||
*/ | ||
return await this.consul._delete(req, utils.body); | ||
} | ||
Kv.prototype.del = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { key: opts }; | ||
delete() { | ||
return this.del.apply(this, arguments); | ||
} | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'kv.del', | ||
path: '/kv/{key}', | ||
params: { key: (opts.key || '') }, | ||
query: {}, | ||
}; | ||
if (opts.recurse) req.query.recurse = 'true'; | ||
if (opts.hasOwnProperty('cas')) req.query.cas = opts.cas; | ||
utils.options(req, opts); | ||
this.consul._delete(req, utils.body, callback); | ||
}; | ||
Kv.meta.delete = { type: 'alias' }; | ||
Kv.prototype.delete = Kv.prototype.del; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Kv = Kv; |
401
lib/query.js
@@ -1,281 +0,244 @@ | ||
/** | ||
* Query manipulation | ||
*/ | ||
const errors = require("./errors"); | ||
const utils = require("./utils"); | ||
'use strict'; | ||
class Query { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Lists all queries | ||
*/ | ||
async list(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
const req = { | ||
name: "query.list", | ||
path: "/query", | ||
}; | ||
/** | ||
* Initialize a new `Query` client. | ||
*/ | ||
utils.options(req, opts); | ||
function Query(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Lists all queries | ||
*/ | ||
Query.prototype.list = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Create a new query | ||
*/ | ||
async create(opts) { | ||
if (typeof opts === "string") { | ||
opts = { service: { service: opts } }; | ||
} | ||
var req = { | ||
name: 'query.list', | ||
path: '/query', | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
const req = { | ||
name: "query.create", | ||
path: "/query", | ||
query: {}, | ||
type: "json", | ||
}; | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
this._params(req, opts); | ||
/** | ||
* Create a new query | ||
*/ | ||
Query.prototype.create = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { service: { service: opts } }; | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'query.create', | ||
path: '/query', | ||
query: {}, | ||
type: 'json', | ||
}; | ||
try { | ||
this._params(req, opts); | ||
if (!req.body.Service || !req.body.Service.Service) { | ||
throw errors.Validation('service required'); | ||
throw this.consul._err(errors.Validation("service required"), req); | ||
} | ||
} catch (err) { | ||
return callback(this.consul._err(err, req)); | ||
} | ||
utils.options(req, opts, { near: true }); | ||
utils.options(req, opts, { near: true }); | ||
this.consul._post(req, utils.body, callback); | ||
}; | ||
/** | ||
* Gets a given query | ||
*/ | ||
Query.prototype.get = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { query: opts }; | ||
return await this.consul._post(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Gets a given query | ||
*/ | ||
async get(opts) { | ||
if (typeof opts === "string") { | ||
opts = { query: opts }; | ||
} | ||
var req = { | ||
name: 'query.get', | ||
path: '/query/{query}', | ||
params: { query: opts.query }, | ||
query: {}, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.query) { | ||
return callback(this.consul._err(errors.Validation('query required'), req)); | ||
} | ||
const req = { | ||
name: "query.get", | ||
path: "/query/{query}", | ||
params: { query: opts.query }, | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.query) { | ||
throw this.consul._err(errors.Validation("query required"), req); | ||
} | ||
this.consul._get(req, utils.bodyItem, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Update existing query | ||
*/ | ||
Query.prototype.update = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._get(req, utils.bodyItem); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Update existing query | ||
*/ | ||
async update(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'query.update', | ||
path: '/query/{query}', | ||
params: { query: opts.query }, | ||
query: {}, | ||
type: 'json', | ||
}; | ||
const req = { | ||
name: "query.update", | ||
path: "/query/{query}", | ||
params: { query: opts.query }, | ||
query: {}, | ||
type: "json", | ||
}; | ||
try { | ||
if (!opts.query) throw errors.Validation('query required'); | ||
if (!opts.query) { | ||
throw this.consul._err(errors.Validation("query required"), req); | ||
} | ||
this._params(req, opts); | ||
if (!req.body.Service || !req.body.Service.Service) { | ||
throw errors.Validation('service required'); | ||
throw this.consul._err(errors.Validation("service required"), req); | ||
} | ||
} catch (err) { | ||
return callback(this.consul._err(err, req)); | ||
} | ||
utils.options(req, opts, { near: true }); | ||
utils.options(req, opts, { near: true }); | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
/** | ||
* Destroys a given query | ||
*/ | ||
Query.prototype.destroy = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { query: opts }; | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Destroys a given query | ||
*/ | ||
async destroy(opts) { | ||
if (typeof opts === "string") { | ||
opts = { query: opts }; | ||
} | ||
var req = { | ||
name: 'query.destroy', | ||
path: '/query/{query}', | ||
params: { query: opts.query }, | ||
query: {}, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.query) { | ||
return callback(this.consul._err(errors.Validation('query required'), req)); | ||
} | ||
const req = { | ||
name: "query.destroy", | ||
path: "/query/{query}", | ||
params: { query: opts.query }, | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.query) { | ||
throw this.consul._err(errors.Validation("query required"), req); | ||
} | ||
this.consul._delete(req, utils.empty, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Executes a given query | ||
*/ | ||
Query.prototype.execute = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { query: opts }; | ||
return await this.consul._delete(req, utils.empty); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Executes a given query | ||
*/ | ||
async execute(opts) { | ||
if (typeof opts === "string") { | ||
opts = { query: opts }; | ||
} | ||
var req = { | ||
name: 'query.execute', | ||
path: '/query/{query}/execute', | ||
params: { query: opts.query }, | ||
query: {}, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.query) { | ||
return callback(this.consul._err(errors.Validation('query required'), req)); | ||
} | ||
const req = { | ||
name: "query.execute", | ||
path: "/query/{query}/execute", | ||
params: { query: opts.query }, | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.query) { | ||
throw this.consul._err(errors.Validation("query required"), req); | ||
} | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Explain a given query | ||
*/ | ||
Query.prototype.explain = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { query: opts }; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Explain a given query | ||
*/ | ||
async explain(opts) { | ||
if (typeof opts === "string") { | ||
opts = { query: opts }; | ||
} | ||
var req = { | ||
name: 'query.explain', | ||
path: '/query/{query}/explain', | ||
params: { query: opts.query }, | ||
query: {}, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.query) { | ||
return callback(this.consul._err(errors.Validation('query required'), req)); | ||
} | ||
const req = { | ||
name: "query.explain", | ||
path: "/query/{query}/explain", | ||
params: { query: opts.query }, | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.query) { | ||
throw this.consul._err(errors.Validation("query required"), req); | ||
} | ||
this.consul._get(req, utils.bodyItem, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Generate body for query create and update | ||
*/ | ||
return await this.consul._get(req, utils.bodyItem); | ||
} | ||
Query.prototype._params = function(req, opts) { | ||
var body = req.body || {}; | ||
/** | ||
* Generate body for query create and update | ||
*/ | ||
_params(req, opts) { | ||
const body = req.body || {}; | ||
if (opts.name) body.Name = opts.name; | ||
if (opts.session) body.Session = opts.session; | ||
if (opts.token) { | ||
body.Token = opts.token; | ||
delete opts.token; | ||
} | ||
if (opts.near) body.Near = opts.near; | ||
if (opts.template) { | ||
var template = utils.normalizeKeys(opts.template); | ||
if (template.type || template.regexp) { | ||
body.Template = {}; | ||
if (template.type) body.Template.Type = template.type; | ||
if (template.regexp) body.Template.Regexp = template.regexp; | ||
if (opts.name) body.Name = opts.name; | ||
if (opts.session) body.Session = opts.session; | ||
if (opts.token) { | ||
body.Token = opts.token; | ||
delete opts.token; | ||
} | ||
} | ||
if (opts.service) { | ||
var service = utils.normalizeKeys(opts.service); | ||
body.Service = {}; | ||
if (service.service) body.Service.Service = service.service; | ||
if (service.failover) { | ||
var failover = utils.normalizeKeys(service.failover); | ||
if (typeof failover.nearestn === 'number' || failover.datacenters) { | ||
body.Service.Failover = {}; | ||
if (typeof failover.nearestn === 'number') { | ||
body.Service.Failover.NearestN = failover.nearestn; | ||
if (opts.near) body.Near = opts.near; | ||
if (opts.template) { | ||
const template = utils.normalizeKeys(opts.template); | ||
if (template.type || template.regexp) { | ||
body.Template = {}; | ||
if (template.type) body.Template.Type = template.type; | ||
if (template.regexp) body.Template.Regexp = template.regexp; | ||
} | ||
} | ||
if (opts.service) { | ||
const service = utils.normalizeKeys(opts.service); | ||
body.Service = {}; | ||
if (service.service) body.Service.Service = service.service; | ||
if (service.failover) { | ||
const failover = utils.normalizeKeys(service.failover); | ||
if (typeof failover.nearestn === "number" || failover.datacenters) { | ||
body.Service.Failover = {}; | ||
if (typeof failover.nearestn === "number") { | ||
body.Service.Failover.NearestN = failover.nearestn; | ||
} | ||
if (failover.datacenters) { | ||
body.Service.Failover.Datacenters = failover.datacenters; | ||
} | ||
} | ||
if (failover.datacenters) { | ||
body.Service.Failover.Datacenters = failover.datacenters; | ||
} | ||
} | ||
if (typeof service.onlypassing === "boolean") { | ||
body.Service.OnlyPassing = service.onlypassing; | ||
} | ||
if (service.tags) body.Service.Tags = service.tags; | ||
} | ||
if (typeof service.onlypassing === 'boolean') { | ||
body.Service.OnlyPassing = service.onlypassing; | ||
if (opts.dns) { | ||
const dns = utils.normalizeKeys(opts.dns); | ||
if (dns.ttl) body.DNS = { TTL: dns.ttl }; | ||
} | ||
if (service.tags) body.Service.Tags = service.tags; | ||
req.body = body; | ||
} | ||
if (opts.dns) { | ||
var dns = utils.normalizeKeys(opts.dns); | ||
if (dns.ttl) body.DNS = { TTL: dns.ttl }; | ||
} | ||
} | ||
req.body = body; | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Query = Query; |
@@ -1,194 +0,164 @@ | ||
/** | ||
* Session manipulation | ||
*/ | ||
const errors = require("./errors"); | ||
const utils = require("./utils"); | ||
'use strict'; | ||
class Session { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Creates a new session | ||
*/ | ||
async create(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
const req = { | ||
name: "session.create", | ||
path: "/session/create", | ||
query: {}, | ||
type: "json", | ||
body: {}, | ||
}; | ||
/** | ||
* Initialize a new `Session` client. | ||
*/ | ||
if (opts.lockdelay) req.body.LockDelay = opts.lockdelay; | ||
if (opts.name) req.body.Name = opts.name; | ||
if (opts.node) req.body.Node = opts.node; | ||
if (opts.checks) req.body.Checks = opts.checks; | ||
if (opts.behavior) req.body.Behavior = opts.behavior; | ||
if (opts.ttl) req.body.TTL = opts.ttl; | ||
function Session(consul) { | ||
this.consul = consul; | ||
} | ||
utils.options(req, opts); | ||
/** | ||
* Creates a new session | ||
*/ | ||
Session.prototype.create = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._put(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Destroys a given session | ||
*/ | ||
async destroy(opts) { | ||
if (typeof opts === "string") { | ||
opts = { id: opts }; | ||
} | ||
var req = { | ||
name: 'session.create', | ||
path: '/session/create', | ||
query: {}, | ||
type: 'json', | ||
body: {}, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (opts.lockdelay) req.body.LockDelay = opts.lockdelay; | ||
if (opts.name) req.body.Name = opts.name; | ||
if (opts.node) req.body.Node = opts.node; | ||
if (opts.checks) req.body.Checks = opts.checks; | ||
if (opts.behavior) req.body.Behavior = opts.behavior; | ||
if (opts.ttl) req.body.TTL = opts.ttl; | ||
const req = { | ||
name: "session.destroy", | ||
path: "/session/destroy/{id}", | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.id) { | ||
throw this.consul._err(errors.Validation("id required"), req); | ||
} | ||
this.consul._put(req, utils.body, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Destroys a given session | ||
*/ | ||
Session.prototype.destroy = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
return await this.consul._put(req, utils.empty); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Queries a given session | ||
*/ | ||
async info(opts) { | ||
if (typeof opts === "string") { | ||
opts = { id: opts }; | ||
} | ||
var req = { | ||
name: 'session.destroy', | ||
path: '/session/destroy/{id}', | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
const req = { | ||
name: "session.info", | ||
path: "/session/info/{id}", | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
utils.options(req, opts); | ||
if (!opts.id) { | ||
throw this.consul._err(errors.Validation("id required"), req); | ||
} | ||
this.consul._put(req, utils.empty, callback); | ||
}; | ||
utils.options(req, opts); | ||
/** | ||
* Queries a given session | ||
*/ | ||
Session.prototype.info = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
return await this.consul._get(req, utils.bodyItem); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'session.info', | ||
path: '/session/info/{id}', | ||
params: { id: opts.id }, | ||
query: {}, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
get(opts) { | ||
return this.info(opts); | ||
} | ||
utils.options(req, opts); | ||
/** | ||
* Lists sessions belonging to a node | ||
*/ | ||
async node(opts) { | ||
if (typeof opts === "string") { | ||
opts = { node: opts }; | ||
} | ||
this.consul._get(req, utils.bodyItem, callback); | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
Session.prototype.get = Session.prototype.info; | ||
const req = { | ||
name: "session.node", | ||
path: "/session/node/{node}", | ||
params: { node: opts.node }, | ||
}; | ||
/** | ||
* Lists sessions belonging to a node | ||
*/ | ||
if (!opts.node) { | ||
throw this.consul._err(errors.Validation("node required"), req); | ||
} | ||
Session.prototype.node = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { node: opts }; | ||
} | ||
utils.options(req, opts); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'session.node', | ||
path: '/session/node/{node}', | ||
params: { node: opts.node }, | ||
}; | ||
if (!opts.node) { | ||
return callback(this.consul._err(errors.Validation('node required'), req)); | ||
return await this.consul._get(req, utils.body); | ||
} | ||
utils.options(req, opts); | ||
/** | ||
* Lists all the active sessions | ||
*/ | ||
async list(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
const req = { | ||
name: "session.list", | ||
path: "/session/list", | ||
}; | ||
/** | ||
* Lists all the active sessions | ||
*/ | ||
utils.options(req, opts); | ||
Session.prototype.list = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Renews a TTL-based session | ||
*/ | ||
async renew(opts) { | ||
if (typeof opts === "string") { | ||
opts = { id: opts }; | ||
} | ||
var req = { | ||
name: 'session.list', | ||
path: '/session/list', | ||
}; | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
utils.options(req, opts); | ||
const req = { | ||
name: "session.renew", | ||
path: "/session/renew/{id}", | ||
params: { id: opts.id }, | ||
}; | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
if (!opts.id) { | ||
throw this.consul._err(errors.Validation("id required"), req); | ||
} | ||
/** | ||
* Renews a TTL-based session | ||
*/ | ||
utils.options(req, opts); | ||
Session.prototype.renew = function(opts, callback) { | ||
if (typeof opts === 'string') { | ||
opts = { id: opts }; | ||
return await this.consul._put(req, utils.body); | ||
} | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'session.renew', | ||
path: '/session/renew/{id}', | ||
params: { id: opts.id }, | ||
}; | ||
if (!opts.id) { | ||
return callback(this.consul._err(errors.Validation('id required'), req)); | ||
} | ||
utils.options(req, opts); | ||
this.consul._put(req, utils.body, callback); | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Session = Session; |
@@ -1,71 +0,43 @@ | ||
/** | ||
* Status information | ||
*/ | ||
const utils = require("./utils"); | ||
'use strict'; | ||
class Status { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Returns the current Raft leader. | ||
*/ | ||
async leader(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var utils = require('./utils'); | ||
const req = { | ||
name: "status.leader", | ||
path: "/status/leader", | ||
}; | ||
/** | ||
* Initialize a new `Status` client. | ||
*/ | ||
utils.options(req, opts); | ||
function Status(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Returns the current Raft leader. | ||
*/ | ||
Status.prototype.leader = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Returns the current Raft peer set | ||
*/ | ||
async peers(opts) { | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'status.leader', | ||
path: '/status/leader', | ||
}; | ||
const req = { | ||
name: "status.peers", | ||
path: "/status/peers", | ||
}; | ||
utils.options(req, opts); | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Returns the current Raft peer set | ||
*/ | ||
Status.prototype.peers = function(opts, callback) { | ||
if (!callback) { | ||
callback = opts; | ||
opts = {}; | ||
return await this.consul._get(req, utils.body); | ||
} | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
var req = { | ||
name: 'status.peers', | ||
path: '/status/peers', | ||
}; | ||
utils.options(req, opts); | ||
this.consul._get(req, utils.body, callback); | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Status = Status; |
@@ -1,81 +0,58 @@ | ||
/** | ||
* Transaction | ||
*/ | ||
const errors = require("./errors"); | ||
const utils = require("./utils"); | ||
'use strict'; | ||
class Transaction { | ||
constructor(consul) { | ||
this.consul = consul; | ||
} | ||
/** | ||
* Module dependencies. | ||
*/ | ||
/** | ||
* Create | ||
*/ | ||
async create(operations) { | ||
let opts; | ||
switch (arguments.length) { | ||
case 2: | ||
// create(operations, opts) | ||
opts = utils.clone(arguments[1]); | ||
opts.operations = operations; | ||
break; | ||
case 1: | ||
// create(operations) | ||
opts = { operations }; | ||
break; | ||
default: | ||
throw this.consul._err( | ||
errors.Validation( | ||
"a list of operations are required as first arguments" | ||
), | ||
{ name: "Transaction.create" } | ||
); | ||
} | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
opts = utils.normalizeKeys(opts); | ||
opts = utils.defaults(opts, this.consul._defaults); | ||
/** | ||
* Initialize a new `Transaction` client. | ||
*/ | ||
const req = { | ||
name: "Transaction.create", | ||
path: "/txn", | ||
params: {}, | ||
query: {}, | ||
type: "json", | ||
body: opts.operations, | ||
}; | ||
function Transaction(consul) { | ||
this.consul = consul; | ||
} | ||
if (!(Array.isArray(opts.operations) && opts.operations.length > 0)) { | ||
throw this.consul._err( | ||
errors.Validation("operations must be an array with at least one item"), | ||
req | ||
); | ||
} | ||
/** | ||
* Object meta | ||
*/ | ||
utils.options(req, opts); | ||
Transaction.meta = {}; | ||
/** | ||
* Set | ||
*/ | ||
Transaction.prototype.create = function(operations, callback) { | ||
var options; | ||
switch (arguments.length) { | ||
case 3: | ||
// create(operations, opts, callback) | ||
options = arguments[1]; | ||
options.operations = arguments[0]; | ||
callback = arguments[2]; | ||
break; | ||
case 2: | ||
// create(operations, callback) | ||
options = { | ||
operations: arguments[0], | ||
}; | ||
callback = arguments[1]; | ||
break; | ||
default: | ||
return arguments[0]( | ||
this.consul._err(errors.Validation('a list of operations are required as first arguments'), | ||
{ name: 'Transaction.create' })); | ||
return await this.consul._put(req, utils.body); | ||
} | ||
} | ||
options = utils.normalizeKeys(options); | ||
options = utils.defaults(options, this.consul._defaults); | ||
var req = { | ||
name: 'Transaction.create', | ||
path: '/txn', | ||
params: {}, | ||
query: {}, | ||
type: 'json', | ||
body: options.operations, | ||
}; | ||
if (!(Array.isArray(options.operations) && options.operations.length > 0)) { | ||
return callback( | ||
this.consul._err(errors.Validation('operations must be an array with at least one item'), | ||
req)); | ||
} | ||
utils.options(req, options); | ||
this.consul._put(req, utils.body, callback); | ||
}; | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Transaction = Transaction; |
288
lib/utils.js
@@ -0,12 +1,46 @@ | ||
"use strict"; | ||
const http = require("http"); | ||
const https = require("https"); | ||
const constants = require("./constants"); | ||
/** | ||
* Helper functions | ||
* Get HTTP agent | ||
*/ | ||
function getAgent(baseUrl) { | ||
if (!baseUrl) return; | ||
'use strict'; | ||
let secure; | ||
if (typeof baseUrl === "string") { | ||
secure = !!baseUrl.match(/^https:/i); | ||
} else if (baseUrl.protocol) { | ||
secure = baseUrl.protocol === "https:"; | ||
} else { | ||
return; | ||
} | ||
const Agent = secure ? https.Agent : http.Agent; | ||
return new Agent({ keepAlive: true }); | ||
} | ||
/** | ||
* Module dependencies. | ||
* Inject response result | ||
*/ | ||
function responseResult(request, ...args) { | ||
if (request.ctx && request.ctx.includeResponse) { | ||
return [request.res, ...args]; | ||
} else { | ||
return args[0]; | ||
} | ||
} | ||
var constants = require('./constants'); | ||
/** | ||
* Inject response into error | ||
*/ | ||
function applyErrorResponse(request) { | ||
if (request.err && request.ctx && request.ctx.includeResponse) { | ||
request.err.response = request.res; | ||
} | ||
} | ||
@@ -16,7 +50,9 @@ /** | ||
*/ | ||
function body(request, next) { | ||
if (request.err) return next(false, request.err, undefined, request.res); | ||
if (request.err) { | ||
applyErrorResponse(request); | ||
return next(false, request.err); | ||
} | ||
next(false, undefined, request.res.body, request.res); | ||
next(false, undefined, responseResult(request, request.res.body)); | ||
} | ||
@@ -27,11 +63,13 @@ | ||
*/ | ||
function bodyItem(request, next) { | ||
if (request.err) return next(false, request.err, undefined, request.res); | ||
if (request.err) { | ||
applyErrorResponse(request); | ||
return next(false, request.err); | ||
} | ||
if (request.res.body && request.res.body.length) { | ||
return next(false, undefined, request.res.body[0], request.res); | ||
return next(false, undefined, responseResult(request, request.res.body[0])); | ||
} | ||
next(false, undefined, undefined, request.res); | ||
next(false, undefined, responseResult(request)); | ||
} | ||
@@ -42,7 +80,9 @@ | ||
*/ | ||
function empty(request, next) { | ||
if (request.err) return next(false, request.err, undefined, request.res); | ||
if (request.err) { | ||
applyErrorResponse(request); | ||
return next(false, request.err); | ||
} | ||
next(false, undefined, undefined, request.res); | ||
next(false, undefined, responseResult(request)); | ||
} | ||
@@ -53,10 +93,9 @@ | ||
*/ | ||
function normalizeKeys(obj) { | ||
var result = {}; | ||
const result = {}; | ||
if (obj) { | ||
for (var name in obj) { | ||
for (const name in obj) { | ||
if (obj.hasOwnProperty(name)) { | ||
result[name.replace(/_/g, '').toLowerCase()] = obj[name]; | ||
result[name.replace(/_/g, "").toLowerCase()] = obj[name]; | ||
} | ||
@@ -72,10 +111,9 @@ } | ||
*/ | ||
function defaults(obj) { | ||
if (!obj) obj = {}; | ||
var src; | ||
for (var i = 0; i < arguments.length; i++) { | ||
let src; | ||
for (let i = 0; i < arguments.length; i++) { | ||
src = arguments[i]; | ||
for (var p in src) { | ||
for (const p in src) { | ||
if (src.hasOwnProperty(p) && !obj.hasOwnProperty(p)) { | ||
@@ -93,9 +131,8 @@ obj[p] = src[p]; | ||
*/ | ||
function parseDuration(value) { | ||
if (typeof value === 'number') return value / 1e6; | ||
if (typeof value !== 'string') return; | ||
if (typeof value === "number") return value / 1e6; | ||
if (typeof value !== "string") return; | ||
var n; | ||
var m = value.match(/^(\d*\.?\d*)$/); | ||
let n; | ||
let m = value.match(/^(\d*\.?\d*)$/); | ||
@@ -108,3 +145,3 @@ if (m) { | ||
m = value.match(/^([\d\.]*)(ns|us|ms|s|m|h)$/); | ||
m = value.match(/^([\d.]*)(ns|us|ms|s|m|h)$/); | ||
@@ -117,3 +154,3 @@ if (!m) return; | ||
return n * constants.DURATION_UNITS[m[2]] / 1e6; | ||
return (n * constants.DURATION_UNITS[m[2]]) / 1e6; | ||
} | ||
@@ -124,3 +161,2 @@ | ||
*/ | ||
function options(req, opts, ignore) { | ||
@@ -133,3 +169,4 @@ if (!opts) opts = {}; | ||
// headers | ||
if (opts.hasOwnProperty('token') && !ignore.token) req.headers['x-consul-token'] = opts.token; | ||
if (opts.hasOwnProperty("token") && !ignore.token) | ||
req.headers["x-consul-token"] = opts.token; | ||
@@ -140,22 +177,24 @@ // query | ||
if (opts.dc && !ignore.dc) req.query.dc = opts.dc; | ||
if (opts.wan && !ignore.wan) req.query.wan = '1'; | ||
if (opts.wan && !ignore.wan) req.query.wan = "1"; | ||
if (opts.consistent && !ignore.consistent) { | ||
req.query.consistent = '1'; | ||
req.query.consistent = "1"; | ||
} else if (opts.stale && !ignore.stale) { | ||
req.query.stale = '1'; | ||
req.query.stale = "1"; | ||
} | ||
if (opts.hasOwnProperty('index') && !ignore.index) req.query.index = opts.index; | ||
if (opts.hasOwnProperty('wait') && !ignore.wait) req.query.wait = opts.wait; | ||
if (opts.hasOwnProperty('near') && !ignore.near) req.query.near = opts.near; | ||
if (opts.hasOwnProperty('node-meta') && !ignore['node-meta']) { | ||
req.query['node-meta'] = opts['node-meta']; | ||
if (opts.hasOwnProperty("index") && !ignore.index) | ||
req.query.index = opts.index; | ||
if (opts.hasOwnProperty("wait") && !ignore.wait) req.query.wait = opts.wait; | ||
if (opts.hasOwnProperty("near") && !ignore.near) req.query.near = opts.near; | ||
if (opts.hasOwnProperty("node-meta") && !ignore["node-meta"]) { | ||
req.query["node-meta"] = opts["node-meta"]; | ||
} | ||
if (opts.hasOwnProperty('filter') && !ignore.filter) req.query.filter = opts.filter; | ||
if (opts.hasOwnProperty("filter") && !ignore.filter) | ||
req.query.filter = opts.filter; | ||
// papi | ||
if (opts.hasOwnProperty('ctx') && !ignore.ctx) req.ctx = opts.ctx; | ||
if (opts.hasOwnProperty('timeout') && !ignore.timeout) { | ||
if (typeof opts.timeout === 'string') { | ||
if (opts.hasOwnProperty("ctx") && !ignore.ctx) req.ctx = opts.ctx; | ||
if (opts.hasOwnProperty("timeout") && !ignore.timeout) { | ||
if (typeof opts.timeout === "string") { | ||
req.timeout = parseDuration(opts.timeout); | ||
@@ -171,8 +210,7 @@ } else { | ||
*/ | ||
function defaultCommonOptions(opts) { | ||
opts = normalizeKeys(opts); | ||
var defaults; | ||
let defaults; | ||
constants.DEFAULT_OPTIONS.forEach(function(key) { | ||
constants.DEFAULT_OPTIONS.forEach(function (key) { | ||
if (!opts.hasOwnProperty(key)) return; | ||
@@ -189,6 +227,5 @@ if (!defaults) defaults = {}; | ||
*/ | ||
function decode(value, opts) { | ||
if (typeof value !== 'string') return value; | ||
value = Buffer.from(value, 'base64'); | ||
if (typeof value !== "string") return value; | ||
value = Buffer.from(value, "base64"); | ||
if (!opts || !opts.buffer) value = value.toString(); | ||
@@ -201,7 +238,6 @@ return value; | ||
*/ | ||
function clone(src) { | ||
var dst = {}; | ||
const dst = {}; | ||
for (var key in src) { | ||
for (const key in src) { | ||
if (src.hasOwnProperty(key)) { | ||
@@ -218,12 +254,11 @@ dst[key] = src[key]; | ||
*/ | ||
function setTimeoutContext(fn, ctx, timeout) { | ||
var id; | ||
let id; | ||
var cancel = function() { | ||
const cancel = function () { | ||
clearTimeout(id); | ||
}; | ||
id = setTimeout(function() { | ||
ctx.removeListener('cancel', cancel); | ||
id = setTimeout(function () { | ||
ctx.removeListener("cancel", cancel); | ||
@@ -233,26 +268,10 @@ fn(); | ||
ctx.once('cancel', cancel); | ||
ctx.once("cancel", cancel); | ||
} | ||
/** | ||
* Set interval with cancel support | ||
*/ | ||
function setIntervalContext(fn, ctx, timeout) { | ||
var id; | ||
var cancel = function() { | ||
clearInterval(id); | ||
}; | ||
id = setInterval(function() { fn(); }, timeout); | ||
ctx.once('cancel', cancel); | ||
} | ||
function _createTaggedAddress(src) { | ||
var dst = {}; | ||
const dst = {}; | ||
if (src.hasOwnProperty('address')) dst.Address = src.address; | ||
if (src.hasOwnProperty('port')) dst.Port = src.port; | ||
if (src.hasOwnProperty("address")) dst.Address = src.address; | ||
if (src.hasOwnProperty("port")) dst.Port = src.port; | ||
@@ -263,3 +282,3 @@ return dst; | ||
function _createTaggedAddresses(src) { | ||
var dst = {}; | ||
const dst = {}; | ||
@@ -285,14 +304,17 @@ if (src.lan) { | ||
*/ | ||
function _createServiceCheck(src) { | ||
var dst = {}; | ||
const dst = {}; | ||
if ((src.grpc || src.http || src.tcp || src.args || src.script) && src.interval) { | ||
if ( | ||
(src.grpc || src.http || src.tcp || src.args || src.script) && | ||
src.interval | ||
) { | ||
if (src.grpc) { | ||
dst.GRPC = src.grpc; | ||
if (src.hasOwnProperty('grpcusetls')) dst.GRPCUseTLS = src.grpcusetls; | ||
if (src.hasOwnProperty("grpcusetls")) dst.GRPCUseTLS = src.grpcusetls; | ||
} else if (src.http) { | ||
dst.HTTP = src.http; | ||
if (src.hasOwnProperty('tlsskipverify')) dst.TLSSkipVerify = src.tlsskipverify; | ||
} else if (src.tcp){ | ||
if (src.hasOwnProperty("tlsskipverify")) | ||
dst.TLSSkipVerify = src.tlsskipverify; | ||
} else if (src.tcp) { | ||
dst.TCP = src.tcp; | ||
@@ -305,24 +327,27 @@ } else { | ||
} | ||
if (src.hasOwnProperty('dockercontainerid')) dst.DockerContainerID = src.dockercontainerid; | ||
if (src.hasOwnProperty('shell')) dst.Shell = src.shell; | ||
if (src.hasOwnProperty("dockercontainerid")) | ||
dst.DockerContainerID = src.dockercontainerid; | ||
if (src.hasOwnProperty("shell")) dst.Shell = src.shell; | ||
} | ||
dst.Interval = src.interval; | ||
if (src.hasOwnProperty('timeout')) dst.Timeout = src.timeout; | ||
if (src.hasOwnProperty("timeout")) dst.Timeout = src.timeout; | ||
} else if (src.ttl) { | ||
dst.TTL = src.ttl; | ||
} else if (src.aliasnode || src.aliasservice) { | ||
if (src.hasOwnProperty('aliasnode')) dst.AliasNode = src.aliasnode; | ||
if (src.hasOwnProperty('aliasservice')) dst.AliasService = src.aliasservice; | ||
if (src.hasOwnProperty("aliasnode")) dst.AliasNode = src.aliasnode; | ||
if (src.hasOwnProperty("aliasservice")) dst.AliasService = src.aliasservice; | ||
} else { | ||
throw new Error('args/grpc/http/tcp and interval, ttl, or aliasnode/aliasservice'); | ||
throw new Error( | ||
"args/grpc/http/tcp and interval, ttl, or aliasnode/aliasservice" | ||
); | ||
} | ||
if (src.hasOwnProperty('notes')) dst.Notes = src.notes; | ||
if (src.hasOwnProperty('status')) dst.Status = src.status; | ||
if (src.hasOwnProperty('deregistercriticalserviceafter')) { | ||
if (src.hasOwnProperty("notes")) dst.Notes = src.notes; | ||
if (src.hasOwnProperty("status")) dst.Status = src.status; | ||
if (src.hasOwnProperty("deregistercriticalserviceafter")) { | ||
dst.DeregisterCriticalServiceAfter = src.deregistercriticalserviceafter; | ||
} | ||
if (src.hasOwnProperty('failuresbeforecritical')) { | ||
if (src.hasOwnProperty("failuresbeforecritical")) { | ||
dst.FailuresBeforeCritical = src.failuresbeforecritical; | ||
} | ||
if (src.hasOwnProperty('successbeforepassing')) { | ||
if (src.hasOwnProperty("successbeforepassing")) { | ||
dst.SuccessBeforePassing = src.successbeforepassing; | ||
@@ -339,3 +364,3 @@ } | ||
function _createServiceProxy(src) { | ||
var dst = {}; | ||
const dst = {}; | ||
@@ -345,7 +370,10 @@ if (src.destinationservicename) { | ||
} else { | ||
throw Error('destinationservicename required'); | ||
throw Error("destinationservicename required"); | ||
} | ||
if (src.destinationserviceid) dst.DestinationServiceID = src.destinationserviceid; | ||
if (src.localserviceaddress) dst.LocalServiceAddress = src.localserviceaddress; | ||
if (src.hasOwnProperty('localserviceport')) dst.LocalServicePort = src.localserviceport; | ||
if (src.destinationserviceid) | ||
dst.DestinationServiceID = src.destinationserviceid; | ||
if (src.localserviceaddress) | ||
dst.LocalServiceAddress = src.localserviceaddress; | ||
if (src.hasOwnProperty("localserviceport")) | ||
dst.LocalServicePort = src.localserviceport; | ||
if (src.config) dst.Config = src.config; | ||
@@ -360,3 +388,3 @@ if (src.upstreams) dst.Upstreams = src.upstreams; | ||
function _createService(src, isSidecar) { | ||
var dst = {}; | ||
const dst = {}; | ||
@@ -366,5 +394,5 @@ if (src.name) dst.Name = src.name; | ||
if (src.tags) dst.Tags = src.tags; | ||
if (src.meta) dst.Meta = src.meta; | ||
if (src.hasOwnProperty('address')) dst.Address = src.address; | ||
if (src.hasOwnProperty('port')) dst.Port = src.port; | ||
if (src.meta) dst.Meta = src.meta; | ||
if (src.hasOwnProperty("address")) dst.Address = src.address; | ||
if (src.hasOwnProperty("port")) dst.Port = src.port; | ||
@@ -378,6 +406,6 @@ if (Array.isArray(src.checks)) { | ||
if (src.connect) { | ||
var connect = normalizeKeys(src.connect); | ||
const connect = normalizeKeys(src.connect); | ||
dst.Connect = {}; | ||
if (connect.hasOwnProperty('native')) dst.Connect.Native = connect.native; | ||
if (connect.hasOwnProperty("native")) dst.Connect.Native = connect.native; | ||
@@ -391,5 +419,7 @@ if (connect.proxy) { | ||
dst.Connect.SidecarService = _createService( | ||
normalizeKeys(connect.sidecarservice), true); | ||
normalizeKeys(connect.sidecarservice), | ||
true | ||
); | ||
} else { | ||
throw new Error('sidecarservice cannot be nested'); | ||
throw new Error("sidecarservice cannot be nested"); | ||
} | ||
@@ -423,7 +453,6 @@ } | ||
*/ | ||
function createCheck(src) { | ||
src = normalizeKeys(src); | ||
var dst = _createServiceCheck(src); | ||
const dst = _createServiceCheck(src); | ||
@@ -433,7 +462,7 @@ if (src.name) { | ||
} else { | ||
throw new Error('name required'); | ||
throw new Error("name required"); | ||
} | ||
if (src.hasOwnProperty('id')) dst.ID = src.id; | ||
if (src.hasOwnProperty('serviceid')) dst.ServiceID = src.serviceid; | ||
if (src.hasOwnProperty("id")) dst.ID = src.id; | ||
if (src.hasOwnProperty("serviceid")) dst.ServiceID = src.serviceid; | ||
@@ -446,6 +475,5 @@ return dst; | ||
*/ | ||
function hasIndexChanged(index, prevIndex) { | ||
if (typeof index !== 'string' || !index) return false; | ||
if (typeof prevIndex !== 'string' || !prevIndex) return true; | ||
if (typeof index !== "string" || !index) return false; | ||
if (typeof prevIndex !== "string" || !prevIndex) return true; | ||
return index !== prevIndex; | ||
@@ -457,18 +485,18 @@ } | ||
*/ | ||
function parseQueryMeta(res) { | ||
var meta = {}; | ||
const meta = {}; | ||
if (res && res.headers) { | ||
if (res.headers['x-consul-index']) { | ||
meta.LastIndex = res.headers['x-consul-index']; | ||
if (res.headers["x-consul-index"]) { | ||
meta.LastIndex = res.headers["x-consul-index"]; | ||
} | ||
if (res.headers['x-consul-lastcontact']) { | ||
meta.LastContact = parseInt(res.headers['x-consul-lastcontact'], 10); | ||
if (res.headers["x-consul-lastcontact"]) { | ||
meta.LastContact = parseInt(res.headers["x-consul-lastcontact"], 10); | ||
} | ||
if (res.headers['x-consul-knownleader']) { | ||
meta.KnownLeader = res.headers['x-consul-knownleader'] === 'true'; | ||
if (res.headers["x-consul-knownleader"]) { | ||
meta.KnownLeader = res.headers["x-consul-knownleader"] === "true"; | ||
} | ||
if (res.headers['x-consul-translate-addresses']) { | ||
meta.AddressTranslationEnabled = res.headers['x-consul-translate-addresses'] === 'true'; | ||
if (res.headers["x-consul-translate-addresses"]) { | ||
meta.AddressTranslationEnabled = | ||
res.headers["x-consul-translate-addresses"] === "true"; | ||
} | ||
@@ -480,6 +508,5 @@ } | ||
/** | ||
* Module exports | ||
*/ | ||
exports.getAgent = getAgent; | ||
exports.responseResult = responseResult; | ||
exports.applyErrorResponse = applyErrorResponse; | ||
exports.body = body; | ||
@@ -496,3 +523,2 @@ exports.bodyItem = bodyItem; | ||
exports.setTimeoutContext = setTimeoutContext; | ||
exports.setIntervalContext = setIntervalContext; | ||
exports.createServiceCheck = createServiceCheck; | ||
@@ -499,0 +525,0 @@ exports.createService = createService; |
294
lib/watch.js
@@ -1,193 +0,175 @@ | ||
/** | ||
* Watch. | ||
*/ | ||
const events = require("events"); | ||
'use strict'; | ||
const errors = require("./errors"); | ||
const utils = require("./utils"); | ||
/** | ||
* Module dependencies. | ||
* Initialize a new `Watch` instance. | ||
*/ | ||
var events = require('events'); | ||
var util = require('util'); | ||
class Watch extends events.EventEmitter { | ||
constructor(consul, opts) { | ||
super(); | ||
var errors = require('./errors'); | ||
var utils = require('./utils'); | ||
this.consul = consul; | ||
/** | ||
* Initialize a new `Watch` instance. | ||
*/ | ||
opts = utils.normalizeKeys(opts); | ||
function Watch(consul, opts) { | ||
var self = this; | ||
let options = utils.normalizeKeys(opts.options || {}); | ||
options = utils.defaults(options, consul._defaults); | ||
options.wait = options.wait || "30s"; | ||
options.index = options.index || "0"; | ||
events.EventEmitter.call(self); | ||
if ( | ||
typeof options.timeout !== "string" && | ||
typeof options.timeout !== "number" | ||
) { | ||
const wait = utils.parseDuration(options.wait); | ||
// A small random amount of additional wait time is added to the supplied | ||
// maximum wait time to spread out the wake up time of any concurrent | ||
// requests. This adds up to wait / 16 additional time to the maximum duration. | ||
options.timeout = Math.ceil(wait + Math.max(wait * 0.1, 500)); | ||
} | ||
opts = utils.normalizeKeys(opts); | ||
let backoffFactor = 100; | ||
if ( | ||
opts.hasOwnProperty("backofffactor") && | ||
typeof opts.backofffactor === "number" | ||
) { | ||
backoffFactor = opts.backofffactor; | ||
} | ||
let backoffMax = 30 * 1000; | ||
if ( | ||
opts.hasOwnProperty("backoffmax") && | ||
typeof opts.backoffmax === "number" | ||
) { | ||
backoffMax = opts.backoffmax; | ||
} | ||
let maxAttempts = -1; | ||
if ( | ||
opts.hasOwnProperty("maxattempts") && | ||
typeof opts.maxattempts === "number" | ||
) { | ||
maxAttempts = opts.maxattempts; | ||
} | ||
var options = utils.normalizeKeys(opts.options || {}); | ||
options = utils.defaults(options, consul._defaults); | ||
options.wait = options.wait || '30s'; | ||
options.index = options.index || '0'; | ||
this._context = { consul: consul }; | ||
this._options = options; | ||
this._attempts = 0; | ||
this._maxAttempts = maxAttempts; | ||
this._backoffMax = backoffMax; | ||
this._backoffFactor = backoffFactor; | ||
this._method = opts.method; | ||
this.includeResponse = true; | ||
if (typeof options.timeout !== 'string' && typeof options.timeout !== 'number') { | ||
var wait = utils.parseDuration(options.wait); | ||
// A small random amount of additional wait time is added to the supplied | ||
// maximum wait time to spread out the wake up time of any concurrent | ||
// requests. This adds up to wait / 16 additional time to the maximum duration. | ||
options.timeout = Math.ceil(wait + Math.max(wait * 0.1, 500)); | ||
if (typeof opts.method !== "function") { | ||
throw errors.Validation("method required"); | ||
} | ||
process.nextTick(() => this._run()); | ||
} | ||
var backoffFactor = 100; | ||
if (opts.hasOwnProperty('backofffactor') && typeof opts.backofffactor === 'number') { | ||
backoffFactor = opts.backofffactor; | ||
/** | ||
* Is running | ||
*/ | ||
isRunning() { | ||
return !this._end; | ||
} | ||
var backoffMax = 30 * 1000; | ||
if (opts.hasOwnProperty('backoffmax') && typeof opts.backoffmax === 'number') { | ||
backoffMax = opts.backoffmax; | ||
/** | ||
* Update time | ||
*/ | ||
updateTime() { | ||
return this._updateTime; | ||
} | ||
var maxAttempts = -1; | ||
if (opts.hasOwnProperty('maxattempts') && typeof opts.maxattempts === 'number') { | ||
maxAttempts = opts.maxattempts; | ||
} | ||
self._context = { consul: consul }; | ||
self._options = options; | ||
self._attempts = 0; | ||
self._maxAttempts = maxAttempts; | ||
self._backoffMax = backoffMax; | ||
self._backoffFactor = backoffFactor; | ||
self._method = opts.method; | ||
/** | ||
* End watch | ||
*/ | ||
end() { | ||
if (this._end) return; | ||
this._end = true; | ||
if (typeof opts.method !== 'function') { | ||
throw errors.Validation('method required'); | ||
this.emit("cancel"); | ||
this.emit("end"); | ||
} | ||
process.nextTick(function() { self._run(); }); | ||
} | ||
/** | ||
* Wait | ||
*/ | ||
_wait() { | ||
this._attempts += 1; | ||
if (this._attemptsMaxed) { | ||
return this._backoffMax; | ||
} | ||
const timeout = Math.pow(2, this._attempts) * this._backoffFactor; | ||
if (timeout < this._backoffMax) { | ||
return timeout; | ||
} else { | ||
this._attemptsMaxed = true; | ||
return this._backoffMax; | ||
} | ||
} | ||
util.inherits(Watch, events.EventEmitter); | ||
/** | ||
* Error helper | ||
*/ | ||
_err(err, res) { | ||
if (this._end) return; | ||
/** | ||
* Object meta | ||
*/ | ||
this.emit("error", err, res); | ||
Watch.meta = {}; | ||
if (err && err.isValidation) return this.end(); | ||
if (res && res.statusCode === 400) return this.end(); | ||
if (this._maxAttempts >= 0 && this._attempts >= this._maxAttempts) | ||
return this.end(); | ||
/** | ||
* Is running | ||
*/ | ||
Watch.meta.isRunning = { type: 'sync' }; | ||
Watch.prototype.isRunning = function() { | ||
return !this._end; | ||
}; | ||
/** | ||
* Update time | ||
*/ | ||
Watch.meta.updateTime = { type: 'sync' }; | ||
Watch.prototype.updateTime = function() { | ||
return this._updateTime; | ||
}; | ||
/** | ||
* End watch | ||
*/ | ||
Watch.meta.end = { type: 'sync' }; | ||
Watch.prototype.end = function() { | ||
if (this._end) return; | ||
this._end = true; | ||
this.emit('cancel'); | ||
this.emit('end'); | ||
}; | ||
/** | ||
* Wait | ||
*/ | ||
Watch.prototype._wait = function() { | ||
this._attempts += 1; | ||
if (this._attemptsMaxed) { | ||
return this._backoffMax; | ||
utils.setTimeoutContext( | ||
() => { | ||
this._run(); | ||
}, | ||
this, | ||
this._wait() | ||
); | ||
} | ||
var timeout = Math.pow(2, this._attempts) * this._backoffFactor; | ||
if (timeout < this._backoffMax) { | ||
return timeout; | ||
} else { | ||
this._attemptsMaxed = true; | ||
return this._backoffMax; | ||
} | ||
}; | ||
/** | ||
* Error helper | ||
*/ | ||
/** | ||
* Run | ||
*/ | ||
_run() { | ||
if (this._end) return; | ||
Watch.prototype._err = function(err, res) { | ||
var self = this; | ||
const opts = utils.clone(this._options); | ||
opts.ctx = this; | ||
if (self._end) return; | ||
this._method | ||
.call(this._context, opts) | ||
.then(([res, data]) => { | ||
this._updateTime = +new Date(); | ||
self.emit('error', err, res); | ||
this._attempts = 0; | ||
this._attemptsMaxed = false; | ||
if (err && err.isValidation) return self.end(); | ||
if (res && res.statusCode === 400) return self.end(); | ||
if (self._maxAttempts >= 0 && self._attempts >= self._maxAttempts) return self.end(); | ||
const newIndex = res.headers["x-consul-index"]; | ||
utils.setTimeoutContext(function() { self._run(); }, self, self._wait()); | ||
}; | ||
if (newIndex === undefined) { | ||
return this._err(errors.Validation("Watch not supported"), res); | ||
} | ||
/** | ||
* Run | ||
*/ | ||
if (utils.hasIndexChanged(newIndex, this._options.index)) { | ||
this._options.index = newIndex; | ||
Watch.prototype._run = function() { | ||
var self = this; | ||
this.emit("change", data, res); | ||
} | ||
if (self._end) return; | ||
var opts = utils.clone(self._options); | ||
opts.ctx = self; | ||
try { | ||
self._method.call(self._context, opts, function(err, data, res) { | ||
if (err) { | ||
return self._err(err, res); | ||
} | ||
self._updateTime = +new Date(); | ||
self._attempts = 0; | ||
self._attemptsMaxed = false; | ||
var newIndex = res.headers['x-consul-index']; | ||
if (newIndex === undefined) { | ||
return self._err(errors.Validation('Watch not supported'), res); | ||
} | ||
if (utils.hasIndexChanged(newIndex, self._options.index)) { | ||
self._options.index = newIndex; | ||
self.emit('change', data, res); | ||
} | ||
process.nextTick(function() { self._run(); }); | ||
}); | ||
} catch (err) { | ||
self._err(err); | ||
process.nextTick(() => { | ||
this._run(); | ||
}); | ||
}) | ||
.catch((err) => { | ||
this._err(err, err.response); | ||
}); | ||
} | ||
}; | ||
} | ||
/** | ||
* Module exports. | ||
*/ | ||
exports.Watch = Watch; |
{ | ||
"name": "consul", | ||
"version": "0.40.0", | ||
"version": "1.0.0", | ||
"description": "Consul client", | ||
"main": "./lib", | ||
"files": ["./lib"], | ||
"dependencies": { | ||
"papi": "^0.29.0" | ||
"papi": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"async": "^2.6.1", | ||
"bluebird": "^3.1.1", | ||
"debug": "^3.1.0", | ||
"istanbul": "^0.4.5", | ||
"jscs": "^3.0.7", | ||
"async": "^3.2.0", | ||
"debug": "^4.3.1", | ||
"jshint": "^2.5.5", | ||
"lodash": "^4.17.10", | ||
"mocha": "^5.2.0", | ||
"nock": "^9.3.2", | ||
"mocha": "^8.3.0", | ||
"nock": "^13.0.7", | ||
"node-uuid": "^1.4.3", | ||
"nyc": "^15.1.0", | ||
"should": "^13.2.1", | ||
"sinon": "^6.1.3", | ||
"temp": "^0.8.1" | ||
"sinon": "^9.2.4", | ||
"temp": "^0.9.4" | ||
}, | ||
"scripts": { | ||
"cover": "istanbul cover _mocha -- --recursive && open coverage/lcov-report/index.html", | ||
"test": "jshint lib test && jscs lib test && istanbul cover --report text _mocha -- --recursive --check-leaks && istanbul check-coverage --statements 100 --functions 100 --branches 100 --lines 100", | ||
"acceptance": "ACCEPTANCE=true istanbul cover --report text _mocha -- test/acceptance --recursive --check-leaks --timeout 15000", | ||
"test": "jshint lib test && nyc mocha -- --recursive --check-leaks && nyc check-coverage --statements 100 --functions 100 --branches 100 --lines 100", | ||
"acceptance": "ACCEPTANCE=true nyc mocha -- test/acceptance --recursive --check-leaks --timeout 30000", | ||
"acceptanceSetupMacOS": "sudo ifconfig lo0 alias 127.0.0.2 up && sudo ifconfig lo0 alias 127.0.0.3 up" | ||
@@ -29,0 +26,0 @@ }, |
551
README.md
@@ -13,6 +13,5 @@ # Consul | ||
* [Consul](#init) | ||
* [Callback](#callback) | ||
* [Promise](#promise) | ||
* [Common Method Call Options](#common-options) | ||
* [ACL](#acl) | ||
* [Legacy](#acl-legacy) | ||
* [Agent](#agent) | ||
@@ -28,3 +27,2 @@ * [Check](#agent-check) | ||
* [KV](#kv) | ||
* [Lock](#lock) | ||
* [Query](#query) | ||
@@ -37,3 +35,3 @@ * [Session](#session) | ||
<a name="init"></a> | ||
### consul([options]) | ||
### Consul([options]) | ||
@@ -47,28 +45,18 @@ Initialize a new Consul client. | ||
* secure (Boolean, default: false): enable HTTPS | ||
* ca (String[], optional): array of strings or Buffers of trusted certificates in PEM format | ||
* defaults (Object, optional): common method call options that will be included with every call (ex: set default `token`), these options can be override on a per call basis | ||
* promisify (Boolean|Function, optional): convert callback methods to promises | ||
Advanced options | ||
* agent (http.Agent|https.Agent, optionals): if not set uses the global agent | ||
* baseUrl, headers, tags, socketPath, and timeout (see [Papi](https://github.com/silas/node-papi/blob/main/README.md#client) for details) | ||
* tls options: ca, cert, ciphers, clientCertEngine, crl, dhparam, ecdhCurve, honorCipherOrder, key, passphrase, pfx, rejectUnauthorized, secureOptions, secureProtocol, servername, and sessionIdContext (see [Node.js docs](https://nodejs.org/dist/latest/docs/api/tls.html#tls_tls_connect_options_callback) for details) | ||
Usage | ||
``` javascript | ||
var consul = require('consul')(); | ||
const Consul = require('consul'); | ||
const consul = new Consul(); | ||
``` | ||
<a name="callback"></a> | ||
### Callback | ||
All callback methods have the following signature `function(err, data, res)`. | ||
* err (Error, optional): set if there was an error, otherwise falsy | ||
* data (Object, optional): response data if any, otherwise `undefined` | ||
* res (http.IncomingMessage, optional): HTTP response object with additional `body` property. This might not exist when `err` is set. The `body` property can be a decoded object, string, or Buffer. | ||
<a name="promise"></a> | ||
### Promise | ||
Promise support can be enabled by setting `promisify` to `true` in Node `>= 0.12` or passing a wrapper (ex: `bluebird.fromCallback`) in older versions. | ||
If you need access to the `res` object you can create a custom wrapper ([see example below](#promise-wrapper)). | ||
<a name="common-options"></a> | ||
@@ -99,12 +87,7 @@ ### Common Method Call Options | ||
* [bootstrap](#acl-bootstrap) | ||
* [create](#acl-create) | ||
* [update](#acl-update) | ||
* [destroy](#acl-destroy) | ||
* [get](#acl-get) | ||
* [clone](#acl-clone) | ||
* [list](#acl-list) | ||
* [legacy](#acl-legacy) | ||
* [replication](#acl-replication) | ||
<a name="acl-bootstrap"></a> | ||
### consul.acl.bootstrap(callback) | ||
### consul.acl.bootstrap() | ||
@@ -116,5 +99,3 @@ Creates one-time management token if not configured. | ||
``` javascript | ||
consul.acl.bootstrap(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.acl.bootstrap(); | ||
``` | ||
@@ -130,5 +111,39 @@ | ||
<a name="acl-create"></a> | ||
### consul.acl.create([options], callback) | ||
<a name="acl-replication"></a> | ||
### consul.acl.replication([options]) | ||
Get the status of the ACL replication process in the datacenter. | ||
Usage | ||
``` javascript | ||
await consul.acl.replication(); | ||
``` | ||
Result | ||
``` json | ||
{ | ||
"Enabled": true, | ||
"Running": true, | ||
"SourceDatacenter": "dc1", | ||
"ReplicatedIndex": 1976, | ||
"LastSuccess": "2016-08-05T06:28:58Z", | ||
"LastError": "2016-08-05T06:28:28Z" | ||
} | ||
``` | ||
<a name="acl-legacy"></a> | ||
### consul.acl.legacy | ||
* [create](#acl-legacy-create) | ||
* [update](#acl-legacy-update) | ||
* [destroy](#acl-legacy-destroy) | ||
* [get](#acl-legacy-get) | ||
* [clone](#acl-legacy-clone) | ||
* [list](#acl-legacy-list) | ||
<a name="acl-legacy-create"></a> | ||
### consul.acl.legacy.create([options]) | ||
Creates a new token with policy. | ||
@@ -145,5 +160,3 @@ | ||
``` javascript | ||
consul.acl.create(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.acl.legacy.create(); | ||
``` | ||
@@ -159,4 +172,4 @@ | ||
<a name="acl-update"></a> | ||
### consul.acl.update(options, callback) | ||
<a name="acl-legacy-update"></a> | ||
### consul.acl.legacy.update(options) | ||
@@ -175,9 +188,10 @@ Update the policy of a token. | ||
``` javascript | ||
consul.acl.update({ id: '63e1d82e-f718-eb92-3b7d-61f0c71d45b4', name: 'test' }, function(err) { | ||
if (err) throw err; | ||
await consul.acl.legacy.update({ | ||
id: '63e1d82e-f718-eb92-3b7d-61f0c71d45b4', | ||
name: 'test', | ||
}); | ||
``` | ||
<a name="acl-destroy"></a> | ||
### consul.acl.destroy(options, callback) | ||
<a name="acl-legacy-destroy"></a> | ||
### consul.acl.legacy.destroy(options) | ||
@@ -193,9 +207,7 @@ Destroys a given token. | ||
``` javascript | ||
consul.acl.destroy('b1f4c10e-b61b-e1de-de95-218c9fefdd3e', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.acl.legacy.destroy('b1f4c10e-b61b-e1de-de95-218c9fefdd3e'); | ||
``` | ||
<a name="acl-get"></a> | ||
### consul.acl.get(options, callback) | ||
<a name="acl-legacy-get"></a> | ||
### consul.acl.legacy.get(options) | ||
@@ -211,5 +223,3 @@ Queries the policy of a given token. | ||
``` javascript | ||
consul.acl.get('63e1d82e-f718-eb92-3b7d-61f0c71d45b4', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.acl.legacy.get('63e1d82e-f718-eb92-3b7d-61f0c71d45b4'); | ||
``` | ||
@@ -230,4 +240,4 @@ | ||
<a name="acl-clone"></a> | ||
### consul.acl.clone(options, callback) | ||
<a name="acl-legacy-clone"></a> | ||
### consul.acl.legacy.clone(options) | ||
@@ -243,5 +253,3 @@ Creates a new token by cloning an existing token. | ||
``` javascript | ||
consul.acl.clone('63e1d82e-f718-eb92-3b7d-61f0c71d45b4', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.acl.legacy.clone('63e1d82e-f718-eb92-3b7d-61f0c71d45b4'); | ||
``` | ||
@@ -257,4 +265,4 @@ | ||
<a name="acl-list"></a> | ||
### consul.acl.list([options], callback) | ||
<a name="acl-legacy-list"></a> | ||
### consul.acl.legacy.list([options]) | ||
@@ -266,5 +274,3 @@ Lists all the active tokens. | ||
``` javascript | ||
consul.acl.list(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.acl.legacy.list(); | ||
``` | ||
@@ -295,28 +301,2 @@ | ||
<a name="acl-replication"></a> | ||
### consul.acl.replication([options], callback) | ||
Get the status of the ACL replication process in the datacenter. | ||
Usage | ||
``` javascript | ||
consul.acl.replication(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
``` | ||
Result | ||
``` json | ||
{ | ||
"Enabled": true, | ||
"Running": true, | ||
"SourceDatacenter": "dc1", | ||
"ReplicatedIndex": 1976, | ||
"LastSuccess": "2016-08-05T06:28:58Z", | ||
"LastError": "2016-08-05T06:28:28Z" | ||
} | ||
``` | ||
<a name="agent"></a> | ||
@@ -335,3 +315,3 @@ ### consul.agent | ||
<a name="agent-members"></a> | ||
### consul.agent.members([options], callback) | ||
### consul.agent.members([options]) | ||
@@ -347,5 +327,3 @@ Returns the members as seen by the consul agent. | ||
``` javascript | ||
consul.agent.members(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.members(); | ||
``` | ||
@@ -383,3 +361,3 @@ | ||
<a name="agent-reload"></a> | ||
### consul.agent.reload([options], callback) | ||
### consul.agent.reload([options]) | ||
@@ -391,9 +369,7 @@ Reload agent configuration. | ||
``` javascript | ||
consul.agent.reload(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.reload(); | ||
``` | ||
<a name="agent-self"></a> | ||
### consul.agent.self(callback) | ||
### consul.agent.self() | ||
@@ -405,5 +381,3 @@ Returns the agent node configuration. | ||
``` javascript | ||
consul.agent.self(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.self(); | ||
``` | ||
@@ -489,3 +463,3 @@ | ||
<a name="agent-maintenance"></a> | ||
### consul.agent.maintenance(options, callback) | ||
### consul.agent.maintenance(options) | ||
@@ -502,9 +476,7 @@ Set node maintenance mode. | ||
``` javascript | ||
consul.agent.maintenance(true, function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.maintenance(true); | ||
``` | ||
<a name="agent-join"></a> | ||
### consul.agent.join(options, callback) | ||
### consul.agent.join(options) | ||
@@ -521,9 +493,7 @@ Trigger agent to join a node. | ||
``` javascript | ||
consul.agent.join('127.0.0.2', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.join('127.0.0.2'); | ||
``` | ||
<a name="agent-force-leave"></a> | ||
### consul.agent.forceLeave(options, callback) | ||
### consul.agent.forceLeave(options) | ||
@@ -539,5 +509,3 @@ Force remove node. | ||
``` javascript | ||
consul.agent.forceLeave('node2', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.forceLeave('node2'); | ||
``` | ||
@@ -556,3 +524,3 @@ | ||
<a name="agent-check-list"></a> | ||
### consul.agent.check.list(callback) | ||
### consul.agent.check.list() | ||
@@ -564,5 +532,3 @@ Returns the checks the agent is managing. | ||
``` javascript | ||
consul.agent.check.list(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.check.list(); | ||
``` | ||
@@ -588,3 +554,3 @@ | ||
<a name="agent-check-register"></a> | ||
### consul.agent.check.register(options, callback) | ||
### consul.agent.check.register(options) | ||
@@ -624,10 +590,6 @@ Registers a new check. | ||
``` javascript | ||
var check = { | ||
await consul.agent.check.register({ | ||
name: 'example', | ||
ttl: '15s', | ||
notes: 'This is an example check.', | ||
}; | ||
consul.agent.check.register(check, function(err) { | ||
if (err) throw err; | ||
}); | ||
@@ -637,3 +599,3 @@ ``` | ||
<a name="agent-check-deregister"></a> | ||
### consul.agent.check.deregister(options, callback) | ||
### consul.agent.check.deregister(options) | ||
@@ -649,9 +611,7 @@ Deregister a check. | ||
``` javascript | ||
consul.agent.check.deregister('example', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.check.deregister('example'); | ||
``` | ||
<a name="agent-check-pass"></a> | ||
### consul.agent.check.pass(options, callback) | ||
### consul.agent.check.pass(options) | ||
@@ -668,9 +628,7 @@ Mark a test as passing. | ||
``` javascript | ||
consul.agent.check.pass('example', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.check.pass('example'); | ||
``` | ||
<a name="agent-check-warn"></a> | ||
### consul.agent.check.warn(options, callback) | ||
### consul.agent.check.warn(options) | ||
@@ -687,9 +645,7 @@ Mark a test as warning. | ||
``` javascript | ||
consul.agent.check.warn('example', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.check.warn('example'); | ||
``` | ||
<a name="agent-check-fail"></a> | ||
### consul.agent.check.fail(options, callback) | ||
### consul.agent.check.fail(options) | ||
@@ -706,5 +662,3 @@ Mark a test as critical. | ||
``` javascript | ||
consul.agent.check.fail('example', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.check.fail('example'); | ||
``` | ||
@@ -721,3 +675,3 @@ | ||
<a name="agent-service-list"></a> | ||
### consul.agent.service.list(callback) | ||
### consul.agent.service.list() | ||
@@ -729,5 +683,3 @@ Returns the services the agent is managing. | ||
``` javascript | ||
consul.agent.service.list(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.service.list(); | ||
``` | ||
@@ -752,3 +704,3 @@ | ||
<a name="agent-service-register"></a> | ||
### consul.agent.service.register(options, callback) | ||
### consul.agent.service.register(options) | ||
@@ -786,9 +738,7 @@ Registers a new service. | ||
``` javascript | ||
consul.agent.service.register('example', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.service.register('example'); | ||
``` | ||
<a name="agent-service-deregister"></a> | ||
### consul.agent.service.deregister(options, callback) | ||
### consul.agent.service.deregister(options) | ||
@@ -804,9 +754,7 @@ Deregister a service. | ||
``` javascript | ||
consul.agent.service.deregister('example', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.service.deregister('example'); | ||
``` | ||
<a name="agent-service-maintenance"></a> | ||
### consul.agent.service.maintenance(options, callback) | ||
### consul.agent.service.maintenance(options) | ||
@@ -824,5 +772,3 @@ Set service maintenance mode. | ||
``` javascript | ||
consul.agent.service.maintenance({ id: 'example', enable: true }, function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.agent.service.maintenance({ id: 'example', enable: true }); | ||
``` | ||
@@ -839,3 +785,3 @@ | ||
<a name="catalog-datacenters"></a> | ||
### consul.catalog.datacenters(callback) | ||
### consul.catalog.datacenters() | ||
@@ -847,5 +793,3 @@ Lists known datacenters. | ||
``` javascript | ||
consul.catalog.datacenters(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.catalog.datacenters(); | ||
``` | ||
@@ -867,3 +811,3 @@ | ||
<a name="catalog-connect-nodes"></a> | ||
### consul.catalog.connect.nodes(options, callback) | ||
### consul.catalog.connect.nodes(options) | ||
@@ -880,5 +824,3 @@ Lists the nodes for a given Connect-capable service. | ||
``` javascript | ||
consul.catalog.connect.nodes('example', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.catalog.connect.nodes('example'); | ||
``` | ||
@@ -928,3 +870,3 @@ | ||
<a name="catalog-node-list"></a> | ||
### consul.catalog.node.list([options], callback) | ||
### consul.catalog.node.list([options]) | ||
@@ -940,5 +882,3 @@ Lists nodes in a given datacenter. | ||
``` javascript | ||
consul.catalog.node.list(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.catalog.node.list(); | ||
``` | ||
@@ -958,3 +898,3 @@ | ||
<a name="catalog-node-services"></a> | ||
### consul.catalog.node.services(options, callback) | ||
### consul.catalog.node.services(options) | ||
@@ -970,5 +910,3 @@ Lists the services provided by a node. | ||
``` javascript | ||
consul.catalog.node.services('node1', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.catalog.node.services('node1'); | ||
``` | ||
@@ -1011,3 +949,3 @@ | ||
<a name="catalog-service-list"></a> | ||
### consul.catalog.service.list([options], callback) | ||
### consul.catalog.service.list([options]) | ||
@@ -1023,5 +961,3 @@ Lists services in a given datacenter. | ||
``` javascript | ||
consul.catalog.service.list(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.catalog.service.list(); | ||
``` | ||
@@ -1042,3 +978,3 @@ | ||
<a name="catalog-service-nodes"></a> | ||
### consul.catalog.service.nodes(options, callback) | ||
### consul.catalog.service.nodes(options) | ||
@@ -1056,5 +992,3 @@ Lists the nodes for a given service. | ||
``` javascript | ||
consul.catalog.service.nodes('example', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.catalog.service.nodes('example'); | ||
``` | ||
@@ -1087,3 +1021,3 @@ | ||
<a name="event-fire"></a> | ||
### consul.event.fire(options, callback) | ||
### consul.event.fire(options) | ||
@@ -1103,5 +1037,3 @@ Fires a new user event. | ||
``` javascript | ||
consul.event.fire('deploy', '53', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.event.fire('deploy', '53'); | ||
``` | ||
@@ -1125,3 +1057,3 @@ | ||
<a name="event-list"></a> | ||
### consul.event.list([options], callback) | ||
### consul.event.list([options]) | ||
@@ -1137,5 +1069,3 @@ Lists the most recent events an agent has seen. | ||
``` javascript | ||
consul.event.list('deploy', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.event.list('deploy'); | ||
``` | ||
@@ -1169,3 +1099,3 @@ | ||
<a name="health-node"></a> | ||
### consul.health.node(options, callback) | ||
### consul.health.node(options) | ||
@@ -1182,5 +1112,3 @@ Returns the health info of a node. | ||
``` javascript | ||
consul.health.node('node1', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.health.node('node1'); | ||
``` | ||
@@ -1216,3 +1144,3 @@ | ||
<a name="health-checks"></a> | ||
### consul.health.checks(options, callback) | ||
### consul.health.checks(options) | ||
@@ -1229,5 +1157,3 @@ Returns the checks of a service. | ||
``` javascript | ||
consul.health.checks('example', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.health.checks('example'); | ||
``` | ||
@@ -1253,3 +1179,3 @@ | ||
<a name="health-service"></a> | ||
### consul.health.service(options, callback) | ||
### consul.health.service(options) | ||
@@ -1268,5 +1194,3 @@ Returns the nodes and health info of a service. | ||
``` javascript | ||
consul.health.service('example', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.health.service('example'); | ||
``` | ||
@@ -1316,3 +1240,3 @@ | ||
<a name="health-state"></a> | ||
### consul.health.state(options, callback) | ||
### consul.health.state(options) | ||
@@ -1329,5 +1253,3 @@ Returns the checks in a given state. | ||
``` javascript | ||
consul.health.state('critical', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.health.state('critical'); | ||
``` | ||
@@ -1361,3 +1283,3 @@ | ||
<a name="kv-get"></a> | ||
### consul.kv.get(options, callback) | ||
### consul.kv.get(options) | ||
@@ -1379,6 +1301,3 @@ Return key/value (kv) pair(s) or `undefined` if key not found. | ||
``` javascript | ||
consul.kv.get('hello', function(err, result) { | ||
if (err) throw err; | ||
if (result === undefined) throw new Error('key not found'); | ||
}); | ||
await consul.kv.get('hello'); | ||
``` | ||
@@ -1400,3 +1319,3 @@ | ||
<a name="kv-keys"></a> | ||
### consul.kv.keys(options, callback) | ||
### consul.kv.keys(options) | ||
@@ -1414,5 +1333,3 @@ Return keys for a given prefix. | ||
``` javascript | ||
consul.kv.keys('a/', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.kv.keys('a/'); | ||
``` | ||
@@ -1430,3 +1347,3 @@ | ||
<a name="kv-set"></a> | ||
### consul.kv.set(options, callback) | ||
### consul.kv.set(options) | ||
@@ -1448,5 +1365,3 @@ Set key/value (kv) pair. | ||
``` javascript | ||
consul.kv.set('hello', 'world', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.kv.set('hello', 'world'); | ||
``` | ||
@@ -1461,3 +1376,3 @@ | ||
<a name="kv-del"></a> | ||
### consul.kv.del(options, callback) | ||
### consul.kv.del(options) | ||
@@ -1476,62 +1391,5 @@ Delete key/value (kv) pair(s). | ||
``` javascript | ||
consul.kv.del('hello', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.kv.del('hello'); | ||
``` | ||
<a name="lock"></a> | ||
### consul.lock(options) | ||
_Experimental_ | ||
Lock a key using the method described in the [leader election](https://www.consul.io/docs/guides/leader-election.html) guide. | ||
Options | ||
* key (String): lock key | ||
* value (String|Buffer, optional): lock value | ||
* session (Object|String, optional): session options | ||
Events | ||
* `acquire`: lock successfully acquired | ||
* `error`: lock related error | ||
* `retry`: lock retry attempt | ||
* `release`: lock gracefully released (not always emitted) | ||
* `end`: lock ended (always emitted) | ||
Usage | ||
``` javascript | ||
var lock = consul.lock({ key: 'test' }); | ||
lock.on('acquire', function() { | ||
console.log('lock acquired'); | ||
lock.release(); | ||
}); | ||
lock.on('release', function() { | ||
console.log('lock released'); | ||
}); | ||
lock.on('error', function() { | ||
console.log('lock error:', err); | ||
}); | ||
lock.on('end', function(err) { | ||
console.log('lock released or there was a permanent failure'); | ||
}); | ||
lock.acquire(); | ||
``` | ||
Result | ||
``` | ||
lock acquired | ||
lock released | ||
lock released or there was a permanent failure | ||
``` | ||
<a name="query"></a> | ||
@@ -1549,3 +1407,3 @@ ### consul.query | ||
<a name="query-list"></a> | ||
### consul.query.list(callback) | ||
### consul.query.list() | ||
@@ -1557,5 +1415,3 @@ List prepared query. | ||
``` javascript | ||
consul.query.list(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.query.list(); | ||
``` | ||
@@ -1603,3 +1459,3 @@ | ||
<a name="query-create"></a> | ||
### consul.query.create(options, callback) | ||
### consul.query.create(options) | ||
@@ -1624,3 +1480,3 @@ Create a new prepared query. | ||
``` javascript | ||
var opts = { | ||
await consul.query.create({ | ||
name: 'redis', | ||
@@ -1631,6 +1487,2 @@ service: { | ||
}, | ||
}; | ||
consul.query.create(opts, function(err, result) { | ||
if (err) throw err; | ||
}); | ||
@@ -1648,3 +1500,3 @@ ``` | ||
<a name="query-update"></a> | ||
### consul.query.update(options, callback) | ||
### consul.query.update(options) | ||
@@ -1662,3 +1514,3 @@ Update existing prepared query. | ||
``` javascript | ||
var opts = { | ||
await consul.query.update({ | ||
query: '422b14b9-874b-4520-bd2e-e149a42b0066', | ||
@@ -1670,6 +1522,2 @@ name: 'redis', | ||
}, | ||
}; | ||
consul.query.update(opts, function(err, result) { | ||
if (err) throw err; | ||
}); | ||
@@ -1679,3 +1527,3 @@ ``` | ||
<a name="query-get"></a> | ||
### consul.query.get(options, callback) | ||
### consul.query.get(options) | ||
@@ -1691,5 +1539,3 @@ Get prepared query. | ||
``` javascript | ||
consul.query.get('6119cabf-c052-48fe-9f07-711762e52931', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.query.get('6119cabf-c052-48fe-9f07-711762e52931'); | ||
``` | ||
@@ -1736,3 +1582,3 @@ | ||
<a name="query-destroy"></a> | ||
### consul.query.destroy(options, callback) | ||
### consul.query.destroy(options) | ||
@@ -1748,9 +1594,7 @@ Delete prepared query. | ||
``` javascript | ||
consul.query.destroy('422b14b9-874b-4520-bd2e-e149a42b0066', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.query.destroy('422b14b9-874b-4520-bd2e-e149a42b0066'); | ||
``` | ||
<a name="query-execute"></a> | ||
### consul.query.execute(options, callback) | ||
### consul.query.execute(options) | ||
@@ -1766,5 +1610,3 @@ Execute prepared query. | ||
``` javascript | ||
consul.query.execute('6119cabf-c052-48fe-9f07-711762e52931', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.query.execute('6119cabf-c052-48fe-9f07-711762e52931'); | ||
``` | ||
@@ -1826,3 +1668,3 @@ | ||
<a name="query-explain"></a> | ||
### consul.query.explain(options, callback) | ||
### consul.query.explain(options) | ||
@@ -1838,6 +1680,3 @@ Explain prepared query. | ||
``` javascript | ||
consul.query.explain('422b14b9-874b-4520-bd2e-e149a42b0066', function(err, result) { | ||
if (err) throw err; | ||
console.log(result); | ||
}); | ||
await consul.query.explain('422b14b9-874b-4520-bd2e-e149a42b0066'); | ||
``` | ||
@@ -1895,3 +1734,3 @@ | ||
<a name="session-create"></a> | ||
### consul.session.create([options], callback) | ||
### consul.session.create([options]) | ||
@@ -1913,5 +1752,3 @@ Create a new session. | ||
``` javascript | ||
consul.session.create(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.session.create(); | ||
``` | ||
@@ -1928,3 +1765,3 @@ | ||
<a name="session-destroy"></a> | ||
### consul.session.destroy(options, callback) | ||
### consul.session.destroy(options) | ||
@@ -1941,9 +1778,7 @@ Destroy a given session. | ||
``` javascript | ||
consul.session.destroy('a0f5dc05-84c3-5f5a-1d88-05b875e524e1', function(err) { | ||
if (err) throw err; | ||
}); | ||
await consul.session.destroy('a0f5dc05-84c3-5f5a-1d88-05b875e524e1'); | ||
``` | ||
<a name="session-get"></a> | ||
### consul.session.get(options, callback) | ||
### consul.session.get(options) | ||
@@ -1960,5 +1795,3 @@ Queries a given session. | ||
``` javascript | ||
consul.session.get('a0f5dc05-84c3-5f5a-1d88-05b875e524e1', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.session.get('a0f5dc05-84c3-5f5a-1d88-05b875e524e1'); | ||
``` | ||
@@ -1982,3 +1815,3 @@ | ||
<a name="session-node"></a> | ||
### consul.session.node(options, callback) | ||
### consul.session.node(options) | ||
@@ -1995,5 +1828,3 @@ Lists sessions belonging to a node. | ||
``` javascript | ||
consul.session.node('node1', function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.session.node('node1'); | ||
``` | ||
@@ -2019,3 +1850,3 @@ | ||
<a name="session-list"></a> | ||
### consul.session.list([options], callback) | ||
### consul.session.list([options]) | ||
@@ -2031,5 +1862,3 @@ Lists all the active sessions. | ||
``` javascript | ||
consul.session.list(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.session.list(); | ||
``` | ||
@@ -2055,3 +1884,3 @@ | ||
<a name="session-renew"></a> | ||
### consul.session.renew(options, callback) | ||
### consul.session.renew(options) | ||
@@ -2068,5 +1897,3 @@ Renew a given session. | ||
``` javascript | ||
consul.session.renew('a0f5dc05-84c3-5f5a-1d88-05b875e524e1', function(err, renew) { | ||
if (err) throw err; | ||
}); | ||
await consul.session.renew('a0f5dc05-84c3-5f5a-1d88-05b875e524e1'); | ||
``` | ||
@@ -2100,3 +1927,3 @@ | ||
<a name="status-leader"></a> | ||
### consul.status.leader(callback) | ||
### consul.status.leader() | ||
@@ -2108,5 +1935,3 @@ Returns the current Raft leader. | ||
``` javascript | ||
consul.status.leader(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.status.leader(); | ||
``` | ||
@@ -2121,3 +1946,3 @@ | ||
<a name="status-peers"></a> | ||
### consul.status.peers(callback) | ||
### consul.status.peers() | ||
@@ -2129,5 +1954,3 @@ Returns the current Raft peer set. | ||
``` javascript | ||
consul.status.peers(function(err, result) { | ||
if (err) throw err; | ||
}); | ||
await consul.status.peers(); | ||
``` | ||
@@ -2144,3 +1967,3 @@ | ||
<a name="transaction"></a> | ||
### consul.transaction.create(operations, callback) | ||
### consul.transaction.create(operations) | ||
@@ -2151,3 +1974,3 @@ operations: The body of the request should be a list of operations to perform inside the atomic transaction. Up to 64 operations may be present in a single transaction. | ||
``` javascript | ||
consul.transaction.create([ | ||
await consul.transaction.create([ | ||
{ | ||
@@ -2194,3 +2017,3 @@ { | ||
``` javascript | ||
var watch = consul.watch({ | ||
const watch = consul.watch({ | ||
method: consul.kv.get, | ||
@@ -2201,45 +2024,13 @@ options: { key: 'test' }, | ||
watch.on('change', function(data, res) { | ||
watch.on('change', (data, res) => { | ||
console.log('data:', data); | ||
}); | ||
watch.on('error', function(err) { | ||
watch.on('error', (err) => { | ||
console.log('error:', err); | ||
}); | ||
setTimeout(function() { watch.end(); }, 30 * 1000); | ||
setTimeout(() => { watch.end(); }, 30 * 1000); | ||
``` | ||
<a name="promise-wrapper"></a> | ||
### Promise Wrapper | ||
``` javascript | ||
var Bluebird = require('bluebird'); | ||
function fromCallback(fn) { | ||
return new Bluebird(function(resolve, reject) { | ||
try { | ||
return fn(function(err, data, res) { | ||
if (err) { | ||
err.res = res; | ||
return reject(err); | ||
} | ||
return resolve([data, res]); | ||
}); | ||
} catch (err) { | ||
return reject(err); | ||
} | ||
}); | ||
} | ||
var consul = require('consul')({ promisify: fromCallback }); | ||
consul.kv.set('test', 'hello world').then(function() { | ||
consul.kv.keys().spread(function(data, res) { | ||
console.log('data:', data); | ||
console.log('headers:', res.headers); | ||
}); | ||
}); | ||
``` | ||
## Acceptance Tests | ||
@@ -2280,4 +2071,4 @@ | ||
[consul]: http://www.consul.io/ | ||
[consul-docs-api]: http://www.consul.io/docs/agent/http.html | ||
[download]: http://www.consul.io/downloads.html | ||
[consul]: https://www.consul.io/ | ||
[consul-docs-api]: https://www.consul.io/api-docs | ||
[download]: https://www.consul.io/downloads |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances 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
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
10
0
100
1
99065
25
2075
1967
3
+ Addedpapi@1.1.2(transitive)
- Removedpapi@0.29.1(transitive)
Updatedpapi@^1.1.0