Browser API
The npm package nanoflash exports a TypeScript API with no runtime dependency, and
nanoflash/button registers the <nanoflash-button> element. Use the browser
package shows how to use them.
Functions
| Export | Role |
|---|---|
isSupported(): boolean | Whether the browser has Web Serial. |
prepare(manifestUrl): Promise<Prepared> | Reads the manifest and starts to load the WebAssembly module. Call it before the click. |
detect({ protocol, port?, onProgress? }): Promise<ChipInformation> | Tells what chip the device on a port is. Without port, asks the user to choose one. |
checkLayout(text, images, name?, options?): Promise<LayoutReport> | Checks a layout. images is a list of { partition, size }. |
writeLayout(text, format, name?, options?): Promise<Uint8Array> | Writes a table: "espressif", "mbr", "linker" or "constants". |
loadBackend(protocol): Promise<BackendModule> | Imports the WebAssembly module of a protocol. |
NanoflashError | The class of every failure. |
camelize, fromWasm | Helpers that turn the values of the library into the types of the package. |
protocol is "espressif", "samba", "stk500v1", "stk500v2" or "avr109". The three AVR
protocols share the avr module.
Prepared
| Member | Value |
|---|---|
manifestUrl | The URL of the manifest. Paths in the manifest are resolved against it. |
protocols | The protocols of the boards. |
filters | Filters for requestPort: the devices of the boards. Empty when any board lists none. |
bootloaderFilters | The devices of the boards in their bootloader (bootloader-devices). |
promptErase | Whether to offer to erase the whole flash (prompt-erase, new_install_prompt_erase). |
flash(options?): Promise<FlashReport> | Asks the user to choose a port, filtered by the devices of the manifest, before its first await, then flashes. |
FlashOptions
| Option | Value |
|---|---|
port | The port to use. Without it, the user is asked to choose one. |
boardIdentifier | The board of the manifest to flash. Without it, the board that the chip is. |
eraseAll | Erase the whole flash first. |
verify | Read back and check what was written. Default true. |
baudRate | The baud rate to flash at. USB-Serial-JTAG boards and the ESP32-C2 stay at 115200 baud (Espressif). |
onProgress(event) | Receives a ProgressEvent: stage (connecting, erasing, writing, verifying, resetting), offset, done and total in bytes. |
onMessage(text) | Receives the notes of the library, and the warnings about the flash size. |
FlashReport
| Field | Value |
|---|---|
board | The identifier of the board. |
chip | The ChipInformation. |
images | The images written: label, offset, size. |
verified | Whether the flash was read back and checked. |
warnings | A list of warnings, each with a kind and the message to show. |
Warning kinds and their fields:
| Kind | Fields |
|---|---|
partition-table-differs | partition |
partition-table-not-checked | partition |
not-verified-in-secure-download-mode | |
erases-to-end | from, to, partitions |
partitions-grew | flashSize, minimumFlashSize, partitions (each name, sizeAtMinimum, size), protocol |
partitions-moved | flashSize, minimumFlashSize, partitions (each name, offsetAtMinimum, offset) |
flash-size-not-detected | minimumFlashSize |
See Errors and warnings for their meaning.
ChipInformation
| Field | Value |
|---|---|
protocol | The protocol. |
chip | The chip, such as esp32-s3. |
flashSize | The flash size in bytes, or null when the chip does not tell. |
secureDownloadMode | Whether an Espressif chip is in secure download mode. |
usb | { vendor, product, serialNumber }, or null. |
writableFrom | The lowest offset that the bootloader writes, when it protects the flash below it: 0x10000 on the Nano 33 BLE, 0x2000 on the SAMD21 boards. Nothing below it is written or erased. |
writableTo | The end of the flash that the bootloader writes, where an AVR bootloader starts: 0x7e00 on an Uno. Nothing at or above it is written. |
erasesToEnd | Whether the bootloader erases from an offset to the end of flash (SAMD21). |
LayoutReport
| Field | Value |
|---|---|
flashSize | The flash size that the layout is resolved for. |
minimumFlashSize | The minimum of a layout with size = "detect", else null. |
unallocated | The bytes that no partition uses. |
partitions | A list of PartitionReport. |
PartitionReport has name, role, offset, end, size, imageSize, headroom,
headroomPercent, and for a detect layout growsWithFlash and movesWithFlash (its size or
offset depends on the chip), minimumSize (the minimum that the layout declares, 0 for
remaining) and growth (the bytes above it).
LayoutOptions is the last argument of checkLayout and writeLayout. flashSize, a number of
bytes or a text such as "16 MiB", resolves a layout with size = "detect" for that flash
instead of its minimum. On a layout with a fixed size it is a layout error, and a size that does
not parse is an invalid-argument error. checkLayout still checks the images against the
partitions on the minimum flash. For a size above the minimum, writeLayout fails with a table
error for linker and constants.
NanoflashError
| Field | Value |
|---|---|
kind | What failed, such as no-sync, verification-failed, not-granted, disconnected. |
errorClass | input, device or verification. |
message | The message to show. |
details | The fields of the library, in camelCase. |
during | What the library was doing, for errors of the port. |
raw | The object that the library threw, as it was. |
See Errors and warnings for every kind.
<nanoflash-button>
<script type="module">import "nanoflash/button";</script>
<nanoflash-button manifest="firmware/manifest.json" label="Install"></nanoflash-button>
Importing nanoflash does not register the element. The manifest is a nanoflash or an
esp-web-tools manifest, in JSON.
| Attribute | Value |
|---|---|
manifest | The URL of the manifest. |
label | The text of the button. Default Install. |
erase-all | Erase the whole flash first. Without it, the element offers a checkbox when the manifest asks for it. |
The element asks for the port inside the click. It shows the progress, the notes of the library,
the result with what was not verified and every warning, and the errors. A broken module asks the
user to reload the page. In a browser without Web Serial, it says so and points to the nanoflash
command. When the board comes back in its bootloader under another USB identity, it shows a second
button, “Select the board in bootloader mode”, which asks for that port. Install stays available
beside it, to start over. A manifest with boards of several protocols works too: the chosen port
picks the board.
| Item | Values |
|---|---|
data-state | unsupported, loading, ready, connecting, erasing, writing, verifying, resetting, done, error, bootloader-permission. |
| Events (bubbling, composed) | nanoflash-progress (detail: the progress event), nanoflash-done (the report), nanoflash-error (the error). |
| CSS custom properties | --nanoflash-button-background, --nanoflash-button-color, --nanoflash-button-radius, --nanoflash-progress-color, --nanoflash-font. |
| Parts | button, erase, progress, message, bootloader-button. |
Bundlers
The WebAssembly modules ship in dist/wasm and are loaded relative to the module that imports them
(new URL("...wasm", import.meta.url)). Each module (espressif, samba, and avr for the three
AVR protocols) is a separate dynamic import, so a bundler loads only the module of the board that
is flashed. If your bundler does not copy .wasm files that it finds this way, copy
node_modules/nanoflash/dist/wasm next to the chunk.