@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.
- package/dist/index.d.mts +55 -1
- package/dist/index.d.ts +55 -1
- package/dist/index.mjs +1 -44
- package/dist/packem_shared/ADMIN_FUNCTIONS-PxZ8Lr9e.mjs +1 -0
- package/dist/packem_shared/AGGREGATE_SQL_FUNCTION-QYaiuty4.mjs +1 -0
- package/dist/packem_shared/AUTH_METRICS_BUCKETS_TABLE-D0wNaez7.mjs +22 -0
- package/dist/packem_shared/CDC_LOG_TABLE-E_J5LPoK.mjs +8 -0
- package/dist/packem_shared/ConflictError-C8GtJmjS.mjs +1 -0
- package/dist/packem_shared/CountRlsUnsupportedError-Cl8XpYDL.mjs +1 -0
- package/dist/packem_shared/DATA_MIGRATION_STATE_TABLE-CaO6L0Ee.mjs +31 -0
- package/dist/packem_shared/DEFAULT_MAX_RELATION_KEYS-BRdmX7WW.mjs +1 -0
- package/dist/packem_shared/FUNCTION_METRICS_BUCKETS_TABLE-BPPeqM11.mjs +56 -0
- package/dist/packem_shared/GEO_DEFAULT_PRECISION-CWAm_oYY.mjs +1 -0
- package/dist/packem_shared/LogBuffer-bIvCelI-.mjs +1 -0
- package/dist/packem_shared/MAIL_RETENTION-KmozO2NQ.mjs +18 -0
- package/dist/packem_shared/MAX_SQL_ROWS-Bdu25ASB.mjs +2 -0
- package/dist/packem_shared/MIN_ADMIN_TOKEN_LENGTH-Ch6OW6_3.mjs +1 -0
- package/dist/packem_shared/NotFoundError-J3tjf4Uo.mjs +1 -0
- package/dist/packem_shared/NotUniqueError-BcP6GOem.mjs +1 -0
- package/dist/packem_shared/RANK_TIEBREAK-DtX8zQyc.mjs +1 -0
- package/dist/packem_shared/RLS_UNWRAP_SYMBOL-C6_WX3dG.mjs +1 -0
- package/dist/packem_shared/ROOT_DO_SIZE_WARN_BYTES-CP5wvJOW.mjs +101 -0
- package/dist/packem_shared/ReactiveCache-1_9Rs7J_.mjs +1 -0
- package/dist/packem_shared/SCAN_DEP-D_yR9EeV.mjs +1 -0
- package/dist/packem_shared/SESSION_DO_TTL_DEFAULT-GvBy_DBz.mjs +1 -0
- package/dist/packem_shared/SHARD_REGISTRY_DO_NAME-Caa0fR4N.mjs +1 -0
- package/dist/packem_shared/aggregateTableName-G-eXyjcz.mjs +1 -0
- package/dist/packem_shared/applyOnDelete-BvQN7pDL.mjs +1 -0
- package/dist/packem_shared/applySelect-Bq2KOrkL.mjs +1 -0
- package/dist/packem_shared/armRestore-BNzdvQ_o.mjs +1 -0
- package/dist/packem_shared/backfillAggregateIndexes-BAQ3Fwwh.mjs +1 -0
- package/dist/packem_shared/buildFtsMatch-CV0Z7PWv.mjs +1 -0
- package/dist/packem_shared/compileWhereSql-BLcfs4QW.mjs +1 -0
- package/dist/packem_shared/constant-time-equal-BVG05Guz.mjs +1 -0
- package/dist/packem_shared/context-telemetry-8eigmiNQ.mjs +1 -0
- package/dist/packem_shared/createMetrics-CyjLFXtA.mjs +1 -0
- package/dist/packem_shared/createSystemReader-DcDcrtM3.mjs +1 -0
- package/dist/packem_shared/ctx-db-idempotency-wiVoGnpQ.mjs +19 -0
- package/dist/packem_shared/ctx-db-shapes-DzX_H5q8.mjs +1 -0
- package/dist/packem_shared/diffExternalSource-DMpkJta1.mjs +1 -0
- package/dist/packem_shared/do-exec-BLe9lLrN.mjs +1 -0
- package/dist/packem_shared/do-sql-BYIQTG3z.mjs +1 -0
- package/dist/packem_shared/exportShardRows-kt42wijd.mjs +1 -0
- package/dist/packem_shared/hasTrigger-_rexbWMO.mjs +1 -0
- package/dist/packem_shared/isSoftDeleted-BvhQov04.mjs +9 -0
- package/dist/packem_shared/json-response-wrh9TBPw.mjs +1 -0
- package/dist/packem_shared/materializeExternalRows-BFmT9gsw.mjs +1 -0
- package/dist/packem_shared/renderSql-B5lF5Jd9.mjs +1 -0
- package/dist/packem_shared/runShardMigrations-bxOHpfID.mjs +5 -0
- package/dist/packem_shared/security-audit-BKUOgE0x.mjs +1 -0
- package/dist/packem_shared/selectExpiredIds-BGVP3d8-.mjs +1 -0
- package/dist/packem_shared/serialize-sql-DiRzL7A4.mjs +1 -0
- package/dist/packem_shared/serveRelationFanout-Ct5D2Tbk.mjs +1 -0
- package/dist/packem_shared/stableStringify-BjLh4gvA.mjs +1 -0
- package/dist/packem_shared/stableWireKey-YEHLaX6X.mjs +1 -0
- package/dist/packem_shared/subscriptionListDeltas-Bs69JbA8.mjs +1 -0
- package/dist/packem_shared/wire-codec-Ctnni0h6.mjs +1 -0
- package/package.json +1 -1
- package/dist/packem_shared/ADMIN_FUNCTIONS-CnZkbXH_.mjs +0 -374
- 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-uwOJxJJZ.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/GEO_DEFAULT_PRECISION-BWnsNmpP.mjs +0 -114
- 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-iFAA8FbD.mjs +0 -61
- 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-DbtlWwcG.mjs +0 -2031
- 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-B_zt4RQE.mjs +0 -7676
- package/dist/packem_shared/ReactiveCache-DnSvbjil.mjs +0 -233
- 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-BGTwynwh.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/context-telemetry-CDpyil58.mjs +0 -223
- package/dist/packem_shared/createMetrics-C4I2Yanj.mjs +0 -1
- 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-DTeFiHYS.mjs +0 -53
- package/dist/packem_shared/diffExternalSource-CovHfdyo.mjs +0 -44
- package/dist/packem_shared/do-exec-5eQy5cEi.mjs +0 -12
- package/dist/packem_shared/do-sql-CGAgiQUz.mjs +0 -88
- package/dist/packem_shared/exportShardRows-Dy3oFZ26.mjs +0 -157
- package/dist/packem_shared/hasTrigger-5N6_Fx0A.mjs +0 -20
- package/dist/packem_shared/isSoftDeleted-CFJmhjFP.mjs +0 -217
- package/dist/packem_shared/json-response-BdbtpOhm.mjs +0 -3
- package/dist/packem_shared/materializeExternalRows-BtEGs1Fv.mjs +0 -45
- package/dist/packem_shared/renderSql-D6eUcn2N.mjs +0 -16
- package/dist/packem_shared/runShardMigrations-BVax6rYu.mjs +0 -120
- package/dist/packem_shared/security-audit-CucgBice.mjs +0 -158
- package/dist/packem_shared/selectExpiredIds-C5W29Upb.mjs +0 -18
- package/dist/packem_shared/serialize-sql-BlRUoiQe.mjs +0 -14
- package/dist/packem_shared/serveRelationFanout-k7HTh3CC.mjs +0 -21
- package/dist/packem_shared/stableStringify-mC40mZts.mjs +0 -40
- package/dist/packem_shared/stableWireKey-DKuXO7T5.mjs +0 -6
- package/dist/packem_shared/subscriptionListDeltas-CT76bYny.mjs +0 -126
- package/dist/packem_shared/wire-codec-CzQc1pvf.mjs +0 -225
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
const SECRET_NAME_PATTERN = /key|secret|token|password|passwd|credential|private|auth|bearer|session|cookie|salt|signature|dsn|webhook/iu;
|
|
2
|
-
const LUNORA_INTERNAL_VARS = /* @__PURE__ */ new Set(["LUNORA_ADMIN_TOKEN", "LUNORA_WS_BEARER"]);
|
|
3
|
-
const WORKER_URL_VARS = ["CF_PAGES_URL", "WORKER_URL", "LUNORA_WORKER_URL"];
|
|
4
|
-
const ENVIRONMENT_VARS = ["CF_ENV", "ENVIRONMENT", "WORKER_ENV", "NODE_ENV"];
|
|
5
|
-
const redact = (value) => {
|
|
6
|
-
if (value.length <= 4) {
|
|
7
|
-
return "••••";
|
|
8
|
-
}
|
|
9
|
-
return `${value.slice(0, 4)}${"•".repeat(Math.min(8, value.length - 4))}`;
|
|
10
|
-
};
|
|
11
|
-
const looksSecret = (name) => SECRET_NAME_PATTERN.test(name);
|
|
12
|
-
const bindingType = (value) => {
|
|
13
|
-
const has = (key) => typeof value[key] === "function";
|
|
14
|
-
if (has("idFromName") || has("idFromString")) {
|
|
15
|
-
return "durable-object";
|
|
16
|
-
}
|
|
17
|
-
if (has("createMultipartUpload") || has("put") && has("get") && has("list") && has("head")) {
|
|
18
|
-
return "r2";
|
|
19
|
-
}
|
|
20
|
-
if (has("getWithMetadata")) {
|
|
21
|
-
return "kv";
|
|
22
|
-
}
|
|
23
|
-
if (has("prepare") && has("dump")) {
|
|
24
|
-
return "d1";
|
|
25
|
-
}
|
|
26
|
-
if (has("send") && has("sendBatch")) {
|
|
27
|
-
return "queue";
|
|
28
|
-
}
|
|
29
|
-
if (has("fetch") && !has("get")) {
|
|
30
|
-
return "service";
|
|
31
|
-
}
|
|
32
|
-
return "object";
|
|
33
|
-
};
|
|
34
|
-
const readDeployInfo = (env) => {
|
|
35
|
-
const deploy = {};
|
|
36
|
-
for (const key of WORKER_URL_VARS) {
|
|
37
|
-
if (typeof env[key] === "string" && env[key] !== "") {
|
|
38
|
-
deploy.workerUrl = env[key];
|
|
39
|
-
break;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
for (const key of ENVIRONMENT_VARS) {
|
|
43
|
-
if (typeof env[key] === "string" && env[key] !== "") {
|
|
44
|
-
deploy.environment = env[key];
|
|
45
|
-
break;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
const versionMetadata = env["CF_VERSION_METADATA"];
|
|
49
|
-
if (versionMetadata !== null && typeof versionMetadata === "object") {
|
|
50
|
-
const meta = versionMetadata;
|
|
51
|
-
if (typeof meta["id"] === "string") {
|
|
52
|
-
deploy.deploymentId = meta["id"];
|
|
53
|
-
}
|
|
54
|
-
if (typeof meta["tag"] === "string") {
|
|
55
|
-
deploy.versionTag = meta["tag"];
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
return deploy;
|
|
59
|
-
};
|
|
60
|
-
const DEV_ENVIRONMENT_PATTERN = /^(?:dev(?:elopment)?|local(?:host)?|test)$/iu;
|
|
61
|
-
const isDevEnvironment = (rawEnv) => {
|
|
62
|
-
const env = rawEnv ?? {};
|
|
63
|
-
for (const key of ENVIRONMENT_VARS) {
|
|
64
|
-
const value = env[key];
|
|
65
|
-
if (typeof value === "string" && DEV_ENVIRONMENT_PATTERN.test(value)) {
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
return false;
|
|
70
|
-
};
|
|
71
|
-
const buildSettings = (rawEnv) => {
|
|
72
|
-
const env = rawEnv ?? {};
|
|
73
|
-
const settings = [];
|
|
74
|
-
for (const [name, value] of Object.entries(env)) {
|
|
75
|
-
if (LUNORA_INTERNAL_VARS.has(name)) {
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
if (typeof value === "string") {
|
|
79
|
-
const secret = looksSecret(name);
|
|
80
|
-
settings.push({
|
|
81
|
-
kind: secret ? "secret" : "var",
|
|
82
|
-
name,
|
|
83
|
-
// Both vars and secrets are masked — a "var" may still hold a
|
|
84
|
-
// sensitive value, and the studio never needs the raw text.
|
|
85
|
-
value: redact(value)
|
|
86
|
-
});
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
90
|
-
settings.push({ kind: "var", name, value: String(value) });
|
|
91
|
-
continue;
|
|
92
|
-
}
|
|
93
|
-
if (value !== null && typeof value === "object") {
|
|
94
|
-
settings.push({ bindingType: bindingType(value), kind: "binding", name, value: null });
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
settings.push({ kind: "var", name, value: null });
|
|
98
|
-
}
|
|
99
|
-
settings.sort((a, b) => a.name.localeCompare(b.name));
|
|
100
|
-
return { deploy: readDeployInfo(env), settings };
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
const MIN_ADMIN_TOKEN_LENGTH = 24;
|
|
104
|
-
const MIN_AUTH_SECRET_LENGTH = 32;
|
|
105
|
-
const LEVEL_ORDER = { error: 0, info: 2, warning: 1 };
|
|
106
|
-
const DISABLED_ENV_VALUES = /* @__PURE__ */ new Set(["0", "disabled", "false", "no", "off"]);
|
|
107
|
-
const ENABLED_ENV_VALUES = /* @__PURE__ */ new Set(["1", "enabled", "on", "true", "yes"]);
|
|
108
|
-
const readFlag = (value) => typeof value === "string" ? value.trim().toLowerCase() : void 0;
|
|
109
|
-
const auditAuthSecret = (env) => {
|
|
110
|
-
const authSecret = env["AUTH_SECRET"] ?? env["BETTER_AUTH_SECRET"];
|
|
111
|
-
const length = typeof authSecret === "string" ? authSecret.trim().length : 0;
|
|
112
|
-
if (length > 0 && length < MIN_AUTH_SECRET_LENGTH) {
|
|
113
|
-
return [{ detail: { length, min: MIN_AUTH_SECRET_LENGTH }, kind: "auth-secret-weak", level: "warning" }];
|
|
114
|
-
}
|
|
115
|
-
return [];
|
|
116
|
-
};
|
|
117
|
-
const auditCors = (env) => {
|
|
118
|
-
const allowedOrigins = readFlag(env["LUNORA_ALLOWED_ORIGINS"]);
|
|
119
|
-
const corsCredentials = ENABLED_ENV_VALUES.has(readFlag(env["LUNORA_CORS_ALLOW_CREDENTIALS"]) ?? "");
|
|
120
|
-
const hasWildcard = allowedOrigins?.split(",").some((entry) => entry.trim() === "*") ?? false;
|
|
121
|
-
return hasWildcard && corsCredentials ? [{ kind: "cors-wildcard-credentials", level: "error" }] : [];
|
|
122
|
-
};
|
|
123
|
-
const auditSecurityLayers = (env, dev) => {
|
|
124
|
-
if (dev) {
|
|
125
|
-
return [];
|
|
126
|
-
}
|
|
127
|
-
const findings = [];
|
|
128
|
-
if (DISABLED_ENV_VALUES.has(readFlag(env["LUNORA_SECURITY_HEADERS"]) ?? "")) {
|
|
129
|
-
findings.push({ kind: "security-headers-disabled", level: "warning" });
|
|
130
|
-
}
|
|
131
|
-
if (DISABLED_ENV_VALUES.has(readFlag(env["LUNORA_SECURITY_CSRF"]) ?? "")) {
|
|
132
|
-
findings.push({ kind: "csrf-disabled", level: "warning" });
|
|
133
|
-
}
|
|
134
|
-
if (readFlag(env["BETTER_AUTH_URL"])?.startsWith("http://") === true) {
|
|
135
|
-
findings.push({ kind: "cookies-insecure", level: "warning" });
|
|
136
|
-
}
|
|
137
|
-
return findings;
|
|
138
|
-
};
|
|
139
|
-
const buildSecurityAudit = (rawEnv) => {
|
|
140
|
-
const env = rawEnv ?? {};
|
|
141
|
-
const findings = [];
|
|
142
|
-
const adminToken = env["LUNORA_ADMIN_TOKEN"];
|
|
143
|
-
if (typeof adminToken === "string" && adminToken.length > 0 && adminToken.length < MIN_ADMIN_TOKEN_LENGTH) {
|
|
144
|
-
findings.push({ detail: { length: adminToken.length, min: MIN_ADMIN_TOKEN_LENGTH }, kind: "admin-token-weak", level: "warning" });
|
|
145
|
-
}
|
|
146
|
-
const wsBearer = env["LUNORA_WS_BEARER"];
|
|
147
|
-
const dev = isDevEnvironment(env);
|
|
148
|
-
if (typeof wsBearer !== "string" || wsBearer === "") {
|
|
149
|
-
findings.push({ kind: "ws-gate-open", level: dev ? "info" : "error" });
|
|
150
|
-
}
|
|
151
|
-
if (dev) {
|
|
152
|
-
findings.push({ kind: "dev-args-unredacted", level: "warning" });
|
|
153
|
-
}
|
|
154
|
-
findings.push(...auditAuthSecret(env), ...auditCors(env), ...auditSecurityLayers(env, dev));
|
|
155
|
-
return { findings: findings.toSorted((a, b) => LEVEL_ORDER[a.level] - LEVEL_ORDER[b.level]) };
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
export { MIN_ADMIN_TOKEN_LENGTH as M, MIN_AUTH_SECRET_LENGTH as a, buildSecurityAudit as b, buildSettings as c, isDevEnvironment as i };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { sql } from 'drizzle-orm';
|
|
2
|
-
import { r as runDrizzle } from './do-exec-5eQy5cEi.mjs';
|
|
3
|
-
import { j as jsonPathSql } from './do-sql-CGAgiQUz.mjs';
|
|
4
|
-
|
|
5
|
-
const selectExpiredIds = (sql$1, spec, now, limit) => {
|
|
6
|
-
const cutoff = now - (spec.after ?? 0);
|
|
7
|
-
const conditions = [sql`${jsonPathSql(spec.field)} IS NOT NULL`, sql`${jsonPathSql(spec.field)} < ${cutoff}`];
|
|
8
|
-
if (spec.softDeleteField !== void 0) {
|
|
9
|
-
conditions.push(sql`${jsonPathSql(spec.softDeleteField)} IS NULL`);
|
|
10
|
-
}
|
|
11
|
-
const query = sql`SELECT id FROM ${sql.identifier(spec.table)} WHERE ${sql.join(conditions, sql` AND `)} LIMIT ${sql.raw(String(Math.max(0, Math.floor(limit)) + 1))}`;
|
|
12
|
-
const rows = runDrizzle(sql$1, query).toArray();
|
|
13
|
-
const hasMore = rows.length > limit;
|
|
14
|
-
const ids = rows.slice(0, limit).map((row) => row.id);
|
|
15
|
-
return { hasMore, ids };
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export { selectExpiredIds };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
const serializeSqlValue = (value) => {
|
|
2
|
-
if (typeof value === "boolean") {
|
|
3
|
-
return value ? 1 : 0;
|
|
4
|
-
}
|
|
5
|
-
if (value === null || typeof value === "string" || typeof value === "number") {
|
|
6
|
-
return value;
|
|
7
|
-
}
|
|
8
|
-
if (typeof value === "bigint") {
|
|
9
|
-
return value.toString();
|
|
10
|
-
}
|
|
11
|
-
return JSON.stringify(value);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
export { serializeSqlValue as s };
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { LunoraError } from '@lunora/errors';
|
|
2
|
-
import { RELATION_FUNCTION_PREFIX } from './ADMIN_FUNCTIONS-CnZkbXH_.mjs';
|
|
3
|
-
|
|
4
|
-
const serveRelationFanout = async (schema, database, functionPath, args) => {
|
|
5
|
-
const table = typeof args["table"] === "string" ? args["table"] : "";
|
|
6
|
-
const definition = schema.tables[table];
|
|
7
|
-
if (!definition) {
|
|
8
|
-
throw new LunoraError("UNKNOWN_TABLE", `${RELATION_FUNCTION_PREFIX} unknown table "${table}"`, { status: 404 });
|
|
9
|
-
}
|
|
10
|
-
if (definition.shardMode?.kind === "global") {
|
|
11
|
-
throw new LunoraError("BAD_REQUEST", `${RELATION_FUNCTION_PREFIX} table "${table}" is global, not shard-local`);
|
|
12
|
-
}
|
|
13
|
-
const where = args["where"] ?? void 0;
|
|
14
|
-
if (functionPath === `${RELATION_FUNCTION_PREFIX}count`) {
|
|
15
|
-
return database.count(table, where);
|
|
16
|
-
}
|
|
17
|
-
const result = await database.findMany(table, { orderBy: args["orderBy"], where, with: args["with"] });
|
|
18
|
-
return result.page;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export { serveRelationFanout };
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
const compareKeys = (a, b) => {
|
|
2
|
-
if (a < b) {
|
|
3
|
-
return -1;
|
|
4
|
-
}
|
|
5
|
-
return a > b ? 1 : 0;
|
|
6
|
-
};
|
|
7
|
-
const stableStringify = (value) => {
|
|
8
|
-
if (value === void 0) {
|
|
9
|
-
return "null";
|
|
10
|
-
}
|
|
11
|
-
if (typeof value === "bigint") {
|
|
12
|
-
throw new TypeError("stableStringify: cannot use a bigint in a stable JSON cache key — pass it as a string, or use stableWireKey");
|
|
13
|
-
}
|
|
14
|
-
if (value === null || typeof value !== "object") {
|
|
15
|
-
return JSON.stringify(value);
|
|
16
|
-
}
|
|
17
|
-
if (Array.isArray(value)) {
|
|
18
|
-
return `[${value.map((item) => stableStringify(item)).join(",")}]`;
|
|
19
|
-
}
|
|
20
|
-
const proto = Object.getPrototypeOf(value);
|
|
21
|
-
if (proto !== null && proto !== Object.prototype) {
|
|
22
|
-
const name = value.constructor?.name ?? "value";
|
|
23
|
-
throw new TypeError(
|
|
24
|
-
`stableStringify: cannot use a ${name} in a stable JSON cache key — only plain objects, arrays, and JSON primitives are supported (wire-typed values key via stableWireKey)`
|
|
25
|
-
);
|
|
26
|
-
}
|
|
27
|
-
const record = value;
|
|
28
|
-
const keys = Object.keys(record).toSorted(compareKeys);
|
|
29
|
-
const parts = [];
|
|
30
|
-
for (const key of keys) {
|
|
31
|
-
const raw = record[key];
|
|
32
|
-
if (raw === void 0) {
|
|
33
|
-
continue;
|
|
34
|
-
}
|
|
35
|
-
parts.push(`${JSON.stringify(key)}:${stableStringify(raw)}`);
|
|
36
|
-
}
|
|
37
|
-
return `{${parts.join(",")}}`;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export { stableStringify };
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { e as encodeWire } from './wire-codec-CzQc1pvf.mjs';
|
|
2
|
-
|
|
3
|
-
const ROW_ID_FIELD = "_id";
|
|
4
|
-
const DELTA_FALLBACK_TABLE = "__lunora__";
|
|
5
|
-
const readRowId = (row) => {
|
|
6
|
-
if (typeof row !== "object" || row === null || Array.isArray(row)) {
|
|
7
|
-
return void 0;
|
|
8
|
-
}
|
|
9
|
-
const id = row[ROW_ID_FIELD];
|
|
10
|
-
return typeof id === "string" ? id : void 0;
|
|
11
|
-
};
|
|
12
|
-
const indexRowsById = (rows) => {
|
|
13
|
-
const byId = /* @__PURE__ */ new Map();
|
|
14
|
-
const order = [];
|
|
15
|
-
for (const row of rows) {
|
|
16
|
-
const id = readRowId(row);
|
|
17
|
-
if (id === void 0 || byId.has(id)) {
|
|
18
|
-
return void 0;
|
|
19
|
-
}
|
|
20
|
-
byId.set(id, row);
|
|
21
|
-
order.push(id);
|
|
22
|
-
}
|
|
23
|
-
return { byId, order };
|
|
24
|
-
};
|
|
25
|
-
const survivorsKeepOrder = (previous, next) => {
|
|
26
|
-
const survivingPrevious = previous.order.filter((id) => next.byId.has(id));
|
|
27
|
-
const survivingNext = next.order.filter((id) => previous.byId.has(id));
|
|
28
|
-
if (survivingPrevious.length !== survivingNext.length) {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
return survivingPrevious.every((id, index) => survivingNext[index] === id);
|
|
32
|
-
};
|
|
33
|
-
const collectDeleteDeltas = (previous, next, deltaTable, tableJson) => {
|
|
34
|
-
const out = [];
|
|
35
|
-
for (const id of previous.order) {
|
|
36
|
-
if (!next.byId.has(id)) {
|
|
37
|
-
out.push({
|
|
38
|
-
delta: { key: id, op: "delete", table: deltaTable },
|
|
39
|
-
frame: `{"key":${JSON.stringify(id)},"op":"delete","table":${tableJson}}`
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return out;
|
|
44
|
-
};
|
|
45
|
-
const collectUpsertDeltas = (previous, next, deltaTable, tableJson) => {
|
|
46
|
-
const out = [];
|
|
47
|
-
for (const id of next.order) {
|
|
48
|
-
const nextRow = next.byId.get(id);
|
|
49
|
-
const previousRow = previous.byId.get(id);
|
|
50
|
-
const nextFingerprint = JSON.stringify(encodeWire(nextRow));
|
|
51
|
-
const previousFingerprint = previousRow === void 0 ? void 0 : JSON.stringify(previousRow);
|
|
52
|
-
if (previousFingerprint === nextFingerprint) {
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
const op = previousFingerprint === void 0 ? "insert" : "update";
|
|
56
|
-
out.push({
|
|
57
|
-
delta: { key: id, op, row: nextRow, table: deltaTable },
|
|
58
|
-
frame: `{"key":${JSON.stringify(id)},"op":"${op}","row":${nextFingerprint},"table":${tableJson}}`
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
return out;
|
|
62
|
-
};
|
|
63
|
-
const subscriptionListDeltas = (previousJson, nextResult, table, frames) => {
|
|
64
|
-
let parsed;
|
|
65
|
-
try {
|
|
66
|
-
parsed = JSON.parse(previousJson);
|
|
67
|
-
} catch {
|
|
68
|
-
return void 0;
|
|
69
|
-
}
|
|
70
|
-
if (!Array.isArray(parsed) || !Array.isArray(nextResult)) {
|
|
71
|
-
return void 0;
|
|
72
|
-
}
|
|
73
|
-
const previous = indexRowsById(parsed);
|
|
74
|
-
const next = indexRowsById(nextResult);
|
|
75
|
-
if (previous === void 0 || next === void 0) {
|
|
76
|
-
return void 0;
|
|
77
|
-
}
|
|
78
|
-
if (!survivorsKeepOrder(previous, next)) {
|
|
79
|
-
return void 0;
|
|
80
|
-
}
|
|
81
|
-
const deltaTable = table === "" ? DELTA_FALLBACK_TABLE : table;
|
|
82
|
-
const tableJson = JSON.stringify(deltaTable);
|
|
83
|
-
const framed = [...collectDeleteDeltas(previous, next, deltaTable, tableJson), ...collectUpsertDeltas(previous, next, deltaTable, tableJson)];
|
|
84
|
-
if (framed.length > next.order.length) {
|
|
85
|
-
return void 0;
|
|
86
|
-
}
|
|
87
|
-
if (frames !== void 0) {
|
|
88
|
-
for (const { frame } of framed) {
|
|
89
|
-
frames.push(frame);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return framed.map(({ delta }) => delta);
|
|
93
|
-
};
|
|
94
|
-
const trySendFrame = (ws, frame) => {
|
|
95
|
-
try {
|
|
96
|
-
ws.send(frame);
|
|
97
|
-
return true;
|
|
98
|
-
} catch {
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
const sendDeltaFrames = (ws, subId, deltaFrames, cursorSuffix) => {
|
|
103
|
-
const idJson = JSON.stringify(subId);
|
|
104
|
-
let delivered = true;
|
|
105
|
-
for (const deltaBody of deltaFrames) {
|
|
106
|
-
if (!trySendFrame(ws, `{"type":"delta","id":${idJson},"delta":${deltaBody}${cursorSuffix}}`)) {
|
|
107
|
-
delivered = false;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return delivered;
|
|
111
|
-
};
|
|
112
|
-
const awaitWsDrain = async (ws) => {
|
|
113
|
-
let attempts = 0;
|
|
114
|
-
while (attempts < 100) {
|
|
115
|
-
attempts += 1;
|
|
116
|
-
const buffered = ws.bufferedAmount;
|
|
117
|
-
if (typeof buffered !== "number" || buffered < 1048576) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
await new Promise((resolve) => {
|
|
121
|
-
setTimeout(resolve, 20);
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
export { awaitWsDrain, sendDeltaFrames, subscriptionListDeltas, trySendFrame };
|
|
@@ -1,225 +0,0 @@
|
|
|
1
|
-
const toBase64 = (bytes) => {
|
|
2
|
-
let binary = "";
|
|
3
|
-
const chunk = 32768;
|
|
4
|
-
for (let index = 0; index < bytes.length; index += chunk) {
|
|
5
|
-
binary += String.fromCharCode(...bytes.subarray(index, index + chunk));
|
|
6
|
-
}
|
|
7
|
-
return btoa(binary);
|
|
8
|
-
};
|
|
9
|
-
const fromBase64 = (base64) => {
|
|
10
|
-
const binary = atob(base64);
|
|
11
|
-
const bytes = new Uint8Array(binary.length);
|
|
12
|
-
for (let index = 0; index < binary.length; index += 1) {
|
|
13
|
-
bytes[index] = binary.codePointAt(index) ?? 0;
|
|
14
|
-
}
|
|
15
|
-
return bytes;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
const TAG = "$lunora.wire$";
|
|
19
|
-
const MAX_DEPTH = 64;
|
|
20
|
-
const MAX_BIGINT_DIGITS = 1024;
|
|
21
|
-
const UNSAFE_KEY = "__proto__";
|
|
22
|
-
const TYPED_ARRAY_CTORS = {
|
|
23
|
-
BigInt64Array,
|
|
24
|
-
BigUint64Array,
|
|
25
|
-
Float32Array,
|
|
26
|
-
Float64Array,
|
|
27
|
-
Int8Array,
|
|
28
|
-
Int16Array,
|
|
29
|
-
Int32Array,
|
|
30
|
-
Uint8Array,
|
|
31
|
-
Uint8ClampedArray,
|
|
32
|
-
Uint16Array,
|
|
33
|
-
Uint32Array
|
|
34
|
-
};
|
|
35
|
-
const ERROR_CTORS = {
|
|
36
|
-
Error,
|
|
37
|
-
EvalError,
|
|
38
|
-
RangeError,
|
|
39
|
-
ReferenceError,
|
|
40
|
-
SyntaxError,
|
|
41
|
-
TypeError,
|
|
42
|
-
URIError
|
|
43
|
-
};
|
|
44
|
-
const encodeWire = (value, depth = 0) => {
|
|
45
|
-
if (depth > MAX_DEPTH) {
|
|
46
|
-
throw new RangeError(`wire-codec: value nesting exceeds the ${MAX_DEPTH}-level limit`);
|
|
47
|
-
}
|
|
48
|
-
if (value === void 0) {
|
|
49
|
-
return [TAG, "undefined"];
|
|
50
|
-
}
|
|
51
|
-
if (value === null) {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
const kind = typeof value;
|
|
55
|
-
if (kind === "bigint") {
|
|
56
|
-
return [TAG, "bigint", value.toString()];
|
|
57
|
-
}
|
|
58
|
-
if (kind === "number") {
|
|
59
|
-
const numeric = value;
|
|
60
|
-
if (Number.isNaN(numeric)) {
|
|
61
|
-
return [TAG, "nan"];
|
|
62
|
-
}
|
|
63
|
-
if (numeric === Infinity) {
|
|
64
|
-
return [TAG, "inf"];
|
|
65
|
-
}
|
|
66
|
-
if (numeric === -Infinity) {
|
|
67
|
-
return [TAG, "-inf"];
|
|
68
|
-
}
|
|
69
|
-
return numeric;
|
|
70
|
-
}
|
|
71
|
-
if (kind !== "object") {
|
|
72
|
-
return value;
|
|
73
|
-
}
|
|
74
|
-
if (value instanceof Date) {
|
|
75
|
-
return [TAG, "date", encodeWire(value.getTime(), depth + 1)];
|
|
76
|
-
}
|
|
77
|
-
if (value instanceof Error) {
|
|
78
|
-
const error = value;
|
|
79
|
-
const properties = {};
|
|
80
|
-
for (const key of Object.keys(error)) {
|
|
81
|
-
if (error[key] !== void 0) {
|
|
82
|
-
properties[key] = encodeWire(error[key], depth + 1);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
const encodedError = [TAG, "error", error.name, error.message, properties];
|
|
86
|
-
if (error.cause !== void 0) {
|
|
87
|
-
encodedError.push(encodeWire(error.cause, depth + 1));
|
|
88
|
-
}
|
|
89
|
-
return encodedError;
|
|
90
|
-
}
|
|
91
|
-
if (value instanceof URL) {
|
|
92
|
-
return [TAG, "url", value.href];
|
|
93
|
-
}
|
|
94
|
-
if (value instanceof Map) {
|
|
95
|
-
return [TAG, "map", [...value.entries()].map(([k, v]) => [encodeWire(k, depth + 1), encodeWire(v, depth + 1)])];
|
|
96
|
-
}
|
|
97
|
-
if (value instanceof Set) {
|
|
98
|
-
return [TAG, "set", [...value].map((item) => encodeWire(item, depth + 1))];
|
|
99
|
-
}
|
|
100
|
-
if (value instanceof ArrayBuffer) {
|
|
101
|
-
return [TAG, "bytes", toBase64(new Uint8Array(value)), "ArrayBuffer"];
|
|
102
|
-
}
|
|
103
|
-
if (ArrayBuffer.isView(value)) {
|
|
104
|
-
const view = value;
|
|
105
|
-
const ctorName = view.constructor.name;
|
|
106
|
-
const bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
107
|
-
return ctorName === "Uint8Array" ? [TAG, "bytes", toBase64(bytes)] : [TAG, "bytes", toBase64(bytes), ctorName];
|
|
108
|
-
}
|
|
109
|
-
if (Array.isArray(value)) {
|
|
110
|
-
const encoded = value.map((item) => encodeWire(item, depth + 1));
|
|
111
|
-
return encoded.length > 0 && encoded[0] === TAG ? [TAG, "arr", encoded] : encoded;
|
|
112
|
-
}
|
|
113
|
-
const proto = Object.getPrototypeOf(value);
|
|
114
|
-
if (proto !== null && proto !== Object.prototype) {
|
|
115
|
-
const name = value.constructor?.name ?? "value";
|
|
116
|
-
throw new TypeError(
|
|
117
|
-
`wire-codec: cannot encode a ${name} over the Lunora wire — only plain objects, arrays, and the supported built-ins (Date, Error, URL, Map, Set, ArrayBuffer/typed arrays, bigint) round-trip`
|
|
118
|
-
);
|
|
119
|
-
}
|
|
120
|
-
const source = value;
|
|
121
|
-
const result = {};
|
|
122
|
-
for (const key of Object.keys(source)) {
|
|
123
|
-
const field = source[key];
|
|
124
|
-
if (field !== void 0) {
|
|
125
|
-
result[key] = encodeWire(field, depth + 1);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
return result;
|
|
129
|
-
};
|
|
130
|
-
const decodeWire = (value, depth = 0) => {
|
|
131
|
-
if (depth > MAX_DEPTH) {
|
|
132
|
-
throw new RangeError(`wire-codec: value nesting exceeds the ${MAX_DEPTH}-level limit`);
|
|
133
|
-
}
|
|
134
|
-
if (value === null || typeof value !== "object") {
|
|
135
|
-
return value;
|
|
136
|
-
}
|
|
137
|
-
if (Array.isArray(value)) {
|
|
138
|
-
if (value[0] === TAG) {
|
|
139
|
-
const tag = value[1];
|
|
140
|
-
switch (tag) {
|
|
141
|
-
case "-inf": {
|
|
142
|
-
return -Infinity;
|
|
143
|
-
}
|
|
144
|
-
case "arr": {
|
|
145
|
-
return value[2].map((item) => decodeWire(item, depth + 1));
|
|
146
|
-
}
|
|
147
|
-
case "bigint": {
|
|
148
|
-
const raw = value[2];
|
|
149
|
-
if (typeof raw !== "string" || raw.length > MAX_BIGINT_DIGITS || !/^-?\d+$/.test(raw)) {
|
|
150
|
-
throw new RangeError(`wire-codec: invalid or over-long bigint (max ${MAX_BIGINT_DIGITS} digits)`);
|
|
151
|
-
}
|
|
152
|
-
return BigInt(raw);
|
|
153
|
-
}
|
|
154
|
-
case "date": {
|
|
155
|
-
return new Date(decodeWire(value[2], depth + 1));
|
|
156
|
-
}
|
|
157
|
-
case "map": {
|
|
158
|
-
return new Map(value[2].map(([k, v]) => [decodeWire(k, depth + 1), decodeWire(v, depth + 1)]));
|
|
159
|
-
}
|
|
160
|
-
case "set": {
|
|
161
|
-
return new Set(value[2].map((item) => decodeWire(item, depth + 1)));
|
|
162
|
-
}
|
|
163
|
-
case "url": {
|
|
164
|
-
return new URL(value[2]);
|
|
165
|
-
}
|
|
166
|
-
case "error": {
|
|
167
|
-
const name = value[2];
|
|
168
|
-
const message = value[3];
|
|
169
|
-
const Ctor = (Object.hasOwn(ERROR_CTORS, name) ? ERROR_CTORS[name] : void 0) ?? Error;
|
|
170
|
-
const error = new Ctor(message);
|
|
171
|
-
if (error.name !== name) {
|
|
172
|
-
Object.defineProperty(error, "name", { configurable: true, value: name, writable: true });
|
|
173
|
-
}
|
|
174
|
-
const props = decodeWire(value[4], depth + 1);
|
|
175
|
-
for (const key of Object.keys(props)) {
|
|
176
|
-
if (key === UNSAFE_KEY) {
|
|
177
|
-
Object.defineProperty(error, key, { configurable: true, enumerable: true, value: props[key], writable: true });
|
|
178
|
-
} else {
|
|
179
|
-
error[key] = props[key];
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
if (value.length > 5) {
|
|
183
|
-
Object.defineProperty(error, "cause", { configurable: true, value: decodeWire(value[5], depth + 1), writable: true });
|
|
184
|
-
}
|
|
185
|
-
return error;
|
|
186
|
-
}
|
|
187
|
-
case "bytes": {
|
|
188
|
-
const bytes = fromBase64(value[2]);
|
|
189
|
-
const ctorName = value[3] ?? "Uint8Array";
|
|
190
|
-
if (ctorName === "ArrayBuffer") {
|
|
191
|
-
return bytes.buffer.byteLength === bytes.byteLength ? bytes.buffer : bytes.slice().buffer;
|
|
192
|
-
}
|
|
193
|
-
const Ctor = Object.hasOwn(TYPED_ARRAY_CTORS, ctorName) ? TYPED_ARRAY_CTORS[ctorName] : void 0;
|
|
194
|
-
return Ctor ? new Ctor(bytes.slice().buffer) : bytes;
|
|
195
|
-
}
|
|
196
|
-
case "inf": {
|
|
197
|
-
return Infinity;
|
|
198
|
-
}
|
|
199
|
-
case "nan": {
|
|
200
|
-
return Number.NaN;
|
|
201
|
-
}
|
|
202
|
-
case "undefined": {
|
|
203
|
-
return void 0;
|
|
204
|
-
}
|
|
205
|
-
default: {
|
|
206
|
-
return value.map((item) => decodeWire(item, depth + 1));
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
return value.map((item) => decodeWire(item, depth + 1));
|
|
211
|
-
}
|
|
212
|
-
const source = value;
|
|
213
|
-
const result = {};
|
|
214
|
-
for (const key of Object.keys(source)) {
|
|
215
|
-
const decoded = decodeWire(source[key], depth + 1);
|
|
216
|
-
if (key === UNSAFE_KEY) {
|
|
217
|
-
Object.defineProperty(result, key, { configurable: true, enumerable: true, value: decoded, writable: true });
|
|
218
|
-
} else {
|
|
219
|
-
result[key] = decoded;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
return result;
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
export { decodeWire as d, encodeWire as e };
|