Name: node-bigfig
Owner: Yahoo Inc.
Description: configs that vary based on multiple considerations
Created: 2015-08-06 04:34:14.0
Updated: 2017-04-27 14:49:26.0
Pushed: 2017-04-27 14:49:25.0
Homepage: https://www.npmjs.com/package/bigfig
Size: 26
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
configuration objects that vary based on multiple considerations
Intended Audience: Applications which need to be configured in particular ways for different environments.
NODE_ENV
), so it
doesn't dictate how you solve your problemread()
runs as fast a possibleThis example is a bit complex to demonstrate some of the value/features of this library. Your config can be as simple/complex as you need it to be ?
bigfig = require("./index.js");
fig, config;
= new bigfig.Config({
// default (development)
apiURL: "http://localhost:3001/",
assetURL: "http://localhost:3000/static",
// don't expose this config to the client!
"__context?runtime=server": {
listenPort: 3000,
memcache: {
host: "localhost",
port: 11211
},
},
// stand-alone staging environment for validation testing
"__context?env=staging": {
listenPort: 80,
apiURL: "http://staging.mysite.com:4080/",
assetURL: "http://staging.mysite.com/static",
memcache: {
host: "memcache.staging.mysite.com"
}
},
// adjust the config for production hosts
"__context?env=production": {
apiURL: "http://api.mysite.com/",
assetURL: "http://cdn.provider.com/mysite/",
"__context?secure=true": {
assetURL: "https://cdn.provider.com/mysite/"
}
},
"__context?env=production&runtime=server": {
listenPort: 80,
// perhaps you have more than one type of production host
"__context?colo=east": {
apiURL: "http://api.east.mysite.com:4080/",
memcache: {
host: "memcache.east.mysite.com",
// for some legacy reason the eastern memcached is on a weird port
port: 11666
}
},
"__context?colo=west": {
apiURL: "http:/api.west.mysite.com:4080/",
memcache: {
host: "memcache.west.mysite.com"
}
}
}
});
ig = fig.read({
runtime: "server",
env: "production", // might come from process.env.NODE_ENV
colo: "east" // might be interpreted from the hostname
apiURL: 'http://api.east.mysite.com:4080/',
assetURL: 'http://cdn.provider.com/mysite/',
listenPort: 80,
memcache: {
host: 'memcache.east.mysite.com',
port: 11666
}
ig = fig.read({
runtime: "client",
env: "production", // might come from process.env.NODE_ENV
secure: "true", // might come from req.protocol === "https"
apiURL: 'http://api.mysite.com/',
assetURL: 'https://cdn.provider.com/mysite/'
The source of the configuration is a JSON-like object – an object with scalars, and objects and arrays which can be nested arbitrarily deep.
port: 80,
memcache: {
host: "localhost",
port: 11211,
settings: {
timeout: 1000
}
}
This simple config we call a default or root.
(If this simple approach meets your needs then you probably don't need this library ?)
You can add sections, each of which describes how the config should be different for a different situation. The situation is described by a set of keys and values we call a context.
port: 80,
memcache: {
host: "localhost",
port: 11211,
settings: {
timeout: 1000
}
},
"__context?env=staging": {
memcache: {
host: "memcache.staging.mysite.com"
}
},
"__context?env=production": {
memcache: {
host: "memcache.mysite.com"
}
}
As well, a section can have further speciallizations within it.
port: 80,
memcache: {
host: "localhost",
port: 11211,
settings: {
timeout: 1000
}
},
"__context?env=production": {
"__context?colo=east": {
memcache: {
host: "memcache.east.mysite.com"
}
},
"__context?colo=west": {
memcache: {
host: "memcache.west.mysite.com"
}
},
}
The section specializations can occur arbitrarily deep.
port: 80,
memcache: {
host: "localhost",
port: 11211,
settings: {
timeout: 1000,
"__context?env=production": {
timeout: 500
}
}
}
The context keys have these properties:
__context?
__context?env=production&colo=east
%xx
)FYI, the __context?...
keys don't need to be quoted in YAML files.
Config(source, options)
constructor throws This creates a new bigfig object, on which you can call read()
multiple
times with different contexts.
source
{Object} the source of the configuration, as describe in Source Object Format aboveoptions
{Object} an optional object containing settings used to adjust how
the source is interprettedThere currently are no defined options.
This constructor will intentionally throw an error on the following conditions:
color: 'red',
"__context?env=production": {
color: 'green',
"__context?env=development": {
color: 'blue',
}
}
Config.read(context, options)
method Creates a config object, customized to the specified context.
context
{Object} a simple object with dimension names and valuesoptions
{Object} an optional object containing settings used to adjust how
the config object is createdThere currently are no defined options.
Config.match(context, options)
method This lower-level method isn't normally called. It returns all sections which match the context.
context
{Object} a simple object with dimension names and valuesoptions
{Object} an optional object containing settings used to adjust how
the sections are matchedThere currently are no defined options.
Config.merge(sections, options)
method This lower-level method isn't normally called. It merges the sections into a configuration object.
sections
{Array} an array of config objects to mergeoptions
{Object} an optional object containing settings used to adjust how
the configs are mergedThere currently are no defined options.
matcher(sectionContext, runContext, options)
The default match algorithm. See Customizing the Match Algorithm below for details on how to replace this with your own algorithm.
sectionContext
{Object} the context generated from the __context?
keys
in the sourcerunContext
{Object} the context passed to read()
or match()
options
{Object} an optional object containing settings used to adjust how
the contexts are matchedrunContext
matches sectionContext
There currently are no defined options.
cloner(oldObject)
This is a low-level utility for cloning an object. You usually don't need to call or overrride this function.
oldObject
{Object} the object to clonemerger(base, changes, options)
The default merge algorithm. See Customizing the Merge Algorithm below for details on how to replace this with your own algorithm.
base
{Object} object whose keys and values will be modified by changes
changes
{Object} object which contains changes to apply to base
options
{Object} an optional object containing settings used to adjust how
the objects are mergedThis merger has the following behavior:
changes
clobbers the value in
base
changes
clobbers the value in base
There currently are no defined options.
Once this libraries has identified which sections to use, it needs to merge
the sections down into a single config. This final config is what is returned
from read()
.
It does this by merging each section onto the root ? later sections in the source are merged over earlier sections. Each section is merged using the merger function, which defaults to the one described above.
If you want to override how merging happens you can replace the one exported by this module. Your customer merger should have the same signature as the default merger.
bigfig = require('bigfig');
hoek = require('hoek');
fig, config;
= new bifgig.Config(...);
ig.merger = function(base, changes, options) {
// use hoek's implementation
hoek.merge(base, changes, true, false);
return base;
ig = fig.read({...});
While the merger should return the config to use, it doesn't need to be a
newly created object. The base
argument can be returned, modified or
unmodified. (This can be a bit tricky ? it's suggested to create a unit test
which has a complex source, and calls read()
multiple times on a single
Config()
object.)
The context passed to read()
is matched to each section in the source. This
is done by calling the matcher, which defaults to the one described above.
The matcher is called with the context in the section, the context passed to
read()
, and should return a boolean indicating whether that section should
be used.
If you want to override how matching happens you can replace the one exported by this module. Your custom matcher should have the same signature as the default merger.
bigfig = require('bigfig');
fig, config;
= new bifgig.Config(...);
ig.matcher = function(sectionContext, runContext, options) {
// this toy matcher only matches sections based on the `env` key
return sectionContext.env === runContext.env;
ig = fig.read({...});
If you want to trim the CPU, memory, and GC overhead of this library, here are some tricks:
Create a Config
object once (perhaps at app startup) and call read()
multiple times. This library is specifically optimized for this usage
pattern.
The read()
overhead depends on how many sections and how deeply nested the
configs are. More deeply nested configs mean more time is spent in merging
(which can also affect GC). Lots of sections means more time spent in
matching, which is a simple algorithm on long-lived objects (little GC
cost).
Deeply nested sections are optimized by the constructor and so don't affect
performance. The following two examples have the same performance during
read()
:
memcache: {
settings: {
timeout: 1000,
"__context?env=production": {
timeout: 500
}
}
}
memcache: {
settings: {
timeout: 1000
}
},
"__context?env=production": {
memcache: {
settings: {
timeout: 500
}
}
}
__context?
){env: 'prod/east'}
matches both __context?env=prod
and __context?env=prod/east
)Bigfig is a direct decendent of ycb, which pioneered many of the ideas and priorities expressed in this library.
MIT License, see LICENSE.txt for details.