npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

consulea

v1.2.1

Published

Load Consul keys, environment vars, and command line arguments in a predictable, standardized way.

Downloads

17

Readme

Consulea

Load Consul keys, environment vars, and command line arguments in a predictable, standardized way.

Module goals:

  • Compile single config object from Consul kv, environment variables, and command line arguments.
  • Watch for changes in Consul prefix, send an event with updated config.
  • Verify required variables are set.
  • Be extremely light weight, having minimal dependencies.
  • Predictable config key outcome given different standards of input variables using different sources.
  • Simplify origin of keys in Consul and Env. Each project should have it's own namespace, nothing shared.

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.

Install

npm install --save consulea

Example

// Load module
const Consulea = require('consulea');

// Create new instance of module, pass in config
const consulea = new Consulea({
    consulToken: '4fe3dee9-4148-404e-9928-d95cfb1e6947',
    consulPrefix: 'test-svc/',
    envPrefix: 'TESTSVC',
    requiredKeys: ['serverName', 'port'],
    ifMissingKeysOnUpdate: 'warn',
    defaultData: {
        maxConnects: "100",
        timeOutMs: "10000",
    },
});

// Store your config however you please.
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', (err, data, meta) => {
    myConfig = data;
});

// Continue starting up project, with all config loaded for the first time.
// This event is only called once.
consulea.on('ready', (err, data) => {
    // Proceed with starting up... open service ports, etc.
});

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

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

Config is now available to project from three different sources:

| Consul key | Env key | CLI key | Resulting variable | | - | - | - | - | | test-svc/port | TESTSVC_PORT | --port | port | | test-svc/server-name | TESTSVC_SERVER_NAME | --server-name | serverName | | test-svc/max-connects | TESTSVC_MAX_CONNECTS | --max-connects | maxConnects | | test-svc/time-out-ms | TESTSVC_TIME_OUT_MS | --time-out-ms | timeOutMs |

# Assuming Consul has all of the above keys configured,
# they can be overridden by the ENV variables by doing:
export TESTSVC_MAX_CONNECTS=100
export TESTSVC_SERVER_NAME="New staging server"
node someScript.js
# ... or inline:
TESTSVC_MAX_CONNECTS=100 TESTSVC_SERVER_NAME="New staging server" node someScript.js

# And they can be overridden again by using CLI arguments:
node someScript.js --max-connects=50 --server-name="Prod server"

Config object options

| Key | Required | Description | | - | - | - | | consulToken | No | ACL Token used to authenticate with Consul service | | consulPrefix | Yes | Namespace/prefix to search for keys in Consul | | envPrefix | No | Namespace/prefix to search for keys in local environment | | requiredKeys | No | List of camelCased keys which are considered to be required | | consulClientConfig | No | Consul config object, if you'd rather configure it yourself | | ifMissingKeysOnStartUp | No | Set behavior when required keys are missing on first start up (default: exit) | | ifMissingKeysOnUpdate | No | Set behavior when required keys are missing on any update after start up (default: exit) | | suppressErrors | No | Set true to prevent Consulea from emitting errors. The emitted error event will still occur | | defaultData | No | Pass in default KV data, which may be overwritten by Consul, ENV or command line arguments |

ifMissingKeysOnStartUp and ifMissingKeysOnUpdate can be one of the following:

  • exit: Consulea will exit if any keys are missing.
  • warn: Consulea will warn if any keys are missing.
  • skip: Consulea will skip emitting the update/ready event if any keys are missing.
  • lastGoodValue: Consulea will try to use the last good value for a given missing key and will exit if any keys did not previously exist.

Notes

The Consul host:port can be also be defined using the environment variable CONSUL_HTTP_ADDR. This may be helpful if code is running in Docker or the Consul agent isn't running on the local instance.

# Don't use localhost:8500, connect somewhere else
export CONSUL_HTTP_ADDR='https://remoteconsulserver.com:8500'
node someScript.js