trellis 3.1.6 → 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.
Files changed (56) hide show
  1. package/README.md +1 -0
  2. package/bin/trellis.mjs +68 -2
  3. package/dist/better-sqlite-backend-ahx5p0br.js +147 -0
  4. package/dist/cli/index.js +116 -45
  5. package/dist/client/index.js +5 -4
  6. package/dist/cms/client.d.ts +2 -1
  7. package/dist/cms/client.d.ts.map +1 -1
  8. package/dist/cms/index.js +4 -0
  9. package/dist/cms/types.d.ts +1 -1
  10. package/dist/cms/types.d.ts.map +1 -1
  11. package/dist/core/index.d.ts +4 -0
  12. package/dist/core/index.d.ts.map +1 -1
  13. package/dist/core/index.js +14 -4
  14. package/dist/core/persist/factory.d.ts +28 -0
  15. package/dist/core/persist/factory.d.ts.map +1 -0
  16. package/dist/core/persist/factory.js +8 -0
  17. package/dist/core/persist/sqlite-backend.d.ts.map +1 -1
  18. package/dist/core/persist/sqljs-backend.d.ts +60 -0
  19. package/dist/core/persist/sqljs-backend.d.ts.map +1 -0
  20. package/dist/core/persist/sqljs-backend.js +8 -0
  21. package/dist/db/index.js +11 -10
  22. package/dist/embeddings/index.js +1 -1
  23. package/dist/embeddings/store.d.ts.map +1 -1
  24. package/dist/{index-0zk3fx2s.js → index-4wxa8xz4.js} +4 -237
  25. package/dist/{index-6n5dcebj.js → index-53f3b8p8.js} +84 -30
  26. package/dist/{index-y3d71wzd.js → index-7pjz3tsy.js} +36 -6
  27. package/dist/{index-0q7wbasy.js → index-a2a394zz.js} +7 -3
  28. package/dist/index-h32txmxe.js +42 -0
  29. package/dist/index-h7zxhhhh.js +252 -0
  30. package/dist/index-h9e2efx4.js +251 -0
  31. package/dist/{index-2917tjd8.js → index-hr9qvv77.js} +15 -3
  32. package/dist/{index-hmdbnd4n.js → index-hy73j9z8.js} +1 -1
  33. package/dist/{index-q31hfjja.js → index-jgda3xyv.js} +1 -1
  34. package/dist/{index-7e27kvvj.js → index-wncptktd.js} +1 -1
  35. package/dist/index.js +8 -6
  36. package/dist/react/index.js +5 -4
  37. package/dist/{sdk-snn5gad3.js → sdk-bepky0xs.js} +5 -4
  38. package/dist/server/index.d.ts +2 -2
  39. package/dist/server/index.d.ts.map +1 -1
  40. package/dist/server/index.js +27 -24
  41. package/dist/server/node-adapter.d.ts +38 -0
  42. package/dist/server/node-adapter.d.ts.map +1 -0
  43. package/dist/server/node-adapter.js +108 -0
  44. package/dist/server/server-shared.d.ts +21 -0
  45. package/dist/server/server-shared.d.ts.map +1 -0
  46. package/dist/server/server.d.ts +19 -2
  47. package/dist/server/server.d.ts.map +1 -1
  48. package/dist/server/tenancy.d.ts +28 -2
  49. package/dist/server/tenancy.d.ts.map +1 -1
  50. package/dist/{server-mrctdwzr.js → server-szdjx0nt.js} +5 -3
  51. package/dist/sqlite-backend-0vsmc6qj.js +8 -0
  52. package/dist/{tenancy-7d1g4ayp.js → tenancy-pjm32b4v.js} +4 -3
  53. package/dist/vcs/blob-store.d.ts +2 -1
  54. package/dist/vcs/blob-store.d.ts.map +1 -1
  55. package/dist/vcs/index.js +2 -2
  56. package/package.json +16 -3
