@lunora/do 1.0.0-alpha.44 → 1.0.0-alpha.45

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 (112) hide show
  1. package/dist/index.d.mts +55 -1
  2. package/dist/index.d.ts +55 -1
  3. package/dist/index.mjs +1 -44
  4. package/dist/packem_shared/ADMIN_FUNCTIONS-PxZ8Lr9e.mjs +1 -0
  5. package/dist/packem_shared/AGGREGATE_SQL_FUNCTION-QYaiuty4.mjs +1 -0
  6. package/dist/packem_shared/AUTH_METRICS_BUCKETS_TABLE-D0wNaez7.mjs +22 -0
  7. package/dist/packem_shared/CDC_LOG_TABLE-E_J5LPoK.mjs +8 -0
  8. package/dist/packem_shared/ConflictError-C8GtJmjS.mjs +1 -0
  9. package/dist/packem_shared/CountRlsUnsupportedError-Cl8XpYDL.mjs +1 -0
  10. package/dist/packem_shared/DATA_MIGRATION_STATE_TABLE-CaO6L0Ee.mjs +31 -0
  11. package/dist/packem_shared/DEFAULT_MAX_RELATION_KEYS-BRdmX7WW.mjs +1 -0
  12. package/dist/packem_shared/FUNCTION_METRICS_BUCKETS_TABLE-BPPeqM11.mjs +56 -0
  13. package/dist/packem_shared/GEO_DEFAULT_PRECISION-CWAm_oYY.mjs +1 -0
  14. package/dist/packem_shared/LogBuffer-bIvCelI-.mjs +1 -0
  15. package/dist/packem_shared/MAIL_RETENTION-KmozO2NQ.mjs +18 -0
  16. package/dist/packem_shared/MAX_SQL_ROWS-Bdu25ASB.mjs +2 -0
  17. package/dist/packem_shared/MIN_ADMIN_TOKEN_LENGTH-Ch6OW6_3.mjs +1 -0
  18. package/dist/packem_shared/NotFoundError-J3tjf4Uo.mjs +1 -0
  19. package/dist/packem_shared/NotUniqueError-BcP6GOem.mjs +1 -0
  20. package/dist/packem_shared/RANK_TIEBREAK-DtX8zQyc.mjs +1 -0
  21. package/dist/packem_shared/RLS_UNWRAP_SYMBOL-C6_WX3dG.mjs +1 -0
  22. package/dist/packem_shared/ROOT_DO_SIZE_WARN_BYTES-CP5wvJOW.mjs +101 -0
  23. package/dist/packem_shared/ReactiveCache-1_9Rs7J_.mjs +1 -0
  24. package/dist/packem_shared/SCAN_DEP-D_yR9EeV.mjs +1 -0
  25. package/dist/packem_shared/SESSION_DO_TTL_DEFAULT-GvBy_DBz.mjs +1 -0
  26. package/dist/packem_shared/SHARD_REGISTRY_DO_NAME-Caa0fR4N.mjs +1 -0
  27. package/dist/packem_shared/aggregateTableName-G-eXyjcz.mjs +1 -0
  28. package/dist/packem_shared/applyOnDelete-BvQN7pDL.mjs +1 -0
  29. package/dist/packem_shared/applySelect-Bq2KOrkL.mjs +1 -0
  30. package/dist/packem_shared/armRestore-BNzdvQ_o.mjs +1 -0
  31. package/dist/packem_shared/backfillAggregateIndexes-BAQ3Fwwh.mjs +1 -0
  32. package/dist/packem_shared/buildFtsMatch-CV0Z7PWv.mjs +1 -0
  33. package/dist/packem_shared/compileWhereSql-BLcfs4QW.mjs +1 -0
  34. package/dist/packem_shared/constant-time-equal-BVG05Guz.mjs +1 -0
  35. package/dist/packem_shared/context-telemetry-8eigmiNQ.mjs +1 -0
  36. package/dist/packem_shared/createMetrics-CyjLFXtA.mjs +1 -0
  37. package/dist/packem_shared/createSystemReader-DcDcrtM3.mjs +1 -0
  38. package/dist/packem_shared/ctx-db-idempotency-wiVoGnpQ.mjs +19 -0
  39. package/dist/packem_shared/ctx-db-shapes-DzX_H5q8.mjs +1 -0
  40. package/dist/packem_shared/diffExternalSource-DMpkJta1.mjs +1 -0
  41. package/dist/packem_shared/do-exec-BLe9lLrN.mjs +1 -0
  42. package/dist/packem_shared/do-sql-BYIQTG3z.mjs +1 -0
  43. package/dist/packem_shared/exportShardRows-kt42wijd.mjs +1 -0
  44. package/dist/packem_shared/hasTrigger-_rexbWMO.mjs +1 -0
  45. package/dist/packem_shared/isSoftDeleted-BvhQov04.mjs +9 -0
  46. package/dist/packem_shared/json-response-wrh9TBPw.mjs +1 -0
  47. package/dist/packem_shared/materializeExternalRows-BFmT9gsw.mjs +1 -0
  48. package/dist/packem_shared/renderSql-B5lF5Jd9.mjs +1 -0
  49. package/dist/packem_shared/runShardMigrations-bxOHpfID.mjs +5 -0
  50. package/dist/packem_shared/security-audit-BKUOgE0x.mjs +1 -0
  51. package/dist/packem_shared/selectExpiredIds-BGVP3d8-.mjs +1 -0
  52. package/dist/packem_shared/serialize-sql-DiRzL7A4.mjs +1 -0
  53. package/dist/packem_shared/serveRelationFanout-Ct5D2Tbk.mjs +1 -0
  54. package/dist/packem_shared/stableStringify-BjLh4gvA.mjs +1 -0
  55. package/dist/packem_shared/stableWireKey-YEHLaX6X.mjs +1 -0
  56. package/dist/packem_shared/subscriptionListDeltas-Bs69JbA8.mjs +1 -0
  57. package/dist/packem_shared/wire-codec-Ctnni0h6.mjs +1 -0
  58. package/package.json +1 -1
  59. package/dist/packem_shared/ADMIN_FUNCTIONS-CnZkbXH_.mjs +0 -374
  60. package/dist/packem_shared/AGGREGATE_SQL_FUNCTION-CQsu2Xga.mjs +0 -56
  61. package/dist/packem_shared/AUTH_METRICS_BUCKETS_TABLE-CiHHYeJi.mjs +0 -84
  62. package/dist/packem_shared/CDC_LOG_TABLE-uwOJxJJZ.mjs +0 -107
  63. package/dist/packem_shared/ConflictError-CLoq37xH.mjs +0 -12
  64. package/dist/packem_shared/CountRlsUnsupportedError-BGxj0pgS.mjs +0 -133
  65. package/dist/packem_shared/DATA_MIGRATION_STATE_TABLE-CYwBpyTr.mjs +0 -245
  66. package/dist/packem_shared/DEFAULT_MAX_RELATION_KEYS-BEan1CRD.mjs +0 -211
  67. package/dist/packem_shared/FUNCTION_METRICS_BUCKETS_TABLE-UDNVD7FS.mjs +0 -248
  68. package/dist/packem_shared/GEO_DEFAULT_PRECISION-BWnsNmpP.mjs +0 -114
  69. package/dist/packem_shared/LogBuffer-B_Ezju_N.mjs +0 -37
  70. package/dist/packem_shared/MAIL_RETENTION-CPpgl-dX.mjs +0 -104
  71. package/dist/packem_shared/MAX_SQL_ROWS-iFAA8FbD.mjs +0 -61
  72. package/dist/packem_shared/MIN_ADMIN_TOKEN_LENGTH-CCAvoFlr.mjs +0 -1
  73. package/dist/packem_shared/NotFoundError-C70b9hLw.mjs +0 -9
  74. package/dist/packem_shared/NotUniqueError-DbtlWwcG.mjs +0 -2031
  75. package/dist/packem_shared/RANK_TIEBREAK-CXhdcA1o.mjs +0 -91
  76. package/dist/packem_shared/RLS_UNWRAP_SYMBOL-DTvHvRzY.mjs +0 -141
  77. package/dist/packem_shared/ROOT_DO_SIZE_WARN_BYTES-B_zt4RQE.mjs +0 -7676
  78. package/dist/packem_shared/ReactiveCache-DnSvbjil.mjs +0 -233
  79. package/dist/packem_shared/SCAN_DEP-DLJF8dsj.mjs +0 -19
  80. package/dist/packem_shared/SESSION_DO_TTL_DEFAULT-BnSKgVO4.mjs +0 -169
  81. package/dist/packem_shared/SHARD_REGISTRY_DO_NAME-D99roc-r.mjs +0 -144
  82. package/dist/packem_shared/aggregateTableName-CxNqY1Sl.mjs +0 -64
  83. package/dist/packem_shared/applyOnDelete-BXSq3S70.mjs +0 -176
  84. package/dist/packem_shared/applySelect-WQY8m62C.mjs +0 -103
  85. package/dist/packem_shared/armRestore-4Px61hHS.mjs +0 -49
  86. package/dist/packem_shared/backfillAggregateIndexes-BGTwynwh.mjs +0 -81
  87. package/dist/packem_shared/buildFtsMatch-BLEMawrp.mjs +0 -38
  88. package/dist/packem_shared/compileWhereSql-DE6yfRcQ.mjs +0 -129
  89. package/dist/packem_shared/constant-time-equal-BVRWZgES.mjs +0 -12
  90. package/dist/packem_shared/context-telemetry-CDpyil58.mjs +0 -223
  91. package/dist/packem_shared/createMetrics-C4I2Yanj.mjs +0 -1
  92. package/dist/packem_shared/createSystemReader-D12eNH13.mjs +0 -82
  93. package/dist/packem_shared/ctx-db-idempotency-BdcNpvY4.mjs +0 -108
  94. package/dist/packem_shared/ctx-db-shapes-DTeFiHYS.mjs +0 -53
  95. package/dist/packem_shared/diffExternalSource-CovHfdyo.mjs +0 -44
  96. package/dist/packem_shared/do-exec-5eQy5cEi.mjs +0 -12
  97. package/dist/packem_shared/do-sql-CGAgiQUz.mjs +0 -88
  98. package/dist/packem_shared/exportShardRows-Dy3oFZ26.mjs +0 -157
  99. package/dist/packem_shared/hasTrigger-5N6_Fx0A.mjs +0 -20
  100. package/dist/packem_shared/isSoftDeleted-CFJmhjFP.mjs +0 -217
  101. package/dist/packem_shared/json-response-BdbtpOhm.mjs +0 -3
  102. package/dist/packem_shared/materializeExternalRows-BtEGs1Fv.mjs +0 -45
  103. package/dist/packem_shared/renderSql-D6eUcn2N.mjs +0 -16
  104. package/dist/packem_shared/runShardMigrations-BVax6rYu.mjs +0 -120
  105. package/dist/packem_shared/security-audit-CucgBice.mjs +0 -158
  106. package/dist/packem_shared/selectExpiredIds-C5W29Upb.mjs +0 -18
  107. package/dist/packem_shared/serialize-sql-BlRUoiQe.mjs +0 -14
  108. package/dist/packem_shared/serveRelationFanout-k7HTh3CC.mjs +0 -21
  109. package/dist/packem_shared/stableStringify-mC40mZts.mjs +0 -40
  110. package/dist/packem_shared/stableWireKey-DKuXO7T5.mjs +0 -6
  111. package/dist/packem_shared/subscriptionListDeltas-CT76bYny.mjs +0 -126
  112. package/dist/packem_shared/wire-codec-CzQc1pvf.mjs +0 -225
