@preventive/triage 1.0.0-alpha.17 → 1.0.0-alpha.19

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 (44) hide show
  1. package/api/reap.ts +13 -75
  2. package/common/server-info.ts +6 -4
  3. package/out/client-managed-import.js +81 -0
  4. package/out/client-managed.js +68 -41
  5. package/out/client-sync.js +14 -14
  6. package/out/graph.js +10 -10
  7. package/out/index.html +1 -0
  8. package/out/view.css +1 -1
  9. package/out/view.js +179 -142
  10. package/package.json +16 -6
  11. package/report/index.js +2 -1
  12. package/report/src/finding.js +12 -0
  13. package/report/src/security.js +63 -0
  14. package/server-common/database-config.ts +16 -0
  15. package/server-common/initialize.ts +18 -0
  16. package/server-common/npm-advisories.ts +101 -0
  17. package/server-common/reap.ts +48 -0
  18. package/server-common/standalone.ts +29 -0
  19. package/server-common/storage-log.ts +34 -0
  20. package/server-common/vercel-blob.ts +110 -0
  21. package/server-e2e/app.ts +485 -0
  22. package/server-e2e/cli.js +7 -2
  23. package/server-e2e/config.ts +36 -40
  24. package/server-e2e/db-neon.ts +1 -1
  25. package/server-e2e/db.ts +1 -1
  26. package/server-e2e/http.ts +12 -2
  27. package/server-e2e/index.ts +12 -481
  28. package/server-e2e/lifecycle.ts +59 -172
  29. package/server-e2e/neon-driver.ts +2 -2
  30. package/server-e2e/npm-proxy.ts +10 -143
  31. package/server-e2e/objstore/blob-vercel.ts +4 -108
  32. package/server-e2e/objstore/init.ts +15 -23
  33. package/server-e2e/objstore/store-neon.ts +5 -9
  34. package/server-e2e/static.ts +11 -17
  35. package/out/provider-icons/claude.svg +0 -1
  36. package/out/provider-icons/deepseek.svg +0 -1
  37. package/out/provider-icons/google.svg +0 -1
  38. package/out/provider-icons/grok.svg +0 -1
  39. package/out/provider-icons/moonshot.svg +0 -1
  40. package/out/provider-icons/nvidia.svg +0 -1
  41. package/out/provider-icons/openai.svg +0 -1
  42. package/out/provider-icons/openrouter.svg +0 -1
  43. package/out/provider-icons/qwen.svg +0 -1
  44. package/out/provider-icons/zai.svg +0 -1