@@ -0,0 +1,251 @@
1
+ // @bun
2
+ import {
3
+ __require
4
+ } from "./index-a76rekgs.js";
5
+
6
+ // src/core/persist/sqljs-backend.ts
7
+ var SCHEMA_SQL = `
8
+ CREATE TABLE IF NOT EXISTS ops (
9
+ hash TEXT PRIMARY KEY,
10
+ kind TEXT NOT NULL,
11
+ timestamp TEXT NOT NULL,
12
+ agent_id TEXT NOT NULL,
13
+ previous_hash TEXT,
14
+ payload TEXT NOT NULL
15
+ );
16
+ CREATE TABLE IF NOT EXISTS snapshots (
17
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
18
+ last_op_hash TEXT NOT NULL,
19
+ data TEXT NOT NULL,
20
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
21
+ );
22
+ CREATE TABLE IF NOT EXISTS blobs (
23
+ hash TEXT PRIMARY KEY,
24
+ content BLOB NOT NULL
25
+ );
26
+ CREATE INDEX IF NOT EXISTS idx_ops_kind ON ops(kind);
27
+ CREATE INDEX IF NOT EXISTS idx_ops_timestamp ON ops(timestamp);
28
+ CREATE INDEX IF NOT EXISTS idx_ops_agent ON ops(agent_id);
29
+ CREATE INDEX IF NOT EXISTS idx_ops_previous ON ops(previous_hash);
30
+ CREATE INDEX IF NOT EXISTS idx_snapshots_op ON snapshots(last_op_hash);
31
+ `;
32
+
33
+ class SqlJsKernelBackend {
34
+ opts;
35
+ db;
36
+ stmts;
37
+ writes = 0;
38
+ flushEvery;
39
+ initialized = false;
40
+ constructor(opts) {
41
+ this.opts = opts;
42
+ this.flushEvery = opts.autoFlushEvery ?? 50;
43
+ }
44
+ static async create(opts) {
45
+ const backend = new SqlJsKernelBackend(opts);
46
+ await backend.bootstrap();
47
+ return backend;
48
+ }
49
+ async bootstrap() {
50
+ let initSqlJs;
51
+ try {
52
+ const mod = await import("sql.js");
53
+ initSqlJs = mod.default ?? mod;
54
+ } catch (e) {
55
+ throw new Error('SqlJsKernelBackend requires the optional dependency "sql.js". Install it: npm install sql.js');
56
+ }
57
+ let sqljsDistDir = null;
58
+ if (typeof window === "undefined") {
59
+ try {
60
+ const moduleMod = await import("module");
61
+ const pathMod = await import("path");
62
+ const req = moduleMod.createRequire(import.meta.url);
63
+ const sqlJsEntry = req.resolve("sql.js");
64
+ sqljsDistDir = pathMod.dirname(sqlJsEntry);
65
+ } catch {
66
+ sqljsDistDir = null;
67
+ }
68
+ }
69
+ const SQL = await initSqlJs({
70
+ locateFile: (file) => {
71
+ if (typeof window !== "undefined")
72
+ return `/sql-wasm/${file}`;
73
+ if (sqljsDistDir)
74
+ return `${sqljsDistDir}/${file}`;
75
+ return file;
76
+ }
77
+ });
78
+ const existing = this.loadFromDisk();
79
+ this.db = existing ? new SQL.Database(existing) : new SQL.Database;
80
+ }
81
+ loadFromDisk() {
82
+ if (this.opts.dbPath === ":memory:")
83
+ return null;
84
+ try {
85
+ const fs = __require("fs");
86
+ if (!fs.existsSync(this.opts.dbPath))
87
+ return null;
88
+ return new Uint8Array(fs.readFileSync(this.opts.dbPath));
89
+ } catch {
90
+ return null;
91
+ }
92
+ }
93
+ flushToDisk() {
94
+ if (this.opts.dbPath === ":memory:")
95
+ return;
96
+ try {
97
+ const fs = __require("fs");
98
+ const path = __require("path");
99
+ const data = this.db.export();
100
+ fs.mkdirSync(path.dirname(this.opts.dbPath), { recursive: true });
101
+ const tmp = `${this.opts.dbPath}.tmp`;
102
+ fs.writeFileSync(tmp, Buffer.from(data));
103
+ fs.renameSync(tmp, this.opts.dbPath);
104
+ } catch {}
105
+ }
106
+ init() {
107
+ if (this.initialized)
108
+ return;
109
+ this.db.exec(SCHEMA_SQL);
110
+ this.prepareStatements();
111
+ this.initialized = true;
112
+ }
113
+ prepareStatements() {
114
+ this.stmts = {
115
+ insert: this.db.prepare(`INSERT OR IGNORE INTO ops (hash, kind, timestamp, agent_id, previous_hash, payload)
116
+ VALUES ($hash, $kind, $timestamp, $agentId, $previousHash, $payload)`),
117
+ readAll: this.db.prepare(`SELECT hash, kind, timestamp, agent_id, previous_hash, payload
118
+ FROM ops ORDER BY rowid ASC`),
119
+ readUntil: this.db.prepare(`SELECT hash, kind, timestamp, agent_id, previous_hash, payload
120
+ FROM ops WHERE rowid <= (SELECT rowid FROM ops WHERE hash = $hash)
121
+ ORDER BY rowid ASC`),
122
+ readAfter: this.db.prepare(`SELECT hash, kind, timestamp, agent_id, previous_hash, payload
123
+ FROM ops WHERE rowid > (SELECT rowid FROM ops WHERE hash = $hash)
124
+ ORDER BY rowid ASC`),
125
+ getByHash: this.db.prepare(`SELECT hash, kind, timestamp, agent_id, previous_hash, payload
126
+ FROM ops WHERE hash = $hash`),
127
+ getLast: this.db.prepare(`SELECT hash, kind, timestamp, agent_id, previous_hash, payload
128
+ FROM ops ORDER BY rowid DESC LIMIT 1`),
129
+ count: this.db.prepare(`SELECT COUNT(*) AS cnt FROM ops`),
130
+ saveSnapshot: this.db.prepare(`INSERT INTO snapshots (last_op_hash, data) VALUES ($lastOpHash, $data)`),
131
+ loadSnapshot: this.db.prepare(`SELECT last_op_hash, data FROM snapshots ORDER BY id DESC LIMIT 1`)
132
+ };
133
+ }
134
+ append(op) {
135
+ const payload = JSON.stringify({
136
+ facts: op.facts,
137
+ links: op.links,
138
+ ...op.deleteFacts?.length ? { deleteFacts: op.deleteFacts } : {},
139
+ ...op.deleteLinks?.length ? { deleteLinks: op.deleteLinks } : {},
140
+ ...op.vcs ? { vcs: op.vcs } : {},
141
+ ...op.signature ? { signature: op.signature } : {}
142
+ });
143
+ this.stmts.insert.run({
144
+ $hash: op.hash,
145
+ $kind: op.kind,
146
+ $timestamp: op.timestamp,
147
+ $agentId: op.agentId,
148
+ $previousHash: op.previousHash ?? null,
149
+ $payload: payload
150
+ });
151
+ this.stmts.insert.reset();
152
+ this.tickFlush();
153
+ }
154
+ readAll() {
155
+ return this.runAll(this.stmts.readAll);
156
+ }
157
+ readUntil(hash) {
158
+ return this.runAll(this.stmts.readUntil, { $hash: hash });
159
+ }
160
+ readAfter(hash) {
161
+ return this.runAll(this.stmts.readAfter, { $hash: hash });
162
+ }
163
+ readUntilTimestamp(iso) {
164
+ const stmt = this.db.prepare(`SELECT hash, kind, timestamp, agent_id, previous_hash, payload
165
+ FROM ops WHERE timestamp <= $ts ORDER BY rowid ASC`);
166
+ const rows = this.runAll(stmt, { $ts: iso });
167
+ stmt.free();
168
+ return rows;
169
+ }
170
+ getByHash(hash) {
171
+ return this.runOne(this.stmts.getByHash, { $hash: hash });
172
+ }
173
+ getLastOp() {
174
+ return this.runOne(this.stmts.getLast);
175
+ }
176
+ getOpCount() {
177
+ this.stmts.count.bind({});
178
+ const has = this.stmts.count.step();
179
+ const row = has ? this.stmts.count.getAsObject() : { cnt: 0 };
180
+ this.stmts.count.reset();
181
+ return Number(row.cnt ?? 0);
182
+ }
183
+ saveSnapshot(lastOpHash, data) {
184
+ this.stmts.saveSnapshot.run({
185
+ $lastOpHash: lastOpHash,
186
+ $data: typeof data === "string" ? data : JSON.stringify(data)
187
+ });
188
+ this.stmts.saveSnapshot.reset();
189
+ this.tickFlush();
190
+ }
191
+ loadLatestSnapshot() {
192
+ this.stmts.loadSnapshot.bind({});
193
+ const has = this.stmts.loadSnapshot.step();
194
+ if (!has) {
195
+ this.stmts.loadSnapshot.reset();
196
+ return;
197
+ }
198
+ const row = this.stmts.loadSnapshot.getAsObject();
199
+ this.stmts.loadSnapshot.reset();
200
+ return { lastOpHash: row.last_op_hash, data: row.data };
201
+ }
202
+ close() {
203
+ try {
204
+ this.flushToDisk();
205
+ } finally {
206
+ for (const s of Object.values(this.stmts ?? {}))
207
+ s?.free?.();
208
+ this.db?.close?.();
209
+ }
210
+ }
211
+ flush() {
212
+ this.flushToDisk();
213
+ }
214
+ runAll(stmt, params = {}) {
215
+ stmt.bind(params);
216
+ const rows = [];
217
+ while (stmt.step())
218
+ rows.push(rowToOp(stmt.getAsObject()));
219
+ stmt.reset();
220
+ return rows;
221
+ }
222
+ runOne(stmt, params = {}) {
223
+ stmt.bind(params);
224
+ const has = stmt.step();
225
+ const row = has ? stmt.getAsObject() : undefined;
226
+ stmt.reset();
227
+ return row ? rowToOp(row) : undefined;
228
+ }
229
+ tickFlush() {
230
+ if (this.flushEvery === 0)
231
+ return;
232
+ if (++this.writes % this.flushEvery === 0)
233
+ this.flushToDisk();
234
+ }
235
+ }
236
+ function rowToOp(row) {
237
+ const payload = JSON.parse(row.payload);
238
+ return {
239
+ hash: row.hash,
240
+ kind: row.kind,
241
+ timestamp: row.timestamp,
242
+ agentId: row.agent_id,
243
+ previousHash: row.previous_hash ?? undefined,
244
+ facts: payload.facts,
245
+ links: payload.links,
246
+ deleteFacts: payload.deleteFacts,
247
+ deleteLinks: payload.deleteLinks
248
+ };
249
+ }
250
+
251
+ export { SqlJsKernelBackend };
@@ -72,12 +72,24 @@ var init_model = __esm(() => {
72
72
  });
