Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | /** * Server-side feature flags library * * Provides global on/off toggles with optional JSON config. * Uses in-memory cache with 30s TTL and Redis pub/sub for * cross-replica invalidation. * * Supports per-user overrides: when a userId is provided, * the override value takes precedence over the global flag. */ import { and, eq } from 'drizzle-orm' import { db, schema } from '@/db' import { createRedisClient } from '@/lib/redis' const REDIS_CHANNEL = 'featureflags:changed' const CACHE_TTL_MS = 30_000 // In-memory cache interface CachedFlag { enabled: boolean config: string | null allowedRoles: string | null // raw JSON string, parsed at evaluation time } let flagCache: Map<string, CachedFlag> | null = null let cacheTimestamp = 0 let subscriberSetup = false function isCacheValid(): boolean { return flagCache !== null && Date.now() - cacheTimestamp < CACHE_TTL_MS } async function loadAllFlags(): Promise<Map<string, CachedFlag>> { const rows = await db.select().from(schema.featureFlags) const map = new Map<string, CachedFlag>() for (const row of rows) { map.set(row.key, { enabled: row.enabled, config: row.config, allowedRoles: row.allowedRoles }) } return map } async function getCache(): Promise<Map<string, CachedFlag>> { if (isCacheValid()) return flagCache! flagCache = await loadAllFlags() cacheTimestamp = Date.now() // Set up Redis subscriber on first cache load if (!subscriberSetup) { subscriberSetup = true setupRedisSubscriber() } return flagCache } function invalidateCache(): void { flagCache = null cacheTimestamp = 0 } /** * Notify all replicas that feature flags have changed. * Call this after any mutation to feature_flags. */ export async function notifyFlagsChanged(): Promise<void> { const publisher = createRedisClient() if (!publisher) return try { await publisher.publish(REDIS_CHANNEL, Date.now().toString()) publisher.disconnect() } catch (err) { console.error('[feature-flags] Failed to publish change:', err) publisher.disconnect() } } function setupRedisSubscriber(): void { const subscriber = createRedisClient() if (!subscriber) return subscriber.subscribe(REDIS_CHANNEL).catch((err) => { console.error('[feature-flags] Failed to subscribe to channel:', err) }) subscriber.on('message', (channel) => { if (channel === REDIS_CHANNEL) { invalidateCache() console.log('[feature-flags] Cache invalidated from Redis notification') } }) } // --------------------------------------------------------------------------- // Override helpers (per-user) // --------------------------------------------------------------------------- /** * Load all overrides for a specific user. Returns a map of flagKey -> override. * Queries the DB directly (no cache) since overrides are low-volume. */ async function loadUserOverrides( userId: string ): Promise<Map<string, { enabled: boolean; config: string | null }>> { const rows = await db .select() .from(schema.featureFlagOverrides) .where(eq(schema.featureFlagOverrides.userId, userId)) const map = new Map<string, { enabled: boolean; config: string | null }>() for (const row of rows) { map.set(row.flagKey, { enabled: row.enabled, config: row.config }) } return map } function parseConfig(raw: string | null): unknown { if (!raw) return null try { return JSON.parse(raw) } catch { return raw } } /** * Check if a user role is allowed by a flag's allowedRoles setting. * Returns true if allowedRoles is null (all roles allowed) or if the role is in the list. */ function isRoleAllowed(userRole: string | undefined, allowedRoles: string | null): boolean { if (!allowedRoles) return true // null = all roles try { const roles = JSON.parse(allowedRoles) as string[] return roles.includes(userRole ?? 'guest') } catch { return true // malformed = permissive } } // --------------------------------------------------------------------------- // Public evaluation API // --------------------------------------------------------------------------- /** * Check if a flag is enabled. * If userId is provided, checks for a per-user override first. * If userRole is provided, checks role allowlist before evaluating. * Returns defaultValue if the flag doesn't exist (defaults to false). */ export async function isEnabled( key: string, opts?: boolean | { userId?: string; userRole?: string; defaultValue?: boolean } ): Promise<boolean> { // Support legacy signature: isEnabled(key, defaultValue) const defaultValue = typeof opts === 'boolean' ? opts : (opts?.defaultValue ?? false) const userId = typeof opts === 'object' ? opts?.userId : undefined const userRole = typeof opts === 'object' ? opts?.userRole : undefined const cache = await getCache() const flag = cache.get(key) if (!flag) return defaultValue // Check role allowlist — if role is not allowed, return default if (!isRoleAllowed(userRole, flag.allowedRoles)) return defaultValue // If userId is provided, check for override if (userId) { const override = await db .select() .from(schema.featureFlagOverrides) .where( and( eq(schema.featureFlagOverrides.flagKey, key), eq(schema.featureFlagOverrides.userId, userId) ) ) .limit(1) if (override.length > 0) { return override[0].enabled } } return flag.enabled } /** * Get full flag data (enabled + parsed config). * If userId is provided, checks for a per-user override first. * If userRole is provided, checks role allowlist before evaluating. * Returns null if the flag doesn't exist or role is not allowed. */ export async function getFlag( key: string, opts?: { userId?: string; userRole?: string } ): Promise<{ enabled: boolean; config: unknown } | null> { const cache = await getCache() const flag = cache.get(key) if (!flag) return null // Check role allowlist — if role is not allowed, return null if (!isRoleAllowed(opts?.userRole, flag.allowedRoles)) return null const userId = opts?.userId // Check for user override if (userId) { const override = await db .select() .from(schema.featureFlagOverrides) .where( and( eq(schema.featureFlagOverrides.flagKey, key), eq(schema.featureFlagOverrides.userId, userId) ) ) .limit(1) if (override.length > 0) { // Override config takes precedence; fall back to global config if null const config = override[0].config ?? flag.config return { enabled: override[0].enabled, config: parseConfig(config) } } } return { enabled: flag.enabled, config: parseConfig(flag.config) } } /** * Get typed config from a flag. * Returns defaultValue if the flag is missing or config is null. */ export async function getFlagConfig<T>(key: string, defaultValue: T): Promise<T> { const result = await getFlag(key) if (!result || result.config === null) return defaultValue return result.config as T } /** * Get all flags (for API routes). * If userId is provided, merges per-user overrides into the result. * If userRole is provided, excludes flags where the role is not in the allowlist. */ export async function getAllFlags( userId?: string, userRole?: string ): Promise<Record<string, { enabled: boolean; config: unknown }>> { const cache = await getCache() const result: Record<string, { enabled: boolean; config: unknown }> = {} for (const [key, flag] of cache) { // Skip flags where the user's role is not in the allowlist if (!isRoleAllowed(userRole, flag.allowedRoles)) continue result[key] = { enabled: flag.enabled, config: parseConfig(flag.config) } } // Merge user overrides if userId provided if (userId) { const overrides = await loadUserOverrides(userId) for (const [key, override] of overrides) { const globalFlag = cache.get(key) // Only apply override if the user's role is in the allowlist if (globalFlag && !isRoleAllowed(userRole, globalFlag.allowedRoles)) continue // Override config takes precedence; fall back to global config if null const config = override.config ?? globalFlag?.config ?? null result[key] = { enabled: override.enabled, config: parseConfig(config) } } } return result } // --------------------------------------------------------------------------- // Global flag CRUD // --------------------------------------------------------------------------- /** * Create a new feature flag. */ export async function createFlag(data: { key: string enabled?: boolean config?: string | null description?: string | null allowedRoles?: string | null }): Promise<void> { const now = new Date() await db.insert(schema.featureFlags).values({ key: data.key, enabled: data.enabled ?? false, config: data.config ?? null, description: data.description ?? null, allowedRoles: data.allowedRoles ?? null, createdAt: now, updatedAt: now, }) invalidateCache() await notifyFlagsChanged() } /** * Update an existing feature flag. */ export async function updateFlag( key: string, data: { enabled?: boolean config?: string | null description?: string | null allowedRoles?: string | null } ): Promise<boolean> { const result = await db .update(schema.featureFlags) .set({ ...data, updatedAt: new Date(), }) .where(eq(schema.featureFlags.key, key)) if (result.rowsAffected === 0) return false invalidateCache() await notifyFlagsChanged() return true } /** * Delete a feature flag. */ export async function deleteFlag(key: string): Promise<boolean> { const result = await db.delete(schema.featureFlags).where(eq(schema.featureFlags.key, key)) if (result.rowsAffected === 0) return false invalidateCache() await notifyFlagsChanged() return true } /** * Get all flags with full metadata (for admin API). */ export async function getAllFlagsAdmin() { return db.select().from(schema.featureFlags) } // --------------------------------------------------------------------------- // Override CRUD (admin operations) // --------------------------------------------------------------------------- /** * Get all overrides for a given flag key. */ export async function getOverridesForFlag(flagKey: string) { return db .select() .from(schema.featureFlagOverrides) .where(eq(schema.featureFlagOverrides.flagKey, flagKey)) } /** * Set (upsert) an override for a specific user on a specific flag. */ export async function setOverride( flagKey: string, userId: string, data: { enabled: boolean; config?: string | null } ): Promise<void> { const now = new Date() await db .insert(schema.featureFlagOverrides) .values({ flagKey, userId, enabled: data.enabled, config: data.config ?? null, createdAt: now, updatedAt: now, }) .onConflictDoUpdate({ target: [schema.featureFlagOverrides.flagKey, schema.featureFlagOverrides.userId], set: { enabled: data.enabled, config: data.config ?? null, updatedAt: now, }, }) await notifyFlagsChanged() } /** * Delete a specific override. */ export async function deleteOverride(flagKey: string, userId: string): Promise<boolean> { const result = await db .delete(schema.featureFlagOverrides) .where( and( eq(schema.featureFlagOverrides.flagKey, flagKey), eq(schema.featureFlagOverrides.userId, userId) ) ) if (result.rowsAffected === 0) return false await notifyFlagsChanged() return true } |