@opuscapita/config
Advanced tools
Comparing version 3.0.14 to 3.0.15
const ConfigClientBase = require('./ConfigClientBase'); | ||
const consul = require('consul'); | ||
const crypto = require('crypto'); | ||
@@ -131,3 +130,3 @@ const Promise = require('bluebird'); | ||
* remote service capacity problems. For further details have a look at the [DefaultConfig]{@link DefaultConfig}. | ||
* @param {mixed} keyOrPrefix - Key(s) to request. Can be either a single key string, a key-prefix, an array of keys or and array of key-prefixes. For using key-prefixes, the *recusrive* parameter has to be set to true. All keys an prefixes will automatically get prefixed with [serviceName]{@link serviceName}. | ||
* @param {string|array} keyOrPrefix - Key(s) to request. Can be either a single key string, a key-prefix, an array of keys or and array of key-prefixes. For using key-prefixes, the *recusrive* parameter has to be set to true. All keys an prefixes will automatically get prefixed with [serviceName]{@link serviceName}. | ||
* @param {boolean} recursive - If set to true, the *keyOrPrefix* parameter is used as key-prefix to recursively list all keys below this prefix. | ||
@@ -140,6 +139,5 @@ * @param {boolean} silent - If set to true, this method will not use retry to get something from consul so it will return faster and it will not pass any errors up to the calling code. | ||
if(Array.isArray(keyOrPrefix)) | ||
return Promise.all(keyOrPrefix.map(k => this.getProperty(k, recursive))); | ||
return Promise.all(keyOrPrefix.map(k => this.getProperty(k, recursive, silent))); | ||
const keyName = this.getFullKey(keyOrPrefix); | ||
const config = this.config; | ||
@@ -179,2 +177,34 @@ const isValidKey = !recursive && this.checkKey(keyOrPrefix); | ||
/** | ||
* Removes a single value or a list of values from consul's key-value store. | ||
* @param {string|array} keyOrPrefix - Key to remove. Will automatically get prefixed with [serviceName]{@link serviceName}. | ||
* @param {boolean} recursive - If set to true, the *keyOrPrefix* parameter is used as key-prefix to recursively list all keys below this prefix. | ||
* @param {boolean} silent - If set to true, this method will not use retry to get something from consul so it will return faster and it will not pass any errors up to the calling code. | ||
* @returns {Promise} Returns a bluebird [Promise]{@link http://bluebirdjs.com/docs/api-reference.html}. | ||
*/ | ||
deleteProperty(keyOrPrefix, recursive, silent) | ||
{ | ||
if(Array.isArray(keyOrPrefix)) | ||
return Promise.all(keyOrPrefix.map(k => this.deleteProperty(k, recursive))); | ||
const keyName = this.getFullKey(keyOrPrefix); | ||
const isValidKey = !recursive && this.checkKey(keyOrPrefix); | ||
const isValidPrefix = recursive && this.checkKeyPrefix(keyOrPrefix); | ||
if(isValidKey || isValidPrefix) | ||
{ | ||
const task = () => retry(() => this._deleteConsulValue(keyName, recursive), { max_tries : 5, interval : 500 }); | ||
if(silent) | ||
return task().catch(e => this.logger.error(e)); | ||
else | ||
return task(); | ||
} | ||
else | ||
{ | ||
return Promise.reject(new Error('The passed key or prefix is invalid: ' + keyOrPrefix)); | ||
} | ||
} | ||
/** | ||
* Gets an endpoint from consul's service registry. | ||
@@ -266,2 +296,14 @@ * Be aware, that this method only returns service endpoints that are marked *healthy* by consul. | ||
/** | ||
* Removes a value from consul's key-value store. Alias for [deleteProperty()]{@link deleteProperty}. | ||
* @param {string} key - Key to remove. Will automatically get prefixed with [serviceName]{@link serviceName}. | ||
* @param {boolean} recursive - If set to true, the *keyOrPrefix* parameter is used as key-prefix to recursively list all keys below this prefix. | ||
* @param {boolean} silent - If set to true, this method will not use retry to get something from consul so it will return faster and it will not pass any errors up to the calling code. | ||
* @returns {Promise} Returns a bluebird [Promise]{@link http://bluebirdjs.com/docs/api-reference.html}. | ||
*/ | ||
delete(keyOrPrefix, recursive, silent) | ||
{ | ||
return this.deleteProperty(keyOrPrefix, recursive); | ||
} | ||
/** | ||
* Gets an encrypted value from consul's key-value store. | ||
@@ -268,0 +310,0 @@ * This method uses retry in order to bypass problems with network connections, service latencies or |
@@ -136,10 +136,2 @@ const EventEmitter = require('events'); | ||
// async _getServiceData(consul, serviceName, passing = true) | ||
// { | ||
// return (await consul.health.service({ service : serviceName, passing })).map(n => ({ | ||
// host : n.Service.Address || n.Node.Address, | ||
// port : n.Service.Port | ||
// })); | ||
// } | ||
async _watchServices(consul, callback) | ||
@@ -218,4 +210,4 @@ { | ||
{ | ||
if(this.keyWatches[key]) | ||
return resolve(this.keyWatches[key]); | ||
// if(this.keyWatches[key]) | ||
// return resolve(this.keyWatches[key]); | ||
@@ -234,4 +226,4 @@ const watch = consul.watch({ method : consul.kv.keys, options : { key : key } }); | ||
{ | ||
if(this.keyWatches[key]) | ||
this.keyWatches[key].end(); | ||
// if(this.keyWatches[key]) | ||
// this.keyWatches[key].end(); | ||
@@ -349,6 +341,32 @@ delete this.keyWatches[key]; | ||
{ | ||
this.logger.warn('Config key %s could not be set: %s', keyName, e.message) | ||
this.logger.warn('Config key %s could not be set: %s', keyName, e.message); | ||
} | ||
} | ||
async _deleteConsulValue(keyName, recursive) | ||
{ | ||
try | ||
{ | ||
if(recursive) | ||
{ | ||
const keys = (await this.cache.keys()).filter(k => k.startsWith(keyName)); | ||
await Promise.all(keys.map(k => this.cache.delete(k))); | ||
} | ||
else | ||
{ | ||
await this.cache.delete(keyName); | ||
} | ||
await this.consul.kv.del({ key : keyName, recurse : recursive }); | ||
} | ||
catch(e) | ||
{ | ||
this.logger.warn('Config key %s could not be deleted: %s', keyName, e.message); | ||
return false; | ||
} | ||
return true; | ||
} | ||
_getRandomValue(array) | ||
@@ -355,0 +373,0 @@ { |
{ | ||
"name": "@opuscapita/config", | ||
"version": "3.0.14", | ||
"version": "3.0.15", | ||
"description": "Configuration API connector module for OpusCapita Business Network Portal.", | ||
@@ -8,6 +8,6 @@ "main": "index.js", | ||
"start": "npm run test", | ||
"test": "npm run setupConsul ; npx nyc mocha --timeout 30000 -R mocha-junit-reporter", | ||
"test-raw": "npm run setupConsul ; npx nyc mocha --timeout 30000", | ||
"test": "npm run setupConsul ; npx nyc mocha --exit --timeout 30000 -R mocha-junit-reporter", | ||
"test-raw": "npm run setupConsul ; npx nyc mocha --exit --timeout 30000", | ||
"setupConsul": "sh ./setup-consul.sh", | ||
"test-coverage": "npm run setupConsul ; npx nyc --reporter=lcov mocha --timeout 20000 && sed -i 's/\\/home\\/node\\/config\\//\\.\\//g' coverage/lcov.info", | ||
"test-coverage": "npm run setupConsul ; npx nyc --reporter=lcov mocha --exit --timeout 20000 && sed -i 's/\\/home\\/node/\\/Users\\/work\\/Documents\\/workspace/g' coverage/lcov.info", | ||
"upload-coverage": "cat ./coverage/lcov.info | npx coveralls", | ||
@@ -36,3 +36,3 @@ "api-doc": "npx jsdoc2md --files ./lib/* > wiki/Home.md", | ||
"bluebird-retry": "~0.11.0", | ||
"consul": "~0.33.1", | ||
"consul": "^0.34.0", | ||
"extend": "~3.0.1", | ||
@@ -39,0 +39,0 @@ "ocbesbn-cache": "~1.0.9", |
@@ -156,5 +156,5 @@ # @opuscapita/config | ||
retryTimeout : 1000, | ||
logger : null, | ||
logger : null, | ||
serviceSecretPath : `/run/secrets/${ConfigClientBase.serviceName}-consul-key` | ||
} | ||
``` |
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
37814
659
+ Addedconsul@0.34.1(transitive)
+ Addedpapi@0.29.1(transitive)
- Removedconsul@0.33.1(transitive)
- Removedpapi@0.28.0(transitive)
Updatedconsul@^0.34.0