73
73
 
74
74
  // src/embeddings/store.ts
75
- import { Database } from "bun:sqlite";
75
+ function loadDatabaseCtor() {
76
+ if (_DatabaseCtor)
77
+ return _DatabaseCtor;
78
+ try {
79
+ const { createRequire } = __require("module");
80
+ const requireCJS = createRequire(import.meta.url);
81
+ _DatabaseCtor = requireCJS("bun:sqlite").Database;
82
+ return _DatabaseCtor;
83
+ } catch {
84
+ throw new Error("EmbeddingStore requires the Bun runtime (built-in `bun:sqlite`). " + "It is not available under Node/WebContainer.");
85
+ }
86
+ }
76
87
 
77
88
  class VectorStore {
78
89
  db;
79
90
  constructor(dbPath) {
80
- this.db = new Database(dbPath);
91
+ const DatabaseCtor = loadDatabaseCtor();
92
+ this.db = new DatabaseCtor(dbPath);
81
93
  this.db.exec("PRAGMA journal_mode=WAL;");
82
94
  this.db.exec("PRAGMA foreign_keys=ON;");
83
95
  this.db.exec(SCHEMA_SQL);
@@ -251,7 +263,7 @@ function cosineSimilarity(a, b) {
251
263
  const denom = Math.sqrt(normA) * Math.sqrt(normB);
252
264
  return denom === 0 ? 0 : dot / denom;
253
265
  }
254
- var SCHEMA_SQL = `
266
+ var _DatabaseCtor = null, SCHEMA_SQL = `
255
267
  CREATE TABLE IF NOT EXISTS chunks (
256
268
  id TEXT PRIMARY KEY,
257
269
  entity_id TEXT NOT NULL,
@@ -9,7 +9,7 @@ import {
9
9
  init_issue,
10
10
  init_merge,
11
11
  init_milestone
12
- } from "./index-0q7wbasy.js";
12
+ } from "./index-a2a394zz.js";
13
13
  import {
14
14
  init_ops,
15
15
  init_types,
@@ -42,7 +42,7 @@ import {
42
42
  triageIssue,
43
43
  unblockIssue,
44
44
  updateIssue
45
- } from "./index-0q7wbasy.js";
45
+ } from "./index-a2a394zz.js";
46
46
  import {
47
47
  getDecision,
48
48
  getDecisionChain,
@@ -4,7 +4,7 @@ import {
4
4
  } from "./index-xzym9w0m.js";
5
5
  import {
6
6
  TenantPool
7
- } from "./index-y3d71wzd.js";
7
+ } from "./index-7pjz3tsy.js";
8
8
  import {
9
9
  __require
10
10
  } from "./index-a76rekgs.js";
package/dist/index.js CHANGED
@@ -11,10 +11,10 @@ import {
11
11
  loadProfile,
12
12
  saveProfile,
13
13
  writeAgentScaffold
14
- } from "./index-q31hfjja.js";
14
+ } from "./index-jgda3xyv.js";
15
15
  import {
16
16
  VcsMiddleware
17
- } from "./index-hmdbnd4n.js";
17
+ } from "./index-hy73j9z8.js";
18
18
  import {
19
19
  BlobStore,
20
20
  addCriterion,
@@ -53,7 +53,7 @@ import {
53
53
  triageIssue,
54
54
  unblockIssue,
55
55
  updateIssue
56
- } from "./index-0q7wbasy.js";
56
+ } from "./index-a2a394zz.js";
57
57
  import"./index-65z0xfjw.js";
