Détail du package

confabulous

guidesmiths3.9kMIT2.1.2

A pluggable, hierarchical, asynchronous config loader and post processor with support for environment variables, command line arguments, json, javascript, http, vault, etcd and postgres

configuration, loader, asynchronous, hierarchical

readme

Confabulous

Confabulous is a hierarchical, asynchronous config loader and post processor. It can load config from command line arguments, environment variables, files, web servers, databases, and even scm systems. It's easy to extend too. You can watch config sources for changes and apply post processors to do things like decrypt secrets or unflatten key/value pairs into structured objects.

NPM version NPM downloads Node.js CI Release and Publish Code Climate Test Coverage Code Style

TL;DR

const Confabulous = require('confabulous');
const loaders = Confabulous.loaders;

const confabulous = new Confabulous()
  .add((config) => loaders.require({ path: './conf/defaults.js' }))
  .add((config) => loaders.require({ path: './conf/production.js' }))
  .end((err, config) => {
    // Your code goes here
  });

Merging

Confabulous recursively merges (and subsequently freezes) configuration from multiple sources. If you want to override the default merge behaviour you can supply your own merge function, providing it is varardic and favours the right most parameter, e.g.

function shallow(...args) {
  return Object.assign({}, ...args);
}

new Confabulous({ merge: shallow })
  .add((config) => loaders.require({ path: './conf/defaults.js' }))
  .add((config) => loaders.require({ path: './conf/production.js' }))
  .end((err, config) => {
    // Your code goes here
  });

Loaders

Loaders are used to load config. Out of the box you can load config from command line parameters, environment variables and files.

args

Loads config from command line arguments

new Confabulous().add((config) => {
  return loaders.args();
});

You cannot watch command line arguments

env

Loads config from envrionment variables

new Confabulous().add((config) => {
  return loaders.env();
});

You cannot watch environment variables

require

Loads config from a .js or .json file

new Confabulous().add((config) => {
  return loaders.require({ path: './conf/defaults.js' });
});
Option Type Default Notes
path string undefined The javascript or json config file to be required
mandatory boolean true Causes an error/reload_error to be emitted if the configuration does not exist
watch boolean undefined Watching implemented via fs.watch. Be sure to read the caveats section if you encounter problems.

file

Loads config from the specified file. Files are read using the specified encoding (defaults to 'utf8'). Use a post processor if you want to convert them to json.

new Confabulous().add((config) => {
  return loaders.file({ path: './conf/defaults.js' }, [processors.json()]);
});
Option Type Default Notes
path string undefined The config file to be read
mandatory boolean true Causes an error/reload_error to be emitted if the configuration does not exist
watch boolean undefined Watching implemented via fs.watch. Be sure to read the caveats section if you encounter problems.
encoding string utf8 Specified the file encoding

More Loaders

The following loaders are proviced as separate modules

Post Processors

Post processes can be used to transform or validate your configuration after it's been loaded. Out of the box you can mount config at a specified key, unflatten key value pairs into structured documents, parse json, decrypt content and transform environment variables.

mount

Mounts the configuration at the specified key

new Confabulous().add((config) => {
  return loaders.require({ path: './extra.json' }, [processors.mount({ key: 'move.to.here' })]);
});

unflatten

Unflattens config into structured documents. Useful for command line arguments and environment variables.

new Confabulous().add((config) => {
  return loaders.env([processors.unflatten()]);
});

envToProp

Converts environment variables in the form NODE_ENV=test to nested properties in the form { node: { env: "test" } }

new Confabulous().add((config) => {
  return loaders.env([processors.envToProp()]);
});

If you want to prefix your environment variables with an application discriminator you can also strip the prefix.

new Confabulous().add((config) => {
  return loaders.env([
    processors.envToProp({ prefix: 'GS_' }), // GS_SERVER_PORT => server.port
  ]);
});

You can also filter environment variables to include only those you want

new Confabulous().add((config) => {
  return loaders.env([
    processors.envToProp({ filter: /^GS_/ }), // Only include environment variables starting with GS_
  ]);
});

envToCamelCaseProp

Converts environment variables in the form USER__FIRST_NAME=fred to nested properties in the form { user: { firstName: "fred" } }

new Confabulous().add((config) => {
  return loaders.env([processors.envToCamelCaseProp()]);
});

If you want to prefix your environment variables with an application discriminator you can also strip the prefix.

new Confabulous().add((config) => {
  return loaders.env([
    // GS_SERVER_PORT => server.port
    processors.envToCamelCaseProp({ prefix: 'GS_' }),
  ]);
});

You can also filter environment variables to include only those you want

