UNPKG

node-etcd

Version:

etcd library for node.js (etcd v2 api)

269 lines (227 loc) 7.62 kB
// Generated by CoffeeScript 1.12.7 var Client, Etcd, HttpsAgent, URL, Watcher, _, exports, bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; _ = require('lodash'); Watcher = require('./watcher'); Client = require('./client'); HttpsAgent = (require('https')).Agent; URL = require('url-parse'); Etcd = (function() { function Etcd(hosts, options) { if (hosts == null) { hosts = "127.0.0.1:2379"; } if (options == null) { options = {}; } this.watcher = bind(this.watcher, this); this.hosts = this._cleanHostList(hosts); this.client = new Client(this.hosts, options, null); } Etcd.prototype.set = function(key, value, options, callback) { var opt, ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; opt = this._prepareOpts("keys/" + this._stripSlashPrefix(key), "/v2", value, options); return this.client.put(opt, callback); }; Etcd.prototype.setSync = function(key, value, options) { if (options == null) { options = {}; } return this.set(key, value, this._synchronousOpts(options)); }; Etcd.prototype.get = function(key, options, callback) { var opt, ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; opt = this._prepareOpts("keys/" + this._stripSlashPrefix(key), "/v2", null, options); return this.client.get(opt, callback); }; Etcd.prototype.getSync = function(key, options) { if (options == null) { options = {}; } return this.get(key, this._synchronousOpts(options)); }; Etcd.prototype.create = function(dir, value, options, callback) { var opt, ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; opt = this._prepareOpts("keys/" + this._stripSlashPrefix(dir), "/v2", value, options); return this.client.post(opt, callback); }; Etcd.prototype.post = Etcd.prototype.create; Etcd.prototype.del = function(key, options, callback) { var opt, ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; opt = this._prepareOpts("keys/" + this._stripSlashPrefix(key), "/v2", null, options); return this.client["delete"](opt, callback); }; Etcd.prototype["delete"] = Etcd.prototype.del; Etcd.prototype.delSync = function(key, options) { if (options == null) { options = {}; } return this.del(key, this._synchronousOpts(options)); }; Etcd.prototype.mkdir = function(dir, options, callback) { var ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; options.dir = true; return this.set(dir, null, options, callback); }; Etcd.prototype.mkdirSync = function(dir, options) { if (options == null) { options = {}; } return this.mkdir(dir, this._synchronousOpts(options)); }; Etcd.prototype.rmdir = function(dir, options, callback) { var ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; options.dir = true; return this.del(dir, options, callback); }; Etcd.prototype.rmdirSync = function(dir, options) { if (options == null) { options = {}; } return this.rmdir(dir, this._synchronousOpts(options)); }; Etcd.prototype.compareAndSwap = function(key, value, oldvalue, options, callback) { var ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; if (options == null) { options = {}; } options.prevValue = oldvalue; return this.set(key, value, options, callback); }; Etcd.prototype.testAndSet = Etcd.prototype.compareAndSwap; Etcd.prototype.compareAndDelete = function(key, oldvalue, options, callback) { var ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; if (options == null) { options = {}; } options.prevValue = oldvalue; return this.del(key, options, callback); }; Etcd.prototype.testAndDelete = Etcd.prototype.compareAndDelete; Etcd.prototype.raw = function(method, key, value, options, callback) { var opt, ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; opt = this._prepareOpts(key, "", value, options); return this.client.execute(method, opt, callback); }; Etcd.prototype.watch = function(key, options, callback) { var ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; if (options == null) { options = {}; } options.wait = true; return this.get(key, options, callback); }; Etcd.prototype.watchIndex = function(key, index, options, callback) { var ref; ref = this._argParser(options, callback), options = ref[0], callback = ref[1]; if (options == null) { options = {}; } options.waitIndex = index; return this.watch(key, options, callback); }; Etcd.prototype.watcher = function(key, index, options) { if (index == null) { index = null; } if (options == null) { options = {}; } return new Watcher(this, key, index, options); }; Etcd.prototype.machines = function(callback) { var opt; opt = this._prepareOpts("keys/_etcd/machines"); return this.client.get(opt, callback); }; Etcd.prototype.getHosts = function() { return _.clone(this.hosts); }; Etcd.prototype.leader = function(callback) { var opt; opt = this._prepareOpts("leader"); return this.client.get(opt, callback); }; Etcd.prototype.leaderStats = function(callback) { var opt; opt = this._prepareOpts("stats/leader"); return this.client.get(opt, callback); }; Etcd.prototype.selfStats = function(callback) { var opt; opt = this._prepareOpts("stats/self"); return this.client.get(opt, callback); }; Etcd.prototype.version = function(callback) { var opt; opt = this._prepareOpts("version", ""); return this.client.get(opt, callback); }; Etcd.prototype._stripSlashPrefix = function(key) { return key.replace(/^\//, ''); }; Etcd.prototype._synchronousOpts = function(options) { return _.extend({}, options, { synchronous: true }); }; Etcd.prototype._prepareOpts = function(path, apiVersion, value, allOpts) { var clientOptions, opt, queryString; if (apiVersion == null) { apiVersion = "/v2"; } if (value == null) { value = null; } if (allOpts == null) { allOpts = {}; } queryString = _.omit(allOpts, 'maxRetries', 'synchronous'); clientOptions = _.pick(allOpts, 'maxRetries'); return opt = { path: apiVersion + "/" + path, json: true, qs: queryString, clientOptions: clientOptions, synchronous: allOpts.synchronous, form: value != null ? { value: value } : void 0, agentOptions: this.sslopts != null ? this.sslopts : void 0 }; }; Etcd.prototype._argParser = function(options, callback) { if (options == null) { options = {}; } if (typeof options === 'function') { return [{}, options]; } else { return [options, callback]; } }; Etcd.prototype._cleanHostList = function(hosts) { var hostlist; hostlist = _.isArray(hosts) ? hosts : [hosts]; return hostlist.map(function(host) { var url; if (host.indexOf('http') === -1) { host = 'http://' + host; } url = new URL(host); return url.href.replace(/\/$/, ""); }); }; return Etcd; })(); exports = module.exports = Etcd;