@voltro/plugin-flags 0.85.0 → 0.86.0

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.
@@ -0,0 +1,82 @@
1
+ import { toDefinition as e } from "./evaluate.js";
2
+ //#region src/lifecycle.ts
3
+ var t = 864e5, n = (e) => typeof e == "number" ? e : Date.parse(e), r = (t, r) => {
4
+ let i = e(t);
5
+ if (i.enabled === !1) return {
6
+ shape: "constantOff",
7
+ reason: "enabled: false — resolves false for every caller"
8
+ };
9
+ if (i.schedule) {
10
+ let { activateAt: e, deactivateAt: t } = i.schedule;
11
+ if (t != null && r >= n(t)) return {
12
+ shape: "expired",
13
+ reason: `schedule.deactivateAt (${String(t)}) has passed — the flag can never be on again`
14
+ };
15
+ if (e != null && r < n(e)) return {
16
+ shape: "notYetActive",
17
+ reason: `schedule.activateAt (${String(e)}) is in the future — pending, not dead`
18
+ };
19
+ }
20
+ let a = i.rollout ?? 100;
21
+ if (a <= 0) return {
22
+ shape: "constantOff",
23
+ reason: `rollout: ${a} — nobody is in the rollout`
24
+ };
25
+ let o = (i.targeting?.length ?? 0) > 0, s = (i.variants?.length ?? 0) > 1, c = i.schedule !== void 0 && (i.schedule.ramp !== void 0 || i.schedule.deactivateAt != null && r < n(i.schedule.deactivateAt));
26
+ return a >= 100 && !o && !s && !c ? {
27
+ shape: "constantOn",
28
+ reason: "rollout 100 with no targeting, no variants and no live schedule — resolves true for every caller"
29
+ } : {
30
+ shape: "conditional",
31
+ reason: `still selects between callers (${[
32
+ a < 100 ? `rollout ${a}%` : void 0,
33
+ o ? `${i.targeting?.length} targeting rule(s)` : void 0,
34
+ s ? `${i.variants?.length} variants` : void 0,
35
+ c ? "a live schedule" : void 0
36
+ ].filter((e) => e !== void 0).join(", ")})`
37
+ };
38
+ }, i = [
39
+ "Reachability is not decided. \"Not evaluated since <date>\" is a measurement; \"this code path is dead\" is not decidable in general. A seasonal flag, a flag behind a route nobody visited this month, and a flag whose last call site was deleted are indistinguishable from here.",
40
+ "Only SERVER-side reads are counted as use: isFlagEnabled / requireFlag / flagValue and a gatedBy interception. A flag consulted only in the browser via useFlag() / useFlagValue() reads from the bulk set the server already sent, so it never reaches this report as a named read.",
41
+ "A \"bulk\" observation means the flag was INCLUDED in a flags.evaluate / flags.variants response. It does not mean any client read that key — one useFlags() poll evaluates the whole registry.",
42
+ "The window is bounded by retention (see coverage.retentionDays). Nothing older is evidence: a flag last read BEFORE the window has no observation at all and reads \"neverObserved\", which is exactly what a flag declared this morning reads.",
43
+ "\"neverObserved\" is not a removal candidate. A flag declared yesterday and a flag abandoned last year produce the identical row; only a flag that WAS observed and then stopped (\"stale\") is proven unused."
44
+ ], a = (e) => {
45
+ let n = e.now ?? Date.now(), a = e.staleAfterDays, o = n - a * t, s = /* @__PURE__ */ new Map(), c = /* @__PURE__ */ new Map(), l;
46
+ for (let t of e.usage) {
47
+ let e = t.lastAt.getTime();
48
+ if (!Number.isFinite(e)) continue;
49
+ (l === void 0 || e < l) && (l = e);
50
+ let n = t.source === "bulk" ? c : s, r = n.get(t.flag);
51
+ (r === void 0 || e > r) && n.set(t.flag, e);
52
+ }
53
+ let u = l === void 0 ? 0 : Math.floor((n - l) / t);
54
+ return {
55
+ coverage: {
56
+ tracking: e.tracking,
57
+ observedSince: l === void 0 ? null : new Date(l).toISOString().slice(0, 10),
58
+ observedDays: u,
59
+ retentionDays: e.retentionDays,
60
+ staleAfterDays: a
61
+ },
62
+ findings: Object.entries(e.flags).map(([t, i]) => {
63
+ let { shape: l, reason: d } = r(i, n), f = s.get(t), p = c.get(t), m = [d], h;
64
+ e.tracking ? f !== void 0 && f >= o ? (h = "evaluated", m.push(`last server-side read ${new Date(f).toISOString()}`)) : f === void 0 ? (h = "neverObserved", m.push(u > 0 ? `no server-side read in the ${u}-day observation window` : "no server-side read observed yet (the observation window is empty)"), p !== void 0 && m.push("the flag WAS delivered to a client in a bulk flags.evaluate response — which does not prove it was read")) : (h = "stale", m.push(`last server-side read ${new Date(f).toISOString()}, more than ${a} day(s) ago, over an observation window of ${u} day(s)`)) : (h = "untracked", m.push("evaluation tracking is off — no usage evidence exists for this deployment"));
65
+ let g = l === "constantOn" || l === "constantOff" || l === "expired";
66
+ return g && m.push("the definition alone makes this flag a constant — it selects nothing"), {
67
+ key: t,
68
+ shape: l,
69
+ shapeReason: d,
70
+ usage: h,
71
+ lastTargetedAt: f === void 0 ? null : new Date(f).toISOString(),
72
+ lastBulkAt: p === void 0 ? null : new Date(p).toISOString(),
73
+ liveTargetedEvaluations: e.liveCounts?.get(t) ?? 0,
74
+ removalCandidate: g || h === "stale",
75
+ why: m
76
+ };
77
+ }),
78
+ limits: i
79
+ };
80
+ };
81
+ //#endregion
82
+ export { i as FLAG_LIFECYCLE_LIMITS, r as classifyShape, a as flagLifecycleReport };
package/dist/store.js ADDED
@@ -0,0 +1,154 @@
1
+ import { toDefinition as e } from "./evaluate.js";
2
+ import { boolean as t, derivedRowId as n, eq as r, id as i, integer as a, json as o, queryFor as s, table as c, text as l, timestamp as u } from "@voltro/database";
3
+ //#region src/store.ts
4
+ var d = "_voltro_feature_flag_audit", f = "_voltro_feature_flags", p = c(f, {
5
+ id: i({ prefix: "flag" }),
6
+ key: l().unique(),
7
+ enabled: t().default(!0),
8
+ rollout: a().nullable(),
9
+ rolloutBy: l().nullable(),
10
+ targeting: o().nullable(),
11
+ variants: o().nullable(),
12
+ offVariant: l().nullable(),
13
+ schedule: o().nullable(),
14
+ description: l().nullable(),
15
+ updatedAt: u().default("now").onUpdate("now")
16
+ }), m = c(d, {
17
+ id: i({ prefix: "flagaud" }),
18
+ flag: l(),
19
+ enabled: t(),
20
+ previousEnabled: t().nullable(),
21
+ actorId: l().nullable(),
22
+ at: u().default("now")
23
+ }).index("byFlagAuditFlag", ["flag"]).index("byFlagAuditAt", ["at"]), h = (e, t) => {
24
+ let n = { ...e };
25
+ for (let e of t) {
26
+ let t = {
27
+ enabled: e.enabled,
28
+ ...e.rollout == null ? {} : { rollout: e.rollout },
29
+ ...e.rolloutBy === "tenant" || e.rolloutBy === "subject" ? { rolloutBy: e.rolloutBy } : {},
30
+ ...Array.isArray(e.targeting) ? { targeting: e.targeting } : {},
31
+ ...Array.isArray(e.variants) ? { variants: e.variants } : {},
32
+ ...e.offVariant == null ? {} : { offVariant: e.offVariant },
33
+ ...e.schedule != null && typeof e.schedule == "object" ? { schedule: e.schedule } : {},
34
+ ...e.description == null ? {} : { description: e.description }
35
+ };
36
+ n[e.key] = t;
37
+ }
38
+ return n;
39
+ }, g = class {
40
+ flags = {};
41
+ set(e) {
42
+ this.flags = e;
43
+ }
44
+ all() {
45
+ return this.flags;
46
+ }
47
+ get(e) {
48
+ return this.flags[e];
49
+ }
50
+ }, _ = new g(), v = (e, t) => y(e, t) !== null, y = (t, n) => {
51
+ let r = _.get(t);
52
+ if (r === void 0) return null;
53
+ let i = e(r), a = i.enabled !== !1;
54
+ return _.set({
55
+ ..._.all(),
56
+ [t]: {
57
+ ...i,
58
+ enabled: n
59
+ }
60
+ }), a;
61
+ }, b = (t) => Object.entries(t).map(([t, n]) => {
62
+ let r = e(n);
63
+ return {
64
+ key: t,
65
+ enabled: r.enabled !== !1,
66
+ rollout: r.rollout ?? 100,
67
+ variants: r.variants && r.variants.length > 0 ? r.variants.map((e) => e.name) : void 0,
68
+ scheduled: r.schedule != null,
69
+ description: r.description
70
+ };
71
+ }), x = (t, n) => {
72
+ let r = e(n);
73
+ return {
74
+ key: t,
75
+ enabled: r.enabled !== !1,
76
+ rollout: r.rollout ?? null,
77
+ rolloutBy: r.rolloutBy ?? null,
78
+ targeting: r.targeting ?? null,
79
+ variants: r.variants ?? null,
80
+ offVariant: r.offVariant ?? null,
81
+ schedule: r.schedule ?? null,
82
+ description: r.description ?? null
83
+ };
84
+ }, S = (e) => ({
85
+ loadOverrides: async () => (await e.query(s(p).descriptor)).map((e) => ({
86
+ key: String(e.key),
87
+ enabled: e.enabled === !0 || e.enabled === 1,
88
+ rollout: e.rollout == null ? null : Number(e.rollout),
89
+ rolloutBy: e.rolloutBy ?? null,
90
+ targeting: e.targeting ?? null,
91
+ variants: e.variants ?? null,
92
+ offVariant: e.offVariant ?? null,
93
+ schedule: e.schedule ?? null,
94
+ description: e.description ?? null
95
+ })),
96
+ upsertOverride: async (t) => {
97
+ let n = C(t);
98
+ await e.upsert(f, n, { conflictColumns: ["key"] });
99
+ }
100
+ }), C = (e) => ({
101
+ id: n("flag", e.key),
102
+ key: e.key,
103
+ enabled: e.enabled,
104
+ rollout: e.rollout,
105
+ rolloutBy: e.rolloutBy,
106
+ targeting: e.targeting,
107
+ variants: e.variants,
108
+ offVariant: e.offVariant,
109
+ schedule: e.schedule,
110
+ description: e.description,
111
+ updatedAt: /* @__PURE__ */ new Date()
112
+ }), w = (e) => ({
113
+ flag: String(e.flag),
114
+ enabled: e.enabled === !0 || e.enabled === 1,
115
+ previousEnabled: e.previousEnabled == null ? null : e.previousEnabled === !0 || e.previousEnabled === 1,
116
+ actorId: e.actorId ?? null,
117
+ at: e.at instanceof Date ? e.at : new Date(String(e.at))
118
+ }), T = (e) => ({
119
+ record: async (t) => {
120
+ await e.insert(d, {
121
+ id: `flagaud_${t.at.getTime()}_${Math.random().toString(36).slice(2, 10)}`,
122
+ flag: t.flag,
123
+ enabled: t.enabled,
124
+ previousEnabled: t.previousEnabled,
125
+ actorId: t.actorId,
126
+ at: t.at
127
+ });
128
+ },
129
+ list: async (t) => {
130
+ let n = s(m).orderBy("at", "desc");
131
+ return t?.flag != null && (n = n.where(r("flag", t.flag))), t?.limit != null && (n = n.limit(t.limit)), (await e.query(n.descriptor)).map(w);
132
+ }
133
+ }), E = async (e, t, i, a, o) => {
134
+ await e.transactional(async (e) => {
135
+ await e.insertIgnore(f, {
136
+ ...C(x(t, i)),
137
+ id: n("flag", crypto.randomUUID())
138
+ }, { conflictColumns: ["key"] });
139
+ let c = (await e.query(s(p).where(r("key", t)).forUpdate().descriptor))[0];
140
+ if (!c) throw Error("flag override disappeared during toggle");
141
+ await e.update(f, String(c.id), {
142
+ enabled: a,
143
+ updatedAt: /* @__PURE__ */ new Date()
144
+ }), await T(e).record({
145
+ flag: t,
146
+ enabled: a,
147
+ previousEnabled: c.enabled === !0 || c.enabled === 1,
148
+ actorId: o,
149
+ at: /* @__PURE__ */ new Date()
150
+ });
151
+ });
152
+ };
153
+ //#endregion
154
+ export { d as FLAGS_AUDIT_TABLE, f as FLAGS_TABLE, g as FlagRegistry, T as dataStoreFlagAuditStore, S as dataStoreFlagStore, b as describeFlags, p as featureFlagsTable, m as flagAuditTable, x as flagValueToRow, y as flipFlagEnabled, h as mergeOverrides, E as persistFlagToggle, _ as registry, v as setFlagEnabled };
package/dist/usage.js ADDED
@@ -0,0 +1,113 @@
1
+ import { id as e, queryFor as t, registerRetention as n, retentionTtlMsFromEnv as r, table as i, text as a, timestamp as o } from "@voltro/database";
2
+ //#region src/usage.ts
3
+ var s = "_voltro_feature_flag_usage", c = i(s, {
4
+ id: e({ prefix: "flagusg" }),
5
+ flag: a(),
6
+ day: a(),
7
+ source: a(),
8
+ lastAt: o()
9
+ }).unique([
10
+ "flag",
11
+ "day",
12
+ "source"
13
+ ]).index("byFlagUsageLastAt", ["lastAt"]).index("byFlagUsageFlag", ["flag"]), l = (e) => e.toISOString().slice(0, 10), u = (e) => {
14
+ let t = 2166136261;
15
+ for (let n = 0; n < e.length; n++) t ^= e.charCodeAt(n), t = Math.imul(t, 16777619);
16
+ return (t >>> 0).toString(16).padStart(8, "0");
17
+ }, d = (e, t, n) => `flagusg_${t.replace(/-/g, "")}_${n === "targeted" ? "t" : "b"}_${u(e)}`, f = new class {
18
+ on = !1;
19
+ pending = /* @__PURE__ */ new Map();
20
+ liveCounts = /* @__PURE__ */ new Map();
21
+ enable() {
22
+ this.on = !0;
23
+ }
24
+ disable() {
25
+ this.on = !1, this.pending.clear();
26
+ }
27
+ get enabled() {
28
+ return this.on;
29
+ }
30
+ record(e, t, n = Date.now()) {
31
+ if (!this.on) return;
32
+ let r = `${t}\u0000${e}`, i = this.pending.get(r);
33
+ i ? i.lastAt = n : this.pending.set(r, {
34
+ flag: e,
35
+ source: t,
36
+ lastAt: n
37
+ }), t === "targeted" && this.liveCounts.set(e, (this.liveCounts.get(e) ?? 0) + 1);
38
+ }
39
+ drain() {
40
+ let e = [...this.pending.values()];
41
+ return this.pending.clear(), e;
42
+ }
43
+ restore(e) {
44
+ if (this.on) for (let t of e) {
45
+ let e = `${t.source}\u0000${t.flag}`, n = this.pending.get(e);
46
+ (!n || n.lastAt < t.lastAt) && this.pending.set(e, { ...t });
47
+ }
48
+ }
49
+ liveTargetedCounts() {
50
+ return this.liveCounts;
51
+ }
52
+ reset() {
53
+ this.on = !1, this.pending.clear(), this.liveCounts.clear();
54
+ }
55
+ }(), p = (e, t, n) => f.record(e, t, n), m = (e) => e === "bulk" ? "bulk" : "targeted", h = (e) => ({
56
+ upsertMany: async (t) => {
57
+ for (let n of t) {
58
+ let t = l(n.lastAt);
59
+ await e.upsert(s, {
60
+ id: d(n.flag, t, n.source),
61
+ flag: n.flag,
62
+ day: t,
63
+ source: n.source,
64
+ lastAt: n.lastAt
65
+ }, { conflictColumns: [
66
+ "flag",
67
+ "day",
68
+ "source"
69
+ ] });
70
+ }
71
+ },
72
+ load: async () => (await e.query(t(c).descriptor)).map((e) => ({
73
+ flag: String(e.flag),
74
+ day: String(e.day),
75
+ source: m(e.source),
76
+ lastAt: e.lastAt instanceof Date ? e.lastAt : new Date(String(e.lastAt))
77
+ }))
78
+ }), g = 3e5, _ = (e) => {
79
+ let t = e.intervalMs ?? 3e5, n = !1, r = async () => {
80
+ let t = f.drain();
81
+ if (t.length !== 0) try {
82
+ await e.store.upsertMany(t.map((e) => ({
83
+ flag: e.flag,
84
+ source: e.source,
85
+ lastAt: new Date(e.lastAt)
86
+ })));
87
+ } catch (n) {
88
+ f.restore(t), e.log?.warn?.("flag usage flush failed — observations retained for the next pass", {
89
+ pending: t.length,
90
+ cause: n.message
91
+ });
92
+ }
93
+ }, i = setInterval(() => {
94
+ r();
95
+ }, t);
96
+ return typeof i.unref == "function" && i.unref(), {
97
+ flushNow: r,
98
+ stop: async () => {
99
+ n || (n = !0, clearInterval(i), await r());
100
+ }
101
+ };
102
+ }, v = (e = process.env, t = 90) => {
103
+ let i = r(e.VOLTRO_FLAG_USAGE_TTL_HOURS, 24 * t);
104
+ return n({
105
+ table: s,
106
+ timeColumn: "lastAt",
107
+ source: "plugin",
108
+ ttlMs: i,
109
+ envVar: "VOLTRO_FLAG_USAGE_TTL_HOURS"
110
+ }), i;
111
+ };
112
+ //#endregion
113
+ export { g as DEFAULT_FLAG_USAGE_FLUSH_MS, s as FLAG_USAGE_TABLE, h as dataStoreFlagUsageStore, f as flagUsageBuffer, d as flagUsageRowId, c as flagUsageTable, p as recordFlagEvaluation, v as registerFlagUsageRetention, _ as startFlagUsageFlush, l as utcDay };
package/dist/web.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { BASE_NAME as e } from "./rpc.js";
2
- import { i as t } from "./define-BHpLJh1F.js";
2
+ import { typedValueOf as t } from "./define.js";
3
3
  import { pluginTag as n } from "@voltro/protocol";
4
4
  import { useSubscription as r } from "@voltro/client";
5
5
  //#region src/web.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voltro/plugin-flags",
3
- "version": "0.85.0",
3
+ "version": "0.86.0",
4
4
  "description": "Feature flags — per-subject / per-tenant targeting, deterministic % rollouts, kill-switch. Gate mutations/queries/actions declaratively or guard in-handler; evaluate flags client-side for UI gating. Config-as-code (memory) or runtime-toggleable (postgres).",
5
5
  "keywords": [
6
6
  "voltro",
@@ -48,10 +48,10 @@
48
48
  "node": ">=24.0.0"
49
49
  },
50
50
  "dependencies": {
51
- "@voltro/client": "0.85.0",
52
- "@voltro/database": "0.85.0",
53
- "@voltro/logger": "0.85.0",
54
- "@voltro/protocol": "0.85.0"
51
+ "@voltro/client": "0.86.0",
52
+ "@voltro/database": "0.86.0",
53
+ "@voltro/logger": "0.86.0",
54
+ "@voltro/protocol": "0.86.0"
55
55
  },
56
56
  "peerDependencies": {
57
57
  "effect": "4.0.1"