Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
Reference in new issue
Block a user