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

c11n

v0.9.1

Published

Dead simple configuration loader and parser for node deployments.

Readme

view on npm npm module downloads per month

c11n

c11n is a dead simple configuration loader and parser for node deployments.
It outputs an object that encapsulates unified configuration settings from environment variables, json files and command line arguments.

Usage

Let's consider we have an example.json file with the following content:

{
    "redis": { 
        "host": "redis.my-great-app.com",
        "port": 6379
    },

    "facebook": {
        "id": 123456789,
        "key": "myfacebookapikey"
    } 
}

Drop a few lines into index.js:

const conf = require('c11n')

conf('./example', { prefix: 'my_app' })
    .then((config) => console.log(config))
    .catch((error) => console.error(error.stack || error))

Finally, expose settings to env and argv as well and run our app:

export my_app_domain=my-great-app.com
export my_app_redis_auth=foobar
export my_app_cookie_secret=foobar
export my_app_cookie_maxAge=365

node ./ --cookie_secret=barfoo --http_port=8080

Output:

{ 
  domain: 'my-great-app.com',
  redis: { 
    auth: 'foobar', 
    host: 'redis.my-great-app.com', 
    port: 6379 
  },
  cookie: { 
    secret: 'barfoo', 
    maxage: 365 
  },
  facebook: { 
    id: 123456789, 
    key: 'myfacebookapikey' 
  },
  http: { port: 8080 } 
}

API

var c11n = require('c11n')

c11n([filePaths...], [options], [callback]) ⇒ Promise

options.prefix Include only those env variables which are starting with this prefix. Defaults to null. options.dir Directory path to try to find and include NODE_ENV.json. Defaults to null. options.dirs An array of directory paths. Each of them will be treated like options.dir.

Gather and merge configuration from env, additional .json files and argv in this order. c11n supports both error-first callback and Promise APIs.

Note If no file extension provided in a filePath then .json will be used.

c11n.load([filePaths...], [options], [callback]) ⇒ Promise

It's the module.exports of this package, same as the above-mentioned.

c11n.loadFromEnv([options]) ⇒ Object

options.prefix Include only those env variables which are starting with this prefix. Defaults to null.

Parse configuration from process.env and return result immediately.

c11n.loadFromFile(path, [options], [callback]) ⇒ Promise

Read and parse configuration from the file at given path. Please notice that, it's an async function.

c11n.loadFromArgv([options]) ⇒ Object

Parse configuration from process.argv and return result immediately. This function uses minimist internally for pre-parsing.

c11n.loadFromObject(object, [options]) ⇒ Object

Parse configuration from any given object and return result immediately. Used internally by other methods of this module.

This function iterates over keys of an object and breaks it into sub-objects along the '.'s and '_'s as separator characters. Leading and trailing separators are ignored, repeating separators are reduced to a single '_'.

Example

const obj = {
    '__redis_port': 6379,
    'redis__host': 'localhost',
    'domain___': 'my-domain.com',
    'http.port': 8080,
    '_https...port': 8081
}

console.log(c11n.loadFromObject(obj))

Output:

{
    redis: {
        port: 6379,
        host: 'localhost'
    },
    domain: 'my-domain.com',
    http: { port: 8080 },
    https: { port: 8081 }
}

If a key has a value but there are nested settings for the same key, then the original value will be accessible under the special key: '_'.

Example

const obj = {
   domain: 'example.com',
   cache: true,
   cache_ttl: 1209600,
   cache_size: '300mb'
}

console.log(c11n.loadFromObject(obj))

Output:

{
   domain: 'example.com',
   cache: {
       _: true,
       ttl: 1209600,
       size: '300mb'
   }
}

options.freeze

If set to false then resulting object will be mutable. Defaults to true. All the above-mentioned methods are accepting this option.

Installation

With npm:

npm install --save c11n

With git:

git clone git://github.com/schwarzkopfb/c11n.git
cd c11n
npm test

License

MIT license.