trellis 3.1.35 → 3.2.2
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/CHANGELOG.md +23 -5
- package/README.md +63 -156
- package/bin/trellis.mjs +2 -75
- package/dist/{better-sqlite-backend-ahx5p0br.js → better-sqlite-backend-2P4F6AOJ.js} +91 -29
- package/dist/boot-middleware-MHLHFCUC.js +9 -0
- package/dist/browser/index.d.ts +18 -0
- package/dist/browser/index.d.ts.map +1 -0
- package/dist/browser/index.js +95 -0
- package/dist/{index-53f3b8p8.js → chunk-2W3MHTMG.js} +552 -213
- package/dist/chunk-44FFQKQX.js +142 -0
- package/dist/chunk-4HYPLFJD.js +150 -0
- package/dist/chunk-4XIVMVHC.js +361 -0
- package/dist/{index-yp88he8n.js → chunk-6CTA6MCE.js} +88 -64
- package/dist/chunk-6GEZ6DN5.js +394 -0
- package/dist/chunk-7ZFRVCUE.js +211 -0
- package/dist/chunk-CAG4FULI.js +121 -0
- package/dist/chunk-FF36CQHZ.js +61 -0
- package/dist/{index-h32txmxe.js → chunk-FKQO5A3H.js} +6 -13
- package/dist/{index-nq520y6k.js → chunk-FWRS4CSC.js} +46 -32
- package/dist/chunk-GFZCJ4EH.js +108 -0
- package/dist/chunk-GLM4HGN4.js +55 -0
- package/dist/{index-4cdr7x2x.js → chunk-GU374KWZ.js} +182 -111
- package/dist/chunk-H6JB3PZ3.js +45 -0
- package/dist/chunk-IZX2PLIB.js +1240 -0
- package/dist/chunk-J2TXUFCZ.js +289 -0
- package/dist/chunk-J3WYZO3Q.js +211 -0
- package/dist/{index-xzym9w0m.js → chunk-JA7AIHRK.js} +12 -7
- package/dist/chunk-JHHEXEDM.js +162 -0
- package/dist/chunk-JUEMDWLU.js +233 -0
- package/dist/chunk-KFQGP6VL.js +33 -0
- package/dist/chunk-KIJITUZB.js +419 -0
- package/dist/{index-4wxa8xz4.js → chunk-KQ4A2D2P.js} +162 -23
- package/dist/chunk-KSU2GW22.js +958 -0
- package/dist/{index-rv1p47gp.js → chunk-L5N5PR2S.js} +3015 -2480
- package/dist/{index-h9e2efx4.js → chunk-LNUIGRDZ.js} +71 -55
- package/dist/{index-skhn0agf.js → chunk-MLC5BUYO.js} +19 -20
- package/dist/chunk-MPXUVGT3.js +104 -0
- package/dist/chunk-N6MYZT4O.js +68 -0
- package/dist/chunk-O2AV5WMN.js +48 -0
- package/dist/chunk-PZ4XYZ4J.js +143 -0
- package/dist/chunk-QB5ISE47.js +419 -0
- package/dist/chunk-RSWUFTO2.js +857 -0
- package/dist/chunk-S3XLFZ27.js +1095 -0
- package/dist/{index-w5wccer3.js → chunk-S6GBJ2ZB.js} +28 -12
- package/dist/{index-7pjz3tsy.js → chunk-SGMDTWJJ.js} +57 -15
- package/dist/chunk-SPSPV5BC.js +390 -0
- package/dist/chunk-TCKXSOBP.js +123 -0
- package/dist/chunk-UBGUDMUV.js +59 -0
- package/dist/{index-wt8rz4gn.js → chunk-UQISRW6T.js} +97 -26
- package/dist/chunk-USEINYUL.js +12 -0
- package/dist/{index-bmyt7k8n.js → chunk-UZRUP7QW.js} +12 -4
- package/dist/{index-xhcp6xrn.js → chunk-VBYUTRXX.js} +695 -514
- package/dist/{index-vkpkfwhq.js → chunk-VDAKEOML.js} +234 -194
- package/dist/chunk-WXSWA3MV.js +0 -0
- package/dist/{index-n9f2qyh5.js → chunk-X5FLTRUB.js} +55 -33
- package/dist/{index-h7zxhhhh.js → chunk-XMQ7G77X.js} +46 -38
- package/dist/{index-w7ng765c.js → chunk-YCM7ZYEZ.js} +102 -49
- package/dist/cli/deploy-cli.d.ts +18 -0
- package/dist/cli/deploy-cli.d.ts.map +1 -0
- package/dist/cli/errors.d.ts +6 -0
- package/dist/cli/errors.d.ts.map +1 -0
- package/dist/cli/examples.d.ts +23 -0
- package/dist/cli/examples.d.ts.map +1 -0
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/index.js +2426 -6225
- package/dist/client/index.browser.d.ts +29 -0
- package/dist/client/index.browser.d.ts.map +1 -0
- package/dist/client/index.browser.js +64 -0
- package/dist/client/index.d.ts +6 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +41 -21
- package/dist/client/live.d.ts +67 -0
- package/dist/client/live.d.ts.map +1 -0
- package/dist/client/reactive.d.ts +31 -0
- package/dist/client/reactive.d.ts.map +1 -0
- package/dist/client/sdk.browser.d.ts +104 -0
- package/dist/client/sdk.browser.d.ts.map +1 -0
- package/dist/client/sdk.browser.js +9 -0
- package/dist/client/sdk.d.ts +26 -2
- package/dist/client/sdk.d.ts.map +1 -1
- package/dist/client/sdk.js +9 -0
- package/dist/client/vcs-client.d.ts +129 -0
- package/dist/client/vcs-client.d.ts.map +1 -0
- package/dist/cms/index.js +171 -163
- package/dist/{config-8hczw0rs.js → config-PFTP67TR.js} +8 -9
- package/dist/core/computation/index.d.ts +5 -0
- package/dist/core/computation/index.d.ts.map +1 -0
- package/dist/core/computation/rollup.d.ts +18 -0
- package/dist/core/computation/rollup.d.ts.map +1 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +171 -104
- package/dist/core/kernel/boot-middleware.d.ts +10 -0
- package/dist/core/kernel/boot-middleware.d.ts.map +1 -0
- package/dist/core/kernel/logic-middleware.d.ts +7 -1
- package/dist/core/kernel/logic-middleware.d.ts.map +1 -1
- package/dist/core/kernel/trellis-kernel.d.ts.map +1 -1
- package/dist/core/ontology/types.d.ts +5 -0
- package/dist/core/ontology/types.d.ts.map +1 -1
- package/dist/core/persist/better-sqlite-backend.d.ts +9 -2
- package/dist/core/persist/better-sqlite-backend.d.ts.map +1 -1
- package/dist/core/persist/factory.d.ts +6 -7
- package/dist/core/persist/factory.d.ts.map +1 -1
- package/dist/core/persist/factory.js +2 -3
- package/dist/core/persist/sqljs-backend.js +2 -3
- package/dist/db/index.d.ts +4 -1
- package/dist/db/index.d.ts.map +1 -1
- package/dist/db/index.js +67 -54
- package/dist/db/inspector.js +11 -19
- package/dist/db/trellis.css +1 -1
- package/dist/decisions/index.js +9 -10
- package/dist/deploy-LQ7GUGJ5.js +9 -0
- package/dist/deploy-YNVG5E4E.js +9 -0
- package/dist/deploy-cli-24BCHAXY.js +67 -0
- package/dist/deploy-cli-C35HTITO.js +67 -0
- package/dist/embeddings/auto-embed.d.ts +2 -2
- package/dist/embeddings/auto-embed.d.ts.map +1 -1
- package/dist/embeddings/index.js +20 -22
- package/dist/embeddings/search.d.ts +2 -1
- package/dist/embeddings/search.d.ts.map +1 -1
- package/dist/embeddings/store.d.ts +25 -4
- package/dist/embeddings/store.d.ts.map +1 -1
- package/dist/engine.d.ts +59 -19
- package/dist/engine.d.ts.map +1 -1
- package/dist/import-UZEKXU3B.js +10 -0
- package/dist/index.js +102 -93
- package/dist/launch-explorer-HZ4ZJA4X.js +95 -0
- package/dist/links/index.js +24 -26
- package/dist/mcp/server.d.ts.map +1 -1
- package/dist/plugins/agent-memory/index.js +316 -0
- package/dist/plugins/idea-garden/index.js +165 -0
- package/dist/plugins/plan-approval/index.js +579 -0
- package/dist/plugins/proactive-watcher/index.js +194 -0
- package/dist/{index-2r4jxwnb.js → query-XYLTJYSA.js} +14 -15
- package/dist/react/index.js +39 -70
- package/dist/react/realtime.js +11 -0
- package/dist/react/schema-hooks.js +87 -0
- package/dist/realtime/broadcast-channel-transport.d.ts +43 -0
- package/dist/realtime/broadcast-channel-transport.d.ts.map +1 -0
- package/dist/realtime/durable-object-relay-transport.d.ts +96 -0
- package/dist/realtime/durable-object-relay-transport.d.ts.map +1 -0
- package/dist/realtime/index.d.ts +44 -0
- package/dist/realtime/index.d.ts.map +1 -0
- package/dist/realtime/index.js +40 -0
- package/dist/realtime/memory-hub.d.ts +32 -0
- package/dist/realtime/memory-hub.d.ts.map +1 -0
- package/dist/realtime/persistent-channel.d.ts +105 -0
- package/dist/realtime/persistent-channel.d.ts.map +1 -0
- package/dist/realtime/presence.d.ts +73 -0
- package/dist/realtime/presence.d.ts.map +1 -0
- package/dist/realtime/relay-persistence.d.ts +37 -0
- package/dist/realtime/relay-persistence.d.ts.map +1 -0
- package/dist/realtime/relay-persistence.js +107 -0
- package/dist/realtime/relay-server.d.ts +103 -0
- package/dist/realtime/relay-server.d.ts.map +1 -0
- package/dist/realtime/room.d.ts +98 -0
- package/dist/realtime/room.d.ts.map +1 -0
- package/dist/realtime/text.d.ts +73 -0
- package/dist/realtime/text.d.ts.map +1 -0
- package/dist/realtime/types.d.ts +89 -0
- package/dist/realtime/types.d.ts.map +1 -0
- package/dist/realtime/websocket-relay-transport.d.ts +28 -0
- package/dist/realtime/websocket-relay-transport.d.ts.map +1 -0
- package/dist/realtime.bundle.js +1100 -0
- package/dist/{remote-manager-xvbg4230.js → remote-manager-ODE3T2KR.js} +25 -15
- package/dist/scaffold/seed.d.ts.map +1 -1
- package/dist/scaffold/write.d.ts +1 -1
- package/dist/scaffold/write.d.ts.map +1 -1
- package/dist/schema/define.d.ts +137 -0
- package/dist/schema/define.d.ts.map +1 -0
- package/dist/schema/entity-projection.d.ts +34 -0
- package/dist/schema/entity-projection.d.ts.map +1 -0
- package/dist/schema/eql.d.ts +46 -0
- package/dist/schema/eql.d.ts.map +1 -0
- package/dist/schema/index.d.ts +21 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/schema/index.js +46 -0
- package/dist/schema/kernel-resolve.d.ts +21 -0
- package/dist/schema/kernel-resolve.d.ts.map +1 -0
- package/dist/schema/mutations.d.ts +20 -0
- package/dist/schema/mutations.d.ts.map +1 -0
- package/dist/schema/resolve.d.ts +21 -0
- package/dist/schema/resolve.d.ts.map +1 -0
- package/dist/server/deploy-meta.d.ts +25 -0
- package/dist/server/deploy-meta.d.ts.map +1 -0
- package/dist/server/deploy.d.ts +7 -2
- package/dist/server/deploy.d.ts.map +1 -1
- package/dist/server/index.d.ts +7 -2
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +100 -58
- package/dist/server/node-adapter.js +5 -104
- package/dist/server/realtime.d.ts +14 -3
- package/dist/server/realtime.d.ts.map +1 -1
- package/dist/server/server.d.ts +8 -16
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/sprites.d.ts +18 -1
- package/dist/server/sprites.d.ts.map +1 -1
- package/dist/server/tenancy.d.ts +4 -0
- package/dist/server/tenancy.d.ts.map +1 -1
- package/dist/server/usage-meter.d.ts +53 -0
- package/dist/server/usage-meter.d.ts.map +1 -0
- package/dist/server-MB3OPSPI.js +18 -0
- package/dist/server-ZVWGLLRF.js +18 -0
- package/dist/sprites-KQGTGM6W.js +27 -0
- package/dist/{sprites-vc4qbrp1.js → sprites-RU3E4XQN.js} +6 -7
- package/dist/svelte/index.d.ts +17 -0
- package/dist/svelte/index.d.ts.map +1 -0
- package/dist/svelte/index.js +51 -0
- package/dist/svelte/schema-hooks.d.ts +45 -0
- package/dist/svelte/schema-hooks.d.ts.map +1 -0
- package/dist/svelte/schema-hooks.js +71 -0
- package/dist/svelte/stores.d.ts +78 -0
- package/dist/svelte/stores.d.ts.map +1 -0
- package/dist/sync/http-transport.d.ts +3 -3
- package/dist/sync/http-transport.d.ts.map +1 -1
- package/dist/sync/index.d.ts +15 -1
- package/dist/sync/index.d.ts.map +1 -1
- package/dist/sync/index.js +617 -0
- package/dist/sync/memory-room.d.ts +37 -0
- package/dist/sync/memory-room.d.ts.map +1 -0
- package/dist/sync/memory-transport.d.ts +3 -3
- package/dist/sync/memory-transport.d.ts.map +1 -1
- package/dist/sync/partykit-room.d.ts +40 -0
- package/dist/sync/partykit-room.d.ts.map +1 -0
- package/dist/sync/partykit-transport.d.ts +84 -0
- package/dist/sync/partykit-transport.d.ts.map +1 -0
- package/dist/sync/room-core.d.ts +53 -0
- package/dist/sync/room-core.d.ts.map +1 -0
- package/dist/sync/sync-engine.d.ts +40 -2
- package/dist/sync/sync-engine.d.ts.map +1 -1
- package/dist/sync/sync-room-server.d.ts +39 -0
- package/dist/sync/sync-room-server.d.ts.map +1 -0
- package/dist/sync/types.d.ts +70 -3
- package/dist/sync/types.d.ts.map +1 -1
- package/dist/sync/vcs-sync-peer.d.ts +63 -0
- package/dist/sync/vcs-sync-peer.d.ts.map +1 -0
- package/dist/sync/ws-transport.d.ts +2 -2
- package/dist/sync/ws-transport.d.ts.map +1 -1
- package/dist/tenancy-NBLLGCNJ.js +14 -0
- package/dist/tenancy-POMWQP6M.js +16 -0
- package/dist/ui/client.html +355 -67
- package/dist/ui/launch-explorer.d.ts +22 -0
- package/dist/ui/launch-explorer.d.ts.map +1 -0
- package/dist/ui/server.d.ts +1 -1
- package/dist/ui/server.d.ts.map +1 -1
- package/dist/vcs/blob-store.d.ts.map +1 -1
- package/dist/vcs/idb-op-log.d.ts +67 -0
- package/dist/vcs/idb-op-log.d.ts.map +1 -0
- package/dist/vcs/index.d.ts +1 -0
- package/dist/vcs/index.d.ts.map +1 -1
- package/dist/vcs/index.js +81 -72
- package/dist/vcs/issue.d.ts +22 -5
- package/dist/vcs/issue.d.ts.map +1 -1
- package/dist/vcs/lane-materialize.d.ts +5 -0
- package/dist/vcs/lane-materialize.d.ts.map +1 -1
- package/dist/vcs/op-log.d.ts +28 -2
- package/dist/vcs/op-log.d.ts.map +1 -1
- package/dist/vcs/ops.d.ts +10 -0
- package/dist/vcs/ops.d.ts.map +1 -1
- package/dist/vcs/types.d.ts +2 -0
- package/dist/vcs/types.d.ts.map +1 -1
- package/dist/{vm-config-6xhsj6b3.js → vm-config-JI4OXKDQ.js} +9 -10
- package/dist/vue/hooks.d.ts +64 -0
- package/dist/vue/hooks.d.ts.map +1 -0
- package/dist/vue/index.d.ts +19 -0
- package/dist/vue/index.d.ts.map +1 -0
- package/dist/vue/index.js +59 -0
- package/dist/vue/schema-hooks.d.ts +41 -0
- package/dist/vue/schema-hooks.d.ts.map +1 -0
- package/dist/vue/schema-hooks.js +64 -0
- package/package.json +130 -17
- package/dist/deploy-999q207z.js +0 -10
- package/dist/engine-y0724kjq.js +0 -8
- package/dist/import-s2b8e0ft.js +0 -11
- package/dist/index-a76rekgs.js +0 -67
- package/dist/index-c9h37r6h.js +0 -1
- package/dist/index-hr9qvv77.js +0 -860
- package/dist/index-k5b0xskw.js +0 -1
- package/dist/index-nzb3am4f.js +0 -80
- package/dist/index-wncptktd.js +0 -292
- package/dist/index-y6a4kj0p.js +0 -43
- package/dist/index-yhwjgfvj.js +0 -342
- package/dist/sdk-bepky0xs.js +0 -16
- package/dist/server-szdjx0nt.js +0 -13
- package/dist/sqlite-backend-0vsmc6qj.js +0 -8
- package/dist/tenancy-pjm32b4v.js +0 -14
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// src/schema/entity-projection.ts
|
|
2
|
+
function entityRecordToPlain(entity) {
|
|
3
|
+
const obj = { id: entity.id, type: entity.type };
|
|
4
|
+
for (const f of entity.facts) {
|
|
5
|
+
if (f.a !== "type") obj[f.a] = f.v;
|
|
6
|
+
}
|
|
7
|
+
return obj;
|
|
8
|
+
}
|
|
9
|
+
function bindingEntityId(row) {
|
|
10
|
+
const id = row.id ?? row.e ?? row["?e"];
|
|
11
|
+
return typeof id === "string" && id.length > 0 ? id : null;
|
|
12
|
+
}
|
|
13
|
+
function isSparseBinding(row) {
|
|
14
|
+
const id = bindingEntityId(row);
|
|
15
|
+
if (!id) return false;
|
|
16
|
+
return typeof row.type !== "string";
|
|
17
|
+
}
|
|
18
|
+
function bindingToEntity(row) {
|
|
19
|
+
const id = bindingEntityId(row);
|
|
20
|
+
if (id && typeof row.type === "string") {
|
|
21
|
+
return { ...row, id, type: row.type };
|
|
22
|
+
}
|
|
23
|
+
if (id) return { id, type: String(row.type ?? ""), ...row };
|
|
24
|
+
return row;
|
|
25
|
+
}
|
|
26
|
+
function hydrateBindings(kernel, bindings) {
|
|
27
|
+
return bindings.map((row) => {
|
|
28
|
+
if (!isSparseBinding(row)) return bindingToEntity(row);
|
|
29
|
+
const id = bindingEntityId(row);
|
|
30
|
+
const entity = kernel.getEntity(id);
|
|
31
|
+
return entity ? entityRecordToPlain(entity) : bindingToEntity(row);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/schema/eql.ts
|
|
36
|
+
function escapeValue(v) {
|
|
37
|
+
return v.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
38
|
+
}
|
|
39
|
+
var WHERE_OP_TO_EQL = {
|
|
40
|
+
eq: "=",
|
|
41
|
+
ne: "!=",
|
|
42
|
+
lt: "<",
|
|
43
|
+
lte: "<=",
|
|
44
|
+
gt: ">",
|
|
45
|
+
gte: ">=",
|
|
46
|
+
contains: "contains",
|
|
47
|
+
startsWith: "startsWith",
|
|
48
|
+
endsWith: "endsWith"
|
|
49
|
+
};
|
|
50
|
+
function isWhereFilter(value) {
|
|
51
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
return Object.keys(WHERE_OP_TO_EQL).some(
|
|
55
|
+
(op) => value[op] !== void 0
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
function formatEqlLiteral(value) {
|
|
59
|
+
if (typeof value === "string") return `"${escapeValue(value)}"`;
|
|
60
|
+
if (typeof value === "boolean") return value ? "true" : "false";
|
|
61
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
62
|
+
if (value === null || value === void 0) {
|
|
63
|
+
throw new Error("Cannot format null/undefined as an EQL literal");
|
|
64
|
+
}
|
|
65
|
+
return `"${escapeValue(String(value))}"`;
|
|
66
|
+
}
|
|
67
|
+
function whereCondition(attr, value) {
|
|
68
|
+
if (isWhereFilter(value)) {
|
|
69
|
+
const ops = Object.keys(WHERE_OP_TO_EQL).filter(
|
|
70
|
+
(op2) => value[op2] !== void 0
|
|
71
|
+
);
|
|
72
|
+
if (ops.length === 0) {
|
|
73
|
+
throw new Error(`Empty where filter for attribute "${attr}"`);
|
|
74
|
+
}
|
|
75
|
+
if (ops.length > 1) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`Where filter for "${attr}" must specify one operator, got: ${ops.join(", ")}`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
const op = ops[0];
|
|
81
|
+
return `${attr} ${WHERE_OP_TO_EQL[op]} ${formatEqlLiteral(value[op])}`;
|
|
82
|
+
}
|
|
83
|
+
return `${attr} = ${formatEqlLiteral(value)}`;
|
|
84
|
+
}
|
|
85
|
+
function entitiesQuery(type, where) {
|
|
86
|
+
const conds = [`type = "${escapeValue(type)}"`];
|
|
87
|
+
for (const [k, v] of Object.entries(where ?? {})) {
|
|
88
|
+
if (v === void 0 || v === null) continue;
|
|
89
|
+
conds.push(whereCondition(k, v));
|
|
90
|
+
}
|
|
91
|
+
return `find ?e where ${conds.join(" and ")}`;
|
|
92
|
+
}
|
|
93
|
+
function entityQuery(type, entityId) {
|
|
94
|
+
const t = formatEqlLiteral(type);
|
|
95
|
+
const id = formatEqlLiteral(entityId);
|
|
96
|
+
return `SELECT ?e
|
|
97
|
+
WHERE {
|
|
98
|
+
[?e "type" ${t}]
|
|
99
|
+
}
|
|
100
|
+
FILTER ?e = ${id}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// src/schema/resolve.ts
|
|
104
|
+
function isNestedResolveSpec(v) {
|
|
105
|
+
return typeof v === "object" && v !== null;
|
|
106
|
+
}
|
|
107
|
+
function relationTargetName(r) {
|
|
108
|
+
return typeof r.target === "string" ? r.target : r.target().type;
|
|
109
|
+
}
|
|
110
|
+
function relationTargetSchema(r, schemaLookup) {
|
|
111
|
+
if (typeof r.target !== "string") return r.target();
|
|
112
|
+
return schemaLookup?.(r.target) ?? null;
|
|
113
|
+
}
|
|
114
|
+
function inverseForeignKey(parent, relationName, child) {
|
|
115
|
+
for (const [key, r] of Object.entries(child.relations)) {
|
|
116
|
+
if (relationTargetName(r) === parent.type) return key;
|
|
117
|
+
}
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
async function loadByIds(client, ids, cache) {
|
|
121
|
+
const missing = ids.filter((id) => !cache.has(id));
|
|
122
|
+
if (missing.length === 0) return;
|
|
123
|
+
await Promise.all(
|
|
124
|
+
missing.map(async (id) => {
|
|
125
|
+
cache.set(id, await client.read(id));
|
|
126
|
+
})
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
async function resolveReverseMany(client, parent, relationName, parents, cache, schemaLookup) {
|
|
130
|
+
const rel = parent.relations[relationName];
|
|
131
|
+
if (!rel || rel.cardinality !== "many") return;
|
|
132
|
+
const childSchema = relationTargetSchema(rel, schemaLookup);
|
|
133
|
+
if (!childSchema) return;
|
|
134
|
+
const foreignKey = inverseForeignKey(parent, relationName, childSchema);
|
|
135
|
+
if (!foreignKey) return;
|
|
136
|
+
const parentIds = new Set(parents.map((p) => p.id));
|
|
137
|
+
const qr = await client.query(entitiesQuery(childSchema.type));
|
|
138
|
+
const childIds = qr.bindings.map((b) => bindingEntityId(b)).filter((id) => Boolean(id));
|
|
139
|
+
await loadByIds(client, childIds, cache);
|
|
140
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
141
|
+
for (const id of childIds) {
|
|
142
|
+
const child = cache.get(id);
|
|
143
|
+
if (!child) continue;
|
|
144
|
+
const fk = child[foreignKey];
|
|
145
|
+
if (typeof fk !== "string" || !parentIds.has(fk)) continue;
|
|
146
|
+
if (!grouped.has(fk)) grouped.set(fk, []);
|
|
147
|
+
grouped.get(fk).push(child);
|
|
148
|
+
}
|
|
149
|
+
for (const row of parents) {
|
|
150
|
+
row[relationName] = grouped.get(row.id) ?? [];
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
async function resolveForwardOne(client, parents, relationName, cache) {
|
|
154
|
+
const ids = parents.map((p) => p[relationName]).filter((v) => typeof v === "string" && v.length > 0);
|
|
155
|
+
await loadByIds(client, ids, cache);
|
|
156
|
+
for (const row of parents) {
|
|
157
|
+
const ref = row[relationName];
|
|
158
|
+
if (typeof ref !== "string") continue;
|
|
159
|
+
const loaded = cache.get(ref);
|
|
160
|
+
if (loaded) row[relationName] = loaded;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
async function resolveRelations(client, schema, entities, spec, opts) {
|
|
164
|
+
if (entities.length === 0 || Object.keys(spec).length === 0) return entities;
|
|
165
|
+
const cache = /* @__PURE__ */ new Map();
|
|
166
|
+
const rows = opts?.copy === false ? entities : entities.map((e) => ({ ...e }));
|
|
167
|
+
for (const [name, enabled] of Object.entries(spec)) {
|
|
168
|
+
if (!enabled) continue;
|
|
169
|
+
const rel = schema.relations[name];
|
|
170
|
+
if (!rel) continue;
|
|
171
|
+
if (rel.cardinality === "many") {
|
|
172
|
+
await resolveReverseMany(
|
|
173
|
+
client,
|
|
174
|
+
schema,
|
|
175
|
+
name,
|
|
176
|
+
rows,
|
|
177
|
+
cache,
|
|
178
|
+
opts?.schemaLookup
|
|
179
|
+
);
|
|
180
|
+
if (isNestedResolveSpec(enabled)) {
|
|
181
|
+
const childSchema = relationTargetSchema(rel, opts?.schemaLookup);
|
|
182
|
+
if (childSchema) {
|
|
183
|
+
for (const row of rows) {
|
|
184
|
+
const kids = row[name];
|
|
185
|
+
if (kids?.length) {
|
|
186
|
+
await resolveRelations(client, childSchema, kids, enabled, {
|
|
187
|
+
copy: false,
|
|
188
|
+
schemaLookup: opts?.schemaLookup
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
} else if (isNestedResolveSpec(enabled)) {
|
|
195
|
+
await resolveForwardOne(client, rows, name, cache);
|
|
196
|
+
const childSchema = relationTargetSchema(rel, opts?.schemaLookup);
|
|
197
|
+
if (childSchema) {
|
|
198
|
+
const nested = [];
|
|
199
|
+
for (const row of rows) {
|
|
200
|
+
const loaded = row[name];
|
|
201
|
+
if (loaded && typeof loaded === "object" && "id" in loaded) {
|
|
202
|
+
nested.push(loaded);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (nested.length) {
|
|
206
|
+
await resolveRelations(client, childSchema, nested, enabled, {
|
|
207
|
+
copy: false,
|
|
208
|
+
schemaLookup: opts?.schemaLookup
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
} else {
|
|
213
|
+
await resolveForwardOne(client, rows, name, cache);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return rows;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export {
|
|
220
|
+
entityRecordToPlain,
|
|
221
|
+
bindingEntityId,
|
|
222
|
+
isSparseBinding,
|
|
223
|
+
bindingToEntity,
|
|
224
|
+
hydrateBindings,
|
|
225
|
+
escapeValue,
|
|
226
|
+
isWhereFilter,
|
|
227
|
+
formatEqlLiteral,
|
|
228
|
+
whereCondition,
|
|
229
|
+
entitiesQuery,
|
|
230
|
+
entityQuery,
|
|
231
|
+
inverseForeignKey,
|
|
232
|
+
resolveRelations
|
|
233
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
6
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
7
|
+
}) : x)(function(x) {
|
|
8
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
9
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
10
|
+
});
|
|
11
|
+
var __esm = (fn, res) => function __init() {
|
|
12
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
13
|
+
};
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
|
|
28
|
+
export {
|
|
29
|
+
__require,
|
|
30
|
+
__esm,
|
|
31
|
+
__export,
|
|
32
|
+
__toCommonJS
|
|
33
|
+
};
|
|
@@ -0,0 +1,419 @@
|
|
|
1
|
+
// src/client/sdk.ts
|
|
2
|
+
function isRemote(opts) {
|
|
3
|
+
return "url" in opts;
|
|
4
|
+
}
|
|
5
|
+
var TrellisDb = class _TrellisDb {
|
|
6
|
+
opts;
|
|
7
|
+
_pool = null;
|
|
8
|
+
_poolPromise = null;
|
|
9
|
+
_ws = null;
|
|
10
|
+
/** In-flight connect — concurrent subscribe() shares one socket open. */
|
|
11
|
+
_wsPromise = null;
|
|
12
|
+
_subCallbacks = /* @__PURE__ */ new Map();
|
|
13
|
+
_subOpts = /* @__PURE__ */ new Map();
|
|
14
|
+
constructor(opts) {
|
|
15
|
+
this.opts = opts;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a TrellisDb instance from `.trellis-db.json` in the given directory.
|
|
19
|
+
*/
|
|
20
|
+
static async fromConfig(dir = ".") {
|
|
21
|
+
const { readConfig } = await import("./config-PFTP67TR.js");
|
|
22
|
+
const config = readConfig(dir);
|
|
23
|
+
if (!config)
|
|
24
|
+
throw new Error(
|
|
25
|
+
"No .trellis-db.json found. Run `trellis db init` first."
|
|
26
|
+
);
|
|
27
|
+
if (config.mode === "remote" && config.url) {
|
|
28
|
+
return new _TrellisDb({ url: config.url, apiKey: config.apiKey });
|
|
29
|
+
}
|
|
30
|
+
if (config.dbPath) {
|
|
31
|
+
return new _TrellisDb({ path: config.dbPath });
|
|
32
|
+
}
|
|
33
|
+
throw new Error("Invalid .trellis-db.json: missing url or dbPath.");
|
|
34
|
+
}
|
|
35
|
+
// -------------------------------------------------------------------------
|
|
36
|
+
// CRUD
|
|
37
|
+
// -------------------------------------------------------------------------
|
|
38
|
+
/**
|
|
39
|
+
* Create a new entity.
|
|
40
|
+
* Returns the generated entity ID.
|
|
41
|
+
*/
|
|
42
|
+
async create(type, attributes = {}, links) {
|
|
43
|
+
if (isRemote(this.opts)) {
|
|
44
|
+
const res = await this._fetch("POST", "/entities", {
|
|
45
|
+
type,
|
|
46
|
+
attributes,
|
|
47
|
+
links
|
|
48
|
+
});
|
|
49
|
+
return res.id;
|
|
50
|
+
}
|
|
51
|
+
const pool = await this._getPool();
|
|
52
|
+
const tenantId = this.opts.tenantId ?? null;
|
|
53
|
+
const kernel = pool.get(tenantId);
|
|
54
|
+
const entityId = `${type.toLowerCase()}:${crypto.randomUUID()}`;
|
|
55
|
+
await kernel.createEntity(entityId, type, attributes, links);
|
|
56
|
+
return entityId;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Read an entity by ID.
|
|
60
|
+
* Returns null if not found.
|
|
61
|
+
*/
|
|
62
|
+
async read(id) {
|
|
63
|
+
if (isRemote(this.opts)) {
|
|
64
|
+
try {
|
|
65
|
+
return await this._fetch(
|
|
66
|
+
"GET",
|
|
67
|
+
`/entities/${encodeURIComponent(id)}`
|
|
68
|
+
);
|
|
69
|
+
} catch (err) {
|
|
70
|
+
if (err?.status === 404) return null;
|
|
71
|
+
throw err;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const pool = await this._getPool();
|
|
75
|
+
const tenantId = this.opts.tenantId ?? null;
|
|
76
|
+
const kernel = pool.get(tenantId);
|
|
77
|
+
const entity = kernel.getEntity(id);
|
|
78
|
+
if (!entity) return null;
|
|
79
|
+
return entityToPlain(entity);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Update an entity's attributes (partial update).
|
|
83
|
+
*/
|
|
84
|
+
async update(id, attributes) {
|
|
85
|
+
if (isRemote(this.opts)) {
|
|
86
|
+
await this._fetch(
|
|
87
|
+
"PUT",
|
|
88
|
+
`/entities/${encodeURIComponent(id)}`,
|
|
89
|
+
attributes
|
|
90
|
+
);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const pool = await this._getPool();
|
|
94
|
+
const tenantId = this.opts.tenantId ?? null;
|
|
95
|
+
const kernel = pool.get(tenantId);
|
|
96
|
+
await kernel.updateEntity(id, attributes);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Delete an entity by ID.
|
|
100
|
+
*/
|
|
101
|
+
async delete(id) {
|
|
102
|
+
if (isRemote(this.opts)) {
|
|
103
|
+
await this._fetch("DELETE", `/entities/${encodeURIComponent(id)}`);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const pool = await this._getPool();
|
|
107
|
+
const tenantId = this.opts.tenantId ?? null;
|
|
108
|
+
const kernel = pool.get(tenantId);
|
|
109
|
+
await kernel.deleteEntity(id);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* List entities of a given type.
|
|
113
|
+
*/
|
|
114
|
+
async list(type, opts = {}) {
|
|
115
|
+
if (isRemote(this.opts)) {
|
|
116
|
+
const params = new URLSearchParams();
|
|
117
|
+
if (type) params.set("type", type);
|
|
118
|
+
if (opts.limit) params.set("limit", String(opts.limit));
|
|
119
|
+
if (opts.offset) params.set("offset", String(opts.offset));
|
|
120
|
+
const qs = params.toString() ? `?${params}` : "";
|
|
121
|
+
return await this._fetch("GET", `/entities${qs}`);
|
|
122
|
+
}
|
|
123
|
+
const pool = await this._getPool();
|
|
124
|
+
const tenantId = this.opts.tenantId ?? null;
|
|
125
|
+
const kernel = pool.get(tenantId);
|
|
126
|
+
const entities = kernel.listEntities(type, opts.filters);
|
|
127
|
+
const limit = opts.limit ?? 100;
|
|
128
|
+
const offset = opts.offset ?? 0;
|
|
129
|
+
const page = entities.slice(offset, offset + limit);
|
|
130
|
+
return {
|
|
131
|
+
data: page.map(entityToPlain),
|
|
132
|
+
total: entities.length,
|
|
133
|
+
limit,
|
|
134
|
+
offset
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
// -------------------------------------------------------------------------
|
|
138
|
+
// Query
|
|
139
|
+
// -------------------------------------------------------------------------
|
|
140
|
+
/**
|
|
141
|
+
* Run an EQL-S query string.
|
|
142
|
+
*/
|
|
143
|
+
async query(eql) {
|
|
144
|
+
if (isRemote(this.opts)) {
|
|
145
|
+
return await this._fetch("POST", "/query", {
|
|
146
|
+
query: eql
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
const { parseSimple } = await import("./query-XYLTJYSA.js");
|
|
150
|
+
const pool = await this._getPool();
|
|
151
|
+
const tenantId = this.opts.tenantId ?? null;
|
|
152
|
+
const kernel = pool.get(tenantId);
|
|
153
|
+
const parsed = parseSimple(eql);
|
|
154
|
+
return kernel.query(parsed);
|
|
155
|
+
}
|
|
156
|
+
// -------------------------------------------------------------------------
|
|
157
|
+
// Schema registration
|
|
158
|
+
// -------------------------------------------------------------------------
|
|
159
|
+
/**
|
|
160
|
+
* Register a user/system-tier ontology schema with the kernel.
|
|
161
|
+
*
|
|
162
|
+
* Accepts a {@link SchemaDefinition} or anything carrying one (e.g. a
|
|
163
|
+
* `defineType` handle: `client.registerType(NavItem)`). Local mode calls
|
|
164
|
+
* `kernel.createOntology` directly; remote mode POSTs to `/ontologies`.
|
|
165
|
+
*/
|
|
166
|
+
async registerType(schema) {
|
|
167
|
+
const def = "@id" in schema ? schema : schema.definition;
|
|
168
|
+
if (isRemote(this.opts)) {
|
|
169
|
+
try {
|
|
170
|
+
await this._fetch("POST", "/ontologies", def);
|
|
171
|
+
} catch (err) {
|
|
172
|
+
if (err instanceof FetchError && err.status === 409) return;
|
|
173
|
+
throw err;
|
|
174
|
+
}
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
const pool = await this._getPool();
|
|
178
|
+
const tenantId = this.opts.tenantId ?? null;
|
|
179
|
+
const kernel = pool.get(tenantId);
|
|
180
|
+
const exists = kernel.listOntologies().some((ont) => ont["@id"] === def["@id"]);
|
|
181
|
+
if (exists) return;
|
|
182
|
+
try {
|
|
183
|
+
kernel.createOntology(def);
|
|
184
|
+
} catch (err) {
|
|
185
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
186
|
+
if (msg.includes("already exists")) return;
|
|
187
|
+
throw err;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// -------------------------------------------------------------------------
|
|
191
|
+
// File upload
|
|
192
|
+
// -------------------------------------------------------------------------
|
|
193
|
+
/**
|
|
194
|
+
* Upload a file to the blob store.
|
|
195
|
+
* Returns a content-addressed hash.
|
|
196
|
+
*/
|
|
197
|
+
async upload(data, contentType = "application/octet-stream") {
|
|
198
|
+
const raw = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
|
|
199
|
+
const cleanBuf = raw.buffer.slice(
|
|
200
|
+
raw.byteOffset,
|
|
201
|
+
raw.byteOffset + raw.byteLength
|
|
202
|
+
);
|
|
203
|
+
const buffer = new Uint8Array(cleanBuf);
|
|
204
|
+
if (isRemote(this.opts)) {
|
|
205
|
+
const res = await fetch(`${this.opts.url}/upload`, {
|
|
206
|
+
method: "POST",
|
|
207
|
+
headers: {
|
|
208
|
+
"Content-Type": contentType,
|
|
209
|
+
...this.opts.apiKey ? { Authorization: `Bearer ${this.opts.apiKey}` } : {}
|
|
210
|
+
},
|
|
211
|
+
body: cleanBuf
|
|
212
|
+
});
|
|
213
|
+
if (!res.ok) throw new FetchError(res.status, await res.text());
|
|
214
|
+
return await res.json();
|
|
215
|
+
}
|
|
216
|
+
const hashBuf = await crypto.subtle.digest("SHA-256", buffer);
|
|
217
|
+
const hash = `blob:${Array.from(new Uint8Array(hashBuf)).map((b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
218
|
+
const pool = await this._getPool();
|
|
219
|
+
const tenantId = this.opts.tenantId ?? null;
|
|
220
|
+
const kernel = pool.get(tenantId);
|
|
221
|
+
const backend = kernel.getBackend();
|
|
222
|
+
if (!backend.hasBlob(hash)) backend.putBlob(hash, buffer);
|
|
223
|
+
return { hash, size: buffer.length, contentType };
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Download a file by its blob hash.
|
|
227
|
+
*/
|
|
228
|
+
async getFile(hash) {
|
|
229
|
+
if (isRemote(this.opts)) {
|
|
230
|
+
const res = await fetch(
|
|
231
|
+
`${this.opts.url}/files/${encodeURIComponent(hash)}`,
|
|
232
|
+
{
|
|
233
|
+
headers: this.opts.apiKey ? { Authorization: `Bearer ${this.opts.apiKey}` } : {}
|
|
234
|
+
}
|
|
235
|
+
);
|
|
236
|
+
if (res.status === 404) return null;
|
|
237
|
+
if (!res.ok) throw new FetchError(res.status, await res.text());
|
|
238
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
239
|
+
}
|
|
240
|
+
const pool = await this._getPool();
|
|
241
|
+
const tenantId = this.opts.tenantId ?? null;
|
|
242
|
+
const kernel = pool.get(tenantId);
|
|
243
|
+
const backend = kernel.getBackend();
|
|
244
|
+
return backend.getBlob(hash) ?? null;
|
|
245
|
+
}
|
|
246
|
+
// -------------------------------------------------------------------------
|
|
247
|
+
// Auth helpers
|
|
248
|
+
// -------------------------------------------------------------------------
|
|
249
|
+
async register(email, password, name) {
|
|
250
|
+
if (!isRemote(this.opts))
|
|
251
|
+
throw new Error("register() requires remote mode");
|
|
252
|
+
return await this._fetch("POST", "/auth/register", {
|
|
253
|
+
email,
|
|
254
|
+
password,
|
|
255
|
+
name
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
async login(email, password) {
|
|
259
|
+
if (!isRemote(this.opts)) throw new Error("login() requires remote mode");
|
|
260
|
+
return await this._fetch("POST", "/auth/login", {
|
|
261
|
+
email,
|
|
262
|
+
password
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Set the active API key / JWT token for subsequent requests.
|
|
267
|
+
*/
|
|
268
|
+
setToken(token) {
|
|
269
|
+
if (!isRemote(this.opts)) return;
|
|
270
|
+
this.opts.apiKey = token;
|
|
271
|
+
}
|
|
272
|
+
// -------------------------------------------------------------------------
|
|
273
|
+
// Realtime
|
|
274
|
+
// -------------------------------------------------------------------------
|
|
275
|
+
/**
|
|
276
|
+
* Subscribe to a live EQL-S query.
|
|
277
|
+
* Callback is fired immediately with the initial result, then on every update.
|
|
278
|
+
*
|
|
279
|
+
* Requires remote mode (WebSocket to server).
|
|
280
|
+
*/
|
|
281
|
+
subscribe(eql, callback, opts) {
|
|
282
|
+
if (!isRemote(this.opts)) {
|
|
283
|
+
throw new Error(
|
|
284
|
+
"subscribe() requires remote mode (connect to a running server)"
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
const subId = `sub_${crypto.randomUUID()}`;
|
|
288
|
+
this._subCallbacks.set(subId, callback);
|
|
289
|
+
if (opts) this._subOpts.set(subId, opts);
|
|
290
|
+
this._ensureWs().then((ws) => {
|
|
291
|
+
ws.send(
|
|
292
|
+
JSON.stringify({
|
|
293
|
+
type: "subscribe",
|
|
294
|
+
id: subId,
|
|
295
|
+
query: eql,
|
|
296
|
+
...opts?.entityType ? { entityType: opts.entityType } : {},
|
|
297
|
+
...opts?.resolve ? { resolve: opts.resolve } : {}
|
|
298
|
+
})
|
|
299
|
+
);
|
|
300
|
+
});
|
|
301
|
+
return {
|
|
302
|
+
unsubscribe: () => {
|
|
303
|
+
this._subCallbacks.delete(subId);
|
|
304
|
+
this._subOpts.delete(subId);
|
|
305
|
+
this._ws?.send(JSON.stringify({ type: "unsubscribe", id: subId }));
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Close the WebSocket connection.
|
|
311
|
+
*/
|
|
312
|
+
disconnect() {
|
|
313
|
+
const ws = this._ws;
|
|
314
|
+
this._ws = null;
|
|
315
|
+
this._wsPromise = null;
|
|
316
|
+
ws?.close();
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Close local kernel pool connections.
|
|
320
|
+
*/
|
|
321
|
+
close() {
|
|
322
|
+
this._pool?.closeAll();
|
|
323
|
+
this._pool = null;
|
|
324
|
+
this._poolPromise = null;
|
|
325
|
+
this.disconnect();
|
|
326
|
+
}
|
|
327
|
+
// -------------------------------------------------------------------------
|
|
328
|
+
// Private
|
|
329
|
+
// -------------------------------------------------------------------------
|
|
330
|
+
async _getPool() {
|
|
331
|
+
if (this._pool) return this._pool;
|
|
332
|
+
if (!this._poolPromise) {
|
|
333
|
+
this._poolPromise = import("./tenancy-NBLLGCNJ.js").then(({ TenantPool }) => {
|
|
334
|
+
const path = this.opts.path;
|
|
335
|
+
this._pool = new TenantPool(path);
|
|
336
|
+
return this._pool;
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
return this._poolPromise;
|
|
340
|
+
}
|
|
341
|
+
async _fetch(method, path, body) {
|
|
342
|
+
const opts = this.opts;
|
|
343
|
+
const url = `${opts.url}${path}`;
|
|
344
|
+
const res = await fetch(url, {
|
|
345
|
+
method,
|
|
346
|
+
headers: {
|
|
347
|
+
"Content-Type": "application/json",
|
|
348
|
+
...opts.apiKey ? { Authorization: `Bearer ${opts.apiKey}` } : {}
|
|
349
|
+
},
|
|
350
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0
|
|
351
|
+
});
|
|
352
|
+
const data = await res.json();
|
|
353
|
+
if (!res.ok)
|
|
354
|
+
throw new FetchError(
|
|
355
|
+
res.status,
|
|
356
|
+
data?.message ?? res.statusText,
|
|
357
|
+
data
|
|
358
|
+
);
|
|
359
|
+
return data;
|
|
360
|
+
}
|
|
361
|
+
_ensureWs() {
|
|
362
|
+
if (this._ws?.readyState === WebSocket.OPEN) {
|
|
363
|
+
return Promise.resolve(this._ws);
|
|
364
|
+
}
|
|
365
|
+
if (this._wsPromise) {
|
|
366
|
+
return this._wsPromise;
|
|
367
|
+
}
|
|
368
|
+
const opts = this.opts;
|
|
369
|
+
const wsUrl = opts.url.replace(/^https?/, opts.url.startsWith("https") ? "wss" : "ws") + "/realtime";
|
|
370
|
+
this._wsPromise = new Promise((resolve, reject) => {
|
|
371
|
+
const ws = new WebSocket(wsUrl);
|
|
372
|
+
ws.onopen = () => {
|
|
373
|
+
this._ws = ws;
|
|
374
|
+
resolve(ws);
|
|
375
|
+
};
|
|
376
|
+
ws.onerror = (e) => {
|
|
377
|
+
reject(e instanceof Error ? e : new Error("WebSocket error"));
|
|
378
|
+
};
|
|
379
|
+
ws.onmessage = (e) => {
|
|
380
|
+
try {
|
|
381
|
+
const msg = JSON.parse(e.data);
|
|
382
|
+
if (msg.type === "data" && this._subCallbacks.has(msg.id)) {
|
|
383
|
+
const meta = msg.resolved === true ? { resolved: true } : void 0;
|
|
384
|
+
this._subCallbacks.get(msg.id)(msg.result, msg.diff, meta);
|
|
385
|
+
}
|
|
386
|
+
} catch {
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
ws.onclose = () => {
|
|
390
|
+
if (this._ws === ws) {
|
|
391
|
+
this._ws = null;
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
}).finally(() => {
|
|
395
|
+
this._wsPromise = null;
|
|
396
|
+
});
|
|
397
|
+
return this._wsPromise;
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
function entityToPlain(entity) {
|
|
401
|
+
const obj = { id: entity.id, type: entity.type };
|
|
402
|
+
for (const f of entity.facts) {
|
|
403
|
+
if (f.a !== "type") obj[f.a] = f.v;
|
|
404
|
+
}
|
|
405
|
+
return obj;
|
|
406
|
+
}
|
|
407
|
+
var FetchError = class extends Error {
|
|
408
|
+
constructor(status, message, body) {
|
|
409
|
+
super(`HTTP ${status}: ${message}`);
|
|
410
|
+
this.status = status;
|
|
411
|
+
this.body = body;
|
|
412
|
+
this.name = "FetchError";
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
export {
|
|
417
|
+
TrellisDb,
|
|
418
|
+
FetchError
|
|
419
|
+
};
|