trellis 3.1.3 → 3.1.7
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 +1 -0
- package/bin/trellis.mjs +68 -2
- package/dist/better-sqlite-backend-ahx5p0br.js +147 -0
- package/dist/cli/index.js +116 -45
- package/dist/client/index.js +5 -4
- package/dist/cms/client.d.ts +4 -3
- package/dist/cms/client.d.ts.map +1 -1
- package/dist/cms/formula.d.ts +10 -0
- package/dist/cms/formula.d.ts.map +1 -0
- package/dist/cms/index.d.ts +3 -2
- package/dist/cms/index.d.ts.map +1 -1
- package/dist/cms/index.js +156 -4
- package/dist/cms/types.d.ts +11 -0
- package/dist/cms/types.d.ts.map +1 -1
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +14 -4
- package/dist/core/persist/factory.d.ts +28 -0
- package/dist/core/persist/factory.d.ts.map +1 -0
- package/dist/core/persist/factory.js +8 -0
- package/dist/core/persist/sqlite-backend.d.ts.map +1 -1
- package/dist/core/persist/sqljs-backend.d.ts +60 -0
- package/dist/core/persist/sqljs-backend.d.ts.map +1 -0
- package/dist/core/persist/sqljs-backend.js +8 -0
- package/dist/db/index.js +11 -10
- package/dist/embeddings/index.js +1 -1
- package/dist/embeddings/store.d.ts.map +1 -1
- package/dist/{index-0zk3fx2s.js → index-4wxa8xz4.js} +4 -237
- package/dist/{index-6n5dcebj.js → index-53f3b8p8.js} +84 -30
- package/dist/{index-y3d71wzd.js → index-7pjz3tsy.js} +36 -6
- package/dist/{index-0q7wbasy.js → index-a2a394zz.js} +7 -3
- package/dist/index-h32txmxe.js +42 -0
- package/dist/index-h7zxhhhh.js +252 -0
- package/dist/index-h9e2efx4.js +251 -0
- package/dist/{index-2917tjd8.js → index-hr9qvv77.js} +15 -3
- package/dist/{index-hmdbnd4n.js → index-hy73j9z8.js} +1 -1
- package/dist/{index-q31hfjja.js → index-jgda3xyv.js} +1 -1
- package/dist/{index-7e27kvvj.js → index-wncptktd.js} +1 -1
- package/dist/index.js +8 -6
- package/dist/react/index.js +5 -4
- package/dist/{sdk-snn5gad3.js → sdk-bepky0xs.js} +5 -4
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +27 -24
- package/dist/server/node-adapter.d.ts +38 -0
- package/dist/server/node-adapter.d.ts.map +1 -0
- package/dist/server/node-adapter.js +108 -0
- package/dist/server/server-shared.d.ts +21 -0
- package/dist/server/server-shared.d.ts.map +1 -0
- package/dist/server/server.d.ts +19 -2
- package/dist/server/server.d.ts.map +1 -1
- package/dist/server/tenancy.d.ts +28 -2
- package/dist/server/tenancy.d.ts.map +1 -1
- package/dist/{server-mrctdwzr.js → server-szdjx0nt.js} +5 -3
- package/dist/sqlite-backend-0vsmc6qj.js +8 -0
- package/dist/{tenancy-7d1g4ayp.js → tenancy-pjm32b4v.js} +4 -3
- package/dist/vcs/blob-store.d.ts +2 -1
- package/dist/vcs/blob-store.d.ts.map +1 -1
- package/dist/vcs/index.js +2 -2
- package/package.json +16 -3
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// @bun
|
|
2
|
+
import {
|
|
3
|
+
QueryEngine
|
|
4
|
+
} from "./index-yp88he8n.js";
|
|
2
5
|
import {
|
|
3
6
|
EAVStore,
|
|
4
7
|
init_eav_store
|
|
5
8
|
} from "./index-yhwjgfvj.js";
|
|
6
|
-
import {
|
|
7
|
-
QueryEngine
|
|
8
|
-
} from "./index-yp88he8n.js";
|
|
9
9
|
|
|
10
10
|
// src/core/kernel/trellis-kernel.ts
|
|
11
11
|
init_eav_store();
|
|
@@ -768,237 +768,4 @@ class TrellisKernel {
|
|
|
768
768
|
}
|
|
769
769
|
}
|
|
770
770
|
|
|
771
|
-
|
|
772
|
-
import { Database } from "bun:sqlite";
|
|
773
|
-
var SCHEMA_SQL = `
|
|
774
|
-
CREATE TABLE IF NOT EXISTS ops (
|
|
775
|
-
hash TEXT PRIMARY KEY,
|
|
776
|
-
kind TEXT NOT NULL,
|
|
777
|
-
timestamp TEXT NOT NULL,
|
|
778
|
-
agent_id TEXT NOT NULL,
|
|
779
|
-
previous_hash TEXT,
|
|
780
|
-
payload TEXT NOT NULL
|
|
781
|
-
);
|
|
782
|
-
|
|
783
|
-
CREATE TABLE IF NOT EXISTS snapshots (
|
|
784
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
785
|
-
last_op_hash TEXT NOT NULL,
|
|
786
|
-
data TEXT NOT NULL,
|
|
787
|
-
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
788
|
-
);
|
|
789
|
-
|
|
790
|
-
CREATE TABLE IF NOT EXISTS blobs (
|
|
791
|
-
hash TEXT PRIMARY KEY,
|
|
792
|
-
content BLOB NOT NULL
|
|
793
|
-
);
|
|
794
|
-
|
|
795
|
-
CREATE INDEX IF NOT EXISTS idx_ops_kind ON ops(kind);
|
|
796
|
-
CREATE INDEX IF NOT EXISTS idx_ops_timestamp ON ops(timestamp);
|
|
797
|
-
CREATE INDEX IF NOT EXISTS idx_ops_agent ON ops(agent_id);
|
|
798
|
-
CREATE INDEX IF NOT EXISTS idx_ops_previous ON ops(previous_hash);
|
|
799
|
-
CREATE INDEX IF NOT EXISTS idx_snapshots_op ON snapshots(last_op_hash);
|
|
800
|
-
`;
|
|
801
|
-
|
|
802
|
-
class SqliteKernelBackend {
|
|
803
|
-
dbPath;
|
|
804
|
-
db;
|
|
805
|
-
_stmts = null;
|
|
806
|
-
constructor(dbPath) {
|
|
807
|
-
this.dbPath = dbPath;
|
|
808
|
-
this.db = new Database(dbPath);
|
|
809
|
-
}
|
|
810
|
-
init() {
|
|
811
|
-
this.db.exec("PRAGMA journal_mode=WAL;");
|
|
812
|
-
this.db.exec("PRAGMA foreign_keys=ON;");
|
|
813
|
-
this.db.exec("PRAGMA synchronous=NORMAL;");
|
|
814
|
-
this.db.exec(SCHEMA_SQL);
|
|
815
|
-
this._prepareStatements();
|
|
816
|
-
}
|
|
817
|
-
_prepareStatements() {
|
|
818
|
-
this._stmts = {
|
|
819
|
-
insert: this.db.prepare(`
|
|
820
|
-
INSERT OR IGNORE INTO ops (hash, kind, timestamp, agent_id, previous_hash, payload)
|
|
821
|
-
VALUES ($hash, $kind, $timestamp, $agentId, $previousHash, $payload)
|
|
822
|
-
`),
|
|
823
|
-
readAll: this.db.prepare(`
|
|
824
|
-
SELECT hash, kind, timestamp, agent_id, previous_hash, payload
|
|
825
|
-
FROM ops ORDER BY rowid ASC
|
|
826
|
-
`),
|
|
827
|
-
readUntil: this.db.prepare(`
|
|
828
|
-
SELECT hash, kind, timestamp, agent_id, previous_hash, payload
|
|
829
|
-
FROM ops WHERE rowid <= (SELECT rowid FROM ops WHERE hash = $hash)
|
|
830
|
-
ORDER BY rowid ASC
|
|
831
|
-
`),
|
|
832
|
-
readAfter: this.db.prepare(`
|
|
833
|
-
SELECT hash, kind, timestamp, agent_id, previous_hash, payload
|
|
834
|
-
FROM ops WHERE rowid > (SELECT rowid FROM ops WHERE hash = $hash)
|
|
835
|
-
ORDER BY rowid ASC
|
|
836
|
-
`),
|
|
837
|
-
getByHash: this.db.prepare(`
|
|
838
|
-
SELECT hash, kind, timestamp, agent_id, previous_hash, payload
|
|
839
|
-
FROM ops WHERE hash = $hash
|
|
840
|
-
`),
|
|
841
|
-
getLast: this.db.prepare(`
|
|
842
|
-
SELECT hash, kind, timestamp, agent_id, previous_hash, payload
|
|
843
|
-
FROM ops ORDER BY rowid DESC LIMIT 1
|
|
844
|
-
`),
|
|
845
|
-
count: this.db.prepare("SELECT COUNT(*) as cnt FROM ops"),
|
|
846
|
-
saveSnapshot: this.db.prepare(`
|
|
847
|
-
INSERT INTO snapshots (last_op_hash, data)
|
|
848
|
-
VALUES ($lastOpHash, $data)
|
|
849
|
-
`),
|
|
850
|
-
loadSnapshot: this.db.prepare(`
|
|
851
|
-
SELECT last_op_hash, data FROM snapshots
|
|
852
|
-
ORDER BY id DESC LIMIT 1
|
|
853
|
-
`),
|
|
854
|
-
putBlob: this.db.prepare(`
|
|
855
|
-
INSERT OR IGNORE INTO blobs (hash, content) VALUES ($hash, $content)
|
|
856
|
-
`),
|
|
857
|
-
getBlob: this.db.prepare(`
|
|
858
|
-
SELECT content FROM blobs WHERE hash = $hash
|
|
859
|
-
`),
|
|
860
|
-
hasBlob: this.db.prepare(`
|
|
861
|
-
SELECT 1 FROM blobs WHERE hash = $hash
|
|
862
|
-
`)
|
|
863
|
-
};
|
|
864
|
-
}
|
|
865
|
-
append(op) {
|
|
866
|
-
const payload = JSON.stringify({
|
|
867
|
-
facts: op.facts,
|
|
868
|
-
links: op.links,
|
|
869
|
-
...op.deleteFacts?.length ? { deleteFacts: op.deleteFacts } : {},
|
|
870
|
-
...op.deleteLinks?.length ? { deleteLinks: op.deleteLinks } : {},
|
|
871
|
-
...op.vcs ? { vcs: op.vcs } : {},
|
|
872
|
-
...op.signature ? { signature: op.signature } : {}
|
|
873
|
-
});
|
|
874
|
-
this._stmts.insert.run({
|
|
875
|
-
$hash: op.hash,
|
|
876
|
-
$kind: op.kind,
|
|
877
|
-
$timestamp: op.timestamp,
|
|
878
|
-
$agentId: op.agentId,
|
|
879
|
-
$previousHash: op.previousHash ?? null,
|
|
880
|
-
$payload: payload
|
|
881
|
-
});
|
|
882
|
-
}
|
|
883
|
-
appendBatch(ops) {
|
|
884
|
-
if (ops.length === 0)
|
|
885
|
-
return;
|
|
886
|
-
this.db.transaction(() => {
|
|
887
|
-
for (const op of ops) {
|
|
888
|
-
this.append(op);
|
|
889
|
-
}
|
|
890
|
-
})();
|
|
891
|
-
}
|
|
892
|
-
readAll() {
|
|
893
|
-
const rows = this._stmts.readAll.all();
|
|
894
|
-
return rows.map(rowToOp);
|
|
895
|
-
}
|
|
896
|
-
readUntil(hash) {
|
|
897
|
-
const rows = this._stmts.readUntil.all({ $hash: hash });
|
|
898
|
-
return rows.map(rowToOp);
|
|
899
|
-
}
|
|
900
|
-
readAfter(hash) {
|
|
901
|
-
const rows = this._stmts.readAfter.all({ $hash: hash });
|
|
902
|
-
return rows.map(rowToOp);
|
|
903
|
-
}
|
|
904
|
-
readUntilTimestamp(isoTimestamp) {
|
|
905
|
-
const rows = this.db.prepare(`SELECT hash, kind, timestamp, agent_id, previous_hash, payload
|
|
906
|
-
FROM ops WHERE timestamp <= $ts ORDER BY rowid ASC`).all({ $ts: isoTimestamp });
|
|
907
|
-
return rows.map(rowToOp);
|
|
908
|
-
}
|
|
909
|
-
getLastOp() {
|
|
910
|
-
const row = this._stmts.getLast.get();
|
|
911
|
-
return row ? rowToOp(row) : undefined;
|
|
912
|
-
}
|
|
913
|
-
getByHash(hash) {
|
|
914
|
-
return this.getOpByHash(hash);
|
|
915
|
-
}
|
|
916
|
-
getOpCount() {
|
|
917
|
-
return this.count();
|
|
918
|
-
}
|
|
919
|
-
getOpByHash(hash) {
|
|
920
|
-
const row = this._stmts.getByHash.get({ $hash: hash });
|
|
921
|
-
return row ? rowToOp(row) : undefined;
|
|
922
|
-
}
|
|
923
|
-
count() {
|
|
924
|
-
const row = this._stmts.count.get();
|
|
925
|
-
return row?.cnt ?? 0;
|
|
926
|
-
}
|
|
927
|
-
findCommonAncestor(hashA, hashB) {
|
|
928
|
-
const ancestorsA = new Set;
|
|
929
|
-
let cursor = hashA;
|
|
930
|
-
while (cursor) {
|
|
931
|
-
ancestorsA.add(cursor);
|
|
932
|
-
const op = this.getOpByHash(cursor);
|
|
933
|
-
cursor = op?.previousHash;
|
|
934
|
-
}
|
|
935
|
-
cursor = hashB;
|
|
936
|
-
while (cursor) {
|
|
937
|
-
if (ancestorsA.has(cursor)) {
|
|
938
|
-
return this.getOpByHash(cursor);
|
|
939
|
-
}
|
|
940
|
-
const op = this.getOpByHash(cursor);
|
|
941
|
-
cursor = op?.previousHash;
|
|
942
|
-
}
|
|
943
|
-
return;
|
|
944
|
-
}
|
|
945
|
-
saveSnapshot(lastOpHash, data) {
|
|
946
|
-
this._stmts.saveSnapshot.run({
|
|
947
|
-
$lastOpHash: lastOpHash,
|
|
948
|
-
$data: JSON.stringify(data)
|
|
949
|
-
});
|
|
950
|
-
}
|
|
951
|
-
loadLatestSnapshot() {
|
|
952
|
-
const row = this._stmts.loadSnapshot.get();
|
|
953
|
-
if (!row)
|
|
954
|
-
return;
|
|
955
|
-
return {
|
|
956
|
-
lastOpHash: row.last_op_hash,
|
|
957
|
-
data: JSON.parse(row.data)
|
|
958
|
-
};
|
|
959
|
-
}
|
|
960
|
-
putBlob(hash, content) {
|
|
961
|
-
this._stmts.putBlob.run({
|
|
962
|
-
$hash: hash,
|
|
963
|
-
$content: Buffer.from(content)
|
|
964
|
-
});
|
|
965
|
-
}
|
|
966
|
-
getBlob(hash) {
|
|
967
|
-
const row = this._stmts.getBlob.get({ $hash: hash });
|
|
968
|
-
if (!row)
|
|
969
|
-
return;
|
|
970
|
-
return new Uint8Array(row.content);
|
|
971
|
-
}
|
|
972
|
-
hasBlob(hash) {
|
|
973
|
-
return !!this._stmts.hasBlob.get({ $hash: hash });
|
|
974
|
-
}
|
|
975
|
-
close() {
|
|
976
|
-
this.db.close();
|
|
977
|
-
}
|
|
978
|
-
}
|
|
979
|
-
function rowToOp(row) {
|
|
980
|
-
const payload = JSON.parse(row.payload);
|
|
981
|
-
const op = {
|
|
982
|
-
hash: row.hash,
|
|
983
|
-
kind: row.kind,
|
|
984
|
-
timestamp: row.timestamp,
|
|
985
|
-
agentId: row.agent_id
|
|
986
|
-
};
|
|
987
|
-
if (row.previous_hash)
|
|
988
|
-
op.previousHash = row.previous_hash;
|
|
989
|
-
if (payload.facts)
|
|
990
|
-
op.facts = payload.facts;
|
|
991
|
-
if (payload.links)
|
|
992
|
-
op.links = payload.links;
|
|
993
|
-
if (payload.deleteFacts)
|
|
994
|
-
op.deleteFacts = payload.deleteFacts;
|
|
995
|
-
if (payload.deleteLinks)
|
|
996
|
-
op.deleteLinks = payload.deleteLinks;
|
|
997
|
-
if (payload.vcs)
|
|
998
|
-
op.vcs = payload.vcs;
|
|
999
|
-
if (payload.signature)
|
|
1000
|
-
op.signature = payload.signature;
|
|
1001
|
-
return op;
|
|
1002
|
-
}
|
|
1003
|
-
|
|
1004
|
-
export { TrellisKernel, SqliteKernelBackend };
|
|
771
|
+
export { TrellisKernel };
|
|
@@ -5,10 +5,14 @@ import {
|
|
|
5
5
|
import {
|
|
6
6
|
QueryEngine
|
|
7
7
|
} from "./index-yp88he8n.js";
|
|
8
|
+
import {
|
|
9
|
+
__require
|
|
10
|
+
} from "./index-a76rekgs.js";
|
|
8
11
|
|
|
9
12
|
// src/server/server.ts
|
|
10
13
|
import { existsSync, readFileSync } from "fs";
|
|
11
|
-
import { join } from "path";
|
|
14
|
+
import { dirname, join } from "path";
|
|
15
|
+
import { fileURLToPath } from "url";
|
|
12
16
|
|
|
13
17
|
// src/server/auth.ts
|
|
14
18
|
var ANONYMOUS = {
|
|
@@ -415,7 +419,22 @@ function computeDiff(prev, next) {
|
|
|
415
419
|
}
|
|
416
420
|
|
|
417
421
|
// src/server/server.ts
|
|
422
|
+
var __moduleDir = import.meta.dir ?? dirname(fileURLToPath(import.meta.url));
|
|
418
423
|
function startServer(opts) {
|
|
424
|
+
return startServerBun(opts);
|
|
425
|
+
}
|
|
426
|
+
async function startServerCrossRuntime(opts) {
|
|
427
|
+
if (typeof globalThis.Bun !== "undefined") {
|
|
428
|
+
const bunServer = startServerBun(opts);
|
|
429
|
+
return {
|
|
430
|
+
port: bunServer.port ?? opts.port ?? opts.config.port ?? 3000,
|
|
431
|
+
hostname: bunServer.hostname,
|
|
432
|
+
stop: (closeActive) => bunServer.stop(closeActive)
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
return startServerNode(opts);
|
|
436
|
+
}
|
|
437
|
+
function buildServerContext(opts) {
|
|
419
438
|
const { pool, permissions, config } = opts;
|
|
420
439
|
const port = opts.port ?? config.port ?? 3000;
|
|
421
440
|
const authConfig = {
|
|
@@ -424,35 +443,42 @@ function startServer(opts) {
|
|
|
424
443
|
allowPublic: true
|
|
425
444
|
};
|
|
426
445
|
const subs = new SubscriptionManager(pool, permissions ?? null);
|
|
427
|
-
const
|
|
446
|
+
const handleHttp = async (req) => {
|
|
447
|
+
const url = new URL(req.url);
|
|
448
|
+
const path = url.pathname;
|
|
449
|
+
const auth = await resolveAuth(req.headers.get("authorization"), authConfig);
|
|
450
|
+
const tenantId = auth.tenantId ?? url.searchParams.get("tenantId") ?? null;
|
|
451
|
+
try {
|
|
452
|
+
return await route(req, url, path, auth, tenantId, {
|
|
453
|
+
pool,
|
|
454
|
+
permissions: permissions ?? null,
|
|
455
|
+
subs,
|
|
456
|
+
authConfig,
|
|
457
|
+
config,
|
|
458
|
+
oauthProviders: opts.oauthProviders ?? {}
|
|
459
|
+
});
|
|
460
|
+
} catch (err) {
|
|
461
|
+
if (err instanceof PermissionError) {
|
|
462
|
+
return json(err.toResponse(), 403);
|
|
463
|
+
}
|
|
464
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
465
|
+
return json({ error: "Internal Server Error", message: msg }, 500);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
return { port, authConfig, subs, handleHttp };
|
|
469
|
+
}
|
|
470
|
+
function startServerBun(opts) {
|
|
471
|
+
const { port, subs, handleHttp } = buildServerContext(opts);
|
|
472
|
+
return Bun.serve({
|
|
428
473
|
port,
|
|
429
|
-
async fetch(req,
|
|
430
|
-
const url = new URL(req.url);
|
|
431
|
-
const path = url.pathname;
|
|
474
|
+
async fetch(req, server) {
|
|
432
475
|
if (req.headers.get("upgrade") === "websocket") {
|
|
433
|
-
const upgraded =
|
|
476
|
+
const upgraded = server.upgrade(req, { data: undefined });
|
|
434
477
|
if (upgraded)
|
|
435
478
|
return;
|
|
436
479
|
return new Response("WebSocket upgrade failed", { status: 400 });
|
|
437
480
|
}
|
|
438
|
-
|
|
439
|
-
const tenantId = auth.tenantId ?? url.searchParams.get("tenantId") ?? null;
|
|
440
|
-
try {
|
|
441
|
-
return await route(req, url, path, auth, tenantId, {
|
|
442
|
-
pool,
|
|
443
|
-
permissions: permissions ?? null,
|
|
444
|
-
subs,
|
|
445
|
-
authConfig,
|
|
446
|
-
config,
|
|
447
|
-
oauthProviders: opts.oauthProviders ?? {}
|
|
448
|
-
});
|
|
449
|
-
} catch (err) {
|
|
450
|
-
if (err instanceof PermissionError) {
|
|
451
|
-
return json(err.toResponse(), 403);
|
|
452
|
-
}
|
|
453
|
-
const msg = err instanceof Error ? err.message : String(err);
|
|
454
|
-
return json({ error: "Internal Server Error", message: msg }, 500);
|
|
455
|
-
}
|
|
481
|
+
return handleHttp(req);
|
|
456
482
|
},
|
|
457
483
|
websocket: {
|
|
458
484
|
async open(ws) {
|
|
@@ -476,14 +502,42 @@ function startServer(opts) {
|
|
|
476
502
|
}
|
|
477
503
|
}
|
|
478
504
|
});
|
|
479
|
-
|
|
505
|
+
}
|
|
506
|
+
async function startServerNode(opts) {
|
|
507
|
+
const { port, subs, handleHttp } = buildServerContext(opts);
|
|
508
|
+
const { startNodeServer } = await import("./server/node-adapter.js");
|
|
509
|
+
return startNodeServer({
|
|
510
|
+
port,
|
|
511
|
+
fetch: handleHttp,
|
|
512
|
+
websocket: {
|
|
513
|
+
open(ws) {
|
|
514
|
+
const id = crypto.randomUUID();
|
|
515
|
+
ws.__clientId = id;
|
|
516
|
+
subs.addClient(id, ws, {
|
|
517
|
+
userId: null,
|
|
518
|
+
tenantId: null,
|
|
519
|
+
roles: [],
|
|
520
|
+
claims: {},
|
|
521
|
+
authenticated: false
|
|
522
|
+
}, null);
|
|
523
|
+
},
|
|
524
|
+
async message(ws, raw) {
|
|
525
|
+
const id = ws.__clientId;
|
|
526
|
+
await subs.handleMessage(id, typeof raw === "string" ? raw : raw.toString());
|
|
527
|
+
},
|
|
528
|
+
close(ws) {
|
|
529
|
+
const id = ws.__clientId;
|
|
530
|
+
subs.removeClient(id);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
});
|
|
480
534
|
}
|
|
481
535
|
async function route(req, url, path, auth, tenantId, ctx) {
|
|
482
536
|
const method = req.method.toUpperCase();
|
|
483
537
|
if (method === "GET" && path === "/__trellis/inspector.js") {
|
|
484
538
|
const candidates = [
|
|
485
|
-
join(
|
|
486
|
-
join(
|
|
539
|
+
join(__moduleDir, "db", "inspector.js"),
|
|
540
|
+
join(__moduleDir, "inspector.js")
|
|
487
541
|
];
|
|
488
542
|
for (const p of candidates) {
|
|
489
543
|
if (existsSync(p)) {
|
|
@@ -498,8 +552,8 @@ async function route(req, url, path, auth, tenantId, ctx) {
|
|
|
498
552
|
}
|
|
499
553
|
if (method === "GET" && path === "/__trellis/trellis.css") {
|
|
500
554
|
const candidates = [
|
|
501
|
-
join(
|
|
502
|
-
join(
|
|
555
|
+
join(__moduleDir, "db", "trellis.css"),
|
|
556
|
+
join(__moduleDir, "trellis.css")
|
|
503
557
|
];
|
|
504
558
|
for (const p of candidates) {
|
|
505
559
|
if (existsSync(p)) {
|
|
@@ -844,4 +898,4 @@ function json(data, status = 200) {
|
|
|
844
898
|
});
|
|
845
899
|
}
|
|
846
900
|
|
|
847
|
-
export { ANONYMOUS, signJwt, verifyJwt, resolveAuth, GOOGLE_PROVIDER, GITHUB_PROVIDER, buildOAuthUrl, exchangeOAuthCode, PermissionRegistry, PermissionError, PUBLIC_READ, FULLY_PUBLIC, OWNER_ONLY, ADMIN_ONLY, SubscriptionManager, startServer };
|
|
901
|
+
export { ANONYMOUS, signJwt, verifyJwt, resolveAuth, GOOGLE_PROVIDER, GITHUB_PROVIDER, buildOAuthUrl, exchangeOAuthCode, PermissionRegistry, PermissionError, PUBLIC_READ, FULLY_PUBLIC, OWNER_ONLY, ADMIN_ONLY, SubscriptionManager, startServer, startServerCrossRuntime };
|
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
import {
|
|
3
|
-
SqliteKernelBackend,
|
|
4
3
|
TrellisKernel
|
|
5
|
-
} from "./index-
|
|
4
|
+
} from "./index-4wxa8xz4.js";
|
|
5
|
+
import {
|
|
6
|
+
SqliteKernelBackend
|
|
7
|
+
} from "./index-h7zxhhhh.js";
|
|
8
|
+
import {
|
|
9
|
+
__require
|
|
10
|
+
} from "./index-a76rekgs.js";
|
|
6
11
|
|
|
7
12
|
// src/server/tenancy.ts
|
|
8
13
|
import { existsSync, mkdirSync } from "fs";
|
|
@@ -14,18 +19,34 @@ class TenantPool {
|
|
|
14
19
|
pool = new Map;
|
|
15
20
|
dbPath;
|
|
16
21
|
agentId;
|
|
17
|
-
|
|
22
|
+
backendOpts;
|
|
23
|
+
constructor(dbPath, agentIdOrOpts = "trellis-db") {
|
|
18
24
|
this.dbPath = resolve(dbPath);
|
|
19
|
-
|
|
25
|
+
if (typeof agentIdOrOpts === "string") {
|
|
26
|
+
this.agentId = agentIdOrOpts;
|
|
27
|
+
this.backendOpts = undefined;
|
|
28
|
+
} else {
|
|
29
|
+
this.agentId = agentIdOrOpts.agentId ?? "trellis-db";
|
|
30
|
+
this.backendOpts = agentIdOrOpts.backend;
|
|
31
|
+
}
|
|
20
32
|
this._ensureDirs();
|
|
21
33
|
}
|
|
22
34
|
get(tenantId) {
|
|
23
35
|
const id = tenantId ?? DEFAULT_TENANT;
|
|
24
36
|
if (!this.pool.has(id)) {
|
|
25
|
-
this.pool.set(id, this.
|
|
37
|
+
this.pool.set(id, this._createKernelSync(id));
|
|
26
38
|
}
|
|
27
39
|
return this.pool.get(id);
|
|
28
40
|
}
|
|
41
|
+
async preload(tenantId) {
|
|
42
|
+
const id = tenantId ?? DEFAULT_TENANT;
|
|
43
|
+
const cached = this.pool.get(id);
|
|
44
|
+
if (cached)
|
|
45
|
+
return cached;
|
|
46
|
+
const kernel = await this._createKernelAsync(id);
|
|
47
|
+
this.pool.set(id, kernel);
|
|
48
|
+
return kernel;
|
|
49
|
+
}
|
|
29
50
|
has(tenantId) {
|
|
30
51
|
return this.pool.has(tenantId);
|
|
31
52
|
}
|
|
@@ -52,9 +73,18 @@ class TenantPool {
|
|
|
52
73
|
}
|
|
53
74
|
return join(this.dbPath, TENANTS_DIR, `${id}.sqlite`);
|
|
54
75
|
}
|
|
55
|
-
|
|
76
|
+
_createKernelSync(tenantId) {
|
|
56
77
|
const sqlitePath = this.dbFilePath(tenantId);
|
|
57
78
|
const backend = new SqliteKernelBackend(sqlitePath);
|
|
79
|
+
return this._wrapKernel(backend);
|
|
80
|
+
}
|
|
81
|
+
async _createKernelAsync(tenantId) {
|
|
82
|
+
const sqlitePath = this.dbFilePath(tenantId);
|
|
83
|
+
const { createKernelBackend } = await import("./core/persist/factory.js");
|
|
84
|
+
const backend = await createKernelBackend(sqlitePath, this.backendOpts);
|
|
85
|
+
return this._wrapKernel(backend);
|
|
86
|
+
}
|
|
87
|
+
_wrapKernel(backend) {
|
|
58
88
|
const kernel = new TrellisKernel({
|
|
59
89
|
backend,
|
|
60
90
|
agentId: this.agentId,
|
|
@@ -434,9 +434,13 @@ class BlobStore {
|
|
|
434
434
|
return this.hexFromBuffer(hashBuffer);
|
|
435
435
|
}
|
|
436
436
|
hashSync(content) {
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
437
|
+
if (typeof globalThis.Bun !== "undefined") {
|
|
438
|
+
const hasher = new globalThis.Bun.CryptoHasher("sha256");
|
|
439
|
+
hasher.update(content);
|
|
440
|
+
return hasher.digest("hex");
|
|
441
|
+
}
|
|
442
|
+
const { createHash } = __require("crypto");
|
|
443
|
+
return createHash("sha256").update(content).digest("hex");
|
|
440
444
|
}
|
|
441
445
|
count() {
|
|
442
446
|
try {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
import {
|
|
3
|
+
__require
|
|
4
|
+
} from "./index-a76rekgs.js";
|
|
5
|
+
|
|
6
|
+
// src/core/persist/factory.ts
|
|
7
|
+
async function createKernelBackend(dbPath, opts = {}) {
|
|
8
|
+
const choice = opts.backend ?? detectBackend();
|
|
9
|
+
if (choice === "bun") {
|
|
10
|
+
const { SqliteKernelBackend } = await import("./sqlite-backend-0vsmc6qj.js");
|
|
11
|
+
const backend2 = new SqliteKernelBackend(dbPath);
|
|
12
|
+
backend2.init();
|
|
13
|
+
return backend2;
|
|
14
|
+
}
|
|
15
|
+
if (choice === "better-sqlite") {
|
|
16
|
+
const { BetterSqliteKernelBackend } = await import("./better-sqlite-backend-ahx5p0br.js");
|
|
17
|
+
const backend2 = new BetterSqliteKernelBackend(dbPath);
|
|
18
|
+
backend2.init();
|
|
19
|
+
return backend2;
|
|
20
|
+
}
|
|
21
|
+
const { SqlJsKernelBackend } = await import("./core/persist/sqljs-backend.js");
|
|
22
|
+
const backend = await SqlJsKernelBackend.create({
|
|
23
|
+
dbPath,
|
|
24
|
+
autoFlushEvery: opts.sqljs?.autoFlushEvery
|
|
25
|
+
});
|
|
26
|
+
backend.init();
|
|
27
|
+
return backend;
|
|
28
|
+
}
|
|
29
|
+
function detectBackend() {
|
|
30
|
+
if (typeof globalThis.Bun !== "undefined")
|
|
31
|
+
return "bun";
|
|
32
|
+
try {
|
|
33
|
+
const { createRequire } = __require("module");
|
|
34
|
+
const req = createRequire(import.meta.url);
|
|
35
|
+
req.resolve("better-sqlite3");
|
|
36
|
+
return "better-sqlite";
|
|
37
|
+
} catch {
|
|
38
|
+
return "sqljs";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { createKernelBackend };
|