@threadedstack/wmd-web 0.1.38 → 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.
- package/README.md +239 -2
- package/RELEASE.json +97 -53
- 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 +321 -12
- package/src/edge/auto.js +77 -0
- package/src/edge/base.js +10 -0
- package/src/edge/bridge.js +76 -7
- package/src/edge/ctl.js +21 -4
- package/src/edge/error-kind.js +6 -0
- package/src/edge/frame-code.gen.js +6 -0
- package/src/edge/frame.js +58 -15
- package/src/edge/msg.js +3 -0
- package/src/edge/own/adapter.js +208 -19
- package/src/edge/own/exit-now.js +62 -0
- package/src/edge/own/fs-own-route.js +40 -3
- package/src/edge/own/fs-stats.js +11 -0
- package/src/edge/own/hub.js +125 -14
- package/src/edge/own/loader.js +3 -2
- package/src/edge/own/marks.js +158 -0
- package/src/edge/own/net-host.js +873 -0
- package/src/edge/own/node-globals.js +142 -0
- package/src/edge/own/pool.js +30 -1
- package/src/edge/own/service-receiver.js +7 -1
- package/src/edge/own/shim-post.js +38 -0
- package/src/edge/own/worker.js +34 -1
- package/src/edge/page.js +448 -25
- package/src/edge/pins.gen.js +17 -0
- package/src/edge/pins.js +21 -0
- package/src/edge/plan.js +45 -9
- package/src/edge/python-module.js +18 -0
- package/src/edge/python-pins.js +64 -0
- package/src/edge/python-plan.js +280 -0
- package/src/edge/python-source.js +34 -0
- package/src/edge/runtime.js +13 -2
- package/src/edge/sbx/cap.js +36 -0
- package/src/edge/sbx/common.js +186 -0
- package/src/edge/sbx/doc.js +121 -0
- package/src/edge/sbx/files.js +112 -0
- package/src/edge/sbx/frame.js +126 -0
- package/src/edge/sbx/gate.js +115 -0
- package/src/edge/sbx/sha256.js +68 -0
- package/src/edge/sbx/worker-entry.js +36 -0
- package/src/edge/sems.js +113 -0
- package/src/edge/service-host.js +57 -5
- package/src/edge/service-port.js +79 -5
- package/src/edge/setup.js +275 -0
- package/src/edge/shim.js +3 -1
- package/src/edge/watch.js +101 -0
- package/src/edge/worker.js +31 -2
- package/src/gowasm/boot-setup.js +175 -0
- package/src/gowasm/make-pins-module.mjs +35 -0
- package/src/gowasm/pins-data.js +162 -0
- package/src/gowasm/setup.js +167 -0
- package/src/gowasm/twin-fetch.js +27 -0
- package/src/host/chan-service.js +33 -6
- package/src/host/chan-services.js +29 -4
- package/src/host/ckpt.js +78 -0
- package/src/host/csp-reason.js +64 -0
- package/src/host/ctl.js +30 -8
- package/src/host/edge-go.js +94 -4
- package/src/host/edge-host.js +58 -3
- package/src/host/gowasm-guard.js +11 -1
- package/src/host/gowasm-worker.js +90 -35
- package/src/host/pinned-fetch.js +436 -0
- package/wmdpack.wasm +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@threadedstack/wmd-web",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.41",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"homepage": "https://github.com/threadedstack/wmd-releases",
|
|
6
6
|
"repository": {
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
},
|
|
10
10
|
"description": "Browser host for wmd's guest vm.wasm (Web Workers + shared memory + WASI preview1)",
|
|
11
11
|
"wmd": {
|
|
12
|
-
"tag": "v0.1.
|
|
13
|
-
"commit": "
|
|
12
|
+
"tag": "v0.1.41",
|
|
13
|
+
"commit": "f896db7a78550adb1b3292eb291af93045826254",
|
|
14
14
|
"web": "0.5.1"
|
|
15
15
|
},
|
|
16
16
|
"type": "module",
|
package/src/boot.js
CHANGED
|
@@ -45,9 +45,10 @@ import {
|
|
|
45
45
|
CONSOLE_IN_LIMIT, chanCounters, chanView, consoleInputGate, createChanBlock, markGuestGone, markServiceDead, ringBell, ringRead,
|
|
46
46
|
} from './host/chan.js'
|
|
47
47
|
import { ControlClient } from './host/ctl.js'
|
|
48
|
+
import { chooseTwin, eventsOf, putBackTwin, specKeysDiffering, twinForBoot } from './gowasm/boot-setup.js'
|
|
48
49
|
import { PAGE as CKPT_PAGE, requestCheckpoint } from './host/ckpt-point.js'
|
|
49
50
|
import {
|
|
50
|
-
CheckpointError, serviceErrorCode, assembleCheckpoint,
|
|
51
|
+
CheckpointError, serviceErrorCode, assembleCheckpoint, bootKeyForChoice, specChoice, bootKeyOf, checkCheckpointFits, checkPacksFit, checkVmKeyFits, packDigestsOf, fdTableBytes, parseCheckpoint, restoreFdTable, resumePorts, sha256Hex, validateFdTable,
|
|
51
52
|
} from './host/ckpt.js'
|
|
52
53
|
import { captureJit, jitSnapStats, modulesOf, ownersNeedingHub, restoreStore } from './host/jit-snap.js'
|
|
53
54
|
export { CheckpointError, memoryStore, opfsStore } from './host/ckpt.js'
|
|
@@ -58,6 +59,169 @@ export { P9_CACHE_DEFAULT, P9_FEATURES_LIMIT_MS, P9_FEATURES_MAX_BYTES, P9_NAMES
|
|
|
58
59
|
import { bootGuestFeatures, p9DropNames, p9HandleFields } from './host/p9mount.js'
|
|
59
60
|
export { loadImageTar } from './registry/load-tar.js'
|
|
60
61
|
|
|
62
|
+
/** X5: the decisions of edgeSetup below, counted: `on` (node in the frame) and `off` by reason (node in the guest). */
|
|
63
|
+
export const edgePlanCounts = { on: 0, off: Object.create(null) }
|
|
64
|
+
/** X5: whether a page's `edge` option asks for node in the frame. On by default (no option, or `true`, or an object of settings): only `edge: false` turns it off. */
|
|
65
|
+
export const edgeWanted = v => v !== false && v !== null
|
|
66
|
+
/**
|
|
67
|
+
* X5: the container's node in a frame on another site, for a page that embeds the package (web/README.md "Node in the frame for an embedder").
|
|
68
|
+
* The frame site's origin is the one thing a page must give; the rest has defaults (src/edge/setup.js). Resolves {use, reason, edgeNode, bootOption(),
|
|
69
|
+
* prepare(), close(), bridge(), site}: hand `edgeNode` to containerBundle and `bootOption()` to bootWmd's `edge`, call prepare() after the boot and
|
|
70
|
+
* close() when the machine ends. With the switch off or no origin no edge module is loaded and node runs in the guest, the reason counted.
|
|
71
|
+
* What the origin means (X5 fix3p P2, web/README.md "Node in the frame for an embedder"): a container's frame runs on its origin, which the browser
|
|
72
|
+
* makes a security boundary. Containers given the SAME origin share that site's storage (IndexedDB, Cache Storage, OPFS), service names, locks and
|
|
73
|
+
* channels: a program of one can leave data for a program of another, when it reaches for the browser's objects on purpose (the runtime itself stores
|
|
74
|
+
* nothing there). An embedder that runs containers which must be kept apart gives each its own origin (a host name of its own, which needs a wildcard
|
|
75
|
+
* certificate and name, D-582); the files of the runtime are then fetched and compiled once per origin. An origin keeps its storage after its container
|
|
76
|
+
* ends: reuse of a host name for a new container must clear the site's data first, or take a new name. Kept frames never cross setups (P1).
|
|
77
|
+
* One page, one live setup per origin (check 2 F2, src/edge/setup.js): a second one gets node in the guest ("the frame site is in use") until the
|
|
78
|
+
* first one's close(), unless both passed `shareSite: true`.
|
|
79
|
+
* @param {{origin?: string, edge?: boolean, env?: string[], shareSite?: boolean}} [o]
|
|
80
|
+
*/
|
|
81
|
+
export async function edgeSetup(o = {}) {
|
|
82
|
+
const off = reason => {
|
|
83
|
+
edgePlanCounts.off[reason] = (edgePlanCounts.off[reason] || 0) + 1
|
|
84
|
+
return { use: false, reason, edgeNode: undefined, bootOption: () => undefined, prepare: () => null, close: () => {}, bridge: () => null, site: null, prepared: null }
|
|
85
|
+
}
|
|
86
|
+
if (!edgeWanted(o.edge)) return off('the switch is off')
|
|
87
|
+
// no frame site: the library makes the frame itself and the page fetches the files from the sources (src/edge/sbx/, options `sources` or `base`, default
|
|
88
|
+
// src/edge/base.js); their pins are the generated file src/edge/pins.gen.js, and a build without it runs node in the guest with one reason
|
|
89
|
+
let pinned = null
|
|
90
|
+
if (!o.origin) {
|
|
91
|
+
const F = await import('./edge/sbx/files.js')
|
|
92
|
+
if (!F.edgeSources(o)) return off('no frame site')
|
|
93
|
+
const P = await import('./edge/pins.js')
|
|
94
|
+
pinned = await (o.loadPins || P.loadEdgePins)()
|
|
95
|
+
if (!pinned) return off(P.EDGE_PINS_MISSING)
|
|
96
|
+
}
|
|
97
|
+
const { edgeSetupIn } = await import('./edge/setup.js')
|
|
98
|
+
return edgeSetupIn(pinned ? { ...o, pinned } : o, edgePlanCounts)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* X5 option A: the whole setup of node in the frame for a container, from the options bootWmd got. Only `edge: { base }` (or `edge: {}`) asks for it;
|
|
103
|
+
* `edge: false`, no `edge`, and the page bridge a page made itself (edgeSetup().bootOption(), it has onMessage) come back as they are. It reads the 9P
|
|
104
|
+
* service whose files hold bundle/config/config.json (and bundle/config/imageconfig.json), asks planEdge, adds the edge mounts to the spec (once), and
|
|
105
|
+
* returns the options with `edge` as the bridge's own option. The other fields of the object (watch, prepare, onLog, settings, a test's win and
|
|
106
|
+
* fetchFiles) go to edgeSetup. Nothing the caller passed is changed. -> {o, setup}: setup is edgeSetup's answer (use false: node runs in the guest, the
|
|
107
|
+
* reason counted and in setup.reason), or null when nothing was asked.
|
|
108
|
+
* A restore gives the same options and so the same files (the boot key is the same). The restored machine is a new machine of the same setup: its
|
|
109
|
+
* node runs come to a new bridge, the setup stays, its frame prepare is not made twice.
|
|
110
|
+
*/
|
|
111
|
+
export async function edgeAuto(o, counts = edgePlanCounts) {
|
|
112
|
+
const e = o.edge === true || o.edge === undefined ? {} : o.edge // `true` and no option are `{}`: the default base (none in the package) means node in the guest, with the reason
|
|
113
|
+
if (!e || typeof e !== 'object' || typeof e.onMessage === 'function') return { o, setup: null }
|
|
114
|
+
// F4d: a list of sources longer than the limit is a plain error here, before anything is fetched
|
|
115
|
+
const F = await import('./edge/sbx/files.js')
|
|
116
|
+
if (Array.isArray(e.sources)) F.checkCount(e.sources, 'edge.sources')
|
|
117
|
+
if (Array.isArray(e.base)) F.checkCount(e.base, 'edge.base')
|
|
118
|
+
const A = await import('./edge/auto.js')
|
|
119
|
+
const off = reason => {
|
|
120
|
+
counts.off[reason] = (counts.off[reason] || 0) + 1
|
|
121
|
+
return { o: { ...o, edge: undefined }, setup: { use: false, reason } }
|
|
122
|
+
}
|
|
123
|
+
const svc = A.findService(o)
|
|
124
|
+
if (!svc) return off('no 9P service holds bundle/config/config.json')
|
|
125
|
+
let facts
|
|
126
|
+
try {
|
|
127
|
+
facts = A.containerFacts(svc.files, o.args || [])
|
|
128
|
+
} catch {
|
|
129
|
+
return off('bundle/config/config.json is not JSON')
|
|
130
|
+
}
|
|
131
|
+
// a container that cannot use node in the frame loads no more of the edge code than the plan (the reason is counted)
|
|
132
|
+
const pre = A.preplan(facts, { rt: e.rt, win: e.win })
|
|
133
|
+
if (!pre.use) return off(pre.reason)
|
|
134
|
+
const setup = await edgeSetup({
|
|
135
|
+
...e,
|
|
136
|
+
edge: true,
|
|
137
|
+
env: facts.env,
|
|
138
|
+
user: facts.user,
|
|
139
|
+
readonlyRoot: facts.readonlyRoot,
|
|
140
|
+
hasTmpfs: facts.hasTmpfs,
|
|
141
|
+
p9port: svc.port,
|
|
142
|
+
root: svc.at ?? 'rootfs',
|
|
143
|
+
settings: { hostname: facts.hostname, ...(facts.cpus ? { cpus: facts.cpus } : {}), ...(e.settings || {}) },
|
|
144
|
+
})
|
|
145
|
+
if (!setup.use) return { o: { ...o, edge: undefined }, setup }
|
|
146
|
+
const files = A.addEdgeMounts(svc.files, setup.edgeNode)
|
|
147
|
+
const services = o.channels.services.map(s => (s === svc ? { ...s, files } : s))
|
|
148
|
+
return { o: { ...o, channels: { ...o.channels, services }, edge: setup.bootOption() }, setup }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** A browser that has everything node in the frame needs: used only to make the spec text of a checkpoint's choice (nothing is started with it). */
|
|
152
|
+
const ABLE_WIN = { SharedArrayBuffer: function SharedArrayBuffer() {}, crossOriginIsolated: true, WebAssembly: { Exception: function Exception() {} } }
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* X5 option A part 3, item 16: the options of the same container with the edge mounts of its node in the spec text, whatever this boot's own choice
|
|
156
|
+
* (edge: false, no pins, no sources and a browser that cannot included): only the tools decide the text, as X6's twin does. null: no 9P service with a
|
|
157
|
+
* spec, a spec that is not JSON, or an image without node (a checkpoint of such a container cannot have had node in the frame).
|
|
158
|
+
*/
|
|
159
|
+
async function edgeTextOptions(plain) {
|
|
160
|
+
const A = await import('./edge/auto.js')
|
|
161
|
+
const svc = A.findService(plain)
|
|
162
|
+
if (!svc) return null
|
|
163
|
+
try {
|
|
164
|
+
const e = plain.edge && typeof plain.edge === 'object' ? plain.edge : {}
|
|
165
|
+
const facts = A.containerFacts(svc.files, plain.args || [])
|
|
166
|
+
const pre = A.preplan(facts, { rt: e.rt, win: ABLE_WIN })
|
|
167
|
+
if (!pre.use) return null
|
|
168
|
+
const files = A.addEdgeMounts(svc.files, pre.node)
|
|
169
|
+
if (files === svc.files) return null // the spec already holds the mounts (the page made them itself): nothing to choose
|
|
170
|
+
return { ...plain, channels: { ...plain.channels, services: plain.channels.services.map(x => (x === svc ? { ...x, files } : x)) } }
|
|
171
|
+
} catch {
|
|
172
|
+
return null
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* X5 option A part 3, item 16: a restore keeps the choice of node in the frame its checkpoint was taken with (the rule X6 made for the Go twin, kept
|
|
178
|
+
* independent of it). `plain` is the options as the caller gave them, `auto` what edgeAuto made of them ({o, setup}; setup null when nothing was asked) and
|
|
179
|
+
* `header` the checkpoint's (or a base snapshot's) header. Which of the four spec texts (plain, twin, node, both) the header's boot description holds
|
|
180
|
+
* names the checkpoint's choice; the twin half is left to chooseTwin, which then sees a base that has the node text the checkpoint had.
|
|
181
|
+
* - the checkpoint had node and this boot has it: nothing changes;
|
|
182
|
+
* - the checkpoint had NO node and this boot would: the options without it, the setup is to be closed (`close`), one event {t: 'edge-off', rule: 'checkpoint'};
|
|
183
|
+
* - the checkpoint had node and this boot does not (edge: false, no pins, no sources, a browser that cannot): the machine keeps the checkpoint's spec text
|
|
184
|
+
* (its guest has the stub mounted), no setup, no frame, nothing fetched; every node start is refused and the stub runs the image's node in the guest;
|
|
185
|
+
* one event {t: 'edge-off', rule: 'checkpoint-node'}.
|
|
186
|
+
* A header with no description, a text that is none of the four, or an image without node change nothing: the usual check of the restore decides.
|
|
187
|
+
* -> {o, events, use, close}: `use` says the machine has its setup (the frame is used).
|
|
188
|
+
*/
|
|
189
|
+
export async function chooseEdge(plain, auto, header) {
|
|
190
|
+
const used = !!(auto && auto.setup && auto.setup.use)
|
|
191
|
+
const none = { o: auto.o, events: [], use: used, close: false }
|
|
192
|
+
if (!header || typeof header !== 'object' || (plain.edge && typeof plain.edge === 'object' && typeof plain.edge.onMessage === 'function')) return none
|
|
193
|
+
const A = await import('./edge/auto.js')
|
|
194
|
+
const svc = A.findService(plain)
|
|
195
|
+
if (!svc) return none
|
|
196
|
+
const i = plain.channels.services.indexOf(svc)
|
|
197
|
+
const withNode = await edgeTextOptions(plain)
|
|
198
|
+
if (!withNode) return none
|
|
199
|
+
const [tP, tE] = [await twinForBoot(plain), await twinForBoot(withNode)]
|
|
200
|
+
const text = (o, k) => (o.channels.services[k].files[A.BUNDLE_CONFIG])
|
|
201
|
+
const twinText = (v, k) => (v.texts || []).find(t => t.i === k)?.twin
|
|
202
|
+
const names = { plain: text(plain, i), node: text(withNode, i) }
|
|
203
|
+
if (twinText(tP, i) !== undefined) names.twin = twinText(tP, i)
|
|
204
|
+
if (twinText(tE, i) !== undefined) names.both = twinText(tE, i)
|
|
205
|
+
const n = await specChoice(header, i, svc.port, A.BUNDLE_CONFIG, names)
|
|
206
|
+
if (n === null) return none
|
|
207
|
+
const hadNode = n === 'node' || n === 'both'
|
|
208
|
+
if (hadNode === used) return none
|
|
209
|
+
if (hadNode) {
|
|
210
|
+
return { o: { ...withNode, edge: undefined }, events: [{ t: 'edge-off', rule: 'checkpoint-node', why: "the checkpoint was taken with node in the frame and node in the frame is off in this boot: the machine keeps the container's spec (node's stub is mounted in its memory) and every node start runs in the guest" }], use: false, close: false }
|
|
211
|
+
}
|
|
212
|
+
return { o: { ...plain, edge: undefined }, events: [{ t: 'edge-off', rule: 'checkpoint', why: 'the checkpoint was taken without node in the frame, so this machine runs without it (node runs in the guest)' }], use: false, close: true }
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** X5 option A: the setup closes when its machine ends (not when a restore replaces the machine). Resolves after that. */
|
|
216
|
+
export function edgeAutoEnd(setup, exited, restoring) {
|
|
217
|
+
return Promise.resolve(exited).then(
|
|
218
|
+
() => {},
|
|
219
|
+
() => {}
|
|
220
|
+
).then(() => {
|
|
221
|
+
if (!restoring()) setup.close()
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
|
|
61
225
|
const PAGE = 65536
|
|
62
226
|
/** A trap with the memory this close to its maximum is reported as out of memory. */
|
|
63
227
|
export const OOM_MARGIN = 64 << 20
|
|
@@ -697,9 +861,46 @@ export async function bootWmd(o) {
|
|
|
697
861
|
if (!globalThis.crossOriginIsolated) {
|
|
698
862
|
throw new Error('page is not cross-origin isolated (needs COOP same-origin + COEP require-corp)')
|
|
699
863
|
}
|
|
864
|
+
// X5 option A: `edge: { base }` is all a page says; the setup of node in the frame is done here (edgeAuto below)
|
|
865
|
+
const plain0 = o // the options as the caller gave them: a restore chooses node in the frame from its checkpoint's choice (chooseEdge)
|
|
866
|
+
let auto = null
|
|
867
|
+
if (o.edge === true || o.edge === undefined || (o.edge && typeof o.edge === 'object' && typeof o.edge.onMessage !== 'function')) {
|
|
868
|
+
auto = await edgeAuto(o)
|
|
869
|
+
o = auto.setup?.use ? { ...auto.o, _edgeAuto: auto.setup } : auto.o
|
|
870
|
+
}
|
|
871
|
+
if (o.restore?.header) {
|
|
872
|
+
// item 16: a checkpoint in hand decides whether node is in the frame, whatever edge says (also with edge: false and the other kinds of off)
|
|
873
|
+
const c = await chooseEdge(plain0, auto || { o: plain0, setup: null }, o.restore.header)
|
|
874
|
+
for (const e of c.events) o.onEvent?.(e)
|
|
875
|
+
if (c.close) o._edgeAuto?.close()
|
|
876
|
+
if (c.events.length || c.close) o = { ...o, channels: c.o.channels, edge: c.o.edge, _edgeAuto: c.use ? o._edgeAuto : undefined }
|
|
877
|
+
}
|
|
878
|
+
if (!o._edgePlain) o = { ...o, _edgePlain: plain0 }
|
|
879
|
+
try {
|
|
880
|
+
return await bootMachine(o)
|
|
881
|
+
} catch (e) {
|
|
882
|
+
o._edgeAuto?.close() // a boot that failed (a restore's too) leaves no frame site held
|
|
883
|
+
throw e
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
async function bootMachine(o) {
|
|
700
888
|
const t0 = absMs()
|
|
701
889
|
const timings = { t0 }
|
|
702
890
|
const ev = e => o.onEvent?.(e)
|
|
891
|
+
// X6 part 3: the Go twin's setup (src/gowasm/boot-setup.js): stub mounts in the bundle's spec and the service's gowasm field, from the pins; nothing for a
|
|
892
|
+
// caller that passed `gowasm: false`, named no source and has no default, or whose service has its own gowasm field
|
|
893
|
+
// Part 4: a boot that restores takes the twin choice its checkpoint was taken with (chooseTwin): here for a checkpoint in hand (o.restore), when the
|
|
894
|
+
// base snapshot's header is known for o.restoreLazy (below, with the layout), in vm.restore() before the running machine is stopped. A checkpoint taken
|
|
895
|
+
// with the twin that this boot has off (gowasm: false, no source, a setup that is not usable) restores (part 6): its spec text stays (the guest has the stub mounts),
|
|
896
|
+
// the service's gowasm field says `off` so the page starts no twin Worker, the channel service builds a GoEdge that is off from the start (every stub is answered
|
|
897
|
+
// with the fallback, the guest compiles), and one event {t: 'gowasm-off', rule: 'checkpoint-twin'} says so.
|
|
898
|
+
let twin = await twinForBoot(o)
|
|
899
|
+
const twinFirst = await chooseTwin(twin, o.restore?.header || null)
|
|
900
|
+
o = twinFirst.o
|
|
901
|
+
// the twin's events are said once: now, or, for a base snapshot (o.restoreLazy), when its header is known (its choice may supersede the boot's own reason)
|
|
902
|
+
let twinSaid = !o.restoreLazy
|
|
903
|
+
if (twinSaid) for (const e of eventsOf(twin, twinFirst)) ev(e)
|
|
703
904
|
// W24: the start-up events (host/boot-events.js); be.close() at the end
|
|
704
905
|
const be = createBootEvents({ emit: ev, t0, now: absMs })
|
|
705
906
|
// the emulator's last stderr text, for the error a wait on a dead machine gives
|
|
@@ -856,7 +1057,7 @@ export async function bootWmd(o) {
|
|
|
856
1057
|
})
|
|
857
1058
|
filesP.catch(() => {}) // awaited below; must not go unhandled if vm.wasm fails first
|
|
858
1059
|
// P-B3: the channel services' packs (fetched in parallel with the rest); pack:<n> numbers them over all services
|
|
859
|
-
|
|
1060
|
+
let chanSvcs = o.channels?.services || []
|
|
860
1061
|
let packN = 0
|
|
861
1062
|
const packIds = chanSvcs.map(svc => (svc.layers || []).map(() => packN++))
|
|
862
1063
|
const layersP = A
|
|
@@ -868,7 +1069,11 @@ export async function bootWmd(o) {
|
|
|
868
1069
|
).then(downloaded)
|
|
869
1070
|
layersP.catch(() => {})
|
|
870
1071
|
let module, files, chanLayers, tableMin, vmBytes
|
|
871
|
-
|
|
1072
|
+
let edgeBefore = null // the options and twin before the base snapshot's header changed node's choice
|
|
1073
|
+
let closeEdgeAfter = null
|
|
1074
|
+
let twinByBase = false // the base snapshot's header changed the twin choice (a header refused after that puts the options' choice back)
|
|
1075
|
+
let twinChosen = null // chooseTwin's answer for the base snapshot's header: its events are said when the snapshot is accepted (or the options' own, when it is refused)
|
|
1076
|
+
async function baseRefused(err) {
|
|
872
1077
|
const lazy = lazyIn || baseLazy
|
|
873
1078
|
lazyIn = null
|
|
874
1079
|
baseLazy = null
|
|
@@ -876,6 +1081,30 @@ export async function bootWmd(o) {
|
|
|
876
1081
|
lazy?.abort?.(err)
|
|
877
1082
|
} catch {}
|
|
878
1083
|
if (!o.restoreFallback) throw err
|
|
1084
|
+
// fix 10 (F1): the machine boots cold, so it takes the options' own twin choice (spec text, gowasm field, boot key) whatever refused the snapshot, the header or
|
|
1085
|
+
// the checks of its body: this is the one place
|
|
1086
|
+
// X5 item 16: and node's own choice of the options first (the frame's stubs go back into the spec; the setup of the options' node is still open, it is
|
|
1087
|
+
// let go only once the snapshot is accepted), so the twin put back below is the one made for that spec
|
|
1088
|
+
if (edgeBefore) {
|
|
1089
|
+
o = { ...o, channels: edgeBefore.o.channels, edge: edgeBefore.o.edge, _edgeAuto: edgeBefore.o._edgeAuto }
|
|
1090
|
+
twin = edgeBefore.twin
|
|
1091
|
+
chanSvcs = o.channels?.services || []
|
|
1092
|
+
edgeBefore = null
|
|
1093
|
+
}
|
|
1094
|
+
closeEdgeAfter = null
|
|
1095
|
+
if (twinByBase) {
|
|
1096
|
+
const back = await putBackTwin(twin, o, bootDesc, { flat: flatRam, maxPages: maximum })
|
|
1097
|
+
o = back.o
|
|
1098
|
+
chanSvcs = o.channels?.services || []
|
|
1099
|
+
twinByBase = false
|
|
1100
|
+
if (back.key) {
|
|
1101
|
+
bootKey = back.key.key
|
|
1102
|
+
bootDesc = back.key.desc
|
|
1103
|
+
if (heldAssets) heldAssets.keys = { ...heldAssets.keys, bootKey, bootDesc }
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
if (!twinSaid) for (const e of twin.events) ev(e)
|
|
1107
|
+
twinSaid = true
|
|
879
1108
|
timings.baseFallback = { code: err && err.code ? err.code : 'error', message: err && err.message ? err.message : String(err) }
|
|
880
1109
|
// W24: the restore stage ends here, unused; the kernel stage follows
|
|
881
1110
|
be.now('restore', { t: 'boot', stage: 'restore', state: 'done', used: false, fallback: timings.baseFallback.code, bytes: baseBytes, total: baseTotal, source: baseSource })
|
|
@@ -890,12 +1119,35 @@ export async function bootWmd(o) {
|
|
|
890
1119
|
const h = await raceAbort(lazyIn.header)
|
|
891
1120
|
checkVmKeyFits(h, vmKey)
|
|
892
1121
|
settleLayout(chooseLayout(o.args, o, h)) // X24 default: the base snapshot's own layout
|
|
1122
|
+
// part 4: and its own twin choice. The spec text is first used by the boot key and by the channel service's Worker, both below this point in this same
|
|
1123
|
+
// try, so the choice made here is made before either has it; only the layers and ports of the services were used before (their packs download meanwhile)
|
|
1124
|
+
// item 16: and its own choice of node in the frame (the twin below then sees the spec text of that choice)
|
|
1125
|
+
const ce = o._edgePlain ? await chooseEdge(o._edgePlain, { o, setup: { use: !!o._edgeAuto } }, h) : null
|
|
1126
|
+
if (ce && ce.o !== o) {
|
|
1127
|
+
edgeBefore = { o, twin }
|
|
1128
|
+
o = { ...o, channels: ce.o.channels, edge: ce.o.edge }
|
|
1129
|
+
twin = await twinForBoot(o)
|
|
1130
|
+
chanSvcs = o.channels?.services || []
|
|
1131
|
+
twinByBase = true
|
|
1132
|
+
for (const e of ce.events) ev(e)
|
|
1133
|
+
if (ce.close) closeEdgeAfter = o._edgeAuto
|
|
1134
|
+
if (!ce.use) o = { ...o, _edgeAuto: undefined }
|
|
1135
|
+
}
|
|
1136
|
+
const tw = await chooseTwin(twin, h)
|
|
1137
|
+
if (tw.o !== o) {
|
|
1138
|
+
o = { ...o, channels: tw.o.channels }
|
|
1139
|
+
chanSvcs = o.channels?.services || []
|
|
1140
|
+
twinByBase = true
|
|
1141
|
+
}
|
|
1142
|
+
twinChosen = tw
|
|
893
1143
|
lazyIn.proceed?.() // the reader decodes only from here: a refused header costs what was queued, not the file
|
|
894
1144
|
} catch (err) {
|
|
895
1145
|
if (signal?.aborted) throw err
|
|
896
|
-
baseRefused(err)
|
|
1146
|
+
await baseRefused(err) // (the machine that boots cold takes the options' own twin choice back there, whatever refused the snapshot)
|
|
897
1147
|
}
|
|
898
1148
|
}
|
|
1149
|
+
if (!twinSaid && !lazyIn) for (const e of twin.events) ev(e) // no base snapshot after all
|
|
1150
|
+
if (!lazyIn) twinSaid = true
|
|
899
1151
|
if (!layoutSaid) settleLayout(layout0) // no base snapshot, or its header was refused: the options' layout
|
|
900
1152
|
const tc = absMs()
|
|
901
1153
|
be.now('compile', { t: 'boot', stage: 'compile', state: 'start', moduleBytes: vmBytes.byteLength, reused: !!A })
|
|
@@ -919,12 +1171,15 @@ export async function bootWmd(o) {
|
|
|
919
1171
|
if (A) {
|
|
920
1172
|
packs = A.keys.packs
|
|
921
1173
|
const ad = A.keys.bootDesc
|
|
922
|
-
|
|
1174
|
+
// part 4: the spec texts of this boot (the twin choice the checkpoint made) against the ones the assets' machine had
|
|
1175
|
+
const cfgKeys = ad ? await specKeysDiffering(ad, chanSvcs) : {}
|
|
1176
|
+
if (!ad || (ad.maxPages === maximum && Array.isArray(ad.env) && ad.env.includes('WMD_RAM_FLAT=1') === flatRam && !Object.keys(cfgKeys).length)) {
|
|
923
1177
|
bootKey = A.keys.bootKey
|
|
924
1178
|
bootDesc = A.keys.bootDesc
|
|
925
1179
|
} else {
|
|
926
1180
|
// X24 default: the checkpoint chose another RAM layout than the machine these assets came from had: the same key, with that layout
|
|
927
|
-
|
|
1181
|
+
// (part 4: and the same with the other spec text when the checkpoint chose another twin state)
|
|
1182
|
+
const k = await bootKeyForChoice(ad, { flat: flatRam, maxPages: maximum }, cfgKeys)
|
|
928
1183
|
bootKey = k.key
|
|
929
1184
|
bootDesc = k.desc
|
|
930
1185
|
}
|
|
@@ -964,10 +1219,14 @@ export async function bootWmd(o) {
|
|
|
964
1219
|
checkCheckpointFits(parsed.header, { vmKey, bootKey, bootDesc, maxPages: maximum, ramPages, hasJit: o.jit !== false, fmaFused: fmaFuses() })
|
|
965
1220
|
checkPacksFit(parsed.header.packs, packDigests)
|
|
966
1221
|
restoreIn = parsed
|
|
1222
|
+
if (!twinSaid) for (const e of twinChosen ? eventsOf(twin, twinChosen) : twin.events) ev(e) // the snapshot is accepted: the checkpoint's twin choice stands
|
|
1223
|
+
twinSaid = true
|
|
1224
|
+
closeEdgeAfter?.close() // and its choice of node: the setup of a node that this machine does not use is let go
|
|
1225
|
+
closeEdgeAfter = null
|
|
967
1226
|
lazyIn = null // baseLazy stays: accept() when the restore has succeeded, reject() if it fails
|
|
968
1227
|
} catch (err) {
|
|
969
1228
|
if (signal?.aborted) throw err
|
|
970
|
-
baseRefused(err)
|
|
1229
|
+
await baseRefused(err)
|
|
971
1230
|
}
|
|
972
1231
|
}
|
|
973
1232
|
if (restoreIn) checkCheckpointFits(restoreIn.header, { vmKey, bootKey, bootDesc, maxPages: maximum, ramPages, hasJit: o.jit !== false, fmaFused: fmaFuses() })
|
|
@@ -1137,6 +1396,7 @@ export async function bootWmd(o) {
|
|
|
1137
1396
|
// P-B10: the network Worker, when a net service has a relay
|
|
1138
1397
|
let netWorker = null
|
|
1139
1398
|
let goWorker = null // X6 (default off): the Wasm twin of the guest's Go compiler
|
|
1399
|
+
let goCspOff = null // takes the document's policy-violation listener of the live twin Worker off again
|
|
1140
1400
|
let netStats = null
|
|
1141
1401
|
let netMember = null // W56: this guest on an in-page switch (switch.js attach())
|
|
1142
1402
|
const netWaiters = new Map()
|
|
@@ -1248,6 +1508,7 @@ export async function bootWmd(o) {
|
|
|
1248
1508
|
else if (m.t === 'gowasm-stop') {
|
|
1249
1509
|
goWorker?.terminate()
|
|
1250
1510
|
goWorker = null
|
|
1511
|
+
goCspOff?.()
|
|
1251
1512
|
}
|
|
1252
1513
|
else if (m.t === 'gowasm-restart') {
|
|
1253
1514
|
// the twin was in the middle of a compile that was cancelled: a Worker cannot be stopped inside one, so it ends and a new one starts
|
|
@@ -1324,10 +1585,36 @@ export async function bootWmd(o) {
|
|
|
1324
1585
|
const mc = new MessageChannel()
|
|
1325
1586
|
const w = (goWorker = new Worker(new URL('./host/gowasm-worker.js', import.meta.url), { type: 'module', name: 'wmd-gowasm' }))
|
|
1326
1587
|
w.onerror = err => chanSvc?.postMessage({ t: 'gowasm-died', why: `the twin Worker failed: ${err.message || err}` })
|
|
1327
|
-
|
|
1588
|
+
// fix 10 (F4): a policy violation the document sees is told to the Worker too (the Worker listens for its own); its reason text then says the policy blocked the source
|
|
1589
|
+
if (typeof document !== 'undefined') {
|
|
1590
|
+
goCspOff?.() // one listener, for the live Worker
|
|
1591
|
+
const onViolation = e => {
|
|
1592
|
+
if (goWorker !== w) return // that Worker is gone
|
|
1593
|
+
try {
|
|
1594
|
+
w.postMessage({ t: 'csp', violation: { blockedURI: String(e.blockedURI), effectiveDirective: String(e.effectiveDirective || ''), disposition: String(e.disposition || 'enforce') } })
|
|
1595
|
+
} catch {}
|
|
1596
|
+
}
|
|
1597
|
+
document.addEventListener('securitypolicyviolation', onViolation)
|
|
1598
|
+
goCspOff = () => {
|
|
1599
|
+
document.removeEventListener('securitypolicyviolation', onViolation)
|
|
1600
|
+
goCspOff = null
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
w.postMessage({ t: 'init', port: mc.port2, wasmUrl: goSvc.gowasm.wasmUrl, wasmSha256: goSvc.gowasm.wasmSha256, modules: goSvc.gowasm.modules, preload: goSvc.gowasm.preload, sources: goSvc.gowasm.sources, cacheBytes: goSvc.gowasm.cacheBytes }, [mc.port2])
|
|
1328
1604
|
return mc.port1
|
|
1329
1605
|
}
|
|
1330
|
-
|
|
1606
|
+
// X6 part 3: a twin Worker that cannot start (a page whose CSP forbids it, no Worker support) must not stop the machine: the channel service takes the
|
|
1607
|
+
// stub mounts out of the bundle (no twin Worker) and one event says why
|
|
1608
|
+
let gowasmPort = null
|
|
1609
|
+
let gowasmWhy = ''
|
|
1610
|
+
// part 6: a service whose gowasm field says `off` (a checkpoint taken with the twin, restored in a boot that has the twin off) gets no Worker
|
|
1611
|
+
if (goSvc && !goSvc.gowasm.off) {
|
|
1612
|
+
try {
|
|
1613
|
+
gowasmPort = spawnGoTwin()
|
|
1614
|
+
} catch (err) {
|
|
1615
|
+
gowasmWhy = `the twin Worker could not start: ${(err && err.message) || err}`
|
|
1616
|
+
}
|
|
1617
|
+
}
|
|
1331
1618
|
chanSvc.postMessage({
|
|
1332
1619
|
t: 'init',
|
|
1333
1620
|
sab: chanSab,
|
|
@@ -1348,6 +1635,7 @@ export async function bootWmd(o) {
|
|
|
1348
1635
|
netPort,
|
|
1349
1636
|
edge: o.edge ? { port9p: o.edge.port, root: o.edge.root || '', rt: o.edge.rt || '' } : undefined, // X5 (edge=1 only)
|
|
1350
1637
|
gowasmPort,
|
|
1638
|
+
gowasmWhy,
|
|
1351
1639
|
}, [...(netPort ? [netPort] : []), ...(gowasmPort ? [gowasmPort] : [])])
|
|
1352
1640
|
consoleGate = consoleInputGate(chanSab, o.channels.consoleInputBytes ?? CONSOLE_IN_LIMIT)
|
|
1353
1641
|
}
|
|
@@ -1564,6 +1852,7 @@ export async function bootWmd(o) {
|
|
|
1564
1852
|
failUpperWaiters('the machine has ended (its files ended with it)')
|
|
1565
1853
|
netWorker?.terminate()
|
|
1566
1854
|
goWorker?.terminate()
|
|
1855
|
+
goCspOff?.()
|
|
1567
1856
|
netMember?.detach() // W56: its peers' streams to it fail; the name keeps its address
|
|
1568
1857
|
if (netWorker) timings.net = netStats
|
|
1569
1858
|
if (chanSab) timings.chan = { ...chanCounters(chanSab), service: chanStats, died: chanDied }
|
|
@@ -1632,6 +1921,7 @@ export async function bootWmd(o) {
|
|
|
1632
1921
|
netWorker = null
|
|
1633
1922
|
if (goWorker) goWorker.onerror = null
|
|
1634
1923
|
goWorker = null
|
|
1924
|
+
goCspOff?.()
|
|
1635
1925
|
netMember?.detach()
|
|
1636
1926
|
module = files = null
|
|
1637
1927
|
}
|
|
@@ -2135,8 +2425,21 @@ export async function bootWmd(o) {
|
|
|
2135
2425
|
// X24 default (#70): the checkpoint decides the next machine's RAM layout (bootWmd does the same from the same header), so the check is
|
|
2136
2426
|
// made against that layout's key, memory maximum and RAM memory, not this machine's
|
|
2137
2427
|
const L = chooseLayout(o.args, { ...o, ...over }, parsed.header)
|
|
2428
|
+
// part 4: and its twin choice (the next machine's own, from the options as the caller gave them): a checkpoint taken with the twin restores whatever this boot's twin state (chooseTwin keeps its spec text and says the twin is off), the usual fit check follows, before this machine stops
|
|
2429
|
+
let nextOpts = { ...o, channels: twin.plain.channels, ...over }
|
|
2430
|
+
// item 16: and its choice of node in the frame (before the twin's: the twin's texts are made on the spec text of that choice)
|
|
2431
|
+
let edgeEvents = []
|
|
2432
|
+
let closeEdge = null
|
|
2433
|
+
const ce = o._edgePlain && !('channels' in over) && !('edge' in over) ? await chooseEdge(o._edgePlain, { o: nextOpts, setup: { use: !!o._edgeAuto } }, parsed.header) : null
|
|
2434
|
+
if (ce && (ce.events.length || ce.close)) {
|
|
2435
|
+
nextOpts = { ...nextOpts, channels: ce.o.channels, edge: ce.o.edge, _edgeAuto: ce.use ? o._edgeAuto : undefined }
|
|
2436
|
+
edgeEvents = ce.events
|
|
2437
|
+
closeEdge = ce.close ? o._edgeAuto : null
|
|
2438
|
+
}
|
|
2439
|
+
const tw = await chooseTwin(await twinForBoot(nextOpts), parsed.header)
|
|
2440
|
+
const cfgKeys = bootDesc ? await specKeysDiffering(bootDesc, tw.o.channels?.services) : {}
|
|
2138
2441
|
let target = { key: bootKey, desc: bootDesc }
|
|
2139
|
-
if (bootDesc && (L.flat !== flatRam || L.maximum !== maximum)) target = await
|
|
2442
|
+
if (bootDesc && (L.flat !== flatRam || L.maximum !== maximum || Object.keys(cfgKeys).length)) target = await bootKeyForChoice(bootDesc, { flat: L.flat, maxPages: L.maximum }, cfgKeys)
|
|
2140
2443
|
checkCheckpointFits(parsed.header, { vmKey, bootKey: target.key, bootDesc: target.desc, maxPages: L.maximum, ramPages: L.ramPages, hasJit: o.jit !== false, fmaFused: fmaFuses() })
|
|
2141
2444
|
if (!heldAssets) throw new CheckpointError('not-ready', 'this machine was booted without checkpoint support: it has no kept boot files to restore with')
|
|
2142
2445
|
validateFdTable(parsed.ctl, heldAssets.files.length) // the fd table is checked against this boot's files now, not after the old machine is gone
|
|
@@ -2151,13 +2454,15 @@ export async function bootWmd(o) {
|
|
|
2151
2454
|
keepHub = !!(hub && parsed.jit)
|
|
2152
2455
|
let oldHub = null
|
|
2153
2456
|
try {
|
|
2457
|
+
for (const e of edgeEvents) ev(e) // the restore is taken (the checks above passed): node's choice is said once
|
|
2154
2458
|
finish(EXIT_STOPPED, 'stopped')
|
|
2155
2459
|
await exited
|
|
2156
2460
|
memory = null
|
|
2157
2461
|
oldHub = keptHub
|
|
2158
2462
|
keptHub = null
|
|
2159
|
-
const next = await bootWmd({ ...
|
|
2463
|
+
const next = await bootWmd({ ...nextOpts, checkpoint: true, restore: ckpt, assets, _adopt: oldHub, _net: { instance: netInstance, names: [...netNames] } })
|
|
2160
2464
|
heldAssets = null // the new machine holds them
|
|
2465
|
+
closeEdge?.close() // a checkpoint taken without node: the setup of the machine that had it is let go
|
|
2161
2466
|
return next
|
|
2162
2467
|
} finally {
|
|
2163
2468
|
keepHub = false
|
|
@@ -2287,7 +2592,7 @@ export async function bootWmd(o) {
|
|
|
2287
2592
|
ours.push(mc.port1)
|
|
2288
2593
|
theirs.push(mc.port2)
|
|
2289
2594
|
}
|
|
2290
|
-
chanSvc.postMessage({ t: 'edge-fs', port9p: port, mounts, root, ports: ours, yieldMs, run, own, job, kickAll }, ours)
|
|
2595
|
+
chanSvc.postMessage({ t: 'edge-fs', port9p: port, mounts, root, ports: ours, yieldMs, run, own, job, kickAll, ringMax: globalThis.__wmdRingMax }, ours) // ringMax: a page's test knob (the first run's value counts)
|
|
2291
2596
|
return theirs
|
|
2292
2597
|
},
|
|
2293
2598
|
/** X5 (edge=1 only): the page's answer or output for a node start the service asked for ({id, ev: 'reply'|'out'|'exit', ...}). */
|
|
@@ -2364,5 +2669,9 @@ export async function bootWmd(o) {
|
|
|
2364
2669
|
// fix10: the guest drops the names and attributes it keeps (9P names; plain mmap keeps none): the bridge asks it after a run whose stub went away
|
|
2365
2670
|
const edgeDropNames = () => (control && !done && !chanDied ? p9DropNames(control) : Promise.reject(new Error('edgeDropNames: the machine has ended')))
|
|
2366
2671
|
Object.assign(edgeHandle, { edgeFs: api.edgeFs, edgeMessage: api.edgeMessage, edgeYield: api.edgeYield, edgeRelease: api.edgeRelease, edgeDropNames })
|
|
2672
|
+
if (o._edgeAuto) {
|
|
2673
|
+
o._edgeAuto.prepare() // once per setup: the frame is made when the page is idle
|
|
2674
|
+
edgeAutoEnd(o._edgeAuto, exited, () => restoring)
|
|
2675
|
+
}
|
|
2367
2676
|
return api
|
|
2368
2677
|
}
|
package/src/edge/auto.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// X5 option A item 3: what bootWmd reads from a container's files to set up node in the frame by itself (src/boot.js edgeAuto), and the one thing it
|
|
2
|
+
// changes there: the three mounts of the image's node (src/host/ctl.js edgeSpecMounts). Plain functions, no browser.
|
|
3
|
+
import { EDGE_CONTAINER_DIR, NODE_PATHS, edgeSpecMounts, planEdge } from './plan.js'
|
|
4
|
+
|
|
5
|
+
export const BUNDLE_CONFIG = 'bundle/config/config.json'
|
|
6
|
+
export const BUNDLE_IMAGE = 'bundle/config/imageconfig.json'
|
|
7
|
+
const dec = new TextDecoder()
|
|
8
|
+
const enc = new TextEncoder()
|
|
9
|
+
|
|
10
|
+
/** The text of a 9P service file: its text, its bytes, or {data}. -> string, or null for a url or something else. */
|
|
11
|
+
export function textOf(v) {
|
|
12
|
+
if (typeof v === 'string') return v
|
|
13
|
+
if (v instanceof Uint8Array) return dec.decode(v)
|
|
14
|
+
if (v instanceof ArrayBuffer) return dec.decode(new Uint8Array(v))
|
|
15
|
+
if (v && typeof v === 'object' && 'data' in v) return textOf(v.data)
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
/** `text` in the form `old` had (text stays text, bytes stay bytes, {data, mode, ...} keeps its fields). */
|
|
19
|
+
export function sameForm(old, text) {
|
|
20
|
+
if (typeof old === 'string') return text
|
|
21
|
+
if (old instanceof Uint8Array || old instanceof ArrayBuffer) return enc.encode(text)
|
|
22
|
+
return { ...old, data: sameForm(old.data, text) }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The 9P service of the container's files: the first of kind 9p (the default kind) whose files hold the bundle's config.json. */
|
|
26
|
+
export const findService = o => (o?.channels?.services || []).find(s => s && (s.kind ?? '9p') === '9p' && s.files && s.files[BUNDLE_CONFIG] !== undefined) || null
|
|
27
|
+
|
|
28
|
+
const userOf = u => (u && typeof u === 'object' ? `${u.uid ?? 0}${u.gid !== undefined ? ':' + u.gid : ''}` : String(u ?? ''))
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* What planEdge needs, read from the service's files: the spec (bundle/config/config.json) and the image's config (bundle/config/imageconfig.json).
|
|
32
|
+
* The spec's environment is the container's; the image's own is the fallback when the spec has none. Throws when the spec is not JSON.
|
|
33
|
+
* @returns {{env: string[], user: string, readonlyRoot: boolean, hasTmpfs: boolean, hostname: string, cpus: number|undefined}}
|
|
34
|
+
*/
|
|
35
|
+
export function containerFacts(files, args = []) {
|
|
36
|
+
const spec = JSON.parse(textOf(files[BUNDLE_CONFIG]) ?? '')
|
|
37
|
+
let image = {}
|
|
38
|
+
try {
|
|
39
|
+
image = JSON.parse(textOf(files[BUNDLE_IMAGE]) ?? '{}') || {}
|
|
40
|
+
} catch {
|
|
41
|
+
image = {}
|
|
42
|
+
}
|
|
43
|
+
const specEnv = spec?.process?.env
|
|
44
|
+
const env = Array.isArray(specEnv) && specEnv.length ? specEnv.map(String) : (image?.config?.Env || []).map(String)
|
|
45
|
+
// the engine's own tmpfs mounts (/dev, /dev/shm) are not the user's; any other tmpfs is, and the desktop refuses it too
|
|
46
|
+
const hasTmpfs = (spec?.mounts || []).some(m => m && m.type === 'tmpfs' && m.destination !== '/dev' && m.destination !== '/dev/shm')
|
|
47
|
+
const i = args.indexOf('--cpus')
|
|
48
|
+
const cpus = i >= 0 && Number(args[i + 1]) > 0 ? Number(args[i + 1]) : undefined
|
|
49
|
+
return { env, user: userOf(spec?.process?.user), readonlyRoot: !!spec?.root?.readonly, hasTmpfs, hostname: spec?.hostname || 'wmd-web', cpus }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The files with the edge mounts of the image's node added to the spec, once: a spec that already has them (containerBundle's `edgeNode`, or an earlier
|
|
54
|
+
* call) comes back unchanged. The mounts go where containerBundle puts them (before the /etc files). A new files object; the caller's is not changed.
|
|
55
|
+
*/
|
|
56
|
+
export function addEdgeMounts(files, node, rootfsPath = '/run/wmd9p/rootfs') {
|
|
57
|
+
const text = textOf(files[BUNDLE_CONFIG])
|
|
58
|
+
const spec = JSON.parse(text)
|
|
59
|
+
const have = new Set((spec.mounts || []).map(m => m.destination))
|
|
60
|
+
const root = spec.root?.path || rootfsPath
|
|
61
|
+
const add = edgeSpecMounts(node, root).filter(m => !have.has(m.destination))
|
|
62
|
+
if (!add.length) return files
|
|
63
|
+
const mounts = [...(spec.mounts || [])]
|
|
64
|
+
const at = mounts.findIndex(m => String(m.destination).startsWith('/etc/'))
|
|
65
|
+
mounts.splice(at < 0 ? mounts.length : at, 0, ...add)
|
|
66
|
+
const out = JSON.stringify({ ...spec, mounts }, null, 2) + '\n'
|
|
67
|
+
return { ...files, [BUNDLE_CONFIG]: sameForm(files[BUNDLE_CONFIG], out) }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export { EDGE_CONTAINER_DIR }
|
|
71
|
+
|
|
72
|
+
/** What the browser needs for node in the frame (the same test as setup.js edgeBrowserOk, here so that a container that cannot use it loads nothing more). */
|
|
73
|
+
export const browserOk = (g = globalThis) => typeof g.SharedArrayBuffer === 'function' && g.crossOriginIsolated === true && typeof g.WebAssembly?.Exception === 'function'
|
|
74
|
+
|
|
75
|
+
/** planEdge for the facts of a container (see containerFacts). -> {use, node?, reason?} */
|
|
76
|
+
export const preplan = (facts, { rt, win = globalThis } = {}) =>
|
|
77
|
+
planEdge({ enabled: true, env: facts.env, user: facts.user, readonlyRoot: facts.readonlyRoot, hasTmpfs: facts.hasTmpfs, hasJspi: browserOk(win), rt, nodePath: NODE_PATHS[0] })
|
package/src/edge/base.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// The places the files of node in the sandboxed frame come from when the embedder names none (the option edge: { sources } of bootWmd; edge: { base }
|
|
2
|
+
// is the short form for one file base). ONE constant, the same shape as X6's GOWASM_SOURCES_DEFAULT (src/gowasm/setup.js): the registry's tarball of the
|
|
3
|
+
// package first, then two file mirrors of the same package. The package and its version are not written here: only the pins (src/edge/pins.gen.js,
|
|
4
|
+
// written by the release from the files it publishes as @threadedstack/wmd-node, version = the release's) say which, in the {name} {version} {file} of
|
|
5
|
+
// the urls. A checkout or a package without pins runs node in the guest, with one calm reason.
|
|
6
|
+
export const EDGE_SOURCES_DEFAULT = [
|
|
7
|
+
{ kind: 'tgz', url: 'https://registry.npmjs.org/{name}/-/{file}-{version}.tgz' },
|
|
8
|
+
{ kind: 'file', base: 'https://cdn.jsdelivr.net/npm/{name}@{version}' },
|
|
9
|
+
{ kind: 'file', base: 'https://unpkg.com/{name}@{version}' },
|
|
10
|
+
]
|