@esp-vision/device-core 0.3.0 → 0.4.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.1 (unreleased)
4
+
5
+ ### Fixed
6
+
7
+ - Fail camera mode checks on allocation, timeout and unexpected errors instead of silently classifying those modes as unsupported; inspect optional storage state only when advertised.
8
+ - Keep hardware-check fixtures in bounded RAM-only Python calls, independent of filesystem availability. Add expected board/firmware and required-check validation, report storage mount state, and reject explicit file acceptance on unmounted Flash without formatting.
9
+
10
+ ## 0.4.0
11
+
12
+ ### Added
13
+
14
+ - Reuse browser cache only for hash-pinned release manifests and complete-BIN sources; retry a fetch-stage TypeError once with no-store for cached CORS recovery, while hash/HTTP/metadata failures and legacy app-only downloads retain strict failure/cache behavior.
15
+
16
+ - Export immutable official release-manifest readers with publication/version/board/hash pinning and bounded, source-constrained validation of firmware, website, models and MCP snapshots; model files are unique by path while a group may share one slug, and normalized paths cannot escape the pinned source commit.
17
+ - Support application updates from the qualified complete BIN through `image.source`, verifying the whole container and embedded segments before extracting the application; preserve explicit full installation and legacy app-only descriptors.
18
+
19
+ - Export passive `openBrowserRom({ port, logger?, signal? })` at `/browser` with a lazily loaded, pinned tasmota browser backend; share verified Core firmware install/update workflows without host-maintained ROM patches.
20
+ - Validate the real browser bundle, P4/S31 stub assets, adapter cleanup/cancellation and isolated package imports.
21
+
3
22
  ## 0.3.0
4
23
 
5
24
  ### Added
package/README.md CHANGED
@@ -19,10 +19,10 @@
19
19
  | Visibility | Public; installation needs no npm login |
20
20
  | Source location | `host/device-core` in the ESP-VISION firmware repository |
21
21
 
22
- The `/node` byte transports and bundled checks require 0.1.1; complete installation and `/node` `openNodeRom` are introduced in 0.3.0. Check the registry for the published version. CI authenticates with the protected `NPM_PUBLISH_TOKEN` and verifies that `npm whoami` returns `esp-vision` before publishing. Tokens are not part of this package or its documentation.
22
+ This source prepares **0.4.0 (not published by this change)** with browser ROM flashing, immutable release-manifest readers and application updates from a single complete BIN. npm 0.3.0 does not contain these APIs. The `/node` byte transports and bundled checks require 0.1.1; complete installation and `/node` `openNodeRom` are introduced in 0.3.0. Check the registry before using the 0.4.0 install examples; until publication, use a tarball packed from an exact reviewed source commit. CI authenticates with the protected `NPM_PUBLISH_TOKEN` and verifies that `npm whoami` returns `esp-vision` before publishing. Tokens are not part of this package or its documentation.
23
23
 
24
24
  ```sh
25
- npm install --save-exact @esp-vision/device-core@0.3.0 --registry=https://registry.npmjs.org/
25
+ npm install --save-exact @esp-vision/device-core@0.4.0 --registry=https://registry.npmjs.org/
26
26
  ```
27
27
 
28
28
  EV-MUX v4 firmware is required. A v3 hello is rejected with `VERSION_MISMATCH`; applications should ask the user to update firmware. Core has no runtime dependency on the IDE, Agent, DOM or Node serial libraries. The host supplies the physical byte transport and discovery adapter; Core owns protocol parsing, session negotiation, REPL coordination and device operations.
@@ -41,7 +41,7 @@ Documentation source lives in this repository under `docs/en/device-core/` and `
41
41
  The npm package installs `esp-vision-device`. `inspect` combines passive wired/LAN enumeration and targeted queries. By default it lists USB serial candidates; `--all-ports` also includes native UART/Bluetooth ports. Explicit `--port` bypasses discovery filtering; `configure`, `control`, and `firmware check/update` cover management. See the [CLI guide](https://docs.espressif.com/projects/esp-vision/en/latest/esp32p4/device-core/cli.html) ([中文](https://docs.espressif.com/projects/esp-vision/zh_CN/latest/esp32p4/device-core/cli.html)).
42
42
 
43
43
  ```sh
44
- npm install -g @esp-vision/device-core@0.3.0
44
+ npm install -g @esp-vision/device-core@0.4.0
45
45
  esp-vision-device inspect
46
46
  esp-vision-device inspect --all-ports
47
47
  esp-vision-device inspect --interface usj,usb --probe --json
@@ -110,6 +110,47 @@ try {
110
110
  }
