Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

consulea

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

consulea - npm Package Compare versions

Comparing version 1.1.6 to 1.2.0

test/index.test.js

101

consulea.js

@@ -1,5 +0,5 @@

var EventEmitter = require('events').EventEmitter;
var util = require('util');
var consul = require('consul');
var camelCase = require('camelcase');
const EventEmitter = require('events').EventEmitter;
const util = require('util');
const consul = require('consul');
const camelCase = require('camelcase');

@@ -10,9 +10,9 @@ /**

*/
function makeConsulConfig (configIn) {
function makeConsulConfig (configIn, envObj) {
// Consul client config can be passed in whole
var consulConfig = configIn.consulClientConfig || {};
const consulConfig = configIn.consulClientConfig || {};
// Consul host/port can be sourced from env CONSUL_HTTP_ADDR
if (process.env.CONSUL_HTTP_ADDR) {
var httpAddr = process.env.CONSUL_HTTP_ADDR;
if (envObj.CONSUL_HTTP_ADDR) {
let httpAddr = envObj.CONSUL_HTTP_ADDR;
consulConfig.secure = (httpAddr.substr(0, 5) === 'https');

@@ -31,2 +31,3 @@ httpAddr = httpAddr.replace('http://', '').replace('https://', '');

}
exports.makeConsulConfig = makeConsulConfig;

@@ -42,6 +43,6 @@ /**

if (dataIn) {
for (var i = 0; i < dataIn.length; i++) {
for (let i = 0; i < dataIn.length; i++) {
if (dataIn[i].Key !== undefined && dataIn[i].Value !== undefined) {
// Standardize the key
var kvKey = dataIn[i].Key;
let kvKey = dataIn[i].Key;
kvKey = kvKey.replace(prefix, '');

@@ -59,2 +60,3 @@ kvKey = camelCase(kvKey);

}
exports.parseConsul = parseConsul;

@@ -67,12 +69,12 @@ /**

*/
function parseEnv (kvData, prefix) {
function parseEnv (kvData, envObj, prefix) {
if (prefix) {
prefix += '_';
Object.keys(process.env).forEach(function (envKey) {
Object.keys(envObj).forEach((envKey) => {
// Skip env keys which don't start with prefix
if (envKey.substring(0, prefix.length) === prefix) {
// Standardize the key
var kvKey = envKey.replace(prefix, '');
let kvKey = envKey.replace(prefix, '');
kvKey = camelCase(kvKey);
kvData[kvKey] = process.env[envKey];
kvData[kvKey] = envObj[envKey];
}

@@ -83,2 +85,3 @@ });

}
exports.parseEnv = parseEnv;

@@ -90,11 +93,11 @@ /**

*/
function parseArgs (kvData) {
process.argv.forEach(function (val) {
function parseArgs (kvData, args) {
args.forEach((val) => {
// Skip args that don't start with "--"
if (val.substring(0, 2) === '--') {
// Break apart key/value
var argParts = val.substring(2).split('=', 2);
const argParts = val.substring(2).split('=', 2);
if (argParts.length > 1) {
// Standardize the key
var newKey = camelCase(argParts[0]);
const newKey = camelCase(argParts[0]);
kvData[newKey] = argParts[1];

@@ -106,2 +109,3 @@ }

}
exports.parseArgs = parseArgs;

@@ -115,6 +119,6 @@ /**

function findMissingKeys (kvData, requiredKeys) {
var missingKeys = [];
const missingKeys = [];
if (requiredKeys) {
for (var i = 0; i < requiredKeys.length; i++) {
var requiredKey = requiredKeys[i];
for (let i = 0; i < requiredKeys.length; i++) {
const requiredKey = requiredKeys[i];
if (kvData[requiredKey] === undefined) {

@@ -127,2 +131,3 @@ missingKeys.push(requiredKey);

}
exports.findMissingKeys = findMissingKeys;

@@ -136,9 +141,9 @@

function findChangedKeys (self) {
var changedKeys = [];
var temp = JSON.parse(JSON.stringify(self.lastGoodKvData));
const changedKeys = [];
const temp = JSON.parse(JSON.stringify(self.lastGoodKvData));
// Loop on previous findings, check for existence of and compare Key=>ModifyIndex map
var newKeys = Object.keys(self.kvData);
for (var j = 0; j < newKeys.length; j++) {
var newKey = newKeys[j];
var newVal = self.kvData[newKey];
const newKeys = Object.keys(self.kvData);
for (let j = 0; j < newKeys.length; j++) {
const newKey = newKeys[j];
const newVal = self.kvData[newKey];
if (temp[newKey] && temp[newKey] === newVal) {

@@ -151,3 +156,3 @@ delete temp[newKey];

// Standardize the key
Object.keys(temp).forEach(function (kvKey) {
Object.keys(temp).forEach((kvKey) => {
kvKey = kvKey.replace(self.config.consulPrefix, '');

@@ -158,2 +163,3 @@ changedKeys.push(camelCase(kvKey));

}
exports.findChangedKeys = findChangedKeys;

@@ -163,3 +169,3 @@ /**

*/
var Consulea = (function () {
const Consulea = (function () {
// JavaScript Pseudo-class constructor

@@ -172,3 +178,3 @@ function Consulea (configIn) {

var self = this;
const self = this;
this.config = configIn;

@@ -179,3 +185,3 @@ this.kvDataDefault = configIn.defaultData || {};

this.isReady = false;
this.consulConfig = makeConsulConfig(this.config);
this.consulConfig = makeConsulConfig(this.config, process.env);
this.consulClient = consul(this.consulConfig);

@@ -211,3 +217,3 @@ this.lastGoodKvData = {};

} else {
setTimeout(function () {
setTimeout(() => {
self.callbackWhenReady(callback);

@@ -232,3 +238,3 @@ }, 50);

Consulea.prototype.watchStart = function () {
var self = this;
const self = this;

@@ -245,3 +251,3 @@ // Start a watcher

// When Consul namespace changes or is first loaded...
this._watcher.on('change', function (response, res) {
this._watcher.on('change', (response, res) => {
// Try to catch errors using http.IncomingMessage

@@ -259,19 +265,19 @@ if (res.statusCode !== 200) {

// Build a new kvData from Consul, then Env, then Arguments
var kvData = self.kvDataDefault;
let kvData = self.kvDataDefault;
kvData = parseConsul(kvData, response, self.config.consulPrefix);
kvData = parseEnv(kvData, self.config.envPrefix);
kvData = parseArgs(kvData);
kvData = parseEnv(kvData, process.env, self.config.envPrefix);
kvData = parseArgs(kvData, process.argv);
self.kvData = kvData;
var changedKeys = findChangedKeys(self);
const changedKeys = findChangedKeys(self);
// Verify require keys
var missingKeys = findMissingKeys(kvData, self.config.requiredKeys);
const missingKeys = findMissingKeys(kvData, self.config.requiredKeys);
// Something is missing
if (missingKeys.length > 0) {
var missingKeyList = missingKeys.join(', ');
const missingKeyList = missingKeys.join(', ');
// This is handled differently, depending on if this is the first time or not
var whichRule = (self.initialLoad ? 'ifMissingKeysOnStartUp' : 'ifMissingKeysOnUpdate');
var ruleValue = self.config[whichRule];
const whichRule = (self.initialLoad ? 'ifMissingKeysOnStartUp' : 'ifMissingKeysOnUpdate');
const ruleValue = self.config[whichRule];

@@ -304,4 +310,4 @@ switch (ruleValue) {

case 'lastGoodValue':
for (var i = 0; i < missingKeys.length; i++) {
var missingKey = missingKeys[i];
for (let i = 0; i < missingKeys.length; i++) {
const missingKey = missingKeys[i];
if (self.lastGoodKvData[missingKey] !== undefined) {

@@ -337,3 +343,3 @@ self.handleError({

// Make a copy so the code using this module can not modify kvData by accident
var kvDataCopy = JSON.parse(JSON.stringify(self.kvData));
const kvDataCopy = JSON.parse(JSON.stringify(self.kvData));

@@ -355,3 +361,3 @@ // Emit "update" event every time there is a change, and include some extra metadata

// Emit "error" when Consul connection emits an error
this._watcher.on('error', function (err) {
this._watcher.on('error', (err) => {
self.handleError({

@@ -372,2 +378,3 @@ code: 'CLIENT_ERR',

module.exports = Consulea;
// module.exports = Consulea;
exports.Consulea = Consulea;
// Load module
var Consulea = require('./');
const Consulea = require('./');
// Create new instance of module, pass in config
var consulea = new Consulea({
const consulea = new Consulea({
// consulClientConfig: {

@@ -18,5 +18,5 @@ // host: 'localhost',

// Store your config however you please.
var myConfig = {};
const myConfig = {};
consulea.on('update', function (err, newConfig, meta) {
consulea.on('update', (err, newConfig, meta) => {
// The state of config has changed, use this event to save a new copy or action upon the result.

@@ -29,3 +29,3 @@ // This event is called every time the Consul namespace is updated and also upon first start.

consulea.on('ready', function (err, newConfig) {
consulea.on('ready', (err, newConfig) => {
// Continue starting up project, with all config loaded for the first time.

@@ -40,3 +40,3 @@ // This event is only called once.

consulea.on('error', function (err) {
consulea.on('error', (err) => {
console.error('consulea on-error triggered: ', err);

@@ -46,7 +46,7 @@ });

// Optionally stop the watch
setTimeout(function () {
setTimeout(() => {
consulea.watchStop();
}, 5000);
setTimeout(function () {
setTimeout(() => {
consulea.watchStart();

@@ -57,2 +57,2 @@ }, 10000);

// Run example script for 15 minutes
setTimeout(function () {}, 900000);
setTimeout(() => {}, 900000);

@@ -10,3 +10,3 @@ /**

var Consulea = require('./consulea');
const Consulea = require("./consulea");

@@ -17,2 +17,3 @@ /**

module.exports = Consulea;
// Publically export only the Consulea class
module.exports = Consulea.Consulea;
{
"name": "consulea",
"version": "1.1.6",
"version": "1.2.0",
"description": "Load Consul keys, environment vars, and command line arguments in a predictable, standardized way.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "tape test/*.test.js | tap-diff"
},

@@ -38,4 +38,8 @@ "repository": {

"camelcase": "~3.0.0",
"consul": "^0.30.0"
"consul": "^0.40.0"
},
"devDependencies": {
"tap-diff": "~0.1.1",
"tape": "^5.3.2"
}
}

@@ -11,3 +11,2 @@ # Consulea

- Simplify origin of keys in Consul and Env. Each project should have it's own namespace, nothing shared.
- No ES6 dependency, so it works under Node.js 0.10 and onward.

@@ -24,6 +23,6 @@ Variables are first read from Consul, then the environment, then command line arguments, allowing the user to override something already previously set in Consul. The config key `webPort` can be set by Consul key `test-svc/web-port` and can be overridden by environment variable `TESTSVC_WEB_PORT`, and both can be overridden by `--web-port`.

// Load module
var Consulea = require('consulea');
const Consulea = require('consulea');
// Create new instance of module, pass in config
var consulea = new Consulea({
const consulea = new Consulea({
consulToken: '4fe3dee9-4148-404e-9928-d95cfb1e6947',

@@ -41,7 +40,7 @@ consulPrefix: 'test-svc/',

// Store your config however you please.
var myConfig = {};
let myConfig = {};
// This event is called every time the Consul namespace is updated and upon first start.
// "meta" will supply things like keysChanged, and initialLoad
consulea.on('update', function (err, data, meta) {
consulea.on('update', (err, data, meta) => {
myConfig = data;

@@ -52,3 +51,3 @@ });

// This event is only called once.
consulea.on('ready', function (err, data) {
consulea.on('ready', (err, data) => {
// Proceed with starting up... open service ports, etc.

@@ -58,3 +57,3 @@ });

// Or, if callbacks are more your style...
consulea.callbackWhenReady(function (err){
consulea.callbackWhenReady((err) => {
// Proceed with starting up... open service ports, etc.

@@ -64,3 +63,3 @@ });

// Handle errors and warnings as you see fit
consulea.on('error', function (err) {
consulea.on('error', (err) => {
console.error('Consulea error:', err);

@@ -67,0 +66,0 @@ });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc