@nexussdk/flags
v0.0.5
Published
Ultra-lightweight feature flags and remote config client SDK for browsers and edge runtimes
Maintainers
Readme
@nexussdk/flags
Ultra-lightweight, production-grade feature flags and remote config client SDK for browsers and edge runtimes with MurmurHash3 bucketing, ABAC rule evaluation, real-time SSE hot-swapping, and zero-infra local dev server (< 8KB gzipped).
Key Features
Core (all versions):
- Deterministic Rollout: Pure TypeScript MurmurHash3 32-bit guarantees deterministic 0–100% bucketing matching server-side evaluation.
- ABAC Rule Engine: Full attribute-based access control — string, number, array operators (
IN,CONTAINS,STARTS_WITH), comparison, and semantic versioning rules. - Real-Time Streaming (SSE):
SSEManagerwith singleton connection pooling, exponential backoff reconnects, and instantFLAG_UPDATE/FLAG_DELETElive sync. - Offline-First & Bootstrap: Instant 0ms boot via
bootstrapoptions (SSR pre-evaluation, local JSON, or boolean dictionary) with background revalidation. - Cross-Tab Synchronization: In-memory cache backed by
localStorageandBroadcastChannel. - Built-in Local Dev Server:
nexus-flags-devCLI with single-file web dashboard and live SSE broadcasting.
v0.0.5 — New Features:
- 🧹 Stale Flag Detector:
StaleFlagDetectorscans your active flags and emits[NEXUS_STALE_FLAG]warnings for any flag that has been at 100% rollout longer than your configured threshold (default: 30 days). Eliminates the "flag graveyard" problem.
Installation
pnpm add @nexussdk/flags
# or
npm install @nexussdk/flags(If using React or Vue, install @nexussdk/sdk instead for built-in hooks like useFeatureFlag and useVariant).
Quickstart (Vanilla JS / TypeScript)
import { NexusFlagsClient } from '@nexussdk/flags';
// 1. Initialize client
const flags = new NexusFlagsClient({
apiKey: 'pk_live_your_api_key',
baseUrl: 'https://api.nexus.dev',
user: {
id: 'usr_4829',
country: 'VN',
plan: 'enterprise',
appVersion: '2.4.0',
},
realtime: true, // Enable SSE live streaming
});
// 2. Synchronous evaluation (0ms from cache / bootstrap)
if (flags.isEnabled('new_checkout_flow', false)) {
mountNewCheckout();
} else {
mountLegacyCheckout();
}
// 3. Dynamic variant configurations
const discountRate = flags.getVariant<number>('promo_banner_v2', 'discount_rate', 0);
const layoutTheme = flags.getVariant<string>('checkout_v2', 'theme', 'standard');
// 4. Listen for realtime hot-swap updates
const unsubscribe = flags.onFlagChange('new_checkout_flow', (result) => {
console.log('Flag updated in realtime:', result.enabled, result.reason);
if (result.enabled) {
mountNewCheckout();
}
});
// 5. Update user identity (e.g. after login)
await flags.identify({
id: 'usr_9981',
country: 'SG',
plan: 'pro',
});
// 6. Reset user on logout
flags.reset();
// 7. Cleanup on unmount
unsubscribe();
flags.destroy();Configuration Options (NexusFlagsOptions)
| Option | Type | Default | Description |
| :--- | :--- | :--- | :--- |
| apiKey | string | Env | Public API key (pk_live_... or nxs_dev_...) |
| baseUrl | string | https://api.nexus.dev | Ingestion endpoint URL |
| user | UserContext | Anonymous UUID | Initial user identity for ABAC rules and rollout bucketing |
| bootstrap | Record<string, ...> | undefined | Pre-hydrated flags for 0ms SSR boot or offline JSON mode |
| realtime | boolean | true | Enables Server-Sent Events (SSE) stream subscription |
| timeoutMs | number | 3000 | Network timeout for background evaluation refresh |
Offline & Bootstrap Mode (Local JSON)
For small projects or offline development, you can completely bypass backend servers by bootstrapping flags from a local JSON file:
import localFlags from './nexus-flags.json';
const flags = new NexusFlagsClient({
bootstrap: localFlags,
realtime: false, // Optional: disable network calls completely
});
// Ready immediately without any network calls
const isDarkMode = flags.isEnabled('dark_mode', false);The bootstrap parameter accepts:
- Simple boolean flags:
{ dark_mode: true, beta_ui: false } - Standard
nexus-flags.jsonobjects created by the dev server - Full
FlagEvaluationResultmaps generated from SSR
Local Dev Server (nexus-flags-dev)
The @nexussdk/flags package includes a standalone local flag management CLI with an embedded Web GUI dashboard:
# Start local flag management server
npx nexus-flags-dev
# or in this monorepo
pnpm --filter @nexussdk/flags flags-serverCLI Options
nexus-flags-dev [options]
Options:
-p, --port <number> Port to listen on (default: 4568)
--host <string> Host interface to bind (default: localhost)
-f, --file <path> Path to JSON flags file (default: ./nexus-flags.json)
-h, --help Show help messageEndpoints
| Method | Endpoint | Description |
| :--- | :--- | :--- |
| GET | / or /gui | Interactive Web GUI Dashboard |
| GET | /health | Health status and connected clients count |
| GET | /api/v1/flags | List all flags (JSON) |
| POST | /api/v1/flags | Create a new flag |
| GET | /api/v1/flags/:key | Retrieve a single flag definition |
| PATCH | /api/v1/flags/:key | Update flag properties, rollout %, or variants |
| POST | /api/v1/flags/:key/toggle | Toggle enabled switch & broadcast to clients |
| DELETE | /api/v1/flags/:key | Delete flag & broadcast deletion |
| GET | /api/v1/flags/stream | Real-time SSE stream for SDK live hot-swap |
| GET | /api/v1/flags/eval | Evaluation snapshot for SDK background refresh |
| GET | /api/v1/flags/bootstrap | Pre-load snapshot for SSR bootstrap |
Connecting SDK to Local Dev Server
const client = new NexusFlagsClient({
apiKey: 'nxs_dev_local',
baseUrl: 'http://localhost:4568',
realtime: true, // Whenever you toggle a flag in the GUI, your app updates instantly!
});Programmatic Dev Server Export
You can also embed the flags dev server programmatically in your test harness or Vite dev server:
import { startFlagsDevServer } from '@nexussdk/flags/dev-server';
const devServer = await startFlagsDevServer({
port: 4568,
flagsFile: './test-flags.json',
seed: true,
});
// Later in teardown:
await devServer.close();Framework Compatibility Matrix
@nexussdk/flags is completely framework-agnostic with zero runtime dependencies. It supports modern and legacy frontends:
| Framework | Supported Versions | Paradigm | Documentation |
| :--- | :--- | :--- | :--- |
| React / Next.js | React 16.8 – 19 / Next.js 13 – 16 | Server Components, Hooks, Context | React Integration Guide |
| Vue / Nuxt | Vue 2.7 & 3.x / Nuxt 3 & 4 | Composition API, <script setup>, SSR | Vue Integration Guide |
| Angular | Angular 14 – 19+ / AngularJS | Signals (@if), Standalone, RxJS | Angular Integration Guide |
| Svelte / SvelteKit | Svelte 3 – 5 / SvelteKit 1 & 2 | Runes ($state), Stores (writable) | Svelte Integration Guide |
| Vanilla JS / Node.js | Any ES2022+ runtime (Solid, Qwik, Lit) | Direct Class instance | Vanilla JS Guide |
License
MIT © Nexus Platform
