confabulous
Advanced tools
Comparing version 0.2.2 to 0.3.0
const noop = require('lodash.noop') | ||
const confabulous = require('..') | ||
const Confabulous = confabulous.Confabulous | ||
const loaders = confabulous.loaders | ||
const processors = confabulous.processors | ||
const Confabulous = require('..') | ||
const loaders = Confabulous.loaders | ||
const processors = Confabulous.processors | ||
new Confabulous() | ||
.add((config) => loaders.env([ processors.mount({ key: 'env' }) ])) | ||
.add((config) => loaders.require({ path: './conf/defaults.js', watch: true })) | ||
.add((config) => loaders.require({ path: `./conf/${config.env.NODE_ENV}.js`, watch: true })) | ||
.add((config) => loaders.require({ path: './conf/runtime.js', mandatory: false })) | ||
.add((config) => loaders.file({ path: './conf/secret.json.encrypted' }, [ | ||
.add(config => loaders.env([ processors.mount({ key: 'env' }) ])) | ||
.add(config => loaders.require({ path: './conf/defaults.js', watch: true })) | ||
.add(config => loaders.require({ path: `./conf/${process.env.NODE_ENV}.js`, watch: true })) | ||
.add(config => loaders.require({ path: './conf/runtime.js', mandatory: false })) | ||
.add(config => loaders.file({ path: './conf/secret.json.encrypted' }, [ | ||
processors.decrypt({ algorithm: 'aes192', password: config.env.SECRET }), | ||
@@ -17,9 +16,6 @@ processors.json() | ||
.add((config) => loaders.args()) | ||
.add((config) => loaders.http({ url: config.server.url, mandatory: false, watch: { interval: '5m' } })) | ||
.on('loaded', (config) => console.log('Loaded', JSON.stringify(config, null, 2))) | ||
.on('reloaded', (config) => console.log('Reloaded', JSON.stringify(config, null, 2))) | ||
.on('error', (err) => console.error('Error', err)) | ||
.on('reload_error', (err) => console.error('Reload Error', err)) | ||
.end() | ||
.end((err, config) => { | ||
console.log('Loaded', JSON.stringify(config, null, 2)) | ||
}) | ||
setInterval(noop, Number.MAX_SAFE_INTEGER) | ||
setInterval(noop, Number.MAX_SAFE_INTEGER) |
12
index.js
var requireAll = require('require-all') | ||
var path = require('path') | ||
module.exports = { | ||
Confabulous: require('./lib/Confabulous'), | ||
loaders: requireAll(path.join(__dirname, './lib/loaders')), | ||
processors: requireAll(path.join(__dirname, './lib/processors')) | ||
} | ||
var Confabulous = require('./lib/Confabulous') | ||
Confabulous.Confabulous = Confabulous // Remain backwards compatible with old API (yuck!!!!) | ||
Confabulous.loaders = requireAll(path.join(__dirname, './lib/loaders')) | ||
Confabulous.processors = requireAll(path.join(__dirname, './lib/processors')) | ||
module.exports = Confabulous |
@@ -20,9 +20,9 @@ var debug = require('debug')('confabulous:Confabulous') | ||
self.end = function end() { | ||
self.end = function end(cb) { | ||
if (!cb) return self.end(onLoaded) | ||
debug('loading') | ||
self.emit('loading') | ||
setImmediate(function() { | ||
load(function(err, config) { | ||
if (err) return self.emit('error', err) | ||
self.emit('loaded', freeze(config)) | ||
if (err) return cb(err) | ||
cb(null, freeze(config)) | ||
}) | ||
@@ -32,2 +32,7 @@ }) | ||
function onLoaded(err, config) { | ||
if (err) return self.emit('error', err) | ||
self.emit('loaded', config) | ||
} | ||
function reload() { | ||
@@ -34,0 +39,0 @@ debug('reloading') |
{ | ||
"name": "confabulous", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "A hierarchical, asynchronous config loader and post processor", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -7,26 +7,15 @@ [![Build Status](https://travis-ci.org/guidesmiths/confabulous.png)](https://travis-ci.org/guidesmiths/confabulous) | ||
``` | ||
const confabulous = require('confabulous') | ||
const Confabulous = confabulous.Confabulous | ||
const loaders = confabulous.loaders | ||
const processors = confabulous.processors | ||
const Confabulous = require('confabulous') | ||
const loaders = Confabulous.loaders | ||
new Confabulous() | ||
.add((config) => loaders.env([ processors.mount({ key: 'env' }) ])) | ||
.add((config) => loaders.require({ path: './conf/defaults.js', watch: true })) | ||
.add((config) => loaders.require({ path: `./conf/${config.env.NODE_ENV}.js`, watch: true })) | ||
.add((config) => loaders.require({ path: './conf/runtime.js', mandatory: false })) | ||
.add((config) => loaders.file({ path: './conf/secret.json.encrypted' }, [ | ||
processors.decrypt({ algorithm: 'aes192', password: config.env.SECRET }), | ||
processors.json() | ||
])) | ||
.add((config) => loaders.args()) | ||
.on('loaded', (config) => console.log('Loaded', JSON.stringify(config, null, 2))) | ||
.on('reloaded', (config) => console.log('Reloaded', JSON.stringify(config, null, 2))) | ||
.on('error', (err) => console.error('Error', err)) | ||
.on('reload_error', (err) => console.error('Reload Error', err)) | ||
.end() | ||
.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, files, and web servers. The following loaders are proviced as separate modules | ||
Loaders are used to load config. Out of the box you can load config from command line parameters, environment variables and files. The following loaders are proviced as separate modules | ||
@@ -65,2 +54,3 @@ * [http-loader](https://github.com/guidesmiths/confabulous-http-loader) | ||
|----------|--------|-----------|---------| | ||
| 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 | | ||
@@ -80,2 +70,3 @@ | watch | boolean | undefined | Watching implemented via [fs.watch](https://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener). Be sure to read the caveats section if you encounter problems. | | ||
|----------|--------|-----------|---------| | ||
| 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 | | ||
@@ -140,5 +131,19 @@ | watch | boolean | undefined | Watching implemented via [fs.watch](https://nodejs.org/api/fs.html#fs_fs_watch_filename_options_listener). Be sure to read the caveats section if you encounter problems. | | ||
## Events | ||
### loaded | ||
Emitted when loading config for the first time. **Deprecated. Pass a callback to the ```end``` function instead.** | ||
### error | ||
Emitted when an error occurs loading config for the first time. **Deprecated. Pass a callback to the ```end``` function instead.** | ||
### 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.<br/> | ||
A. Because fs.watch problem doesn't notice them either. You can workaround by modifying some configuration watched by a different loader higher up in the confabulous stack | ||
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 | ||
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
33
547
146
27913
7