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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@canary-flags/core

v1.0.1

Published

A powerful feature flag and configuration management library for JavaScript/TypeScript applications. CanaryCore provides a flexible, event-driven system for managing feature flags, configuration values, and runtime overrides.

Readme

CanaryCore

A powerful feature flag and configuration management library for JavaScript/TypeScript applications. CanaryCore provides a flexible, event-driven system for managing feature flags, configuration values, and runtime overrides.

Features

  • 🚀 Feature Flag Management - Enable/disable features dynamically
  • Runtime Overrides - Override values at runtime without restarts
  • 📡 Event-Driven - Listen to configuration changes in real-time
  • 🔄 Configuration Merging - Merge configurations from multiple sources
  • 🎯 Type Safety - Full TypeScript support with type-safe values
  • 🧪 Testing Support - Easy mocking and testing utilities

Installation

npm install @canary-flags/core
# or
yarn add @canary-flags/core

Basic Usage

Creating a CanaryCore Instance

import CanaryCore from '@canary-flags/core'

// Create an empty instance
const canary = new CanaryCore()

// Create with initial configuration
const canary = new CanaryCore([
  { name: 'new-feature', value: true },
  { name: 'beta-mode', value: false }
])

// Create with overrides
const canary = new CanaryCore(undefined, {
  'new-feature': true,
  'beta-mode': true
})

Setting and Getting Values

// Assign a value to a feature
canary.assign('my-feature', true)

// Get a feature value
const isEnabled = canary.get('my-feature') // true

// Override a value (takes precedence over assigned value)
canary.assign('my-feature', false)
canary.override('my-feature', true)
const value = canary.get('my-feature') // true (override takes precedence)

Configuration Management

// Set a complete feature configuration
canary.config('advanced-feature', {
  name: 'advanced-feature',
  value: 'enabled',
  variants: ['disabled', 'enabled', 'premium'],
  metadata: {
    description: 'Advanced feature with multiple variants'
  }
})

// Get the full feature configuration
const config = canary.getFeature('advanced-feature')
// Returns: { name: 'advanced-feature', value: 'enabled', variants: [...], metadata: {...} }

// Initialize multiple features at once
canary.initializeConfig([
  { name: 'feature-1', value: true },
  { name: 'feature-2', value: 'beta' },
  { name: 'feature-3', value: 42 }
])

Advanced Features

Event System

CanaryCore is built on EventEmitter and provides real-time notifications for configuration changes:

// Listen to specific feature changes
canary.on('assign:my-feature', (value, instance) => {
  console.log(`my-feature assigned to: ${value}`)
})

canary.on('override:my-feature', (value, instance) => {
  console.log(`my-feature overridden to: ${value}`)
})

canary.on('config:my-feature', (config) => {
  console.log('my-feature configuration updated:', config)
})

// Listen to any override
canary.on('override', (key, value, instance) => {
  console.log(`Feature ${key} changed to: ${value}`)
})

// Listen to all changes
canary.on('change', (key, value, instance) => {
  console.log(`Feature ${key} changed to: ${value}`)
})

// Listen to bulk changes
canary.on('change:bulk', (instance) => {
  console.log('Multiple features changed')
})

// Listen to reset events
canary.on('reset', (instance) => {
  console.log('All overrides have been reset')
})

Runtime Overrides

// Override multiple values at once
canary.overrideValues({
  'feature-1': true,
  'feature-2': 'enabled',
  'feature-3': 100
})

// Check if a feature has an override
const hasOverride = canary.hasOverride('my-feature') // boolean

// Reset all overrides
canary.reset()

// Check if a feature has an assignment
const hasAssignment = canary.hasAssignment('my-feature') // boolean

Merging Configurations

// Create a child configuration
const childCore = new CanaryCore([
  { name: 'child-feature', value: true },
  { name: 'shared-feature', value: 'child-value' }
])

// Merge child into parent
canary.merge(childCore)