@@ -1,248 +0,0 @@
1
- const FUNCTION_METRICS_TABLE = "__lunora_metrics";
2
- const FUNCTION_METRICS_BUCKETS_TABLE = "__lunora_metrics_buckets";
3
- const FUNCTION_METRICS_SCANS_TABLE = "__lunora_metrics_scans";
4
- const FUNCTION_METRICS_INDEX_TABLE = "__lunora_metrics_index";
5
- const FUNCTION_METRICS_BUCKET_MS = 6e4;
6
- const FUNCTION_METRICS_BUCKET_RETENTION = 1440;
7
- const FUNCTION_METRICS_MAX_PATHS = 5e3;
8
- const FUNCTION_METRICS_READ_LIMIT = 1e3;
9
- const runSql = (sql, query, ...params) => {
10
- const runner = sql.exec;
11
- return runner.call(sql, query, ...params);
12
- };
13
- const bucketFloor = (ts) => Math.floor(ts / FUNCTION_METRICS_BUCKET_MS) * FUNCTION_METRICS_BUCKET_MS;
14
- const dedupeIndexHits = (hits) => {
15
- const seen = /* @__PURE__ */ new Map();
16
- for (const hit of hits) {
17
- seen.set(`${hit.table}\0${hit.index}`, { index: hit.index, table: hit.table });
18
- }
19
- return [...seen.values()];
20
- };
21
- const ensureFunctionMetricsTables = (sql) => {
22
- runSql(
23
- sql,
24
- `CREATE TABLE IF NOT EXISTS "${FUNCTION_METRICS_TABLE}" (
25
- path TEXT PRIMARY KEY,
26
- calls INTEGER NOT NULL DEFAULT 0,
27
- errors INTEGER NOT NULL DEFAULT 0,
28
- conflicts INTEGER NOT NULL DEFAULT 0,
29
- scans INTEGER NOT NULL DEFAULT 0,
30
- total_duration_ms REAL NOT NULL DEFAULT 0,
31
- min_duration_ms REAL,
32
- max_duration_ms REAL NOT NULL DEFAULT 0,
33
- last_called_at REAL NOT NULL DEFAULT 0,
34
- last_error_at REAL,
35
- last_error_message TEXT
36
- )`
37
- );
38
- for (const column of ["scans", "conflicts"]) {
39
- try {
40
- runSql(sql, `ALTER TABLE "${FUNCTION_METRICS_TABLE}" ADD COLUMN ${column} INTEGER NOT NULL DEFAULT 0`);
41
- } catch {
42
- }
43
- }
44
- runSql(
45
- sql,
46
- `CREATE TABLE IF NOT EXISTS "${FUNCTION_METRICS_BUCKETS_TABLE}" (
47
- path TEXT NOT NULL,
48
- bucket_ms INTEGER NOT NULL,
49
- calls INTEGER NOT NULL DEFAULT 0,
50
- errors INTEGER NOT NULL DEFAULT 0,
51
- PRIMARY KEY (path, bucket_ms)
52
- )`
53
- );
54
- runSql(
55
- sql,
56
- `CREATE TABLE IF NOT EXISTS "${FUNCTION_METRICS_SCANS_TABLE}" (
57
- path TEXT NOT NULL,
58
- table_name TEXT NOT NULL,
59
- scans INTEGER NOT NULL DEFAULT 0,
60
- PRIMARY KEY (path, table_name)
61
- )`
62
- );
63
- runSql(
64
- sql,
65
- `CREATE TABLE IF NOT EXISTS "${FUNCTION_METRICS_INDEX_TABLE}" (
66
- table_name TEXT NOT NULL,
67
- index_name TEXT NOT NULL,
68
- reads INTEGER NOT NULL DEFAULT 0,
69
- PRIMARY KEY (table_name, index_name)
70
- )`
71
- );
72
- };
73
- const recordFunctionMetric = (sql, input) => {
74
- ensureFunctionMetricsTables(sql);
75
- const pathCountRow = runSql(sql, `SELECT COUNT(*) AS n FROM "${FUNCTION_METRICS_TABLE}"`).one();
76
- if (pathCountRow.n >= FUNCTION_METRICS_MAX_PATHS) {
77
- const tracked = runSql(sql, `SELECT COUNT(*) AS c FROM "${FUNCTION_METRICS_TABLE}" WHERE path = ?`, input.path).one();
78
- if (tracked.c === 0) {
79
- return;
80
- }
81
- }
82
- const scannedTables = input.scannedTables ? [...new Set(input.scannedTables)] : [];
83
- const scanCount = scannedTables.length;
84
- const indexHits = dedupeIndexHits(input.indexHits ?? []);
85
- const errorCount = input.errored ? 1 : 0;
86
- const conflictCount = input.conflicted ? 1 : 0;
87
- const lastErrorAt = input.errored ? input.ts : null;
88
- const lastErrorMessage = input.errored ? input.errorMessage ?? null : null;
89
- runSql(
90
- sql,
91
- `INSERT INTO "${FUNCTION_METRICS_TABLE}"
92
- (path, calls, errors, conflicts, scans, total_duration_ms, min_duration_ms, max_duration_ms, last_called_at, last_error_at, last_error_message)
93
- VALUES (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?)
94
- ON CONFLICT(path) DO UPDATE SET
95
- calls = calls + 1,
96
- errors = errors + excluded.errors,
97
- conflicts = conflicts + excluded.conflicts,
98
- scans = scans + excluded.scans,
99
- total_duration_ms = total_duration_ms + excluded.total_duration_ms,
100
- min_duration_ms = MIN(COALESCE(min_duration_ms, excluded.min_duration_ms), excluded.min_duration_ms),
101
- max_duration_ms = MAX(max_duration_ms, excluded.max_duration_ms),
102
- last_called_at = excluded.last_called_at,
103
- last_error_at = CASE WHEN excluded.last_error_at IS NULL THEN last_error_at ELSE excluded.last_error_at END,
104
- last_error_message = CASE WHEN excluded.last_error_at IS NULL THEN last_error_message ELSE excluded.last_error_message END`,
105
- input.path,
106
- errorCount,
107
- conflictCount,
108
- scanCount,
109
- input.durationMs,
110
- input.durationMs,
111
- input.durationMs,
112
- input.ts,
113
- lastErrorAt,
114
- lastErrorMessage
115
- );
116
- const bucket = bucketFloor(input.ts);
117
- runSql(
118
- sql,
119
- `INSERT INTO "${FUNCTION_METRICS_BUCKETS_TABLE}" (path, bucket_ms, calls, errors)
120
- VALUES (?, ?, 1, ?)
121
- ON CONFLICT(path, bucket_ms) DO UPDATE SET
122
- calls = calls + 1,
123
- errors = errors + excluded.errors`,
124
- input.path,
125
- bucket,
126
- errorCount
127
- );
128
- runSql(
129
- sql,
130
- `DELETE FROM "${FUNCTION_METRICS_BUCKETS_TABLE}"
131
- WHERE path = ?
132
- AND bucket_ms <= (
133
- SELECT MAX(bucket_ms) - ? FROM "${FUNCTION_METRICS_BUCKETS_TABLE}" WHERE path = ?
134
- )`,
135
- input.path,
136
- FUNCTION_METRICS_BUCKET_RETENTION * FUNCTION_METRICS_BUCKET_MS,
137
- input.path
138
- );
139
- for (const table of scannedTables) {
140
- runSql(
141
- sql,
142
- `INSERT INTO "${FUNCTION_METRICS_SCANS_TABLE}" (path, table_name, scans)
143
- VALUES (?, ?, 1)
144
- ON CONFLICT(path, table_name) DO UPDATE SET
145
- scans = scans + 1`,
146
- input.path,
147
- table
148
- );
149
- }
150
- for (const hit of indexHits) {
151
- runSql(
152
- sql,
153
- `INSERT INTO "${FUNCTION_METRICS_INDEX_TABLE}" (table_name, index_name, reads)
154
- VALUES (?, ?, 1)
155
- ON CONFLICT(table_name, index_name) DO UPDATE SET
156
- reads = reads + 1`,
157
- hit.table,
158
- hit.index
159
- );
160
- }
161
- };
162
- const readFunctionMetricScans = (sql) => {
163
- ensureFunctionMetricsTables(sql);
164
- const rows = runSql(
165
- sql,
166
- // Bounded read: cap the materialized rows so a bloated attribution table
167
- // can't blow up DO memory. Highest-scan rows lead so the dominant
168
- // attributions survive the cut.
169
- `SELECT path, table_name, scans FROM "${FUNCTION_METRICS_SCANS_TABLE}" ORDER BY scans DESC, path ASC, table_name ASC LIMIT ${String(FUNCTION_METRICS_READ_LIMIT)}`
170
- ).toArray();
171
- const byPath = /* @__PURE__ */ new Map();
172
- for (const row of rows) {
173
- const list = byPath.get(row.path);
174
- const entry = { scans: row.scans, table: row.table_name };
175
- if (list === void 0) {
176
- byPath.set(row.path, [entry]);
177
- } else {
178
- list.push(entry);
179
- }
180
- }
181
- return byPath;
182
- };
183
- const readFunctionMetricIndexHits = (sql) => {
184
- ensureFunctionMetricsTables(sql);
185
- const rows = runSql(
186
- sql,
187
- `SELECT table_name, index_name, reads FROM "${FUNCTION_METRICS_INDEX_TABLE}" ORDER BY table_name ASC, index_name ASC`
188
- ).toArray();
189
- return rows.map((row) => {
190
- return { index: row.index_name, reads: row.reads, table: row.table_name };
191
- });
192
- };
193
- const mergeScanAttribution = (into, scanned) => {
194
- for (const table of scanned) {
195
- const entry = into.find((attribution) => attribution.table === table);
196
- if (entry === void 0) {
197
- into.push({ scans: 1, table });
198
- } else {
199
- entry.scans += 1;
200
- }
201
- }
202
- into.sort((a, b) => b.scans - a.scans || a.table.localeCompare(b.table));
203
- return into;
204
- };
205
- const readFunctionMetrics = (sql) => {
206
- ensureFunctionMetricsTables(sql);
207
- const scansByPath = readFunctionMetricScans(sql);
208
- const rows = runSql(sql, `SELECT * FROM "${FUNCTION_METRICS_TABLE}" ORDER BY last_called_at DESC LIMIT ${String(FUNCTION_METRICS_READ_LIMIT)}`).toArray();
209
- return rows.map((row) => {
210
- return {
211
- calls: row.calls,
212
- conflicts: row.conflicts,
213
- errors: row.errors,
214
- lastCalledAt: row.last_called_at,
215
- lastErrorAt: row.last_error_at,
216
- lastErrorMessage: row.last_error_message,
217
- maxDurationMs: row.max_duration_ms,
218
- path: row.path,
219
- scannedTables: scansByPath.get(row.path) ?? [],
220
- scans: row.scans,
221
- totalDurationMs: row.total_duration_ms
222
- };
223
- });
224
- };
225
- const readFunctionMetricBuckets = (sql, path) => {
226
- ensureFunctionMetricsTables(sql);
227
- const rows = path === void 0 ? runSql(
228
- sql,
229
- `SELECT path, bucket_ms, calls, errors FROM "${FUNCTION_METRICS_BUCKETS_TABLE}" ORDER BY bucket_ms ASC, path ASC`
230
- ).toArray() : runSql(
231
- sql,
232
- `SELECT path, bucket_ms, calls, errors FROM "${FUNCTION_METRICS_BUCKETS_TABLE}" WHERE path = ? ORDER BY bucket_ms ASC`,
233
- path
234
- ).toArray();
235
- return rows.map((row) => {
236
- return { bucketMs: row.bucket_ms, calls: row.calls, errors: row.errors, path: row.path };
237
- });
238
- };
239
- const readFunctionMetricsTotals = (sql) => {
240
- ensureFunctionMetricsTables(sql);
241
- const row = runSql(
242
- sql,
243
- `SELECT SUM(calls) AS requests, SUM(errors) AS errors FROM "${FUNCTION_METRICS_TABLE}"`
244
- ).one();
245
- return { errors: row.errors ?? 0, requests: row.requests ?? 0 };
246
- };
247
-
248
- export { FUNCTION_METRICS_BUCKETS_TABLE, FUNCTION_METRICS_BUCKET_MS, FUNCTION_METRICS_BUCKET_RETENTION, FUNCTION_METRICS_INDEX_TABLE, FUNCTION_METRICS_MAX_PATHS, FUNCTION_METRICS_READ_LIMIT, FUNCTION_METRICS_SCANS_TABLE, FUNCTION_METRICS_TABLE, ensureFunctionMetricsTables, mergeScanAttribution, readFunctionMetricBuckets, readFunctionMetricIndexHits, readFunctionMetricScans, readFunctionMetrics, readFunctionMetricsTotals, recordFunctionMetric };
@@ -1,114 +0,0 @@
1
- const BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz";
2
- const GEO_DEFAULT_PRECISION = 9;
3
- const EARTH_RADIUS_METERS = 63710088e-1;
4
- const CELL_WIDTH_METERS = [Number.POSITIVE_INFINITY, 5009400, 1252300, 156500, 39100, 4900, 1200, 152.9, 38.2, 4.77, 1.19, 0.149, 0.037];
5
- const clampPrecision = (precision) => Math.min(Math.max(Math.trunc(precision), 1), 12);
6
- const encodeGeohash = (point, precision) => {
7
- const chars = clampPrecision(precision);
8
- let latMin = -90;
9
- let latMax = 90;
10
- let lngMin = -180;
11
- let lngMax = 180;
12
- let hash = "";
13
- let bit = 0;
14
- let index = 0;
15
- let even = true;
16
- while (hash.length < chars) {
17
- if (even) {
18
- const mid = (lngMin + lngMax) / 2;
19
- if (point.lng >= mid) {
20
- index = index * 2 + 1;
21
- lngMin = mid;
22
- } else {
23
- index *= 2;
24
- lngMax = mid;
25
- }
26
- } else {
27
- const mid = (latMin + latMax) / 2;
28
- if (point.lat >= mid) {
29
- index = index * 2 + 1;
30
- latMin = mid;
31
- } else {
32
- index *= 2;
33
- latMax = mid;
34
- }
35
- }
36
- even = !even;
37
- if (bit < 4) {
38
- bit += 1;
39
- } else {
40
- hash += BASE32[index] ?? "0";
41
- bit = 0;
42
- index = 0;
43
- }
44
- }
45
- return hash;
46
- };
47
- const haversineMeters = (a, b) => {
48
- const toRadians = (degrees) => degrees * Math.PI / 180;
49
- const dLat = toRadians(b.lat - a.lat);
50
- const dLng = toRadians(b.lng - a.lng);
51
- const lat1 = toRadians(a.lat);
52
- const lat2 = toRadians(b.lat);
53
- const h = Math.sin(dLat / 2) ** 2 + Math.cos(lat1) * Math.cos(lat2) * Math.sin(dLng / 2) ** 2;
54
- return 2 * EARTH_RADIUS_METERS * Math.asin(Math.min(1, Math.sqrt(h)));
55
- };
56
- const precisionForRadius = (radiusMeters) => {
57
- for (let length = CELL_WIDTH_METERS.length - 1; length >= 1; length -= 1) {
58
- const width = CELL_WIDTH_METERS[length];
59
- if (width !== void 0 && width >= radiusMeters) {
60
- return length;
61
- }
62
- }
63
- return 1;
64
- };
65
- const NEIGHBOURS = {
66
- east: ["bc01fg45238967deuvhjyznpkmstqrwx", "bc01fg45238967deuvhjyznpkmstqrwx"],
67
- north: ["p0r21436x8zb9dcf5h7kjnmqesgutwvy", "bc01fg45238967deuvhjyznpkmstqrwx"],
68
- south: ["14365h7k9dcfesgujnmqp0r2twvyx8zb", "238967debc01fg45kmstqrwxuvhjyznp"],
69
- west: ["238967debc01fg45kmstqrwxuvhjyznp", "238967debc01fg45kmstqrwxuvhjyznp"]
70
- };
71
- const BORDERS = {
72
- east: ["bcfguvyz", "prxz"],
73
- north: ["prxz", "bcfguvyz"],
74
- south: ["028b", "0145hjnp"],
75
- west: ["0145hjnp", "028b"]
76
- };
77
- const adjacent = (hash, direction) => {
78
- const lower = hash.toLowerCase();
79
- const lastChar = lower.at(-1) ?? "";
80
- let base = lower.slice(0, -1);
81
- const type = lower.length % 2 === 0 ? 0 : 1;
82
- if (BORDERS[direction][type].includes(lastChar) && base !== "") {
83
- base = adjacent(base, direction);
84
- }
85
- return base + (BASE32[NEIGHBOURS[direction][type].indexOf(lastChar)] ?? "");
86
- };
87
- const coveringGeohashes = (center, radiusMeters) => {
88
- const precision = precisionForRadius(Math.max(radiusMeters, 1));
89
- const origin = encodeGeohash(center, precision);
90
- const north = adjacent(origin, "north");
91
- const south = adjacent(origin, "south");
92
- const cells = [
93
- origin,
94
- north,
95
- south,
96
- adjacent(origin, "east"),
97
- adjacent(origin, "west"),
98
- adjacent(north, "east"),
99
- adjacent(north, "west"),
100
- adjacent(south, "east"),
101
- adjacent(south, "west")
102
- ];
103
- return [...new Set(cells)];
104
- };
105
- const pointInBoundingBox = (point, box) => point.lat >= box.sw.lat && point.lat <= box.ne.lat && point.lng >= box.sw.lng && point.lng <= box.ne.lng;
106
- const boundingBoxCenter = (box) => {
107
- return { lat: (box.sw.lat + box.ne.lat) / 2, lng: (box.sw.lng + box.ne.lng) / 2 };
108
- };
109
- const boundingBoxGeohashes = (box) => {
110
- const center = boundingBoxCenter(box);
111
- return coveringGeohashes(center, Math.max(haversineMeters(center, box.ne), 1));
112
- };
113
-
114
- export { GEO_DEFAULT_PRECISION, boundingBoxCenter, boundingBoxGeohashes, coveringGeohashes, encodeGeohash, haversineMeters, pointInBoundingBox };
@@ -1,37 +0,0 @@
1
- const DEFAULT_CAPACITY = 500;
2
- class LogBuffer {
3
- /** Backing store, kept in insertion order (oldest first). */
4
- buffer = [];
5
- capacity;
6
- constructor(capacity = DEFAULT_CAPACITY) {
7
- this.capacity = capacity > 0 ? Math.trunc(capacity) : DEFAULT_CAPACITY;
8
- }
9
- /** Number of entries currently buffered. */
10
- get size() {
11
- return this.buffer.length;
12
- }
13
- /** Drop every buffered entry. */
14
- clear() {
15
- this.buffer.length = 0;
16
- }
17
- /**
18
- * Snapshot of the buffered entries, **newest first** so the panel renders
19
- * the most recent activity at the top without re-sorting. Returns a fresh
20
- * array each call; the caller may mutate it freely.
21
- */
22
- entries() {
23
- return this.buffer.toReversed();
24
- }
25
- /**
26
- * Append an entry, evicting the oldest when at capacity so the buffer never
27
- * grows past its bound.
28
- */
29
- push(entry) {
30
- this.buffer.push(entry);
31
- if (this.buffer.length > this.capacity) {
32
- this.buffer.shift();
33
- }
34
- }
35
- }
36
-
37
- export { LogBuffer };
@@ -1,104 +0,0 @@
1
- const MAIL_TABLE = "__lunora_mail";
2
- const MAIL_RETENTION = 500;
3
- const MAX_BODY_CHARS = 256 * 1024;
4
- const capBody = (value) => value !== void 0 && value.length > MAX_BODY_CHARS ? `${value.slice(0, MAX_BODY_CHARS)}
5
- … [truncated by the dev mail catcher]` : value;
6
- const runSql = (sql, query, ...params) => {
7
- const runner = sql.exec;
8
- return runner.call(sql, query, ...params);
9
- };
10
- const orNull = (value) => (
11
- // eslint-disable-next-line unicorn/no-null -- SQL NULL is the correct value for an absent column.
12
- value ?? null
13
- );
14
- const encode = (value) => {
15
- if (value === void 0) {
16
- return null;
17
- }
18
- return JSON.stringify(value);
19
- };
20
- const decode = (value) => {
21
- if (value === null || value === void 0 || value === "") {
22
- return void 0;
23
- }
24
- try {
25
- return JSON.parse(value);
26
- } catch {
27
- return void 0;
28
- }
29
- };
30
- const ensureMailTable = (sql) => {
31
- runSql(
32
- sql,
33
- `CREATE TABLE IF NOT EXISTS "${MAIL_TABLE}" (
34
- id TEXT PRIMARY KEY,
35
- captured_at INTEGER NOT NULL,
36
- from_addr TEXT,
37
- to_addrs TEXT NOT NULL,
38
- cc_addrs TEXT,
39
- bcc_addrs TEXT,
40
- reply_to TEXT,
41
- subject TEXT NOT NULL,
42
- html TEXT,
43
- body_text TEXT,
44
- headers TEXT
45
- )`
46
- );
47
- };
48
- const recordCapturedMail = (sql, input, capturedAt) => {
49
- ensureMailTable(sql);
50
- const id = crypto.randomUUID();
51
- runSql(
52
- sql,
53
- `INSERT INTO "${MAIL_TABLE}" (id, captured_at, from_addr, to_addrs, cc_addrs, bcc_addrs, reply_to, subject, html, body_text, headers)
54
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
55
- id,
56
- capturedAt,
57
- orNull(input.from),
58
- JSON.stringify(input.to),
59
- encode(input.cc),
60
- encode(input.bcc),
61
- orNull(input.replyTo),
62
- input.subject,
63
- orNull(capBody(input.html)),
64
- orNull(capBody(input.text)),
65
- encode(input.headers)
66
- );
67
- runSql(
68
- sql,
69
- `DELETE FROM "${MAIL_TABLE}"
70
- WHERE id NOT IN (
71
- SELECT id FROM "${MAIL_TABLE}" ORDER BY captured_at DESC, id DESC LIMIT ?
72
- )`,
73
- MAIL_RETENTION
74
- );
75
- return { id };
76
- };
77
- const readCapturedMail = (sql, options = {}) => {
78
- ensureMailTable(sql);
79
- const limit = Math.min(Math.max(options.limit ?? 50, 1), MAIL_RETENTION);
80
- const rows = runSql(sql, `SELECT * FROM "${MAIL_TABLE}" ORDER BY captured_at DESC, id DESC LIMIT ?`, limit).toArray();
81
- const entries = rows.map((row) => {
82
- return {
83
- bcc: decode(row.bcc_addrs),
84
- capturedAt: row.captured_at,
85
- cc: decode(row.cc_addrs),
86
- from: row.from_addr ?? void 0,
87
- headers: decode(row.headers),
88
- html: row.html ?? void 0,
89
- id: row.id,
90
- replyTo: row.reply_to ?? void 0,
91
- subject: row.subject,
92
- text: row.body_text ?? void 0,
93
- to: decode(row.to_addrs) ?? row.to_addrs
94
- };
95
- });
96
- return { entries };
97
- };
98
- const clearCapturedMail = (sql) => {
99
- ensureMailTable(sql);
100
- runSql(sql, `DELETE FROM "${MAIL_TABLE}"`);
101
- return { cleared: true };
102
- };
103
-
104
- export { MAIL_RETENTION, MAIL_TABLE, MAX_BODY_CHARS, clearCapturedMail, ensureMailTable, readCapturedMail, recordCapturedMail };
@@ -1,61 +0,0 @@
1
- import { LunoraError } from '@lunora/errors';
2
-
3
- const MAX_SQL_ROWS = 1e3;
4
- const READONLY_LEAD = /^(?:explain\s+(?:query\s+plan\s+)?)?(?:select|with)\b/iu;
5
- const FORBIDDEN_KEYWORD = /\b(?:alter|attach|create|delete|detach|drop|insert|pragma|reindex|replace|truncate|update|vacuum)\b/iu;
6
- const TRAILING_SEMICOLON = /;\s*$/u;
7
- const WHITESPACE = /\s/u;
8
- const skipLineComment = (sql, from) => {
9
- let index = from + 2;
10
- while (index < sql.length && sql[index] !== "\n") {
11
- index += 1;
12
- }
13
- return index;
14
- };
15
- const skipBlockComment = (sql, from) => {
16
- const close = sql.indexOf("*/", from + 2);
17
- return close === -1 ? -1 : close + 2;
18
- };
19
- const stripLeading = (sql) => {
20
- let index = 0;
21
- while (index < sql.length) {
22
- const char = sql[index];
23
- if (char !== void 0 && WHITESPACE.test(char)) {
24
- index += 1;
25
- } else if (char === "-" && sql[index + 1] === "-") {
26
- index = skipLineComment(sql, index);
27
- } else if (char === "/" && sql[index + 1] === "*") {
28
- const next = skipBlockComment(sql, index);
29
- if (next === -1) {
30
- break;
31
- }
32
- index = next;
33
- } else {
34
- break;
35
- }
36
- }
37
- return sql.slice(index);
38
- };
39
- const sqlError = (message, code) => new LunoraError(code, message, { status: 400 });
40
- const assertReadonly = (query) => {
41
- const trimmed = stripLeading(query).trim();
42
- if (trimmed === "") {
43
- throw sqlError("the query is empty", "SQL_EMPTY");
44
- }
45
- const single = trimmed.replace(TRAILING_SEMICOLON, "");
46
- if (single.includes(";")) {
47
- throw sqlError("only a single statement may be run", "SQL_MULTIPLE_STATEMENTS");
48
- }
49
- if (!READONLY_LEAD.test(single) || FORBIDDEN_KEYWORD.test(single)) {
50
- throw sqlError("the SQL editor is read-only — only SELECT / WITH / EXPLAIN queries are allowed", "SQL_NOT_READONLY");
51
- }
52
- };
53
- const runReadonlySql = (sql, query) => {
54
- assertReadonly(query);
55
- const all = sql.exec(query).toArray();
56
- const rows = all.length > MAX_SQL_ROWS ? all.slice(0, MAX_SQL_ROWS) : all;
57
- const columns = rows.length > 0 ? Object.keys(rows[0]) : [];
58
- return { columns, rowCount: all.length, rows, truncated: all.length > MAX_SQL_ROWS };
59
- };
60
-
61
- export { MAX_SQL_ROWS, assertReadonly, runReadonlySql };
@@ -1 +0,0 @@
1
- export { M as MIN_ADMIN_TOKEN_LENGTH, a as MIN_AUTH_SECRET_LENGTH, b as buildSecurityAudit } from './security-audit-CucgBice.mjs';
@@ -1,9 +0,0 @@
1
- import { LunoraError } from '@lunora/errors';
2
-
3
- class NotFoundError extends LunoraError {
4
- constructor(message = "Document not found") {
5
- super("NOT_FOUND", message, { name: "NotFoundError" });
6
- }
7
- }
8
-
9
- export { NotFoundError as default };