shortstop
Sometimes JSON just isn't enough for configuration needs. Occasionally it would be nice to use arbitrary types as values,
but JSON is necessarily a subset of all available JS types. shortstop
enables the use of protocols and handlers to
enable identification and special handling of json values.
The Basics
{
"secret": "buffer:SGVsbG8sIHdvcmxkIQ==",
"ssl": {
"pfx": "file:foo/bar",
"key": "file:foo/baz.key",
}
}
var fs = require('fs'),
shortstop = require('shortstop');
function buffer(value) {
return new Buffer(value);
}
function file(value) {
return fs.readFileSync(value);
}
var resolver, data;
resolver = shortstop.create();
resolver.use('buffer', buffer);
resolver.use('file', file);
data = resolver.resolve(json);
Multiple handlers
Multiple handlers can be registered for a given protocol. They will be executed in the order registered and the output
of one handler will be the input of the next handler in the chain.
{
"key": "file:foo/baz.key",
"certs": "path:certs/myapp"
}
var fs = require('fs'),
path = require('path'),
shortstop = require('shortstop');
function path(value) {
if (path.resolve(value) === value) {
return value;
}
return path.join(process.cwd(), value;
}
function file(value) {
return fs.readFileSync(value);
}
var resolver, data;
resolver = shortstop.create();
resolver.use('path', path);
resolver.use('file', path);
resolver.use('file', file);
data = resolver.resolve(json);
Removing Handlers
When registered, handlers return an unregister
function you can call when you no longer want a handler in the chain.
{
"key": "path:foo/baz.key"
}
{
"key": "path:foo/bar.key"
}
var fs = require('fs'),
path = require('path'),
shortstop = require('shortstop');
function path(value) {
if (path.resolve(value) === value) {
return value;
}
return path.join(process.cwd(), value;
}
var resolver, unuse, data;
resolver = shortstop.create();
unuse = resolver.use('path', path);
data = resolver.resolve(json1);
unuse();
data = resolver.resolve(json2);