@voltro/runtime 0.85.0 → 0.87.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.
Files changed (184) hide show
  1. package/CHANGELOG.md +5987 -0
  2. package/README.md +43 -0
  3. package/THIRD-PARTY-NOTICES.md +864 -253
  4. package/dist/aggregate.js +42 -0
  5. package/dist/aggregateExtrema.js +80 -0
  6. package/dist/aggregateIvm.js +115 -0
  7. package/dist/analyticsMirror.js +228 -0
  8. package/dist/analyticsSink.js +32 -0
  9. package/dist/apiKeyUsage.js +33 -0
  10. package/dist/apiKeys.js +227 -0
  11. package/dist/apiKeysWiring.js +114 -0
  12. package/dist/apiRoleBucketSampler.js +64 -0
  13. package/dist/apiRoleWorkloadHistory.js +56 -0
  14. package/dist/apiRoleWorkloadMetrics.js +78 -0
  15. package/dist/apiRoleWorkloadStorage.js +95 -0
  16. package/dist/approvalBuiltins.js +10 -0
  17. package/dist/approvals.js +262 -0
  18. package/dist/authMiddlewareLayer.js +48 -0
  19. package/dist/bindEvent.js +92 -0
  20. package/dist/bootListener.js +143 -0
  21. package/dist/bootRefusal.js +7 -0
  22. package/dist/bootStoreCodec.js +71 -0
  23. package/dist/businessRules.js +67 -0
  24. package/dist/changeClaimsTable.js +12 -0
  25. package/dist/circuitBreaker.js +1 -0
  26. package/dist/columnOnUpdate.js +23 -0
  27. package/dist/composeAnalytics.js +129 -0
  28. package/dist/computedQuery.js +4 -0
  29. package/dist/computedQueryCache.js +231 -0
  30. package/dist/connectionBuiltins.js +81 -0
  31. package/dist/connectionCredentials.js +66 -0
  32. package/dist/connectionState.js +34 -0
  33. package/dist/connectionVault.js +410 -0
  34. package/dist/connectionsFacade.js +56 -0
  35. package/dist/coordinatedSchedule.js +145 -0
  36. package/dist/crdtHistory.js +138 -0
  37. package/dist/crdtMerge.js +179 -0
  38. package/dist/crdtVersionsTable.js +25 -0
  39. package/dist/credentialBound.js +12 -0
  40. package/dist/crud.js +72 -0
  41. package/dist/dataStore.js +712 -0
  42. package/dist/dataloader.js +61 -0
  43. package/dist/declaredTablesGuard.js +49 -0
  44. package/dist/defineExecutor.js +4 -0
  45. package/dist/dependencyGraph.js +32 -0
  46. package/dist/devices.js +228 -0
  47. package/dist/devicesWiring.js +168 -0
  48. package/dist/dialectReplicationAdapter.js +29 -0
  49. package/dist/dispatcher.js +522 -0
  50. package/dist/effectStore.js +71 -0
  51. package/dist/engineClients.js +24 -0
  52. package/dist/eventBus.js +260 -0
  53. package/dist/eventPublish.js +49 -0
  54. package/dist/events.js +193 -0
  55. package/dist/expectation.js +52 -0
  56. package/dist/expectationEvaluator.js +308 -0
  57. package/dist/experiment.js +96 -0
  58. package/dist/experimentEvaluator.js +245 -0
  59. package/dist/externalizedTasks.js +38 -0
  60. package/dist/fieldCipher.js +130 -0
  61. package/dist/fieldKeyRegistry.js +295 -0
  62. package/dist/fieldKeyRekey.js +286 -0
  63. package/dist/fieldKeyScan.js +136 -0
  64. package/dist/fieldKeyring.js +77 -0
  65. package/dist/fingerprint.js +21 -0
  66. package/dist/finops.js +42 -0
  67. package/dist/finopsAccountant.js +216 -0
  68. package/dist/handlerBody.js +8 -0
  69. package/dist/healthProbes.js +55 -0
  70. package/dist/hot.d.ts +56 -0
  71. package/dist/hot.js +3 -0
  72. package/dist/hotDataStore.js +148 -0
  73. package/dist/hotDataStoreSnapshot.js +98 -0
  74. package/dist/hotDataStoreTables.js +18 -0
  75. package/dist/http-range.d.ts +53 -0
  76. package/dist/http-range.js +52 -0
  77. package/dist/httpClientSsrf.js +24 -0
  78. package/dist/httpClientTracing.js +14 -0
  79. package/dist/httpCompression.js +76 -0
  80. package/dist/httpInputStream.js +82 -0
  81. package/dist/httpKeepAlive.js +12 -0
  82. package/dist/httpRouteMounts.js +48 -0
  83. package/dist/idempotencyStore.js +110 -0
  84. package/dist/idleDetector.js +21 -0
  85. package/dist/index.d.ts +1249 -517
  86. package/dist/index.js +155 -15292
  87. package/dist/insertRequiredColumns.js +19 -0
  88. package/dist/inspectWorkflow.js +1 -0
  89. package/dist/instanceMembership.js +98 -0
  90. package/dist/ivm.js +125 -0
  91. package/dist/kvDataStore.js +86 -0
  92. package/dist/kvFacade.js +38 -0
  93. package/dist/matcher.js +304 -0
  94. package/dist/memoryColumnDefaults.js +11 -0
  95. package/dist/memoryQueryJoins.js +114 -0
  96. package/dist/memorySetOperations.js +43 -0
  97. package/dist/metrics.js +175 -0
  98. package/dist/observability.js +123 -0
  99. package/dist/observedGraph.js +52 -0
  100. package/dist/originGuard.js +116 -0
  101. package/dist/outbox.js +352 -0
  102. package/dist/pluginRefRules.js +65 -0
  103. package/dist/policyGuardResolver.js +55 -0
  104. package/dist/preview.js +62 -0
  105. package/dist/projectedRows.js +12 -0
  106. package/dist/provenance.js +63 -0
  107. package/dist/rangeIndex.js +89 -0
  108. package/dist/rawReadScope.js +17 -0
  109. package/dist/reaction.js +85 -0
  110. package/dist/reactiveConfig.js +11 -0
  111. package/dist/readClassification.js +57 -0
  112. package/dist/readRecorder.js +67 -0
  113. package/dist/readScopePredicate.js +12 -0
  114. package/dist/rebac.js +104 -0
  115. package/dist/rebacInterceptor.js +81 -0
  116. package/dist/registry.js +29 -0
  117. package/dist/relevantFields.js +65 -0
  118. package/dist/replicaHealth.js +42 -0
  119. package/dist/replicaLocality.js +44 -0
  120. package/dist/replicaObservations.js +121 -0
  121. package/dist/replicaSelection.js +32 -0
  122. package/dist/replicatedDataStore.js +257 -0
  123. package/dist/requestLoader.js +7 -0
  124. package/dist/resumeCensus.js +28 -0
  125. package/dist/revalidation.js +22 -0
  126. package/dist/rowFilter.js +82 -0
  127. package/dist/rowStateBytes.js +45 -0
  128. package/dist/rpcBodyCap.js +63 -0
  129. package/dist/rpcConnections.js +83 -0
  130. package/dist/rpcDefectDiagnostics.js +15 -0
  131. package/dist/rpcServer.js +1065 -0
  132. package/dist/rpcWorkloadAdmission.js +50 -0
  133. package/dist/runAsSystem.js +23 -0
  134. package/dist/rywPositionStore.js +119 -0
  135. package/dist/schedule.js +23 -0
  136. package/dist/scheduleBackfillRequests.js +151 -0
  137. package/dist/scheduleClaimsTable.js +11 -0
  138. package/dist/scheduleRunsTable.js +37 -0
  139. package/dist/scheduler.js +840 -0
  140. package/dist/schemaRegistry.js +65 -0
  141. package/dist/secretsResolver.js +35 -0
  142. package/dist/securityHeaders.js +33 -0
  143. package/dist/servePipeline.js +374 -0
  144. package/dist/serverOnlyAudit.js +66 -0
  145. package/dist/serverTracer.js +15 -0
  146. package/dist/serviceCredentials.js +40 -0
  147. package/dist/setReadScopes.js +28 -0
  148. package/dist/singleNodeOrchestrator.js +91 -0
  149. package/dist/snapshotValueCodec.js +46 -0
  150. package/dist/sourceResolution.js +52 -0
  151. package/dist/storeMiddleware.js +882 -0
  152. package/dist/storeQuery.js +204 -0
  153. package/dist/storeRead.js +10 -0
  154. package/dist/subscriber.js +8 -0
  155. package/dist/subscriptionOutbox.js +139 -0
  156. package/dist/subscriptionRegistry.js +82 -0
  157. package/dist/subscriptionResume.js +132 -0
  158. package/dist/subscriptionSocketMetrics.js +65 -0
  159. package/dist/systemSubject.js +5 -0
  160. package/dist/tenantScope.js +11 -0
  161. package/dist/tenantSelection.js +27 -0
  162. package/dist/timeline.js +105 -0
  163. package/dist/traceContext.js +5 -0
  164. package/dist/traceSink.js +63 -0
  165. package/dist/traceSpansTable.js +29 -0
  166. package/dist/traceparent.js +23 -0
  167. package/dist/transactionSubject.js +12 -0
  168. package/dist/trustedProxy.js +155 -0
  169. package/dist/undo.js +129 -0
  170. package/dist/undoBuiltins.js +69 -0
  171. package/dist/undoCapture.js +74 -0
  172. package/dist/undoLogTable.js +16 -0
  173. package/dist/upsertBinding.js +20 -0
  174. package/dist/wakeOrchestrator.js +34 -0
  175. package/dist/wakeRegistry.js +71 -0
  176. package/dist/wakeRouter.js +63 -0
  177. package/dist/wakeSupervisor.js +62 -0
  178. package/dist/wakeupsTable.js +12 -0
  179. package/dist/webSocketGatewayRuntime.js +154 -0
  180. package/dist/webhookVerification.js +18 -0
  181. package/dist/wireColumnStrip.js +84 -0
  182. package/dist/workflowReadBuiltins.js +87 -0
  183. package/dist/workflowRuntime.js +708 -0
  184. package/package.json +19 -7
