Ship code dark, roll out by percentage/cohort, roll back instantly. A feature-flag service (Express + PostgreSQL + Redis), one TypeScript SDK with entry points for servers, browsers and React, and an admin dashboard (Next.js), managed as an npm-workspaces monorepo.
Full requirements spec: ../docs/SRS.md — it sits in the parent folder, alongside this one, so the link resolves in a local checkout rather than on GitHub alone. Requirements coverage maps every requirement in it to the test that proves it.
┌─ writes ───────────────────────────┐ ┌─ reads ─────────────────────────┐
│ │ │ │
│ Admin dashboard (Next.js) │ │ Host app │
│ Server Components + Server │ │ + flagpilot │
│ Actions. The JWT lives in an │ │ isEnabled() is synchronous, │
│ httpOnly cookie; the browser │ │ in-process, and never throws │
│ never holds it. │ │ │
│ │ admin JWT │ │ ▲ │
│ │ │ │ GET /api/sdk/ │
│ Error tracker (System B — │ │ │ flags │
│ not built yet) │ │ │ x-api-key │
│ │ x-integration-key │ │ │ │
│ │ (disable only) │ │ │ every │
└──────────────┼────────────────────-┘ └──────────────┼──────────────────┘
│ │ refreshIntervalMs
▼ │
┌──────────────────────────────────────────────────┴─────────┐
│ Flag Service API — Express 5 │
│ │
│ read L1 in-process Map ─▶ L2 Redis ─▶ L3 PostgreSQL │
│ ▲ circuit │
│ breaker │
│ write ONE transaction: flag row + audit row │
│ → commit → clear L1 + L2 → PUBLISH the key │
└────────┬──────────────────┬───────────────────┬─────────────┘
▼ ▼ ▼
┌──────────────┐ ┌──────────────────┐ ┌────────────────────┐
│ PostgreSQL │ │ Redis flag cache │ │ Redis Pub/Sub │
│ flags, │ │ shared by every │ │ flags:invalidate — │
│ audit_log, │ │ instance, 30 s │ │ every other API │
│ users │ │ TTL │ │ instance drops its │
│ — the truth │ └──────────────────┘ │ L1 within 2 s │
└──────────────┘ └────────────────────┘
The SDK never touches Redis. It downloads whole flag configs from /api/sdk/flags on a
timer and evaluates them in-process with @feature-flags/core, so a check costs no network
at all and a consumer needs no cache credentials. GET /api/flags/:key/evaluate exists as a
server-side path for non-JavaScript clients and the k6 load test; it imports the same
evaluator, which is why the two can never disagree about a user.
Browsers get answers, never rules. The SDK key can read every rule and allowlist, so it must
never reach a browser. flagpilot/browser and flagpilot/react instead call
POST /api/client/flags with a separate, public client key, and receive only
{ "flag-key": true | false } — evaluated on the server, for one user, and only for flags an admin
has marked Available to browser SDKs. A signed-in user's identity arrives as a token signed by the
host's server, so a visitor cannot claim to be someone else. See
Client-side flags.
- Node.js ≥ 20 and npm ≥ 10
- Docker (with Compose v2) — used both for the local stack and for integration tests
From a clean clone, the back end needs exactly this:
npm install
npm run compose:up # postgres + redis → migrate → seed → api
curl -i http://localhost:4000/healthYou can log in immediately as admin@example.com / change-me-please — override with
ADMIN_EMAIL and ADMIN_PASSWORD before compose:up.
npm run compose:up is docker compose up --build -d. Two one-shot services run to
completion before api starts:
migrateapplies pending migrations. Re-running is safe; applied migrations are tracked indrizzle.__drizzle_migrations.seedcreates the single admin. It upserts, so everyupre-syncs the password toADMIN_PASSWORD— which makes a forgotten password recoverable without SQL, but also means a password changed elsewhere does not survive the nextup.
The dashboard is not in compose — it is a separate Next.js app, two commands:
cp packages/dashboard/.env.example packages/dashboard/.env.local
npm run dev:dashboard # http://localhost:3000It is kept out deliberately: it is deployed to Vercel rather than self-hosted, and a container on port 3000 would collide with the server Playwright starts for the E2E suite.
packages/api/.env is only needed to run the API outside Docker, or to run npm run db:seed
from the host — compose supplies its own environment. See Environment variables.
Compose does not rebuild on its own. A plain docker compose up -d reuses the existing
image, so the container keeps serving the previous build and your change appears to have had
no effect. After editing anything in packages/api or packages/core:
npm run compose:up # rebuilds, then restartsTo confirm which build is actually running, grep the compiled output inside the container:
docker compose exec -T api grep -c "some string from your change" dist/<path>.jsA 0 means the image is stale. If a rebuild does not clear it, force the container onto the
new image:
docker compose up -d --force-recreate apiThis is worth doing before any load or chaos run. During M7, npm run compose:up rebuilt the
image but left the container on the previous one, and the blackout test then reported the
unfixed numbers against what looked like a fixed build — a chaos test cannot tell you it is
measuring the wrong artifact.
(For iterating quickly, prefer npm run dev below: tsx watch picks up source changes
immediately. Use the container to verify the real artifact.)
cp packages/api/.env.example packages/api/.env # points at the compose-exposed ports on localhost
docker compose up -d postgres redis # dependencies only — no api image to rebuild
npm run db:migrate
npm run db:seed
npm run dev # tsx watch, http://localhost:4000This is the loop to use while writing code: tsx watch reloads on every save, so there is
no rebuild step. docker compose up -d postgres redis is safe without --build because
those are stock images, not built from this repo.
The .env file must live in packages/api/ (not the repo root): dotenv reads from the
working directory, and npm runs workspace scripts from inside the package. Both
npm run dev and npm run db:migrate load it; alternatively set the variables in
your shell, e.g. DATABASE_URL=postgres://... npm run db:migrate.
Postgres is published on host port 5433 (not 5432) so it can coexist with a
locally installed Postgres. Inside the compose network the api still uses
postgres:5432. Connect DB tools to localhost:5433, user/password/db flags.
There are two, because "is the process alive" and "can it serve traffic" are different questions and conflating them is expensive on free infrastructure.
GET /health/live — always 200, {"status":"live"}, queries nothing. This is what a platform
health check and a keep-alive ping should poll. Render polls every ~30 s and that cannot be turned
off; pointing it at /health would run a Postgres query and a Redis PING forever, which keeps a
scale-to-zero database (Neon's free tier) permanently awake until its monthly compute allowance is
gone — quite possibly mid-demo.
GET /health checks every dependency concurrently, each bounded by HEALTH_CHECK_TIMEOUT_MS
(2 s by default):
- 200
{ "status": "ok", ... }— Postgres and Redis both reachable - 503
{ "status": "degraded", ... }— at least one dependency unreachable or timed out
{
"status": "degraded",
"checks": {
"postgres": { "status": "ok", "latencyMs": 2 },
"redis": { "status": "error", "latencyMs": 1, "error": "Stream isn't writeable ..." }
}
}The API process starts and stays up even when a dependency is down (NFR-04); the
health payload is how you find out which one. /health/live keeps answering 200 throughout —
that is the property a platform health check needs, so a cold database reads as "slow dependency"
rather than "dead service".
Two credentials, deliberately separate so a client app can never change a flag:
| Credential | Header | Grants |
|---|---|---|
Admin JWT (from POST /api/auth/login) |
Authorization: Bearer <token> |
Everything, including all writes |
SDK_API_KEY |
x-api-key: <key> |
Read-only SDK endpoints, nothing else |
INTEGRATION_API_KEY |
x-integration-key: <key> |
Disabling a flag via the alert endpoint, nothing else |
CLIENT_API_KEY — public by design |
x-flagpilot-client-key: <key> |
The on/off values of browser-exposed flags, from allowed origins only |
The SDK key and the integration key are secrets, so the API refuses either one on any request that
carries an Origin header — which means a browser sent it (403 secret_key_from_browser, logged
with the origin, never the key). Server-side callers send no Origin, so they are unaffected.
Admin-JWT requests are untouched: the dashboard calls the API server-to-server anyway.
| Method | Endpoint | Auth | Purpose |
|---|---|---|---|
POST |
/api/auth/login |
— | { email, password } → { token, expiresAt, user } |
POST |
/api/flags |
admin | Create a flag → 201 |
GET |
/api/flags |
admin | List live flags |
GET |
/api/flags/:key |
admin | One flag |
PATCH |
/api/flags/:key |
admin | Partial update (the key itself is immutable) |
DELETE |
/api/flags/:key |
admin | Soft delete → 204 |
GET |
/api/sdk/flags |
SDK key or admin | Flag configs for the SDK to evaluate locally |
GET |
/api/flags/:key/evaluate |
SDK key or admin | Server-side evaluation for non-JS clients |
GET |
/api/flags/:key/audit |
admin | Change history, newest first, with the actor resolved to an email |
POST |
/api/integrations/alert |
x-integration-key |
Disables a flag in response to an external alert |
POST |
/api/client/flags |
client key, from an allowed origin | { flags: { key: boolean } } for browser-exposed flags, for one user. Only exists when CLIENT_API_KEY is set — see Client-side flags |
GET |
/api/settings/sdk |
admin | What the dashboard's SDK setup page shows: the public client key, allowed origins, signing mode. Never a secret |
Errors always use one envelope: { "error": { "code", "message", "details"? } } —
400 validation_error / invalid_json, 401 unauthorized, 404 flag_not_found,
409 flag_key_exists, 413 payload_too_large, 415 unsupported_media_type. The browser endpoint
adds 401 invalid_client_key, 403 origin_not_allowed / signed_context_required /
invalid_signed_context, 400 invalid_context / ambiguous_context, and
503 flags_unavailable.
TOKEN=$(curl -s -X POST localhost:4000/api/auth/login -H 'content-type: application/json' \
-d '{"email":"admin@example.com","password":"..."}' | jq -r .token)
curl -X POST localhost:4000/api/flags -H "Authorization: Bearer $TOKEN" \
-H 'content-type: application/json' \
-d '{"key":"new-checkout-flow","name":"New checkout","rolloutPercentage":25,"enabled":true}'
curl localhost:4000/api/sdk/flags -H "x-api-key: $SDK_API_KEY"curl "localhost:4000/api/flags/new-checkout-flow/evaluate?userId=user_1&attr.plan=pro" -H "x-api-key: $SDK_API_KEY"
# {"key":"new-checkout-flow","userId":"user_1","enabled":true,"reason":"rollout_in","bucket":12}userId is optional (absent means anonymous) and any attr.<name> parameter becomes a
targeting attribute. reason is one of kill_switch, anonymous, rule_match,
rollout_in, rollout_out, or unavailable.
This endpoint exists for non-JavaScript clients and for the k6 load test. The JS SDK does
not use it — it fetches whole configs from /api/sdk/flags and runs the same
@feature-flags/core evaluator in-process, so its checks cost no network at all. Both paths
import the same module, which is why they can never disagree about a user.
Every evaluation emits one structured line for a future analytics consumer:
{"event":"flag_evaluation","ts":"…","flagKey":"…","userId":"…","enabled":true,"reason":"rollout_in","bucket":12}A Next.js App Router app in packages/dashboard: log in, see every flag, change a rollout,
flip a kill switch, and read who changed what.
cp packages/dashboard/.env.example packages/dashboard/.env.local
npm run dev:dashboard # http://localhost:3000The JWT never reaches the browser. The login form posts to the dashboard's own Route
Handler, which calls the Express API server-side and stores the token in an httpOnly
cookie. Server Components read that cookie and forward it as an Authorization header — so
the browser never holds the token, and no CORS is involved. Every API call sets
cache: 'no-store', because flag state must never look stale to an admin.
proxy.ts (Next 16's replacement for middleware.ts) redirects anyone without a session
before a protected page renders, so there is no flash of protected content. An expired
session is a separate case: the token passes the cookie check and the API returns 401, which
lib/api.ts turns into a redirect through /api/logout, clearing the cookie and explaining
why on the login page.
Each flag has an Available to browser SDKs switch (off for every new flag). Turning it on lets
browsers read that flag's on/off value; rules and allowlists never leave the server. The list marks
such flags with a Browser badge, and the history records the change as Browser SDKs:
server only → exposed. The SDK setup page (/settings/sdk, linked from the flag list) shows
whether the browser endpoint is on, the public client key, the allowed origins and the signing
mode, with copy-paste setup for Express, React and Next.js. It never shows the SDK key — the API
does not return it, even to an admin.
Mutations are Server Actions that call the API and then revalidatePath, so a change appears
in the list without a manual refresh. They return failures rather than throwing: a
duplicate key becomes an inline message on the key field with the typed input preserved, and
an unreachable API becomes an error panel rather than a blank page.
Playwright is the dashboard's only test suite — the flows span a Route Handler, a Server Component fetch and a Server Action, so testing those pieces in isolation would prove little.
npm run compose:up # the API must be running
npx playwright install chromium # one time, ~100 MB
npm run test:e2eIt stays out of npm test deliberately, so the default suite needs no dashboard and no
browser. Playwright builds and serves a production bundle rather than using next dev: the
dev server compiles routes on demand, which can stall a first navigation past an assertion
timeout. Set E2E_DEV_SERVER=1 to run against the dev server while iterating on the UI.
Private browsing works. Incognito does not block cookies — it gives the window a fresh,
isolated cookie jar that is discarded on close, which is exactly what a session cookie needs.
A Playwright browser context is an incognito profile, so every one of these specs already
runs in one, and e2e/private-browsing.spec.ts asserts it explicitly. Only a browser
configured to block all cookies outright would fail, and that breaks every cookie-based login
on the web.
flagpilot is the package a host application's server installs. It does not call the API
per check: it downloads whole flag configs from /api/sdk/flags on a timer and evaluates each
user in-process with @feature-flags/core — the same module the API runs.
import { init, isEnabled } from 'flagpilot';
await init({ apiUrl: process.env.FLAGS_API_URL, apiKey: process.env.FLAGS_API_KEY });
if (isEnabled('new-checkout-flow', { userId: user.id })) { /* ... */ }isEnabledis synchronous and touches only memory — roughly a microsecond per call.- Load on the API is proportional to SDK instances, not users.
- Nothing throws — not
isEnabled, and since 0.2.0 notiniteither. A wrong key, a bad URL or a flag service that is down resolvesinit()to{ ok: false, status }, and every check serves the last known configs or its default, so a flag-service problem can never stop the host app from booting. A rejected key is retried every 5 minutes, so fixing it needs no restart. - Server-side only: the SDK key must never reach a browser. If browser code imports
flagpilotanyway, bundlers resolve it to a stub with no network code and no key handling, which logs one error and returns defaults. - For the browser SDKs it also provides
getClientFlags(context)— the browser flags' values, for a correct server-rendered first paint — andsignContext(context), a signed identity token.
See packages/sdk/README.md for the full API, and examples/victim-app for a runnable demo:
FLAGS_API_URL=http://localhost:4000 FLAGS_API_KEY=dev-only-sdk-api-key-change-me \
FLAG_KEY=demo-flag REFRESH_MS=5000 node examples/victim-app/index.jsChange the flag's rollout through the API and the printed count follows within
REFRESH_MS, with no restart.
The same package has two more entry points, for code that runs in a browser. One install:
npm install flagpilot.
| Import | For |
|---|---|
flagpilot |
Servers — Node, Express, Next.js server components and route handlers |
flagpilot/browser |
Browser code in any framework, or none |
flagpilot/react |
React 18 and 19 — Vite, Create React App, Next.js client components |
| Risk | What prevents it |
|---|---|
| A browser reads targeting rules, percentages or allowlists | They never leave the server. The browser endpoint evaluates there and returns { key: boolean } only — no reason, bucket or rule |
| A browser learns that a server-only flag exists | Per-flag opt-in, off by default. Flags that are not exposed are absent from the response, not false |
| A visitor claims to be another user, to see their flags or probe an allowlist | With CLIENT_REQUIRE_SIGNED_CONTEXT=true (the default), a user id or attributes are accepted only inside an HMAC-SHA256 token from the host server's signContext(). The signature is checked in constant time, before the payload is even parsed |
| Another website uses your client key | Exact-origin allowlist (CLIENT_ALLOWED_ORIGINS). A disallowed origin gets 403 and no CORS headers, so its browser cannot read the answer. Wildcard subdomains and the null origin are refused at boot |
| The SDK key ends up in a browser | flagpilot resolves to a key-less stub in browser bundles; the API refuses a secret key sent with an Origin header; a secret sent as the client key is rejected and logged; the API will not start if the client key equals a secret |
| Oversized or hostile input | 8 KB body limit, strict schemas, bounded contexts, __proto__ / constructor / prototype refused as attribute names, own-property lookups in the evaluator |
| A user's answer cached or leaked | Cache-Control: no-store, Vary: Origin, nosniff. The context travels in a POST body, never a URL; cookies are never sent (credentials: 'omit'); no Referer; redirects refused |
Why the signing key comes from the SDK key: the host server already holds it, so signContext
needs no extra setup — and anyone with the SDK key can already read every rule and evaluate any user,
so being able to sign adds nothing. Tokens do not expire, deliberately: a token only ever reveals that
user's own browser flags, which their browser already shows, and a stable token means a server
re-render does not trigger a refetch. Rotating the SDK key invalidates every token.
- Turn the endpoint on. On the API, set
CLIENT_API_KEY(a new random value) andCLIENT_ALLOWED_ORIGINS(your sites' exact origins). Without them/api/client/flagsdoes not exist. The local compose stack sets both. - Expose a flag. In the dashboard, switch on Available to browser SDKs.
- Use it. The dashboard's SDK setup page has these snippets with your values filled in.
React (Vite, Create React App):
import { FlagProvider, useFlag } from 'flagpilot/react';
<FlagProvider apiUrl="https://flags.example.com" clientKey="…" signedContext={session?.flagToken}>
<App />
</FlagProvider>;
function Navbar() {
return <h1>{useFlag('new-navbar-title') ? 'New title' : 'Old title'}</h1>;
}Your backend adds the token wherever it returns the signed-in user:
flagToken: await signContext({ userId: user.id }). Leave it out for visitors who are not signed in.
examples/vite-react is the complete, tested version, on React 18.
Next.js (App Router), where the server renders the right values on the first paint:
// app/layout.tsx — a server component
const { initialFlags, signedContext } = await flagBootstrap(user); // getClientFlags() + signContext()
<FlagProvider apiUrl={…} clientKey={…} initialFlags={initialFlags} signedContext={signedContext}>
{children}
</FlagProvider>;Client components call useFlag; server components call isEnabled directly.
examples/next-app is the complete, tested version.
Anything else:
import { createBrowserClient } from 'flagpilot/browser';
const flags = createBrowserClient({ apiUrl, clientKey, signedContext });
flags.subscribe(render); // called whenever a value changes
flags.isEnabled('new-navbar-title'); // synchronous, from memoryProxy pattern. apiUrl may be a same-origin path such as /flagpilot that your own server
forwards to the flag service. Then there is no CORS at all, the request is first-party, and ad
blockers that block third-party requests do not see it.
Content Security Policy. A page with a CSP needs the flag service in connect-src
(connect-src 'self' https://flags.example.com). Without it the browser blocks the request; the SDK
serves defaults and logs once, naming CSP as a likely cause.
None of the three entry points can break the page or the server that uses it:
- No public function throws or rejects. A bad config, a wrong key, a disallowed origin or a dead flag service all mean the last known values or defaults, and at most one console message.
- Nothing runs at import — no
window,documentor storage access — so server rendering is safe. - Bounded work. One request at a time, a timeout on each, backoff on failure, no polling while the tab is hidden, and a refresh when the tab becomes visible again or the network returns, if the values are due. Responses over 256 KB or 2,000 flags are refused.
- A user switch never shows the previous user's values — not even while the new request is in flight — and a late answer for the old user is discarded.
- Hydration-safe. React first renders exactly what the server rendered, then applies newer values.
- Host bugs stay contained. A throwing subscriber does not stop the others, and a polluted
Object.prototypecannot invent a flag.
The browser SDKs need no cookies and no storage, and create no identifiers, so they behave
the same in a normal window, a private/incognito window, or with storage disabled entirely. Storage is
used only with persist: true (the Vite example turns it on): the last values are then kept in
localStorage, tagged with a hash of the user's identity so they are never shown to a different user,
and ignored after 7 days. If storage is blocked or full it is simply not used; in a private window the
cache ends with the session.
| Request body | 8 KB |
userId |
256 characters |
| Attributes | 32; names 1–64 characters; values a string of ≤ 256 characters, a finite number, or a boolean |
| Signed token | 4,096 characters |
| Browser refresh | every 60 s by default (15 s to 1 h; 0 stops after the first success), plus on focus and reconnect when due |
Every suite below must pass for the project to be considered complete.
| Suite | Command | Needs | What it covers |
|---|---|---|---|
| Vitest unit | npm run test:unit |
nothing | Bucketing and rule evaluation, JWT, scrypt, config parsing, the credential guard, the circuit breaker, origin parsing and context signing; the server SDK against a stub server; the browser client (happy-dom) and the React bindings, including SSR and hydration; checks on the built package |
| Vitest integration | npm test |
Docker | The same, plus every route against a real Postgres and Redis via Testcontainers — cache tiers, invalidation, audit, durability, degradation, and the browser endpoint's CORS, keys and signed identities |
| Playwright dashboard | npm run test:e2e |
API running, npx playwright install chromium |
The dashboard's flows end to end, one real SDK consumer observing a UI change, and the browser-SDK switch reaching the browser endpoint |
| Playwright browser SDK | npm run test:sdk-browser |
API running, npx playwright install chromium firefox webkit |
flagpilot/browser in Chromium, Firefox and WebKit with real CORS: disallowed origins, CSP blocks, sandboxed iframes, blocked storage, offline, flag service down — zero page errors in every one |
| Playwright examples | npm run test:examples |
API running | The Next.js and Vite + React 18 examples as production builds: first paint, hydration, signed identities, live updates, outages, bundle contents |
| React 18 | npm run test:react18 -w packages/sdk |
network | The React tests against React 18, installed from the packed tarball |
| Tarball | npm run test:tarball -w packages/sdk |
network | The packed package installed outside the repo: Node ESM + CJS without React, a Vite + React 18 build, a Next.js build |
| k6 load + chaos | npm run load:test, load:chaos, load:blackout, load:client |
stack running | NFR-FD-01 latency, NFR-FD-03 throughput, NFR-FD-04 degradation under load; the browser endpoint at the same bar |
A healthy run:
npm run test:unit → api 159, core 117, sdk 194 (470 tests, 29 files)
npm test → api 313, core 117, sdk 194 (624 tests, 40 files)
npm run test:e2e → 30 passed
npm run test:sdk-browser → 33 passed (11 scenarios × 3 browsers)
npm run test:examples → 6 passed (next-app), 6 passed (vite-react)
npm run test:react18 -w packages/sdk → 18 passed on React 18.3.1
npm run test:tarball -w packages/sdk → 14 checks ok
npm run load:test → exit 0, every threshold green (and load:chaos, load:blackout, load:client)
npm test is the default gate and needs Docker, because the integration specs start real
containers — a mocked Postgres would not catch the ambiguous-column and transaction bugs these
have actually caught. Playwright stays out of npm test deliberately, so the default suite needs
neither a dashboard nor a browser; see End-to-end tests. The k6 gates and
their measured results are in Performance.
npm run test:e2e builds core and sdk first, because the consumer spec imports the SDK's
compiled output — a stale dist would otherwise fail in a confusing way.
Every requirement in ../docs/SRS.md §3 and §5, what implements it, and the test that proves it. This is the table that makes "complete" checkable instead of asserted.
On the two numbering schemes: the SRS uses FR-FD-nn / NFR-FD-nn; the milestone notes in
../docs/ use the shorter FR-nn / NFR-nn from the build plan. They are the same requirements
— FR-FD-07 ≡ FR-07 — with one exception: NFR-FD-05 appears only in the SRS, so no
milestone claimed it until M8 traced the spec and gave it a test.
| SRS | Requirement | Implemented in | Proved by |
|---|---|---|---|
| FR-FD-01 | Admin creates a flag with a unique key | services/flags/flag-service.ts createFlag, partial unique index on live keys |
flags.integration.test.ts (duplicate → 409), durability.integration.test.ts ("commits exactly one flag and one audit row under two concurrent creates") |
| FR-FD-02 | A flag can be toggled fully on or off | enabled column; kill_switch branch in core/evaluate.ts |
core/evaluate.test.ts ("a disabled flag is false even at 100% with a matching allowlist"), evaluate.integration.test.ts ("honours the kill switch"), e2e/flags.spec.ts ("the list switch is a kill switch that persists") |
| FR-FD-03 | Percentage rollout, 0–100 | core/evaluate.ts; flags_rollout_percentage_range check constraint |
core/evaluate.test.ts ("0% is false for everybody", "100% is true for everybody", "exclusive at the boundary"), validation.test.ts |
| FR-FD-04 | Targeting rules on user attributes, including an allowlist | core/evaluate.ts — rule matching, with case- and whitespace-insensitive attribute comparison |
core/evaluate.test.ts ("a matching rule wins over a 0% rollout"), core/normalize.test.ts, evaluate.integration.test.ts ("applies an allowlist rule regardless of bucket", "reads attr.* parameters"), e2e/flags.spec.ts ("an allowlist rule round-trips through the builder") |
| FR-FD-05 | Deterministic bucketing, hash(userId + flagKey) % 100 |
core/hash.ts — FNV-1a plus a MurmurHash3 finalizer; frozen |
core/bucket.test.ts ("is deterministic across 1,000 calls"), core/golden.test.ts, sdk ("uses the same hash, so a bucket is the same in both places") |
| FR-FD-06 | Audit entry per change: actor, timestamp, old and new value | writeAudit, inside each mutation's transaction |
audit.integration.test.ts ("carries the before and after values of each change"), durability.integration.test.ts ("commits the audit row in the same transaction as the flag itself") |
| FR-FD-07 | SDK exposes isEnabled(flagKey, userContext): boolean |
packages/sdk/src/client.ts |
sdk ("is synchronous — it returns a boolean, not a promise", "returns the default before init() instead of throwing") |
| FR-FD-08 | Resolved from cache, with no network round trip per check | SDK in-memory snapshot; API's L1 → L2 → L3 tiers | sdk ("makes zero HTTP requests across 10,000 checks"), cache.integration.test.ts ("serves the next read from the in-process tier, without touching Postgres") |
| FR-FD-09 | Changes reach every SDK instance inside the NFR-FD-02 window | SDK refresh timer; Redis Pub/Sub flags:invalidate |
sdk ("picks up a change made on the server (FR-09)"), e2e/consumer.spec.ts — the full chain, UI → API → a real SDK client |
| FR-FD-10 | Authentication required before a flag can be seen or changed | middleware/auth.ts requireAdmin; dashboard proxy.ts |
auth-middleware.test.ts, flags.integration.test.ts, e2e/auth.spec.ts ("redirects an unauthenticated visitor to login, with no flash of the flag list") |
| FR-FD-11 | POST /api/integrations/alert disables a flag on an external signal |
integrations/alert.ts |
integrations.integration.test.ts, e2e/integration-alert.spec.ts |
| FR-FD-12 | Integration-created audit entries are tagged system-initiated | SYSTEM_INTEGRATION_ACTOR in db/schema.ts |
integrations.integration.test.ts, audit.integration.test.ts ("labels a system-initiated change rather than showing a raw sentinel") |
| NFR-FD-01 | Evaluation latency from cache — P99 < 5 ms | Three-tier cache; Server-Timing on the route |
k6/evaluate.js threshold server_ms: p(99)<5. Observed 0.26–0.41 ms — see Performance |
| NFR-FD-02 | Change propagation — ≤ 2 s | flags:invalidate Pub/Sub, with a TTL as the safety net |
cache.integration.test.ts ("drops a second instance’s in-process copy within 2s of a mutation", "refreshes a stale local copy even when the invalidation never arrives") |
| NFR-FD-03 | Throughput, single instance — ≥ 500 req/s | — | k6/evaluate.js threshold on http_reqs. Observed 499.6 req/s over 30,005 requests |
| NFR-FD-04 | Configurable fail-open / fail-closed; never an unhandled crash | FLAG_FALLBACK_POLICY, CacheUnavailableError, the circuit breaker |
cache-fallback.test.ts, cache-chaos.integration.test.ts ("is still alive and still answering /health"), k6/chaos.js (0 wrong answers), k6/blackout.js (0 policy violations at 500 req/s) |
| NFR-FD-05 | Durability — persisted to Postgres before a change is considered committed | One transaction per mutation (flag + audit); cache invalidation strictly after commit | durability.integration.test.ts — reads back through a separate pool that bypasses every cache tier, and compares xmin to prove one transaction wrote both rows |
| FR-INT-03 | System A disables the referenced flag on a qualifying alert | integrations/alert.ts → updateFlag |
integrations.integration.test.ts, e2e/integration-alert.spec.ts |
| FR-INT-05 | Every auto-disable appears in the audit log, system-tagged | Same path; audit written in the same transaction | integrations.integration.test.ts ("writes exactly one audit entry when the same alert arrives twice") |
Not SRS requirements — the SRS's SDK is server-side — so traced here separately.
| Capability | Implemented in | Proved by |
|---|---|---|
| Browsers get one user's booleans, only for exposed flags; no rules ever | routes/client.ts, per-flag client_side column |
client.integration.test.ts ("never sends rules, percentages, reasons or buckets", "does not reveal that a server-only flag exists — absent, not false"), e2e/browser-sdk.spec.ts ("hiding a flag again removes it from browsers entirely") |
| A visitor cannot impersonate a user | client/signed-context.ts (HMAC-SHA256, constant-time, verified before parsing); SDK signContext |
signed-context.test.ts, sign-interop.test.ts (SDK tokens verify in the API, byte-identical), client.integration.test.ts ("refuses a browser naming its own user while signing is required"), examples/*/e2e (heidi sees false only through her signed token) |
| Only allowed sites can use the client key | client/origins.ts, CORS in routes/client.ts |
origins.test.ts, client-config.test.ts, sdk/e2e/browser.spec.ts (a real disallowed origin, a sandboxed iframe) |
| Secret keys cannot be used from a browser | refuseSecretFromBrowser in middleware/auth.ts; the browser export condition |
browser-support.integration.test.ts, sdk/tests/package.test.ts (a browser bundle of flagpilot contains no key handling), examples/*/e2e (no secret in any bundle or request) |
| No SDK failure can break the host | Never-throw contract in all three entry points | sdk/tests/browser/*, sdk/tests/init.test.ts, sdk/tests/react/*, sdk/e2e/browser.spec.ts (zero page errors under every failure), examples/*/e2e (outages) |
| Normal, private and locked-down browsers | No cookies, optional guarded storage | sdk/tests/browser/persistence.test.ts, sdk/e2e/browser.spec.ts (a reused profile vs a fresh private one, storage that throws on access), examples/vite-react/e2e ("storage blocked entirely") |
| React 18 and 19, SSR, hydration, Strict Mode | flagpilot/react (useSyncExternalStore with a stable server snapshot) |
sdk/tests/react/* on React 19, and on React 18 via test:react18; examples/next-app/e2e (server HTML already correct, no hydration errors) |
| Managed from the dashboard | Editor switch, list badge, audit label, /settings/sdk |
e2e/browser-sdk.spec.ts (survives a save, reaches browsers, the setup page never contains a secret) |
| Fast enough | In-memory evaluation behind the endpoint | k6/client.js: p99 0.49 ms at 500 req/s |
FR-INT-01, FR-INT-02 and FR-INT-04 are System B's responsibility — tagging an issue with
a flag key, deciding a threshold was crossed, and retrying with backoff. This side implements the
endpoint they call and is deliberately unaware of them; nothing calls it yet.
- Fail-closed when no flag source is reachable (see below) — an outage must never switch unfinished features on.
- The evaluator lives in
packages/core, imported by both the API and the SDK, so the two can never disagree about a user. The bucketing hash is frozen; changing it would reshuffle every user. - The SDK evaluates locally. Known simplification: because of this, the API's
per-evaluation log line only sees
/api/flags/:key/evaluatetraffic, not SDK evaluations. A future analytics service would have the SDK batch evaluation counts back to the API. - Soft delete keeps audit rows valid; a partial unique index lets a deleted key be reused.
- Browsers never evaluate. The server SDK evaluates locally because it can be trusted with
every rule; a browser cannot, so the browser SDKs ask the API for one user's booleans instead.
One package with three entry points rather than three packages: one version, one install, and a
browserexport condition that makes the server entry safe to import by mistake. - v1 omissions: login rate limiting, refresh tokens, negative caching, single-flight on a simultaneous cache expiry (thundering herd). For the browser endpoint: per-IP rate limiting (the client IP depends on proxy configuration, and a wrong setting would throttle whole offices behind one NAT — volumetric protection belongs at the platform edge), streaming updates (polling plus refresh-on-focus suffices), auto-generated anonymous ids (a device identifier with consent implications — hosts pass their own), token expiry (reasoned in Client-side flags), wildcard-subdomain origins, and bindings beyond React.
Where this repo departs from ../docs/SRS.md, and why:
| Deviation | Why |
|---|---|
The evaluator lives in packages/core, not packages/api/src/services/targeting/ (§3.7) |
The API and the SDK must evaluate identically, and the SDK evaluates locally. A separate package with zero runtime dependencies is the only way it can bundle the evaluator without dragging Express into a consumer's app |
| The SDK does not read Redis directly, as the §3.3 diagram shows it doing | It fetches whole configs from /api/sdk/flags and evaluates in-process, so a check costs no network at all and a consumer needs no cache credentials. The trade-off is a refresh-interval delay instead of instant propagation, which NFR-FD-02's 2 s window accommodates |
Playwright lives in packages/dashboard/**e2e**/, not tests/ (§3.7) |
Keeps it outside the Vitest glob, so npm test needs no browser |
CLAUDE.md is gitignored though §3.7 lists it at the root |
It is agent instructions, not product documentation; everything a human needs is in this file |
Extra top-level directories not in §3.7: packages/core/, k6/, examples/ (victim-app, next-app, vite-react) |
Consequences of the two decisions above, plus the load suite and runnable SDK demos — the Next.js and Vite examples double as the browser SDKs' real-bundler tests |
| Browser and React SDKs, which the SRS does not ask for (its SDK is server-side) | Added in M10 so the same flags can drive client-rendered UI. They add an endpoint and a dashboard toggle without changing any SRS behaviour: no flag is exposed until an admin opts it in, and the existing SDK protocol is untouched |
The dashboard is not in docker-compose |
It is deployed separately (Vercel), and a container on port 3000 would collide with Playwright's own server. Compose covers the back end, which is what needs orchestrating |
| No multi-tenancy, projects or user management | Out of scope by design: one seeded admin, one flag namespace. There is no signup flow |
| RabbitMQ is not used, though it appears in the wider stack | Nothing here is queue-shaped: a flag write is a single transaction that must be immediately readable. It belongs in System B, where fingerprinting and alerting genuinely are |
Render's health check polls /health/live, not /health as M9's wording says |
/health queries Postgres on every call, and Render polls every ~30 s with no way to slow it down. That would keep Neon's scale-to-zero database permanently awake and exhaust its free compute allowance. /health is unchanged and is still the readiness view |
| Migrations run at container start, not as a pre-deploy step | Render's preDeployCommand requires a paid instance type. The start command chains the same migrate entrypoint M9 asks for, and both scripts wait for the database so a cold Neon pauses the boot instead of failing the deploy |
| A circuit breaker on the Postgres read tier, which the SRS does not mention | Added in M7 after load testing showed a total outage was correct but 6× slower than it needed to be. See Performance |
Three tiers, fastest first:
| Tier | Scope | Invalidated by |
|---|---|---|
| In-process map | One API instance | A pub/sub message, or FLAG_CACHE_TTL_SECONDS |
| Redis | Shared by all instances | The mutating instance deleting the key, or the same TTL |
| PostgreSQL | Source of truth | — |
A mutation writes Postgres, clears both cache tiers, and publishes the changed key on
flags:invalidate. Every other instance drops its in-process copy on that message, which is
how a change reaches all instances within ~2 s (NFR-02). The TTL is the safety net for a
message that never arrives: an instance can be stale for at most that long, never
indefinitely.
Fail-closed is the documented policy (FLAG_FALLBACK_POLICY). When no tier can answer
— Redis and Postgres both unreachable, for a flag this instance has never cached — evaluation
returns false with reason: "unavailable", and HTTP 200, never an exception. False
means users keep the behaviour the app had before the flag existed; fail-open would expose
every in-progress feature at 100% during an outage. /api/sdk/flags instead returns 503
rather than an empty list, because {"flags":[]} would tell the SDK that every flag had been
deleted.
What survives a Redis outage: evaluation (served from Postgres), the SDK flag list, and
mutations. /health reports 503 naming Redis, and the API process stays up.
A circuit breaker guards the Postgres read tier. After 5 consecutive database failures,
flag reads stop attempting Postgres and fail fast for 1 s, then let exactly one request through
as a probe. Without it, a total outage was still correct — 200, enabled: false,
reason: "unavailable" — but every request first waited out the pool's 1.5 s connect timeout,
which capped a single instance at 238 req/s while it was down. With it, the same outage answers
in ~1 ms at the full 500 req/s. Writes are not affected: an admin save still attempts the
database and still reports a real error, because a failed save must be visible. See
docs/milestone-7-load-and-chaos.md.
Known simplification: when the cached snapshot expires simultaneously on several instances they all reload from Postgres (a thundering herd). Acceptable at this scale; single-flight is the fix.
An external service — the error tracker this project is designed to pair with — can switch a flag off when it detects a problem:
curl -X POST localhost:4000/api/integrations/alert -H "x-integration-key: $INTEGRATION_API_KEY" -H 'content-type: application/json' -d '{"flagKey":"checkout-v2","reason":"error rate 12% over 5 minutes","source":"error-tracker"}'
# -> {"flagKey":"checkout-v2","disabled":true,"alreadyDisabled":false,"auditLogged":true}The audit entry is attributed to system:integration and carries the reason, so the dashboard
shows not just that a flag went off but why. The rollout percentage is left untouched, so
re-enabling resumes where it left off.
It is idempotent. A caller retrying on its own timeouts gets alreadyDisabled: true, auditLogged: false and no second history entry — always 200, because a machine that retries
on non-2xx would otherwise retry forever against a flag already in the state it asked for.
Its key is its own. Not the admin JWT, not the SDK key. Every row this endpoint writes is attributed to the system, so accepting a human's token would make that attribution a lie — and this key can do nothing except switch a flag off.
Unknown or deleted flag → 404. Missing reason or source → 400.
Every create, update and delete writes an audit_log row — actor, action, and the
full before/after flag — in the same transaction as the change, so a change is
never committed without its audit entry. A PATCH that changes nothing writes no row.
Deletes are soft (deleted_at), so audit rows keep pointing at a real flag; the key
itself becomes reusable, because the unique index only covers live flags.
Measured with k6, 500 req/s for 60 s, single API instance, Docker Desktop on a laptop. k6 runs inside the compose network so the numbers exclude Docker's host port proxy, which on Windows loopback adds 1-3 ms and would dominate a 5 ms budget.
The gate is on server-side handler time, which the route reports as Server-Timing: app;dur=<ms>;
the round-trip figure is given beside it rather than gated, because it includes the network.
| Observed | Target | |
|---|---|---|
| Throughput | 498.8 req/s sustained, 30,005 requests | 500 req/s (NFR-03) |
| Evaluation p99, server-side | 0.40 ms | < 5 ms (NFR-01) |
| Evaluation p95, server-side | 0.26 ms | |
| Round trip p95 / p99 | 1.51 ms / 3.14 ms | |
| Errors | 0.000% |
12x under the latency budget, because an L1 hit is a Map lookup plus two Math.imul
chains. FLAG_EVAL_LOG_SAMPLE_RATE was measured rather than assumed: logging every evaluation
costs about 0.12 ms at p99 (0.40 ms vs 0.28 ms at 1% sampling), so the default stays at 1 —
a full evaluation log is worth 2.4% of the budget.
Redis killed mid-run (down for 25 s of a 60 s run at 500 req/s): 0 failed requests, p99 0.32 ms, and a correctness counter comparing a pinned user's answer against its pre-outage value stayed at 0 wrong answers across 30,005 requests. The in-process tier absorbed the outage almost entirely.
Redis and Postgres both killed, requesting keys no tier had ever cached, so every request took
the fallback: still 500.1 req/s, still 0 failed, p99 1.07 ms, and every response was
200 {"enabled":false,"reason":"unavailable"} — 10,001 responses, zero policy violations. This is
the run that produced the circuit breaker described above; before it, the same test answered
correctly but at 238 req/s with a p99 of 1503 ms.
The blackout gate is applied to the steady state: after the first 3 s, p99 must stay under 25 ms. The first moments of an outage are spent detecting it — the breaker opens after 5 consecutive database failures, and how long those take depends on how fast the host's DNS reports a stopped container's hostname as gone (anywhere from a few ms to 230 ms has been observed on the same laptop). Those requests are bounded instead (none may exceed 3 s). Disabling the breaker still fails the gate decisively: steady-state p99 1,548 ms at 230 req/s.
The browser endpoint (POST /api/client/flags, M10) is held to the same bar. Each request
carries an allowed Origin and the client key; 70% carry a signed identity the API must verify
with HMAC before evaluating 25 browser flags:
| Observed | Target | |
|---|---|---|
| Requests | 30,080, 0.000% failed, every check passing | 500 req/s for 60 s |
| Handler p99 | 0.49 ms | < 5 ms |
| Handler p95 | 0.30 ms |
Reproduce any of it:
npm run compose:up
npm run load:test # the NFR gate
npm run load:chaos # the same load, Redis killed mid-run
npm run load:blackout # the same load, nothing reachable at all
npm run load:client # the browser endpoint, signed identities includedEach run exits non-zero if a threshold is crossed, so these are pass/fail gates rather than reports. Full method, all numbers and the pass/fail criteria per step: docs/milestone-7-load-and-chaos.md.
Four free services. The API is defined as code in render.yaml, so the deployment is
reviewable rather than a set of remembered dashboard clicks.
| Piece | Where | Notes |
|---|---|---|
| API | Render (Docker, free) | render.yaml blueprint; health check on /health/live |
| Postgres | Neon (free) | Scales to zero after ~5 min idle. Connection string needs ?sslmode=require |
| Redis | Upstash (free) | rediss:// — TLS, enabled by the scheme |
| Dashboard | Vercel | Root directory packages/dashboard, one env var: FLAGS_API_URL |
| SDK | npm — flagpilot |
npm install flagpilot; 0.2.0 adds flagpilot/browser and flagpilot/react |
Step-by-step instructions are in docs/milestone-9-deployment.md. Four things that are easy to get wrong, and are worth knowing before you start:
Migrations run at container start, not as a pre-deploy step. Render's preDeployCommand needs a
paid instance type, so render.yaml chains them into the start command instead:
node dist/db/migrate.js && node dist/db/seed.js && node dist/index.js. Both are idempotent —
Drizzle tracks applied migrations, and seedAdmin upserts — and both wait for the database first
(src/db/wait.ts, up to 60 s), so a Neon instance that is asleep when a deploy lands causes a pause
rather than a failed deploy.
Raise the connect timeouts. The defaults (1500 ms) are right for local Docker, where connecting
is instant. A Neon cold start takes seconds and Upstash adds a cross-region TLS handshake, so at the
local defaults an ordinary cold start reads as an outage — and five of those in a row open the
circuit breaker, which then fails closed on healthy infrastructure.
render.yaml sets 10000 / 5000 / 8000.
Never set ALLOW_DEV_CREDENTIALS in a deployment. Leaving it unset is what makes a forgotten
secret fail the deploy loudly instead of quietly publishing an unauthenticated service. See
Credentials.
Browser SDKs: deploy in this order. (1) The API first — the browser endpoint and the
client_side migration must exist before anything calls them; set CLIENT_API_KEY and
CLIENT_ALLOWED_ORIGINS in Render's Environment tab (a blueprint only prompts for sync: false
values when it is first created). (2) The dashboard — its editor now sends clientSide, which an
older API would reject. (3) Publish flagpilot@0.2.0. (4) Add each site that uses a browser SDK to
CLIENT_ALLOWED_ORIGINS. Existing flagpilot@0.1.0 installs keep working against the new API: the
only change to the feed they read is an extra field, which they ignore.
Free services sleep. Render spins a free web service down after ~15 minutes idle, and the next
request pays a cold start of up to ~50 s. Point a cron (cron-job.org or similar) at
https://<your-api>/health/live every 10 minutes: it keeps Render warm and, because that endpoint
touches nothing, it lets Neon sleep. Or simply warm the API by hand two minutes before a demo.
A rehearsed sequence that shows the whole system in about five minutes. The numbers below are
real, not illustrative: the bucketing hash is frozen, so these eight accounts land in exactly
these buckets for the flag new-checkout-flow, and
golden.test.ts fails if that ever stops being true.
carol=2 dave=32 alice=34 grace=39 frank=40 bob=41 erin=57 heidi=85
Set up: create a flag new-checkout-flow, and run the consumer somewhere visible —
ideally on a second machine, which is what makes the propagation real rather than a claim:
# Outside this repository, so it exercises the published package, not the workspace link.
mkdir flagpilot-demo && cd flagpilot-demo && npm init -y
npm pkg set type=module # the app is ESM; `npm init -y` writes a CommonJS manifest
npm install flagpilot express
# copy examples/victim-app/index.js here, then:
FLAGS_API_URL=https://<your-api>.onrender.com FLAGS_API_KEY=<your SDK_API_KEY> FLAG_KEY=new-checkout-flow REFRESH_MS=5000 node index.js| # | Do this in the dashboard | What the audience sees |
|---|---|---|
| 0 | Warm the API first — hit /health/live and wait for 200 |
A free service that is asleep makes step 1 look broken |
| 1 | Rollout 0 %, enabled on | Nobody is in. The feature is shipped but dark |
| 2 | Rollout 10 % | carol turns on, alone. One account, deterministically |
| 3 | Reload the page, twice | carol is still in. Same user, same bucket, every time — no random assignment |
| 4 | Rollout 50 % | Six of the eight: carol, dave, alice, grace, frank, bob |
| 5 | Back to 10 %, then allowlist heidi (userId in [heidi]) |
carol and heidi. heidi's bucket is 85 — the furthest out of anyone — so this can only be the rule, not luck |
| 6 | Toggle Enabled off | Nobody, instantly, while the rollout still reads 10 %. This is the rollback |
| 7 | Open the flag's history | Every step above, with who did it and the before/after values |
Then the parts a dashboard cannot show:
# 8. An external service disables the flag — and does it twice, to show idempotency
curl -X POST https://<your-api>/api/integrations/alert -H "x-integration-key: $INTEGRATION_API_KEY" -H 'content-type: application/json' -d '{"flagKey":"new-checkout-flow","reason":"error rate 12% over 5 minutes","source":"error-tracker"}'
# -> {"disabled":true,"alreadyDisabled":false,"auditLogged":true}
# run it again:
# -> {"disabled":true,"alreadyDisabled":true,"auditLogged":false} 200, and no second history entryThe history now shows System (integration) turning the flag off, with the reason — the error-tracking story this project is designed to pair with, and the point at which "we shipped a bug" becomes "the system caught it and rolled itself back".
# 9. Break the cache and show it degrades rather than falls over
curl https://<your-api>/health # 503, naming redis; /health/live is still 200
curl -H "x-api-key: $SDK_API_KEY" "https://<your-api>/api/flags/new-checkout-flow/evaluate?userId=carol"
# still answers, served from Postgres. The victim app keeps working throughoutDo step 9 locally, not against the deployment. A managed Redis cannot be stopped: breaking
Upstash means repointing REDIS_URL and redeploying, or deleting the database. Run
docker compose stop redis on your own machine, show the same two curls against
localhost:4000, then docker compose start redis to recover. It is the same code, and the
Performance numbers measure it at 500 req/s — attempting to break production live is
the one step in this sequence likely to go wrong in front of an audience.
With both tiers down, evaluation returns 200 {"enabled":false,"reason":"unavailable"} — the
documented fail-closed policy.
| Script | What it does |
|---|---|
npm run dev |
Start the API with tsx watch |
npm run build |
Compile every package to dist/ |
npm run typecheck |
tsc --noEmit across packages |
npm test |
Vitest in every package — includes Testcontainers integration tests (needs Docker) |
npm run test:e2e |
Playwright against the dashboard; builds core + sdk first (needs the API running) |
npm run dev:dashboard |
Start the dashboard on :3000 |
npm run test:unit |
Vitest, excluding *.integration.test.ts |
npm run db:generate |
Diff src/db/schema.ts against drizzle/ and emit a new SQL migration |
npm run db:migrate |
Apply pending migrations to DATABASE_URL |
npm run db:seed |
Create/update the single admin from ADMIN_EMAIL + ADMIN_PASSWORD |
npm run compose:up / compose:down |
Wrapper around docker compose |
npm run load:test |
k6 load test against the evaluate endpoint — the NFR-01/NFR-03 gate |
npm run load:chaos |
The same load with Redis stopped mid-run |
npm run load:blackout |
The same load with Redis and Postgres stopped — exercises the fallback policy |
npm run load:client |
k6 against the browser endpoint, with signed identities — the same 5 ms / 500 req/s bar |
npm run test:sdk-browser |
Playwright: the browser SDK in Chromium, Firefox and WebKit against the real API (needs the stack running) |
npm run test:examples |
Playwright: the Next.js and Vite + React 18 examples, production builds, against the real API |
npm run test:react18 -w packages/sdk |
Packs the SDK and runs the React tests against React 18 in a throwaway project (needs network) |
npm run test:tarball -w packages/sdk |
Pre-publish check: installs the packed tarball outside the repo and builds a Node, a Vite + React 18 and a Next.js project with it (needs network) |
- Edit
packages/api/src/db/schema.ts. npm run db:generate— commit the generateddrizzle/NNNN_*.sqlanddrizzle/meta/.npm run db:migratelocally, ornpm run compose:up(themigrateservice applies it).
feature-flag-dashboard/
├── docker-compose.yml # postgres:16, redis:7, migrate + seed (one-shot), api, k6 (profile: load)
├── render.yaml # Render blueprint for the API — no secret values
├── LICENSE # MIT; copied into packages/sdk so the npm tarball carries it
├── package.json # npm workspaces root
├── tsconfig.base.json
├── k6/ # load + chaos tests (evaluate, chaos, blackout, client, orchestrator)
├── examples/
│ ├── victim-app/ # runnable server-SDK demo (also the M9 runbook script)
│ ├── next-app/ # Next.js App Router: server + client flags, signed identities (port 3001)
│ └── vite-react/ # Vite + React 18 SPA, plus a tiny signing server (ports 5173, 5180)
└── packages/
├── core/ # @feature-flags/core — bucketing + evaluation + types, ZERO runtime deps
├── api/ # Flag Service API (Express 5, Drizzle, ioredis)
│ ├── Dockerfile # build context = repo root
│ ├── drizzle/ # generated SQL migrations + meta (committed)
│ ├── src/
│ │ ├── index.ts # entrypoint: config → deps → listen
│ │ ├── app.ts # createApp(deps) — testable without a port
│ │ ├── config.ts # zod-validated env
│ │ ├── auth/ # jwt.ts (hand-rolled HS256), password.ts (scrypt)
│ │ ├── db/ # schema.ts, client.ts, migrate.ts, seed.ts, wait.ts (cold-start retry)
│ │ ├── middleware/ # auth.ts (credentials; secret keys refused from browsers), errors.ts (one envelope)
│ │ ├── routes/ # health.ts, auth.ts, flags.ts, sdk.ts, evaluate.ts, client.ts, settings.ts
│ │ ├── client/ # origins.ts (exact-origin allowlist), signed-context.ts (HMAC verification)
│ │ ├── services/flags/ # flag-service.ts — the only place flag SQL lives
│ │ ├── services/cache/ # redis.ts, flag-cache.ts (two tiers), circuit.ts (breaker on the DB tier)
│ │ ├── validation/ # zod schemas for flag input
│ │ ├── integrations/ # alert.ts — POST /api/integrations/alert
│ └── tests/ # *.test.ts (unit), *.integration.test.ts (Testcontainers)
├── sdk/ # flagpilot — published to npm; bundles core
│ ├── src/index.ts # flagpilot server SDK: local evaluation, getClientFlags, signContext
│ ├── src/server-stub.ts # what `flagpilot` resolves to in a browser bundle — no key, no fetch
│ ├── src/browser/ # flagpilot/browser framework-free browser client
│ ├── src/react/ # flagpilot/react FlagProvider + hooks ("use client")
│ ├── tests/ # Vitest: node, happy-dom, React
│ ├── scripts/ # test-react-18.mjs — the React tests on React 18, from the packed tarball
│ └── e2e/ # Playwright: Chromium, Firefox, WebKit against the real API
└── dashboard/ # Next.js App Router admin UI
├── app/ # login, flags list + detail, SDK setup page, route handlers, server actions
├── components/ # rule builder, audit timeline, UI primitives
├── lib/ # server-only API client (holds the cookie)
├── proxy.ts # auth gate (Next 16's middleware)
└── e2e/ # Playwright
core holds computeBucket, evaluateFlag/evaluateFlagDetailed and the rule types.
It exists because the SDK evaluates flags locally from cached configs rather
than asking the API per user — so the evaluator and rule types must be importable by
both the API and the SDK, and must carry no dependencies into a consumer's app.
| Variable | Default (packages/api/.env.example) |
Notes |
|---|---|---|
PORT |
4000 |
|
DATABASE_URL |
postgres://flags:flags@localhost:5433/flags |
Must be a postgres:// URL; 5433 is the compose host port |
REDIS_URL |
redis://localhost:6379 |
redis:// or rediss:// |
JWT_SECRET |
— (required) | Signs admin tokens; at least 32 characters |
JWT_EXPIRES_IN_SECONDS |
3600 |
Admin session lifetime |
SDK_API_KEY |
— (required) | Read-only key the SDK sends as x-api-key; at least 16 characters |
INTEGRATION_API_KEY |
— (required) | Key the alert endpoint accepts as x-integration-key; at least 16 characters |
FLAG_CACHE_TTL_SECONDS |
30 |
TTL on both cache tiers; the safety net for a missed invalidation |
FLAG_FALLBACK_POLICY |
fail-closed |
fail-closed | fail-open — what an unresolvable flag evaluates to |
DB_CONNECT_TIMEOUT_MS |
1500 |
Postgres connect timeout. Raise to ~10000 for a database that scales to zero — see Deployment |
REDIS_CONNECT_TIMEOUT_MS |
1500 |
Redis connect timeout. Raise to ~5000 for a managed instance reached over TLS in another region |
HEALTH_CHECK_TIMEOUT_MS |
2000 |
Per-dependency ceiling for /health. Keep it above the two values above |
ALLOW_DEV_CREDENTIALS |
false |
Permits the public placeholder credentials below. Set only by docker-compose.yml; a real deployment must never set it |
FLAG_EVAL_LOG_SAMPLE_RATE |
1 |
Fraction of evaluations that emit the structured log line (0-1); costs ~0.12 ms at p99 at the default |
ADMIN_EMAIL / ADMIN_PASSWORD |
— | Read by npm run db:seed only, not by the running API |
CLIENT_API_KEY |
unset (.env.example: dev-only-client-key-public) |
Public key for the browser SDKs, at least 16 characters. Unset → the browser endpoint does not exist. Must differ from SDK_API_KEY, INTEGRATION_API_KEY and JWT_SECRET, or the API refuses to start |
CLIENT_ALLOWED_ORIGINS |
— (required with CLIENT_API_KEY) |
Comma-separated exact origins (https://app.example.com,http://localhost:5173), or * alone for any site. Paths, wildcard subdomains and null are refused at boot |
CLIENT_REQUIRE_SIGNED_CONTEXT |
true |
true: an identified browser user must arrive as a token from signContext(). false accepts a plain user id — fine for a demo, unsafe for real users |
Four credential values are committed to this repository — in packages/api/.env.example,
docker-compose.yml, the k6 scripts and this README:
JWT_SECRET dev-only-jwt-secret-change-me-at-least-32-chars
SDK_API_KEY dev-only-sdk-api-key-change-me
INTEGRATION_API_KEY dev-only-integration-key-change-me
ADMIN_PASSWORD change-me-please
They are not secrets and never were. They exist so that a clean clone runs with no
configuration at all, which is what makes docker compose up a two-command start. But anyone who
can read this repo can sign an admin token with that JWT secret, so a deployment using it has no
authentication whatsoever.
So the API and the seed script refuse to start on any of them unless
ALLOW_DEV_CREDENTIALS=true is set:
Refusing to start: JWT_SECRET, SDK_API_KEY, INTEGRATION_API_KEY are still set to this
repository's development placeholder, which is public.
Generate real values with:
node -e "console.log(require('crypto').randomBytes(48).toString('base64url'))"
Set ALLOW_DEV_CREDENTIALS=true only for a throwaway local stack.
docker-compose.yml sets it, because that stack is throwaway by design. Nothing else should.
The client key's placeholder (dev-only-client-key-public) is deliberately not in that guard: the
client key is public by design — it ships in every page that uses it — so a known value grants
nothing a real one would not. The origin allowlist and signed identities are what protect the
browser endpoint. Generate a fresh one for a deployment anyway, so it can be rotated on its own.
A deployment that forgets to supply a real secret therefore fails loudly at boot instead of
starting up unauthenticated — the failure mode you want, rather than the silent one.
Opting in requires the exact string true: ALLOW_DEV_CREDENTIALS=false or 0 still refuses, so
the guard cannot be disabled by accident. It is covered by
cache-fallback.test.ts, whose tests are mostly
about proving the guard fails correctly.
You never edit docker-compose.yml to set a secret. Every value in it is written as
${VAR:-public-default}, and Compose reads VAR from a .env file in the same directory as
docker-compose.yml — automatically, with no flag — or from your shell, falling back to the
public default only when neither supplies it.
cp .env.example .env # in feature-flag-dashboard/, next to docker-compose.yml
# fill in JWT_SECRET, SDK_API_KEY, INTEGRATION_API_KEY, ADMIN_PASSWORD
# and set ALLOW_DEV_CREDENTIALS=false
npm run compose:up.env is gitignored, so the secrets stay out of git while docker-compose.yml — which is
committed — carries no real value at all.
There are two different .env files, and mixing them up is the easy mistake:
| File | Read by | Used when |
|---|---|---|
./.env (next to docker-compose.yml) |
Docker Compose, for ${...} substitution |
The containers — npm run compose:up |
packages/api/.env |
dotenv, inside the API process |
The API running on the host — npm run dev, npm run db:seed |
Compose never reads packages/api/.env. Putting your real secrets only there leaves the containers
on the public defaults, silently. If you run the API both ways, maintain both files.
Two details that make a partly-filled .env safe rather than surprising:
- An empty value (
JWT_SECRET=) counts as unset and falls back to the default, because the substitutions use${VAR:-default}and not${VAR-default}. So a half-completed.envstill boots. - Which is exactly why
ALLOW_DEV_CREDENTIALS=falseis worth setting once your values are real: it turns "I filled in two of the three secrets" from a silent fallback into a refusal that names the one you missed.
To confirm what the containers will actually receive:
docker compose config | grep -E "JWT_SECRET|SDK_API_KEY|INTEGRATION_API_KEY"That prints the resolved values, so don't paste its output anywhere — which is also the quickest
way to prove your .env is being picked up.
No real secret is tracked in this repository, and none ever has been. Every env file is
gitignored via .env* with !.env.example, which covers the variants a deployment tends to
create (.env.production, .env.test, .env.production.local); Playwright's test-results/
(traces can contain a live session token) and k6/results/*.json are ignored too; and nothing
logs a credential at startup or on any request path.