58
58
  import {
59
59
  DEFAULT_CONFIG,
@@ -70,11 +70,13 @@ import {
70
70
  milestoneEntityId
71
71
  } from "./index-v9b4hqa7.js";
72
72
  import {
73
- SqliteKernelBackend,
74
73
  TrellisKernel
75
- } from "./index-0zk3fx2s.js";
76
- import"./index-yhwjgfvj.js";
74
+ } from "./index-4wxa8xz4.js";
77
75
  import"./index-yp88he8n.js";
76
+ import"./index-yhwjgfvj.js";
77
+ import {
78
+ SqliteKernelBackend
79
+ } from "./index-h7zxhhhh.js";
78
80
  import"./index-a76rekgs.js";
79
81
 
80
82
  // src/index.ts
@@ -1,12 +1,13 @@
1
1
  // @bun
2
2
  import {
3
3
  TrellisDb
4
- } from "../index-7e27kvvj.js";
4
+ } from "../index-wncptktd.js";
5
5
  import"../index-xzym9w0m.js";
6
- import"../index-y3d71wzd.js";
7
- import"../index-0zk3fx2s.js";
8
- import"../index-yhwjgfvj.js";
6
+ import"../index-7pjz3tsy.js";
7
+ import"../index-4wxa8xz4.js";
9
8
  import"../index-yp88he8n.js";
