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

@leisurelink/env-configurator

v0.4.2

Published

Configuration library that attempts to get configuration based on environment variables, Consul.io KV stores, and DNS SRV records

Downloads

5

Readme

env-configurator

env-configurator manages configuration look up for applications and their libraries. Currently, env-configuration supports pulling configuration from environment variables, Consul.io KV stores, and DNS SRV records.

The basic concept of the library is that an application gives it a configuration spec with JSON ptrs that, when fulfilled, will correspond to configuration values required by the application. See config-example.json for a basic example of a configuration spec which may be given to env-configurator.

env-configuration manages configuration for the client library or application and therefore does not return a configuration object. Instead, after indicating that configuration is complete, env-configurator makes configuration keys available via a #get function.

Notes & TODOs

  • Reorganize tests into more descriptive describe blocks
  • Refactor the DNS resolution module (lib\dns.js). I don't like the way errors are currently handled and I really don't like how the module is dependent on having the app config passed to it for prefix and suffix resolution and DNS fallback. This module is currently what prevents the configuration providers (Consul, Env, others?) from being reordered.
  • Add support for more configuration providers (etcd, files, remote files, etc)

Installation

Clone this repository and use npm to install dependencies.

npm install

##Tests

npm test

or

mocha -R spec

Configuring the configurator

env-configurator requires a configuration object in order to determine what configuration keys to fetch for the client. The configurator validates configuration specification passed to it against a JSON schema located at lib\configuration-schema.json. An example configuration spec that would validate is:

{
    "name": "mylib",
    "services": [
        {
            "name": "someapi.service.consul",
            "key": "#/someapi/uri"
        },
        {
            "name": "authentic.service.consul",
            "key": "#/security/uri"
        }
    ],
    "keys": [
        "#/someapi/apikey",
        "#/privatekeyUri"
    ]
}

The configuration spec declares names (in the keys array and key property of the services array objects) at which it will make configuration available. For example, if the above config spec resulted in a successful configuration then a configuration key could be retrieved by calling:

configuratorObj.get('mylib', '#/someapi/apikey');

Optional keys

Optional keys get a separate section because they're special. Optional keys should be used sparingly in order to avoid unintentionally placing default values in your application. That being said, sometimes it is useful to use the absence of a configuration key as a configuration parameter.

Add an optional array in order to declare keys (defined in either the services or keys array) as optional. The functionality of the optional array is essentially the inverse of the required array in a JSON Schema definition.

Example:

{
    "name": "myApp",
    "keys": [
        "#/foo"
    ],
    "optional": [
        "#/foo"
    ]
}

In the above example if #/foo were missing no errors will be thrown by the library during configuration. Any subsequent call to get('myApp', '#/foo') will return undefined.

Lookup conventions

In order to retrieve configuration keys the configurator uses a couple conventions.

Environment lookup

The configurator uses the client's provided name and keys to retrieve configuration from environmental varibles. Specifically, given:

{
    "name": "a",
    "keys": [
        "#/foo"
    ]
}

The configurator will look for an environment variable with the name A_FOO. In short, names are prefixed with the provided app name, '/' are transformed into '_', and all components are upper cased.

When naming variables

Keep in mind rules for POSIX and Windows environment variable names:

Generally speaking, things matching /[A-Z0-9_]+/ should be safe to use.

Consul KV lookup

Given:

{
    "name": "a",
    "keys": [
        "#/foo"
    ]
}

The configurator will lookup all keys prefixed with a/ in Consul and attempt to find a key with the name full path name a/foo in the results.

DNS SRV lookup

Services are handled slightly differently from keys in that they have a separate configuration section and a fallback in case of lookup failure.

Given:

{
    "name": "a",
    "sevices": [
        {
            "name": "foo.bar.com",
            "key": "#/foo/bar/uri"
        }
    ]
}

The configurator will attempt to lookup a DNS SRV record with the name foo.bar.com and assign it the config path #/foo/bar/uri. If and only if the SRV record lookup fails will the configurator attempt to do an env or consul kv look up for the value based on the key.

Example usage

var Configurator = require('env-configurator'),
       configurator = new Configurator();
   
   configurator.fulfill({
        "name": "mylib",
        "services": [
            {
                "name": "someapi.service.consul",
                "key": "#/someapi/uri"
            },
            {
                "name": "authentic.service.consul",
                "key": "#/security/uri"
            }
        ],
        "keys": [
            "#/someapi/apikey",
            "#/privatekeyUri"
        ]
    }, function(errs) {
        if (!errs) {
            //Config was successful
            console.log(configurator.get('mylib', '#/security/uri');
        } else {
            //errs contains one or more error objs
        }
    });