toiljs 0.0.115 → 0.0.117
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 +10 -0
- package/README.md +16 -7
- package/TYPESCRIPT_LAW.md +1946 -2073
- package/build/backend/.tsbuildinfo +1 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2375 -258
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/dev/devtools.js +1 -1
- package/build/client/dev/error-overlay.js +1 -1
- package/build/client/routing/mount.js +1 -1
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/auth-emails.js +7 -3
- package/build/compiler/emails.js +1 -1
- package/build/compiler/index.d.ts +1 -0
- package/build/compiler/index.js +25 -4
- package/build/compiler/pages.js +2 -3
- package/build/compiler/prerender.d.ts +3 -8
- package/build/compiler/prerender.js +88 -127
- package/build/compiler/ssg.js +3 -4
- package/build/compiler/template-build.js +3 -4
- package/build/compiler/toil-docs.generated.js +6 -5
- package/build/compiler/vite.js +3 -6
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/db/database.js +7 -3
- package/build/devserver/db/types.js +1 -1
- package/build/devserver/production.js +4 -3
- package/build/devserver/runtime/crypto.d.ts +1 -1
- package/build/io/.tsbuildinfo +1 -1
- package/build/logger/.tsbuildinfo +1 -1
- package/build/shared/.tsbuildinfo +1 -1
- package/examples/basic/client/routes/analytics.tsx +7 -5
- package/examples/basic/server/tsconfig.json +1 -3
- package/package.json +45 -44
- package/presets/no-uint8array-tostring.js +120 -153
- package/presets/oxlint.d.ts +5 -0
- package/presets/oxlint.js +224 -0
- package/server/runtime/tsconfig.json +1 -3
- package/tsconfig.json +2 -11
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
- package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -90
- package/.github/ISSUE_TEMPLATE/config.yml +0 -8
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/PULL_REQUEST_TEMPLATE.md +0 -43
- package/.github/changelog-config.json +0 -45
- package/.github/dependabot.yml +0 -27
- package/.github/workflows/ci.yml +0 -222
- package/.vscode/settings.json +0 -10
- package/docs/README.md +0 -107
- package/docs/auth/README.md +0 -104
- package/docs/auth/configuration.md +0 -105
- package/docs/auth/emails.md +0 -92
- package/docs/auth/extending.md +0 -202
- package/docs/auth/how-it-works.md +0 -138
- package/docs/auth/usage.md +0 -188
- package/docs/backend/README.md +0 -143
- package/docs/backend/data.md +0 -351
- package/docs/backend/rest.md +0 -402
- package/docs/backend/rpc.md +0 -226
- package/docs/background/README.md +0 -114
- package/docs/background/daemons.md +0 -276
- package/docs/background/derive.md +0 -179
- package/docs/cli/README.md +0 -379
- package/docs/concepts/ai-guide.md +0 -132
- package/docs/concepts/config.md +0 -416
- package/docs/concepts/decorators.md +0 -127
- package/docs/concepts/security.md +0 -108
- package/docs/concepts/tiers.md +0 -166
- package/docs/concepts/types.md +0 -216
- package/docs/database/README.md +0 -143
- package/docs/database/capacity.md +0 -350
- package/docs/database/counters.md +0 -174
- package/docs/database/documents.md +0 -277
- package/docs/database/events.md +0 -307
- package/docs/database/membership.md +0 -246
- package/docs/database/setup.md +0 -155
- package/docs/database/unique.md +0 -216
- package/docs/database/views.md +0 -246
- package/docs/frontend/README.md +0 -104
- package/docs/frontend/components.md +0 -286
- package/docs/frontend/data-fetching.md +0 -260
- package/docs/frontend/images.md +0 -148
- package/docs/frontend/metadata.md +0 -344
- package/docs/frontend/navigation.md +0 -296
- package/docs/frontend/rendering.md +0 -236
- package/docs/frontend/routing.md +0 -346
- package/docs/frontend/scripts.md +0 -118
- package/docs/frontend/search.md +0 -191
- package/docs/frontend/styling.md +0 -147
- package/docs/frontend/toil-global.md +0 -216
- package/docs/getting-started/README.md +0 -81
- package/docs/getting-started/create-project.md +0 -131
- package/docs/getting-started/deploy.md +0 -101
- package/docs/getting-started/first-app.md +0 -215
- package/docs/getting-started/installation.md +0 -124
- package/docs/getting-started/migrating.md +0 -125
- package/docs/getting-started/project-structure.md +0 -163
- package/docs/introduction/README.md +0 -66
- package/docs/introduction/design-principles.md +0 -81
- package/docs/introduction/distributed.md +0 -91
- package/docs/introduction/how-it-works.md +0 -121
- package/docs/introduction/hyperscale.md +0 -70
- package/docs/introduction/modern-stack.md +0 -85
- package/docs/introduction/vs-other-frameworks.md +0 -64
- package/docs/introduction/why-toil.md +0 -99
- package/docs/llms.txt +0 -95
- package/docs/realtime/README.md +0 -102
- package/docs/realtime/channels.md +0 -211
- package/docs/realtime/streams.md +0 -369
- package/docs/services/README.md +0 -60
- package/docs/services/analytics.md +0 -279
- package/docs/services/caching.md +0 -175
- package/docs/services/cookies.md +0 -235
- package/docs/services/crypto.md +0 -209
- package/docs/services/email.md +0 -289
- package/docs/services/environment.md +0 -141
- package/docs/services/ratelimit.md +0 -174
- package/docs/services/time.md +0 -85
- package/eslint.config.js +0 -57
- package/examples/basic/client/routes/docs/[...slug].tsx +0 -12
- package/presets/eslint.js +0 -88
- package/src/backend/index.ts +0 -204
- package/src/cli/configure.ts +0 -363
- package/src/cli/create.ts +0 -924
- package/src/cli/db.ts +0 -158
- package/src/cli/diagnostics.ts +0 -665
- package/src/cli/doctor.ts +0 -982
- package/src/cli/features.ts +0 -170
- package/src/cli/index.ts +0 -326
- package/src/cli/notify.ts +0 -120
- package/src/cli/proc.ts +0 -56
- package/src/cli/ui.ts +0 -169
- package/src/cli/update.ts +0 -248
- package/src/cli/updates.ts +0 -92
- package/src/cli/validate.ts +0 -31
- package/src/cli/version-check.ts +0 -125
- package/src/client/auth.ts +0 -869
- package/src/client/channel/channel.ts +0 -146
- package/src/client/components/Form.tsx +0 -65
- package/src/client/components/Image.tsx +0 -160
- package/src/client/components/Script.tsx +0 -113
- package/src/client/components/Slot.tsx +0 -21
- package/src/client/dev/devtools.tsx +0 -1089
- package/src/client/dev/error-overlay.tsx +0 -225
- package/src/client/errors.ts +0 -11
- package/src/client/head/head.ts +0 -182
- package/src/client/head/metadata.ts +0 -110
- package/src/client/index.ts +0 -130
- package/src/client/navigation/Link.tsx +0 -108
- package/src/client/navigation/NavLink.tsx +0 -86
- package/src/client/navigation/navigation.ts +0 -235
- package/src/client/navigation/prefetch.ts +0 -169
- package/src/client/navigation/scroll.ts +0 -53
- package/src/client/routing/Router.tsx +0 -216
- package/src/client/routing/action.ts +0 -122
- package/src/client/routing/error-boundary.tsx +0 -43
- package/src/client/routing/hooks.ts +0 -140
- package/src/client/routing/lazy.ts +0 -77
- package/src/client/routing/loader.ts +0 -381
- package/src/client/routing/match.ts +0 -47
- package/src/client/routing/mount.tsx +0 -90
- package/src/client/routing/params-context.ts +0 -10
- package/src/client/routing/slot-context.ts +0 -7
- package/src/client/rpc.ts +0 -97
- package/src/client/search/search.ts +0 -189
- package/src/client/search/use-page-search.ts +0 -73
- package/src/client/ssr/markers.tsx +0 -172
- package/src/client/stream/client.ts +0 -237
- package/src/client/types.ts +0 -73
- package/src/compiler/auth-emails.ts +0 -312
- package/src/compiler/config.ts +0 -369
- package/src/compiler/docs.ts +0 -127
- package/src/compiler/email-preview.ts +0 -314
- package/src/compiler/emails.ts +0 -404
- package/src/compiler/fonts.ts +0 -89
- package/src/compiler/generate.ts +0 -471
- package/src/compiler/image-blur.ts +0 -42
- package/src/compiler/image-report.ts +0 -88
- package/src/compiler/index.ts +0 -1362
- package/src/compiler/pages.ts +0 -49
- package/src/compiler/plugin.ts +0 -391
- package/src/compiler/prerender.ts +0 -263
- package/src/compiler/routes.ts +0 -170
- package/src/compiler/seo.ts +0 -415
- package/src/compiler/sri.ts +0 -144
- package/src/compiler/ssg.ts +0 -177
- package/src/compiler/ssr-codegen.ts +0 -85
- package/src/compiler/template-build.ts +0 -676
- package/src/compiler/template.ts +0 -262
- package/src/compiler/toil-docs.generated.ts +0 -77
- package/src/compiler/vite.ts +0 -245
- package/src/devserver/analytics/index.ts +0 -317
- package/src/devserver/config/dotenv.ts +0 -102
- package/src/devserver/config/env.ts +0 -30
- package/src/devserver/config/ratelimit.ts +0 -123
- package/src/devserver/daemon/catalog.ts +0 -120
- package/src/devserver/daemon/cron.ts +0 -87
- package/src/devserver/daemon/host.ts +0 -134
- package/src/devserver/daemon/index.ts +0 -369
- package/src/devserver/daemon/runtime.ts +0 -48
- package/src/devserver/db/catalog.ts +0 -109
- package/src/devserver/db/database.ts +0 -1834
- package/src/devserver/db/derives.ts +0 -121
- package/src/devserver/db/index.ts +0 -20
- package/src/devserver/db/routeKinds.ts +0 -191
- package/src/devserver/db/types.ts +0 -168
- package/src/devserver/email/caps.ts +0 -0
- package/src/devserver/email/config.ts +0 -141
- package/src/devserver/email/console.ts +0 -348
- package/src/devserver/email/index.ts +0 -155
- package/src/devserver/email/providers.ts +0 -131
- package/src/devserver/email/status.ts +0 -23
- package/src/devserver/email/validate.ts +0 -37
- package/src/devserver/email/wire.ts +0 -55
- package/src/devserver/http/cache.ts +0 -0
- package/src/devserver/http/envelope.ts +0 -157
- package/src/devserver/http/proxy.ts +0 -172
- package/src/devserver/http/runtime.ts +0 -387
- package/src/devserver/index.ts +0 -43
- package/src/devserver/production-ipc.ts +0 -63
- package/src/devserver/production-worker.ts +0 -83
- package/src/devserver/production.ts +0 -718
- package/src/devserver/runtime/crypto.ts +0 -587
- package/src/devserver/runtime/host.ts +0 -427
- package/src/devserver/runtime/module.ts +0 -442
- package/src/devserver/server.ts +0 -251
- package/src/devserver/ssr.ts +0 -208
- package/src/devserver/stream/catalog.ts +0 -106
- package/src/devserver/stream/host.ts +0 -42
- package/src/devserver/stream/index.ts +0 -301
- package/src/devserver/stream/manager.ts +0 -167
- package/src/devserver/stream/router.ts +0 -101
- package/src/devserver/stream/wire.ts +0 -58
- package/src/devserver/stream/ws.ts +0 -78
- package/src/devserver/wasm/sections.ts +0 -59
- package/src/devserver/wasm/surface.ts +0 -115
- package/src/io/FastMap.ts +0 -151
- package/src/io/FastSet.ts +0 -113
- package/src/io/codec.ts +0 -362
- package/src/io/index.ts +0 -10
- package/src/io/lengths.ts +0 -14
- package/src/io/types.ts +0 -19
- package/src/logger/index.ts +0 -22
- package/src/shared/index.ts +0 -46
- package/test/analytics-dev.test.ts +0 -113
- package/test/assembly/aspect-shim.ts +0 -33
- package/test/assembly/cookie.spec.ts +0 -303
- package/test/assembly/example.spec.ts +0 -22
- package/test/assembly/ssr.spec.ts +0 -95
- package/test/assembly.test.ts +0 -59
- package/test/auth-emails.test.ts +0 -118
- package/test/built-ssr.test.ts +0 -148
- package/test/channel.test.ts +0 -21
- package/test/config-host-port.test.ts +0 -59
- package/test/configure.test.ts +0 -94
- package/test/daemon-build.test.ts +0 -278
- package/test/daemon-catalog.test.ts +0 -349
- package/test/daemon-emulation.test.ts +0 -256
- package/test/db.test.ts +0 -0
- package/test/devserver-database.test.ts +0 -1102
- package/test/devserver-email.test.ts +0 -306
- package/test/devserver-pqauth.test.ts +0 -93
- package/test/devserver-secrets.test.ts +0 -63
- package/test/devserver.test.ts +0 -259
- package/test/doctor.test.ts +0 -298
- package/test/dom/Image.test.tsx +0 -144
- package/test/dom/Link.test.tsx +0 -67
- package/test/dom/NavLink.test.tsx +0 -37
- package/test/dom/Script.test.tsx +0 -48
- package/test/dom/action.test.tsx +0 -146
- package/test/dom/error-overlay.test.tsx +0 -44
- package/test/dom/loader.test.tsx +0 -121
- package/test/dom/navigation.test.ts +0 -59
- package/test/dom/revalidate.test.tsx +0 -38
- package/test/dom/route-head.test.tsx +0 -56
- package/test/dom/router-loading.test.tsx +0 -44
- package/test/dom/scroll.test.ts +0 -56
- package/test/dom/slot.test.tsx +0 -131
- package/test/dom/use-metadata.test.tsx +0 -58
- package/test/dom/view-transitions.test.tsx +0 -53
- package/test/dynamic-routing.test.ts +0 -132
- package/test/email-console.test.ts +0 -86
- package/test/email-preview.test.ts +0 -73
- package/test/emails.test.ts +0 -58
- package/test/errors.test.ts +0 -21
- package/test/example-guestbook.test.ts +0 -123
- package/test/features.test.ts +0 -149
- package/test/fixtures/bignum-wire/spec.ts +0 -25
- package/test/fixtures/daemon-app.ts +0 -65
- package/test/fixtures/stream-echo.ts +0 -26
- package/test/fixtures/stream-gate.ts +0 -28
- package/test/fixtures/stream-trap.ts +0 -18
- package/test/fixtures/stream-typed.ts +0 -41
- package/test/fonts.test.ts +0 -28
- package/test/global-setup.ts +0 -17
- package/test/head.test.ts +0 -42
- package/test/image-blur.test.ts +0 -50
- package/test/io.test.ts +0 -117
- package/test/metadata.test.ts +0 -40
- package/test/navlink.test.ts +0 -28
- package/test/pages.test.ts +0 -105
- package/test/placeholder.test.ts +0 -9
- package/test/pqauth-e2e.test.ts +0 -216
- package/test/pqauth-email.test.ts +0 -801
- package/test/prerender.test.ts +0 -116
- package/test/prettier-plugin.test.ts +0 -46
- package/test/routes.test.ts +0 -76
- package/test/rpc-bignum-wire.test.ts +0 -164
- package/test/rpc-dispatch.test.ts +0 -132
- package/test/rpc-kinds.test.ts +0 -18
- package/test/rpc.test.ts +0 -66
- package/test/search.test.ts +0 -114
- package/test/seo.test.ts +0 -175
- package/test/slot-layouts.test.ts +0 -69
- package/test/sri.test.ts +0 -129
- package/test/ssg.test.ts +0 -36
- package/test/ssr-hydration.test.tsx +0 -122
- package/test/ssr-render.test.ts +0 -197
- package/test/ssr-template.test.tsx +0 -482
- package/test/stream-emulation.test.ts +0 -479
- package/test/ui.test.ts +0 -40
- package/test/update.test.ts +0 -58
- package/test/validate.test.ts +0 -42
- package/test/version-check.test.ts +0 -130
- package/tests/data-parity/generated-parity.ts +0 -99
- package/tests/data-parity/parity.ts +0 -80
- package/tests/data-parity/spec.ts +0 -46
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Dev STREAM connection driver: the resident per-connection box lifecycle for the dev server, a
|
|
3
|
-
* faithful port of the edge session driver `StreamWorker` (`toil-backend` `src/wasm/stream/worker.rs`)
|
|
4
|
-
* and a sibling of `DaemonHost`. It owns one resident {@link DevStreamBox} per live connection (state
|
|
5
|
-
* persists across that connection's events) and mtime-reloads the `release-stream.wasm` artifact so a
|
|
6
|
-
* rebuild during `npm run dev` is picked up - a NEW connection then gets the new artifact while live
|
|
7
|
-
* connections keep their resident box (mirroring the edge's per-connection pinning).
|
|
8
|
-
*
|
|
9
|
-
* It mirrors `StreamWorker`'s decisions byte-for-byte where they matter in dev: `acceptUpgrade` fires
|
|
10
|
-
* `@connect` with the connect context and HONORS the guest accept/reject (the `@connect` bridge);
|
|
11
|
-
* `dispatch` returns reply frames, a guest-reject close, or a TRAP-induced `STREAM_HOOK_TRAPPED` close
|
|
12
|
-
* that discards the poisoned box. The edge-only concerns it omits (the node-mode gate, the per-node
|
|
13
|
-
* RAM admission, and gas-metered kills) do not apply to a single-process dev server: dev serves one
|
|
14
|
-
* app, has no RAM budget, and Node's `WebAssembly` has no gas middleware (a runaway loop hangs in dev,
|
|
15
|
-
* only a real trap surfaces).
|
|
16
|
-
*
|
|
17
|
-
* Transport-agnostic: the dev WebSocket endpoint calls `acceptUpgrade` on a new socket, `dispatch` per
|
|
18
|
-
* inbound frame (feeding the reply frames back out, or closing on a close code), and `close`/
|
|
19
|
-
* `disconnect` on teardown. Kept transport-free so it is unit-testable without a socket.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
import fs from 'node:fs';
|
|
23
|
-
|
|
24
|
-
import { DevStreamBox } from './index.js';
|
|
25
|
-
|
|
26
|
-
/** `0x0208 STREAM_REJECTED`: the upgrade is refused for a non-`@connect` reason (no artifact / load
|
|
27
|
-
* failure). Matches the edge `STREAM_REJECTED`. */
|
|
28
|
-
export const STREAM_REJECTED = 0x0208;
|
|
29
|
-
/** `0x0200 STREAM_HOOK_TRAPPED`: a hook TRAPPED (unreachable / OOB / abort); the box is discarded and
|
|
30
|
-
* the connection closed. Matches the edge `STREAM_HOOK_TRAPPED`. */
|
|
31
|
-
export const STREAM_HOOK_TRAPPED = 0x0200;
|
|
32
|
-
|
|
33
|
-
/** The outcome of an upgrade accept attempt (mirrors the edge `StreamUpgradeOutcome`). */
|
|
34
|
-
export type StreamUpgradeOutcome =
|
|
35
|
-
| { readonly kind: 'accepted'; readonly streamId: bigint; readonly initialEgress: Buffer[] }
|
|
36
|
-
| { readonly kind: 'rejected'; readonly code: number; readonly initialEgress: Buffer[] };
|
|
37
|
-
|
|
38
|
-
/** The outcome of driving one inbound frame (mirrors the edge `StreamDatagramOutcome`). */
|
|
39
|
-
export type StreamDispatchResult =
|
|
40
|
-
| { readonly kind: 'reply'; readonly frames: Buffer[] }
|
|
41
|
-
| { readonly kind: 'close'; readonly code: number }
|
|
42
|
-
| { readonly kind: 'noConnection' };
|
|
43
|
-
|
|
44
|
-
interface ResidentConn {
|
|
45
|
-
readonly box: DevStreamBox;
|
|
46
|
-
readonly streamId: bigint;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
export class StreamDevHost {
|
|
50
|
-
private bytes: Buffer | null = null;
|
|
51
|
-
private loadedMtimeMs = -1;
|
|
52
|
-
private readonly conns = new Map<string, ResidentConn>();
|
|
53
|
-
/** Host-assigned stream id source: monotonic, never 0 (05 section 2.2). */
|
|
54
|
-
private nextStreamId = 1n;
|
|
55
|
-
|
|
56
|
-
constructor(private readonly streamWasmPath: string) {}
|
|
57
|
-
|
|
58
|
-
/** Number of live stream connections. */
|
|
59
|
-
get activeConnections(): number {
|
|
60
|
-
return this.conns.size;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** Whether a connection is live under `connId`. */
|
|
64
|
-
has(connId: string): boolean {
|
|
65
|
-
return this.conns.has(connId);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Accept (or reject) an upgrade for `authority` + `path`, mirroring `StreamWorker::accept_upgrade`:
|
|
70
|
-
* (re)load the artifact on mtime change, instantiate a resident box, fire `@connect` WITH the
|
|
71
|
-
* connect context, and HONOR the guest's accept/reject. Returns the host-assigned stream id on
|
|
72
|
-
* accept, plus any initial egress staged by `@connect`, or a `0x02xx` reject code. Fails closed
|
|
73
|
-
* (no connection registered, box dropped) on a missing/unreadable artifact (`0x0208`), a
|
|
74
|
-
* `@connect` reject (the guest's code), or a load/connect trap (`0x0200`). Throws only on a
|
|
75
|
-
* duplicate `connId`.
|
|
76
|
-
*/
|
|
77
|
-
acceptUpgrade(connId: string, authority: string, path: string): StreamUpgradeOutcome {
|
|
78
|
-
if (this.conns.has(connId))
|
|
79
|
-
throw new Error(`stream connection '${connId}' is already open`);
|
|
80
|
-
this.refresh();
|
|
81
|
-
if (!this.bytes) return { kind: 'rejected', code: STREAM_REJECTED, initialEgress: [] };
|
|
82
|
-
let box: DevStreamBox;
|
|
83
|
-
try {
|
|
84
|
-
box = DevStreamBox.load(this.bytes);
|
|
85
|
-
} catch {
|
|
86
|
-
return { kind: 'rejected', code: STREAM_REJECTED, initialEgress: [] };
|
|
87
|
-
}
|
|
88
|
-
const streamId = this.allocStreamId();
|
|
89
|
-
let outcome;
|
|
90
|
-
try {
|
|
91
|
-
outcome = box.onConnect(streamId, authority, path);
|
|
92
|
-
} catch {
|
|
93
|
-
return { kind: 'rejected', code: STREAM_HOOK_TRAPPED, initialEgress: [] }; // @connect trapped
|
|
94
|
-
}
|
|
95
|
-
if (outcome.kind === 'reject') {
|
|
96
|
-
return { kind: 'rejected', code: outcome.code, initialEgress: outcome.initialEgress };
|
|
97
|
-
}
|
|
98
|
-
this.conns.set(connId, { box, streamId });
|
|
99
|
-
return { kind: 'accepted', streamId, initialEgress: outcome.initialEgress };
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Drive an inbound frame into the connection's `@message` hook, mirroring
|
|
104
|
-
* `StreamWorker::dispatch_datagram`: `reply` frames (to feed back out), a guest-`reject` `close`, a
|
|
105
|
-
* TRAP-induced `close` (discarding the poisoned box), or `noConnection` for an unknown id.
|
|
106
|
-
*/
|
|
107
|
-
dispatch(connId: string, inbound: Buffer): StreamDispatchResult {
|
|
108
|
-
const conn = this.conns.get(connId);
|
|
109
|
-
if (!conn) return { kind: 'noConnection' };
|
|
110
|
-
try {
|
|
111
|
-
const out = conn.box.onMessage(conn.streamId, inbound);
|
|
112
|
-
if (out.kind === 'reply') return { kind: 'reply', frames: out.frames };
|
|
113
|
-
return { kind: 'close', code: out.code };
|
|
114
|
-
} catch {
|
|
115
|
-
// The @message hook TRAPPED (a real wasm trap; dev has no gas middleware) -> discard the
|
|
116
|
-
// poisoned box + close, mirroring the edge's STREAM_HOOK_TRAPPED (05 7.4).
|
|
117
|
-
this.conns.delete(connId);
|
|
118
|
-
return { kind: 'close', code: STREAM_HOOK_TRAPPED };
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** Graceful close: fire `@close` and drop the box. No-op if the connection is gone. */
|
|
123
|
-
close(connId: string): void {
|
|
124
|
-
const conn = this.conns.get(connId);
|
|
125
|
-
if (!conn) return;
|
|
126
|
-
try {
|
|
127
|
-
conn.box.onClose(conn.streamId);
|
|
128
|
-
} catch {
|
|
129
|
-
// a trapping @close still tears the connection down
|
|
130
|
-
}
|
|
131
|
-
this.conns.delete(connId);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/** Abrupt teardown: fire `@disconnect` and drop the box. No-op if the connection is gone. */
|
|
135
|
-
disconnect(connId: string): void {
|
|
136
|
-
const conn = this.conns.get(connId);
|
|
137
|
-
if (!conn) return;
|
|
138
|
-
try {
|
|
139
|
-
conn.box.onDisconnect(conn.streamId);
|
|
140
|
-
} catch {
|
|
141
|
-
// a trapping @disconnect still tears the connection down
|
|
142
|
-
}
|
|
143
|
-
this.conns.delete(connId);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/** (Re)read the artifact bytes when its mtime changes (mirrors `DaemonHost.refresh`). Clears the
|
|
147
|
-
* bytes when the file is missing; leaves live connections' resident boxes untouched. */
|
|
148
|
-
private refresh(): void {
|
|
149
|
-
let mtimeMs: number;
|
|
150
|
-
try {
|
|
151
|
-
mtimeMs = fs.statSync(this.streamWasmPath).mtimeMs;
|
|
152
|
-
} catch {
|
|
153
|
-
this.bytes = null;
|
|
154
|
-
this.loadedMtimeMs = -1;
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
if (mtimeMs === this.loadedMtimeMs && this.bytes) return;
|
|
158
|
-
this.bytes = fs.readFileSync(this.streamWasmPath);
|
|
159
|
-
this.loadedMtimeMs = mtimeMs;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
private allocStreamId(): bigint {
|
|
163
|
-
const id = this.nextStreamId;
|
|
164
|
-
this.nextStreamId = id + 1n;
|
|
165
|
-
return id;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The dev STREAM ROUTER (doc 08 sections 4.1/4.2): the `streamModule` that `wireStreams` drives. It
|
|
3
|
-
* owns the `toilstream.catalog` route table + a single resident-box {@link StreamDevHost} for the app's
|
|
4
|
-
* `release-stream.wasm`, and turns a route-matched WebSocket upgrade into a {@link StreamWsSession}
|
|
5
|
-
* driving that box. `matchRoute` (4.2) lets `wireStreams` tell a `@stream` upgrade from a Vite-HMR one;
|
|
6
|
-
* both the catalog and the box mtime-reload so a rebuild during `npm run dev` is picked up.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import fs from 'node:fs';
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
matchStreamRoute,
|
|
13
|
-
parseStreamCatalog,
|
|
14
|
-
type StreamCatalog,
|
|
15
|
-
type StreamDef,
|
|
16
|
-
} from './catalog.js';
|
|
17
|
-
import { StreamDevHost } from './manager.js';
|
|
18
|
-
import { StreamWsSession } from './ws.js';
|
|
19
|
-
|
|
20
|
-
/** The subset of the hyper-express `Websocket` the router drives (so it is unit-testable with a mock). */
|
|
21
|
-
export interface StreamWs {
|
|
22
|
-
send(data: Buffer, isBinary: boolean): void;
|
|
23
|
-
close(code: number): void;
|
|
24
|
-
on(event: 'message', cb: (message: Buffer, isBinary: boolean) => void): void;
|
|
25
|
-
on(event: 'close', cb: () => void): void;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** The upgrade context `wireStreams` stamps for a stream-route upgrade (doc 08 4.1). */
|
|
29
|
-
export interface StreamUpgradeContext {
|
|
30
|
-
readonly kind: 'stream';
|
|
31
|
-
readonly route: string;
|
|
32
|
-
readonly url: string;
|
|
33
|
-
readonly authority: string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export class StreamRouter {
|
|
37
|
-
private catalog: StreamCatalog = new Map();
|
|
38
|
-
private catalogMtimeMs = -1;
|
|
39
|
-
private readonly host: StreamDevHost;
|
|
40
|
-
private connSeq = 0;
|
|
41
|
-
|
|
42
|
-
constructor(private readonly streamWasmPath: string) {
|
|
43
|
-
this.host = new StreamDevHost(streamWasmPath);
|
|
44
|
-
this.refreshCatalog();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** Live stream connections (diagnostics / tests). */
|
|
48
|
-
get activeConnections(): number {
|
|
49
|
-
return this.host.activeConnections;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** doc 08 4.2: match a request path to a `@stream` route (re-reading the catalog on a rebuild), or
|
|
53
|
-
* `null` when it is not a stream route (the upgrade is then proxied to Vite). */
|
|
54
|
-
matchRoute(path: string): StreamDef | null {
|
|
55
|
-
this.refreshCatalog();
|
|
56
|
-
return matchStreamRoute(this.catalog, path);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** Handle a route-matched upgrade: open a resident box (`@connect`) and bridge the socket to it
|
|
60
|
-
* (inbound frame -> `@message` -> reply frames out; socket close -> `@close`). On a `@connect`
|
|
61
|
-
* reject the session closes the socket and registers no box. */
|
|
62
|
-
onUpgrade(ws: StreamWs, ctx: StreamUpgradeContext): void {
|
|
63
|
-
const connId = `s${String(++this.connSeq)}`;
|
|
64
|
-
const q = ctx.url.indexOf('?');
|
|
65
|
-
const path = q >= 0 ? ctx.url.slice(0, q) : ctx.url;
|
|
66
|
-
const session = new StreamWsSession(this.host, connId, ctx.authority, path, {
|
|
67
|
-
send: (frame) => {
|
|
68
|
-
ws.send(frame, true);
|
|
69
|
-
},
|
|
70
|
-
close: (code) => {
|
|
71
|
-
ws.close(code);
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
if (!session.onOpen()) return; // rejected -> socket closed, nothing resident
|
|
75
|
-
ws.on('message', (message: Buffer) => {
|
|
76
|
-
session.onMessage(message);
|
|
77
|
-
});
|
|
78
|
-
ws.on('close', () => {
|
|
79
|
-
session.onClose();
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** (Re)read the route table when the artifact mtime changes (mirrors `DaemonHost.refresh`). */
|
|
84
|
-
private refreshCatalog(): void {
|
|
85
|
-
let mtimeMs: number;
|
|
86
|
-
try {
|
|
87
|
-
mtimeMs = fs.statSync(this.streamWasmPath).mtimeMs;
|
|
88
|
-
} catch {
|
|
89
|
-
this.catalog = new Map();
|
|
90
|
-
this.catalogMtimeMs = -1;
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
if (mtimeMs === this.catalogMtimeMs && this.catalog.size > 0) return;
|
|
94
|
-
try {
|
|
95
|
-
this.catalog = parseStreamCatalog(fs.readFileSync(this.streamWasmPath));
|
|
96
|
-
} catch {
|
|
97
|
-
this.catalog = new Map();
|
|
98
|
-
}
|
|
99
|
-
this.catalogMtimeMs = mtimeMs;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* doc 08 section 4.1: the stream-aware WebSocket router that REPLACES `wireWebsocketProxy` when the dev
|
|
3
|
-
* process serves streams. It intercepts upgrades whose path matches a `@stream` route (via the
|
|
4
|
-
* {@link StreamRouter}) and drives them as resident-box connections; every OTHER upgrade (Vite HMR) is
|
|
5
|
-
* piped upstream EXACTLY as before ({@link pipeToVite}), so HMR is byte-for-byte unchanged. The branch
|
|
6
|
-
* is decided in `app.upgrade` (which stamps `ctx.kind`) and dispatched in the single catch-all
|
|
7
|
-
* `app.ws` handler, mirroring the existing proxy's structure.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { type Request, type Response, type Server, type Websocket } from '@dacely/hyper-express';
|
|
11
|
-
|
|
12
|
-
import { pipeToVite, type ViteTarget } from '../http/proxy.js';
|
|
13
|
-
import { type StreamRouter, type StreamUpgradeContext, type StreamWs } from './router.js';
|
|
14
|
-
|
|
15
|
-
/** Whether this dev process serves streams (doc 08 4.1): `regional` / `continental` / `all`. An
|
|
16
|
-
* L1 (`hot`) or L4 (`daemon`) dev process does NOT - it rejects/redirects the upgrade. */
|
|
17
|
-
export function streamEmulationEnabled(nodeMode: string): boolean {
|
|
18
|
-
return nodeMode === 'regional' || nodeMode === 'continental' || nodeMode === 'all';
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function wireStreams(app: Server, vite: ViteTarget, router: StreamRouter): void {
|
|
22
|
-
app.upgrade('/*', (request: Request, response: Response) => {
|
|
23
|
-
const def = router.matchRoute(request.path);
|
|
24
|
-
if (def !== null) {
|
|
25
|
-
// A @stream route: stamp the stream context; the ws handler drives the resident box.
|
|
26
|
-
response.upgrade({
|
|
27
|
-
kind: 'stream',
|
|
28
|
-
route: def.route,
|
|
29
|
-
url: request.url,
|
|
30
|
-
authority: request.headers['host'] ?? '',
|
|
31
|
-
});
|
|
32
|
-
} else {
|
|
33
|
-
// Anything else (Vite HMR): the existing proxy context, unchanged.
|
|
34
|
-
response.upgrade({
|
|
35
|
-
kind: 'vite',
|
|
36
|
-
url: request.url,
|
|
37
|
-
protocol: request.headers['sec-websocket-protocol'] ?? '',
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
app.ws(
|
|
43
|
-
'/*',
|
|
44
|
-
// idle_timeout 0: the stream emulator drives liveness itself; the Vite branch never idles out.
|
|
45
|
-
{ message_type: 'Buffer', idle_timeout: 0, max_payload_length: 16 * 1024 * 1024 },
|
|
46
|
-
(ws: Websocket) => {
|
|
47
|
-
const ctx = ws.context as { kind?: string };
|
|
48
|
-
if (ctx.kind === 'stream') {
|
|
49
|
-
router.onUpgrade(
|
|
50
|
-
ws as unknown as StreamWs,
|
|
51
|
-
ws.context as unknown as StreamUpgradeContext,
|
|
52
|
-
);
|
|
53
|
-
} else {
|
|
54
|
-
pipeToVite(ws, vite, ws.context as { url: string; protocol: string });
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
);
|
|
58
|
-
}
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Transport adapter that bridges ONE dev WebSocket connection to the {@link StreamDevHost} session
|
|
3
|
-
* driver. It is the per-socket glue the dev WS endpoint instantiates: socket-open -> `acceptUpgrade`
|
|
4
|
-
* (close on a `@connect`/gate reject), inbound frame -> `dispatch` (send reply frames back, or close
|
|
5
|
-
* on a reject/trap close code), socket-close -> `@close` + drop. It owns NO transport itself - the
|
|
6
|
-
* caller passes a {@link StreamWsTransport} of `send`/`close` callbacks - so it is unit-testable
|
|
7
|
-
* without a live socket and reusable across whatever WS/WebTransport the endpoint ends up speaking.
|
|
8
|
-
*
|
|
9
|
-
* The endpoint that wires hyper-express `app.ws` to this (the URL convention + coexistence with the
|
|
10
|
-
* Vite-HMR catch-all proxy + a `streamWasmFile` config) is the remaining live-server step; this is the
|
|
11
|
-
* transport-agnostic core it drives.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import type { StreamDevHost } from './manager.js';
|
|
15
|
-
|
|
16
|
-
/** The socket-side primitives this adapter needs: send one binary frame, and close with a code. */
|
|
17
|
-
export interface StreamWsTransport {
|
|
18
|
-
/** Send one outbound binary frame to the client. */
|
|
19
|
-
send(frame: Buffer): void;
|
|
20
|
-
/** Close the connection with a `0x02xx` stream close code. */
|
|
21
|
-
close(code: number): void;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class StreamWsSession {
|
|
25
|
-
private open = false;
|
|
26
|
-
|
|
27
|
-
constructor(
|
|
28
|
-
private readonly host: StreamDevHost,
|
|
29
|
-
private readonly connId: string,
|
|
30
|
-
private readonly authority: string,
|
|
31
|
-
private readonly path: string,
|
|
32
|
-
private readonly transport: StreamWsTransport,
|
|
33
|
-
) {}
|
|
34
|
-
|
|
35
|
-
/** Whether the connection is accepted + live. */
|
|
36
|
-
get isOpen(): boolean {
|
|
37
|
-
return this.open;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Socket open: accept the upgrade (node gate is the endpoint's job; this drives the box). On a
|
|
42
|
-
* `@connect`/artifact reject the connection is closed with the code and no box is held. Returns
|
|
43
|
-
* whether the connection was accepted.
|
|
44
|
-
*/
|
|
45
|
-
onOpen(): boolean {
|
|
46
|
-
const up = this.host.acceptUpgrade(this.connId, this.authority, this.path);
|
|
47
|
-
if (up.kind === 'rejected') {
|
|
48
|
-
for (const frame of up.initialEgress) this.transport.send(frame);
|
|
49
|
-
this.transport.close(up.code);
|
|
50
|
-
return false;
|
|
51
|
-
}
|
|
52
|
-
this.open = true;
|
|
53
|
-
for (const frame of up.initialEgress) this.transport.send(frame);
|
|
54
|
-
return true;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** An inbound binary frame: dispatch it; send the reply frames, or close on a reject/trap code. */
|
|
58
|
-
onMessage(inbound: Buffer): void {
|
|
59
|
-
if (!this.open) return;
|
|
60
|
-
const r = this.host.dispatch(this.connId, inbound);
|
|
61
|
-
if (r.kind === 'reply') {
|
|
62
|
-
for (const frame of r.frames) this.transport.send(frame);
|
|
63
|
-
} else if (r.kind === 'close') {
|
|
64
|
-
// A guest reject or a TRAP close: close the socket; the socket-close event runs onClose,
|
|
65
|
-
// which fires @close + drops the box (a no-op if a trap already discarded it).
|
|
66
|
-
this.open = false;
|
|
67
|
-
this.transport.close(r.code);
|
|
68
|
-
}
|
|
69
|
-
// 'noConnection' cannot occur for a live, accepted session.
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Socket close (client-initiated or after our own close): fire `@close` + drop the box. */
|
|
73
|
-
onClose(): void {
|
|
74
|
-
if (!this.open && !this.host.has(this.connId)) return;
|
|
75
|
-
this.open = false;
|
|
76
|
-
this.host.close(this.connId);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared, bounds-checked wasm custom-section walker. Factored out of
|
|
3
|
-
* `db/catalog.ts` so the three Toil section parsers (`toildb.catalog`,
|
|
4
|
-
* `toilstream.catalog`, `toildaemon.catalog`, `toil.surface`) share ONE
|
|
5
|
-
* magic-skipping loop instead of drifting copies.
|
|
6
|
-
*
|
|
7
|
-
* Every input is a tenant-built, possibly mid-rebuild wasm, so the walker must
|
|
8
|
-
* never read past the buffer: a truncated/garbage section table returns `null`
|
|
9
|
-
* (treated by callers as "no section"). Mirrors the host-side walker
|
|
10
|
-
* (`toil-backend` custom-section reader) and the toilscript-side test walker.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
/** Read a LEB128 from `buf` at `pos`; throws on overrun so a truncated module
|
|
14
|
-
* can never over-read (the caller catches and treats it as "no section"). */
|
|
15
|
-
export function leb(buf: Buffer, pos: number): [number, number] {
|
|
16
|
-
let result = 0;
|
|
17
|
-
let shift = 0;
|
|
18
|
-
let p = pos;
|
|
19
|
-
for (;;) {
|
|
20
|
-
if (p >= buf.length) throw new RangeError('leb128 past end of buffer');
|
|
21
|
-
const b = buf[p++];
|
|
22
|
-
result |= (b & 0x7f) << shift;
|
|
23
|
-
if ((b & 0x80) === 0) break;
|
|
24
|
-
shift += 7;
|
|
25
|
-
if (shift > 35) throw new RangeError('leb128 too long');
|
|
26
|
-
}
|
|
27
|
-
return [result >>> 0, p];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** The bytes of the named wasm custom section, or `null` if absent. Bounds-checked
|
|
31
|
-
* so a truncated/garbage module can never read past the buffer. */
|
|
32
|
-
export function customSection(wasm: Buffer, want: string): Buffer | null {
|
|
33
|
-
if (
|
|
34
|
-
wasm.length < 8 ||
|
|
35
|
-
wasm[0] !== 0x00 ||
|
|
36
|
-
wasm[1] !== 0x61 ||
|
|
37
|
-
wasm[2] !== 0x73 ||
|
|
38
|
-
wasm[3] !== 0x6d
|
|
39
|
-
)
|
|
40
|
-
return null;
|
|
41
|
-
let pos = 8; // skip the 8-byte magic + version header
|
|
42
|
-
while (pos < wasm.length) {
|
|
43
|
-
const id = wasm[pos++];
|
|
44
|
-
let size: number;
|
|
45
|
-
[size, pos] = leb(wasm, pos);
|
|
46
|
-
const end = pos + size;
|
|
47
|
-
if (end > wasm.length || end < pos) return null; // truncated section table
|
|
48
|
-
if (id === 0) {
|
|
49
|
-
const [nameLen, namePos] = leb(wasm, pos);
|
|
50
|
-
if (
|
|
51
|
-
namePos + nameLen <= end &&
|
|
52
|
-
wasm.toString('latin1', namePos, namePos + nameLen) === want
|
|
53
|
-
)
|
|
54
|
-
return wasm.subarray(namePos + nameLen, end);
|
|
55
|
-
}
|
|
56
|
-
pos = end;
|
|
57
|
-
}
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parse a compiled artifact's `toil.surface` custom section. Emitted into EVERY
|
|
3
|
-
* Toil artifact by the toilscript `buildToilSurface` pass (hot AND cold). The dev
|
|
4
|
-
* server reads it to decide whether each artifact carries the daemon surface
|
|
5
|
-
* (start the daemon emulator) or the stream surface (Phase 4, deferred).
|
|
6
|
-
*
|
|
7
|
-
* Byte layout (RECONCILIATION Part 5, all little-endian; mirrors the toilscript
|
|
8
|
-
* `CatWriter` emitter byte-for-byte):
|
|
9
|
-
*
|
|
10
|
-
* u16 format_version = 1
|
|
11
|
-
* u8 target_mode (0 = hot, 1 = cold; there is no target_mode = 2)
|
|
12
|
-
* u8 reserved0
|
|
13
|
-
* u32 surface_flags (bit0 rest, bit1 stream, bit2 daemon,
|
|
14
|
-
* bit3 scheduled, bit4 database, bit5 render)
|
|
15
|
-
* u16 abi_version
|
|
16
|
-
* str build_id (u32 len + UTF-8)
|
|
17
|
-
* u32 fingerprint
|
|
18
|
-
* u32 data_coherence_hash
|
|
19
|
-
* u32 pair_coherence_hash (exactly THREE u32 after build_id, not four)
|
|
20
|
-
*
|
|
21
|
-
* Fail-closed per Part 5's host rule: an absent or unparseable section is a
|
|
22
|
-
* corrupt Toil artifact -> do not start that artifact's emulator.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
import { DataReader } from 'toiljs/io';
|
|
26
|
-
|
|
27
|
-
import { customSection } from './sections.js';
|
|
28
|
-
|
|
29
|
-
export const SURFACE_FORMAT_VERSION = 1;
|
|
30
|
-
export const SURFACE_ABI_VERSION = 1;
|
|
31
|
-
|
|
32
|
-
const TARGET_HOT = 0;
|
|
33
|
-
const TARGET_COLD = 1;
|
|
34
|
-
const FLAG_REST = 1 << 0;
|
|
35
|
-
const FLAG_STREAM = 1 << 1;
|
|
36
|
-
const FLAG_DAEMON = 1 << 2;
|
|
37
|
-
const FLAG_SCHEDULED = 1 << 3;
|
|
38
|
-
const FLAG_DATABASE = 1 << 4;
|
|
39
|
-
const FLAG_RENDER = 1 << 5;
|
|
40
|
-
const FLAG_KNOWN_MASK =
|
|
41
|
-
FLAG_REST | FLAG_STREAM | FLAG_DAEMON | FLAG_SCHEDULED | FLAG_DATABASE | FLAG_RENDER;
|
|
42
|
-
|
|
43
|
-
export interface SurfaceFlags {
|
|
44
|
-
readonly rest: boolean;
|
|
45
|
-
readonly stream: boolean;
|
|
46
|
-
readonly daemon: boolean;
|
|
47
|
-
readonly scheduled: boolean;
|
|
48
|
-
readonly database: boolean;
|
|
49
|
-
readonly render: boolean;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface Surface {
|
|
53
|
-
/** 0 = hot, 1 = cold. */
|
|
54
|
-
readonly targetMode: 'hot' | 'cold';
|
|
55
|
-
readonly flags: SurfaceFlags;
|
|
56
|
-
readonly abiVersion: number;
|
|
57
|
-
readonly buildId: string;
|
|
58
|
-
readonly fingerprint: number;
|
|
59
|
-
readonly dataCoherenceHash: number;
|
|
60
|
-
readonly pairCoherenceHash: number;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** `'invalid'` => absent or corrupt (fail closed). Otherwise the parsed surface. */
|
|
64
|
-
export function parseSurface(wasm: Buffer): Surface | 'invalid' {
|
|
65
|
-
let sec: Buffer | null;
|
|
66
|
-
try {
|
|
67
|
-
sec = customSection(wasm, 'toil.surface');
|
|
68
|
-
} catch {
|
|
69
|
-
return 'invalid'; // garbage section table
|
|
70
|
-
}
|
|
71
|
-
if (sec === null) return 'invalid';
|
|
72
|
-
|
|
73
|
-
const r = new DataReader(sec);
|
|
74
|
-
const version = r.readU16(); // format_version
|
|
75
|
-
if (!r.ok || version !== SURFACE_FORMAT_VERSION) return 'invalid';
|
|
76
|
-
const targetModeByte = r.readU8();
|
|
77
|
-
if (!r.ok || (targetModeByte !== TARGET_HOT && targetModeByte !== TARGET_COLD)) {
|
|
78
|
-
return 'invalid';
|
|
79
|
-
}
|
|
80
|
-
const targetMode = targetModeByte === TARGET_COLD ? 'cold' : 'hot';
|
|
81
|
-
const reserved0 = r.readU8(); // reserved0
|
|
82
|
-
if (!r.ok || reserved0 !== 0) return 'invalid';
|
|
83
|
-
const f = r.readU32(); // surface_flags
|
|
84
|
-
if (!r.ok || (f & ~FLAG_KNOWN_MASK) !== 0) return 'invalid';
|
|
85
|
-
if ((f & FLAG_SCHEDULED) !== 0 && (f & FLAG_DAEMON) === 0) return 'invalid';
|
|
86
|
-
if (targetMode === 'hot' && (f & (FLAG_DAEMON | FLAG_SCHEDULED)) !== 0) {
|
|
87
|
-
return 'invalid';
|
|
88
|
-
}
|
|
89
|
-
if (targetMode === 'cold' && (f & (FLAG_REST | FLAG_STREAM | FLAG_RENDER)) !== 0) {
|
|
90
|
-
return 'invalid';
|
|
91
|
-
}
|
|
92
|
-
const abiVersion = r.readU16();
|
|
93
|
-
if (!r.ok || abiVersion !== SURFACE_ABI_VERSION) return 'invalid';
|
|
94
|
-
const buildId = r.readString();
|
|
95
|
-
const fingerprint = r.readU32();
|
|
96
|
-
const dataCoherenceHash = r.readU32(); // exactly THREE u32 after build_id
|
|
97
|
-
const pairCoherenceHash = r.readU32();
|
|
98
|
-
if (!r.ok || r.remaining() !== 0) return 'invalid'; // PRESENT but corrupt => fail closed
|
|
99
|
-
return {
|
|
100
|
-
targetMode,
|
|
101
|
-
flags: {
|
|
102
|
-
rest: !!(f & 1),
|
|
103
|
-
stream: !!(f & 2),
|
|
104
|
-
daemon: !!(f & 4),
|
|
105
|
-
scheduled: !!(f & 8),
|
|
106
|
-
database: !!(f & 16),
|
|
107
|
-
render: !!(f & 32),
|
|
108
|
-
},
|
|
109
|
-
abiVersion,
|
|
110
|
-
buildId,
|
|
111
|
-
fingerprint,
|
|
112
|
-
dataCoherenceHash,
|
|
113
|
-
pairCoherenceHash,
|
|
114
|
-
};
|
|
115
|
-
}
|