new Confabulous().add((config) => {
  return loaders.env([
    // Only include environment variables starting with GS_
    processors.envToCamelCaseProp({ filter: /^GS_/ }),
  ]);
});

json

Parses text into JSON.

new Confabulous().add((config) => {
  return loaders.file({ path: './config.json.encrypted' }, [processors.json()]);
});

decrypt

Decrypts encrypted configuration.

new Confabulous().add((config) => {
  return loaders.file({ path: './config.json.encrypted' }, [
    processors.decrypt({
      algorithm: 'aes-192-cbc',
      key: process.env.SECRET_KEY,
      iv: process.env.IV,
    }),
    processors.json(),
  ]);
});

Events

closing

Calling confabulous.close will emit a 'closing' event. This can be used by loaders to free up resources (e.g. close file watchers)

loaded

Deprecated. Pass a callback to the end function instead. Emitted when loading config for the first time.

error

Deprecated. Pass a callback to the end function instead. Emitted when an error occurs loading config for the first time.

reloaded

Emitted when confabulous successfully reloads a watched config.

reload_error

Emitted when confabulous encounters an error reloading a watched config

FAQ

Q. Why doesn't Confabulous notice new files?
A. Because fs.watch doesn't notice them either. You can workaround by modifying some configuration watched by a different loader higher up in the confabulous stack

Q. Why does jest emit a FSEVENTWRAP error?
A. Because you have configured a loader to watch for changes, but not called confabulous.close() in your test teardown

changelog

Change Log

Unreleased

  • Fixed a few typos in the changelog

2.1.2 (2024-07-01)

☁️ CI

2.1.1 (2024-07-01)

🐛 Bug Fixes

  • added missing flags to NPM publication CI script (e76c12c)
  • coverage test (2cc4a0b)
  • typo in pre-commit hook (c059b9e)

🔧 Others

  • add .nvmrc file with node 14 (ff48224)
  • bump to 2.0.2 (034a64e)
  • extended license (4885ca2)
  • recover package-lock.json (35b9123)
  • recover package-lock.json (f3689dc)
  • regenerate package-lock with node 14 and npm 6 (2b584c3)
  • remove obsolete release gh action (2b30418)
  • remove package-lock-json (039a1c4)
  • rename ci workflow (756f8c7)
  • temporally remove code-climate coverage (4f52ef6)
  • try to make it work with npm 7 (7512788)

📝 Docs

  • improve readme file (358c692)
  • update license reference to MIT (f08d43d)
  • updated license file to MIT (4e3e4f4)

🔄 Code Refactoring

  • remove require-all and do the imports explicitly (416790f)

☁️ CI

  • add .npmrc file configuration (d039537)
  • add g action for code climate test coverage (b869fc0)
  • add g action for pull request validation (d0d1921)
  • add g action for scorecard (4926bbd)
  • add gh action to automate release process (47ed962)
  • improve github actions (f69cba7)
  • modify coverage command (918330f)
  • remove test coverage gh action (6ac5863)

2.1.0

Added

  • confabulous.close - See readme for details

Updated

  • Update dev dependencies
  • Move to prettier

2.0.3

Updated

  • Update dev depenencies

2.0.2

Updated

  • Fix build

2.0.1

Updated

  • Update zUnit
  • Use new npm token

2.0.0

Breaking Changes

Updated

  • Update dependencies
  • Remove lodash.noop dependency
  • Replace mocha with zUnit
  • Replace lodash.set, lodash.has with dot-prop
  • Replace imperative with esnext style
  • Replace chai with node assert
  • Update husky
  • Replace travis with github actions
  • Replace merge with ramda.mergeDeepRight due to issue 41](https://github.com/yeikos/js.merge/issues/41

1.7.0

Updated

  • Update dependencies

1.6.0

Updated

  • Update dependencies

1.5.5

Updated

  • Updated dependencies

1.5.4

Updated

  • Updated dependencies
  • Fixed node deprecation warnings in tests

1.5.3

Updated

  • Improved readme

1.5.2

Updated

  • Improved readme

1.5.1

Updated

  • Improved readme

1.5.0

Added

  • Support for custom merges

Updated

  • Dependencies

1.4.0

Added

  • Codeclimate automation on push

Updated

  • Upgraded dev dependencies

1.3.0

Updated

  • Updated dev dependencies

1.2.0

Added

  • envToCamelCaseProps processor

1.1.3

Updated

  • Updated package.json description

1.1.2

Updated

  • Updated package.json keywords

1.1.1

Added

  • Increasing test coverage
  • This changelog

The format is based on Keep a Changelog](http://keepachangelog.com/)