@opuscapita/config
Advanced tools
Comparing version 3.0.13 to 3.0.14
@@ -53,3 +53,5 @@ const EventEmitter = require('events'); | ||
this.cache = cache; | ||
this.watches = { }; | ||
this.epWatches = { }; | ||
this.keyWatches = { }; | ||
this.knownKeys = { }; | ||
@@ -79,6 +81,10 @@ const [ secret, consul ] = await Promise.all([ | ||
for(const k in this.watches) | ||
this.watches[k].end(); | ||
for(const k in this.epWatches) | ||
this.epWatches[k].end(); | ||
for(const k in this.keyWatches) | ||
this.keyWatches[k].end(); | ||
this.watches = { }; | ||
this.epWatches = { }; | ||
this.keyWatches = { }; | ||
this.knownKeys = { }; | ||
@@ -146,3 +152,3 @@ this.secret = null; | ||
this.watches['all-services'] = watch; | ||
this.epWatches['all-services'] = watch; | ||
@@ -162,8 +168,8 @@ watch.on('change', (data, res) => callback(Object.keys(data))); | ||
if(this.watches[cacheKey]) | ||
return resolve(this.watches[cacheKey]); | ||
if(this.epWatches[cacheKey]) | ||
return resolve(this.epWatches[cacheKey]); | ||
const watch = consul.watch({ method : consul.health.service, options : { service : serviceName } }); | ||
this.watches[cacheKey] = watch; | ||
this.epWatches[cacheKey] = watch; | ||
@@ -215,37 +221,56 @@ const mapService = (s) => ({ | ||
{ | ||
if(this.watches[key]) | ||
return resolve(this.watches[key]); | ||
if(this.keyWatches[key]) | ||
return resolve(this.keyWatches[key]); | ||
const watch = consul.watch({ method : consul.kv.keys, options : { key : key } }); | ||
this.watches[key] = watch; | ||
this.keyWatches[key] = watch; | ||
watch.on('error', (err) => reject(err)); | ||
watch.on('change', (keys) => Promise.all(keys.map(async (key) => | ||
watch.on('change', (keys) => | ||
{ | ||
const result = await consul.kv.get({ key }); | ||
const value = result && result.Value; | ||
const oldValue = await this.cache.get(key); | ||
const oldKeys = Object.keys(this.knownKeys); | ||
const missingKeys = oldKeys.filter(key => !keys.includes(key)); | ||
if(value != oldValue) | ||
Promise.all(missingKeys.map(async key => | ||
{ | ||
if(value) | ||
await this.cache.put(key, value); | ||
else | ||
await this.cache.delete(key); | ||
if(this.keyWatches[key]) | ||
this.keyWatches[key].end(); | ||
delete this.keyWatches[key]; | ||
delete this.knownKeys[key]; | ||
await this.cache.delete(key); | ||
const shortKey = key.substr(key.indexOf(this.serviceName) + this.serviceName.length + 1); | ||
/** | ||
* Change event for properties (kv). | ||
* | ||
* @event ConfigClientBase#propertyChanged | ||
* @type {object} | ||
* @property {string} key - The name of key that changed. | ||
* @property {string} value - The new value for the key. | ||
*/ | ||
this.emit('propertyChanged', shortKey, value); | ||
} | ||
}))); | ||
this.emit('propertyChanged', shortKey); | ||
})); | ||
Promise.all(keys.map(async (key) => | ||
{ | ||
this.knownKeys[key] = true; | ||
const result = await consul.kv.get({ key }); | ||
const value = result && result.Value; | ||
const oldValue = await this.cache.get(key); | ||
if(value != oldValue) | ||
{ | ||
await this.cache.put(key, value); | ||
const shortKey = key.substr(key.indexOf(this.serviceName) + this.serviceName.length + 1); | ||
/** | ||
* Change event for properties (kv). | ||
* | ||
* @event ConfigClientBase#propertyChanged | ||
* @type {object} | ||
* @property {string} key - The name of key that changed. | ||
* @property {string} value - The new value for the key. | ||
*/ | ||
this.emit('propertyChanged', shortKey, value); | ||
} | ||
})); | ||
}); | ||
resolve(watch); | ||
@@ -252,0 +277,0 @@ }); |
{ | ||
"name": "@opuscapita/config", | ||
"version": "3.0.13", | ||
"version": "3.0.14", | ||
"description": "Configuration API connector module for OpusCapita Business Network Portal.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35172
608