97 lines
3.2 KiB
TypeScript
97 lines
3.2 KiB
TypeScript
/**
|
|
* Server-side evaluateFlag bridge.
|
|
*
|
|
* Wires the Prisma FeatureFlag table into the package-level
|
|
* `evaluateFlag` (`@toolctrl/feature-flags`). Caller passes a request
|
|
* + an EvaluationContext; we look up the row by name (caches L2-first
|
|
* via the feature-flag eval cache), run the pure evaluator and
|
|
* memo-cache the typed result for 60s.
|
|
*
|
|
* Multi-key lookup: the package-level loader is keyed by `name`, but
|
|
* dependency-resolution calls back with a parent-flag *id*. We
|
|
* therefore accept either via a single Prisma `OR` query so the
|
|
* second-level lookup keeps working.
|
|
*/
|
|
|
|
import {
|
|
evaluateFlag as evaluatePackage,
|
|
getFeatureFlagVariant as packageGetVariant,
|
|
type EvaluateFlagResult,
|
|
type FlagLoader,
|
|
type StoredFeatureFlag,
|
|
} from "@toolctrl/feature-flags";
|
|
import type { EvaluationContext } from "@toolctrl/feature-flags";
|
|
import type { FeatureFlag } from "@toolctrl/db/prisma-browser";
|
|
import { db } from "~/utils/db.server";
|
|
import { getFeatureFlagCache } from "./featureFlagCache.server";
|
|
|
|
function toStored(row: FeatureFlag | null): StoredFeatureFlag | null {
|
|
if (!row) return null;
|
|
return {
|
|
id: row.id,
|
|
name: row.name,
|
|
enabled: row.enabled,
|
|
rules: row.rules ?? null,
|
|
variants: Array.isArray(row.variants) ? row.variants : [],
|
|
enabledFrom: row.enabledFrom ?? null,
|
|
enabledUntil: row.enabledUntil ?? null,
|
|
parentFlagId: row.parentFlagId ?? null,
|
|
};
|
|
}
|
|
|
|
const createLoader = (): FlagLoader => async (key: string) => {
|
|
// OR-lookup so dependency-resolution (which calls back with an id)
|
|
// works against the same loader.
|
|
const row = await db.featureFlag.findFirst({
|
|
where: { OR: [{ name: key }, { id: key }] },
|
|
});
|
|
return toStored(row);
|
|
};
|
|
|
|
/**
|
|
* Evaluate a single flag with full cache, dependency-resolution, and
|
|
* variant-selection. Returns the structured `{ enabled, variant?, reason }`.
|
|
*/
|
|
export async function evaluateFlagForContext(
|
|
name: string,
|
|
ctx: EvaluationContext,
|
|
): Promise<EvaluateFlagResult> {
|
|
const cache = getFeatureFlagCache();
|
|
const cached = await cache.get(name, ctx);
|
|
if (cached) return cached;
|
|
const result = await evaluatePackage(name, ctx, { load: createLoader() });
|
|
await cache.set(name, ctx, result);
|
|
return result;
|
|
}
|
|
|
|
/** Variant-only convenience wrapper. */
|
|
export async function getFeatureFlagVariantForContext(
|
|
name: string,
|
|
ctx: EvaluationContext,
|
|
) {
|
|
return packageGetVariant(name, ctx, { load: createLoader() });
|
|
}
|
|
|
|
/**
|
|
* Evaluate several flags in parallel. Used by root-loader SSR
|
|
* hydration so the React tree can read flags synchronously through
|
|
* a single context provider without re-fetching per call.
|
|
*/
|
|
export async function evaluateFlagsForContext(
|
|
names: readonly string[],
|
|
ctx: EvaluationContext,
|
|
): Promise<Record<string, EvaluateFlagResult>> {
|
|
const entries = await Promise.all(
|
|
names.map(async (n) => [n, await evaluateFlagForContext(n, ctx)] as const),
|
|
);
|
|
return Object.fromEntries(entries);
|
|
}
|
|
|
|
/**
|
|
* Drop every cached eval for a flag — call after admin mutations.
|
|
* Publishes on the feature-flags:invalidate channel.
|
|
*/
|
|
export async function invalidateFlagCache(flagName: string): Promise<void> {
|
|
await getFeatureFlagCache().invalidateFlag(flagName);
|
|
}
|