9
+ import"../index-yhwjgfvj.js";
10
+ import"../index-h7zxhhhh.js";
10
11
  import"../index-a76rekgs.js";
11
12
 
12
13
  // src/react/provider.tsx
@@ -2,12 +2,13 @@
2
2
  import {
3
3
  FetchError,
4
4
  TrellisDb
5
- } from "./index-7e27kvvj.js";
5
+ } from "./index-wncptktd.js";
6
6
  import"./index-xzym9w0m.js";
7
- import"./index-y3d71wzd.js";
8
- import"./index-0zk3fx2s.js";
9
- import"./index-yhwjgfvj.js";
7
+ import"./index-7pjz3tsy.js";
8
+ import"./index-4wxa8xz4.js";
10
9
  import"./index-yp88he8n.js";
10
+ import"./index-yhwjgfvj.js";
11
+ import"./index-h7zxhhhh.js";
11
12
  import"./index-a76rekgs.js";
12
13
  export {
13
14
  TrellisDb,
@@ -8,8 +8,8 @@
8
8
  *
9
9
  * @module trellis/server
10
10
  */
11
- export { startServer } from './server.js';
12
- export type { ServerConfig } from './server.js';
11
+ export { startServer, startServerCrossRuntime } from './server.js';
12
+ export type { ServerConfig, TrellisHttpServer } from './server.js';
13
13
  export { resolveAuth, signJwt, verifyJwt, buildOAuthUrl, exchangeOAuthCode, GOOGLE_PROVIDER, GITHUB_PROVIDER, ANONYMOUS, } from './auth.js';
14
14
  export type { AuthContext, AuthConfig, OAuthProvider } from './auth.js';
15
15
  export { PermissionRegistry, PermissionError, PUBLIC_READ, FULLY_PUBLIC, OWNER_ONLY, ADMIN_ONLY, } from './permissions.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,OAAO,EACL,WAAW,EACX,OAAO,EACP,SAAS,EACT,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,eAAe,EACf,SAAS,GACV,MAAM,WAAW,CAAC;AACnB,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAGxE,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,WAAW,EACX,YAAY,EACZ,UAAU,EACV,UAAU,GACX,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG/E,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAG1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,YAAY,EACV,YAAY,IAAI,oBAAoB,EACpC,QAAQ,GACT,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACxD,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG/D,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG/D,OAAO,EACL,YAAY,EACZ,aAAa,EACb,oBAAoB,EACpB,eAAe,EACf,aAAa,GACd,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,eAAe,EACf,WAAW,EACX,aAAa,EACb,eAAe,EACf,SAAS,EACT,QAAQ,GACT,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EAAE,WAAW,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACnE,YAAY,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGnE,OAAO,EACL,WAAW,EACX,OAAO,EACP,SAAS,EACT,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,eAAe,EACf,SAAS,GACV,MAAM,WAAW,CAAC;AACnB,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAGxE,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,WAAW,EACX,YAAY,EACZ,UAAU,EACV,UAAU,GACX,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG/E,OAAO,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAG1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACpD,YAAY,EACV,YAAY,IAAI,oBAAoB,EACpC,QAAQ,GACT,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACxD,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG/D,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,YAAY,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG/D,OAAO,EACL,YAAY,EACZ,aAAa,EACb,oBAAoB,EACpB,eAAe,EACf,aAAa,GACd,MAAM,cAAc,CAAC;AAGtB,OAAO,EACL,YAAY,EACZ,YAAY,EACZ,eAAe,EACf,eAAe,EACf,WAAW,EACX,aAAa,EACb,eAAe,EACf,SAAS,EACT,QAAQ,GACT,MAAM,gBAAgB,CAAC"}
@@ -1,5 +1,25 @@
1
1
  // @bun
2
2
  import"../index-c9h37r6h.js";
3
+ import {
4
+ deploy
5
+ } from "../index-wt8rz4gn.js";
6
+ import {
7
+ getActiveSprite,
8
+ getSprite,
9
+ isSpriteTracked,
10
+ loadVmConfig,
11
+ saveVmConfig,
12
+ setActiveSprite,
13
+ trackSprite,
14
+ untrackSprite
15
+ } from "../index-bmyt7k8n.js";
16
+ import {
17
+ assertSpriteCli,
18
+ resolveSprite,
19
+ runSpriteCmd,
20
+ runSpriteCopy,
21
+ runSpriteInteractive
22
+ } from "../index-y6a4kj0p.js";
3
23
  import {
4
24
  ADMIN_ONLY,
5
25
  ANONYMOUS,
@@ -16,46 +36,29 @@ import {
16
36
  resolveAuth,
17
37
  signJwt,
18
38
  startServer,
39
+ startServerCrossRuntime,
19
40
  verifyJwt
20
- } from "../index-6n5dcebj.js";
41
+ } from "../index-53f3b8p8.js";
21
42
  import {
22
43
  importFile,
23
44
  importRecords
24
45
  } from "../index-skhn0agf.js";
25
- import {
26
- deploy
27
- } from "../index-wt8rz4gn.js";
28
- import {
29
- getActiveSprite,
30
- getSprite,
31
- isSpriteTracked,
32
- loadVmConfig,
33
- saveVmConfig,
34
- setActiveSprite,
35
- trackSprite,
36
- untrackSprite
37
- } from "../index-bmyt7k8n.js";
38
- import {
39
- assertSpriteCli,
40
- resolveSprite,
41
- runSpriteCmd,
42
- runSpriteCopy,
43
- runSpriteInteractive
44
- } from "../index-y6a4kj0p.js";
45
46
  import"../index-n9f2qyh5.js";
46
47
  import"../index-xzym9w0m.js";
47
48
  import {
48
49
  DEFAULT_TENANT,
49
50
  TenantPool
50
- } from "../index-y3d71wzd.js";
51
- import"../index-0zk3fx2s.js";
52
- import"../index-yhwjgfvj.js";
51
+ } from "../index-7pjz3tsy.js";
52
+ import"../index-4wxa8xz4.js";
53
53
  import"../index-yp88he8n.js";
