@lunora/do 1.0.0-alpha.27 → 1.0.0-alpha.270
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/README.md +11 -0
- package/dist/index.d.mts +6554 -6101
- package/dist/index.d.ts +6554 -6101
- package/dist/index.mjs +1 -40
- package/dist/packem_chunks/ai-opt-in.mjs +1 -0
- package/dist/packem_chunks/sql-assistant.mjs +13 -0
- package/dist/packem_shared/MERGED_ROLES-C4RToZ16.mjs +1 -0
- package/dist/packem_shared/ROOT_DO_SIZE_WARN_BYTES-C8H5aHLT.mjs +13 -0
- package/dist/packem_shared/SESSION_DO_TTL_DEFAULT-DSvTwRjc.mjs +1 -0
- package/dist/packem_shared/SHARD_REGISTRY_DO_NAME-YZZk79QR.mjs +2 -0
- package/dist/packem_shared/constant-time-equal-BRh9yUCr.mjs +1 -0
- package/dist/packem_shared/createStableIdFactory-DPDT_chw.mjs +1 -0
- package/dist/packem_shared/installDeliveryFence-B6M9KOnb.mjs +1 -0
- package/dist/packem_shared/json-response-0Bq2ky0N.mjs +1 -0
- package/dist/packem_shared/serveRelationFanout-DcSjzAZv.mjs +1 -0
- package/package.json +9 -4
- package/dist/packem_shared/ADMIN_FUNCTIONS-Bxdijn66.mjs +0 -366
- package/dist/packem_shared/AGGREGATE_SQL_FUNCTION-CQsu2Xga.mjs +0 -56
- package/dist/packem_shared/AUTH_METRICS_BUCKETS_TABLE-CiHHYeJi.mjs +0 -84
- package/dist/packem_shared/CDC_LOG_TABLE-DjJEHiM2.mjs +0 -107
- package/dist/packem_shared/ConflictError-CLoq37xH.mjs +0 -12
- package/dist/packem_shared/CountRlsUnsupportedError-BGxj0pgS.mjs +0 -133
- package/dist/packem_shared/DATA_MIGRATION_STATE_TABLE-CYwBpyTr.mjs +0 -245
- package/dist/packem_shared/DEFAULT_MAX_RELATION_KEYS-BEan1CRD.mjs +0 -211
- package/dist/packem_shared/FUNCTION_METRICS_BUCKETS_TABLE-UDNVD7FS.mjs +0 -248
- package/dist/packem_shared/LogBuffer-B_Ezju_N.mjs +0 -37
- package/dist/packem_shared/MAIL_RETENTION-CPpgl-dX.mjs +0 -104
- package/dist/packem_shared/MAX_SQL_ROWS-D57CJaT9.mjs +0 -31
- package/dist/packem_shared/MIN_ADMIN_TOKEN_LENGTH-CCAvoFlr.mjs +0 -1
- package/dist/packem_shared/NotFoundError-C70b9hLw.mjs +0 -9
- package/dist/packem_shared/NotUniqueError-BigrdT_W.mjs +0 -1896
- package/dist/packem_shared/RANK_TIEBREAK-CXhdcA1o.mjs +0 -91
- package/dist/packem_shared/RLS_UNWRAP_SYMBOL-DTvHvRzY.mjs +0 -141
- package/dist/packem_shared/ROOT_DO_SIZE_WARN_BYTES-wTF0-qXJ.mjs +0 -6424
- package/dist/packem_shared/ReactiveCache-BYlSGY0N.mjs +0 -232
- package/dist/packem_shared/SCAN_DEP-DLJF8dsj.mjs +0 -19
- package/dist/packem_shared/SESSION_DO_TTL_DEFAULT-BnSKgVO4.mjs +0 -169
- package/dist/packem_shared/SHARD_REGISTRY_DO_NAME-D99roc-r.mjs +0 -144
- package/dist/packem_shared/aggregateTableName-CxNqY1Sl.mjs +0 -64
- package/dist/packem_shared/applyOnDelete-BXSq3S70.mjs +0 -176
- package/dist/packem_shared/applySelect-WQY8m62C.mjs +0 -103
- package/dist/packem_shared/armRestore-4Px61hHS.mjs +0 -49
- package/dist/packem_shared/backfillAggregateIndexes-DDoT-UUI.mjs +0 -81
- package/dist/packem_shared/buildFtsMatch-BLEMawrp.mjs +0 -38
- package/dist/packem_shared/compileWhereSql-DE6yfRcQ.mjs +0 -129
- package/dist/packem_shared/constant-time-equal-BVRWZgES.mjs +0 -12
- package/dist/packem_shared/createSystemReader-D12eNH13.mjs +0 -82
- package/dist/packem_shared/ctx-db-idempotency-BdcNpvY4.mjs +0 -108
- package/dist/packem_shared/ctx-db-shapes-Cz9dHyh1.mjs +0 -53
- package/dist/packem_shared/diffExternalSource-Cx9HUPJj.mjs +0 -44
- package/dist/packem_shared/do-exec-5eQy5cEi.mjs +0 -12
- package/dist/packem_shared/do-sql-BCHCWtrD.mjs +0 -87
- package/dist/packem_shared/exportShardRows-Dy3oFZ26.mjs +0 -157
- package/dist/packem_shared/hasTrigger-5N6_Fx0A.mjs +0 -20
- package/dist/packem_shared/isSourceDue-CYkt7Ru8.mjs +0 -41
- package/dist/packem_shared/json-response-BdbtpOhm.mjs +0 -3
- package/dist/packem_shared/materializeExternalRows-CTqZisSC.mjs +0 -23
- package/dist/packem_shared/renderSql-D6eUcn2N.mjs +0 -16
- package/dist/packem_shared/runShardMigrations-BGx4v2B6.mjs +0 -105
- package/dist/packem_shared/security-audit-CucgBice.mjs +0 -158
- package/dist/packem_shared/serialize-sql-BlRUoiQe.mjs +0 -14
- package/dist/packem_shared/serveRelationFanout-C56hKu1O.mjs +0 -21
- package/dist/packem_shared/stableStringify-MydiuScU.mjs +0 -40
- package/dist/packem_shared/subscription-delivery-CK8qga-k.mjs +0 -347
- package/dist/packem_shared/subscriptionListDeltas-BxygQlXT.mjs +0 -1
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import { LunoraError } from '@lunora/errors';
|
|
2
|
-
import { distinctValues } from './applyOnDelete-BXSq3S70.mjs';
|
|
3
|
-
|
|
4
|
-
const RELATION_EXISTS_KEY = "__relationExists";
|
|
5
|
-
|
|
6
|
-
const RELATION_OPERATOR_META = {
|
|
7
|
-
every: { kind: "many", negateChild: true, negated: true },
|
|
8
|
-
is: { kind: "one", negated: false },
|
|
9
|
-
isNot: { kind: "one", negated: true, nullDisjunct: true },
|
|
10
|
-
none: { kind: "many", negated: true },
|
|
11
|
-
some: { kind: "many", negated: false }
|
|
12
|
-
};
|
|
13
|
-
const RELATION_OPERATORS = new Set(Object.keys(RELATION_OPERATOR_META));
|
|
14
|
-
const joinColumns = (relation) => relation.kind === "one" ? { clause: relation.field, project: relation.references } : { clause: relation.references, project: relation.field };
|
|
15
|
-
const DEFAULT_MAX_RELATION_KEYS = 5e3;
|
|
16
|
-
const KEY_OVERFLOW = /* @__PURE__ */ Symbol("relation-key-overflow");
|
|
17
|
-
const branchesOf = (value) => Array.isArray(value) ? value.map((branch) => branch ?? {}) : [];
|
|
18
|
-
const combineAnd = (clauses) => {
|
|
19
|
-
if (clauses.length === 1) {
|
|
20
|
-
const [only] = clauses;
|
|
21
|
-
return only ?? {};
|
|
22
|
-
}
|
|
23
|
-
return clauses.length === 0 ? {} : { AND: clauses };
|
|
24
|
-
};
|
|
25
|
-
const isRelationPredicate = (value) => {
|
|
26
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
const keys = Object.keys(value);
|
|
30
|
-
return keys.length > 0 && keys.every((key) => RELATION_OPERATORS.has(key));
|
|
31
|
-
};
|
|
32
|
-
const containsRelationPredicate = (where, schema, tableName) => {
|
|
33
|
-
const relationMap = schema.tables[tableName]?.relationMap ?? {};
|
|
34
|
-
return Object.keys(where).some((key) => {
|
|
35
|
-
const value = where[key];
|
|
36
|
-
if (key === "AND" || key === "OR") {
|
|
37
|
-
return branchesOf(value).some((branch) => containsRelationPredicate(branch, schema, tableName));
|
|
38
|
-
}
|
|
39
|
-
if (key === "NOT") {
|
|
40
|
-
return containsRelationPredicate(value ?? {}, schema, tableName);
|
|
41
|
-
}
|
|
42
|
-
return Boolean(relationMap[key]) && isRelationPredicate(value);
|
|
43
|
-
});
|
|
44
|
-
};
|
|
45
|
-
const assertFlatPredicate = (where, schema, tableName, op) => {
|
|
46
|
-
if (where && containsRelationPredicate(where, schema, tableName)) {
|
|
47
|
-
throw new LunoraError("INTERNAL", `relation-crossing predicates are not supported in ${op}() — use them in findMany/findFirst or an RLS read policy`);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
const projectChildKeys = async (relation, childWhere, projectField, context, escalatable) => {
|
|
51
|
-
const resolvedChildWhere = await resolveForTable(childWhere, relation.table, context);
|
|
52
|
-
const { page } = await context.fetcher(relation.table, {
|
|
53
|
-
baseWhere: context.relationBaseWhere?.(relation.table),
|
|
54
|
-
relationBaseWhere: context.relationBaseWhere,
|
|
55
|
-
where: resolvedChildWhere
|
|
56
|
-
});
|
|
57
|
-
const keys = distinctValues(page, projectField);
|
|
58
|
-
if (keys.length > context.maxRelationKeys) {
|
|
59
|
-
if (escalatable) {
|
|
60
|
-
return KEY_OVERFLOW;
|
|
61
|
-
}
|
|
62
|
-
throw new LunoraError(
|
|
63
|
-
"INTERNAL",
|
|
64
|
-
`relation predicate on "${relation.table}" matched ${String(keys.length)} rows, exceeding the ${String(context.maxRelationKeys)}-key limit; narrow the predicate (a same-shard EXISTS push-down lifts this cap)`
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
return keys;
|
|
68
|
-
};
|
|
69
|
-
const compileOperator = async (operator, relation, childWhere, context, escalatable) => {
|
|
70
|
-
const meta = RELATION_OPERATOR_META[operator];
|
|
71
|
-
if (!meta) {
|
|
72
|
-
throw new LunoraError("INTERNAL", `unknown relation operator "${operator}"`);
|
|
73
|
-
}
|
|
74
|
-
const { clause, project } = joinColumns(relation);
|
|
75
|
-
const keys = await projectChildKeys(relation, meta.negateChild ? { NOT: childWhere } : childWhere, project, context, escalatable);
|
|
76
|
-
if (keys === KEY_OVERFLOW) {
|
|
77
|
-
return KEY_OVERFLOW;
|
|
78
|
-
}
|
|
79
|
-
if (!meta.negated) {
|
|
80
|
-
return { [clause]: { in: keys } };
|
|
81
|
-
}
|
|
82
|
-
if (meta.nullDisjunct) {
|
|
83
|
-
return { OR: [{ [clause]: { notIn: keys } }, { [clause]: { isNull: true } }] };
|
|
84
|
-
}
|
|
85
|
-
return { [clause]: { notIn: keys } };
|
|
86
|
-
};
|
|
87
|
-
const buildExistsMarker = async (operator, relation, childWhere, parentTable, context) => {
|
|
88
|
-
const meta = RELATION_OPERATOR_META[operator];
|
|
89
|
-
if (!meta) {
|
|
90
|
-
throw new LunoraError("INTERNAL", `unknown relation operator "${operator}"`);
|
|
91
|
-
}
|
|
92
|
-
const base = context.relationBaseWhere?.(relation.table);
|
|
93
|
-
const predicatePart = meta.negateChild ? { NOT: childWhere } : childWhere;
|
|
94
|
-
const merged = base ? { AND: [base, predicatePart] } : predicatePart;
|
|
95
|
-
const resolvedChild = await resolveForTable(merged, relation.table, context);
|
|
96
|
-
const marker = { childWhere: resolvedChild, negated: meta.negated, parentTable, relation };
|
|
97
|
-
return { [RELATION_EXISTS_KEY]: marker };
|
|
98
|
-
};
|
|
99
|
-
const assertCardinality = (operator, name, relation) => {
|
|
100
|
-
const meta = RELATION_OPERATOR_META[operator];
|
|
101
|
-
if (meta && meta.kind !== relation.kind) {
|
|
102
|
-
throw new LunoraError("INTERNAL", `relation operator "${operator}" requires a to-${meta.kind} relation, but "${name}" is to-${relation.kind}`);
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
const resolveRelationNode = async (name, relation, predicate, parentTable, context) => {
|
|
106
|
-
const clauses = [];
|
|
107
|
-
for (const operator of Object.keys(predicate)) {
|
|
108
|
-
assertCardinality(operator, name, relation);
|
|
109
|
-
const childWhere = predicate[operator] ?? {};
|
|
110
|
-
const pushable = context.canPushExists?.(relation) ?? false;
|
|
111
|
-
if (pushable && context.existsPushMode === "always") {
|
|
112
|
-
clauses.push(await buildExistsMarker(operator, relation, childWhere, parentTable, context));
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
const semijoin = await compileOperator(operator, relation, childWhere, context, pushable);
|
|
116
|
-
if (semijoin === KEY_OVERFLOW) {
|
|
117
|
-
clauses.push(await buildExistsMarker(operator, relation, childWhere, parentTable, context));
|
|
118
|
-
} else {
|
|
119
|
-
clauses.push(semijoin);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
return combineAnd(clauses);
|
|
123
|
-
};
|
|
124
|
-
const resolveKey = async (key, value, tableName, context) => {
|
|
125
|
-
if (key === "AND" || key === "OR") {
|
|
126
|
-
const resolved = [];
|
|
127
|
-
for (const branch of branchesOf(value)) {
|
|
128
|
-
resolved.push(await resolveForTable(branch, tableName, context));
|
|
129
|
-
}
|
|
130
|
-
return { [key]: resolved };
|
|
131
|
-
}
|
|
132
|
-
if (key === "NOT") {
|
|
133
|
-
return { NOT: await resolveForTable(value ?? {}, tableName, context) };
|
|
134
|
-
}
|
|
135
|
-
const relation = context.schema.tables[tableName]?.relationMap?.[key];
|
|
136
|
-
if (relation && isRelationPredicate(value)) {
|
|
137
|
-
return resolveRelationNode(key, relation, value, tableName, context);
|
|
138
|
-
}
|
|
139
|
-
return { [key]: value };
|
|
140
|
-
};
|
|
141
|
-
const resolveForTable = async (where, tableName, context) => {
|
|
142
|
-
const clauses = [];
|
|
143
|
-
for (const key of Object.keys(where)) {
|
|
144
|
-
clauses.push(await resolveKey(key, where[key], tableName, context));
|
|
145
|
-
}
|
|
146
|
-
return combineAnd(clauses);
|
|
147
|
-
};
|
|
148
|
-
const resolveRelationPredicates = async (where, options) => {
|
|
149
|
-
if (!where || !containsRelationPredicate(where, options.schema, options.tableName)) {
|
|
150
|
-
return where;
|
|
151
|
-
}
|
|
152
|
-
return resolveForTable(where, options.tableName, {
|
|
153
|
-
canPushExists: options.canPushExists,
|
|
154
|
-
existsPushMode: options.existsPushMode ?? "auto",
|
|
155
|
-
fetcher: options.fetcher,
|
|
156
|
-
maxRelationKeys: options.maxRelationKeys ?? DEFAULT_MAX_RELATION_KEYS,
|
|
157
|
-
relationBaseWhere: options.relationBaseWhere,
|
|
158
|
-
schema: options.schema
|
|
159
|
-
});
|
|
160
|
-
};
|
|
161
|
-
const firstShardedHit = (branches, schema, tableName) => {
|
|
162
|
-
for (const branch of branches) {
|
|
163
|
-
const hit = findShardedRelationTarget(branch, schema, tableName);
|
|
164
|
-
if (hit) {
|
|
165
|
-
return hit;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return void 0;
|
|
169
|
-
};
|
|
170
|
-
const inspectKey = (key, value, schema, tableName) => {
|
|
171
|
-
if (key === "AND" || key === "OR") {
|
|
172
|
-
return firstShardedHit(branchesOf(value), schema, tableName);
|
|
173
|
-
}
|
|
174
|
-
if (key === "NOT") {
|
|
175
|
-
return firstShardedHit([value ?? {}], schema, tableName);
|
|
176
|
-
}
|
|
177
|
-
const relation = schema.tables[tableName]?.relationMap?.[key];
|
|
178
|
-
if (!relation || !isRelationPredicate(value)) {
|
|
179
|
-
return void 0;
|
|
180
|
-
}
|
|
181
|
-
if (schema.tables[relation.table]?.shardMode?.kind === "shardBy") {
|
|
182
|
-
return { relation: key, target: relation.table };
|
|
183
|
-
}
|
|
184
|
-
return firstShardedHit(Object.values(value), schema, relation.table);
|
|
185
|
-
};
|
|
186
|
-
const findShardedRelationTarget = (where, schema, tableName) => {
|
|
187
|
-
for (const key of Object.keys(where)) {
|
|
188
|
-
const hit = inspectKey(key, where[key], schema, tableName);
|
|
189
|
-
if (hit) {
|
|
190
|
-
return hit;
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return void 0;
|
|
194
|
-
};
|
|
195
|
-
const assertShapeShardable = (effectiveWhere, schema, table) => {
|
|
196
|
-
if (!effectiveWhere) {
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
const offending = findShardedRelationTarget(effectiveWhere, schema, table);
|
|
200
|
-
if (!offending) {
|
|
201
|
-
return;
|
|
202
|
-
}
|
|
203
|
-
throw Object.assign(
|
|
204
|
-
new Error(
|
|
205
|
-
`shape on "${table}" joins the sharded table "${offending.target}" via relation "${offending.relation}" — a live shape cannot replicate rows that live in another shard's Durable Object. Fix it by (a) denormalizing the joined columns into "${table}", or (b) moving "${offending.target}" to .global() so it is served through the latency-tiered D1 shape tier.`
|
|
206
|
-
),
|
|
207
|
-
{ code: "SHAPE_CROSS_SHARD_JOIN", name: "LunoraError", status: 400 }
|
|
208
|
-
);
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
export { DEFAULT_MAX_RELATION_KEYS, assertFlatPredicate, assertShapeShardable, containsRelationPredicate, isRelationPredicate, resolveRelationPredicates };
|
|
@@ -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,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,31 +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 LEADING_NOISE = /^(?:\s|--[^\n]*\n?|\/\*[\s\S]*?\*\/)+/u;
|
|
7
|
-
const TRAILING_SEMICOLON = /;\s*$/u;
|
|
8
|
-
const stripLeading = (sql) => sql.replace(LEADING_NOISE, "");
|
|
9
|
-
const sqlError = (message, code) => new LunoraError(code, message, { status: 400 });
|
|
10
|
-
const assertReadonly = (query) => {
|
|
11
|
-
const trimmed = stripLeading(query).trim();
|
|
12
|
-
if (trimmed === "") {
|
|
13
|
-
throw sqlError("the query is empty", "SQL_EMPTY");
|
|
14
|
-
}
|
|
15
|
-
const single = trimmed.replace(TRAILING_SEMICOLON, "");
|
|
16
|
-
if (single.includes(";")) {
|
|
17
|
-
throw sqlError("only a single statement may be run", "SQL_MULTIPLE_STATEMENTS");
|
|
18
|
-
}
|
|
19
|
-
if (!READONLY_LEAD.test(single) || FORBIDDEN_KEYWORD.test(single)) {
|
|
20
|
-
throw sqlError("the SQL editor is read-only — only SELECT / WITH / EXPLAIN queries are allowed", "SQL_NOT_READONLY");
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
const runReadonlySql = (sql, query) => {
|
|
24
|
-
assertReadonly(query);
|
|
25
|
-
const all = sql.exec(query).toArray();
|
|
26
|
-
const rows = all.length > MAX_SQL_ROWS ? all.slice(0, MAX_SQL_ROWS) : all;
|
|
27
|
-
const columns = rows.length > 0 ? Object.keys(rows[0]) : [];
|
|
28
|
-
return { columns, rowCount: all.length, rows, truncated: all.length > MAX_SQL_ROWS };
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
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';
|