@lunora/do 0.0.0 → 1.0.0-alpha.10
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.
- package/LICENSE.md +105 -0
- package/README.md +115 -9
- package/__assets__/package-og.svg +14 -0
- package/dist/index.d.mts +6347 -0
- package/dist/index.d.ts +6347 -0
- package/dist/index.mjs +37 -0
- package/dist/packem_shared/ADMIN_FUNCTIONS-D_UiYJFk.mjs +316 -0
- package/dist/packem_shared/AGGREGATE_SQL_FUNCTION-CFk6adSu.mjs +54 -0
- package/dist/packem_shared/AUTH_METRICS_BUCKETS_TABLE-CiHHYeJi.mjs +84 -0
- package/dist/packem_shared/CDC_LOG_TABLE-DSycmnDf.mjs +107 -0
- package/dist/packem_shared/ConflictError-C0STs6bU.mjs +13 -0
- package/dist/packem_shared/CountRlsUnsupportedError-28ZvvwKS.mjs +133 -0
- package/dist/packem_shared/DATA_MIGRATION_STATE_TABLE-PTtTiQ7U.mjs +237 -0
- package/dist/packem_shared/DEFAULT_MAX_RELATION_KEYS-DU-Y4-LJ.mjs +209 -0
- package/dist/packem_shared/FUNCTION_METRICS_BUCKETS_TABLE-UDNVD7FS.mjs +248 -0
- package/dist/packem_shared/LogBuffer-B_Ezju_N.mjs +37 -0
- package/dist/packem_shared/MAIL_RETENTION-CPpgl-dX.mjs +104 -0
- package/dist/packem_shared/MAX_SQL_ROWS-dDcFE1YZ.mjs +29 -0
- package/dist/packem_shared/MIN_ADMIN_TOKEN_LENGTH-CCAvoFlr.mjs +1 -0
- package/dist/packem_shared/NotFoundError-CMuMZt81.mjs +10 -0
- package/dist/packem_shared/NotUniqueError-DZQtH02h.mjs +1835 -0
- package/dist/packem_shared/RANK_TIEBREAK-CXhdcA1o.mjs +91 -0
- package/dist/packem_shared/RLS_UNWRAP_SYMBOL-EtGQdC9d.mjs +132 -0
- package/dist/packem_shared/ROOT_DO_SIZE_WARN_BYTES-DKwBF3Jp.mjs +4996 -0
- package/dist/packem_shared/ReactiveCache-1hDydFyv.mjs +232 -0
- package/dist/packem_shared/SCAN_DEP-DLJF8dsj.mjs +19 -0
- package/dist/packem_shared/SESSION_DO_TTL_DEFAULT-ilPZsVwu.mjs +180 -0
- package/dist/packem_shared/SHARD_REGISTRY_DO_NAME-BsAbi5Mn.mjs +146 -0
- package/dist/packem_shared/aggregateTableName-CxNqY1Sl.mjs +64 -0
- package/dist/packem_shared/applyOnDelete-BQ-8ZlZ1.mjs +175 -0
- package/dist/packem_shared/applySelect-BvZdFUBT.mjs +101 -0
- package/dist/packem_shared/armRestore-BJk53Ro8.mjs +55 -0
- package/dist/packem_shared/backfillAggregateIndexes-BZsOqDXP.mjs +81 -0
- package/dist/packem_shared/buildFtsMatch-BLEMawrp.mjs +38 -0
- package/dist/packem_shared/compileWhereSql-CXrhFA3G.mjs +127 -0
- package/dist/packem_shared/createSystemReader-8CzSZP9V.mjs +80 -0
- package/dist/packem_shared/ctx-db-idempotency-BdcNpvY4.mjs +108 -0
- package/dist/packem_shared/ctx-db-shapes-DVoeZpo-.mjs +53 -0
- package/dist/packem_shared/do-exec-5eQy5cEi.mjs +12 -0
- package/dist/packem_shared/do-sql-BCHCWtrD.mjs +87 -0
- package/dist/packem_shared/exportShardRows-DZEhUeyI.mjs +156 -0
- package/dist/packem_shared/hasTrigger-5N6_Fx0A.mjs +20 -0
- package/dist/packem_shared/renderSql-D6eUcn2N.mjs +16 -0
- package/dist/packem_shared/runShardMigrations-nIwoQeOK.mjs +105 -0
- package/dist/packem_shared/security-audit-CucgBice.mjs +158 -0
- package/dist/packem_shared/serialize-sql-BlRUoiQe.mjs +14 -0
- package/dist/packem_shared/serveRelationFanout-C6lDaesn.mjs +24 -0
- package/dist/packem_shared/stableStringify-CyHKJXre.mjs +30 -0
- package/dist/packem_shared/subscriptionListDeltas-ce84gpwL.mjs +111 -0
- package/package.json +41 -17
|
@@ -0,0 +1,248 @@
|
|
|
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 };
|
|
@@ -0,0 +1,37 @@
|
|
|
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 };
|
|
@@ -0,0 +1,104 @@
|
|
|
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 };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const MAX_SQL_ROWS = 1e3;
|
|
2
|
+
const READONLY_LEAD = /^(?:explain\s+(?:query\s+plan\s+)?)?(?:select|with)\b/iu;
|
|
3
|
+
const FORBIDDEN_KEYWORD = /\b(?:alter|attach|create|delete|detach|drop|insert|pragma|reindex|replace|truncate|update|vacuum)\b/iu;
|
|
4
|
+
const LEADING_NOISE = /^(?:\s|--[^\n]*\n?|\/\*[\s\S]*?\*\/)+/u;
|
|
5
|
+
const TRAILING_SEMICOLON = /;\s*$/u;
|
|
6
|
+
const stripLeading = (sql) => sql.replace(LEADING_NOISE, "");
|
|
7
|
+
const sqlError = (message, code) => Object.assign(new Error(message), { code, name: "LunoraError", status: 400 });
|
|
8
|
+
const assertReadonly = (query) => {
|
|
9
|
+
const trimmed = stripLeading(query).trim();
|
|
10
|
+
if (trimmed === "") {
|
|
11
|
+
throw sqlError("the query is empty", "SQL_EMPTY");
|
|
12
|
+
}
|
|
13
|
+
const single = trimmed.replace(TRAILING_SEMICOLON, "");
|
|
14
|
+
if (single.includes(";")) {
|
|
15
|
+
throw sqlError("only a single statement may be run", "SQL_MULTIPLE_STATEMENTS");
|
|
16
|
+
}
|
|
17
|
+
if (!READONLY_LEAD.test(single) || FORBIDDEN_KEYWORD.test(single)) {
|
|
18
|
+
throw sqlError("the SQL editor is read-only — only SELECT / WITH / EXPLAIN queries are allowed", "SQL_NOT_READONLY");
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const runReadonlySql = (sql, query) => {
|
|
22
|
+
assertReadonly(query);
|
|
23
|
+
const all = sql.exec(query).toArray();
|
|
24
|
+
const rows = all.length > MAX_SQL_ROWS ? all.slice(0, MAX_SQL_ROWS) : all;
|
|
25
|
+
const columns = rows.length > 0 ? Object.keys(rows[0]) : [];
|
|
26
|
+
return { columns, rowCount: all.length, rows, truncated: all.length > MAX_SQL_ROWS };
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { MAX_SQL_ROWS, assertReadonly, runReadonlySql };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { M as MIN_ADMIN_TOKEN_LENGTH, a as MIN_AUTH_SECRET_LENGTH, b as buildSecurityAudit } from './security-audit-CucgBice.mjs';
|