@vllnt/convex-flags 0.1.0-canary.0e432b5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +182 -0
  3. package/dist/client/index.d.ts +122 -0
  4. package/dist/client/index.d.ts.map +1 -0
  5. package/dist/client/index.js +94 -0
  6. package/dist/client/index.js.map +1 -0
  7. package/dist/client/types.d.ts +45 -0
  8. package/dist/client/types.d.ts.map +1 -0
  9. package/dist/client/types.js +2 -0
  10. package/dist/client/types.js.map +1 -0
  11. package/dist/component/_generated/api.d.ts +38 -0
  12. package/dist/component/_generated/api.d.ts.map +1 -0
  13. package/dist/component/_generated/api.js +31 -0
  14. package/dist/component/_generated/api.js.map +1 -0
  15. package/dist/component/_generated/component.d.ts +207 -0
  16. package/dist/component/_generated/component.d.ts.map +1 -0
  17. package/dist/component/_generated/component.js +11 -0
  18. package/dist/component/_generated/component.js.map +1 -0
  19. package/dist/component/_generated/dataModel.d.ts +46 -0
  20. package/dist/component/_generated/dataModel.d.ts.map +1 -0
  21. package/dist/component/_generated/dataModel.js +11 -0
  22. package/dist/component/_generated/dataModel.js.map +1 -0
  23. package/dist/component/_generated/server.d.ts +121 -0
  24. package/dist/component/_generated/server.d.ts.map +1 -0
  25. package/dist/component/_generated/server.js +78 -0
  26. package/dist/component/_generated/server.js.map +1 -0
  27. package/dist/component/convex.config.d.ts +3 -0
  28. package/dist/component/convex.config.d.ts.map +1 -0
  29. package/dist/component/convex.config.js +7 -0
  30. package/dist/component/convex.config.js.map +1 -0
  31. package/dist/component/mutations.d.ts +104 -0
  32. package/dist/component/mutations.d.ts.map +1 -0
  33. package/dist/component/mutations.js +209 -0
  34. package/dist/component/mutations.js.map +1 -0
  35. package/dist/component/queries.d.ts +103 -0
  36. package/dist/component/queries.d.ts.map +1 -0
  37. package/dist/component/queries.js +97 -0
  38. package/dist/component/queries.js.map +1 -0
  39. package/dist/component/schema.d.ts +157 -0
  40. package/dist/component/schema.d.ts.map +1 -0
  41. package/dist/component/schema.js +16 -0
  42. package/dist/component/schema.js.map +1 -0
  43. package/dist/component/validators.d.ts +376 -0
  44. package/dist/component/validators.d.ts.map +1 -0
  45. package/dist/component/validators.js +72 -0
  46. package/dist/component/validators.js.map +1 -0
  47. package/dist/react/index.d.ts +29 -0
  48. package/dist/react/index.d.ts.map +1 -0
  49. package/dist/react/index.js +35 -0
  50. package/dist/react/index.js.map +1 -0
  51. package/dist/shared.d.ts +107 -0
  52. package/dist/shared.d.ts.map +1 -0
  53. package/dist/shared.js +127 -0
  54. package/dist/shared.js.map +1 -0
  55. package/package.json +112 -0
  56. package/src/client/index.ts +242 -0
  57. package/src/client/types.ts +65 -0
  58. package/src/component/_generated/api.ts +54 -0
  59. package/src/component/_generated/component.ts +310 -0
  60. package/src/component/_generated/dataModel.ts +60 -0
  61. package/src/component/_generated/server.ts +156 -0
  62. package/src/component/convex.config.ts +9 -0
  63. package/src/component/mutations.ts +230 -0
  64. package/src/component/queries.ts +115 -0
  65. package/src/component/schema.ts +16 -0
  66. package/src/component/validators.ts +104 -0
  67. package/src/react/index.test.ts +52 -0
  68. package/src/react/index.ts +69 -0
  69. package/src/shared.ts +241 -0
  70. package/src/test.ts +17 -0