111
111
  ```
112
112
 
113
+ ### Immutable release snapshots and a single firmware BIN (0.4.0, unpublished)
114
+
115
+ The `/firmware` entry exports `loadOfficialFirmware({ board?, version?, fetch?, signal? })` and `loadReleaseManifest({ url, sha256, version?, sourceCommit?, boards?, fetch?, signal? })`. The official reader prioritizes `[publication]` in `config.toml`, selects exactly one version reference and verifies its raw manifest SHA-256 before returning `{ schemaVersion: 1, version, sourceCommit, pipelineId, layout, releases, website, mcp?, publication }`. History references include exact board sets; unsupported board/version combinations and malformed explicit publication data reject without falling back to guessed URLs or old metadata. Configs without `[publication]` keep the legacy feed; an explicit version limits old metadata requests too.
116
+
117
+ For a website built from a verified snapshot, keep its selected publication reference rather than re-reading mutable `config.toml` when the user clicks:
118
+
119
+ ```js
120
+ import { loadReleaseManifest } from '@esp-vision/device-core/firmware';
121
+ const snapshot = await loadReleaseManifest({
122
+ url: reference.manifestUrl, sha256: reference.manifestSha256,
123
+ version: reference.version, boards: reference.boards,
124
+ });
125
+ const release = snapshot.releases.find(entry => entry.board === selectedBoard);
126
+ // Pass release to createDevice().firmware.update(); the website never slices BINs.
127
+ ```
128
+
129
+ In `layout: 'single-bin'`, `image.source = { url, bytes, sha256, offset }` describes the complete BIN; `image.url` equals its URL, while `image.bytes/sha256` still describe only the application. The source URL/length/hash must match `install`, and its offset must equal the application's flash offset. Core downloads the full container once (no Range request), validates its whole hash and embedded bootloader/partition/application hashes, then extracts and writes only the application in default update mode. Existing partition checks and full application readback remain mandatory; `mode: 'install'` writes the qualified complete BIN. With `image.source`, supplied `data` must also be the complete BIN; old application-only metadata keeps its existing raw-app `data` semantics. Manifests are bounded at 4 MiB, firmware downloads at 256 MiB and their approved exact size. Official manifest/artifact sources are confined to the ESP-VISION CDN root; model URLs use their full source commit and MCP has a version/commit/hash descriptor. No npm, firmware tag or CDN publication is performed by preparing this source version.
130
+
131
+ To validate before npm publication, check out an exact reviewed commit, run `npm install --ignore-scripts --no-audit --no-fund --package-lock=false` and `npm run release:check` in `host/device-core`, then `npm pack --ignore-scripts --pack-destination <project-local-output>`. Install that locally produced tarball or use a source archive pinned to that exact commit. Do not claim that an unpublished 0.4.0 is available in the registry.
132
+
133
+ ### Browser ROM flashing (unreleased)
134
+
135
+ `@esp-vision/device-core/browser` exports `openBrowserRom({ port, logger?, signal? })`. Build from this branch until a new SDK release is published; installing 0.3.0 does not enable this API. The constructor is passive, and `initialize()` lazily loads the pinned `tasmota-webserial-esptool` browser backend. Use a browser bundler such as webpack, Vite or esbuild; direct native ESM loading of the upstream backend is not supported. No Python installation or consumer-side tasmota patch is needed. The root and `/node` entry points do not load browser modules.
136
+
137
+ ```js
138
+ import { createDevice } from '@esp-vision/device-core';
139
+ import { openBrowserRom } from '@esp-vision/device-core/browser';
140
+
141
+ // Request permission directly in the user's click handler (HTTPS or localhost).
142
+ const port = await navigator.serial.requestPort();
143
+ // release comes from firmware.check(), selected for the physical board.
144
+ const result = await createDevice().firmware.update({
145
+ release, board: release.board, mode: 'update', // Explicit 'install' for complete images.
146
+ openLoader: ({ signal }) => openBrowserRom({ port, signal, logger: console }),
147
+ onProgress: percent => updateProgress(percent),
148
+ });
149
+ if (result.needsReset) showManualReset();
150
+ ```
151
+
152
+ Core owns downloads, chip/revision/security and partition/capacity checks, write, full readback and reset. The adapter owns the selected port until Core disconnects it; the UI must first release any runtime serial connection. It starts no stub during construction or chip initialization: Core checks the approved image and ROM security before calling `runStub`. P4 revision 301/302 flash power and S31 support come from pinned upstream 9.4.2; Core retains bounded SLIP-prefix tolerance, native USB stub startup confirmation and size-scaled erase timeouts. Unsupported stubs fail explicitly. Keep the page and port alive until the update Promise settles; cancellation is accepted before writing, never used to interrupt a started write/readback. This ROM adapter does not provide runtime EV-MUX serial transport or Wi-Fi/TCP OTA.
153
+
113
154
  ### Official Node adapters
114
155
 
115
156
  The Node-only `@esp-vision/device-core/node` subpath exports `tcpTransport({ host, port })` and `serialTransport({ serial, python })`. Constructors are passive; Core opens/closes the link during `connect()`/`disconnect()`. Serial needs pyserial in the selected Python interpreter, and uses role `usj`, `usb` or `uart` for the actual interface. The root/browser entry points do not load Node networking/process modules.
@@ -343,3 +384,5 @@ TCP needs only Node.js. Serial additionally needs a Python interpreter with the
343
384
  ## License
344
385
 
345
386
  The npm package and ESP-VISION contributions use [Apache-2.0](LICENSE), matching the firmware. [NOTICE](NOTICE) retains the original copyright and MIT permission text for historical and upstream-derived portions. Third-party dependencies retain their own licenses; neither package metadata nor the project license overrides them.
387
+
388
+ Hash-pinned manifests and complete-BIN sources use the browser HTTP cache (`cache: default`); mutable config and legacy app-only BINs retain `no-store`. A fetch-stage TypeError on a pinned asset gets one `no-store` retry to recover from an older cached response lacking CORS headers. HTTP, body-read, SHA-256 and metadata failures are not retried or downgraded.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@esp-vision/device-core",
3
- "version": "0.3.0",
3
+ "version": "0.4.1",
4
4
  "description": "Shared EV-MUX, framed REPL, device session, and action contracts for ESP Vision clients.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -10,10 +10,11 @@
10
10
  "./session": "./src/device_session.js",
11
11
  "./actions": "./src/device_actions.js",
12
12
  "./bindings": "./src/device_bindings.js",
13
- "./firmware": "./src/firmware_flash.js",
13
+ "./firmware": "./src/firmware.js",
14
14
  "./discovery": "./src/network_discovery.js",
15
15
  "./discovery/node": "./src/network_discovery_node.js",
16
- "./node": "./src/node.js"
16
+ "./node": "./src/node.js",
17
+ "./browser": "./src/browser.js"
17
18
  },
18
19
  "files": [
19
20
  "src/*.js",
@@ -33,10 +34,11 @@
33
34
  "check:schema": "node scripts/generate-action-schema.mjs --check",
34
35
  "schema:actions": "node scripts/generate-action-schema.mjs",
35
36
  "check:package": "node tests/check-package.mjs",
36
- "release:check": "npm test && npm run check:schema && npm run check:package && node scripts/release-npm.mjs --dry-run",
37
+ "release:check": "npm test && npm run check:schema && npm run check:browser && npm run check:package && node scripts/release-npm.mjs --dry-run",
37
38
  "release:npm": "node scripts/release-npm.mjs",
38
39
  "prepublishOnly": "npm run release:check",
39
- "test:hardware": "node tests/hardware-check.mjs"
40
+ "test:hardware": "node tests/hardware-check.mjs",
41
+ "check:browser": "node tests/check-browser.mjs"
40
42
  },
41
43
  "repository": {
42
44
  "type": "git",
@@ -74,6 +76,10 @@
74
76
  },
75
77
  "dependencies": {
76
78
  "smol-toml": "^1.7.0",
77
- "string-width": "^7.2.0"
79
+ "string-width": "^7.2.0",
80
+ "tasmota-webserial-esptool": "9.4.2"
81
+ },
82
+ "devDependencies": {
83
+ "esbuild": "0.25.12"
78
84
  }
79
85
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "title": "ESP Vision Device Actions",
4
- "deviceCoreVersion": "0.3.0",
4
+ "deviceCoreVersion": "0.4.1",
5
5
  "schemaVersion": 1,
6
6
  "actions": [
7
7
  {
package/src/browser.js ADDED
@@ -0,0 +1,61 @@
1
+ // SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /** Passive browser ROM adapter. Request the SerialPort in the UI's click handler. */
5
+ export function openBrowserRom({ port, logger = {}, signal } = {}) {
6
+ if (!port || typeof port.open !== 'function' || typeof port.close !== 'function') throw new TypeError('A browser SerialPort is required')
7
+ if (signal !== undefined && (!signal || typeof signal.addEventListener !== 'function' || typeof signal.aborted !== 'boolean')) throw new TypeError('signal must be an AbortSignal')
8
+ let raw, initialized, closing = false, cleanup
9
+ const closed = () => Object.assign(new Error('Browser ROM adapter is closed'), { code: 'ROM_CLOSED' })
10
+ const cancelled = () => signal.reason instanceof Error ? signal.reason : Object.assign(new Error('Firmware operation cancelled'), { name: 'AbortError' })
11
+ const check = () => { if (closing) throw closed(); if (signal?.aborted) throw cancelled() }
12
+ const disconnect = () => {
13
+ closing = true
14
+ // Opening may still be in flight. initialize closes again after acquiring it.
15
+ if (!raw) return Promise.resolve()
16
+ return cleanup ??= raw.disconnect()
17
+ }
18
+ const ready = () => {
19
+ if (closing || !raw) throw closed()
20
+ return raw
21
+ }
22
+ const adapter = {
23
+ get chipName() { return raw?.chipName ?? null },
24
+ get chipFamily() { return raw?.chipFamily ?? null },
25
+ get chipRevision() { return raw?.chipRevision ?? null },
26
+ initialize() {
27
+ if (closing) return Promise.reject(closed())
28
+ if (initialized) return initialized
29
+ initialized = (async () => {
30
+ check()
31
+ const { createBrowserLoader } = await import('./browser_rom.js')
32
+ check()
33
+ let rejectAbort
34
+ const aborted = new Promise((_, reject) => { rejectAbort = reject })
35
+ const abort = () => { rejectAbort(cancelled()); void disconnect().catch(() => {}) }
36
+ signal?.addEventListener('abort', abort, { once: true })
37
+ const opening = (async () => {
38
+ raw = await createBrowserLoader(port, {
39
+ log: (...args) => logger.log?.(...args),
40
+ error: (...args) => logger.error?.(...args),
41
+ debug: (...args) => logger.debug?.(...args),
42
+ })
43
+ if (closing || signal?.aborted) { await disconnect(); check() }
44
+ await raw.initialize()
45
+ check()
46
+ })()
47
+ try {
48
+ if (signal?.aborted) abort()
49
+ await Promise.race([opening, aborted])
50
+ } catch (error) { await disconnect().catch(() => {}); throw error }
51
+ finally { signal?.removeEventListener('abort', abort) }
52
+ })()
53
+ return initialized
54
+ },
55
+ getSecurityInfo: () => ready().getSecurityInfo(),
56
+ runStub: skip => ready().runStub(skip),
57
+ hardReset: () => ready().hardReset(),
58
+ disconnect,
59
+ }
60
+ return adapter
61
+ }
@@ -0,0 +1,9 @@
1
+ // SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Loaded only by openBrowserRom.initialize(), never by the root/Node entry point.
4
+ import { connectWithPort } from 'tasmota-webserial-esptool'
5
+ import { browserRomCompatibility } from './browser_rom_compat.js'
6
+
7
+ export async function createBrowserLoader(port, logger) {
8
+ return browserRomCompatibility(await connectWithPort(port, logger))
9
+ }
@@ -0,0 +1,65 @@
1
+ // SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Compatibility for pinned tasmota-webserial-esptool 9.4.2. Keep changes per
4
+ // instance: consumers need neither package-manager patches nor global prototypes.
5
+ export function browserRomCompatibility(loader) {
6
+ const readPacket = loader.readPacket
7
+ loader.readPacket = async function (timeout) {
8
+ const deadline = Date.now() + timeout
9
+ for (;;) {
10
+ try { return await readPacket.call(this, Math.max(1, deadline - Date.now())) }
11
+ catch (error) {
12
+ // Only a byte before the SLIP header is safe to discard. Preserve invalid
13
+ // escapes, mid-packet errors, I/O failures and the overall timeout.
14
+ if (!/^Invalid head of packet \(/.test(error.message) || Date.now() >= deadline) throw error
15
+ }
16
+ }
17
+ }
18
+ const runStub = loader.runStub
19
+ loader.runStub = async function (skipFlashDetection = false) {
20
+ const memFinish = this.memFinish, packet = this.readPacket
21
+ const nativeUsb = this.isUsbJtagOrOtg === true
22
+ if (nativeUsb) {
23
+ // USB can reset before ROM sends MEM_END's reply. Do not claim success:
24
+ // upstream must still receive and validate OHAI from the running stub.
25
+ this.memFinish = async (...args) => {
26
+ try { return await memFinish.apply(this, args) }
27
+ catch (error) {
28
+ if (!/timed?\s*out|timeout/i.test(error.message)) throw error
29
+ }
30
+ }
31
+ this.readPacket = timeout => packet.call(this, Math.max(timeout, 5000))
32
+ }
33
+ try {
34
+ const stub = await runStub.call(this, skipFlashDetection)
35
+ if (!stub.IS_STUB) throw new Error('This device has no supported browser flash stub')
36
+ browserStubCompatibility(stub)
37
+ return stub
38
+ } finally { this.memFinish = memFinish; this.readPacket = packet }
39
+ }
40
+ return loader
41
+ }
42
+
43
+ function browserStubCompatibility(stub) {
44
+ // Official esp-flasher-stub erases the entire region in FLASH_BEGIN. Its erase
45
+ // duration must scale with image size rather than the short command timeout.
46
+ const command = stub.checkCommand
47
+ stub.checkCommand = function (opcode, buffer, checksum, timeout) {
48
+ if (opcode === 0x02 && buffer?.length >= 4) {
49
+ const size = (buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24) >>> 0
50
+ timeout = Math.max(timeout ?? 0, 3000, 30000 * size / 1000000)
51
+ }
52
+ return command.call(this, opcode, buffer, checksum, timeout)
53
+ }
54
+ // Stub commands use their own loader instance and inherit the same SLIP issue.
55
+ const readPacket = stub.readPacket
56
+ stub.readPacket = async function (timeout) {
57
+ const deadline = Date.now() + timeout
58
+ for (;;) {
59
+ try { return await readPacket.call(this, Math.max(1, deadline - Date.now())) }
60
+ catch (error) {
61
+ if (!/^Invalid head of packet \(/.test(error.message) || Date.now() >= deadline) throw error
62
+ }
63
+ }
64
+ }
65
+ }
@@ -0,0 +1,4 @@
1
+ // SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export * from './firmware_flash.js'
4
+ export { loadOfficialFirmware, loadReleaseManifest } from './firmware_feed.js'
@@ -1,7 +1,11 @@
1
1
  // SPDX-FileCopyrightText: 2026 ESP-VISION-IDE
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- const base = 'https://dl.espressif.com/AE/esp-vision/'
4
- export async function loadOfficialFirmware({ board, fetch: fetcher = globalThis.fetch, signal } = {}) {
3
+ import { firmwareSha256 } from './firmware_flash.js'
4
+ import { validateFirmwareRelease } from './firmware_release.js'
5
+ import { OFFICIAL_ROOT as base, MAX_MANIFEST_BYTES, MAX_FIRMWARE_BYTES, SHA256, SOURCE_COMMIT, firmwareUrl, downloadBytes } from './firmware_io.js'
6
+ export async function loadOfficialFirmware({ board, version, fetch: fetcher = globalThis.fetch, signal } = {}) {
7
+ if (board !== undefined && !validName(board, 96)) throw new TypeError('board must be a non-empty board string')
8
+ if (version !== undefined && !validName(version)) throw new TypeError('version must be a non-empty firmware version string')
5
9
  const { parse } = await import('smol-toml')
6
10
  const get = async (url, optional = false) => {
7
11
  signal?.throwIfAborted()
@@ -11,10 +15,23 @@ export async function loadOfficialFirmware({ board, fetch: fetcher = globalThis.
11
15
  return response
12
16
  }
13
17
  const config = parse(await (await get(base + 'config.toml')).text())
18
+ if (Object.hasOwn(config, 'publication')) {
19
+ const publication = validatePublication(config.publication)
20
+ const selected = publication.releases.find(entry => entry.version === (version ?? publication.version))
21
+ if (!selected || (board && !selected.boards.includes(board))) throw unavailable()
22
+ const manifest = await loadReleaseManifest({
23
+ url: selected.manifestUrl, sha256: selected.manifestSha256,
24
+ version: selected.version, boards: selected.boards, sourceCommit: selected.version === publication.version ? publication.sourceCommit : undefined,
25
+ fetch: fetcher, signal,
26
+ })
27
+ return { ...manifest, releases: manifest.releases.filter(entry => !board || entry.board === board), publication }
28
+ }
14
29
  const app = config['esp-vision'], web = config.website
15
30
  if (!app?.image || !Array.isArray(web?.releases) || typeof web.binaryNameTemplate !== 'string') throw new TypeError('Invalid official firmware index')
16
31
  const boards = Object.keys(app.image).filter(name => !board || name === board)
17
- const versions = [...new Set([web.release, ...web.releases].filter(value => typeof value === 'string' && value))]
32
+ const knownVersions = [...new Set([web.release, ...web.releases].filter(value => typeof value === 'string' && value))]
33
+ if (version !== undefined && !knownVersions.includes(version)) throw unavailable()
34
+ const versions = version === undefined ? knownVersions : [version]
18
35
  if (versions.length > 256 || boards.length > 64) throw new RangeError('Official firmware index exceeds limits')
19
36
  const releases = []
20
37
  // Sequential metadata requests keep the official server load bounded.
@@ -34,3 +51,99 @@ export async function loadOfficialFirmware({ board, fetch: fetcher = globalThis.
34
51
  }
35
52
  return { schemaVersion: 1, releases }
36
53
  }
54
+
55
+ function validName(value, max = 255) { return typeof value === 'string' && value.length > 0 && value.length <= max && value.trim() === value }
56
+ function unavailable() { return Object.assign(new Error('Requested firmware version is unavailable'), { code: 'FIRMWARE_VERSION_UNAVAILABLE' }) }
57
+ function descriptor(value) {
58
+ if (!value || typeof value !== 'object' || Array.isArray(value)
59
+ || !validName(value.version) || !SHA256.test(value.manifestSha256 ?? '')
60
+ || !Array.isArray(value.boards) || !value.boards.length || value.boards.length > 64
61
+ || value.boards.some(board => !validName(board, 96)) || new Set(value.boards).size !== value.boards.length) throw new TypeError('Invalid publication manifest reference')
62
+ firmwareUrl(value.manifestUrl, { official: true })
63
+ }
64
+ function validatePublication(value) {
65
+ if (!value || value.schemaVersion !== 1 || !SOURCE_COMMIT.test(value.sourceCommit ?? '')
66
+ || !Array.isArray(value.releases) || !value.releases.length || value.releases.length > 256) throw new TypeError('Invalid official publication index')
67
+ if (!validName(value.version) || !SHA256.test(value.manifestSha256 ?? '')) throw new TypeError('Invalid publication current manifest reference')
68
+ firmwareUrl(value.manifestUrl, { official: true })
69
+ const seen = new Set()
70
+ for (const item of value.releases) {
71
+ descriptor(item)
72
+ if (seen.has(item.version)) throw new TypeError('Duplicate publication version')
73
+ seen.add(item.version)
74
+ }
75
+ const current = value.releases.find(item => item.version === value.version)
76
+ if (!current || current.manifestUrl !== value.manifestUrl || current.manifestSha256 !== value.manifestSha256) throw new TypeError('Publication current manifest reference differs from its history')
77
+ return structuredClone(value)
78
+ }
79
+ function validateWebsite(website, version, sourceCommit) {
80
+ if (!website || typeof website !== 'object' || Array.isArray(website)
81
+ || typeof website.schemaVersion !== 'string' || !/^\d+\.\d+\.\d+$/.test(website.schemaVersion)
82
+ || website.release !== version || !Array.isArray(website.releases) || website.releases.length > 256
83
+ || !website.releases.includes(version) || website.releases.some(item => !validName(item))
84
+ || typeof website.binaryNameTemplate !== 'string' || website.binaryNameTemplate.length > 255
85
+ || !website.binaryNameTemplate.includes('{board}') || !website.binaryNameTemplate.includes('{tag}')
86
+ || !Array.isArray(website.boards) || website.boards.length > 64
87
+ || !Array.isArray(website.models) || website.models.length > 256) throw new TypeError('Invalid release manifest website snapshot')
88
+ const boards = new Set(), models = new Set()
89
+ for (const board of website.boards) {
90
+ if (!board || !validName(board.board, 96) || !validName(board.slug, 96) || !validName(board.name)
91
+ || typeof board.flashable !== 'boolean' || boards.has(board.board)) throw new TypeError('Invalid or duplicate manifest website board')
92
+ if (board.docsUrl !== undefined) firmwareUrl(board.docsUrl)
93
+ boards.add(board.board)
94
+ }
95
+ for (const model of website.models) {
96
+ if (!model || !validName(model.slug, 96) || models.has(model.path)
97
+ || !Number.isSafeInteger(model.sizeBytes) || model.sizeBytes <= 0 || model.sizeBytes > MAX_FIRMWARE_BYTES
98
+ || typeof model.path !== 'string' || !model.path.startsWith('models/') || model.path.length > 512
99
+ || model.path.split('/').some(part => !part || part === '.' || part === '..')
100
+ || model.downloadUrl !== `https://raw.githubusercontent.com/espressif/esp-vision/${sourceCommit}/${model.path}`) throw new TypeError('Invalid manifest model or unpinned model URL')
101
+ const modelUrl = firmwareUrl(model.downloadUrl)
102
+ if (modelUrl.search || !modelUrl.pathname.startsWith(`/espressif/esp-vision/${sourceCommit}/models/`)
103
+ || /%2f|%5c|%2e/i.test(modelUrl.pathname)) throw new TypeError('Invalid manifest model path or source commit boundary')
104
+ models.add(model.path)
105
+ }
106
+ }
107
+ function validateManifest(value, { version, sourceCommit, boards } = {}) {
108
+ if (!value || value.schemaVersion !== 1 || !validName(value.version)
109
+ || !['legacy', 'single-bin'].includes(value.layout)
110
+ || !SOURCE_COMMIT.test(value.sourceCommit ?? '') || !Number.isSafeInteger(value.pipelineId) || value.pipelineId <= 0
111
+ || (version !== undefined && value.version !== version) || (sourceCommit !== undefined && value.sourceCommit !== sourceCommit)
112
+ || !Array.isArray(value.releases) || !value.releases.length || value.releases.length > 64) throw new TypeError('Invalid release manifest identity or bounds')
113
+ const seen = new Set()
114
+ for (const entry of value.releases) {
115
+ validateFirmwareRelease(entry)
116
+ if (entry.image.bytes > MAX_FIRMWARE_BYTES || (entry.install && entry.install.bytes > MAX_FIRMWARE_BYTES)) throw new RangeError('Manifest firmware image exceeds byte limit')
117
+ if (entry.version !== value.version || (entry.sourceCommit !== undefined && entry.sourceCommit !== value.sourceCommit) || seen.has(entry.board)) throw new TypeError('Release manifest board/version mismatch or duplicate board')
118
+ seen.add(entry.board)
119
+ if ((value.layout === 'single-bin') !== (entry.image.source !== undefined)) throw new TypeError('Release manifest BIN layout/source mismatch')
120
+ firmwareUrl(entry.image.url, { official: true })
121
+ if (entry.image.source) firmwareUrl(entry.image.source.url, { official: true })
122
+ if (entry.install) { validateFirmwareRelease(entry, 'install'); firmwareUrl(entry.install.url, { official: true }) }
123
+ }
124
+ if (boards !== undefined && (boards.length !== seen.size || boards.some(board => !seen.has(board)))) throw new TypeError('Release manifest board set differs from publication reference')
125
+ validateWebsite(value.website, value.version, value.sourceCommit)
126
+ if (value.website.boards.some(board => board.flashable && !seen.has(board.board))) throw new TypeError('Manifest website advertises an unqualified board')
127
+ if (value.mcp !== undefined && value.mcp !== null) {
128
+ const mcp = value.mcp
129
+ if (!mcp || typeof mcp !== 'object' || Array.isArray(mcp)
130
+ || !Number.isSafeInteger(mcp.bytes) || mcp.bytes <= 0 || mcp.bytes > 16 * 1024 * 1024
131
+ || !SHA256.test(mcp.sha256 ?? '') || mcp.version !== value.version || mcp.sourceCommit !== value.sourceCommit) throw new TypeError('Invalid release manifest MCP descriptor')
132
+ firmwareUrl(mcp.url, { official: true })
133
+ const mcpPath = new URL(mcp.url).pathname
134
+ if (!mcpPath.startsWith('/AE/esp-vision/bin/mcp/') || decodeURIComponent(mcpPath.slice(mcpPath.lastIndexOf('/') + 1)) !== `mcp-pack-${value.version}.json`) throw new TypeError('Invalid release manifest MCP source')
135
+ }
136
+ return value
137
+ }
138
+
139
+ /** Read exactly one immutable official release snapshot, verified over raw bytes. */
140
+ export async function loadReleaseManifest({ url, sha256, version, sourceCommit, boards, fetch: fetcher = globalThis.fetch, signal } = {}) {
141
+ if (!SHA256.test(sha256 ?? '') || (version !== undefined && !validName(version))
142
+ || (sourceCommit !== undefined && !SOURCE_COMMIT.test(sourceCommit))
143
+ || (boards !== undefined && (!Array.isArray(boards) || !boards.length || boards.length > 64 || boards.some(board => !validName(board, 96)) || new Set(boards).size !== boards.length))) throw new TypeError('Manifest requires a qualified SHA-256, version and source commit')
144
+ const bytes = await downloadBytes(url, { fetch: fetcher, signal, maxBytes: MAX_MANIFEST_BYTES, official: true, immutable: true })
145
+ if (await firmwareSha256(bytes) !== sha256) throw new Error('Release manifest SHA-256 does not match publication')
146
+ signal?.throwIfAborted()
147
+ const value = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes))
148
+ return validateManifest(value, { version, sourceCommit, boards })
149
+ }
@@ -0,0 +1,68 @@
1
+ // SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export const MAX_FIRMWARE_BYTES = 256 * 1024 * 1024
4
+ export const MAX_MANIFEST_BYTES = 4 * 1024 * 1024
5
+ export const OFFICIAL_ROOT = 'https://dl.espressif.com/AE/esp-vision/'
6
+ export const SHA256 = /^[a-f0-9]{64}$/
7
+ export const SOURCE_COMMIT = /^[a-f0-9]{40}$/
8
+
9
+ export function firmwareUrl(value, { official = false } = {}) {
10
+ if (typeof value !== 'string') throw new TypeError('Firmware URLs must use HTTPS')
11
+ let url
12
+ try { url = new URL(value) } catch { throw new TypeError('Firmware URLs must use HTTPS') }
13
+ if (url.protocol !== 'https:' || url.username || url.password || url.hash
14
+ || (official && (!url.href.startsWith(OFFICIAL_ROOT) || url.search || /%2f|%5c|%2e/i.test(url.pathname)))) throw new TypeError('Invalid firmware HTTPS URL or official source')
15
+ return url
16
+ }
17
+
18
+ export async function responseBytes(response, { maxBytes, expectedBytes, signal } = {}) {
19
+ const limit = Math.min(maxBytes, expectedBytes ?? maxBytes)
20
+ const advertised = response.headers?.get?.('content-length')
21
+ if (advertised !== null && advertised !== undefined && (!/^\d+$/.test(advertised) || Number(advertised) > limit)) throw new RangeError('Firmware response exceeds approved byte limit')
22
+ let bytes
23
+ if (response.body?.getReader) {
24
+ const reader = response.body.getReader(), chunks = []
25
+ let length = 0
26
+ const abort = () => { void reader.cancel().catch(() => {}) }
27
+ signal?.addEventListener('abort', abort, { once: true })
28
+ try {
29
+ for (;;) {
30
+ signal?.throwIfAborted()
31
+ const { done, value } = await reader.read()
32
+ if (done) break
33
+ length += value.byteLength
34
+ if (length > limit) throw new RangeError('Firmware response exceeds approved byte limit')
35
+ chunks.push(value)
36
+ }
37
+ bytes = new Uint8Array(length)
38
+ let offset = 0
39
+ for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.byteLength }
40
+ } catch (error) { await reader.cancel().catch(() => {}); throw error }
41
+ finally { signal?.removeEventListener('abort', abort); reader.releaseLock() }
42
+ } else if (response.arrayBuffer) bytes = new Uint8Array(await response.arrayBuffer())
43
+ else if (response.text) bytes = new TextEncoder().encode(await response.text())
44
+ else throw new TypeError('Firmware response must expose bytes')
45
+ signal?.throwIfAborted()
46
+ if (bytes.byteLength > limit) throw new RangeError('Firmware response exceeds approved byte limit')
47
+ if (expectedBytes !== undefined && bytes.byteLength !== expectedBytes) throw new Error('Firmware response size does not match the approved build')
48
+ return bytes
49
+ }
50
+
51
+ export async function downloadBytes(url, { fetch: fetcher = globalThis.fetch, signal, maxBytes, expectedBytes, official = false, immutable = false } = {}) {
52
+ firmwareUrl(url, { official })
53
+ if (typeof fetcher !== 'function') throw new TypeError('Provide fetch or in-memory firmware bytes')
54
+ signal?.throwIfAborted()
55
+ const request = cache => fetcher(url, { signal, cache, redirect: 'error' })
56
+ let response
57
+ try { response = await request(immutable ? 'default' : 'no-store') }
58
+ catch (error) {
59
+ // An older navigation/download response may be cached without CORS headers.
60
+ // Retry only a fetch-stage TypeError once, never HTTP, body, hash or metadata
61
+ // failures. Every successful response still undergoes the same hash checks.
62
+ if (!immutable || signal?.aborted || error?.name !== 'TypeError') throw error
63
+ response = await request('no-store')
64
+ }
65
+ if (!response.ok) throw Object.assign(new Error(`Firmware download failed: HTTP ${response.status}`), { code: 'FIRMWARE_DOWNLOAD_FAILED' })
66
+ if (response.url) firmwareUrl(response.url, { official })
67
+ return responseBytes(response, { maxBytes, expectedBytes, signal })
68
+ }
@@ -0,0 +1,47 @@
1
+ // SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { MAX_FIRMWARE_BYTES, firmwareUrl } from './firmware_io.js'
4
+
5
+ export function validateFirmwareRelease(value, mode = 'update') {
6
+ if (!value || typeof value.board !== 'string' || !value.board || typeof value.version !== 'string' || !value.version) throw new TypeError('Release requires board and version')
7
+ const image = value.image
8
+ const revisionValid = image?.chipRevision !== undefined
9
+ ? Number.isSafeInteger(image.chipRevision) && image.chipRevision >= 0
10
+ : Number.isSafeInteger(image?.chipRevisionMin) && image.chipRevisionMin >= 0
11
+ && Number.isSafeInteger(image?.chipRevisionMax) && image.chipRevisionMax >= image.chipRevisionMin
12
+ if (!image || image.evMuxVersion !== 4 || !Number.isSafeInteger(image.chipFamily)
13
+ || image.chipFamily < 0 || !revisionValid || !Number.isSafeInteger(image.offset) || image.offset < 0
14
+ || !Number.isSafeInteger(image.bytes) || image.bytes <= 0 || !Number.isSafeInteger(image.partitionSize) || image.bytes > image.partitionSize
15
+ || !/^[a-f0-9]{64}$/.test(image.sha256 ?? '') || !Number.isSafeInteger(image.partitionTable?.offset) || image.partitionTable.offset < 0
16
+ || !Number.isSafeInteger(image.partitionTable?.bytes) || image.partitionTable.bytes <= 0
17
+ || !/^[a-f0-9]{64}$/.test(image.partitionTable?.sha256 ?? '')) throw new TypeError('Release requires complete EV-MUX v4 application/chip/partition/SHA-256 metadata')
18
+ if (mode === 'install') {
19
+ const install = value.install
20
+ if (!install || install.offset !== 0 || !Number.isSafeInteger(install.bytes) || install.bytes <= 0
21
+ || !Number.isSafeInteger(install.flashSize) || install.flashSize < install.bytes || install.flashSize > 2 ** 30
22
+ || !Number.isInteger(Math.log2(install.flashSize)) || install.flashSize < 2 ** 18
23
+ || !/^[a-f0-9]{64}$/.test(install.sha256 ?? '') || !install.bootloader
24
+ || !Number.isSafeInteger(install.chipRevisionMin) || !Number.isSafeInteger(install.chipRevisionMax)
25
+ || install.chipRevisionMin < (image.chipRevision ?? image.chipRevisionMin) || install.chipRevisionMax > (image.chipRevision ?? image.chipRevisionMax)
26
+ || install.chipRevisionMin < 0 || install.chipRevisionMax < install.chipRevisionMin) throw Object.assign(new TypeError('Release lacks qualified complete-install metadata'), { code: 'FIRMWARE_INSTALL_UNAVAILABLE' })
27
+ let end = 0
28
+ for (const part of [install.bootloader, image.partitionTable, image]) {
29
+ if (!Number.isSafeInteger(part.offset) || !Number.isSafeInteger(part.bytes) || part.bytes <= 0
30
+ || part.offset < end || part.offset + part.bytes > install.bytes
31
+ || !/^[a-f0-9]{64}$/.test(part.sha256 ?? '')) throw new TypeError('Invalid complete-install segment bounds or hashes')
32
+ end = part.offset + part.bytes
33
+ }
34
+ }
35
+ if (image.source !== undefined) {
36
+ const source = image.source, install = value.install
37
+ if (!source || typeof source !== 'object' || Array.isArray(source)
38
+ || !Number.isSafeInteger(source.bytes) || source.bytes <= 0 || source.bytes > MAX_FIRMWARE_BYTES
39
+ || !Number.isSafeInteger(source.offset) || source.offset < 0 || source.offset !== image.offset
40
+ || !Number.isSafeInteger(source.offset + image.bytes) || source.offset + image.bytes > source.bytes
41
+ || !/^[a-f0-9]{64}$/.test(source.sha256 ?? '') || source.url !== image.url
42
+ || !install || source.url !== install.url || source.bytes !== install.bytes || source.sha256 !== install.sha256) throw new TypeError('Invalid complete BIN source bounds or install consistency')
43
+ firmwareUrl(source.url)
44
+ if (mode !== 'install') validateFirmwareRelease(value, 'install')
45
+ }
46
+ return structuredClone(value)
47
+ }