@threadedstack/wmd-web 0.1.30 → 0.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -2
- package/RELEASE.json +123 -16
- package/guest/arm64/Image +1 -1
- package/guest/arm64/Image-4k +1 -1
- package/guest/arm64/initramfs.cpio.gz +0 -0
- package/package.json +3 -3
- package/src/boot.js +47 -2
- package/src/edge/bridge.js +297 -0
- package/src/edge/codes.js +10 -0
- package/src/edge/ctl.js +199 -0
- package/src/edge/error-kind.js +4 -0
- package/src/edge/frame.html +4 -0
- package/src/edge/frame.js +48 -0
- package/src/edge/fs-route.js +301 -0
- package/src/edge/hist.js +46 -0
- package/src/edge/msg.js +106 -0
- package/src/edge/node-args.js +194 -0
- package/src/edge/node-guest-opts.js +108 -0
- package/src/edge/node-opts.js +82 -0
- package/src/edge/own/adapter.js +271 -0
- package/src/edge/own/capi.js +200 -0
- package/src/edge/own/child-host.js +196 -0
- package/src/edge/own/fs-bridge.js +71 -0
- package/src/edge/own/fs-own-route.js +356 -0
- package/src/edge/own/guest-spawner.js +235 -0
- package/src/edge/own/hub.js +430 -0
- package/src/edge/own/loader.js +81 -0
- package/src/edge/own/memcap.js +95 -0
- package/src/edge/own/payload-size.js +15 -0
- package/src/edge/own/pool.js +160 -0
- package/src/edge/own/procfs.js +84 -0
- package/src/edge/own/serdes-codec.js +317 -0
- package/src/edge/own/service-receiver.js +98 -0
- package/src/edge/own/shim-post.js +185 -0
- package/src/edge/own/stack-fix.js +36 -0
- package/src/edge/own/stdio-pipe.js +80 -0
- package/src/edge/own/v8-serdes.js +970 -0
- package/src/edge/own/wasm-counters.js +144 -0
- package/src/edge/own/worker.js +97 -0
- package/src/edge/page.js +162 -0
- package/src/edge/plan.js +65 -0
- package/src/edge/resolve.js +44 -0
- package/src/edge/runtime.js +94 -0
- package/src/edge/sdk/adapter.js +151 -0
- package/src/edge/sdk/forward.js +80 -0
- package/src/edge/sdk/napi-stub.js +50 -0
- package/src/edge/sdk/relay.js +17 -0
- package/src/edge/service-host.js +382 -0
- package/src/edge/service-port.js +245 -0
- package/src/edge/shim.js +161 -0
- package/src/edge/site.js +106 -0
- package/src/edge/spawn-guard.js +80 -0
- package/src/edge/tz.js +103 -0
- package/src/edge/worker.js +121 -0
- package/src/edge/workers.js +27 -0
- package/src/host/chan-service.js +75 -8
- package/src/host/chan-services.js +77 -3
- package/src/host/ctl.js +26 -5
- package/src/host/edge-go.js +7 -3
- package/src/host/edge-host.js +327 -0
- package/src/host/yamux.js +21 -8
- package/wmdpack.wasm +0 -0
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
// X5 part B (page switch edge=1 only): the host's end of the guest's edge stub (guest/container2wasm/cmd/edgestub). The stub
|
|
2
|
+
// stands in for the container's node; it hands the process to the host over a yamux stream the guest init opens with
|
|
3
|
+
// `{"op":"edge"}\n` and then copies to and from the stub's socket. The stub runs a yamux session of its own on that socket
|
|
4
|
+
// (this file is its server end: stream 1 of that session is the run), and the run's stream carries proto.go's frames:
|
|
5
|
+
// [type u8][length u32 BE][payload]. (The first stage 2 run treated the outer stream as the frames: 1 bad frame, the stub
|
|
6
|
+
// fell back, `x5/gate2b/s2-chromium-2.json`.)
|
|
7
|
+
//
|
|
8
|
+
// stub -> host: frameHdr (0x10) JSON hello {argv, env, cwd, uid, gid, tty?, init?, hold?}, then frameStdin (0, bytes),
|
|
9
|
+
// frameStdinEOF (4), frameSignal (5, one byte), (frameStdinStart (6) only with stdin_demand)
|
|
10
|
+
// host -> stub: frameReply (0x11) JSON {ok, fallback?, error?, stdin_demand?}, then frameStdout (1), frameStderr (2),
|
|
11
|
+
// frameExit (3) JSON {code, signal?}
|
|
12
|
+
//
|
|
13
|
+
// This is what the desktop's edge.Serve does (internal/edge/edge.go), cut to what part B runs: the hello and the stdio
|
|
14
|
+
// frames. It is transport-free and has no idea where node runs: `start(hello)` (the owner's) decides whether the program
|
|
15
|
+
// runs here and returns a runner. Streams that do not say `edge`, a hold (the early path), a terminal, a user that is not
|
|
16
|
+
// root, WMD_EDGE=0 and a program that is not node are answered {ok:false, fallback:true}: the stub then runs the image's
|
|
17
|
+
// own node, which is what the container does without the switch. Sockets (kinds connect/listen/resolve/bind_udp) the host
|
|
18
|
+
// would open towards the stub are not part of this path.
|
|
19
|
+
//
|
|
20
|
+
// start(hello, {openExec}) -> Promise<{fallback: string} | {runner}>; openExec() opens a raw stream to the stub's exec service (a child of node's); runner: {stdin(bytes), stdinEnd(), signal(n), gone()} and the
|
|
21
|
+
// host sets runner.onOut = (kind, bytes) and runner.onExit = ({code, signal}) before the first byte can come.
|
|
22
|
+
|
|
23
|
+
import { YamuxClient } from './yamux.js'
|
|
24
|
+
import { isUtcZone } from '../edge/tz.js'
|
|
25
|
+
|
|
26
|
+
export const FRAME_STDIN = 0
|
|
27
|
+
export const FRAME_STDOUT = 1
|
|
28
|
+
export const FRAME_STDERR = 2
|
|
29
|
+
export const FRAME_EXIT = 3
|
|
30
|
+
export const FRAME_STDIN_EOF = 4
|
|
31
|
+
export const FRAME_SIGNAL = 5
|
|
32
|
+
export const FRAME_STDIN_START = 6
|
|
33
|
+
export const FRAME_HDR = 0x10
|
|
34
|
+
export const FRAME_REPLY = 0x11
|
|
35
|
+
export const MAX_FRAME = 4 << 20
|
|
36
|
+
/** The longest the host waits for its owner to decide on a hello (the page's own limits are shorter; the stub's is longer). */
|
|
37
|
+
export const START_TIMEOUT_MS = 150_000
|
|
38
|
+
|
|
39
|
+
const enc = new TextEncoder()
|
|
40
|
+
const dec = new TextDecoder()
|
|
41
|
+
|
|
42
|
+
export function frame(type, payload) {
|
|
43
|
+
const p = payload instanceof Uint8Array ? payload : enc.encode(JSON.stringify(payload))
|
|
44
|
+
const out = new Uint8Array(5 + p.length)
|
|
45
|
+
out[0] = type
|
|
46
|
+
new DataView(out.buffer).setUint32(1, p.length)
|
|
47
|
+
out.set(p, 5)
|
|
48
|
+
return out
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Why the host does not run this hello, or '' when it may. */
|
|
52
|
+
export function helloRefusal(h) {
|
|
53
|
+
if (h.hold) return 'the early path (hold) is not part of the browser'
|
|
54
|
+
if (h.tty) return 'a terminal'
|
|
55
|
+
if (h.uid !== 0) return 'not root'
|
|
56
|
+
if ((h.env || []).includes('WMD_EDGE=0')) return 'WMD_EDGE=0'
|
|
57
|
+
// the frame's clock is UTC (src/edge/tz.js); a container in another zone would see another time
|
|
58
|
+
const tz = (h.env || []).findLast(kv => kv.startsWith('TZ='))
|
|
59
|
+
if (tz !== undefined && !isUtcZone(tz.slice(3))) return `${tz}: the frame's clock zone is UTC`
|
|
60
|
+
const base = String((h.argv || [])[0] || '').split('/').pop()
|
|
61
|
+
if (!/^node/.test(base)) return `${base || 'no program'} is not node`
|
|
62
|
+
return ''
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The reason a checkpoint must not be taken now ('' when it may): a stub is connected, and its end of the conversation would be lost. */
|
|
66
|
+
export function checkpointRefusal(host) {
|
|
67
|
+
const n = host ? host.openStubs() : 0
|
|
68
|
+
return n ? `${n} node program${n > 1 ? 's are' : ' is'} being handed to the page (page switch edge=1): its files and processes are not in a checkpoint; take it when no such program runs` : ''
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export class EdgeHost {
|
|
72
|
+
/**
|
|
73
|
+
* @param {object} o
|
|
74
|
+
* @param {(hello: object) => Promise<{fallback: string} | {runner: object}>} o.start the owner's decision and start
|
|
75
|
+
* @param {(why: string) => void} [o.log]
|
|
76
|
+
* @param {number} [o.startTimeoutMs] an owner that has not decided by then gets a fallback, and a program it starts later is let go
|
|
77
|
+
*/
|
|
78
|
+
constructor({ start, log = () => {}, startTimeoutMs = START_TIMEOUT_MS }) {
|
|
79
|
+
this.start = start
|
|
80
|
+
this.startTimeoutMs = startTimeoutMs
|
|
81
|
+
this.log = log
|
|
82
|
+
this.stats = { streams: 0, notEdge: 0, fallbacks: 0, runs: 0, exits: 0, badFrames: 0, outBytes: { stdout: 0, stderr: 0 }, outEnded: 0, fallbackWhy: Object.create(null) } // null prototype: the names are the frame's (check5 F4)
|
|
83
|
+
this.stubs = new Set() // the stubs connected now: from the guest's stream until the stub's end (hello, reply and the whole run included)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** How many stubs are connected. A stub that waits for a reply, or whose program runs here, cannot be carried over a checkpoint. */
|
|
87
|
+
openStubs() {
|
|
88
|
+
return this.stubs.size
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The hook for YamuxClient's `accept`: a stream the guest init opened for one stub. */
|
|
92
|
+
accept(stream) {
|
|
93
|
+
this.stats.streams++
|
|
94
|
+
new OuterConn(this, stream)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// The guest init opens one yamux stream per stub, writes `{"op":"edge"}\n` and then copies bytes both ways between that
|
|
99
|
+
// stream and the stub's unix socket. The stub speaks yamux itself on that socket (it is the client, hashicorp's session; its
|
|
100
|
+
// session stream carries proto.go's frames), so what follows the line is a yamux session of its own. This is its server end:
|
|
101
|
+
// the streams the stub opens are runs (EdgeConn). The host opens none (no exec, socket or resolve kinds in part B).
|
|
102
|
+
class OuterConn {
|
|
103
|
+
constructor(host, st) {
|
|
104
|
+
this.host = host
|
|
105
|
+
this.st = st
|
|
106
|
+
host.stubs.add(this)
|
|
107
|
+
const ended = () => host.stubs.delete(this)
|
|
108
|
+
this.buf = new Uint8Array(0)
|
|
109
|
+
this.nested = null
|
|
110
|
+
st.onData = u8 => {
|
|
111
|
+
st.consume(u8.length)
|
|
112
|
+
if (this.nested) return this.nested.feed(u8)
|
|
113
|
+
const b = new Uint8Array(this.buf.length + u8.length)
|
|
114
|
+
b.set(this.buf)
|
|
115
|
+
b.set(u8, this.buf.length)
|
|
116
|
+
this.buf = b
|
|
117
|
+
this.#line()
|
|
118
|
+
}
|
|
119
|
+
st.onFin = () => (ended(), this.nested?.close('the stub ended', 0, false))
|
|
120
|
+
st.onClose = () => (ended(), this.nested?.close('the stub ended', 0, false))
|
|
121
|
+
this.ended = ended
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
#line() {
|
|
125
|
+
const i = this.buf.indexOf(10)
|
|
126
|
+
if (i < 0) {
|
|
127
|
+
if (this.buf.length > 4096) {
|
|
128
|
+
this.host.stats.notEdge++
|
|
129
|
+
this.ended()
|
|
130
|
+
this.st.abort('no request line')
|
|
131
|
+
}
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
let req
|
|
135
|
+
try {
|
|
136
|
+
req = JSON.parse(dec.decode(this.buf.subarray(0, i)))
|
|
137
|
+
} catch {
|
|
138
|
+
req = null
|
|
139
|
+
}
|
|
140
|
+
const rest = this.buf.subarray(i + 1)
|
|
141
|
+
this.buf = new Uint8Array(0)
|
|
142
|
+
if (!req || req.op !== 'edge') {
|
|
143
|
+
this.host.stats.notEdge++
|
|
144
|
+
this.ended()
|
|
145
|
+
return this.st.abort('not an edge stream')
|
|
146
|
+
}
|
|
147
|
+
const hold = !!req.hold
|
|
148
|
+
this.nested = stubSession({ send: u8 => this.st.write(u8), accept: s => new EdgeConn(this.host, s, hold, this.nested) })
|
|
149
|
+
if (rest.length) this.nested.feed(rest)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The host's end of the stub's own yamux session (the stub is a yamux client: its streams have odd ids, 1 is its hello). The streams the host
|
|
155
|
+
* opens on it (execStream) take the even ids; with odd ones the first would be stream 1 again and the stub's yamux would end the session
|
|
156
|
+
* ("duplicate stream"), which the program saw as `lost the host: EOF` (fix round 5).
|
|
157
|
+
*/
|
|
158
|
+
export function stubSession({ send, accept }) {
|
|
159
|
+
return new YamuxClient({ send, keepAliveMs: 0, accept, firstStreamId: 2 })
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* A raw byte stream to the stub's exec service: the host opens a stream on the stub's own yamux session (the stub accepts the streams the host
|
|
164
|
+
* opens and answers an `exec` header, guest/container2wasm/cmd/edgestub/main.go serve()). It is what the own runtime's child spawner
|
|
165
|
+
* (src/edge/own/guest-spawner.js) is given. read() gives the next bytes or null at the end; write(bytes) queues; close() ends the stream.
|
|
166
|
+
*/
|
|
167
|
+
export function execStream(session) {
|
|
168
|
+
const st = session.open()
|
|
169
|
+
const q = []
|
|
170
|
+
let wake = null
|
|
171
|
+
let ended = false
|
|
172
|
+
const poke = () => {
|
|
173
|
+
const w = wake
|
|
174
|
+
wake = null
|
|
175
|
+
w?.()
|
|
176
|
+
}
|
|
177
|
+
st.onData = u8 => {
|
|
178
|
+
q.push(u8.slice())
|
|
179
|
+
st.consume(u8.length)
|
|
180
|
+
poke()
|
|
181
|
+
}
|
|
182
|
+
st.onFin = () => {
|
|
183
|
+
ended = true
|
|
184
|
+
poke()
|
|
185
|
+
}
|
|
186
|
+
st.onClose = () => {
|
|
187
|
+
ended = true
|
|
188
|
+
poke()
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
async read() {
|
|
192
|
+
for (;;) {
|
|
193
|
+
if (q.length) return q.shift()
|
|
194
|
+
if (ended) return null
|
|
195
|
+
await new Promise(r => (wake = r))
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
write: bytes => st.write(bytes),
|
|
199
|
+
close: () => {
|
|
200
|
+
if (!st.closed) st.abort('the page closed the exec stream')
|
|
201
|
+
},
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
class EdgeConn {
|
|
206
|
+
constructor(host, st, hold, session = null) {
|
|
207
|
+
this.host = host
|
|
208
|
+
this.st = st
|
|
209
|
+
this.session = session // the stub's yamux session: the host opens exec streams on it (execStream)
|
|
210
|
+
this.hold = hold
|
|
211
|
+
this.buf = new Uint8Array(0)
|
|
212
|
+
this.phase = 'hello' // hello -> wait -> run
|
|
213
|
+
this.runner = null
|
|
214
|
+
this.ended = false
|
|
215
|
+
st.onData = u8 => {
|
|
216
|
+
this.#append(u8)
|
|
217
|
+
st.consume(u8.length)
|
|
218
|
+
this.#drain()
|
|
219
|
+
}
|
|
220
|
+
st.onFin = () => this.#theyEnded()
|
|
221
|
+
st.onClose = () => this.#theyEnded()
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
#append(u8) {
|
|
225
|
+
const b = new Uint8Array(this.buf.length + u8.length)
|
|
226
|
+
b.set(this.buf)
|
|
227
|
+
b.set(u8, this.buf.length)
|
|
228
|
+
this.buf = b
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
#drain() {
|
|
232
|
+
for (;;) {
|
|
233
|
+
if (this.buf.length < 5) return
|
|
234
|
+
const type = this.buf[0]
|
|
235
|
+
const n = new DataView(this.buf.buffer, this.buf.byteOffset).getUint32(1)
|
|
236
|
+
if (n > MAX_FRAME) {
|
|
237
|
+
this.host.stats.badFrames++
|
|
238
|
+
return this.st.abort('frame too large')
|
|
239
|
+
}
|
|
240
|
+
if (this.buf.length < 5 + n) return
|
|
241
|
+
const p = this.buf.slice(5, 5 + n)
|
|
242
|
+
this.buf = this.buf.subarray(5 + n)
|
|
243
|
+
if (this.phase === 'hello') {
|
|
244
|
+
if (type !== FRAME_HDR) {
|
|
245
|
+
this.host.stats.badFrames++
|
|
246
|
+
return this.st.abort('no hello')
|
|
247
|
+
}
|
|
248
|
+
this.phase = 'wait'
|
|
249
|
+
this.#hello(p)
|
|
250
|
+
continue
|
|
251
|
+
}
|
|
252
|
+
this.#frame(type, p)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
#reply(r) {
|
|
257
|
+
if (!this.ended) this.st.write(frame(FRAME_REPLY, r))
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
#fallback(why) {
|
|
261
|
+
const s = this.host.stats
|
|
262
|
+
s.fallbacks++
|
|
263
|
+
s.fallbackWhy[why] = (Object.hasOwn(s.fallbackWhy, why) ? s.fallbackWhy[why] : 0) + 1
|
|
264
|
+
this.#reply({ ok: false, fallback: true, error: why })
|
|
265
|
+
this.st.closeWrite()
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async #hello(p) {
|
|
269
|
+
let h
|
|
270
|
+
try {
|
|
271
|
+
h = JSON.parse(dec.decode(p))
|
|
272
|
+
} catch {
|
|
273
|
+
this.host.stats.badFrames++
|
|
274
|
+
return this.st.abort('bad hello')
|
|
275
|
+
}
|
|
276
|
+
h.hold = h.hold || this.hold
|
|
277
|
+
const why = helloRefusal(h)
|
|
278
|
+
if (why) return this.#fallback(why)
|
|
279
|
+
let r
|
|
280
|
+
try {
|
|
281
|
+
// an owner that never answers (a page that lost its frame, a machine that ended) must not hold the stub for ever
|
|
282
|
+
let timer
|
|
283
|
+
let late = false
|
|
284
|
+
const started = Promise.resolve(this.host.start(h, { openExec: () => execStream(this.session) }))
|
|
285
|
+
started.then(x => late && x && x.runner?.gone?.(), () => {}) // a start that comes after the timeout is let go at once
|
|
286
|
+
r = await Promise.race([
|
|
287
|
+
started,
|
|
288
|
+
new Promise((_, rej) => (timer = setTimeout(() => ((late = true), rej(new Error(`no answer in ${this.host.startTimeoutMs} ms`))), this.host.startTimeoutMs))),
|
|
289
|
+
]).finally(() => clearTimeout(timer))
|
|
290
|
+
} catch (e) {
|
|
291
|
+
return this.#fallback('start failed: ' + ((e && e.message) || e))
|
|
292
|
+
}
|
|
293
|
+
if (this.ended) return r.runner?.gone?.()
|
|
294
|
+
if (r.fallback) return this.#fallback(r.fallback)
|
|
295
|
+
this.runner = r.runner
|
|
296
|
+
this.host.stats.runs++
|
|
297
|
+
r.runner.onOut = (kind, bytes) => {
|
|
298
|
+
if (this.ended) return void (this.host.stats.outEnded += bytes.length)
|
|
299
|
+
this.host.stats.outBytes[kind === 'stderr' ? 'stderr' : 'stdout'] += bytes.length
|
|
300
|
+
for (let at = 0; at < bytes.length; at += MAX_FRAME) this.st.write(frame(kind === 'stderr' ? FRAME_STDERR : FRAME_STDOUT, bytes.subarray(at, at + MAX_FRAME)))
|
|
301
|
+
}
|
|
302
|
+
r.runner.onExit = e => {
|
|
303
|
+
if (this.ended) return
|
|
304
|
+
this.host.stats.exits++
|
|
305
|
+
this.st.write(frame(FRAME_EXIT, e.signal ? { code: e.code, signal: e.signal } : { code: e.code }))
|
|
306
|
+
this.st.closeWrite()
|
|
307
|
+
}
|
|
308
|
+
this.#reply({ ok: true })
|
|
309
|
+
r.runner.begin?.() // output may come now
|
|
310
|
+
this.phase = 'run'
|
|
311
|
+
this.#drain()
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
#frame(type, p) {
|
|
315
|
+
if (!this.runner) return // frames before the run is going are dropped
|
|
316
|
+
if (type === FRAME_STDIN) this.runner.stdin(p)
|
|
317
|
+
else if (type === FRAME_STDIN_EOF) this.runner.stdinEnd()
|
|
318
|
+
else if (type === FRAME_SIGNAL) this.runner.signal(p[0])
|
|
319
|
+
// FRAME_STDIN_START: the reply did not ask for stdin on demand
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
#theyEnded() {
|
|
323
|
+
if (this.ended) return
|
|
324
|
+
this.ended = true
|
|
325
|
+
this.runner?.gone?.()
|
|
326
|
+
}
|
|
327
|
+
}
|
package/src/host/yamux.js
CHANGED
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
// - Data past a stream's receive window, a bad version or type, a duplicate SYN: Go Away
|
|
29
29
|
// (protocol error) and the session is closed.
|
|
30
30
|
// - Streams the guest opens (op "edge") are refused with RST, unless the session has an
|
|
31
|
-
// accept hook (X6: the Wasm compiler hand-off, default off): then the stream is acknowledged
|
|
31
|
+
// accept hook (X6: the Wasm compiler hand-off, default off; X5: the node host of edge=1): then the stream is acknowledged
|
|
32
32
|
// (Window Update with ACK) and handed to the hook, which may refuse it by returning false.
|
|
33
33
|
|
|
34
34
|
export const YAMUX_HEADER = 12
|
|
@@ -170,11 +170,15 @@ export class YamuxClient {
|
|
|
170
170
|
* @param {(u8: Uint8Array) => void} o.send bytes for the guest
|
|
171
171
|
* @param {() => number} [o.now] ms clock (performance.now)
|
|
172
172
|
* @param {number} [o.keepAliveMs] client pings (5000, as the engine); 0: none
|
|
173
|
+
* @param {number} [o.firstStreamId] the id of the first stream this side opens (1: the client's odd ids, the default; 2: the server's even ids,
|
|
174
|
+
* for a session whose peer is a client: the edge stub's yamux session, where the stub opens the odd ones)
|
|
173
175
|
* @param {number} [o.pingTimeoutMs] an unanswered ping counts as a timeout after this
|
|
174
176
|
* @param {(reason: string) => void} [o.onClose]
|
|
175
|
-
* @param {(s: YamuxStream) => boolean|void} [o.accept]
|
|
177
|
+
* @param {(s: YamuxStream) => boolean|void} [o.accept] takes a stream the peer opened (false: refused); without it they are refused
|
|
178
|
+
* with RST. X6: the compiler hand-off. X5 (edge=1 only): the node host; the guest init opens one per node run with
|
|
179
|
+
* `{"op":"edge"}`; the hook sets s.onData and takes the stream, or calls s.abort()
|
|
176
180
|
*/
|
|
177
|
-
constructor({ send, now = () => performance.now(), keepAliveMs = 5000, pingTimeoutMs = 10_000, onClose = null, accept = null } = {}) {
|
|
181
|
+
constructor({ send, now = () => performance.now(), keepAliveMs = 5000, pingTimeoutMs = 10_000, onClose = null, accept = null, firstStreamId = 1 } = {}) {
|
|
178
182
|
this.sendFn = send
|
|
179
183
|
this.now = now
|
|
180
184
|
this.keepAliveMs = keepAliveMs
|
|
@@ -182,7 +186,7 @@ export class YamuxClient {
|
|
|
182
186
|
this.onClose = onClose
|
|
183
187
|
this.accept = accept
|
|
184
188
|
this.streams = new Map()
|
|
185
|
-
this.nextId =
|
|
189
|
+
this.nextId = firstStreamId
|
|
186
190
|
this.hdr = new Uint8Array(YAMUX_HEADER) // the header being read
|
|
187
191
|
this.hlen = 0
|
|
188
192
|
this.body = null // the stream whose data frame body is arriving (null: discard it)
|
|
@@ -194,7 +198,7 @@ export class YamuxClient {
|
|
|
194
198
|
this.lastPingAt = now()
|
|
195
199
|
this.pingId = 0
|
|
196
200
|
this.pings = new Map() // id -> sent at
|
|
197
|
-
this.stats = { pingsSent: 0, pingsAnswered: 0, pingTimeouts: 0, pingRttMs: [], guestPings: 0, refused: 0, accepted: 0, discarded: 0, opened: 0, bytesIn: 0, bytesOut: 0 }
|
|
201
|
+
this.stats = { pingsSent: 0, pingsAnswered: 0, pingTimeouts: 0, pingRttMs: [], guestPings: 0, refused: 0, accepted: 0, discarded: 0, opened: 0, bytesIn: 0, bytesOut: 0, takerErrors: 0 }
|
|
198
202
|
}
|
|
199
203
|
|
|
200
204
|
_send(u8) {
|
|
@@ -304,10 +308,10 @@ export class YamuxClient {
|
|
|
304
308
|
if (type === T_PING) return this.#ping(flags, len)
|
|
305
309
|
if (type === T_GOAWAY) return this.#goAway(len)
|
|
306
310
|
if (flags & F_SYN) {
|
|
307
|
-
// the guest opening a stream (only op "edge" does; not served here): refuse it, and
|
|
308
|
-
// skip any body it sent with the SYN
|
|
309
311
|
if (this.streams.has(id)) return this.#protoError(`duplicate stream ${id}`)
|
|
310
312
|
if (!this.accept) {
|
|
313
|
+
// the guest opening a stream (only op "edge" does; not served here): refuse it, and
|
|
314
|
+
// skip any body it sent with the SYN
|
|
311
315
|
this.stats.refused++
|
|
312
316
|
this._send(yamuxHeader(T_WINDOW, F_RST, id, 0))
|
|
313
317
|
if (type === T_DATA) this.#discard(len)
|
|
@@ -316,10 +320,19 @@ export class YamuxClient {
|
|
|
316
320
|
// X6: take it. The stream is acknowledged, then this frame goes on as any frame of a stream:
|
|
317
321
|
// its window delta or its data (a refusal leaves no stream, so the frame is dropped below)
|
|
318
322
|
const st = new YamuxStream(this, id)
|
|
323
|
+
st.acked = true
|
|
319
324
|
this.streams.set(id, st)
|
|
320
325
|
this.stats.accepted++
|
|
321
326
|
this._send(yamuxHeader(T_WINDOW, F_ACK, id, 0))
|
|
322
|
-
|
|
327
|
+
let taken
|
|
328
|
+
try {
|
|
329
|
+
taken = this.accept(st)
|
|
330
|
+
} catch {
|
|
331
|
+
// a taker that throws is a refusal: the stream gets a reset, the error is counted and the feed goes on (fix 11, F-B)
|
|
332
|
+
this.stats.takerErrors++
|
|
333
|
+
taken = false
|
|
334
|
+
}
|
|
335
|
+
if (taken === false) {
|
|
323
336
|
this.stats.refused++
|
|
324
337
|
st.abort('refused')
|
|
325
338
|
}
|
package/wmdpack.wasm
CHANGED
|
Binary file
|