@@ -1,486 +1,17 @@
1
- #!/usr/bin/env node
2
- // DeepView triage-sync relay server. WebSocket front-end, SQLite
3
- // backing store. Implements the protocol described in
4
- // `client/triage-sync.js` (and `server-e2e/sign.ts` for the canonical
5
- // signature payloads):
6
- //
7
- // server → client challenge { nonce } — emitted on every
8
- // accept, BEFORE any client
9
- // frame; per-socket random
10
- // 128-bit value the client
11
- // must bind into every
12
- // `workspace-subscribe`
13
- // signature (round-9 H2)
14
- // client → server workspace-save { workspaceTag, base,
15
- // keyframe, nonce, ciphertext,
16
- // signature } — `keyframe` is
17
- // a boolean (`true` exactly,
18
- // else falsy), bound into the
19
- // signed canonical
20
- // client → server workspace-subscribe { workspaceTag, from,
21
- // signature } — `from` is the
22
- // last revision id the client
23
- // claims to have applied (or
24
- // null for fresh)
25
- // client → server ping — heartbeat
26
- // server → client pong — heartbeat reply
27
- // server → client workspace-save-ack { workspaceTag, base, id }
28
- // server → client workspace-save-error { workspaceTag, base, reason }
29
- // — explicit failure surface for
30
- // the legit-signer case where
31
- // the server rejects a signed
32
- // save (e.g. `too-large` past
33
- // MAX_CIPHERTEXT_LEN). Sent
34
- // AFTER sig verify so the
35
- // response only reaches a
36
- // legitimate seed holder; shape
37
- // attacks still drop silently.
38
- // server → client workspace-subscribed { workspaceTag } — explicit
39
- // handshake-complete ack so
40
- // the client can flip its
41
- // status from `connecting` to
42
- // `online` only after the
43
- // server registered it as a
44
- // peer (not just on socket
45
- // open)
46
- // server → client workspace-state { workspaceTag, revisions:
47
- // [{ base, id, keyframe,
48
- // nonce, ciphertext,
49
- // signature }, ...] }
50
- //
51
- // Authentication: every signed message is checked against the
52
- // `workspaceTag` (= base64url Ed25519 public key) before any
53
- // state mutation. Unsigned / bad-sig messages are dropped silently
54
- // — the legitimate signer will retry, and an attacker who learns
55
- // the tag without holding the seed can't get past the verify.
56
- //
57
- // Content opacity: `nonce` and `ciphertext` are opaque to the
58
- // server. We store and forward them; we never inspect.
59
- //
60
- // Subscriber tracking: `subscribers: Map<workspaceTag, Set<socket>>`.
61
- // A socket joins the set ONLY via an explicit, signature-verified
62
- // `workspace-subscribe` (sole call site of `subscribe()` is in
63
- // `handleSubscribe`, gated by the per-connection challenge nonce
64
- // bound into the signature and a post-await `readyState` check).
65
- // It leaves on disconnect. Broadcasts go to every subscriber for
66
- // the workspaceTag except the originator.
67
- //
68
- // `workspace-save` deliberately does NOT auto-attach the sender,
69
- // even on a valid signature — see the audit note in `handleSave`
70
- // (round-9 H1): auto-subscribe-on-save let a passive observer
71
- // replay any captured save frame from any TCP connection to attach
72
- // as a silent mirror, since the duplicate-id path returns ack-only
73
- // and would not reject the attaching socket.
1
+ // Compatibility entry point for the published e2e server. Importing exposes
2
+ // one unbound instance; the combined launcher uses the factory directly.
3
+ import { startServer } from '../server-common/standalone.ts'
4
+ import { createE2eApp } from './app.ts'
5
+ import { HELP, loadConfig } from './config.ts'
74
6
 
