npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@creditkarma/feature-flags

v1.0.1

Published

A dynamic feature flags library for Node.js

Downloads

113

Readme

Feature Flags

A dynamic feature flags library for Node.js. This library gives you fine-grained control over rolling out and testing new features.

This implementation is largely based on Feature Toggles in Twitter's Finagle framework.

For a detail looks at feature flags check out this article Feature Toggles.

The idea behind feature flags is that they give you a way for testing new code and ramping that code over time. In your configuration you would provide a description of what percentage of requests should use a particular code path and the library will somewhat randomly return a boolean for each toggle representing if it should be used on a particular request or not. Once a code path is ramped to 100% it is expected the feature flag for that code would be removed.

Install

This feature flags library has a peer dependency on @creditkarma/dynamic-config.

$ npm install --save @creditkarma/dynamic-config
$ npm install --save @creditkarma/feature-flags

Usage

Using feature flags is very easy. They are really just a boolean indicating which code path you should use.

import { toggleMap, Toggle } from '@creditkarma/feature-flags'

async function startApp(app) {
    const toggle: Toggle = await toggleMap('com.example.service.UseNewBackend')

    app.get('/test', (req, res) => {
        if (toggle()) {
            // Use new code path
        } else {
            // Use old code path
        }
    })
}

In application code there are only two things you need to use. The toggleMap function and the Toggle type.

toggleMap

The toggleMap is a function that returns a Promise of a toggle for a given key. Your application can have many toggles. Each of these toggles has a unique string identifier that you specify. You provide this id to the toggleMap function and it gives you back a Promise<Toggle>. A Toggle is a function that just returns a boolean. It will return, randomly, true based on the configured percentage.

Configuration

The real work of the feature flag is actually the configuration. In your application config you would have a key call toggles. This key is expected to be at the root level of your config JSON:

{
    "toggles": {
        "com.example.service.UseNewBackend" : {
            "description": "Use new backend code",
            "fraction": 0.1,
            "type": "RAMP",
        }
    }
}

Each Toggle must have an id and a fraction, the number indicating how often to return true. The fraction is a number from 0.0 to 1.0. So in the example the value 0.1 indicates that this Toggle will be true 10% of the time it is called.

Dynamic Config

DynamicConfig is a pluggable config library for Node.js that allows for runtime changes to config values. This is the basis for our feature flags support. It certainly isn't required that you use Dyanmic Config for all of your application config, but that would be recommended.

You should check out the docs for DynamicConifg if you are going to use this library. The gist is this. DynamicConfig has plugable support for remote data stores for configuration values. These external data stores can have their values updated without having to restart the application. The feature flags library will be updated if the remote values change and the ramp of your toggles will be updated in real time.

The only time there is a performance penalty for loading the external config is during the creation of a Toggle. After this initial penalty updated values are loaded in the background.

Flag Schema

The schema for the toggles config:

{
    "\$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "toggles": {
            "type": "object",
            "patternProperties": {
                "[A-Za-z0-9 -_.]+": {
                    "type": "object",
                    "properties": {
                        "fraction": {
                            "type": "number",
                            "minimum": 0.0,
                            "maximum": 1.0,
                        },
                        "type": {
                            "type": "string",
                        },
                        "description": {
                            "type": "string",
                        },
                    },
                    "required": [ "type", "fraction" ],
                },
            }
        }
    },
    "required": [ "toggles" ]
}

Contributing

For more information about contributing new features and bug fixes, see our Contribution Guidelines. External contributors must sign Contributor License Agreement (CLA)

License

This project is licensed under Apache License Version 2.0