@threadedstack/wmd-web 0.1.39 → 0.1.42
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 +243 -1
- package/RELEASE.json +84 -49
- 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 +231 -4
- 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 +223 -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 +126 -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 +15 -2
- package/src/edge/sbx/cap.js +48 -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 +278 -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/setup.js +9 -6
- package/src/host/chan-service.js +8 -5
- package/src/host/chan-services.js +29 -4
- package/src/host/edge-host.js +58 -3
- package/wmdpack.wasm +0 -0
package/src/edge/page.js
CHANGED
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
// result. The page takes only log, step and done messages from the frame, and only from the frame's own origin
|
|
7
7
|
// and window: what a container prints reaches the page as the result of a step, never as a page message.
|
|
8
8
|
//
|
|
9
|
-
// runEdge({vm, p9port, origin, mounts, cfg, onOut, control}) -> Promise<{...done message of src/edge/worker.js}>
|
|
9
|
+
// runEdge({vm, p9port, origin, mounts, cfg, onOut, control, owner}) -> Promise<{...done message of src/edge/worker.js}>
|
|
10
|
+
// owner: the container's edgeSetup token (src/edge/setup.js): kept frames and prepared frames belong to it (none: to the document)
|
|
10
11
|
// onOut(kind, bytes): the program's output as it comes (cfg.stream); control: filled with stdin/stdinEnd/signal/abort
|
|
11
12
|
// (abort ends the frame at once; the promise rejects). releaseYield: false when the caller counts the open runs itself.
|
|
12
13
|
// rt: 'own' | 'sdk' (src/edge/runtime.js) goes to the frame in the run's config; job: the service's number of this node run (children)
|
|
@@ -18,11 +19,106 @@
|
|
|
18
19
|
// the Overlay serves there (below `root`, 'rootfs' in the guest's layout).
|
|
19
20
|
|
|
20
21
|
import { cleanRelay } from './msg.js'
|
|
22
|
+
import { kindWord } from './error-kind.js'
|
|
21
23
|
import { portsOf } from './runtime.js'
|
|
24
|
+
import { SANDBOX } from './sbx/common.js'
|
|
25
|
+
import { createSandboxFrame, linkOf } from './sbx/doc.js'
|
|
26
|
+
import { FRAME_CAP_WHY } from './sbx/cap.js'
|
|
22
27
|
|
|
23
28
|
let runSeq = 0
|
|
29
|
+
/** A sandboxed frame's code loads from the page itself and probes its Worker forms: this is how long it may take to say ready (the page's CSP, a loaded machine). */
|
|
30
|
+
export const SBX_START_MS = 15_000
|
|
31
|
+
const gotFiles = new WeakSet() // sandboxed frames the page handed their files
|
|
32
|
+
/** The reason a frame that reports what it cannot do gives (src/edge/sbx/frame.js caps): '' when it can run node. */
|
|
33
|
+
export function capsWhy(c, { pageIsolated = null } = {}) {
|
|
34
|
+
if (!c) return 'the frame reported nothing of what it can do'
|
|
35
|
+
if (c.eval === false) return "the page's Content-Security-Policy lacks 'unsafe-eval' in script-src, which the runtime needs (the frame inherits the page's policy); node runs in the guest"
|
|
36
|
+
if (!c.worker) return `the frame could not start a Worker in any form (${(c.tried || []).map(t => `${t.name}: ${t.why}`).join('; ')}); the page's policy may lack blob: or data: in worker-src`
|
|
37
|
+
if (c.sab === false || c.coi === false) {
|
|
38
|
+
// F6b: a page that is isolated whose frame is not has its Permissions-Policy header to look at (the frame has allow="cross-origin-isolated", but a header that keeps
|
|
39
|
+
// the feature to the page leaves out the frame, whose origin is opaque: only * names it)
|
|
40
|
+
if (pageIsolated === true) return 'the page is cross-origin isolated but its frame is not: the page\'s Permissions-Policy header keeps cross-origin-isolated to the page itself (for example Permissions-Policy: cross-origin-isolated=(self)); the frame\'s origin is opaque, so the header needs cross-origin-isolated=* (or no cross-origin-isolated entry); node runs in the guest'
|
|
41
|
+
return 'the frame is not cross-origin isolated (the page needs Cross-Origin-Opener-Policy same-origin and Cross-Origin-Embedder-Policy require-corp)'
|
|
42
|
+
}
|
|
43
|
+
return ''
|
|
44
|
+
}
|
|
45
|
+
/** The reason for a violation the page's own policy reported while the frame started: the directive and what it blocked. */
|
|
46
|
+
export const cspWhy = v => `the page's Content-Security-Policy refuses the frame: ${v.directive} blocked ${v.blocked || 'a url'} (script-src and worker-src need blob: and data:, frame-src needs blob:); node runs in the guest`
|
|
24
47
|
/** fix7 item 15: the frames this page made and removed (a finished program's frame is removed in `finish`); `live` is the difference. */
|
|
25
|
-
export const frameCounts = { created: 0, removed: 0 }
|
|
48
|
+
export const frameCounts = { created: 0, removed: 0, kept: 0, reused: 0, prepared: 0, stale: 0, released: 0 }
|
|
49
|
+
/**
|
|
50
|
+
* X5 default1 item A (keepFrame): the frame of a run that ended with done, kept in the page for the next run of the same site (origin -> frame).
|
|
51
|
+
* WebKit keeps the malloc pages a frame's start freed resident and takes new ones for the next new frame (default1.md A.2); a frame used again
|
|
52
|
+
* starts only a new Worker. One frame per site is kept; a run that ends any other way removes its frame.
|
|
53
|
+
* X5 fix3p P1: a kept frame belongs to the container that made it. Its key is the `owner` the run or the prepare names (the container's edgeSetup,
|
|
54
|
+
* src/edge/setup.js: the same through restores), else the page's document (runEdge alone, the unit tests). A second container of the page that
|
|
55
|
+
* names the same site makes its own frame and never finds, checks or starts another's.
|
|
56
|
+
*/
|
|
57
|
+
const keptByOwner = new WeakMap() // owner (an edgeSetup's token, else the document) -> Map(origin -> frame)
|
|
58
|
+
const keptOf = owner => {
|
|
59
|
+
let m = keptByOwner.get(owner)
|
|
60
|
+
if (!m) keptByOwner.set(owner, (m = new Map()))
|
|
61
|
+
return m
|
|
62
|
+
}
|
|
63
|
+
const takeKept = (key, origin) => {
|
|
64
|
+
const kept = keptOf(key)
|
|
65
|
+
const f = kept.get(origin)
|
|
66
|
+
kept.delete(origin)
|
|
67
|
+
return f && f.isConnected !== false && f.contentWindow ? f : null
|
|
68
|
+
}
|
|
69
|
+
const dropFrame = (f, origin) => {
|
|
70
|
+
const lk = origin === SANDBOX ? linkOf(f) : null
|
|
71
|
+
try {
|
|
72
|
+
if (lk) lk.post({ x5edge: 'stop' })
|
|
73
|
+
else f.contentWindow && f.contentWindow.postMessage({ x5edge: 'stop' }, origin) // the frame ends its Workers before it goes
|
|
74
|
+
} catch {
|
|
75
|
+
// a frame that is gone takes no messages
|
|
76
|
+
}
|
|
77
|
+
lk?.close()
|
|
78
|
+
try {
|
|
79
|
+
f.remove()
|
|
80
|
+
} catch {
|
|
81
|
+
// already out of the page
|
|
82
|
+
}
|
|
83
|
+
frameCounts.removed++
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* X5 default1 round 2 K1: which sites a machine of this page uses (owner -> Map(origin -> users); the owner is the document without one, P1).
|
|
87
|
+
* useSite(owner, origin) counts one user (a bridge, src/edge/bridge.js) and returns its release; when the last user of a site is released, the site's kept frame (and a prepare in flight for it) ends:
|
|
88
|
+
* stop posted, the frame removed, frameCounts.released. A site no user ever named keeps its frame as before (runEdge alone, the unit tests).
|
|
89
|
+
*/
|
|
90
|
+
const usersByDoc = new WeakMap() // owner (else document) -> Map(origin -> users)
|
|
91
|
+
const usersOf = doc => {
|
|
92
|
+
let m = usersByDoc.get(doc)
|
|
93
|
+
if (!m) usersByDoc.set(doc, (m = new Map()))
|
|
94
|
+
return m
|
|
95
|
+
}
|
|
96
|
+
const inUse = (key, origin) => {
|
|
97
|
+
const n = usersOf(key).get(origin)
|
|
98
|
+
return n === undefined || n > 0
|
|
99
|
+
}
|
|
100
|
+
const preparing = new WeakMap() // frame -> end(result) of its prepareEdge while it waits
|
|
101
|
+
export function useSite(key, origin) {
|
|
102
|
+
if (!key || !origin) return () => {}
|
|
103
|
+
const users = usersOf(key)
|
|
104
|
+
users.set(origin, (users.get(origin) || 0) + 1)
|
|
105
|
+
let done = false
|
|
106
|
+
return () => {
|
|
107
|
+
if (done) return
|
|
108
|
+
done = true
|
|
109
|
+
const n = (users.get(origin) || 1) - 1
|
|
110
|
+
users.set(origin, n)
|
|
111
|
+
if (n > 0) return
|
|
112
|
+
const kept = keptOf(key)
|
|
113
|
+
const f = kept.get(origin)
|
|
114
|
+
if (!f) return
|
|
115
|
+
kept.delete(origin)
|
|
116
|
+
loading.delete(f)
|
|
117
|
+
preparing.get(f)?.({ ok: false, error: 'no machine of the page uses this site any more' })
|
|
118
|
+
dropFrame(f, origin)
|
|
119
|
+
frameCounts.released++
|
|
120
|
+
}
|
|
121
|
+
}
|
|
26
122
|
|
|
27
123
|
/** The error a run ends with when the frame is lost or does not start: why is one of 'start', 'ready', 'removed', 'silent', 'timeout', 'stopped', 'pagehide', 'badmsg'. */
|
|
28
124
|
function lost(why, text) {
|
|
@@ -33,74 +129,222 @@ function lost(why, text) {
|
|
|
33
129
|
|
|
34
130
|
/** How long the page waits for the frame's page to load (fix7 F10; it was 30 s: the first node of a page whose frame site is down waited that long). */
|
|
35
131
|
export const FRAME_START_LIMIT_MS = 5000
|
|
132
|
+
/** K3: how long a kept frame has to answer the check before a start is posted to it; one that does not is removed and a new frame made. */
|
|
133
|
+
export const REUSE_CHECK_MS = 1000
|
|
134
|
+
let checkSeq = 0
|
|
135
|
+
/**
|
|
136
|
+
* X5 default1 round 2 (item 3): how long after its page's load event a frame may take to say x5-frame-ready. A site where nothing answers gives
|
|
137
|
+
* the browser's error page, which loads at once and never says ready: the run ends after this grace, not at the start limit, and the bridge marks
|
|
138
|
+
* the site down (its next runs go to the guest at once, with the reason). Chromium (hold 30: the error page's load 6 ms after the frame went in).
|
|
139
|
+
*/
|
|
140
|
+
export const FRAME_LOAD_GRACE_MS = 1500
|
|
141
|
+
/**
|
|
142
|
+
* The same for a frame whose page came (the page can no longer read its document: it is another origin's) and did not load: Firefox shows its
|
|
143
|
+
* error page that way, with no load event (hold 30, within 100 ms). A frame whose site answers loads and says ready within 100 ms of that in the
|
|
144
|
+
* three browsers (hold 30); the grace is longer than the load's, as the frame's own scripts load in it. WebKit gives a page neither for a
|
|
145
|
+
* site where nothing answers (the frame stays the page's about:blank): there the start limit decides.
|
|
146
|
+
*/
|
|
147
|
+
export const FRAME_COMMIT_GRACE_MS = 2500
|
|
148
|
+
const loaded = new WeakSet() // frames whose page fired its load event (the frame's own page or the browser's error page)
|
|
149
|
+
/** A page-side mark of the cold start (performance.mark 'x5:<name>'; read by the test driver: files-ready, frame-ready, files-handed). Costs nothing a run notices. */
|
|
150
|
+
const pmark = name => {
|
|
151
|
+
try {
|
|
152
|
+
performance.mark('x5:' + name)
|
|
153
|
+
} catch {
|
|
154
|
+
// no performance.mark: no mark
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const saidReady = new WeakSet() // frames whose page said x5-frame-ready once (a kept frame's page did, for its first run)
|
|
158
|
+
const deadFrames = new WeakMap() // frame -> why: frames that prepareEdge found without the frame; a run that took one ends at once with that text
|
|
159
|
+
const watchLoad = f => f.addEventListener?.('load', () => loaded.add(f)) // a test's fake frame may have no events
|
|
160
|
+
// the frame's page came: its document is another origin's (null); before, it is the page's about:blank. A test's fake frame has none (undefined).
|
|
161
|
+
const came = f => {
|
|
162
|
+
try {
|
|
163
|
+
return f.contentDocument === null
|
|
164
|
+
} catch {
|
|
165
|
+
return true
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// X5 fix2 F5: what is known when a frame's page loaded (or came) and its script never said ready. The page cannot tell the causes apart (the browser's
|
|
169
|
+
// error page of a server that is down; a server that answers 404 for /edge/*; a page without the COEP header, whose script the browser does not run), so
|
|
170
|
+
// the text says what is known and lists them; it never says that nothing answered.
|
|
171
|
+
const noFrameWhy = (origin, ms) => `a page loaded in the frame at ${origin} (it may be the browser's error page) and the frame's script did not say ready in ${ms} ms (possible causes: the server is down, a file of /edge/ is missing, the COOP, COEP or CORP headers are missing)`
|
|
172
|
+
// X5 fix3p P7(a): the text of the start limit: no page showed in the frame (no load, no commit) within the limit. True whatever the cause: WebKit's blank
|
|
173
|
+
// frame for a site where nothing answers, a name that does not resolve, a site the page's frame-src does not allow, a very slow site.
|
|
174
|
+
const noLoadWhy = (origin, ms) => `the frame did not load in ${ms} ms at ${origin} (nothing showed in it; possible causes: the server is down or too slow, the name does not resolve, the page's frame-src does not allow the site)`
|
|
175
|
+
// what a frame that has not said ready shows: 'load' (its page loaded), 'came' (its page came, no load), or '' (nothing yet, or it said ready once)
|
|
176
|
+
const shows = f => (saidReady.has(f) ? '' : loaded.has(f) ? 'load' : came(f) ? 'came' : '')
|
|
36
177
|
|
|
37
178
|
export function runEdge({
|
|
38
179
|
vm, job = 0, p9port, origin, mounts, cfg, root = 'rootfs', yieldMs, onLog = () => {}, onOut = null, control = null, timeoutMs = 0, releaseYield = true,
|
|
39
|
-
startTimeoutMs = FRAME_START_LIMIT_MS, readyTimeoutMs = 90_000, pingMs = 1000, pingMissMs = 10_000, tickMs = 250, now = () => Date.now(), win = globalThis, doc = globalThis.document,
|
|
180
|
+
keepFrame = false, owner = null, sbx = null, reuseCheckMs = REUSE_CHECK_MS, startTimeoutMs = FRAME_START_LIMIT_MS, loadGraceMs = FRAME_LOAD_GRACE_MS, commitGraceMs = FRAME_COMMIT_GRACE_MS, readyTimeoutMs = 90_000, pingMs = 1000, pingMissMs = 10_000, tickMs = 250, now = () => Date.now(), win = globalThis, doc = globalThis.document,
|
|
40
181
|
}) {
|
|
182
|
+
const sandbox = origin === SANDBOX // the frame the library makes itself (src/edge/sbx/): one link per frame, no window messages
|
|
183
|
+
if (sandbox) (startTimeoutMs = Math.max(startTimeoutMs, SBX_START_MS)), (loadGraceMs = Math.max(loadGraceMs, SBX_START_MS)), (commitGraceMs = Math.max(commitGraceMs, SBX_START_MS))
|
|
41
184
|
const svcMounts = Object.fromEntries(Object.entries(mounts).map(([id, m]) => [id, { path: m.path, ro: !!m.ro }]))
|
|
42
185
|
const run = ++runSeq
|
|
186
|
+
const key = owner || doc
|
|
43
187
|
const wish = portsOf(cfg.rt) // how many ports the runtime uses and whether they carry its own file messages (src/edge/runtime.js)
|
|
44
188
|
const ports = vm.edgeFs({ port: p9port, mounts: svcMounts, root, count: wish.count, own: wish.own, job, yieldMs, run, kickAll: !!globalThis.__wmdKickAll })
|
|
45
189
|
const runCfg = { ...cfg, mounts: Object.entries(mounts).map(([id, m]) => [m.at, Number(id), !!m.ro]) }
|
|
46
190
|
return new Promise((resolve, reject) => {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
191
|
+
let reused = keepFrame ? takeKept(key, origin) : null
|
|
192
|
+
// fix round item 1: a start that needs a NEW frame while the page has the most its browser holds runs in the guest (one calm reason, no frame made, the
|
|
193
|
+
// service let go); checked here, in the same tick as the frame is made, so that ten starts at once cannot all pass it
|
|
194
|
+
if (!reused && sandbox && sbx && Number.isInteger(sbx.frameCap) && frameCounts.created - frameCounts.removed >= sbx.frameCap) {
|
|
195
|
+
vm.edgeRelease?.(run)
|
|
196
|
+
if (releaseYield) vm.edgeYield(0)
|
|
197
|
+
frameCounts.capped = (frameCounts.capped || 0) + 1
|
|
198
|
+
return resolve({ fallback: true, reason: FRAME_CAP_WHY(sbx.frameCap) })
|
|
199
|
+
}
|
|
200
|
+
const fresh = () => {
|
|
201
|
+
frameCounts.created++
|
|
202
|
+
if (sandbox) {
|
|
203
|
+
const x = createSandboxFrame({ doc, win, code: sbx.code, hint: sbx.hint ? sbx.hint() : '', helloMs: sbx.helloMs })
|
|
204
|
+
watchLoad(x)
|
|
205
|
+
return x
|
|
206
|
+
}
|
|
207
|
+
const x = doc.createElement('iframe')
|
|
208
|
+
x.allow = 'cross-origin-isolated'
|
|
209
|
+
x.src = origin + '/edge/frame.html'
|
|
210
|
+
watchLoad(x)
|
|
211
|
+
return x
|
|
212
|
+
}
|
|
213
|
+
let f = reused || fresh()
|
|
214
|
+
if (reused) frameCounts.reused++
|
|
215
|
+
// K3: a kept frame is asked once (a ping with its own number) before the start is posted: its document may be gone or stuck (navigated away,
|
|
216
|
+
// its process ended), and a start posted there is lost until the ping watch gives up (pingMissMs). No answer in reuseCheckMs: it is removed
|
|
217
|
+
// and the run starts in a new frame (frameCounts.stale).
|
|
218
|
+
let check = 0
|
|
51
219
|
let ended = false
|
|
52
|
-
let sawReady = false // the frame's script runs (it posted x5-frame-ready)
|
|
220
|
+
let sawReady = false // the frame's script runs (it posted x5-frame-ready; a kept frame did so for its first run)
|
|
53
221
|
let started = false // the program runs (step ready)
|
|
54
222
|
let lastPong = 0
|
|
223
|
+
let armReady = () => {}
|
|
55
224
|
let pingSeq = 0
|
|
225
|
+
let loadSeenAt = 0 // when this run first saw the frame's page loaded (or came) without its x5-frame-ready (FRAME_LOAD_GRACE_MS, FRAME_COMMIT_GRACE_MS)
|
|
226
|
+
let seen = '' // what it saw then: 'load' or 'came'
|
|
56
227
|
const timers = []
|
|
57
228
|
const post = (msg, transfer) => {
|
|
58
229
|
try {
|
|
59
|
-
|
|
230
|
+
if (sandbox) linkOf(f)?.post(msg, transfer)
|
|
231
|
+
else f.contentWindow && f.contentWindow.postMessage(msg, origin, transfer)
|
|
60
232
|
} catch {
|
|
61
233
|
// a frame that is gone takes no messages
|
|
62
234
|
}
|
|
63
235
|
}
|
|
236
|
+
// the frame's messages: a window frame's come as page messages (checked by origin and source, below); a sandboxed frame's only on its own port
|
|
237
|
+
const attach = () => {
|
|
238
|
+
if (!sandbox) return
|
|
239
|
+
const lk = linkOf(f)
|
|
240
|
+
lk.sink(onData)
|
|
241
|
+
lk.onViolation = v => !sawReady && finish(reject, lost('runtime', cspWhy(v)))
|
|
242
|
+
lk.onSilent = why => !sawReady && finish(reject, lost('runtime', why))
|
|
243
|
+
}
|
|
244
|
+
// a start posted to the frame: a sandboxed frame is handed its files first (once; a kept frame has them), by the page, checked
|
|
245
|
+
const startRun = () => {
|
|
246
|
+
const go = () => {
|
|
247
|
+
if (ended) return
|
|
248
|
+
post({ x5edge: 'start', cfg: runCfg, ports, run }, ports)
|
|
249
|
+
armReady()
|
|
250
|
+
}
|
|
251
|
+
if (!sandbox || gotFiles.has(f)) return go()
|
|
252
|
+
const mine = f
|
|
253
|
+
sbx.files().then(
|
|
254
|
+
r => {
|
|
255
|
+
if (ended) return
|
|
256
|
+
if (r.error) return finish(reject, lost('runtime', `the files of node could not be fetched: ${r.error}`))
|
|
257
|
+
const t = Object.values(r.files)
|
|
258
|
+
pmark('files-ready')
|
|
259
|
+
linkOf(mine)?.post({ x5sbx: 'files', files: r.files }, t)
|
|
260
|
+
pmark('files-handed')
|
|
261
|
+
gotFiles.add(mine)
|
|
262
|
+
go()
|
|
263
|
+
},
|
|
264
|
+
e => finish(reject, lost('runtime', `the files of node could not be fetched: ${String((e && e.message) || e).split('\n')[0]}`)),
|
|
265
|
+
)
|
|
266
|
+
}
|
|
64
267
|
// `control` (an object the caller passes) gets stdin(bytes), stdinEnd() and signal(n) for the running program
|
|
65
268
|
if (control) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
269
|
+
// X5 fix2 F2: input is for this run only: nothing is posted once it ended (a kept frame is the next run's by then), and the message carries the
|
|
270
|
+
// run's number, which the frame checks against the run on now (frame.js)
|
|
271
|
+
const input = msg => !ended && post({ x5edge: 'in', run, in: msg })
|
|
272
|
+
control.stdin = bytes => input({ stdin: bytes })
|
|
273
|
+
control.stdinEnd = () => input({ stdinEnd: true })
|
|
274
|
+
control.signal = n => input({ signal: n })
|
|
69
275
|
control.abort = () => finish(reject, lost('stopped', 'stopped'))
|
|
70
276
|
control.alive = () => !ended && f.isConnected !== false && !!f.contentWindow
|
|
71
277
|
}
|
|
72
|
-
|
|
278
|
+
// X5 fix3p P6, what a run that ended badly leaves of its frame (pinned by test/unit/edge-x5f3p-ends.test.js, measured in three browsers):
|
|
279
|
+
// a done without `error` keeps the frame, whatever the exit code: SIGINT (130), an uncaught throw (1) and process.exit in a callback end that way;
|
|
280
|
+
// the frame ended the old Worker when it said done and starts a new one per run, and input and relays carry the run's number. A run that ends in
|
|
281
|
+
// any other way is removed with its frame: a done with `error`, a lost frame, and the cut-off after SIGKILL inside a synchronous loop
|
|
282
|
+
// (the bridge's control.abort rejects 'stopped').
|
|
283
|
+
const finish = (fn, v, keep = false) => {
|
|
73
284
|
if (ended) return
|
|
74
285
|
ended = true
|
|
75
286
|
for (const t of timers) (win.clearTimeout(t), win.clearInterval(t))
|
|
76
|
-
|
|
287
|
+
if (sandbox) linkOf(f)?.sink(null)
|
|
288
|
+
else win.removeEventListener('message', onMessage)
|
|
77
289
|
win.removeEventListener('pagehide', onHide)
|
|
78
290
|
post({ x5edge: 'stop' }) // the frame ends its worker before it goes (check3 finding 6c)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
291
|
+
if (keep && keepFrame && inUse(key, origin) && !keptOf(key).has(origin) && f.isConnected !== false && f.contentWindow) {
|
|
292
|
+
keptOf(key).set(origin, f) // keepFrame: the next run of this site starts in it
|
|
293
|
+
frameCounts.kept++
|
|
294
|
+
} else {
|
|
295
|
+
try {
|
|
296
|
+
f.remove()
|
|
297
|
+
} catch {
|
|
298
|
+
// already out of the page
|
|
299
|
+
}
|
|
300
|
+
frameCounts.removed++
|
|
83
301
|
}
|
|
84
|
-
frameCounts.removed++
|
|
85
302
|
vm.edgeRelease?.(run) // the service closes this run's ports and open files
|
|
86
303
|
if (releaseYield) vm.edgeYield(0) // the ports are closed: the service's yield goes back to its default
|
|
87
304
|
fn(v)
|
|
88
305
|
}
|
|
89
306
|
const onMessage = ({ data, origin: o, source }) => {
|
|
90
307
|
if (ended || o !== origin || source !== f.contentWindow) return
|
|
308
|
+
onData(data)
|
|
309
|
+
}
|
|
310
|
+
function onData(data) {
|
|
311
|
+
if (ended) return
|
|
312
|
+
if (sandbox && data && data.x5sbx) {
|
|
313
|
+
// the frame's first word: what it can do under the policy it runs in (a page policy that refuses eval or Workers is told here, not after a watch limit)
|
|
314
|
+
if (data.x5sbx === 'frame-failed') return finish(reject, lost('runtime', `the frame's code did not load: ${String(data.error || '').slice(0, 200)} (the page's Content-Security-Policy may refuse blob: in script-src or frame-src)`))
|
|
315
|
+
if (data.x5sbx !== 'ready') return
|
|
316
|
+
const why = capsWhy(data.caps, { pageIsolated: win.crossOriginIsolated === true })
|
|
317
|
+
if (why) return finish(reject, lost('runtime', why))
|
|
318
|
+
sbx.onCaps?.(data.caps)
|
|
319
|
+
data = 'x5-frame-ready'
|
|
320
|
+
}
|
|
321
|
+
if (check) {
|
|
322
|
+
if (!data || data.x5pong !== check) return // only the answer to this check: an old pong or another run's message says nothing of now
|
|
323
|
+
check = 0
|
|
324
|
+
sawReady = true
|
|
325
|
+
lastPong = now()
|
|
326
|
+
startRun()
|
|
327
|
+
return
|
|
328
|
+
}
|
|
91
329
|
if (data === 'x5-frame-ready') {
|
|
330
|
+
pmark('frame-ready')
|
|
331
|
+
saidReady.add(f)
|
|
92
332
|
if (sawReady) return
|
|
93
333
|
sawReady = true
|
|
94
334
|
lastPong = now()
|
|
95
|
-
|
|
335
|
+
startRun()
|
|
96
336
|
return
|
|
97
337
|
}
|
|
98
338
|
if (data === 'x5-pong') return void (lastPong = now())
|
|
339
|
+
// X5 fix2 F3: a relay must carry this run's number (frame.js tags every one it posts): a kept frame's earlier run, a frame script that tags nothing,
|
|
340
|
+
// or a text for a number is not this run's. The numberless frame messages are the ready word and the plain pong (handled above) and the prepare's result
|
|
341
|
+
// (prepareEdge's own listener): none of them is a relay.
|
|
342
|
+
if (!data || data.x5run !== run) return
|
|
99
343
|
try {
|
|
100
344
|
const r = cleanRelay(data && data.x5relay) // every field is checked here: a bad message ends this run, never the machine (check4 F2)
|
|
101
345
|
if (!r) return
|
|
102
346
|
lastPong = now()
|
|
103
|
-
if (r.type === 'done') return finish(resolve, r)
|
|
347
|
+
if (r.type === 'done') return finish(resolve, r, !r.error)
|
|
104
348
|
if (r.type === 'out') return onOut?.(r.kind, r.bytes)
|
|
105
349
|
if (r.type === 'step' && r.name === 'ready') started = true
|
|
106
350
|
onLog(r)
|
|
@@ -115,7 +359,8 @@ export function runEdge({
|
|
|
115
359
|
if (ev && ev.persisted) return
|
|
116
360
|
finish(reject, lost('pagehide', 'the page was closed or reloaded'))
|
|
117
361
|
}
|
|
118
|
-
|
|
362
|
+
if (sandbox) attach()
|
|
363
|
+
else win.addEventListener('message', onMessage)
|
|
119
364
|
win.addEventListener('pagehide', onHide)
|
|
120
365
|
// the watch: a frame that never loads (its server is down), that is taken out of the page, that stops answering, or whose program
|
|
121
366
|
// does not start must end the run now, with the reason, not leave the guest's command waiting
|
|
@@ -133,8 +378,15 @@ export function runEdge({
|
|
|
133
378
|
}, ms)
|
|
134
379
|
timers.push(t)
|
|
135
380
|
}
|
|
136
|
-
arm(startTimeoutMs, () => !sawReady && finish(reject, lost('start',
|
|
137
|
-
|
|
381
|
+
arm(startTimeoutMs, () => !sawReady && finish(reject, lost('start', shows(f) ? noFrameWhy(origin, startTimeoutMs) : noLoadWhy(origin, startTimeoutMs))))
|
|
382
|
+
// the ready limit runs from the start (a sandboxed frame's start waits for the page's fetch of the files, which has its own limits)
|
|
383
|
+
let readyArmed = false
|
|
384
|
+
armReady = () => {
|
|
385
|
+
if (readyArmed) return
|
|
386
|
+
readyArmed = true
|
|
387
|
+
arm(readyTimeoutMs, () => !started && finish(reject, lost('ready', `node did not start in ${readyTimeoutMs} ms`)))
|
|
388
|
+
}
|
|
389
|
+
if (!sandbox) armReady()
|
|
138
390
|
// no limit on the whole run unless the caller sets one (a node server or a long job runs as long as it does in the guest): the pings are what tell a dead frame
|
|
139
391
|
if (timeoutMs > 0) timers.push(win.setTimeout(() => finish(reject, lost('timeout', `the frame gave no result in ${timeoutMs} ms`)), timeoutMs))
|
|
140
392
|
let lastTick = now()
|
|
@@ -145,7 +397,15 @@ export function runEdge({
|
|
|
145
397
|
const late = t - lastTick > 2 * period
|
|
146
398
|
lastTick = t
|
|
147
399
|
if (f.isConnected === false) return finish(reject, lost('removed', 'the frame was taken out of the page'))
|
|
148
|
-
if (!sawReady)
|
|
400
|
+
if (!sawReady) {
|
|
401
|
+
if (deadFrames.has(f)) return finish(reject, lost('start', deadFrames.get(f)))
|
|
402
|
+
const now2 = shows(f)
|
|
403
|
+
if (!now2) return
|
|
404
|
+
if (now2 !== seen || late) return void ((seen = now2), (loadSeenAt = t))
|
|
405
|
+
const grace = seen === 'load' ? loadGraceMs : commitGraceMs
|
|
406
|
+
if (t - loadSeenAt > grace) finish(reject, lost('start', noFrameWhy(origin, grace)))
|
|
407
|
+
return
|
|
408
|
+
}
|
|
149
409
|
if (late) {
|
|
150
410
|
lastPong = t // the page was not awake: the frame had no chance to be heard. Ask now and give it a full window.
|
|
151
411
|
lastPing = 0
|
|
@@ -157,6 +417,169 @@ export function runEdge({
|
|
|
157
417
|
}
|
|
158
418
|
}, tickMs),
|
|
159
419
|
)
|
|
420
|
+
if (reused && !loading.has(f)) {
|
|
421
|
+
check = ++checkSeq
|
|
422
|
+
post({ x5edge: 'ping', check })
|
|
423
|
+
const t = win.setTimeout(() => {
|
|
424
|
+
if (ended || !check) return
|
|
425
|
+
check = 0
|
|
426
|
+
frameCounts.stale++
|
|
427
|
+
dropFrame(f, origin)
|
|
428
|
+
reused = null
|
|
429
|
+
f = fresh()
|
|
430
|
+
attach()
|
|
431
|
+
loadSeenAt = 0
|
|
432
|
+
seen = ''
|
|
433
|
+
doc.body.append(f)
|
|
434
|
+
}, reuseCheckMs)
|
|
435
|
+
timers.push(t)
|
|
436
|
+
} else if (!reused) doc.body.append(f)
|
|
437
|
+
// a kept frame that prepareEdge made and whose page has not loaded yet: its x5-frame-ready is still to come (onMessage starts the run then)
|
|
438
|
+
})
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const loading = new WeakSet() // frames of prepareEdge whose page has not said x5-frame-ready yet
|
|
442
|
+
/**
|
|
443
|
+
* X5 default1 round 2 (B2.2): the site's frame made and kept before the first node command (the page calls it when idle after boot). The frame is
|
|
444
|
+
* kept at once, as a finished run's frame is (keepFrame), and told to prepare: a Worker of its own fetches, checks and compiles the runtime and
|
|
445
|
+
* the program, and the frame keeps them for every start (frame.js, own/adapter.js prepare). A node command that comes meanwhile takes the frame
|
|
446
|
+
* and the frame starts it when the prepare is done. Resolves {ok, ms, error?}: when the frame answers, or {ok: false} after limitMs.
|
|
447
|
+
*/
|
|
448
|
+
export function prepareEdge({ origin, cfg, owner = null, sbx = null, doc = globalThis.document, win = globalThis, limitMs = 60_000, loadGraceMs = FRAME_LOAD_GRACE_MS, commitGraceMs = FRAME_COMMIT_GRACE_MS, startLimitMs = FRAME_START_LIMIT_MS, tickMs = 250, now = () => performance.now() }) {
|
|
449
|
+
const key = owner || doc
|
|
450
|
+
if (keptOf(key).has(origin)) return Promise.resolve({ ok: false, error: 'a frame of this site is kept already' })
|
|
451
|
+
if (!inUse(key, origin)) return Promise.resolve({ ok: false, error: 'no machine of the page uses this site' })
|
|
452
|
+
const sandbox = origin === SANDBOX
|
|
453
|
+
if (sandbox) (startLimitMs = Math.max(startLimitMs, SBX_START_MS)), (loadGraceMs = Math.max(loadGraceMs, SBX_START_MS)), (commitGraceMs = Math.max(commitGraceMs, SBX_START_MS))
|
|
454
|
+
let f
|
|
455
|
+
if (sandbox) f = createSandboxFrame({ doc, win, code: sbx.code, hint: sbx.hint ? sbx.hint() : '', helloMs: sbx.helloMs })
|
|
456
|
+
else {
|
|
457
|
+
f = doc.createElement('iframe')
|
|
458
|
+
f.allow = 'cross-origin-isolated'
|
|
459
|
+
f.src = origin + '/edge/frame.html'
|
|
460
|
+
}
|
|
461
|
+
watchLoad(f)
|
|
462
|
+
frameCounts.created++
|
|
463
|
+
frameCounts.kept++
|
|
464
|
+
frameCounts.prepared++
|
|
465
|
+
keptOf(key).set(origin, f)
|
|
466
|
+
loading.add(f)
|
|
467
|
+
const t0 = now()
|
|
468
|
+
return new Promise(resolve => {
|
|
469
|
+
let done = false
|
|
470
|
+
let timer = null
|
|
471
|
+
// item 3: a page that loads (or comes) without saying ready is no frame (nothing answers at the site): {ok: false, down: true}; the frame
|
|
472
|
+
// goes if it is still the kept one, and a run that took it meanwhile ends at once (deadFrames). The load's grace starts at the load event;
|
|
473
|
+
// a page that came without one (Firefox) is seen by a poll; a frame that shows nothing (WebKit) ends at the start limit from the frame's
|
|
474
|
+
// creation, so a node command that comes later finds the site down and runs in the guest at once.
|
|
475
|
+
let seen = ''
|
|
476
|
+
let seenAt = 0
|
|
477
|
+
let loadTimer = null
|
|
478
|
+
const dead = why => {
|
|
479
|
+
if (done || !loading.has(f)) return
|
|
480
|
+
loading.delete(f)
|
|
481
|
+
deadFrames.set(f, why)
|
|
482
|
+
if (keptOf(key).get(origin) === f) {
|
|
483
|
+
keptOf(key).delete(origin)
|
|
484
|
+
dropFrame(f, origin)
|
|
485
|
+
}
|
|
486
|
+
end({ ok: false, down: true, error: why })
|
|
487
|
+
}
|
|
488
|
+
f.addEventListener?.('load', () => {
|
|
489
|
+
if (done || !loading.has(f) || saidReady.has(f)) return
|
|
490
|
+
win.clearTimeout(loadTimer)
|
|
491
|
+
loadTimer = win.setTimeout(() => !saidReady.has(f) && dead(noFrameWhy(origin, loadGraceMs)), loadGraceMs)
|
|
492
|
+
})
|
|
493
|
+
const grace = win.setInterval(() => {
|
|
494
|
+
if (done || !loading.has(f)) return
|
|
495
|
+
if (f.isConnected === false) {
|
|
496
|
+
loading.delete(f)
|
|
497
|
+
return end({ ok: false, error: 'the frame was taken out of the page' })
|
|
498
|
+
}
|
|
499
|
+
const t = now()
|
|
500
|
+
const now2 = shows(f)
|
|
501
|
+
if (now2 === 'load') return // the load event's timer decides
|
|
502
|
+
if (!now2) {
|
|
503
|
+
if (t - t0 > startLimitMs) dead(noLoadWhy(origin, startLimitMs))
|
|
504
|
+
return
|
|
505
|
+
}
|
|
506
|
+
if (now2 !== seen) return void ((seen = now2), (seenAt = t))
|
|
507
|
+
if (t - seenAt > commitGraceMs) dead(noFrameWhy(origin, commitGraceMs))
|
|
508
|
+
}, tickMs)
|
|
509
|
+
const on = ({ data, origin: o, source }) => {
|
|
510
|
+
if (o !== origin || source !== f.contentWindow) return
|
|
511
|
+
onData(data)
|
|
512
|
+
}
|
|
513
|
+
function onData(data) {
|
|
514
|
+
if (sandbox && data && data.x5sbx) {
|
|
515
|
+
if (data.x5sbx === 'frame-failed') return end({ ok: false, errorKind: 'runtime', error: `the frame's code did not load: ${String(data.error || '').slice(0, 200)} (the page's Content-Security-Policy may refuse blob: in script-src or frame-src)` })
|
|
516
|
+
if (data.x5sbx !== 'ready') return
|
|
517
|
+
const why = capsWhy(data.caps, { pageIsolated: win.crossOriginIsolated === true })
|
|
518
|
+
if (why) return end({ ok: false, errorKind: 'runtime', error: why })
|
|
519
|
+
sbx.onCaps?.(data.caps)
|
|
520
|
+
data = 'x5-frame-ready'
|
|
521
|
+
}
|
|
522
|
+
if (data === 'x5-frame-ready') {
|
|
523
|
+
pmark('frame-ready')
|
|
524
|
+
loading.delete(f)
|
|
525
|
+
saidReady.add(f)
|
|
526
|
+
if (sandbox) {
|
|
527
|
+
// the page fetches and checks the files and hands them in; then the frame prepares (its Worker compiles the runtime and node). The limit of the
|
|
528
|
+
// prepare counts from the hand-in: a slow download is not the frame's failure (item 12), and a download that failed is the page's `files` event
|
|
529
|
+
// (src/edge/sbx/gate.js), not a runtime that did not load: the frame goes and the site is not marked down
|
|
530
|
+
win.clearTimeout(timer)
|
|
531
|
+
const filesFailed = why => {
|
|
532
|
+
if (done) return
|
|
533
|
+
if (keptOf(key).get(origin) === f) {
|
|
534
|
+
keptOf(key).delete(origin)
|
|
535
|
+
dropFrame(f, origin)
|
|
536
|
+
}
|
|
537
|
+
end({ ok: false, filesFailed: true, error: `the files of node could not be fetched: ${why}` })
|
|
538
|
+
}
|
|
539
|
+
sbx.files().then(
|
|
540
|
+
r => {
|
|
541
|
+
if (done) return
|
|
542
|
+
if (r.error) return filesFailed(r.error)
|
|
543
|
+
const lk = linkOf(f)
|
|
544
|
+
pmark('files-ready')
|
|
545
|
+
lk.post({ x5sbx: 'files', files: r.files }, Object.values(r.files))
|
|
546
|
+
pmark('files-handed')
|
|
547
|
+
gotFiles.add(f)
|
|
548
|
+
lk.post({ x5edge: 'prepare', cfg })
|
|
549
|
+
timer = win.setTimeout(() => end({ ok: false, error: `no answer in ${limitMs} ms` }), limitMs)
|
|
550
|
+
},
|
|
551
|
+
e => filesFailed(String((e && e.message) || e).split('\n')[0]),
|
|
552
|
+
)
|
|
553
|
+
return
|
|
554
|
+
}
|
|
555
|
+
try {
|
|
556
|
+
f.contentWindow.postMessage({ x5edge: 'prepare', cfg }, origin)
|
|
557
|
+
} catch {
|
|
558
|
+
end({ ok: false, error: 'the frame took no message' })
|
|
559
|
+
}
|
|
560
|
+
return
|
|
561
|
+
}
|
|
562
|
+
if (data && data.x5prepared) end({ ok: !!data.x5prepared.ok, ...(data.x5prepared.error ? { error: String(data.x5prepared.error).slice(0, 200) } : {}), ...(kindWord(data.x5prepared.errorKind) ? { errorKind: data.x5prepared.errorKind } : {}), ...(data.x5prepared.already ? { already: true } : {}) })
|
|
563
|
+
}
|
|
564
|
+
const end = r => {
|
|
565
|
+
if (done) return
|
|
566
|
+
done = true
|
|
567
|
+
if (sandbox) linkOf(f)?.sink(null)
|
|
568
|
+
else win.removeEventListener('message', on)
|
|
569
|
+
win.clearTimeout(timer)
|
|
570
|
+
win.clearInterval(grace)
|
|
571
|
+
win.clearTimeout(loadTimer)
|
|
572
|
+
preparing.delete(f)
|
|
573
|
+
resolve({ ...r, ms: Math.round(now() - t0) })
|
|
574
|
+
}
|
|
575
|
+
preparing.set(f, end)
|
|
576
|
+
if (sandbox) {
|
|
577
|
+
const lk = linkOf(f)
|
|
578
|
+
lk.sink(onData)
|
|
579
|
+
lk.onViolation = v => end({ ok: false, errorKind: 'runtime', error: cspWhy(v) })
|
|
580
|
+
lk.onSilent = why => end({ ok: false, errorKind: 'runtime', error: why })
|
|
581
|
+
} else win.addEventListener('message', on)
|
|
582
|
+
timer = win.setTimeout(() => end({ ok: false, error: `no answer in ${limitMs} ms` }), limitMs)
|
|
160
583
|
doc.body.append(f)
|
|
161
584
|
})
|
|
162
585
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// WRITTEN BY scripts/edge-pins.mjs from a build folder; never by hand, and not in git (the release adds it to the package).
|
|
2
|
+
// The files of node in the sandboxed frame: the path under a source (the option edge: { sources } of bootWmd), the role the frame gives the file, its
|
|
3
|
+
// sha256 and its size. The page accepts a file only when it has exactly this sha256 and size. The notice files travel with the rest.
|
|
4
|
+
// EDGE_PACKAGE is the package that holds these files, for the {name} {version} {file} of a registry source's url (null: none named).
|
|
5
|
+
export const EDGE_PACKAGE = { name: '@threadedstack/wmd-node', version: '0.1.42' }
|
|
6
|
+
export const EDGE_PINS = [
|
|
7
|
+
{ path: 'wmd_browser_bg.wasm', role: 'wasm', sha256: '1e3db7f2c5f4be0ee44e5f2c0205cf8ca11f906d9addd5512915fad4821661c2', size: 6334959 },
|
|
8
|
+
{ path: 'wmd_browser.js', role: 'glue', sha256: 'b389b072096881ba39b22cd705d3a230af02acc5409c61fb4f997d1081c4df6f', size: 75647 },
|
|
9
|
+
{ path: 'snippets/wasmer-napi-749f42b30f6a109f/inline0.js', role: 'snippet', sha256: '6b7322b9a8e1c49921d022621e3438a597630a14df26576a8359070e477fe5f7', size: 38363 },
|
|
10
|
+
{ path: 'acorn/acorn.mjs', role: 'acorn', sha256: '5fb5f5d2613ed04b33892f2beba7400e4d6ba624512bc0e8dfb572243b2882f3', size: 207423 },
|
|
11
|
+
{ path: 'acorn/LICENSE', role: 'notice', sha256: '76a876cf886ff9be2a8b5e2e86514fed06223c8c9f0c1e9ee9606e93841e00b7', size: 1099 },
|
|
12
|
+
{ path: 'NOTICES.txt', role: 'notice', sha256: '8ceca5406ee5833737bd5fa4a41d434bf5e4d26eff6da1e42c693176ee113101', size: 640571 },
|
|
13
|
+
{ path: 'edgejs.wasm', role: 'edgejs', sha256: '7131008ab38d52ff31b0d3059c6e105e8a448775408981956ce7aff792bfd98b', size: 59169054 },
|
|
14
|
+
{ path: 'edgejs.NOTICES.txt', role: 'notice', sha256: '7ae1af355689c49eae0a3e1ca3ea85a7d0793100f6bd7f9dabc7916e191e0421', size: 363824 },
|
|
15
|
+
{ path: 'linux.cjs', role: 'linux', sha256: '77bb31f2d92a36f8a76bb129187febd253ce79e1c1dc9d4bccc87474075fef3f', size: 87706 },
|
|
16
|
+
{ path: 'linux.py', role: 'linuxpy', sha256: '5f7ec28282f78691a4d84e37aeb8197cc5aa1888ffb2034f73e4e9c80faf9e22', size: 27198 },
|
|
17
|
+
]
|
package/src/edge/pins.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// The pins of the files of node in the sandboxed frame are GENERATED text (src/edge/pins.gen.js, written by scripts/edge-pins.mjs from a build folder,
|
|
2
|
+
// ignored by git, added to the package by the release), never written by hand. It holds EDGE_PINS (the path under a source, the role the frame gives
|
|
3
|
+
// the file, its sha256 and its size: the page accepts a file only with exactly this sha256 and size; the notice files travel with the rest) and
|
|
4
|
+
// EDGE_PACKAGE ({name, version} of the package that holds those files, for the {name} {version} {file} of a source's url, or null).
|
|
5
|
+
// A checkout or a package WITHOUT the file still boots: loadEdgePins() answers null and node runs in the guest, with one calm reason.
|
|
6
|
+
/** The reason of a setup that has no pins (the file is not there). */
|
|
7
|
+
export const EDGE_PINS_MISSING = "this build has no pins for node's files"
|
|
8
|
+
|
|
9
|
+
/** -> {pins, pkg} or null. `importer` is a test hook for the import of the generated file. Never throws. */
|
|
10
|
+
export async function loadEdgePins(importer = () => import('./pins.gen.js')) {
|
|
11
|
+
let m
|
|
12
|
+
try {
|
|
13
|
+
m = await importer()
|
|
14
|
+
} catch {
|
|
15
|
+
return null
|
|
16
|
+
}
|
|
17
|
+
if (!m || !Array.isArray(m.EDGE_PINS) || !m.EDGE_PINS.length) return null
|
|
18
|
+
const p = m.EDGE_PACKAGE
|
|
19
|
+
const pkg = p && typeof p.name === 'string' && p.name && typeof p.version === 'string' && p.version ? { name: p.name, version: p.version } : null
|
|
20
|
+
return { pins: m.EDGE_PINS, pkg }
|
|
21
|
+
}
|