Knobs
Easy feature toggles
Install
npm install knobs
Usage
Define a list of features, each feature supports
- name (required) - string for the feature name
- env - a boolean environment variable for configuring the features. Takes
precedence over the
default
value. - default - a boolean or function
var features = "name": "autorecovery" "env": "FEATURE_AUTORECOVERY" "default": false "name": "dynamic_scaling" "env": "FEATURE_DYNAMIC_SCALING" "default": false "name": "holiday_promo" { return userid % 2; } ;var Knobs = default;var knobs = features; processenv'FEATURE_DYNAMIC_SCALING' = true;knobs; // falseknobs; // trueknobs; // trueknobs; // false
Using Knobs allows for easy A/B testing. You could schedule future releases or slowly roll out by using a computed value:
knobs; knobs; // enabled for approx half of usersknobs; // enabled after a certain date
Or you can manually override things with environment variables:
script.js
knobs;console;
Running without specified flags
$ node scriptFoo Enabled - false
Running with flag on
$ FEATURE_FOO=true node scriptFoo Enabled - true
API
constructor(features)
Create a new Knobs instance with a list of features.
.load
Load in a list of features.
.set(name, val)
Set the value of a feature. val
can be a boolean or a function.
.enable(name)
Alias for .set(name, true)
.disable(name)
Alias for .set(name, false)
.enabled(name, [...args])
Returns whether a feature is enabled. Accepts optional parameters if the feature is defined by a computed value function.
.disabled(name, [...args])
Inverse of .enabled
Events
Knobs is an event emitter and emits on certain methods.
'load' event
When features are loaded, the "load" event is emitted with the list of features.
knobs;knobs;
'change:[name]' event
Emitted when a feature changes via .set
, .enable
or .disable
with the new feature value.
knobs;knobs;
License
MIT