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

@iondrive/config

v0.0.8

Published

A 12-factor configuration module for Node.js/io.js.

Downloads

5

Readme

@iondrive/config

A 12-factor configuration module for Node.js/io.js.

Build Status

Principles

  • Application config is provided via environment variables. This ensures that infrastructure and deployment concerns don't leak into application code.
  • Every variable must be defined. This prevents scenarios where config can come from two or more places, such as defaults, command line or config files, simplifying debugging and setup.
  • Basic typed config, including enums. This prevents a category of bugs when checking config values, such as "false" !== false.

Install

npm install @iondrive/config

Usage

Definition file

When require('@iondrive/config') is invoked it loads a config definition file config.js in the current working directory or a file specified by the environment variable NODE_CONFIG_PATH. The config definition file should export a map of variable names to types (we use .js over .json to allow commenting and dynamic configurations).

An example config definition file:

// config.js

module.exports = {
  STR: 'string',
  BOOL: 'boolean',
  INT: 'integer',
  NUM: 'number',
  ENM: ['a', 'b', 'c'],
  DUR: 'duration'
};

Types

The type must be one of string, boolean, integer, number, enum (enum is implied when the value is an array) or duration.

  • The string type will match any value (since environment variables are all strings).
  • The boolean type will perform a case insenstive match on 'false', 'true', 'yes', 'no', 'y', 'n', '1' and '0'.
  • The integer type will only match integers in decimal notation, e.g. '123', '-555'.
  • The number type will only match decimal notation, e.g '123', '-3.14'.
  • The enum type will only match the string values provided.
  • The duration type will match either integers (where the value represents milliseconds) or a duration string accepted by the ms package, e.g. '15m', '6h' or '14d'.

### Advanced options

If the value of a config definition key is an object, it can declare the following properties:

  • type: the type of the config variable.
  • env: override the environment variable name (allows you to ignore the config prefix in special circumstances).
  • values: for an enum type, the values allowed.
  • validator: a custom validator function that is passed the config value string. If it returns false, the config value is invalid and an error will be thrown.
// config.js

module.exports = {
  NODE_ENV: {
    type: 'enum',
    env: 'NODE_ENV',
    values: ['development', 'test', 'production']
  }
};

module.exports = {
  CUSTOM_STRING: {
    type: 'string',
    validator: function (value) {
      return /^[a-z]+$/.test(value);
    }
  }
};

Application usage

Given the defintion file above and by running the app as follows:

APP_STR="hello" APP_BOOL="false" APP_INT="123" APP_NUM="3.14" APP_ENM="b" node app.js

The config can be accessed within the app as follows:

// app.js

var assert = require('assert');
var config = require('@iondrive/config');

assert.strictEqual(config.STR, 'hello');
assert.strictEqual(config.BOOL, false);
assert.strictEqual(config.INT, 123);
assert.strictEqual(config.NUM, 3.14);
assert.strictEqual(config.ENM, 'b');

Duration

Duration types are special in that instead of returning a primitive value, an object with conversion methods is returned to ensure that at the point of use the duration is in the correct units. These conversion methods always round the value, so be careful with your precision.

// Assuming APP_DUR has the value '2d'

config.DUR.asMilliseconds(); // 172800000
config.DUR.asSeconds(); // 172800
config.DUR.asMinutes(); // 2880
config.DUR.asHours(); // 48
config.DUR.asDays(); // 2
config.DUR.asYears(); // 0

Prefix

By default all variables must be prefixed by APP in the environment variables as above in order to prevent any clobbering of existing environment variables.

The default APP prefix can be changed with the environment variable NODE_CONFIG_PREFIX:

NODE_CONFIG_PREFIX="ABC" ABC_STR="hello" ABC_BOOL="false" ABC_INT="123" ABC_NUM="3.14" APP_ENM="b" node app.js

Recommended deployment

We recommend an environment file is created that defines the environment configuration for the app (presumably with permissions of 400 for the app user):

# environment

export APP_STR="hello"
export APP_BOOL="false"
export APP_INT="123"
export APP_NUM="3.14"
export APP_ENM="b"

This can be used by sourcing the file prior to executing the application:

. ./environment
node app.js

License

MIT