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

@proc/features

v0.2.0

Published

A feature flag system for @proc/runtime

Downloads

4

Readme

@proc/context-features easy feature flags.

There are 2 type of flag used for feature flagging.

  • Global Flags: These are on or off for the application. All users/entities see the same value for the flag.
  • User Flags: These are set specifically for each user/entity to allow gradual rollout, beta testing, etc...

This package is concerned with User flags, although they can be used to simulate Global flags, and in some cases that is desireable.

In this package, Flags are configured via the regular env or dotenv. They have the prefix FEATURE_ and by convention they are UPPER_SNAKE_CASED. The value in the config file is a float, between 0 and 1, that indicates how likely an un-enrolled entity will be given the enabled flag. A missing key in the environment is defaulted to a split of 0, so no user is automatically assigned the value.

# 50% of users get my awesome thing
FEATURE_MY_AWESOME_THING=0.5

To check the value of a feature flag for a user we can call get. If the flag has not been assigned to this user, then it is set during the call based on the configured split percentage. Once set (either on or off), it will be returned the same for all subsequent calls.

const feature = createFeature(storage);
if (await feature.get("MY_AWESOME_THING", userId)) {
  // do something awesome
}

This is a simplistic way to assign entities to features. Often you will not want to assign randomly. In this case you will have some code to produce the values. In this case, either omit, or explicitly set the split to 0 so no users are automatically given the flag, and then either at runtime or in an offline task you should update your entities and set the flag explicitly.

i.e. you want all users that were created before 2010 to have the flag.

const feature = createFeatureFlags(storage);
const userIds = await fetchUsersCreatedBefore(2010);
await Promise.all(userIds.map(id => feature.set("MY_AWESOME_THING", id, true)));

or you can wrap the feature check into a seperate function that incorporates the logic and initialise it explicitly.

NB: there are 2 caveats to this approach:

  1. The function is only run on initialisation of the flag, not every request, so do not use it for "psuedo" access-control.
  2. This will take precedence over the environment configured values, which can be confusing so this package will log a warning if you configure both.
const feature = createFeatureFlags(storage);
feature.create(
  "MY_AWESOME_FEATURE",
  async (ctx, userId): Promise<boolean> => {
    // look up something about the user.
    return userId === 123;
  }
);

Also this is how the "random" spread is implemented, so you could define all your features in code rather than using configuration files. In many ways this encourages cleaner practices as you can export the feature flag values and keep them and the logic in a single place.

import { spread } from "@proc/context-features";

const feature = createFeatureFlags(storage);

// same as using the configuration
feature.create("MY_AWESOME_THING", spread(0.5));

Of course your sotrage will probably use ctx (@proc/context) so you will need to initialise your features with context, and attach them with an enhancer.