// Child values take precedence over existing values

Custom Callbacks

// Set custom evaluation callback
canary.onEvaluateFlag = (key, config) => {
  console.log(`Feature ${key} evaluated:`, config?.value)
  // Custom logic here
}

// Set custom toggle callback
canary.toggle = () => {
  console.log('Toggle called')
  // Custom toggle logic
}

// Check if callbacks are set
const hasCallback = canary.hasOnEvaluateFlag // boolean

Data Types

CanaryCore supports various data types for feature values:

// Boolean flags
canary.assign('boolean-feature', true)

// String values
canary.assign('string-feature', 'enabled')

// Numeric values
canary.assign('numeric-feature', 42)

// Object values
canary.assign('object-feature', { 
  mode: 'advanced', 
  settings: { timeout: 5000 } 
})

// Null and undefined
canary.assign('null-feature', null)
canary.assign('undefined-feature', undefined)

Variants

Features can have predefined variants:

// Boolean variants (default)
canary.config('simple-flag', {
  name: 'simple-flag',
  value: true
  // variants: [false, true] (default)
})

// String variants
canary.config('theme-feature', {
  name: 'theme-feature',
  value: 'dark',
  variants: ['light', 'dark', 'auto']
})

// Numeric variants
canary.config('timeout-feature', {
  name: 'timeout-feature',
  value: 5000,
  variants: [1000, 5000, 10000]
})

Getters and State

// Get all current overrides
const overrides = canary.overrides
// Returns: { 'feature-1': true, 'feature-2': 'enabled' }

// Get all feature values (including overrides)
const selections = canary.selections
// Returns: { 'feature-1': true, 'feature-2': 'enabled', 'feature-3': false }

// Get all assignments (without overrides)
const assignments = canary._assignments
// Returns: [true, 'enabled', false]

Error Handling

// Handle non-existent features gracefully
const value = canary.get('non-existent') // undefined

// Check if feature exists before using
if (canary.hasAssignment('my-feature')) {
  const value = canary.get('my-feature')
  // Use value safely
}

Performance Considerations

  • CanaryCore uses Map for O(1) feature lookups
  • Event listeners are automatically cleaned up when calling destroy()
  • Configuration merging is optimized to avoid unnecessary updates
  • Memory usage is minimal for typical feature flag scenarios

API Reference

Constructor

new CanaryCore(configArray?: CanaryStoreEntry[], overrides?: Overrides)

Core Methods

  • assign(key: string, value: CanaryValue): CanaryCore
  • override(key: string, value: CanaryValue): CanaryCore
  • get(key: string): CanaryValue
  • config(key: string, entry: CanaryStoreEntry): CanaryCore
  • getFeature(key: string): CanaryStoreEntry | undefined
  • hasOverride(key: string): boolean
  • hasAssignment(key: string): boolean
  • reset(): CanaryCore
  • merge(childCore: CanaryCore): CanaryCore
  • clear(): void
  • destroy(): void

Getters

  • overrides: Overrides
  • selections: { [key: string]: CanaryValue }
  • hasOnEvaluateFlag: boolean

Setters

  • onEvaluateFlag: (key: string, config?: CanaryStoreEntry) => void
  • toggle: () => void

Events

  • assign:${key} - Emitted when a feature is assigned
  • override:${key} - Emitted when a feature is overridden
  • config:${key} - Emitted when a feature configuration is updated
  • change - Emitted for any feature change
  • change:bulk - Emitted for bulk changes
  • reset - Emitted when all overrides are reset

TypeScript Types

interface CanaryStoreEntry {
  name: string
  value: CanaryValue
  variants?: [true, false] | [false, true] | string[] | number[] | object[]
  override?: CanaryValue
  metadata?: {
    description: string
  }
}

type CanaryValue = boolean | string | number | object | null | undefined

interface Overrides {
  [key: string]: CanaryValue
}

License

MIT