toiljs 0.0.67 → 0.0.68
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 +5 -0
- package/README.md +63 -61
- package/build/backend/.tsbuildinfo +1 -1
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +13 -1
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/index.d.ts +2 -0
- package/build/client/index.js +1 -0
- package/build/client/rpc.js +21 -1
- package/build/client/stream/client.d.ts +11 -0
- package/build/client/stream/client.js +59 -0
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/config.d.ts +2 -0
- package/build/compiler/config.js +9 -7
- package/build/compiler/index.d.ts +1 -0
- package/build/compiler/index.js +16 -2
- package/build/compiler/toil-docs.generated.js +2 -2
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/daemon/runtime.d.ts +13 -0
- package/build/devserver/daemon/runtime.js +29 -0
- package/build/devserver/db/database.d.ts +1 -0
- package/build/devserver/db/database.js +10 -0
- package/build/devserver/db/derives.d.ts +7 -0
- package/build/devserver/db/derives.js +94 -0
- package/build/devserver/db/index.d.ts +1 -0
- package/build/devserver/db/index.js +1 -0
- package/build/devserver/db/types.d.ts +1 -0
- package/build/devserver/db/types.js +1 -0
- package/build/devserver/http/proxy.d.ts +5 -1
- package/build/devserver/http/proxy.js +39 -36
- package/build/devserver/http/runtime.d.ts +62 -0
- package/build/devserver/http/runtime.js +194 -0
- package/build/devserver/index.d.ts +2 -0
- package/build/devserver/index.js +1 -0
- package/build/devserver/production-ipc.d.ts +50 -0
- package/build/devserver/production-ipc.js +21 -0
- package/build/devserver/production-worker.d.ts +1 -0
- package/build/devserver/production-worker.js +73 -0
- package/build/devserver/production.d.ts +35 -0
- package/build/devserver/production.js +502 -0
- package/build/devserver/runtime/module.d.ts +5 -0
- package/build/devserver/runtime/module.js +47 -1
- package/build/devserver/server.d.ts +1 -0
- package/build/devserver/server.js +32 -145
- package/build/devserver/ssr.d.ts +2 -0
- package/build/devserver/ssr.js +19 -2
- package/build/devserver/stream/catalog.d.ts +20 -0
- package/build/devserver/stream/catalog.js +54 -0
- package/build/devserver/stream/host.d.ts +9 -0
- package/build/devserver/stream/host.js +15 -0
- package/build/devserver/stream/index.d.ts +37 -0
- package/build/devserver/stream/index.js +220 -0
- package/build/devserver/stream/manager.d.ts +34 -0
- package/build/devserver/stream/manager.js +103 -0
- package/build/devserver/stream/router.d.ts +25 -0
- package/build/devserver/stream/router.js +64 -0
- package/build/devserver/stream/wire.d.ts +5 -0
- package/build/devserver/stream/wire.js +33 -0
- package/build/devserver/stream/ws.d.ts +18 -0
- package/build/devserver/stream/ws.js +46 -0
- package/docs/cli.md +3 -1
- package/docs/getting-started.md +7 -7
- package/examples/basic/server/routes/Guestbook.ts +38 -13
- package/package.json +2 -2
- package/src/cli/index.ts +14 -1
- package/src/client/index.ts +2 -0
- package/src/client/rpc.ts +25 -1
- package/src/client/stream/client.ts +107 -0
- package/src/compiler/config.ts +15 -7
- package/src/compiler/index.ts +24 -5
- package/src/compiler/toil-docs.generated.ts +2 -2
- package/src/devserver/daemon/runtime.ts +48 -0
- package/src/devserver/db/database.ts +14 -0
- package/src/devserver/db/derives.ts +121 -0
- package/src/devserver/db/index.ts +1 -0
- package/src/devserver/db/types.ts +6 -0
- package/src/devserver/http/proxy.ts +53 -39
- package/src/devserver/http/runtime.ts +287 -0
- package/src/devserver/index.ts +2 -0
- package/src/devserver/production-ipc.ts +63 -0
- package/src/devserver/production-worker.ts +83 -0
- package/src/devserver/production.ts +706 -0
- package/src/devserver/runtime/module.ts +95 -1
- package/src/devserver/server.ts +52 -201
- package/src/devserver/ssr.ts +23 -3
- package/src/devserver/stream/catalog.ts +106 -0
- package/src/devserver/stream/host.ts +42 -0
- package/src/devserver/stream/index.ts +308 -0
- package/src/devserver/stream/manager.ts +163 -0
- package/src/devserver/stream/router.ts +101 -0
- package/src/devserver/stream/wire.ts +58 -0
- package/src/devserver/stream/ws.ts +76 -0
- package/test/built-ssr.test.ts +98 -0
- package/test/devserver.test.ts +20 -4
- package/test/example-guestbook.test.ts +8 -5
- package/test/fixtures/stream-echo.ts +26 -0
- package/test/fixtures/stream-gate.ts +24 -0
- package/test/fixtures/stream-trap.ts +18 -0
- package/test/stream-emulation.test.ts +394 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-import surface for the dev STREAM (hot, L2/L3) box, mirroring the production edge's stream box
|
|
3
|
+
* (`toil-backend` `src/wasm/stream`). A `@stream` box is a HOT artifact: it imports the same request
|
|
4
|
+
* `env.*` runtime surface (so the ToilScript runtime + `@data`/crypto/env/`Date.now` code runs
|
|
5
|
+
* unchanged) and drives its lifecycle through the `stream_dispatch` export + the ingress/egress ring
|
|
6
|
+
* bridge ({@link ../index.js}), NOT through `handle`.
|
|
7
|
+
*
|
|
8
|
+
* The `stream.*` namespace (`stream.send` / `@channel`) is DEFERRED: the raw `@message` ring bridge -
|
|
9
|
+
* the only stream feature wired so far - needs NO host imports (the guest reads/writes the rings in
|
|
10
|
+
* its own linear memory). A box that does not use `stream.*` instantiates against `env.*` alone.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { buildDatabaseImports, type DbDevState, freshDbState } from '../db/index.js';
|
|
14
|
+
import { buildCryptoImports, type CryptoState, freshCryptoState } from '../runtime/crypto.js';
|
|
15
|
+
import { buildEnvImports, type MemoryRef } from '../runtime/host.js';
|
|
16
|
+
|
|
17
|
+
/** Per-stream-box host scratch (DB + crypto), analogous to the daemon's `DaemonState`. Each resident
|
|
18
|
+
* connection box carries its own, so two connections never share `@data` transaction scratch. */
|
|
19
|
+
export interface StreamBoxState {
|
|
20
|
+
crypto: CryptoState;
|
|
21
|
+
db: DbDevState;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function freshStreamBoxState(): StreamBoxState {
|
|
25
|
+
return { crypto: freshCryptoState(), db: freshDbState() };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The full `env` import object for a dev stream box: the request-surface `env.*` runtime (built by
|
|
30
|
+
* {@link buildEnvImports}) plus the crypto and `@data` namespaces. The response/stream `env.*`
|
|
31
|
+
* functions a box must not have (`set_status`/`respond_file`/...) are excluded by `buildEnvImports`
|
|
32
|
+
* itself, exactly as for the daemon cold box.
|
|
33
|
+
*/
|
|
34
|
+
export function buildStreamImports(ref: MemoryRef, state: StreamBoxState): WebAssembly.Imports {
|
|
35
|
+
return {
|
|
36
|
+
env: {
|
|
37
|
+
...buildEnvImports(ref, state),
|
|
38
|
+
...buildCryptoImports(ref, state.crypto),
|
|
39
|
+
...buildDatabaseImports(ref, state.db),
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev STREAM (L2/L3) emulation: load a `release-stream.wasm` hot artifact into a resident per-connection
|
|
3
|
+
* box and drive its lifecycle (`@connect` / `@message` / `@close` / `@disconnect`) through the
|
|
4
|
+
* `stream_dispatch` export + the ingress/egress RING BRIDGE - the dev-side port of the production
|
|
5
|
+
* `StreamBox` (`toil-backend` `src/wasm/stream/runtime.rs`). The ring wire format (RingControl 32B +
|
|
6
|
+
* RingFrame 12B + drained-reset + the packed-i64 reject bridge) AND the `@connect` info-block ABI are
|
|
7
|
+
* replicated BYTE-FOR-BYTE so a `@stream` app behaves identically under `npm run dev` and at the edge.
|
|
8
|
+
*
|
|
9
|
+
* RESIDENT: one box per connection, NEVER reset between events (linear memory persists across
|
|
10
|
+
* connect -> message -> close); a fresh `WebAssembly.Instance` per connection IS that residency.
|
|
11
|
+
*
|
|
12
|
+
* DEV LIMITATION vs the edge: Node's `WebAssembly` has NO gas-metering middleware, so a runaway guest
|
|
13
|
+
* loop is NOT gas-killed in dev (it hangs) - only a genuine wasm TRAP (unreachable / OOB / abort)
|
|
14
|
+
* surfaces, which the caller turns into a `STREAM_HOOK_TRAPPED` close. Packet gas, chunk reassembly,
|
|
15
|
+
* `@channel`, and the `stream.*` host imports are deferred exactly as on the edge.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { parseSurface } from '../wasm/surface.js';
|
|
19
|
+
import { buildStreamImports, freshStreamBoxState, type StreamBoxState } from './host.js';
|
|
20
|
+
|
|
21
|
+
// --- the stream_dispatch ABI (toil-backend src/wasm/stream/section.rs) ---
|
|
22
|
+
const EVENT_CONNECT = 1;
|
|
23
|
+
const EVENT_MESSAGE = 2;
|
|
24
|
+
const EVENT_CLOSE = 3;
|
|
25
|
+
const EVENT_DISCONNECT = 4;
|
|
26
|
+
|
|
27
|
+
// --- ring wire format (05 sections 5-6; toil-backend src/wasm/stream/runtime.rs) ---
|
|
28
|
+
const RING_CTRL_BYTES = 32; // RingControl header
|
|
29
|
+
const RING_FRAME_HEADER = 12; // RingFrame header
|
|
30
|
+
const RING_MAGIC = 0x3147_4e52; // "RNG1" little-endian
|
|
31
|
+
const RING_VERSION = 1;
|
|
32
|
+
const RC_WRITE = 12; // write_cursor offset in RingControl
|
|
33
|
+
const RC_READ = 16; // read_cursor offset
|
|
34
|
+
const FRAME_TYPE_DATA_RELIABLE = 1;
|
|
35
|
+
const MAX_STREAM_FRAME_LEN = 65536; // 64 KiB per egress frame (05 6.1)
|
|
36
|
+
|
|
37
|
+
// --- @connect info-block ABI (05 section 2.2; toil-backend runtime.rs write_connect_info) ---
|
|
38
|
+
// Layout: [u64 stream_id][u8 transport][u8 _][u16 auth_len][u16 path_len][u16 _] then auth + path UTF8.
|
|
39
|
+
const SI_TRANSPORT = 8;
|
|
40
|
+
const SI_AUTH_LEN = 10;
|
|
41
|
+
const SI_PATH_LEN = 12;
|
|
42
|
+
const SI_RESERVED2 = 14;
|
|
43
|
+
const SI_BODY = 16;
|
|
44
|
+
const SI_TRANSPORT_WEBTRANSPORT = 1; // the v1 transport (the dev emulates it)
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Decode a guest's NEGATIVE `stream_dispatch` return into a `0x02xx` reject/close code (the Part-3
|
|
48
|
+
* bridge `-(0x10000 + code)`), clamped to the stream range `0x0200..=0x02FF`; an out-of-range value
|
|
49
|
+
* normalizes to `0x0208 STREAM_REJECTED`, mirroring `toil-backend` `decode_reject_code`.
|
|
50
|
+
*/
|
|
51
|
+
export function decodeRejectCode(rc: bigint): number {
|
|
52
|
+
const raw = Number((-rc - 0x10000n) & 0xffffn);
|
|
53
|
+
return raw >= 0x0200 && raw <= 0x02ff ? raw : 0x0208;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface StreamExports {
|
|
57
|
+
readonly memory: WebAssembly.Memory;
|
|
58
|
+
readonly stream_dispatch: (eventKind: number, lo: number, hi: number) => bigint;
|
|
59
|
+
readonly stream_ring_offset?: () => number;
|
|
60
|
+
readonly stream_ring_capacity?: () => number;
|
|
61
|
+
readonly stream_egress_offset?: () => number;
|
|
62
|
+
readonly stream_egress_capacity?: () => number;
|
|
63
|
+
readonly stream_info_offset?: () => number;
|
|
64
|
+
readonly stream_info_capacity?: () => number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface Rings {
|
|
68
|
+
readonly ingressOff: number;
|
|
69
|
+
readonly ingressCap: number;
|
|
70
|
+
readonly egressOff: number;
|
|
71
|
+
readonly egressCap: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
interface StreamInfo {
|
|
75
|
+
readonly offset: number;
|
|
76
|
+
readonly cap: number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A `@message` dispatch outcome: the drained egress reply frames, or a guest reject code. */
|
|
80
|
+
export type StreamMessageOutcome =
|
|
81
|
+
| { readonly kind: 'reply'; readonly frames: Buffer[] }
|
|
82
|
+
| { readonly kind: 'reject'; readonly code: number };
|
|
83
|
+
|
|
84
|
+
/** A `@connect` outcome: the guest accepted (the box is usable) or rejected with a `0x02xx` code. */
|
|
85
|
+
export type StreamConnectOutcome =
|
|
86
|
+
| { readonly kind: 'accept' }
|
|
87
|
+
| { readonly kind: 'reject'; readonly code: number };
|
|
88
|
+
|
|
89
|
+
export class DevStreamBox {
|
|
90
|
+
private constructor(
|
|
91
|
+
private readonly exports: StreamExports,
|
|
92
|
+
private readonly _state: StreamBoxState,
|
|
93
|
+
private readonly rings: Rings | null,
|
|
94
|
+
private readonly streamInfo: StreamInfo | null,
|
|
95
|
+
) {}
|
|
96
|
+
|
|
97
|
+
/** Compile + instantiate a resident stream box from a HOT `release-stream.wasm`. Fails closed: a
|
|
98
|
+
* cold artifact, a missing `stream_dispatch`/`memory`, or a bad module throws. */
|
|
99
|
+
static load(wasm: Buffer): DevStreamBox {
|
|
100
|
+
const surface = parseSurface(wasm);
|
|
101
|
+
if (surface === 'invalid' || surface.targetMode !== 'hot') {
|
|
102
|
+
throw new Error('stream box requires a hot artifact with a valid toil.surface');
|
|
103
|
+
}
|
|
104
|
+
const ref: { memory: WebAssembly.Memory | null } = { memory: null };
|
|
105
|
+
const state = freshStreamBoxState();
|
|
106
|
+
const module = new WebAssembly.Module(new Uint8Array(wasm));
|
|
107
|
+
const instance = new WebAssembly.Instance(module, buildStreamImports(ref, state));
|
|
108
|
+
const exports = instance.exports as unknown as StreamExports;
|
|
109
|
+
if (
|
|
110
|
+
typeof exports.stream_dispatch !== 'function' ||
|
|
111
|
+
!(exports.memory instanceof WebAssembly.Memory)
|
|
112
|
+
) {
|
|
113
|
+
throw new Error("stream artifact must export 'stream_dispatch' + 'memory'");
|
|
114
|
+
}
|
|
115
|
+
ref.memory = exports.memory;
|
|
116
|
+
const rings = DevStreamBox.resolveRings(exports);
|
|
117
|
+
const streamInfo = DevStreamBox.resolveStreamInfo(exports);
|
|
118
|
+
const box = new DevStreamBox(exports, state, rings, streamInfo);
|
|
119
|
+
if (rings) box.stampRings();
|
|
120
|
+
return box;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Whether the box carries the ring runtime (a pre-bridge fixture omits the exports). */
|
|
124
|
+
get hasRings(): boolean {
|
|
125
|
+
return this.rings !== null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Whether the box carries the `@connect` info-block bridge (a `@connect(StreamInbound)` hook). */
|
|
129
|
+
get hasConnectBridge(): boolean {
|
|
130
|
+
return this.streamInfo !== null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Fire `@connect`: write the connect-info block (stream id + transport + authority + path) into the
|
|
135
|
+
* guest's info region, dispatch `EVENT_CONNECT`, and decode the `StreamOutbound` accept/reject. On
|
|
136
|
+
* accept, clear any `@connect`-staged egress (initial-egress is deferred, like the edge) so it does
|
|
137
|
+
* not contaminate the first `@message` reply. A box without the bridge runs `@connect` context-free
|
|
138
|
+
* (the write is a no-op) and always accepts unless it returns a negative code.
|
|
139
|
+
*/
|
|
140
|
+
onConnect(streamId: bigint, authority: string, path: string): StreamConnectOutcome {
|
|
141
|
+
this.writeConnectInfo(streamId, authority, path);
|
|
142
|
+
const rc = this.dispatch(EVENT_CONNECT, streamId);
|
|
143
|
+
if (rc < 0n) return { kind: 'reject', code: decodeRejectCode(rc) };
|
|
144
|
+
this.resetEgressRing();
|
|
145
|
+
return { kind: 'accept' };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** Fire `@close` (graceful close). */
|
|
149
|
+
onClose(streamId: bigint): bigint {
|
|
150
|
+
return this.dispatch(EVENT_CLOSE, streamId);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Fire `@disconnect` (abrupt teardown). */
|
|
154
|
+
onDisconnect(streamId: bigint): bigint {
|
|
155
|
+
return this.dispatch(EVENT_DISCONNECT, streamId);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Drive the raw-bytes `@message` bridge: write `inbound` as one DATA_RELIABLE frame into the
|
|
159
|
+
* ingress ring, fire `@message`, then drain the egress ring (reply) or surface the reject code.
|
|
160
|
+
* A genuine wasm trap propagates (the caller maps it to a STREAM_HOOK_TRAPPED close). */
|
|
161
|
+
onMessage(streamId: bigint, inbound: Buffer): StreamMessageOutcome {
|
|
162
|
+
if (!this.rings) {
|
|
163
|
+
throw new Error('stream box has no ring runtime; the message bridge is unavailable');
|
|
164
|
+
}
|
|
165
|
+
this.ingressWrite(inbound);
|
|
166
|
+
const ret = this.dispatch(EVENT_MESSAGE, streamId);
|
|
167
|
+
if (ret < 0n) return { kind: 'reject', code: decodeRejectCode(ret) };
|
|
168
|
+
return { kind: 'reply', frames: this.egressDrain() };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
private dispatch(eventKind: number, streamId: bigint): bigint {
|
|
172
|
+
const lo = Number(streamId & 0xffff_ffffn) | 0;
|
|
173
|
+
const hi = Number((streamId >> 32n) & 0xffff_ffffn) | 0;
|
|
174
|
+
return this.exports.stream_dispatch(eventKind, lo, hi);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private static resolveRings(e: StreamExports): Rings | null {
|
|
178
|
+
if (
|
|
179
|
+
typeof e.stream_ring_offset !== 'function' ||
|
|
180
|
+
typeof e.stream_ring_capacity !== 'function' ||
|
|
181
|
+
typeof e.stream_egress_offset !== 'function' ||
|
|
182
|
+
typeof e.stream_egress_capacity !== 'function'
|
|
183
|
+
) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
ingressOff: e.stream_ring_offset() >>> 0,
|
|
188
|
+
ingressCap: e.stream_ring_capacity() >>> 0,
|
|
189
|
+
egressOff: e.stream_egress_offset() >>> 0,
|
|
190
|
+
egressCap: e.stream_egress_capacity() >>> 0,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
private static resolveStreamInfo(e: StreamExports): StreamInfo | null {
|
|
195
|
+
if (typeof e.stream_info_offset !== 'function' || typeof e.stream_info_capacity !== 'function') {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
return { offset: e.stream_info_offset() >>> 0, cap: e.stream_info_capacity() >>> 0 };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private stampRings(): void {
|
|
202
|
+
const rings = this.rings;
|
|
203
|
+
if (!rings) return;
|
|
204
|
+
this.stampOne(rings.ingressOff, rings.ingressCap);
|
|
205
|
+
this.stampOne(rings.egressOff, rings.egressCap);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private stampOne(base: number, cap: number): void {
|
|
209
|
+
const dv = new DataView(this.exports.memory.buffer);
|
|
210
|
+
dv.setUint32(base + 0, RING_MAGIC, true);
|
|
211
|
+
dv.setUint16(base + 4, RING_VERSION, true);
|
|
212
|
+
dv.setUint16(base + 6, 0, true); // flags
|
|
213
|
+
dv.setUint32(base + 8, cap, true); // capacity
|
|
214
|
+
dv.setUint32(base + RC_WRITE, 0, true);
|
|
215
|
+
dv.setUint32(base + RC_READ, 0, true);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** Write the `@connect` info block into the guest's info region (no-op if the box carries none).
|
|
219
|
+
* Authority + path are bounded into `[SI_BODY, cap)` - truncated to fit, never an OOB write. */
|
|
220
|
+
private writeConnectInfo(streamId: bigint, authority: string, path: string): void {
|
|
221
|
+
const info = this.streamInfo;
|
|
222
|
+
if (!info) return;
|
|
223
|
+
const base = info.offset;
|
|
224
|
+
const body = Math.max(0, info.cap - SI_BODY);
|
|
225
|
+
const authBytes = Buffer.from(authority, 'utf8');
|
|
226
|
+
const authLen = Math.min(authBytes.length, 0xffff, body);
|
|
227
|
+
const pathBytes = Buffer.from(path, 'utf8');
|
|
228
|
+
const pathLen = Math.min(pathBytes.length, 0xffff, body - authLen);
|
|
229
|
+
const dv = new DataView(this.exports.memory.buffer);
|
|
230
|
+
dv.setBigUint64(base + 0, streamId, true);
|
|
231
|
+
dv.setUint8(base + SI_TRANSPORT, SI_TRANSPORT_WEBTRANSPORT);
|
|
232
|
+
dv.setUint8(base + SI_TRANSPORT + 1, 0); // reserved
|
|
233
|
+
dv.setUint16(base + SI_AUTH_LEN, authLen, true);
|
|
234
|
+
dv.setUint16(base + SI_PATH_LEN, pathLen, true);
|
|
235
|
+
dv.setUint16(base + SI_RESERVED2, 0, true); // reserved
|
|
236
|
+
const memU8 = new Uint8Array(this.exports.memory.buffer);
|
|
237
|
+
if (authLen > 0) memU8.set(authBytes.subarray(0, authLen), base + SI_BODY);
|
|
238
|
+
if (pathLen > 0) memU8.set(pathBytes.subarray(0, pathLen), base + SI_BODY + authLen);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Zero the egress ring cursors, discarding any staged frames. Safe between dispatches (the guest,
|
|
242
|
+
* the sole egress producer, is idle). */
|
|
243
|
+
private resetEgressRing(): void {
|
|
244
|
+
const rings = this.rings;
|
|
245
|
+
if (!rings) return;
|
|
246
|
+
const dv = new DataView(this.exports.memory.buffer);
|
|
247
|
+
dv.setUint32(rings.egressOff + RC_WRITE, 0, true);
|
|
248
|
+
dv.setUint32(rings.egressOff + RC_READ, 0, true);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Host (producer) writes ONE inbound RingFrame into the ingress ring with the drained-reset
|
|
252
|
+
* (host owns write_cursor; reset read_cursor only when the guest has drained). */
|
|
253
|
+
private ingressWrite(inbound: Buffer): void {
|
|
254
|
+
const rings = this.rings;
|
|
255
|
+
if (!rings) throw new Error('ingressWrite: no ring runtime');
|
|
256
|
+
const { ingressOff: base, ingressCap: cap } = rings;
|
|
257
|
+
const dv = new DataView(this.exports.memory.buffer);
|
|
258
|
+
const n = inbound.length;
|
|
259
|
+
const frameLen = RING_FRAME_HEADER + n;
|
|
260
|
+
if (frameLen > cap) {
|
|
261
|
+
throw new Error(`inbound frame (${String(frameLen)} B) exceeds ingress capacity`);
|
|
262
|
+
}
|
|
263
|
+
const w0 = dv.getUint32(base + RC_WRITE, true);
|
|
264
|
+
const r0 = dv.getUint32(base + RC_READ, true);
|
|
265
|
+
let w: number;
|
|
266
|
+
if (r0 === w0) {
|
|
267
|
+
dv.setUint32(base + RC_WRITE, 0, true);
|
|
268
|
+
dv.setUint32(base + RC_READ, 0, true);
|
|
269
|
+
w = 0;
|
|
270
|
+
} else {
|
|
271
|
+
w = w0;
|
|
272
|
+
}
|
|
273
|
+
if (w + frameLen > cap) throw new Error('ingress frame would not fit (v1 is no-wrap)');
|
|
274
|
+
const f = base + RING_CTRL_BYTES + w;
|
|
275
|
+
dv.setUint8(f + 0, RING_VERSION);
|
|
276
|
+
dv.setUint8(f + 1, FRAME_TYPE_DATA_RELIABLE);
|
|
277
|
+
dv.setUint16(f + 2, 0, true); // flags
|
|
278
|
+
dv.setUint32(f + 4, n, true); // length
|
|
279
|
+
dv.setUint32(f + 8, 0, true); // msg_seq
|
|
280
|
+
if (n > 0) new Uint8Array(this.exports.memory.buffer, f + RING_FRAME_HEADER, n).set(inbound);
|
|
281
|
+
dv.setUint32(base + RC_WRITE, w + frameLen, true);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Host (consumer) drains every egress RingFrame the guest staged, copying each payload out and
|
|
285
|
+
* advancing the host-owned read_cursor. The guest does the drained-reset on its next write. */
|
|
286
|
+
private egressDrain(): Buffer[] {
|
|
287
|
+
const rings = this.rings;
|
|
288
|
+
if (!rings) return [];
|
|
289
|
+
const { egressOff: base, egressCap: cap } = rings;
|
|
290
|
+
const dv = new DataView(this.exports.memory.buffer);
|
|
291
|
+
const w = dv.getUint32(base + RC_WRITE, true);
|
|
292
|
+
let r = dv.getUint32(base + RC_READ, true);
|
|
293
|
+
const frames: Buffer[] = [];
|
|
294
|
+
while (r < w) {
|
|
295
|
+
const f = base + RING_CTRL_BYTES + r;
|
|
296
|
+
if (r + RING_FRAME_HEADER > cap) break; // header must fit the frame region
|
|
297
|
+
const len = dv.getUint32(f + 4, true);
|
|
298
|
+
if (len > MAX_STREAM_FRAME_LEN) break; // over-cap frame (contained, not over-read)
|
|
299
|
+
const span = RING_FRAME_HEADER + len;
|
|
300
|
+
if (r + span > cap) break; // payload must fit
|
|
301
|
+
const payloadOff = f + RING_FRAME_HEADER;
|
|
302
|
+
frames.push(Buffer.from(new Uint8Array(this.exports.memory.buffer, payloadOff, len)));
|
|
303
|
+
r += span;
|
|
304
|
+
}
|
|
305
|
+
dv.setUint32(base + RC_READ, r, true);
|
|
306
|
+
return frames;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
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 }
|
|
36
|
+
| { readonly kind: 'rejected'; readonly code: number };
|
|
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, or a `0x02xx` reject code. Fails closed (no connection registered, box dropped) on a
|
|
73
|
+
* missing/unreadable artifact (`0x0208`), a `@connect` reject (the guest's code), or a load/connect
|
|
74
|
+
* trap (`0x0200`). Throws only on a duplicate `connId`.
|
|
75
|
+
*/
|
|
76
|
+
acceptUpgrade(connId: string, authority: string, path: string): StreamUpgradeOutcome {
|
|
77
|
+
if (this.conns.has(connId)) throw new Error(`stream connection '${connId}' is already open`);
|
|
78
|
+
this.refresh();
|
|
79
|
+
if (!this.bytes) return { kind: 'rejected', code: STREAM_REJECTED };
|
|
80
|
+
let box: DevStreamBox;
|
|
81
|
+
try {
|
|
82
|
+
box = DevStreamBox.load(this.bytes);
|
|
83
|
+
} catch {
|
|
84
|
+
return { kind: 'rejected', code: STREAM_REJECTED };
|
|
85
|
+
}
|
|
86
|
+
const streamId = this.allocStreamId();
|
|
87
|
+
let outcome;
|
|
88
|
+
try {
|
|
89
|
+
outcome = box.onConnect(streamId, authority, path);
|
|
90
|
+
} catch {
|
|
91
|
+
return { kind: 'rejected', code: STREAM_HOOK_TRAPPED }; // @connect trapped
|
|
92
|
+
}
|
|
93
|
+
if (outcome.kind === 'reject') return { kind: 'rejected', code: outcome.code };
|
|
94
|
+
this.conns.set(connId, { box, streamId });
|
|
95
|
+
return { kind: 'accepted', streamId };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Drive an inbound frame into the connection's `@message` hook, mirroring
|
|
100
|
+
* `StreamWorker::dispatch_datagram`: `reply` frames (to feed back out), a guest-`reject` `close`, a
|
|
101
|
+
* TRAP-induced `close` (discarding the poisoned box), or `noConnection` for an unknown id.
|
|
102
|
+
*/
|
|
103
|
+
dispatch(connId: string, inbound: Buffer): StreamDispatchResult {
|
|
104
|
+
const conn = this.conns.get(connId);
|
|
105
|
+
if (!conn) return { kind: 'noConnection' };
|
|
106
|
+
try {
|
|
107
|
+
const out = conn.box.onMessage(conn.streamId, inbound);
|
|
108
|
+
if (out.kind === 'reply') return { kind: 'reply', frames: out.frames };
|
|
109
|
+
return { kind: 'close', code: out.code };
|
|
110
|
+
} catch {
|
|
111
|
+
// The @message hook TRAPPED (a real wasm trap; dev has no gas middleware) -> discard the
|
|
112
|
+
// poisoned box + close, mirroring the edge's STREAM_HOOK_TRAPPED (05 7.4).
|
|
113
|
+
this.conns.delete(connId);
|
|
114
|
+
return { kind: 'close', code: STREAM_HOOK_TRAPPED };
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Graceful close: fire `@close` and drop the box. No-op if the connection is gone. */
|
|
119
|
+
close(connId: string): void {
|
|
120
|
+
const conn = this.conns.get(connId);
|
|
121
|
+
if (!conn) return;
|
|
122
|
+
try {
|
|
123
|
+
conn.box.onClose(conn.streamId);
|
|
124
|
+
} catch {
|
|
125
|
+
// a trapping @close still tears the connection down
|
|
126
|
+
}
|
|
127
|
+
this.conns.delete(connId);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Abrupt teardown: fire `@disconnect` and drop the box. No-op if the connection is gone. */
|
|
131
|
+
disconnect(connId: string): void {
|
|
132
|
+
const conn = this.conns.get(connId);
|
|
133
|
+
if (!conn) return;
|
|
134
|
+
try {
|
|
135
|
+
conn.box.onDisconnect(conn.streamId);
|
|
136
|
+
} catch {
|
|
137
|
+
// a trapping @disconnect still tears the connection down
|
|
138
|
+
}
|
|
139
|
+
this.conns.delete(connId);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** (Re)read the artifact bytes when its mtime changes (mirrors `DaemonHost.refresh`). Clears the
|
|
143
|
+
* bytes when the file is missing; leaves live connections' resident boxes untouched. */
|
|
144
|
+
private refresh(): void {
|
|
145
|
+
let mtimeMs: number;
|
|
146
|
+
try {
|
|
147
|
+
mtimeMs = fs.statSync(this.streamWasmPath).mtimeMs;
|
|
148
|
+
} catch {
|
|
149
|
+
this.bytes = null;
|
|
150
|
+
this.loadedMtimeMs = -1;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (mtimeMs === this.loadedMtimeMs && this.bytes) return;
|
|
154
|
+
this.bytes = fs.readFileSync(this.streamWasmPath);
|
|
155
|
+
this.loadedMtimeMs = mtimeMs;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
private allocStreamId(): bigint {
|
|
159
|
+
const id = this.nextStreamId;
|
|
160
|
+
this.nextStreamId = id + 1n;
|
|
161
|
+
return id;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
}
|