package/src/shared.ts ADDED
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Shared constants, types, and the pure evaluation engine used by both
3
+ * `client/` and `component/`.
4
+ *
5
+ * The engine is intentionally pure — no `ctx`, no database — so it can be
6
+ * reused by the component queries and exercised exhaustively end-to-end. Per-
7
+ * subject overrides and unknown-key handling live in the query layer (they need
8
+ * a database read); everything from a flag document downward lives here.
9
+ */
10
+
11
+ /**
12
+ * Why an evaluation returned the value it did. Stable string codes so hosts can
13
+ * branch on the reason without parsing prose.
14
+ */
15
+ export const EVAL_REASON = {
16
+ /** Served the flag's fallthrough value — no rule or rollout matched. */
17
+ flag: "flag",
18
+ /** A targeting rule matched and served a fixed value. */
19
+ rule: "rule",
20
+ /** A percentage rollout deterministically assigned the value. */
21
+ rollout: "rollout",
22
+ /** A per-subject override forced the value. */
23
+ override: "override",
24
+ /** The flag is archived; targeting was skipped and its base value served. */
25
+ disabled: "disabled",
26
+ /** Unknown key; the caller-supplied default was served. */
27
+ default: "default",
28
+ /** Unknown key and no caller default; the safe default (`false`) was served. */
29
+ unknown: "unknown",
30
+ } as const;
31
+
32
+ /** The set of evaluation reason codes. */
33
+ export type EvalReason = (typeof EVAL_REASON)[keyof typeof EVAL_REASON];
34
+
35
+ /**
36
+ * A flag/variant value. Typed primitives only — never `v.any()`. JSON-object
37
+ * variants are a documented future last resort (tracked in the roadmap).
38
+ */
39
+ export type VariantValue = boolean | string | number;
40
+
41
+ /** An attribute value usable in a targeting comparison. */
42
+ export type AttributeValue = boolean | string | number;
43
+
44
+ /**
45
+ * Host-supplied, per-evaluation context. Opaque to the component: the host owns
46
+ * what `subjectRef` and the attribute names/values mean.
47
+ */
48
+ export interface EvalContext {
49
+ /** Stable identifier used for rollout bucketing (e.g. a user or session ref). */
50
+ subjectRef?: string;
51
+ /** Targeting attributes, keyed by host-chosen names. */
52
+ attributes?: Record<string, AttributeValue>;
53
+ }
54
+
55
+ /** Result of evaluating a single flag. */
56
+ export interface FlagEvaluation {
57
+ value: VariantValue;
58
+ reason: EvalReason;
59
+ }
60
+
61
+ /** Comparison operators available to a targeting condition. */
62
+ export type ConditionOp =
63
+ | "eq"
64
+ | "neq"
65
+ | "in"
66
+ | "nin"
67
+ | "contains"
68
+ | "gt"
69
+ | "gte"
70
+ | "lt"
71
+ | "lte";
72
+
73
+ /** One targeting condition: `<attribute> <op> <values>`. */
74
+ export interface Condition {
75
+ attribute: string;
76
+ op: ConditionOp;
77
+ values: AttributeValue[];
78
+ }
79
+
80
+ /** One weighted entry in a rollout's distribution. */
81
+ export interface Split {
82
+ value: VariantValue;
83
+ /** Relative weight; normalized across the rollout's splits. */
84
+ weight: number;
85
+ }
86
+
87
+ /** A deterministic weighted distribution across variant values. */
88
+ export interface Rollout {
89
+ splits: Split[];
90
+ /** Context attribute to bucket by; defaults to `subjectRef` when omitted. */
91
+ by?: string;
92
+ }
93
+
94
+ /** A declared variant (metadata for tooling / labels). */
95
+ export interface Variant {
96
+ value: VariantValue;
97
+ label?: string;
98
+ }
99
+
100
+ /**
101
+ * A targeting rule. All `conditions` must match (AND); rules are evaluated in
102
+ * order and the first match wins. The outcome is a fixed `value` or a `rollout`.
103
+ */
104
+ export interface Rule {
105
+ conditions: Condition[];
106
+ value?: VariantValue;
107
+ rollout?: Rollout;
108
+ }
109
+
110
+ /** The evaluatable shape of a stored flag (the engine's input). */
111
+ export interface EvaluableFlag {
112
+ value: VariantValue;
113
+ status: "active" | "archived";
114
+ rules?: Rule[];
115
+ rollout?: Rollout;
116
+ }
117
+
118
+ /** Operator implementations. A table (not a switch) so there is no unreachable
119
+ * default branch — every operator resolves to one of these. */
120
+ const OPERATORS: Record<
121
+ ConditionOp,
122
+ (actual: AttributeValue, values: AttributeValue[]) => boolean
123
+ > = {
124
+ eq: (actual, values) => actual === values[0],
125
+ neq: (actual, values) => actual !== values[0],
126
+ in: (actual, values) => values.includes(actual),
127
+ nin: (actual, values) => !values.includes(actual),
128
+ contains: (actual, values) =>
129
+ typeof actual === "string" &&
130
+ typeof values[0] === "string" &&
131
+ actual.includes(values[0]),
132
+ gt: (actual, values) =>
133
+ typeof actual === "number" && typeof values[0] === "number" && actual > values[0],
134
+ gte: (actual, values) =>
135
+ typeof actual === "number" && typeof values[0] === "number" && actual >= values[0],
136
+ lt: (actual, values) =>
137
+ typeof actual === "number" && typeof values[0] === "number" && actual < values[0],
138
+ lte: (actual, values) =>
139
+ typeof actual === "number" && typeof values[0] === "number" && actual <= values[0],
140
+ };
141
+
142
+ /** Does a single condition hold for the given context? A missing attribute never
143
+ * matches (mirrors how mainstream flag SDKs treat absent targeting data). */
144
+ function matchesCondition(condition: Condition, context: EvalContext): boolean {
145
+ const actual = context.attributes?.[condition.attribute];
146
+ if (actual === undefined) {
147
+ return false;
148
+ }
149
+ return OPERATORS[condition.op](actual, condition.values);
150
+ }
151
+
152
+ /** Do all of a rule's conditions hold? An empty condition list always matches
153
+ * (a catch-all rule). */
154
+ function matchesRule(rule: Rule, context: EvalContext): boolean {
155
+ return rule.conditions.every((condition) => matchesCondition(condition, context));
156
+ }
157
+
158
+ /** Deterministic 32-bit FNV-1a hash of a string → uint32. */
159
+ export function hashString(input: string): number {
160
+ let hash = 0x811c9dc5;
161
+ for (let i = 0; i < input.length; i++) {
162
+ hash ^= input.charCodeAt(i);
163
+ hash = Math.imul(hash, 0x01000193);
164
+ }
165
+ return hash >>> 0;
166
+ }
167
+
168
+ /**
169
+ * Deterministically pick a split's value by hashing `flagKey:subject` across the
170
+ * cumulative weights. Returns `null` when no split has positive weight.
171
+ */
172
+ export function pickSplit(
173
+ flagKey: string,
174
+ subject: string,
175
+ splits: Split[],
176
+ ): VariantValue | null {
177
+ const positive = splits.filter((split) => split.weight > 0);
178
+ const total = positive.reduce((sum, split) => sum + split.weight, 0);
179
+ if (total === 0) {
180
+ return null;
181
+ }
182
+ const point = hashString(`${flagKey}:${subject}`) % total;
183
+ let cumulative = 0;
184
+ let chosen: VariantValue | null = null;
185
+ for (const split of positive) {
186
+ cumulative += split.weight;
187
+ if (chosen === null && point < cumulative) {
188
+ chosen = split.value;
189
+ }
190
+ }
191
+ return chosen;
192
+ }
193
+
194
+ /** Resolve a rollout to a value, or `null` if it cannot bucket (no rollout, no
195
+ * bucketing subject, or no positive-weight split). */
196
+ function resolveRollout(
197
+ flagKey: string,
198
+ rollout: Rollout | undefined,
199
+ context: EvalContext,
200
+ ): VariantValue | null {
201
+ if (rollout === undefined) {
202
+ return null;
203
+ }
204
+ const subject =
205
+ rollout.by !== undefined ? context.attributes?.[rollout.by] : context.subjectRef;
206
+ if (subject === undefined) {
207
+ return null;
208
+ }
209
+ return pickSplit(flagKey, String(subject), rollout.splits);
210
+ }
211
+
212
+ /**
213
+ * Evaluate a flag against context. Pure — given the same flag document and
214
+ * context it always returns the same result.
215
+ */
216
+ export function evaluateFlag(
217
+ flagKey: string,
218
+ flag: EvaluableFlag,
219
+ context: EvalContext,
220
+ ): FlagEvaluation {
221
+ if (flag.status === "archived") {
222
+ return { value: flag.value, reason: EVAL_REASON.disabled };
223
+ }
224
+ for (const rule of flag.rules ?? []) {
225
+ if (matchesRule(rule, context)) {
226
+ if (rule.value !== undefined) {
227
+ return { value: rule.value, reason: EVAL_REASON.rule };
228
+ }
229
+ const picked = resolveRollout(flagKey, rule.rollout, context);
230
+ if (picked !== null) {
231
+ return { value: picked, reason: EVAL_REASON.rollout };
232
+ }
233
+ return { value: flag.value, reason: EVAL_REASON.flag };
234
+ }
235
+ }
236
+ const picked = resolveRollout(flagKey, flag.rollout, context);
237
+ if (picked !== null) {
238
+ return { value: picked, reason: EVAL_REASON.rollout };
239
+ }
240
+ return { value: flag.value, reason: EVAL_REASON.flag };
241
+ }
package/src/test.ts ADDED
@@ -0,0 +1,17 @@
1
+ /// <reference types="vite/client" />
2
+ import type { TestConvex } from "convex-test";
3
+ import type { GenericSchema, SchemaDefinition } from "convex/server";
4
+ import schema from "./component/schema.js";
5
+
6
+ const modules = import.meta.glob("./component/**/*.ts");
7
+
8
+ /**
9
+ * Register this component with a `convex-test` instance so consuming apps can
10
+ * test integration: `import { register } from "@vllnt/convex-flags/test"`.
11
+ */
12
+ export function register(
13
+ t: TestConvex<SchemaDefinition<GenericSchema, boolean>>,
14
+ name: string = "flags",
15
+ ): void {
16
+ t.registerComponent(name, schema, modules);
17
+ }