@threadedstack/wmd-web 0.1.39 → 0.1.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +236 -1
  2. package/RELEASE.json +84 -49
  3. package/guest/arm64/Image +1 -1
  4. package/guest/arm64/Image-4k +1 -1
  5. package/guest/arm64/initramfs.cpio.gz +0 -0
  6. package/package.json +3 -3
  7. package/src/boot.js +231 -4
  8. package/src/edge/auto.js +77 -0
  9. package/src/edge/base.js +10 -0
  10. package/src/edge/bridge.js +76 -7
  11. package/src/edge/ctl.js +21 -4
  12. package/src/edge/error-kind.js +6 -0
  13. package/src/edge/frame-code.gen.js +6 -0
  14. package/src/edge/frame.js +58 -15
  15. package/src/edge/msg.js +3 -0
  16. package/src/edge/own/adapter.js +208 -19
  17. package/src/edge/own/exit-now.js +62 -0
  18. package/src/edge/own/fs-own-route.js +40 -3
  19. package/src/edge/own/fs-stats.js +11 -0
  20. package/src/edge/own/hub.js +125 -14
  21. package/src/edge/own/loader.js +3 -2
  22. package/src/edge/own/marks.js +158 -0
  23. package/src/edge/own/net-host.js +873 -0
  24. package/src/edge/own/node-globals.js +142 -0
  25. package/src/edge/own/pool.js +30 -1
  26. package/src/edge/own/service-receiver.js +7 -1
  27. package/src/edge/own/shim-post.js +38 -0
  28. package/src/edge/own/worker.js +34 -1
  29. package/src/edge/page.js +448 -25
  30. package/src/edge/pins.gen.js +17 -0
  31. package/src/edge/pins.js +21 -0
  32. package/src/edge/plan.js +45 -9
  33. package/src/edge/python-module.js +18 -0
  34. package/src/edge/python-pins.js +64 -0
  35. package/src/edge/python-plan.js +280 -0
  36. package/src/edge/python-source.js +34 -0
  37. package/src/edge/runtime.js +13 -2
  38. package/src/edge/sbx/cap.js +36 -0
  39. package/src/edge/sbx/common.js +186 -0
  40. package/src/edge/sbx/doc.js +121 -0
  41. package/src/edge/sbx/files.js +112 -0
  42. package/src/edge/sbx/frame.js +126 -0
  43. package/src/edge/sbx/gate.js +115 -0
  44. package/src/edge/sbx/sha256.js +68 -0
  45. package/src/edge/sbx/worker-entry.js +36 -0
  46. package/src/edge/sems.js +113 -0
  47. package/src/edge/service-host.js +57 -5
  48. package/src/edge/service-port.js +79 -5
  49. package/src/edge/setup.js +275 -0
  50. package/src/edge/shim.js +3 -1
  51. package/src/edge/watch.js +101 -0
  52. package/src/edge/worker.js +31 -2
  53. package/src/gowasm/setup.js +9 -6
  54. package/src/host/chan-service.js +8 -5
  55. package/src/host/chan-services.js +29 -4
  56. package/src/host/edge-host.js +58 -3
  57. package/wmdpack.wasm +0 -0
@@ -58,6 +58,9 @@ export function helloRefusal(h) {
58
58
  const tz = (h.env || []).findLast(kv => kv.startsWith('TZ='))
59
59
  if (tz !== undefined && !isUtcZone(tz.slice(3))) return `${tz}: the frame's clock zone is UTC`
60
60
  const base = String((h.argv || [])[0] || '').split('/').pop()
61
+ // X25 gate 2: a python start is the owner's to decide (src/edge/service-host.js, src/edge/python-plan.js): the stub stands over python only when the page's
62
+ // plan took the image's python, and the owner answers the cases of the command line, the tree and the environment with a fallback and its reason
63
+ if (/^python/.test(base)) return ''
61
64
  if (!/^node/.test(base)) return `${base || 'no program'} is not node`
62
65
  return ''
63
66
  }
@@ -79,10 +82,17 @@ export class EdgeHost {
79
82
  this.start = start
80
83
  this.startTimeoutMs = startTimeoutMs
81
84
  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)
85
+ 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), trips: [] } // null prototype: the names are the frame's (check5 F4)
86
+ this.txWait = [] // [trip, field] stamps that wait for the flush that puts the bytes just queued into the guest's ring (flushed())
83
87
  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
88
  }
85
89
 
90
+ /** The channel service put what was queued for the guest into its ring at `t` (page clock): the stamps that waited for it get it. */
91
+ flushed(t = at()) {
92
+ for (const [trip, k] of this.txWait) if (trip[k] === undefined) trip[k] = t
93
+ this.txWait.length = 0
94
+ }
95
+
86
96
  /** How many stubs are connected. A stub that waits for a reply, or whose program runs here, cannot be carried over a checkpoint. */