54
+ import"../index-yhwjgfvj.js";
55
+ import"../index-h7zxhhhh.js";
54
56
  import"../index-a76rekgs.js";
55
57
  export {
56
58
  verifyJwt,
57
59
  untrackSprite,
58
60
  trackSprite,
61
+ startServerCrossRuntime,
59
62
  startServer,
60
63
  signJwt,
61
64
  setActiveSprite,
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Node.js HTTP + WebSocket adapter for the Trellis server.
3
+ *
4
+ * Runs the same request handler as the Bun.serve path, but via Node's
5
+ * `node:http` module and the `ws` library. Used when the host runtime is
6
+ * Node (or WebContainer) rather than Bun.
7
+ *
8
+ * Optional dependency: `ws`. Install only if you intend to run Trellis
9
+ * outside of Bun.
10
+ *
11
+ * @module trellis/server
12
+ */
13
+ import type { TrellisHttpServer } from './server-shared.js';
14
+ export interface NodeAdapterOptions {
15
+ port: number;
16
+ hostname?: string;
17
+ /**
18
+ * Fetch-style request handler. Receives a standard `Request`, returns a
19
+ * standard `Response`. The HTTP routing module already produces these.
20
+ */
21
+ fetch: (req: Request) => Promise<Response>;
22
+ /** Hooks invoked for each WebSocket lifecycle event. */
23
+ websocket: {
24
+ open: (ws: WsLike) => void | Promise<void>;
25
+ message: (ws: WsLike, data: string | Buffer) => void | Promise<void>;
26
+ close: (ws: WsLike) => void;
27
+ };
28
+ }
29
+ /**
30
+ * Minimal interface satisfied by both Bun's WebSocket and the `ws` library's
31
+ * WebSocket. The subscription manager only uses `readyState` and `send`.
32
+ */
33
+ export interface WsLike {
34
+ readyState: number;
35
+ send(data: string): void;
36
+ }
37
+ export declare function startNodeServer(opts: NodeAdapterOptions): Promise<TrellisHttpServer>;
38
+ //# sourceMappingURL=node-adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node-adapter.d.ts","sourceRoot":"","sources":["../../src/server/node-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAKH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,KAAK,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC3C,wDAAwD;IACxD,SAAS,EAAE;QACT,IAAI,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;QAC3C,OAAO,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;QACrE,KAAK,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,IAAI,CAAC;KAC7B,CAAC;CACH;AAED;;;GAGG;AACH,MAAM,WAAW,MAAM;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED,wBAAsB,eAAe,CACnC,IAAI,EAAE,kBAAkB,GACvB,OAAO,CAAC,iBAAiB,CAAC,CA6D5B"}
@@ -0,0 +1,108 @@
1
+ // @bun
2
+ import {
3
+ __require
4
+ } from "../index-a76rekgs.js";
5
+
6
+ // src/server/node-adapter.ts
7
+ async function startNodeServer(opts) {
8
+ const http = await import("http");
9
+ let WebSocketServer;
10
+ try {
11
+ ({ WebSocketServer } = await import("ws"));
12
+ } catch {
13
+ throw new Error('Running the Trellis server outside Bun requires the optional dependency "ws". Install it: npm install ws');
14
+ }
15
+ const httpServer = http.createServer(async (req, res) => {
16
+ try {
17
+ const fetchReq = await toFetchRequest(req);
18
+ const fetchRes = await opts.fetch(fetchReq);
19
+ await writeFetchResponse(res, fetchRes);
20
+ } catch (err) {
21
+ const msg = err instanceof Error ? err.message : String(err);
22
+ res.statusCode = 500;
23
+ res.setHeader("Content-Type", "application/json");
24
+ res.end(JSON.stringify({ error: "Internal Server Error", message: msg }));
25
+ }
26
+ });
27
+ const wss = new WebSocketServer({ noServer: true });
28
+ httpServer.on("upgrade", (req, socket, head) => {
29
+ wss.handleUpgrade(req, socket, head, (ws) => {
30
+ wss.emit("connection", ws, req);
31
+ });
32
+ });
33
+ wss.on("connection", (ws) => {
34
+ Promise.resolve(opts.websocket.open(ws)).catch(() => {});
35
+ ws.on("message", (raw) => {
36
+ const data = Array.isArray(raw) ? Buffer.concat(raw).toString() : raw instanceof ArrayBuffer ? Buffer.from(raw).toString() : raw.toString();
37
+ Promise.resolve(opts.websocket.message(ws, data)).catch(() => {});
38
+ });
39
+ ws.on("close", () => opts.websocket.close(ws));
40
+ });
41
+ await new Promise((resolve) => httpServer.listen(opts.port, opts.hostname, resolve));
42
+ const addr = httpServer.address();
43
+ const boundPort = typeof addr === "object" && addr ? addr.port : opts.port;
44
+ const boundHost = typeof addr === "object" && addr ? addr.address : opts.hostname;
45
+ return wrapNodeServer(httpServer, wss, boundPort, boundHost);
46
+ }
47
+ async function toFetchRequest(req) {
48
+ const host = req.headers.host ?? "localhost";
49
+ const protocol = req.socket?.encrypted ? "https" : "http";
50
+ const url = `${protocol}://${host}${req.url ?? "/"}`;
51
+ const method = req.method ?? "GET";
52
+ const headers = new Headers;
53
+ for (const [key, value] of Object.entries(req.headers)) {
54
+ if (Array.isArray(value)) {
55
+ for (const v of value)
56
+ headers.append(key, v);
57
+ } else if (value != null) {
58
+ headers.set(key, value);
59
+ }
60
+ }
61
+ const hasBody = method !== "GET" && method !== "HEAD";
62
+ const body = hasBody ? new Uint8Array(await readBody(req)) : undefined;
63
+ return new Request(url, { method, headers, body });
64
+ }
65
+ function readBody(req) {
66
+ return new Promise((resolve, reject) => {
67
+ const chunks = [];
68
+ req.on("data", (c) => chunks.push(c));
69
+ req.on("end", () => resolve(Buffer.concat(chunks)));
70
+ req.on("error", reject);
71
+ });
72
+ }
73
+ async function writeFetchResponse(res, fetchRes) {
74
+ res.statusCode = fetchRes.status;
75
+ fetchRes.headers.forEach((value, key) => res.setHeader(key, value));
76
+ if (!fetchRes.body) {
77
+ res.end();
78
+ return;
79
+ }
80
+ const reader = fetchRes.body.getReader();
81
+ while (true) {
82
+ const { done, value } = await reader.read();
83
+ if (done)
84
+ break;
85
+ res.write(value);
86
+ }
87
+ res.end();
88
+ }
89
+ function wrapNodeServer(httpServer, wss, port, hostname) {
90
+ return {
91
+ port,
92
+ hostname: hostname ?? "localhost",
93
+ stop(closeActiveConnections) {
94
+ return new Promise((resolve, reject) => {
95
+ if (closeActiveConnections) {
96
+ for (const client of wss.clients)
97
+ client.terminate();
98
+ }
99
+ wss.close(() => {
100
+ httpServer.close((err) => err ? reject(err) : resolve());
101
+ });
102
+ });
103
+ }
104
+ };
105
+ }
106
+ export {
107
+ startNodeServer
108
+ };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Shared runtime-agnostic types for the Trellis HTTP server.
3
+ *
4
+ * @module trellis/server
5
+ */
6
+ /**
7
+ * Minimal interface satisfied by both Bun's server (returned from `Bun.serve`)
8
+ * and the Node http adapter. Use this when you only need the cross-runtime
9
+ * surface — most consumers only call `.stop()`.
10
+ */
11
+ export interface TrellisHttpServer {
12
+ port: number;
13
+ hostname?: string;
14
+ /**
15
+ * Shut down the server. On Bun the returned value may be void; on Node the
16
+ * returned promise resolves once both the HTTP server and the WebSocket
17
+ * server have closed.
18
+ */
19
+ stop(closeActiveConnections?: boolean): void | Promise<void>;
20
+ }
21
+ //# sourceMappingURL=server-shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server-shared.d.ts","sourceRoot":"","sources":["../../src/server/server-shared.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;GAIG;AACH,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,IAAI,CAAC,sBAAsB,CAAC,EAAE,OAAO,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9D"}