UNPKG

flipr-etcd

Version:

Flipr source to retrieve config from etcd: a distributed, consistent, partition-tolerant key/value store

304 lines (290 loc) 8.57 kB
<!doctype html> <html lang="en"> <head> <title>Code coverage report for lib/flipr-etcd.js</title> <meta charset="utf-8"> <link rel="stylesheet" href="../prettify.css"> <link rel="stylesheet" href="../base.css"> <style type='text/css'> div.coverage-summary .sorter { background-image: url(../sort-arrow-sprite.png); } </style> </head> <body> <div class="header high"> <h1>Code coverage report for <span class="entity">lib/flipr-etcd.js</span></h1> <h2> Statements: <span class="metric">100% <small>(43 / 43)</small></span> &nbsp;&nbsp;&nbsp;&nbsp; Branches: <span class="metric">100% <small>(17 / 17)</small></span> &nbsp;&nbsp;&nbsp;&nbsp; Functions: <span class="metric">100% <small>(6 / 6)</small></span> &nbsp;&nbsp;&nbsp;&nbsp; Lines: <span class="metric">100% <small>(43 / 43)</small></span> &nbsp;&nbsp;&nbsp;&nbsp; Ignored: <span class="metric"><span class="ignore-none">none</span></span> &nbsp;&nbsp;&nbsp;&nbsp; </h2> <div class="path"><a href="../index.html">All files</a> &#187; <a href="index.html">lib/</a> &#187; flipr-etcd.js</div> </div> <div class="body"> <pre><table class="coverage"> <tr><td class="line-count">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83</td><td class="line-coverage"><span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">21</span> <span class="cline-any cline-yes">21</span> <span class="cline-any cline-yes">21</span> <span class="cline-any cline-yes">21</span> <span class="cline-any cline-yes">21</span> <span class="cline-any cline-yes">21</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">21</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">25</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">23</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-yes">8</span> <span class="cline-any cline-yes">8</span> <span class="cline-any cline-yes">8</span> <span class="cline-any cline-yes">3</span> <span class="cline-any cline-yes">3</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">5</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-yes">2</span> <span class="cline-any cline-yes">2</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-yes">3</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">2</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-yes">1</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">24</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes">21</span></td><td class="text"><pre class="prettyprint lang-js">'use strict'; &nbsp; var EventEmitter = require('events').EventEmitter; var util = require('util'); var _ = require('lodash'); var Etcd = require('node-etcd'); var memoize = require('memoizee'); var parseClientResult = require('./parse-client-result'); &nbsp; module.exports = FliprEtcd; &nbsp; function FliprEtcd(options) { //Force instantiation if(!(this instanceof FliprEtcd)) return new FliprEtcd(options); EventEmitter.call(this); options = _.defaults({}, options, { hosts: [], host: '127.0.0.1', port: '4001', directory: 'default', key: 'config' }); &nbsp; var _changeActionsToIgnore = ['delete', 'compareAndDelete', 'expire']; var _fullKey = util.format('flipr/%s/%s', options.directory, options.key); var _lastValidConfig; &nbsp; this.events = { beforeChange: 'before-change', flush: 'flush', afterChange: 'after-change', error: 'error' }; &nbsp; if(options.hosts.length &gt; 0) this.client = new Etcd(options.hosts, options.ssl); else this.client = new Etcd(options.host, options.port, options.ssl); &nbsp; this.getConfig = memoize(_.bind(function(cb){ this.client.get(_fullKey, _.bind(function(err, result){ result = parseClientResult(err, _lastValidConfig, result); if(!result.config) { _.partial(this.emit, this.events.error).apply(this, result.errors || []); return cb(err || _.last(result.errors) || new Error('Unexpected error while getting config from etcd.')); } return cb(null, result.config); }, this)); }, this), {async: true}); &nbsp; //Calling getConfig will memoize the cache. this.preload = this.getConfig; &nbsp; this.flush = _.bind(function(){ this.getConfig.clear(); this.emit(this.events.flush); }, this); &nbsp; this.watcher = this.client.watcher(_fullKey); &nbsp; this.watcher.on('change', _.bind(function(result){ if(!result || !result.action) { this.emit(this.events.error, new Error('Unrecognized result from etcd watcher.'), result); return; } &nbsp; if(_changeActionsToIgnore.indexOf(result.action) &gt; -1) return; &nbsp; this.emit(this.events.beforeChange); //TODO: change should be able to update the cached config without //making the second call to preload. this.flush(); this.preload(_.bind(function(){ this.emit(this.events.afterChange); }, this)); }, this)); &nbsp; this.watcher.on('error', _.partial(this.emit, this.events.error)); } &nbsp; util.inherits(FliprEtcd, EventEmitter);</pre></td></tr> </table></pre> </div> <div class="footer"> <div class="meta">Generated by <a href="http://istanbul-js.org/" target="_blank">istanbul</a> at Mon Apr 06 2015 13:06:27 GMT-0700 (MST)</div> </div> <script src="../prettify.js"></script> <script> window.onload = function () { if (typeof prettyPrint === 'function') { prettyPrint(); } }; </script> <script src="../sorter.js"></script> </body> </html>