87
97
  openStubs() {
88
98
  return this.stubs.size
@@ -108,6 +118,7 @@ class OuterConn {
108
118
  this.buf = new Uint8Array(0)
109
119
  this.nested = null
110
120
  st.onData = u8 => {
121
+ if (this.first === undefined) host.lastOf = this.first = at()
111
122
  st.consume(u8.length)
112
123
  if (this.nested) return this.nested.feed(u8)
113
124
  const b = new Uint8Array(this.buf.length + u8.length)
@@ -202,6 +213,10 @@ export function execStream(session) {
202
213
  }
203
214
  }
204
215
 
216
+ // X25 gate 2e: the page's clock every Worker shares (timeOrigin + now); the stamps of one stub's start are kept in stats.trips (the last 64)
217
+ const at = () => performance.timeOrigin + performance.now()
218
+ const TRIPS_KEPT = 64
219
+
205
220
  class EdgeConn {
206
221
  constructor(host, st, hold, session = null) {
207
222
  this.host = host
@@ -210,6 +225,8 @@ class EdgeConn {
210
225
  this.hold = hold
211
226
  this.buf = new Uint8Array(0)
212
227
  this.phase = 'hello' // hello -> wait -> run
228
+ this.trip = null // {of, hi, rp, ex}: the stamps of this start on the page's clock
229
+ this.of = host.lastOf // the first bytes of the outer stream this stub came by
213
230
  this.runner = null
214
231
  this.ended = false
215
232
  st.onData = u8 => {
@@ -241,6 +258,9 @@ class EdgeConn {
241
258
  const p = this.buf.slice(5, 5 + n)
242
259
  this.buf = this.buf.subarray(5 + n)
243
260
  if (this.phase === 'hello') {
261
+ // X5 net: a stream the stub opened for a connection its listener took is not a hello: its header says `accepted` and what follows is the
262
+ // connection's bytes. The run's network host (set by its start, ctx.setAccepted) takes it; with none, or when it refuses, the stream is reset.
263
+ if (type === FRAME_HDR && this.session && p.length < 512 && this.#accepted(p)) return
244
264
  if (type !== FRAME_HDR) {
245
265
  this.host.stats.badFrames++
246
266
  return this.st.abort('no hello')
@@ -253,7 +273,35 @@ class EdgeConn {
253
273
  }
254
274
  }
255
275
 
276
+ /** True when `p` is an `accepted` header and the stream was handled (taken by the run's network host or reset). */
277
+ #accepted(p) {
278
+ let h
279
+ try {
280
+ h = JSON.parse(dec.decode(p))
281
+ } catch {
282
+ return false
283
+ }
284
+ if (!h || h.kind !== 'accepted') return false
285
+ const st = this.st
286
+ const rest = this.buf
287
+ this.buf = new Uint8Array(0)
288
+ this.ended = true // this connection object does nothing more with the stream
289
+ this.host.stats.accepted = (this.host.stats.accepted || 0) + 1
290
+ let ok = false
291
+ try {
292
+ ok = typeof this.session.accepted === 'function' && this.session.accepted(st, h, rest) !== false
293
+ } catch {
294
+ ok = false
295
+ }
296
+ if (!ok && !st.closed) st.abort('no listener for this stream')
297
+ return true
298
+ }
299
+
256
300
  #reply(r) {
301
+ if (this.trip) {
302
+ this.trip.rp = at()
303
+ this.host.txWait.push([this.trip, 'rw'])
304
+ }
257
305
  if (!this.ended) this.st.write(frame(FRAME_REPLY, r))
258
306
  }
259
307
 
@@ -266,6 +314,9 @@ class EdgeConn {
266
314
  }
267
315
 
268
316
  async #hello(p) {
317
+ const trips = this.host.stats.trips
318
+ trips.push((this.trip = { of: this.of ?? at(), hi: at() }))
319
+ if (trips.length > TRIPS_KEPT) trips.splice(0, trips.length - TRIPS_KEPT)
269
320
  let h
270
321
  try {
271
322
  h = JSON.parse(dec.decode(p))
@@ -281,7 +332,7 @@ class EdgeConn {
281
332
  // an owner that never answers (a page that lost its frame, a machine that ended) must not hold the stub for ever
282
333
  let timer
283
334
  let late = false
284
- const started = Promise.resolve(this.host.start(h, { openExec: () => execStream(this.session) }))
335
+ const started = Promise.resolve(this.host.start(h, { openExec: () => execStream(this.session), openStream: () => this.session.open(), setAccepted: fn => (this.session.accepted = fn) }))
285
336
  started.then(x => late && x && x.runner?.gone?.(), () => {}) // a start that comes after the timeout is let go at once
286
337
  r = await Promise.race([
287
338
  started,
@@ -302,7 +353,11 @@ class EdgeConn {
302
353
  r.runner.onExit = e => {
303
354
  if (this.ended) return
304
355
  this.host.stats.exits++
305
- this.st.write(frame(FRAME_EXIT, e.signal ? { code: e.code, signal: e.signal } : { code: e.code }))
356
+ if (this.trip) {
357
+ this.trip.ex = at()
358
+ this.host.txWait.push([this.trip, 'ew'])
359
+ }
360
+ this.st.write(frame(FRAME_EXIT, e.signal ? { code: e.code, signal: e.signal } : e.clean === true ? { code: e.code, clean: true } : { code: e.code }))
306
361
  this.st.closeWrite()
307
362
  }
308
363
  this.#reply({ ok: true })
package/wmdpack.wasm CHANGED
Binary file