@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/src/edge/plan.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// The rows are the desktop's planEdge (internal/engine/edge.go:403 to 445), with one browser difference: a container that
|
|
7
7
|
// asks for checkpoints still takes the path (the page refuses vm.checkpoint() while an edge program runs).
|
|
8
8
|
import { RT_DEFAULT, unavailable } from './runtime.js'
|
|
9
|
+
import { PYTHON_VERSIONS, WASM32_MAX_MB, pythonFits, pythonMayUseMB } from './python-plan.js'
|
|
9
10
|
|
|
10
11
|
/** Where images keep node, in the order the desktop looks (internal/engine/edge.go nodePaths). */
|
|
11
12
|
export const NODE_PATHS = ['/usr/local/bin/node', '/usr/bin/node']
|
|
@@ -35,6 +36,18 @@ export function nodeVersion(env) {
|
|
|
35
36
|
return /^\d+\.\d+\.\d+$/.test(v) ? 'v' + v : ''
|
|
36
37
|
}
|
|
37
38
|
|
|
39
|
+
/** The rows node and python share (the desktop's planEdge before it looks at either): '' when none refuses. */
|
|
40
|
+
function sharedRefusal(o) {
|
|
41
|
+
if (!o.enabled) return 'the switch is off'
|
|
42
|
+
if (envValue(o.env, 'WMD_EDGE') === '0') return 'the container says WMD_EDGE=0'
|
|
43
|
+
if (o.hasTmpfs) return 'tmpfs mounts'
|
|
44
|
+
if (o.readonlyRoot) return 'read-only root'
|
|
45
|
+
const user = (o.user || '').split(':')[0]
|
|
46
|
+
if (user !== '' && user !== '0' && user !== 'root') return 'not root'
|
|
47
|
+
if (o.hasJspi === false) return 'the browser lacks JSPI, wasm exceptions or SharedArrayBuffer'
|
|
48
|
+
return unavailable(o.rt || RT_DEFAULT)
|
|
49
|
+
}
|
|
50
|
+
|
|
38
51
|
/**
|
|
39
52
|
* @param {object} o
|
|
40
53
|
* @param {boolean} o.enabled the page switch (edge=1)
|
|
@@ -49,17 +62,40 @@ export function nodeVersion(env) {
|
|
|
49
62
|
*/
|
|
50
63
|
export function planEdge(o) {
|
|
51
64
|
const no = reason => ({ use: false, reason })
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
if (o.hasTmpfs) return no('tmpfs mounts')
|
|
55
|
-
if (o.readonlyRoot) return no('read-only root')
|
|
56
|
-
const user = (o.user || '').split(':')[0]
|
|
57
|
-
if (user !== '' && user !== '0' && user !== 'root') return no('not root')
|
|
58
|
-
if (o.hasJspi === false) return no('the browser lacks JSPI, wasm exceptions or SharedArrayBuffer')
|
|
59
|
-
const rtWhy = unavailable(o.rt || RT_DEFAULT)
|
|
60
|
-
if (rtWhy) return no(rtWhy)
|
|
65
|
+
const shared = sharedRefusal(o)
|
|
66
|
+
if (shared) return no(shared)
|
|
61
67
|
if (!o.nodePath) return no('no node in the image')
|
|
62
68
|
if (nodeMajor(o.env) !== NODE_MAJOR) return no(`image node major ${nodeMajor(o.env) || 'unknown'}, not ${NODE_MAJOR}`)
|
|
63
69
|
return { use: true, node: o.nodePath }
|
|
64
70
|
}
|
|
65
71
|
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* X25 gate 2, item 1: the plan for the image's python, the rows of planEdge and the python rows of the desktop's planEdge (internal/engine/edge.go):
|
|
75
|
+
* a python the image has, a module for its major.minor, a container that may hold what the frame's 32-bit python holds. What the tree and each start
|
|
76
|
+
* decide (argv, native extensions) is src/edge/python-plan.js, asked for each start.
|
|
77
|
+
* @param {object} o planEdge's `enabled`, `env`, `user`, `readonlyRoot`, `hasTmpfs`, `hasJspi`, `rt`, and
|
|
78
|
+
* @param {boolean} [o.python] false turns python in the frame off (node's switch is the other one)
|
|
79
|
+
* @param {{path: string, version: string}} [o.imagePython] the image's python (findPython) and its major.minor (imagePythonVersion), found by the caller
|
|
80
|
+
* @param {number} [o.memoryMB] the container's memory limit (0: none); [o.swapMB] its swap; [o.ramMiB] the machine's RAM (the cap follows it)
|
|
81
|
+
* @returns {{use: boolean, python?: string, version?: string, reason?: string}}
|
|
82
|
+
*/
|
|
83
|
+
export function planPython(o) {
|
|
84
|
+
const no = reason => ({ use: false, reason })
|
|
85
|
+
if (!o.enabled) return no('the switch is off')
|
|
86
|
+
if (o.python === false) return no('python in the frame is off (python: false)')
|
|
87
|
+
const shared = sharedRefusal(o)
|
|
88
|
+
if (shared) return no(shared)
|
|
89
|
+
if (!o.imagePython || !o.imagePython.path) return no('no python in the image')
|
|
90
|
+
const v = o.imagePython.version
|
|
91
|
+
if (!v) return no(`image python version unknown, the frame has ${PYTHON_VERSIONS.join(', ')}`)
|
|
92
|
+
if (!PYTHON_VERSIONS.includes(v)) return no(`image python ${v} has no module here (${PYTHON_VERSIONS.join(', ')})`)
|
|
93
|
+
if (envValue(o.env, 'WMD_EDGE_PYTHON') === '0') return no('the container says WMD_EDGE_PYTHON=0')
|
|
94
|
+
if (!pythonFits({ env: o.env, memoryMB: o.memoryMB, swapMB: o.swapMB, ramMiB: o.ramMiB, maxMB: WASM32_MAX_MB })) {
|
|
95
|
+
return no(`the container may hold ${pythonMayUseMB(o)} MB, the frame's python (32-bit) holds at most ${WASM32_MAX_MB} MB`)
|
|
96
|
+
}
|
|
97
|
+
return { use: true, python: o.imagePython.path, version: v }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The stub over the image's python, as containerBundle's `edge.tools` (the same two mounts node has: the real python beside the socket directory, the stub over it). */
|
|
101
|
+
export const pythonSpecTools = path => [{ name: 'python3', path }]
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// X25: which file is the python module of a major.minor, and what it must be. The pins (src/edge/python-pins.js, generated from the Makefile) say it:
|
|
2
|
+
// the browser's file of the minor (the module without its .debug sections: `python.wasm` of the npm package @threadedstack/wmd-python, which is also the file
|
|
3
|
+
// the page's server serves as `/wasm/python-<mm>.wasm`), with its sha256 and size. A page accepts the bytes only with exactly that sha256 and size.
|
|
4
|
+
// `from` is the sha256 of the whole module the pins also name (the one the desktop installs).
|
|
5
|
+
import { PYTHON_PINS } from './python-pins.js'
|
|
6
|
+
import { PYTHON_VERSIONS } from './python-plan.js'
|
|
7
|
+
|
|
8
|
+
export const PYTHON_MODULES = Object.fromEntries(
|
|
9
|
+
Object.entries(PYTHON_PINS)
|
|
10
|
+
.filter(([mm]) => PYTHON_VERSIONS.includes(mm)) // the minors the frame runs (python-plan.js); the pins may name more
|
|
11
|
+
.map(([mm, e]) => [mm, { file: `python-${mm}.wasm`, sha256: e.browser.sha256, size: e.browser.size, from: e.module.sha256, package: { ...e.package } }]),
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
/** {file, url, sha256, size, from, package} of the module for the image's python major.minor, or null when the frame has none. */
|
|
15
|
+
export function pythonModuleFor(version) {
|
|
16
|
+
const m = Object.hasOwn(PYTHON_MODULES, version) ? PYTHON_MODULES[version] : null
|
|
17
|
+
return m ? { ...m, url: `/wasm/${m.file}` } : null
|
|
18
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// X25 (#71): which WASIX CPython modules the page may fetch, and the bytes of each. Per Python minor: the full version, the
|
|
2
|
+
// npm package that holds the module (@threadedstack/wmd-python, built by release/python-modules.yml), and the sha256 and
|
|
3
|
+
// size of the module and of the browser's file (the module without its .debug sections, which is what the package holds as
|
|
4
|
+
// python.wasm). No file or network access here: the page code checks the bytes it fetched against these values.
|
|
5
|
+
//
|
|
6
|
+
// The data between the two marks is made, not written: the Makefile (PYTHON_SHA256_<minor> and the lines next to it) and
|
|
7
|
+
// scripts/python-wasix/build.sh (the CPython tag) are the one place of these values.
|
|
8
|
+
// node scripts/python-wasix/npm-pack.mjs pins --write rewrite the data from the Makefile and build.sh
|
|
9
|
+
// node scripts/python-wasix/npm-pack.mjs pins --check exit 1 when the data is not what they say (make python-test)
|
|
10
|
+
// Nothing imports this file yet.
|
|
11
|
+
|
|
12
|
+
// BEGIN GENERATED (node scripts/python-wasix/npm-pack.mjs pins --write)
|
|
13
|
+
export const PYTHON_PINS = {
|
|
14
|
+
'3.12': {
|
|
15
|
+
full: '3.12.14',
|
|
16
|
+
module: { sha256: '48929135ddf695c1321585601fd874ea472bf8f5fb9cd9c48e445bc021d7e666', size: 38039485 },
|
|
17
|
+
browser: { sha256: '5c8c209c63462c402cf14886c0fbde32db0d5e84a483543820fe4b5918cf83bf', size: 16451533 },
|
|
18
|
+
package: { name: '@threadedstack/wmd-python', version: '3.12.14-1', tag: 'py3.12' },
|
|
19
|
+
},
|
|
20
|
+
'3.13': {
|
|
21
|
+
full: '3.13.16',
|
|
22
|
+
module: { sha256: 'f69468363d3200ec849e908f5edd7a032098680b70e69adcbf5b4555a9bf0a13', size: 38435408 },
|
|
23
|
+
browser: { sha256: '09f5193392b77a3d91ea329194f84d534057f88e36c818febfeac14752f7c605', size: 15316646 },
|
|
24
|
+
package: { name: '@threadedstack/wmd-python', version: '3.13.16-1', tag: 'py3.13' },
|
|
25
|
+
},
|
|
26
|
+
'3.14': {
|
|
27
|
+
full: '3.14.8',
|
|
28
|
+
module: { sha256: 'b39e9374063be9f6d86e71defe076734725ebfa87ef5b5d8a9e47e82e437fb4e', size: 39826844 },
|
|
29
|
+
browser: { sha256: '9bc264aec01bec1cd44cd0c586fe27037f34a7537c02bf232aa9afabe552ac87', size: 15737310 },
|
|
30
|
+
package: { name: '@threadedstack/wmd-python', version: '3.14.8-1', tag: 'py3.14' },
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
// END GENERATED
|
|
34
|
+
|
|
35
|
+
const HEX = /^[0-9a-f]{64}$/
|
|
36
|
+
const isInt = n => Number.isSafeInteger(n) && n > 0
|
|
37
|
+
|
|
38
|
+
/** The package's registry address: scope, name and the dist-tag that names the minor. */
|
|
39
|
+
export const PYTHON_PACKAGE = '@threadedstack/wmd-python'
|
|
40
|
+
|
|
41
|
+
/** A Python minor as the pins spell it. */
|
|
42
|
+
export const isPythonMinor = v => typeof v === 'string' && /^3\.[0-9]{1,2}$/.test(v)
|
|
43
|
+
|
|
44
|
+
/** null when the pins are usable, else what is wrong and where. */
|
|
45
|
+
export function pythonPinsProblem(pins) {
|
|
46
|
+
if (!pins || typeof pins !== 'object') return 'python pins: not an object'
|
|
47
|
+
for (const [mm, e] of Object.entries(pins)) {
|
|
48
|
+
const at = `python ${mm}`
|
|
49
|
+
if (!isPythonMinor(mm)) return `${at}: not a Python minor`
|
|
50
|
+
if (!e || typeof e !== 'object') return `${at}: not an object`
|
|
51
|
+
if (typeof e.full !== 'string' || !e.full.startsWith(`${mm}.`) || !/^3\.[0-9]{1,2}\.[0-9]+$/.test(e.full)) return `${at}: full is not ${mm}.<patch>`
|
|
52
|
+
for (const k of ['module', 'browser']) {
|
|
53
|
+
const p = e[k]
|
|
54
|
+
if (!p || typeof p !== 'object') return `${at}: no ${k}`
|
|
55
|
+
if (!HEX.test(p.sha256)) return `${at}: ${k}.sha256 is not a sha256`
|
|
56
|
+
if (!isInt(p.size)) return `${at}: ${k}.size is not a size`
|
|
57
|
+
}
|
|
58
|
+
const p = e.package
|
|
59
|
+
if (!p || p.name !== PYTHON_PACKAGE) return `${at}: package.name is not ${PYTHON_PACKAGE}`
|
|
60
|
+
if (typeof p.version !== 'string' || !p.version.startsWith(`${e.full}-`) || !/^[1-9][0-9]*$/.test(p.version.slice(e.full.length + 1))) return `${at}: package.version is not ${e.full}-<revision>`
|
|
61
|
+
if (p.tag !== `py${mm}`) return `${at}: package.tag is not py${mm}`
|
|
62
|
+
}
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// X25 gate 2, item 1: the page's decision before each start of the image's python, with the desktop's rules and the same reason texts
|
|
2
|
+
// (internal/edge/pyreal.go pythonReason, pythonArgvReason, pythonNativeReason; internal/engine/edge.go findPython, imagePythonVersion,
|
|
3
|
+
// pythonVersionFromLib, edgePythonFits). The fast path's python is a 32-bit WASIX build: it cannot load native extensions (numpy,
|
|
4
|
+
// cryptography, ...), reports a 32-bit platform to pip and so makes pip look for wheels that do not exist. A start therefore stays in the
|
|
5
|
+
// container, on the image's own python, when it does package or environment work or when the container has native extensions where this
|
|
6
|
+
// python would import from. The tree is looked at for each start (the files change between two commands of one RUN).
|
|
7
|
+
//
|
|
8
|
+
// Where the browser differs from the desktop: the memory a container may hold is `maxMB`, what the frame's python can hold (the desktop's
|
|
9
|
+
// EdgePythonMaxMB is 2048; the frame's number is measured, see docs/research/browser-speed-2026-11/x25/gate2/gate2.md), and the scan is not cached
|
|
10
|
+
// (its cost is measured there too).
|
|
11
|
+
import { resolveIn } from './resolve.js'
|
|
12
|
+
import { byteStr, S_IFDIR, S_IFLNK, S_IFREG } from '../fs/pack.js'
|
|
13
|
+
|
|
14
|
+
/** Where images keep python, in the order the desktop looks (internal/engine/edge.go pythonPaths). */
|
|
15
|
+
export const PYTHON_PATHS = ['/usr/local/bin/python3', '/usr/bin/python3', '/usr/local/bin/python', '/usr/bin/python']
|
|
16
|
+
/** The python major.minor versions the frame has a module for (src/edge/python-module.js has the files): a line more for 3.13 and 3.14. */
|
|
17
|
+
export const PYTHON_VERSIONS = ['3.12']
|
|
18
|
+
/** The cap of the frame python's memory when neither the machine's RAM nor a container limit is known, MB (gate 2's number). */
|
|
19
|
+
export const PYTHON_MAX_MB = 1024
|
|
20
|
+
/** What wasm32 can hold, MB: past it (a machine or a container limit above it) the start goes to the guest (X25 gate 2b item 2). */
|
|
21
|
+
export const WASM32_MAX_MB = 4096
|
|
22
|
+
/** The smallest cap, MB: a tiny machine still holds the interpreter and what it starts with. */
|
|
23
|
+
export const PYTHON_MIN_MB = 128
|
|
24
|
+
/** The files looked at in one scan; past it the program goes to the guest (internal/edge/addons.go maxAddonScan). */
|
|
25
|
+
export const MAX_SCAN = 400000
|
|
26
|
+
|
|
27
|
+
const REAL = "the container's own python"
|
|
28
|
+
const PIP_NAME = /^pip[0-9]*(\.[0-9]+)?$/
|
|
29
|
+
const isPipTool = base => PIP_NAME.test(base) || base === 'virtualenv'
|
|
30
|
+
const base = p => String(p).split('/').filter(Boolean).pop() ?? ''
|
|
31
|
+
const dirname = p => {
|
|
32
|
+
const i = String(p).replace(/\/+$/, '').lastIndexOf('/')
|
|
33
|
+
return i <= 0 ? '/' : p.slice(0, i)
|
|
34
|
+
}
|
|
35
|
+
const join = (...a) => '/' + a.join('/').split('/').filter(Boolean).join('/')
|
|
36
|
+
/** An absolute, cleaned path: `p` from `cwd` (internal/edge abs). */
|
|
37
|
+
function abs(cwd, p) {
|
|
38
|
+
const parts = (String(p).startsWith('/') ? p : `${cwd || '/'}/${p}`).split('/')
|
|
39
|
+
const out = []
|
|
40
|
+
for (const s of parts) {
|
|
41
|
+
if (s === '' || s === '.') continue
|
|
42
|
+
if (s === '..') out.pop()
|
|
43
|
+
else out.push(s)
|
|
44
|
+
}
|
|
45
|
+
return '/' + out.join('/')
|
|
46
|
+
}
|
|
47
|
+
/** The last value of `k` in a KEY=value list, or ''. */
|
|
48
|
+
const envValue = (env, k) => {
|
|
49
|
+
for (let i = (env || []).length - 1; i >= 0; i--) if (env[i].startsWith(k + '=')) return env[i].slice(k.length + 1)
|
|
50
|
+
return ''
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** "3.13" for "3.13.15" or "3.13", or ''. */
|
|
54
|
+
export function majorMinorOf(v) {
|
|
55
|
+
const parts = String(v).split('.')
|
|
56
|
+
if (parts.length < 2) return ''
|
|
57
|
+
return /^\d+$/.test(parts[0]) && /^\d+$/.test(parts[1]) ? `${parts[0]}.${parts[1]}` : ''
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Why this python start must run the container's own python by its command line, or '' (pythonArgvReason). */
|
|
61
|
+
export function pythonArgvReason(argv) {
|
|
62
|
+
if (!argv || argv.length === 0) return ''
|
|
63
|
+
if (isPipTool(base(argv[0]))) return `pip needs ${REAL}`
|
|
64
|
+
for (let i = 1; i < argv.length; i++) {
|
|
65
|
+
const a = argv[i]
|
|
66
|
+
if (a === '--') return i + 1 < argv.length && isPipTool(base(argv[i + 1])) ? `pip needs ${REAL}` : ''
|
|
67
|
+
if (a === '-' || !a.startsWith('-')) return isPipTool(base(a)) ? `pip needs ${REAL}` : ''
|
|
68
|
+
// X25 gate 2b item 4: -i (alone or bundled: -Ei, -iu) is the interactive prompt after the program; the letters that take a value end the bundle
|
|
69
|
+
if (/^-[A-Za-z]+$/.test(a)) {
|
|
70
|
+
for (const l of a.slice(1)) {
|
|
71
|
+
if (l === 'i') return INTERACTIVE_REASON
|
|
72
|
+
if ('cmWXQ'.includes(l)) break
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (a.startsWith('-c')) return ''
|
|
76
|
+
if (a === '-m' || a.startsWith('-m')) {
|
|
77
|
+
let mod = a.slice(2)
|
|
78
|
+
if (mod === '') {
|
|
79
|
+
if (i + 1 >= argv.length) return ''
|
|
80
|
+
mod = argv[i + 1]
|
|
81
|
+
}
|
|
82
|
+
return ['pip', 'ensurepip', 'venv', 'virtualenv'].includes(mod.split('.')[0]) ? `python -m ${mod} needs ${REAL}` : ''
|
|
83
|
+
}
|
|
84
|
+
if (a === '-W' || a === '-X' || a === '-Q') i++
|
|
85
|
+
}
|
|
86
|
+
return ''
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The reason of an interactive start (X25 gate 2b item 4): the prompt is the container's own python's, not the frame's. */
|
|
90
|
+
export const INTERACTIVE_REASON = `an interactive start runs in ${REAL}`
|
|
91
|
+
|
|
92
|
+
/** Whether the command line gives python a program: a script, -c, -m or - (a program on stdin). Options alone do not. */
|
|
93
|
+
export function pythonProgramGiven(argv) {
|
|
94
|
+
for (let i = 1; i < (argv || []).length; i++) {
|
|
95
|
+
const a = argv[i]
|
|
96
|
+
if (a === '-' || !a.startsWith('-')) return true
|
|
97
|
+
if (a === '--') return i + 1 < argv.length
|
|
98
|
+
if (!/^-[A-Za-z]+$/.test(a)) continue
|
|
99
|
+
const letters = a.slice(1)
|
|
100
|
+
for (let k = 0; k < letters.length; k++) {
|
|
101
|
+
const l = letters[k]
|
|
102
|
+
if (l === 'c' || l === 'm') return true
|
|
103
|
+
if ('WXQ'.includes(l)) {
|
|
104
|
+
if (k === letters.length - 1) i++ // the value is the next argument
|
|
105
|
+
break
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return false
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** What only this start's own words say, or '': WMD_EDGE_PYTHON=0 of the container's env (the desktop's), PYTHONINSPECT, a terminal with no program. */
|
|
113
|
+
export function pythonHelloReason(hello) {
|
|
114
|
+
if (envValue(hello.env, 'WMD_EDGE_PYTHON') === '0') return 'WMD_EDGE_PYTHON=0'
|
|
115
|
+
if (envValue(hello.env, 'PYTHONINSPECT')) return INTERACTIVE_REASON
|
|
116
|
+
if (hello.tty && !pythonProgramGiven(hello.argv)) return INTERACTIVE_REASON
|
|
117
|
+
return ''
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Whether the frame's python holds what a container may (edgePythonFits): a memory limit (RAM and swap) of at most `maxMB`, or none.
|
|
122
|
+
* WMD_EDGE_PYTHON in the container's environment says 0 (guest) or 1 (frame) whatever the memory.
|
|
123
|
+
*/
|
|
124
|
+
export function pythonFits({ env = [], memoryMB = 0, swapMB = 0, ramMiB = 0, maxMB = WASM32_MAX_MB }) {
|
|
125
|
+
const mode = envValue(env, 'WMD_EDGE_PYTHON')
|
|
126
|
+
if (mode === '0') return false
|
|
127
|
+
if (mode === '1') return true
|
|
128
|
+
return pythonMayUseMB({ memoryMB, swapMB, ramMiB }) <= maxMB
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** What the container may use in the guest, MB: the smaller of its limit (memory and swap) and the machine's RAM; 0 when neither is known. */
|
|
132
|
+
export function pythonMayUseMB({ memoryMB = 0, swapMB = 0, ramMiB = 0 }) {
|
|
133
|
+
const limit = memoryMB > 0 ? memoryMB + Math.max(0, swapMB) : Infinity
|
|
134
|
+
const machine = ramMiB > 0 ? ramMiB : Infinity
|
|
135
|
+
const may = Math.min(limit, machine)
|
|
136
|
+
return Number.isFinite(may) ? may : 0
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The cap of the frame python's memory, MiB (X25 gate 2b item 2): what the container may use in the guest, not less than PYTHON_MIN_MB; the
|
|
141
|
+
* default (PYTHON_MAX_MB) when neither the RAM nor a limit is known. pythonFits has said it fits wasm32 (WASM32_MAX_MB).
|
|
142
|
+
*/
|
|
143
|
+
export function pythonCapMiB(o) {
|
|
144
|
+
const may = pythonMayUseMB(o)
|
|
145
|
+
return may > 0 ? Math.max(PYTHON_MIN_MB, Math.min(may, WASM32_MAX_MB)) : PYTHON_MAX_MB
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const look = (fs, root, p, follow = true) => {
|
|
149
|
+
try {
|
|
150
|
+
return resolveIn(fs, root, root, byteStr(p), follow)
|
|
151
|
+
} catch {
|
|
152
|
+
return null
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function isElf(fs, n) {
|
|
157
|
+
if (!n || n.type !== S_IFREG) return false
|
|
158
|
+
try {
|
|
159
|
+
const b = fs.read(n, 0, 4)
|
|
160
|
+
return b.length === 4 && b[0] === 0x7f && b[1] === 0x45 && b[2] === 0x4c && b[3] === 0x46
|
|
161
|
+
} catch {
|
|
162
|
+
return false
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** The container path of the image's python binary (an ELF file, or a link that leads to one), or '' (findPython). */
|
|
167
|
+
export function findPython(fs, root) {
|
|
168
|
+
for (const p of PYTHON_PATHS) if (isElf(fs, look(fs, root, p))) return p
|
|
169
|
+
return ''
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const utf8 = s => new TextDecoder().decode(Uint8Array.from(s, c => c.charCodeAt(0)))
|
|
173
|
+
const entries = (fs, node) => {
|
|
174
|
+
const { names, nodes } = fs.listing(node)
|
|
175
|
+
return names.map((n, i) => [utf8(n), nodes[i]])
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** "X.Y" from a lib/pythonX.Y directory in libDir, or '' (pythonVersionFromLib). */
|
|
179
|
+
function versionFromLib(fs, root, libDir) {
|
|
180
|
+
const d = look(fs, root, libDir)
|
|
181
|
+
if (!d || !d.isDir()) return ''
|
|
182
|
+
for (const [name, n] of entries(fs, d)) {
|
|
183
|
+
if (name.startsWith('python') && n.isDir()) {
|
|
184
|
+
const mm = majorMinorOf(name.slice(6))
|
|
185
|
+
if (mm) return mm
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return ''
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The major.minor of the image's python: its PYTHON_VERSION (every official python image sets it), else lib/pythonX.Y beside the binary, else '' (imagePythonVersion). */
|
|
192
|
+
export function imagePythonVersion(env, fs, root, python) {
|
|
193
|
+
const v = envValue(env, 'PYTHON_VERSION')
|
|
194
|
+
if (v) return majorMinorOf(v)
|
|
195
|
+
return versionFromLib(fs, root, join(dirname(dirname(python)), 'lib'))
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** base/lib and base/lib64 hold pythonX.Y/{site,dist}-packages (pyLibDirs). */
|
|
199
|
+
function libDirs(fs, root, baseDir) {
|
|
200
|
+
const out = []
|
|
201
|
+
for (const lib of ['lib', 'lib64']) {
|
|
202
|
+
const dir = join(baseDir, lib)
|
|
203
|
+
const d = look(fs, root, dir)
|
|
204
|
+
if (!d || !d.isDir()) continue
|
|
205
|
+
for (const [name] of entries(fs, d)) if (name.startsWith('python')) out.push(join(dir, name, 'site-packages'), join(dir, name, 'dist-packages'))
|
|
206
|
+
}
|
|
207
|
+
return out
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
class TooMany extends Error {}
|
|
211
|
+
|
|
212
|
+
/** The path of the first *.so under dir (a missing directory has none). */
|
|
213
|
+
function findSharedObject(fs, root, dir, state) {
|
|
214
|
+
const top = look(fs, root, dir)
|
|
215
|
+
if (!top || !top.isDir()) return ''
|
|
216
|
+
const walk = (node, path) => {
|
|
217
|
+
for (const [name, n] of entries(fs, node)) {
|
|
218
|
+
if (++state.n > state.max) throw new TooMany()
|
|
219
|
+
if (n.type !== S_IFDIR && name.endsWith('.so')) return path + '/' + name
|
|
220
|
+
if (n.type === S_IFDIR) {
|
|
221
|
+
const f = walk(n, path + '/' + name)
|
|
222
|
+
if (f) return f
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return ''
|
|
226
|
+
}
|
|
227
|
+
return walk(top, dir === '/' ? '' : dir)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Where the python of `hello` imports from, scanned for the *.so files of native extensions: the image's site-packages and dist-packages, the
|
|
232
|
+
* virtualenv it runs in, the user site and PYTHONPATH. Returns the reason ('native extension /path', or 'native extension scan of /dir: too many files'), or ''.
|
|
233
|
+
* @param {string} python the image's python binary (findPython): its prefix is /usr/local for /usr/local/bin/python3
|
|
234
|
+
*/
|
|
235
|
+
export function pythonNativeReason(fs, root, hello, python, { maxScan = MAX_SCAN } = {}) {
|
|
236
|
+
const dirs = []
|
|
237
|
+
const add = (...d) => dirs.push(...d)
|
|
238
|
+
if (python) add(...libDirs(fs, root, dirname(dirname(python))))
|
|
239
|
+
const venvs = new Set()
|
|
240
|
+
const venv = r => {
|
|
241
|
+
r = abs('/', r)
|
|
242
|
+
if (venvs.has(r)) return
|
|
243
|
+
venvs.add(r)
|
|
244
|
+
if (look(fs, root, join(r, 'pyvenv.cfg'))) add(...libDirs(fs, root, r))
|
|
245
|
+
}
|
|
246
|
+
const cwd = hello.cwd || '/'
|
|
247
|
+
const v = envValue(hello.env, 'VIRTUAL_ENV')
|
|
248
|
+
if (v) venv(abs(cwd, v))
|
|
249
|
+
const argv0 = (hello.argv || [])[0]
|
|
250
|
+
if (typeof argv0 === 'string' && argv0) {
|
|
251
|
+
if (argv0.includes('/')) venv(dirname(dirname(abs(cwd, argv0))))
|
|
252
|
+
else for (const d of envValue(hello.env, 'PATH').split(':')) if (d) venv(dirname(abs(cwd, d)))
|
|
253
|
+
}
|
|
254
|
+
const home = envValue(hello.env, 'HOME') || '/root'
|
|
255
|
+
add(...libDirs(fs, root, join(abs('/', home), '.local')))
|
|
256
|
+
const ub = envValue(hello.env, 'PYTHONUSERBASE')
|
|
257
|
+
if (ub) add(...libDirs(fs, root, abs(cwd, ub)))
|
|
258
|
+
for (const d of envValue(hello.env, 'PYTHONPATH').split(':')) if (d) add(abs(cwd, d))
|
|
259
|
+
const state = { n: 0, max: maxScan }
|
|
260
|
+
const seen = new Set()
|
|
261
|
+
for (const d of dirs) {
|
|
262
|
+
if (seen.has(d)) continue
|
|
263
|
+
seen.add(d)
|
|
264
|
+
try {
|
|
265
|
+
const found = findSharedObject(fs, root, d, state)
|
|
266
|
+
if (found) return `native extension ${found}`
|
|
267
|
+
} catch (e) {
|
|
268
|
+
if (e instanceof TooMany) return `native extension scan of ${d}: too many files`
|
|
269
|
+
throw e
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return ''
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/** Everything the tree decides for one start: the command line, the start's environment, the native extensions. '' when the frame may run it. */
|
|
276
|
+
export function pythonStartReason(fs, root, hello) {
|
|
277
|
+
const why = pythonArgvReason(hello.argv) || pythonHelloReason(hello)
|
|
278
|
+
if (why) return why
|
|
279
|
+
return pythonNativeReason(fs, root, hello, findPython(fs, root))
|
|
280
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// X25: where the page gets the python module of a minor when nothing of its own serves it (the default for a consumer who ran `npm install`): the npm
|
|
2
|
+
// package @threadedstack/wmd-python, the registry's tarball first, then the two CDNs that serve the same package. The bytes are accepted only with the
|
|
3
|
+
// sha256 and size of the pins (src/edge/python-pins.js); one source that fails, stalls or gives other bytes is left for the next (src/host/pinned-fetch.js).
|
|
4
|
+
import { fetchPinned } from '../host/pinned-fetch.js'
|
|
5
|
+
import { PYTHON_PINS } from './python-pins.js'
|
|
6
|
+
|
|
7
|
+
/** The file of the package that holds the module (python-pins.js: package/python.wasm). */
|
|
8
|
+
export const PYTHON_PACKAGE_FILE = 'python.wasm'
|
|
9
|
+
|
|
10
|
+
/** The sources in the order they are tried; {name} {version} {file} are the package's (pinned-fetch.js). */
|
|
11
|
+
export const PYTHON_SOURCES = [
|
|
12
|
+
{ kind: 'tgz', url: 'https://registry.npmjs.org/{name}/-/{file}-{version}.tgz' },
|
|
13
|
+
{ kind: 'file', base: 'https://cdn.jsdelivr.net/npm/{name}@{version}' },
|
|
14
|
+
{ kind: 'file', base: 'https://unpkg.com/{name}@{version}' },
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
/** {files: [{path, sha256, size}], pkg: {name, version}} for the minor, or null when the pins have none. */
|
|
18
|
+
export function pythonPinsFor(minor, pins = PYTHON_PINS) {
|
|
19
|
+
const e = Object.hasOwn(pins, minor) ? pins[minor] : null
|
|
20
|
+
if (!e) return null
|
|
21
|
+
return { files: [{ path: PYTHON_PACKAGE_FILE, sha256: e.browser.sha256, size: e.browser.size }], pkg: { name: e.package.name, version: e.package.version } }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The module's bytes, checked. -> {bytes: ArrayBuffer, source, reasons} | {error, reasons}. Never throws. `o` carries fetchPinned's test hooks
|
|
26
|
+
* (fetchFn, cacheStorage, timeoutMs, ...) and `sources` (default PYTHON_SOURCES), `pins` (default PYTHON_PINS).
|
|
27
|
+
*/
|
|
28
|
+
export async function fetchPythonModule(minor, o = {}) {
|
|
29
|
+
const p = pythonPinsFor(minor, o.pins)
|
|
30
|
+
if (!p) return { error: `no pins for python ${minor}`, reasons: [] }
|
|
31
|
+
const { pins, sources = PYTHON_SOURCES, ...rest } = o
|
|
32
|
+
const r = await fetchPinned({ ...rest, files: p.files, pkg: p.pkg, sources })
|
|
33
|
+
return r.error ? r : { bytes: r.files[PYTHON_PACKAGE_FILE], source: r.source, reasons: r.reasons }
|
|
34
|
+
}
|
package/src/edge/runtime.js
CHANGED
|
@@ -41,7 +41,17 @@ export function pageRuntime(get) {
|
|
|
41
41
|
const base = get('sdkBase')
|
|
42
42
|
// fsTime=1 (fix5 round 6): the own runtime counts its file calls (off otherwise); it travels in handIn to runtimeConfig
|
|
43
43
|
const fsTime = get('fsTime') === '1'
|
|
44
|
-
|
|
44
|
+
// edgeCounts=1 (X5 default1 item B): at the end of each start the own runtime's Workers' counters are collected (fix7 item 15; off otherwise)
|
|
45
|
+
const counts = get('edgeCounts') === '1'
|
|
46
|
+
// X5 default1 round 2 (B2.1): marks that split a start's run stage (own/marks.js), off unless edgeMarks=1
|
|
47
|
+
const marks = get('edgeMarks') === '2' ? 2 : get('edgeMarks') === '1'
|
|
48
|
+
// X5 default1 round 2 (B2.1): edgeKeepMods=0: a kept frame keeps no compiled modules (own/adapter.js keep()); on otherwise
|
|
49
|
+
const noKeep = get('edgeKeepMods') === '0'
|
|
50
|
+
// X5 net: edgeTrace=1|2 turns the runtime's trace of WASIX calls on (0 off, 1 the calls that failed, 2 every call; edge/browser/src/trace.rs)
|
|
51
|
+
const trace = Math.min(2, Math.max(0, Number(get('edgeTrace')) | 0))
|
|
52
|
+
// X5 net: edgeNet=0 turns the frame's network off (every socket call is ENOTSUP again): for the A/B of its cost and for a diagnosis
|
|
53
|
+
const noNet = get('edgeNet') === '0'
|
|
54
|
+
return { rt: parseRt(get('rt')), handIn: base || fsTime || counts || marks || noKeep || trace || noNet ? { ...(base ? handSdk(base) : {}), fsTime, ...(counts ? { counts } : {}), ...(marks ? { marks } : {}), ...(noKeep ? { keepMods: false } : {}), ...(trace ? { trace } : {}), ...(noNet ? { net: false } : {}) } : null }
|
|
45
55
|
}
|
|
46
56
|
|
|
47
57
|
/** The page option rt as a runtime name: '' or absent is the default; anything else is returned as given (unavailable() then refuses it). */
|
|
@@ -61,7 +71,8 @@ export function unavailable(rt) {
|
|
|
61
71
|
* handed one in. `handIn` is what pageRuntime returned.
|
|
62
72
|
*/
|
|
63
73
|
export function runtimeConfig(rt, handIn = null) {
|
|
64
|
-
|
|
74
|
+
// handIn.sbx: the frame the library makes itself has no site to load from: the files are handed in as bytes, under these names (src/edge/sbx/worker-entry.js)
|
|
75
|
+
if (rt === 'own') return { own: { ...(handIn && handIn.sbx ? { wasmUrl: 'sbx:wmd_browser_bg.wasm', glueUrl: 'sbx:wmd_browser.js', workerUrl: 'sbx:own-worker.js' } : { wasmUrl: '/own/wmd_browser_bg.wasm', glueUrl: '/own/wmd_browser.js', workerUrl: '/edge/own/worker.js' }), ...(handIn && handIn.fsTime ? { fsTime: true } : {}), ...(handIn && handIn.counts ? { counts: true } : {}), ...(handIn && handIn.marks ? { marks: handIn.marks } : {}), ...(handIn && handIn.keepMods === false ? { keepMods: false } : {}), ...(handIn && handIn.trace ? { trace: handIn.trace } : {}), ...(handIn && handIn.net === false ? { net: false } : {}) } }
|
|
65
76
|
if (rt === 'sdk' && handIn && handIn.rtBase) return { rtBase: handIn.rtBase }
|
|
66
77
|
return {}
|
|
67
78
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// X5 option A fix rounds A and B and speed-x5-webkit: a bound on the frames of node one page has alive at once, per engine. Measured (fixB.md; footprint of the
|
|
2
|
+
// page's process, the guest alone about 1.1 GiB, under shared load):
|
|
3
|
+
// Chromium and Firefox: a frame of node costs about 0.5 GiB; four frames 2.8 GiB (Chromium) and 3.1 GiB (Firefox). Cap 4.
|
|
4
|
+
// WebKit: the browser ends the web process (reason ExceededMemoryLimit) at 2048 MB; the page then reloads and the app loses its state. The cost was not the
|
|
5
|
+
// frame's code, it was the Worker form: a module Worker from a data: URL writes the module's whole URL into every frame of every Error stack (a 318 KB module: a
|
|
6
|
+
// stack of 424,719 characters against 141 from a blob: URL; 3000 Errors cost 1.27 GB and 1 s against 423 KB and 10 ms), and the runtime makes Errors by the
|
|
7
|
+
// thousand. The data form now runs its code as text under a short source name (stack of 499 characters). One sandboxed frame: `node -e` peak footprint 1.33 GiB
|
|
8
|
+
// (was 2.5), the `npm ci` cell finishes (checksum 31, 14.6 s, tree 3.7 GiB, footprint peak 2.6 GiB in its unpack step; was stopped at the 4.8 GiB guard).
|
|
9
|
+
// One frame is what a WebKit page can hold beside the guest, so the cap there is 1; a page that holds more says `edge: { frameCap: N }` (its own choice).
|
|
10
|
+
// A start that needs a NEW frame while the page has this many runs in the guest (the guest is always right, only slower).
|
|
11
|
+
|
|
12
|
+
/** Frames per page by engine: Chromium 4 (2.8 GiB footprint), Firefox 4 (3.1 GiB), WebKit 1 (one frame, the rest run in the guest), any other browser 1. */
|
|
13
|
+
export const FRAME_CAPS = { webkit: 1, chromium: 4, firefox: 4, other: 1 }
|
|
14
|
+
|
|
15
|
+
/** The engine a user agent text names. */
|
|
16
|
+
export function engineOf(ua = '') {
|
|
17
|
+
if (/Firefox\//.test(ua) && !/Seamonkey/.test(ua)) return /FxiOS/.test(ua) ? 'webkit' : 'firefox'
|
|
18
|
+
if (/AppleWebKit/.test(ua)) return /Chrome\/|Chromium\/|Android/.test(ua) ? 'chromium' : 'webkit' // iOS Chrome (CriOS), Edge (EdgiOS) and Firefox (FxiOS) are WebKit
|
|
19
|
+
if (!ua || /^(Node\.js|Bun|Deno)\//.test(ua)) return 'none' // no browser: no page process to bound (the unit tests)
|
|
20
|
+
return 'other'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The cap for a user agent (undefined when there is no browser: a test run in Bun or Node); `own` (a whole number from 0 up; 0 = no frame) is the page's own choice (option edge.frameCap). */
|
|
24
|
+
export function frameCapOf(ua = globalThis.navigator?.userAgent || '', own) {
|
|
25
|
+
if (Number.isInteger(own) && own >= 0) return own
|
|
26
|
+
return FRAME_CAPS[engineOf(ua)] // undefined: no bound
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The reason of a start that went to the guest because the page has its frames: a calm one, not a failure. */
|
|
30
|
+
export const FRAME_CAP_WHY = n => n === 0 ? 'node runs in the guest in this browser: its frames of node cost more memory than the browser allows a page' : `this page already has ${n} frame${n === 1 ? '' : 's'} of node alive, the most this browser holds: this one runs in the guest`
|
|
31
|
+
|
|
32
|
+
/** The Worker form (src/edge/sbx/common.js STRATEGIES) each engine takes, as an ORDER for the probe of a page's first frame: the probe tries it alone and still decides. */
|
|
33
|
+
export const STRATEGY_OF = { webkit: 'data', firefox: 'module', chromium: 'boot' }
|
|
34
|
+
|
|
35
|
+
/** The form to try first for a user agent ('' for none: every form is tried at once). */
|
|
36
|
+
export const hintOf = (ua = globalThis.navigator?.userAgent || '') => STRATEGY_OF[engineOf(ua)] || ''
|