75
- import { type WebSocket, WebSocketServer } from 'ws'
76
- import { errMsg, errStack } from './util.ts'
77
- import type { PeerRegistry } from './peer.ts'
78
- import { LOOPBACK_HOSTS, createOriginGate } from '../server-common/origin.ts'
79
- import { createHub } from './hub.ts'
80
- import { createAuth } from './auth.ts'
81
- import { createSyncHandlers } from './sync-handlers.ts'
82
- import { WS_UPGRADE_PATH, createHttpServer } from './http.ts'
83
- import { installWsServer } from './ws-server.ts'
84
- import { SSE_OPEN_PATH, installSseServer } from './sse-server.ts'
85
- import type { ServerInfo } from '../common/server-info.ts'
86
- import { createLifecycle } from './lifecycle.ts'
87
- import { loadConfig } from './config.ts'
88
- import { type Handle, openDb } from './db.ts'
89
- import { openNeonDb } from './db-neon.ts'
90
- import { initObjstore } from './objstore/init.ts'
91
- import { type Handle as ObjstoreHandle, listLive, objectMetaWire, openObjstore } from './objstore/store.ts'
92
- import { openNeonObjstore } from './objstore/store-neon.ts'
93
- import { openVercelBlobBackend } from './objstore/blob-vercel.ts'
94
- import {
95
- type NeonClientCtor, type PubSub,
96
- createNeonPubSub, createNoopPubSub,
97
- } from './pubsub.ts'
98
- import { createBusReceiver } from './bus-receiver.ts'
99
-
100
- // All external inputs (env vars + optional config.json) are parsed
101
- // and validated in ./config.ts; destructure into the uppercase names
102
- // the rest of this module uses.
103
- const config = loadConfig()
104
- const {
105
- port: PORT, host: HOST, dbPath: DB_PATH, objstoreDir: OBJSTORE_DIR,
106
- reapIntervalMs: OBJSTORE_REAP_INTERVAL_MS, reapDisabled: OBJSTORE_REAP_DISABLED,
107
- maxInflightPerSocket: MAX_INFLIGHT_PER_SOCKET, debug: DEBUG,
108
- neonUrl: NEON_URL, blobToken: BLOB_TOKEN, tokenSecret: TOKEN_SECRET,
109
- password: CONFIG_PASSWORD, trustProxyEnv: TRUST_PROXY_ENV,
110
- } = config
111
-
112
- // Same-origin gate for the WS upgrade and REST data plane (see
113
- // ./origin.ts). `TRUST_PROXY_ENV` also feeds the boot-time
114
- // misconfiguration fail-fast below.
115
- const { trustProxy: TRUST_PROXY, isOriginAllowed } = createOriginGate(HOST, TRUST_PROXY_ENV)
116
-
117
- // Per-socket buffered-bytes cap. `socket.send` returns synchronously
118
- // even when the kernel/ws library can't drain to the wire fast
119
- // enough; the unsent payload accumulates in `bufferedAmount`. A
120
- // slow / blackholed peer on a high-volume workspace can hold many
121
- // MB of fan-out broadcasts in this buffer with no backpressure on
122
- // the broadcast loop. Drop the message when the buffer crosses the
123
- // cap; the heartbeat will eventually close a peer that never
124
- // drains. Transport audit `server-e2e/index.ts:225`.
125
- const MAX_BUFFERED_BYTES = 16 * 1024 * 1024
126
- // Per-socket in-flight async-handler cap (MAX_INFLIGHT_PER_SOCKET,
127
- // env-validated in config). Each inbound text frame spawns a
128
- // `track(handler)` IIFE; an authorised peer firing valid frames could
129
- // otherwise grow the set without bound, stretching SIGTERM drain time.
130
- // Saves dropped at the cap surface as a typed `busy` NACK. Transport
131
- // audit `server-e2e/index.ts:590`.
132
-
133
- // Per-connection state registry. One `Peer` per accepted socket holds
134
- // the challenge nonce, auth flag, heartbeat liveness, in-flight count,
135
- // and subscribed tags (see ./peer.ts). The connection handler holds
136
- // the Peer in a closure for the hot paths; cross-function call sites
137
- // resolve it via `peers.get(socket)`.
138
- const peers: PeerRegistry = new WeakMap()
139
-
140
- // REST PUT idle-body timeout. A slow-loris client trickling bytes
141
- // within the declared Content-Length holds the staging fd and an
142
- // inFlightSids slot until the global staging TTL reaps it. Aborting
143
- // the per-chunk-idle period closes that window. Transport audit
144
- // `server-e2e/objstore/rest.ts:218`.
145
- const REST_PUT_IDLE_TIMEOUT_MS = 30_000
146
-
147
- // Server-driven WS heartbeat. Every `HEARTBEAT_INTERVAL_MS` we walk
148
- // `wss.clients`, terminate anyone who didn't pong since the last
149
- // tick, and ping the rest. The client-initiated `{type:'ping'}` /
150
- // `{type:'pong'}` JSON heartbeat the protocol already had only
151
- // catches the case where the CLIENT notices the socket's gone — it
152
- // can't recover an FD when the client itself has wandered off
153
- // (battery-killed background tab, mid-transfer NAT timeout, a
154
- // hostile non-browser client that opens the socket and never
155
- // speaks again). The same-origin upgrade gate allows missing
156
- // Origin headers through (legitimate non-browser callers), so a
157
- // hostile CLI can stack arbitrarily many idle sockets without it.
158
- // Kernel TCP keepalive is hours by default; without this interval
159
- // each abandoned socket pins its `wss.clients` Set entry, its `Peer`
160
- // state, and an FD until the kernel reclaims it. Two ticks max
161
- // from silence to termination, so the longest a dead socket
162
- // survives is ~2 × HEARTBEAT_INTERVAL_MS.
163
- const HEARTBEAT_INTERVAL_MS = 30_000
164
-
165
- // Backend selection. Both planes (workspace_revision DB + the
166
- // v1.objstore byte store) are picked from config at boot. Two
167
- // supported pairings:
168
- // 1. DATABASE_URL set → Neon (workspace_revision + objstore
169
- // tables) + Vercel Blob Private Storage (bytes). Requires
170
- // BLOB_READ_WRITE_TOKEN — fail fast at boot if missing, since
171
- // a local-FS byte plane can't back a multi-replica deployment
172
- // (one replica's writes wouldn't be visible to another).
173
- // 2. DATABASE_URL absent → SQLite + local FS bytes. Single-
174
- // process; the only pairing the SQLite plane supports.
175
- // The Neon / Vercel files import their peer deps lazily inside the
176
- // open functions, so static imports here are safe even on a SQLite-
177
- // only install where the optional peer deps aren't present. Explicit
178
- // branch (not a ternary) so the SQLite path keeps its `SqliteHandle`
179
- // narrowing — `sqliteHandle.db` is a non-optional `DatabaseSync` that
180
- // `openObjstore` accepts without a non-null assertion.
181
- let handle: Handle
182
- let objstoreHandle: ObjstoreHandle
183
- let objstoreBanner: string
184
- if (NEON_URL) {
185
- if (!BLOB_TOKEN) {
186
- console.error('DATABASE_URL is set but BLOB_READ_WRITE_TOKEN is not.')
187
- console.error('The Neon DB plane requires the Vercel Blob byte plane (local-FS bytes cannot back a multi-replica deployment).')
188
- console.error('Set BLOB_READ_WRITE_TOKEN to your Vercel Blob R/W token, or unset DATABASE_URL to fall back to SQLite + local FS.')
189
- process.exit(1)
190
- }
191
- if (!TOKEN_SECRET) {
192
- console.error('DATABASE_URL is set but OBJSTORE_TOKEN_SECRET is not.')
193
- console.error('Multi-replica deployments need a shared HMAC secret so REST bearer tokens minted on one replica validate on any other.')
194
- console.error('Generate one with: node -e \'console.log(require("crypto").randomBytes(32).toString("base64"))\'')
195
- process.exit(1)
196
- }
197
- handle = await openNeonDb(NEON_URL)
198
- const blob = await openVercelBlobBackend({ token: BLOB_TOKEN })
199
- objstoreHandle = await openNeonObjstore(NEON_URL, blob)
200
- objstoreBanner = 'objstore: vercel-blob (private)'
201
- } else {
202
- const sqliteHandle = openDb(DB_PATH)
203
- handle = sqliteHandle
204
- objstoreHandle = openObjstore(sqliteHandle.db, OBJSTORE_DIR)
205
- objstoreBanner = `objstore: ${OBJSTORE_DIR}`
206
- }
207
- // Multi-replica deployments behind a load balancer / TLS terminator
208
- // (the typical Vercel + Neon shape) need TRUST_PROXY=1 to honour
209
- // X-Forwarded-Host when computing the same-origin gate's expected
210
- // origin. Otherwise the gate derives the origin from the internal
211
- // container hostname and rejects every browser request as a
212
- // cross-origin attempt — silently from the operator's perspective
213
- // until users report 403s. Fail fast (parallels the
214
- // BLOB_READ_WRITE_TOKEN / OBJSTORE_TOKEN_SECRET checks above) so
215
- // a misconfigured deploy doesn't ship a 100%-403 fleet. An
216
- // operator who genuinely terminates TLS in the container without
217
- // X-Forwarded-* (rare) can set `TRUST_PROXY=0` to acknowledge.
218
- if (NEON_URL && !TRUST_PROXY && !LOOPBACK_HOSTS.has(HOST) && TRUST_PROXY_ENV !== '0' && TRUST_PROXY_ENV !== 'false') {
219
- console.error(`DATABASE_URL is set and HOST=${HOST} is not loopback, but TRUST_PROXY is not enabled.`)
220
- console.error('Browser requests through a load balancer / TLS terminator will be rejected by the same-origin gate (all 403).')
221
- console.error('Set TRUST_PROXY=1 to honour X-Forwarded-Host / X-Forwarded-Proto from the upstream proxy.')
222
- console.error('Set TRUST_PROXY=0 if you really terminate TLS in the container without X-Forwarded-* headers (no proxy).')
223
- process.exit(1)
224
- }
225
-
226
- // "Workspace exists on the server" gate. The auth requirement only
227
- // kicks in for the FIRST action against a never-before-seen tag —
228
- // once any row lands (triage revision or objstore object), the
229
- // workspace is considered established and signature-gated. Checks
230
- // both planes so the gate stays consistent regardless of which
231
- // action the user picks first (workspace-save in the common case;
232
- // objstore-put-begin for a bundle-first flow).
233
- async function workspaceExists(tag: string): Promise<boolean> {
234
- if (await handle.headFor.get(tag)) return true
235
- const c = await objstoreHandle.countLive.get(tag)
236
- return (c?.c ?? 0) > 0
237
- }
238
-
239
- // WS fan-out hub: subscriber registry + backpressure-aware send /
240
- // broadcast (see ./hub.ts).
241
- const hub = createHub({ peers, maxBufferedBytes: MAX_BUFFERED_BYTES, debug: DEBUG })
242
- const { send, broadcast, subscribe, unsubscribeAll, broadcastLocalRaw } = hub
243
-
244
- // Cross-instance pub/sub for live broadcasts (see ./pubsub.ts). SQLite
245
- // mode is single-process by construction so it gets a no-op; Neon mode
246
- // uses Postgres LISTEN/NOTIFY on a dedicated long-lived Client
247
- // connection (separate from the HTTP `neon()` callable the queries flow
248
- // over — LISTEN needs a session-bound socket, the HTTP path is
249
- // stateless). The bus carries lookup hints, not the full wire payload:
250
- // the `workspace-state` broadcast's ciphertext alone can exceed the
251
- // ~8 KB NOTIFY payload cap, so the receiver re-fetches from the shared
252
- // DB to construct the wire frame. The bus is best-effort fan-out, not a
253
- // durability layer — the DB itself is the source of truth, and a
254
- // dropped publish only means peers on other instances miss the live
255
- // push (they still catch up via the chain on their next subscribe).
256
- let pubsub: PubSub = createNoopPubSub()
257
- if (NEON_URL) {
258
- // Dynamic import: the Client export lives in the same optional peer
259
- // dep as the HTTP `neon()` callable (see ./neon-driver.ts), but the
260
- // dep itself is only present on a Neon-mode deploy. The wrapper path
261
- // also lets tests swap in a PGlite-backed shim (`tests/pubsub.test.js`).
262
- const mod = (await import('./neon-driver.ts')) as unknown as { Client?: NeonClientCtor }
263
- if (!mod.Client) {
264
- console.error('DATABASE_URL is set but the @neondatabase/serverless Client export is not available.')
265
- console.error('Cross-instance broadcasts require the WebSocket-based Client (the HTTP `neon()` callable cannot LISTEN).')
266
- console.error('Reinstall the peer dep: pnpm add @neondatabase/serverless')
267
- process.exit(1)
268
- }
269
- const NeonClientImpl = mod.Client
270
- pubsub = createNeonPubSub({
271
- newClient: () => new NeonClientImpl(NEON_URL),
272
- debug: DEBUG,
273
- })
274
- }
275
-
276
- // Password gate (see ./auth.ts) — HMAC derivation + the `authenticate`
277
- // handshake.
278
- const auth = createAuth({ peers, password: CONFIG_PASSWORD, send, debug: DEBUG })
279
- const { requiresAuth, passwordConfigured, handleAuthenticate, sendUnauthorized } = auth
280
-
281
- // Triage-sync protocol handlers (see ./sync-handlers.ts). `getNonce`
282
- // resolves a socket's challenge nonce and is shared with the objstore
283
- // wiring below; `sendSaveError` is reused by the dispatcher's `busy`
284
- // inflight-cap NACK path.
285
- const getNonce = (socket: WebSocket): string | undefined => peers.get(socket)?.challenge
286
- const publishRevision = (tag: string, revisionId: string): void => {
287
- pubsub.publish({ kind: 'rev', tag, id: revisionId })
288
- }
289
- const publishObjPut = (tag: string, resourceTag: string): void => {
290
- pubsub.publish({ kind: 'objput', tag, res: resourceTag })
291
- }
292
- const publishObjDeleted = (tag: string, resourceTag: string, version: number, incarnation: string): void => {
293
- pubsub.publish({ kind: 'objdel', tag, res: resourceTag, ver: version, incarnation })
7
+ if (import.meta.main && (process.argv.includes('--help') || process.argv.includes('-h'))) {
8
+ console.log(HELP)
9
+ process.exit(0)
294
10
  }
295
11
 
296
- const { handleSave, handleSaveRest, handleSubscribe, sendSaveError } = createSyncHandlers({
297
- handle, send, broadcast, publishRevision, subscribe, getNonce,
298
- pauseBroadcasts: hub.pauseBroadcasts,
299
- requiresAuth, passwordConfigured, sendUnauthorized, workspaceExists,
300
- // Folds the objstore inventory into the `workspace-subscribed` ack.
301
- // The objstore store keeps its own richer `Handle`, so we wire the
302
- // query here where both handles exist rather than coupling
303
- // sync-handlers to the store type.
304
- objstoreResources: async (tag) => (await listLive(objstoreHandle, tag)).map(objectMetaWire),
305
- debug: DEBUG,
306
- })
307
-
308
- const { handlers: objstore, restDeps: objstoreRestDeps, startupReap, stopReaper } = initObjstore({
309
- handle: objstoreHandle, reapIntervalMs: OBJSTORE_REAP_INTERVAL_MS,
310
- reapDisabled: OBJSTORE_REAP_DISABLED,
311
- send, broadcast, publishObjPut, publishObjDeleted,
312
- getNonce, debug: DEBUG,
313
- // Auth gate for the FIRST objstore-put-begin against a workspace
314
- // that doesn't yet exist on the server. Mirrors handleSave's gate
315
- // below; handlers.ts calls this AFTER sig verify so the
316
- // `unauthorized` frame only reaches a legitimate signer. Returns
317
- // `false` to allow, `true` to deny — handlers.ts emits the
318
- // `unauthorized` frame and bails on `true`.
319
- authGate: async (socket, tag) => requiresAuth(socket) && !await workspaceExists(tag),
320
- sendUnauthorized,
321
- // Socket-less analog of `authGate` for the REST put-begin mint: a REST
322
- // request can never be operator-authorized, so the gate collapses to
323
- // "password configured AND workspace new". A deny routes the client to
324
- // its in-band WS put-begin fallback.
325
- restPutGate: async (tag) => passwordConfigured && !await workspaceExists(tag),
326
- // `tokenSecret` is set only when OBJSTORE_TOKEN_SECRET was
327
- // provided in env (see TOKEN_SECRET resolution above). Omitted
328
- // → initObjstore mints a fresh per-process secret (fine for
329
- // single-replica).
330
- ...(TOKEN_SECRET ? { tokenSecret: TOKEN_SECRET } : {}),
331
- })
332
-
333
- // 4 MiB cap leaves headroom above MAX_CIPHERTEXT_LEN (2 MiB) for
334
- // the JSON envelope + base64 overhead. `ws` defaults to 100 MiB
335
- // which any unauthenticated peer could spam — every connection
336
- // accepts and JSON.parses up to that before the signature-fail drops
337
- // the frame.
338
- const wss = new WebSocketServer({ noServer: true, maxPayload: 4 * 1024 * 1024 })
339
-
340
- // Process lifecycle (see ./lifecycle.ts): `track` (in-flight request
341
- // drain) and `isShuttingDown` (the new-work gate) are consumed by the
342
- // HTTP + WS planes below; the teardown is `installLifecycle`d once
343
- // every server object exists.
344
- const { track, isShuttingDown, install: installLifecycle } = createLifecycle()
345
-
346
- // The sync protocol this build advertises — emitted as a `server-info` frame
347
- // right after the challenge on every connection. This is the e2e boot, so it
348
- // always advertises e2e.
349
- const SERVER_INFO: ServerInfo = { mode: 'e2e', managed: null, ...(config.deepviewScanServer ? { deepviewScanServer: config.deepviewScanServer } : {}) }
350
-
351
- // Shared per-connection dispatch surface. Both the WS plane
352
- // (installWsServer below) and the SSE+POST fallback (installSseServer
353
- // below) drive `setupPeerConnection` with this; one Peer per accepted
354
- // connection, one message-handler tree.
355
- const peerConnectionDeps = {
356
- peers, serverInfo: SERVER_INFO, send, unsubscribeAll,
357
- handleSave, handleSubscribe, handleAuthenticate, sendSaveError, objstore,
358
- track, isShuttingDown,
359
- maxInflightPerSocket: MAX_INFLIGHT_PER_SOCKET,
360
- debug: DEBUG,
361
- }
362
-
363
- // SSE+POST fallback transport (see ./sse-server.ts). The WS upgrade is
364
- // the preferred path; clients fall back to SSE when `new WebSocket(…)`
365
- // fails to open (corporate proxies that strip the Upgrade header,
366
- // hostile network middleboxes, etc.). Same protocol on the wire — only
367
- // the byte transport differs.
368
- const sseServer = installSseServer({
369
- peerDeps: peerConnectionDeps, isShuttingDown,
370
- // Same cap shape as `wss` (no explicit per-process cap; OS FD limits
371
- // are the upstream bound). 1024 leaves room above the typical web
372
- // session count without being a soft DoS hammer.
373
- maxSessions: 1024,
374
- // Mirror the WS `maxPayload` so the SSE plane can't accept frames
375
- // the WS plane would reject.
376
- maxBodyBytes: 4 * 1024 * 1024,
377
- debug: DEBUG,
378
- })
379
-
380
- // HTTP plane: REST byte-transfer routing + SSE fallback + the WS
381
- // upgrade gate (see ./http.ts). Built after the lifecycle state above
382
- // because the REST shutdown gate reads `shuttingDown` and the request
383
- // drain uses `track`. The WS connection handler is wired on `wss`
384
- // below.
385
- const httpServer = createHttpServer({
386
- wss, restDeps: objstoreRestDeps, sseServer, serverInfo: SERVER_INFO, isOriginAllowed,
387
- isShuttingDown, track, handleSaveRest,
388
- restPutIdleTimeoutMs: REST_PUT_IDLE_TIMEOUT_MS, debug: DEBUG,
389
- })
390
-
391
- // WS runtime: per-connection handler + message dispatch + heartbeat
392
- // sweep (see ./ws-server.ts). Returns the heartbeat timer so shutdown
393
- // can clear it.
394
- const { heartbeatTimer } = installWsServer({
395
- wss, heartbeatIntervalMs: HEARTBEAT_INTERVAL_MS,
396
- ...peerConnectionDeps,
397
- })
398
-
399
- httpServer.on('listening', () => {
400
- // Read the actual bound port from `httpServer.address()` rather
401
- // than the `PORT` env constant. Operators (and the test harness)
402
- // can boot with `PORT=0` to get an OS-assigned ephemeral port;
403
- // the log line then carries the real bound number, not `0`.
404
- // Server-side bind failure took the error path above, so
405
- // `address()` is always a populated AddressInfo here.
406
- const addr = httpServer.address()
407
- const boundPort = typeof addr === 'object' && addr ? addr.port : PORT
408
- // Differentiate the storage banner by backend so the log line
409
- // doesn't claim a misleading DB_PATH under Neon, or a misleading
410
- // OBJSTORE_DIR under Vercel Blob.
411
- const dbBanner = NEON_URL ? 'db: neon-postgres' : `db: ${DB_PATH}`
412
- console.log(`DeepView triage-sync server: ws://${HOST}:${boundPort}${WS_UPGRADE_PATH} (sse fallback http://${HOST}:${boundPort}${SSE_OPEN_PATH}) http://${HOST}:${boundPort}/api/objstore/{workspaceTag}/{resourceTag} (${dbBanner}, ${objstoreBanner})`)
413
- })
414
-
415
- // Cross-instance bus receiver (see ./bus-receiver.ts): a remote NOTIFY
416
- // lands here, we re-fetch any data the bus payload only hinted at,
417
- // then broadcast to local peers via `broadcastLocalRaw`. Extracted so
418
- // the rev / objput / objdel mapping (including the keyframe boolean
419
- // coercion and the `objectMetaWire` shape) is testable without the
420
- // full server bring-up.
421
- const onBusMessage = createBusReceiver({
422
- handle, objstoreHandle, broadcastLocalRaw, debug: DEBUG,
423
- })
424
-
425
- // Kick off the LISTEN loop in the background. `pubsub.start` resolves
426
- // only after the FIRST successful connect, and the internal reconnect
427
- // loop retries indefinitely on transport / LISTEN failure — awaiting
428
- // it here would convert a transient Neon WS hiccup at boot into a
429
- // server that never binds (and a `.catch` that never runs). Instead
430
- // the server binds immediately and the bus comes up async; publishes
431
- // during the down window drop silently (best-effort semantics
432
- // documented in pubsub.ts). The SQLite no-op resolves immediately,
433
- // so the behaviour is identical in that mode.
434
- void pubsub.start(onBusMessage).catch((err) => {
435
- console.warn('pubsub: startup error:', errStack(err))
436
- })
437
-
438
- // App-specific shutdown step (run after the in-flight drain), wired
439
- // into the lifecycle teardown below.
440
- const closeDb = async (): Promise<void> => {
441
- // Stop the bus first so a publish from a still-draining handler
442
- // can't fire into a half-closed Client. The in-flight drain runs
443
- // before this (see lifecycle.ts), so by here all `broadcast` →
444
- // `publish*` calls are settled.
445
- try { await pubsub.stop() } catch (err) { console.warn('pubsub close error:', errMsg(err)) }
446
- // objstoreHandle has no close(): SQLite shares this DatabaseSync and
447
- // Neon has no persistent connection; `handle.close()` covers both.
448
- try { await handle.close() } catch (err) { console.warn('DB close error:', errMsg(err)) }
449
- }
450
- // Wire graceful shutdown + the signal / error / process-catchall
451
- // handlers (see ./lifecycle.ts). Installed last, once httpServer, wss,
452
- // and the heartbeat timer all exist.
453
- installLifecycle({
454
- httpServer, wss, heartbeatTimer, stopReaper,
455
- sseSessions: sseServer.sessions,
456
- sseKeepaliveTimer: sseServer.keepaliveTimer,
457
- closeDb,
458
- })
459
-
460
- // Bind only after the startup orphan sweep finishes — otherwise a
461
- // fresh boot could serve traffic against tags whose on-disk state
462
- // still has residue from a prior crash. `startupReap` already
463
- // resolves on any error (the reaper's own catch logs the failure
464
- // unconditionally and returns void), so no outer `.catch` is
465
- // needed here.
466
- await startupReap
467
-
468
- // Bind the HTTP/WS plane on the configured PORT/HOST. Exported so a
469
- // launcher (server-e2e/cli.js — the triage-server bin) or any `import`er can
470
- // start serving. The top-level `await startupReap` above means the server
471
- // is fully ready — DB open, DDL bootstrapped, objstore reaper swept — by
472
- // the time the import resolves.
473
- export function start(): void {
474
- httpServer.listen(PORT, HOST)
475
- }
12
+ const config = loadConfig()
13
+ export const { httpServer, wss, isShuttingDown, reap, handleRequest, onShutdown } = await createE2eApp(config)
476
14
 
477
- export { httpServer, wss }
15
+ export function start(): void { startServer(httpServer, config) }
478
16
 
479
- // Library mode: when this module is `import`ed (rather than run as the
480
- // entry script) skip the auto-start so consumers can own the bind — e.g.
481
- // wrap CF Access / framework-preset shims around the assembled
482
- // `httpServer`, or just call `start()` when ready. Direct invocation via
483
- // `node server-e2e/index.ts` (or the `triage-server` bin) still listens.
484
- if (import.meta.main) {
485
- start()
486
- }
17
+ if (import.meta.main) start()