knobz
knobz lets you declare and manage feature flags in your JavaScript/Node.js application using JSON Predicates to declarative configure whether features should be enabled.
Quick Start
Install the module using npm:
npm install knobz --save
Usage example in Node.js:
const knobz = ; knobz; if knobz // user account should be locked
Phased rollouts
If you're rolling out a new feature, you might want to verify the feature as expected by slowly enabling it for a percentage of your users.
knobz;
The algorithm for determining whether the feature is enabled for a given context is as follows:
% 100 < featurepercentage * 100;
Define feature criteria using JSON Predicate
Features may be enabled only if a given context satisfies its criteria
, defined as a JSON Predicate.
The following example enables the feature to a subset of users based on their job title:
knobz;
API
configure(options)
Configure knobz using any of the following options:
features
: can be either an array of features or a function to load features dynamically. If a function is passed, it must return a Promise that resolves to an array of features.reloadInterval
: interval in ms used by knobz to reload features when configured with a function to load features dynamically.
getFeatures(context)
Return object with all features IDs as properties, and true/false as values, indicating whether they are enabled for a given context.
isFeatureEnabled(featureId, context)
Return true/false if a feature is enabled for a given context.
reload()
Force a reload by calling the configured function to load features dynamically.
Events
knobz is also an event emitter which provides an on
method allowing your application to listen for any of the following events.
knobz.on('check', fn)
Triggers whenever the feature is checked with a given context. The listener is called with featureId
, the context
used, and the boolean enabled
indicating whether the feature is enabled.
enabled: <Boolean> featureId: <String> context: <Object>
knobz.on('reload', fn)
Triggers whenever the features are reloaded. The listener is called with an object including the features array.
features: <Object>
knobz.on('reload:error', fn)
Triggers whenever the function to reload features throws an error. The event handler is called with the error.
Tests
To run the test suite, first install the dependencies, then run npm test
:
npm installnpm test