@@ -0,0 +1,216 @@
1
+ //#region src/finopsAccountant.ts
2
+ var e = class {
3
+ total = 0;
4
+ byUnit = /* @__PURE__ */ new Map();
5
+ bySubscription = /* @__PURE__ */ new Map();
6
+ lastUpdatedAt = null;
7
+ add(e, t, n) {
8
+ this.total += t, this.byUnit.set(e.unit, (this.byUnit.get(e.unit) ?? 0) + t);
9
+ let r = e.subscriptionId;
10
+ r != null && r.length > 0 && this.bySubscription.set(r, (this.bySubscription.get(r) ?? 0) + t), this.lastUpdatedAt = e.at ?? n;
11
+ }
12
+ state(e) {
13
+ return {
14
+ tenantId: e,
15
+ total: this.total,
16
+ byUnit: Object.fromEntries(this.byUnit),
17
+ bySubscription: Object.fromEntries(this.bySubscription),
18
+ lastUpdatedAt: this.lastUpdatedAt
19
+ };
20
+ }
21
+ }, t = {
22
+ ok: 0,
23
+ warn: 1,
24
+ exceeded: 2
25
+ }, n = class {
26
+ def;
27
+ tenantId;
28
+ spent = 0;
29
+ windowStart = null;
30
+ status = "ok";
31
+ since = null;
32
+ lastUpdatedAt = null;
33
+ lastCause = null;
34
+ constructor(e, t) {
35
+ this.def = e, this.tenantId = t;
36
+ }
37
+ get warnThreshold() {
38
+ return this.def.warnAt * this.def.limit;
39
+ }
40
+ statusOf(e) {
41
+ return e >= this.def.limit ? "exceeded" : e >= this.warnThreshold ? "warn" : "ok";
42
+ }
43
+ rollWindow(e) {
44
+ if (this.def.windowMs === null) return null;
45
+ if (this.windowStart === null) return this.windowStart = e, null;
46
+ let t = e - this.windowStart;
47
+ return t < this.def.windowMs ? null : (this.windowStart = e - t % this.def.windowMs, this.spent = 0, this.settle("ok", e, null));
48
+ }
49
+ settle(e, n, r) {
50
+ let i = this.status;
51
+ return e === i ? null : (this.status = e, this.since = n, t[e] > t[i] ? (this.lastCause = r, {
52
+ kind: e === "exceeded" ? "exceeded" : "warn",
53
+ state: this.state(),
54
+ cause: r
55
+ }) : {
56
+ kind: "recovered",
57
+ state: this.state(),
58
+ cause: null
59
+ });
60
+ }
61
+ apply(e, t, n) {
62
+ let i = [], a = this.rollWindow(n);
63
+ a !== null && i.push(a), this.spent += t, this.lastUpdatedAt = e.at ?? n;
64
+ let o = this.settle(this.statusOf(this.spent), n, r(e));
65
+ return o !== null && i.push(o), i;
66
+ }
67
+ tick(e) {
68
+ return this.rollWindow(e);
69
+ }
70
+ reset(e) {
71
+ return this.spent = 0, this.windowStart = this.def.windowMs === null ? null : e, this.lastUpdatedAt = e, this.settle("ok", e, null);
72
+ }
73
+ state() {
74
+ return {
75
+ budget: this.def.name,
76
+ tenantId: this.tenantId,
77
+ status: this.status,
78
+ severity: this.def.severity,
79
+ onExceeded: this.def.onExceeded,
80
+ spent: this.spent,
81
+ limit: this.def.limit,
82
+ warnThreshold: this.warnThreshold,
83
+ unit: this.def.unit,
84
+ windowStartedAt: this.windowStart,
85
+ since: this.since,
86
+ lastUpdatedAt: this.lastUpdatedAt,
87
+ lastCause: this.lastCause,
88
+ ...this.def.description === void 0 ? {} : { description: this.def.description }
89
+ };
90
+ }
91
+ }, r = (e) => ({
92
+ unit: e.unit,
93
+ subscriptionId: e.subscriptionId ?? null,
94
+ procedure: e.procedure ?? null,
95
+ traceId: e.traceId ?? null
96
+ }), i = (e) => Number.isFinite(e) && e > 0 ? e : 0, a = class {
97
+ now;
98
+ attribution = /* @__PURE__ */ new Map();
99
+ counters = /* @__PURE__ */ new Map();
100
+ budgetsByName = /* @__PURE__ */ new Map();
101
+ totalBudgets = [];
102
+ unitBudgets = /* @__PURE__ */ new Map();
103
+ constructor(e, t = {}) {
104
+ this.now = t.now ?? Date.now;
105
+ for (let t of e) {
106
+ if (this.budgetsByName.has(t.name)) throw Error(`cost budget: duplicate name '${t.name}' — names must be unique across all cost budgets`);
107
+ if (this.budgetsByName.set(t.name, t), this.counters.set(t.name, /* @__PURE__ */ new Map()), t.unit === null) this.totalBudgets.push(t);
108
+ else {
109
+ let e = this.unitBudgets.get(t.unit) ?? [];
110
+ e.push(t), this.unitBudgets.set(t.unit, e);
111
+ }
112
+ }
113
+ }
114
+ counterFor(e, t) {
115
+ let r = this.counters.get(e.name), i = r.get(t);
116
+ return i === void 0 && (i = new n(e, t), r.set(t, i)), i;
117
+ }
118
+ record(t) {
119
+ let n = i(t.amount);
120
+ if (n === 0) return [];
121
+ let r = t.at ?? this.now(), a = this.attribution.get(t.tenantId);
122
+ a === void 0 && (a = new e(), this.attribution.set(t.tenantId, a)), a.add(t, n, r);
123
+ let o = this.unitBudgets.get(t.unit);
124
+ if (this.totalBudgets.length === 0 && o === void 0) return [];
125
+ let s = [];
126
+ for (let e of this.totalBudgets) s.push(...this.counterFor(e, t.tenantId).apply(t, n, r));
127
+ if (o !== void 0) for (let e of o) s.push(...this.counterFor(e, t.tenantId).apply(t, n, r));
128
+ return s;
129
+ }
130
+ tick() {
131
+ let e = this.now(), t = [];
132
+ for (let n of this.budgetsByName.values()) if (n.windowMs !== null) for (let r of this.counters.get(n.name).values()) {
133
+ let n = r.tick(e);
134
+ n !== null && t.push(n);
135
+ }
136
+ return t;
137
+ }
138
+ reset(e) {
139
+ let t = this.now(), n = [], r = (e) => {
140
+ this.attribution.delete(e);
141
+ for (let r of this.counters.values()) {
142
+ let i = r.get(e);
143
+ if (i !== void 0) {
144
+ let e = i.reset(t);
145
+ e !== null && n.push(e);
146
+ }
147
+ }
148
+ };
149
+ if (e === void 0) for (let e of /* @__PURE__ */ new Set([...this.attribution.keys(), ...this.allCounterTenants()])) r(e);
150
+ else r(e);
151
+ return n;
152
+ }
153
+ allCounterTenants() {
154
+ let e = /* @__PURE__ */ new Set();
155
+ for (let t of this.counters.values()) for (let n of t.keys()) e.add(n);
156
+ return e;
157
+ }
158
+ attributionSnapshot() {
159
+ let e = Array(this.attribution.size), t = 0;
160
+ return this.attribution.forEach((n, r) => {
161
+ e[t++] = n.state(r);
162
+ }), e;
163
+ }
164
+ tenantState(e) {
165
+ return this.attribution.get(e)?.state(e) ?? null;
166
+ }
167
+ budgetStates() {
168
+ let e = [];
169
+ for (let t of this.counters.values()) for (let n of t.values()) e.push(n.state());
170
+ return e;
171
+ }
172
+ budgetState(e, t) {
173
+ return this.counters.get(e)?.get(t)?.state() ?? null;
174
+ }
175
+ breaches() {
176
+ return this.budgetStates().filter((e) => e.status !== "ok");
177
+ }
178
+ }, o = (e) => {
179
+ let t = e.now ?? Date.now, n = e.log, r = /* @__PURE__ */ new Set(), i = e.discovered.map((e) => e.definition), o = new a(i, {
180
+ now: t,
181
+ ...n === void 0 ? {} : { log: n }
182
+ }), s = i.some((e) => e.windowMs !== null), c = (e) => {
183
+ for (let t of e) for (let e of r) try {
184
+ e(t);
185
+ } catch (e) {
186
+ n.warn("finops: budget signal listener threw", { budget: t.state.budget }, e);
187
+ }
188
+ }, l = (e) => {
189
+ try {
190
+ c(o.record(e));
191
+ } catch (t) {
192
+ n.warn("finops: cost record failed", { unit: e.unit }, t);
193
+ }
194
+ }, u = () => {
195
+ s && c(o.tick());
196
+ }, d = null;
197
+ return s && (d = setInterval(u, e.tickIntervalMs ?? 3e4), d.unref?.()), {
198
+ registry: {
199
+ attribution: () => o.attributionSnapshot(),
200
+ tenant: (e) => o.tenantState(e),
201
+ budgets: () => o.budgetStates(),
202
+ budget: (e, t) => o.budgetState(e, t),
203
+ breaches: () => o.breaches(),
204
+ subscribe: (e) => (r.add(e), () => r.delete(e))
205
+ },
206
+ record: l,
207
+ reset: (e) => c(o.reset(e)),
208
+ detach: () => {
209
+ d !== null && clearInterval(d), r.clear();
210
+ },
211
+ registered: i.map((e) => e.name),
212
+ runTick: u
213
+ };
214
+ };
215
+ //#endregion
216
+ export { a as CostAccountant, o as attachFinops };
@@ -0,0 +1,8 @@
1
+ import { Effect as e } from "effect";
2
+ //#region src/handlerBody.ts
3
+ function t(t, n) {
4
+ if (e.isEffect(t)) return n ? n(t) : e.runPromise(t);
5
+ if (t instanceof Promise) return t;
6
+ }
7
+ //#endregion
8
+ export { t as settleHandlerBody };
@@ -0,0 +1,55 @@
1
+ //#region src/healthProbes.ts
2
+ var e = "/internal", t = (t, n) => ({
3
+ path: (t?.path ?? e).replace(/\/+$/, "") || e,
4
+ liveness: t?.liveness ?? (() => !0),
5
+ readiness: t?.readiness ?? n
6
+ }), n = (e, t) => {
7
+ if (!t.startsWith(`${e.path}/`)) return;
8
+ let n = t.slice(e.path.length + 1);
9
+ return n === "liveness" || n === "readiness" || n === "startup" ? n : void 0;
10
+ }, r = async (e, t, n) => {
11
+ if (t === "liveness") try {
12
+ return await e.liveness() ? {
13
+ status: 200,
14
+ body: "ok",
15
+ contentType: "text/plain"
16
+ } : {
17
+ status: 503,
18
+ body: "not-live",
19
+ contentType: "text/plain"
20
+ };
21
+ } catch {
22
+ return {
23
+ status: 503,
24
+ body: "not-live",
25
+ contentType: "text/plain"
26
+ };
27
+ }
28
+ if (t === "startup" && n !== void 0) {
29
+ let e = n();
30
+ return {
31
+ status: e.ready ? 200 : 503,
32
+ body: JSON.stringify(e),
33
+ contentType: "application/json"
34
+ };
35
+ }
36
+ try {
37
+ return await e.readiness() ? {
38
+ status: 200,
39
+ body: "ready",
40
+ contentType: "text/plain"
41
+ } : {
42
+ status: 503,
43
+ body: "not-ready",
44
+ contentType: "text/plain"
45
+ };
46
+ } catch {
47
+ return {
48
+ status: 503,
49
+ body: "not-ready",
50
+ contentType: "text/plain"
51
+ };
52
+ }
53
+ };
54
+ //#endregion
55
+ export { r as answerProbe, n as probeFor, t as resolveHealth };
package/dist/hot.d.ts ADDED
@@ -0,0 +1,56 @@
1
+ import { ColumnBuilder } from '@voltro/database';
2
+ import { DataStore } from '@voltro/database';
3
+ import { DialectId } from '@voltro/database';
4
+ import { FieldDefinitions } from '@voltro/database';
5
+ import { HotStoreShape } from '@voltro/kv/hot/contract';
6
+ import { Table } from '@voltro/database';
7
+
8
+ /** Small-deployment SQL backend. One database lock serializes complete batches.
9
+ * The lock row contains no application key, tenant identity or payload. All
10
+ * application rows, including sorted members, are physically removed on delete.
11
+ * SQL stores own transaction retries; callbacks perform no external side effects.
12
+ */
13
+ export declare const dataStoreHotStore: (store: DataStore, options: HotDataStoreOptions) => HotStoreShape;
14
+
15
+ export declare interface HotDataStoreOptions {
16
+ /** Actual SQL dialect: SQLite-family writer admission differs from row locks. */
17
+ readonly dialect: DialectId;
18
+ readonly namespace: string;
19
+ /** Trusted server/test time, sampled after acquiring the transaction lock. */
20
+ readonly now?: () => number;
21
+ }
22
+
23
+ export declare const hotDataStoreTables: readonly [ Table<"_voltro_hot_kv", FieldDefinitions<{
24
+ readonly id: ColumnBuilder<string, "id", boolean>;
25
+ readonly keyHash: ColumnBuilder<string, "text", boolean>;
26
+ readonly namespace: ColumnBuilder<string | null, "text", boolean>;
27
+ readonly key: ColumnBuilder<string | null, "text", boolean>;
28
+ readonly payload: ColumnBuilder<string | null, "text", boolean>;
29
+ readonly expiresAtMs: ColumnBuilder<number | null, "real", boolean>;
30
+ }>, false, never>, Table<"_voltro_hot_z", FieldDefinitions<{
31
+ readonly id: ColumnBuilder<string, "id", boolean>;
32
+ readonly keyHash: ColumnBuilder<string, "text", boolean>;
33
+ readonly memberHash: ColumnBuilder<string, "text", boolean>;
34
+ readonly member: ColumnBuilder<string, "text", boolean>;
35
+ readonly score: ColumnBuilder<number, "real", boolean>;
36
+ }>, false, never>];
37
+
38
+ /** Activated with the database HotStore; importing definitions performs no I/O. */
39
+ export declare const hotKvTable: Table<"_voltro_hot_kv", FieldDefinitions<{
40
+ readonly id: ColumnBuilder<string, "id", boolean>;
41
+ readonly keyHash: ColumnBuilder<string, "text", boolean>;
42
+ readonly namespace: ColumnBuilder<string | null, "text", boolean>;
43
+ readonly key: ColumnBuilder<string | null, "text", boolean>;
44
+ readonly payload: ColumnBuilder<string | null, "text", boolean>;
45
+ readonly expiresAtMs: ColumnBuilder<number | null, "real", boolean>;
46
+ }>, false, never>;
47
+
48
+ export declare const hotZTable: Table<"_voltro_hot_z", FieldDefinitions<{
49
+ readonly id: ColumnBuilder<string, "id", boolean>;
50
+ readonly keyHash: ColumnBuilder<string, "text", boolean>;
51
+ readonly memberHash: ColumnBuilder<string, "text", boolean>;
52
+ readonly member: ColumnBuilder<string, "text", boolean>;
53
+ readonly score: ColumnBuilder<number, "real", boolean>;
54
+ }>, false, never>;
55
+
56
+ export { }
package/dist/hot.js ADDED
@@ -0,0 +1,3 @@
1
+ import { hotDataStoreTables as e, hotKvTable as t, hotZTable as n } from "./hotDataStoreTables.js";
2
+ import { dataStoreHotStore as r } from "./hotDataStore.js";
3
+ export { r as dataStoreHotStore, e as hotDataStoreTables, t as hotKvTable, n as hotZTable };
@@ -0,0 +1,148 @@
1
+ import { decodeHotSnapshot as e, encodeHotSnapshot as t } from "./hotDataStoreSnapshot.js";
2
+ import { hotKvTable as n, hotZTable as r } from "./hotDataStoreTables.js";
3
+ import { Effect as i, Stream as a } from "effect";
4
+ import { and as o, eq as s, generateId as c, gt as l, isNull as u, isSqliteFamily as d, or as f, queryFor as p, startsWith as m } from "@voltro/database";
5
+ import { createHash as h } from "node:crypto";
6
+ import { HotError as g, applyHotOperations as _, compareHotMembers as v, hotStoreFromRunner as y, validateHotOp as b } from "@voltro/kv/hot/contract";
7
+ //#region src/hotDataStore.ts
8
+ var x = n.tableName, S = r.tableName, C = "lock", w = (e) => h("sha256").update(e).digest("hex"), T = (h, T) => {
9
+ let { namespace: E } = T;
10
+ if (!E || !E.isWellFormed() || /[{}]/.test(E)) throw new g({
11
+ operation: "connect",
12
+ key: "",
13
+ reason: "invalid"
14
+ });
15
+ let D = T.now ?? Date.now, O = (e) => w(JSON.stringify([E, e])), k, A = () => k ??= h.transactional(async (e) => {
16
+ let t = {
17
+ id: c({
18
+ kind: "typeid",
19
+ prefix: "hot"
20
+ }, x),
21
+ keyHash: C,
22
+ namespace: null,
23
+ key: null,
24
+ payload: null,
25
+ expiresAtMs: null
26
+ };
27
+ T.dialect === "mssql" ? await e.upsert(x, t, {
28
+ conflictColumns: ["keyHash"],
29
+ update: []
30
+ }) : await e.insertIgnore(x, t, { conflictColumns: ["keyHash"] });
31
+ }).catch((e) => {
32
+ throw k = void 0, e;
33
+ }), j = async (t, i) => {
34
+ let a = O(i), o = (await t.query(p(n).where(s("keyHash", a)).descriptor))[0];
35
+ if (o === void 0) return;
36
+ if (o.namespace !== E || o.key !== i || typeof o.payload != "string") throw new g({
37
+ operation: "read",
38
+ key: i,
39
+ reason: "unavailable"
40
+ });
41
+ let c = o.expiresAtMs;
42
+ if (c !== null && (typeof c != "number" || !Number.isFinite(c))) throw new g({
43
+ operation: "read",
44
+ key: i,
45
+ reason: "unavailable"
46
+ });
47
+ let l = await t.query(p(r).where(s("keyHash", a)).descriptor), u = [];
48
+ for (let e of l) {
49
+ let t = e.member, n = e.score;
50
+ if (typeof t != "string" || typeof n != "number" || !Number.isFinite(n)) throw new g({
51
+ operation: "read",
52
+ key: i,
53
+ reason: "unavailable"
54
+ });
55
+ u.push([t, n]);
56
+ }
57
+ return e(o.payload, c === null ? Infinity : c, u);
58
+ }, M = async (e, n, r) => {
59
+ let i = O(n);
60
+ if (await e.deleteMany(S, { where: s("keyHash", i) }), r === void 0) await e.deleteMany(x, { where: s("keyHash", i) });
61
+ else if (await e.upsert(x, {
62
+ id: c({
63
+ kind: "typeid",
64
+ prefix: "hot"
65
+ }, x),
66
+ keyHash: i,
67
+ namespace: E,
68
+ key: n,
69
+ payload: t(r),
70
+ expiresAtMs: r.expiresAt === Infinity ? null : r.expiresAt
71
+ }, {
72
+ conflictColumns: ["keyHash"],
73
+ update: [
74
+ "namespace",
75
+ "key",
76
+ "payload",
77
+ "expiresAtMs"
78
+ ]
79
+ }), r.kind === "z") for (let [t, n] of r.values) await e.insert(S, {
80
+ id: c({
81
+ kind: "typeid",
82
+ prefix: "hotz"
83
+ }, S),
84
+ keyHash: i,
85
+ memberHash: w(t),
86
+ member: t,
87
+ score: n
88
+ });
89
+ };
90
+ return y((e) => i.try({
91
+ try: () => {
92
+ e.forEach(b);
93
+ },
94
+ catch: (e) => e instanceof g ? e : new g({
95
+ operation: "multi",
96
+ key: "",
97
+ reason: "invalid"
98
+ })
99
+ }).pipe(i.flatMap(() => i.tryPromise({
100
+ try: async () => e.length === 0 ? [] : (await A(), h.transactional(async (t) => {
101
+ if (d(T.dialect) && await t.updateMany(x, { payload: null }, { where: s("keyHash", C) }), (await t.query(p(n).where(s("keyHash", C)).forUpdate().descriptor)).length !== 1) throw new g({
102
+ operation: "multi",
103
+ key: "",
104
+ reason: "unavailable"
105
+ });
106
+ let r = D();
107
+ if (!Number.isFinite(r)) throw new g({
108
+ operation: "multi",
109
+ key: "",
110
+ reason: "invalid"
111
+ });
112
+ let i = /* @__PURE__ */ new Map(), a = [...new Set(e.map((e) => e.key))];
113
+ for (let e of a) {
114
+ let n = await j(t, e);
115
+ n !== void 0 && i.set(e, n);
116
+ }
117
+ let o = new Map(i), c = _(i, e, r);
118
+ for (let e of a) i.get(e) !== o.get(e) && await M(t, e, i.get(e));
119
+ return c;
120
+ })),
121
+ catch: (e) => e instanceof g ? e : new g({
122
+ operation: "multi",
123
+ key: "",
124
+ reason: "unavailable"
125
+ })
126
+ }))), (e) => a.unwrap(i.tryPromise({
127
+ try: async () => {
128
+ if (!e.isWellFormed()) throw new g({
129
+ operation: "scanPrefix",
130
+ key: e,
131
+ reason: "invalid"
132
+ });
133
+ let t = D(), r = (await h.query(p(n).where(o(s("namespace", E), m("key", e), f(u("expiresAtMs"), l("expiresAtMs", t)))).descriptor)).flatMap((t) => t.namespace === E && typeof t.key == "string" && t.key.startsWith(e) && typeof t.payload == "string" ? [t.key] : []);
134
+ return a.fromIterable(r.sort(v));
135
+ },
136
+ catch: (t) => t instanceof g ? t : new g({
137
+ operation: "scanPrefix",
138
+ key: e,
139
+ reason: "unavailable"
140
+ })
141
+ })), {
142
+ atomicMulti: !0,
143
+ crossNode: !0,
144
+ persistent: !0
145
+ });
146
+ };
147
+ //#endregion
148
+ export { T as dataStoreHotStore };
@@ -0,0 +1,98 @@
1
+ import { Schema as e } from "effect";
2
+ //#region src/hotDataStoreSnapshot.ts
3
+ var t = e.Number.pipe(e.check(e.makeFilter(Number.isFinite))), n = e.Union([
4
+ e.Struct({
5
+ kind: e.Literal("fixed"),
6
+ windowStart: t,
7
+ count: t
8
+ }),
9
+ e.Struct({
10
+ kind: e.Literal("sliding"),
11
+ currStart: t,
12
+ currCount: t,
13
+ prevCount: t
14
+ }),
15
+ e.Struct({
16
+ kind: e.Literal("token"),
17
+ tokens: t,
18
+ lastRefill: t
19
+ })
20
+ ]), r = e.Union([
21
+ e.Struct({ kind: e.Literal("z") }),
22
+ e.Struct({
23
+ kind: e.Literal("h"),
24
+ values: e.Array(e.Tuple([e.String, e.String]))
25
+ }),
26
+ e.Struct({
27
+ kind: e.Literal("s"),
28
+ values: e.Array(e.String)
29
+ }),
30
+ e.Struct({
31
+ kind: e.Literal("counter"),
32
+ value: t
33
+ }),
34
+ e.Struct({
35
+ kind: e.Literal("lease"),
36
+ value: e.String
37
+ }),
38
+ e.Struct({
39
+ kind: e.Literal("bucket"),
40
+ tokens: t,
41
+ updatedAt: t
42
+ }),
43
+ e.Struct({
44
+ kind: e.Literal("rateLimit"),
45
+ state: n
46
+ })
47
+ ]), i = e.decodeUnknownSync(r), a = (e) => {
48
+ switch (e.kind) {
49
+ case "z": return JSON.stringify({ kind: "z" });
50
+ case "h":
51
+ case "s": return JSON.stringify({
52
+ kind: e.kind,
53
+ values: [...e.values]
54
+ });
55
+ case "counter": return JSON.stringify({
56
+ kind: e.kind,
57
+ value: e.value
58
+ });
59
+ case "lease": return JSON.stringify({
60
+ kind: e.kind,
61
+ value: e.value
62
+ });
63
+ case "bucket": return JSON.stringify({
64
+ kind: e.kind,
65
+ tokens: e.tokens,
66
+ updatedAt: e.updatedAt
67
+ });
68
+ case "rateLimit": return JSON.stringify({
69
+ kind: e.kind,
70
+ state: e.state
71
+ });
72
+ }
73
+ }, o = (e, t, n) => {
74
+ let r = i(JSON.parse(e));
75
+ switch (r.kind) {
76
+ case "z": return {
77
+ kind: "z",
78
+ values: new Map(n),
79
+ expiresAt: t
80
+ };
81
+ case "h": return {
82
+ kind: "h",
83
+ values: new Map(r.values),
84
+ expiresAt: t
85
+ };
86
+ case "s": return {
87
+ kind: "s",
88
+ values: new Set(r.values),
89
+ expiresAt: t
90
+ };
91
+ default: return {
92
+ ...r,
93
+ expiresAt: t
94
+ };
95
+ }
96
+ };
97
+ //#endregion
98
+ export { o as decodeHotSnapshot, a as encodeHotSnapshot };
@@ -0,0 +1,18 @@
1
+ import { id as e, real as t, table as n, text as r } from "@voltro/database";
2
+ //#region src/hotDataStoreTables.ts
3
+ var i = n("_voltro_hot_kv", {
4
+ id: e({ prefix: "hot" }),
5
+ keyHash: r().maxLength(64),
6
+ namespace: r().nullable(),
7
+ key: r().nullable(),
8
+ payload: r().nullable(),
9
+ expiresAtMs: t().nullable()
10
+ }).unique("hotKeyHash", ["keyHash"]).index(["expiresAtMs"]).nonReactive(), a = n("_voltro_hot_z", {
11
+ id: e({ prefix: "hotz" }),
12
+ keyHash: r().maxLength(64),
13
+ memberHash: r().maxLength(64),
14
+ member: r(),
15
+ score: t()
16
+ }).unique("hotMemberHash", ["keyHash", "memberHash"]).index(["keyHash", "score"]).nonReactive(), o = [i, a];
17
+ //#endregion
18
+ export { o as hotDataStoreTables, i as hotKvTable, a as hotZTable };
@@ -0,0 +1,53 @@
1
+ /** The outcome of a `Range` header against a representation of `size` bytes. */
2
+ export declare type ByteRangeRequest =
3
+ /** Absent, malformed, another unit, or several ranges: serve the whole thing. */
4
+ {
5
+ readonly _tag: 'ignored';
6
+ }
7
+ /** Syntactically valid, but no byte of it exists: 416 `bytes *\/size`. */
8
+ | {
9
+ readonly _tag: 'unsatisfiable';
10
+ }
11
+ /** Serve bytes `start..end`, both inclusive: 206 `bytes start-end/size`. */
12
+ | {
13
+ readonly _tag: 'satisfiable';
14
+ readonly start: number;
15
+ readonly end: number;
16
+ };
17
+
18
+ /**
19
+ * RFC 9110 §13.1.5: may a `Range` be honoured given this `If-Range`?
20
+ *
21
+ * `true` when there is no `If-Range`, or when it names the current
22
+ * representation by a STRONG validator: an identical strong entity tag, or
23
+ * the exact `Last-Modified` second — and only once that second is over
24
+ * (`nowMs`), because a file written twice within one second would otherwise
25
+ * keep a date that no longer names its bytes. A weak tag, an unparsable
26
+ * value, or a validator the representation does not have never matches: the
27
+ * client then gets the whole representation, never bytes of a different one.
28
+ */
29
+ export declare const ifRangeAllowsRange: (ifRange: string | undefined, current: RangeValidators, nowMs?: number) => boolean;
30
+
31
+ /**
32
+ * Resolve a `Range` header against a representation of `size` bytes
33
+ * (a non-negative safe integer, normally from `stat`).
34
+ */
35
+ export declare const parseByteRange: (header: string | undefined, size: number) => ByteRangeRequest;
36
+
37
+ /**
38
+ * An HTTP-date (RFC 9110 §5.6.7) in epoch milliseconds, `undefined` when the
39
+ * value is none. All three historical forms are accepted. asctime carries no
40
+ * zone and HTTP dates are always GMT, so it is read as GMT — `Date.parse`
41
+ * alone would read it in the server's local time zone.
42
+ */
43
+ export declare const parseHttpDate: (value: string) => number | undefined;
44
+
45
+ /** The current validators of the representation a range would be cut from. */
46
+ export declare interface RangeValidators {
47
+ /** The representation's entity tag as sent in `ETag`, quotes included. */
48
+ readonly etag?: string | undefined;
49
+ /** Last modification time in epoch milliseconds (as sent in `Last-Modified`). */
50
+ readonly lastModifiedMs?: number | undefined;
51
+ }
52
+
53
+ export { }