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

static-config

v2.2.0

Published

Config loader for static application configuration

Downloads

21

Readme

static-config

Config loader for static application configuration

Motivation

The static configuration for your application should come from only a few places:

  • Files checked into git
  • Configuration values passed in from the command line
  • A configuration object created in your tests for test overwrites

The first two are used in production, the last one is necessary to sanely integrate with tests.

Here is an example of how you might configure your application

// server.js
var StaticConfig = require('static-config');
var parseArgs = require('minimist');
var fs = require('fs');
var path = require('path');

var MyApplication = require('./app.js');

if (require.main === module) {
    main(parseArgs(process.argv.slice(2)));
}

function main(argv) {
    var configFolder = path.join(__dirname, 'config');
    var environment = argv.env || 'local';
    var dc = fs.existsSync('/etc/datacenter') ?
        fs.readFileSync('/etc/datacenter') : 'no-dc';

    var config = StaticConfig({
        files: [
            path.join(configFolder, 'production.json'),
            path.join(configFolder, environment + '.json'),
            path.join(configFolder, environment + '-' + dc + '.json'),
            path.join(configFolder, 'secrets', 'secrets.json')
        ],
        seedConfig: null
    });

    config.set('argv.port', argv.port);
    config.freeze();

    var app = MyApplication(config, {
        /* ... */
    });

    app.bootstrap();
}

Config documentation

StaticConfig takes two options, options.files and options.seedConfig.

The options.files is required and must be a list of file paths. The options.seedConfig is optional and will be used to overwrite configuration key value pairs if present.

The options.files must be a list of JSON files. Each file must be a flat object of key, value pairs. It's recommended that you use keys like:

{
    "name": "my-name",
    "clients.thingy": {
      "some client": "config"
    },
    "server.my-port": 9999
}

To organize your configuration file.

config.get(key)

The get() call will either return the value for the key or throw a key does not exist exception.

It first reads from the JSON files to see if the key exists. if the key exists in multiple files then the last file in the array wins.

If the key exists in seedConfig then it will favor seedConfig over the JSON files.

If someone has manually set() the key then it will always favor that value over files and over seedConfig.

config.set(key, value)

You can take configuration from external sources, for example from command line arguments and store them in static config.

Any keys take from external sources must not exist in the JSON files.

set() will throw an exception if the key exists

config.inspect()

inspect() returns the entire config object. This should not be mutated and should only be used for inspection or debugging

config.freeze()

Once you freeze the config any further calls to config.set() will throw.

This allows you to make the static config immutable

config.destroy()

Once the config is destroyed any further calls to config.get() will throw.

This allows you to terminate the configuration phase and gives you confidence that your application is now officially bootstrapped.

Installation

npm install static-config

Tests

npm test

Contributors

  • Raynos

MIT Licensed