Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Browser support and security

Which browsers flash

The browser package flashes over Web Serial. Only Chromium-based browsers implement it (Chrome, Edge, Opera), and only on a secure origin (https: or localhost). WebUSB, which later protocols will need, has the same limits. Firefox and Safari do not implement either. In those browsers, use the nanoflash command, which flashes the same manifests.

isSupported() tells whether the browser has Web Serial. The <nanoflash-button> element shows an unsupported state and points to the command line.

Why the port is asked for inside the click

A browser shows the port chooser only during a user gesture, and an await may end the gesture. prepare reads the manifest and starts to load the WebAssembly module before the click. prepared.flash asks for the port before its first await, so call it straight from the click handler.

Why a second button appears

The touch reset of SAM-BA and AVR109 boards makes the bootloader appear as another USB device. The page has no permission for that device the first time, and the browser does not let a page reach a device that it was not granted without a new user gesture. The component asks for the application and bootloader identities in one filter list. When the bootloader device was never granted, it shows a second button, “Select the board in bootloader mode”. The browser keeps the grant, so the next flash finds the bootloader by itself. See Use the browser package.

Why a broken module stays broken

A WebAssembly trap (a bug of nanoflash) is fatal for its module, which cannot be instantiated again. A layout call runs synchronously and sees the trap at once. flash and detect run in microtasks, where a trap is an uncaught exception that would leave their promise pending. While they run, the package listens for uncaught WebAssembly.RuntimeErrors (the error event of the page, uncaughtExceptionMonitor in Node) and rejects the call when one comes. Either way, the call rejects with kind module-broken (class device), its port is closed, and every later call that needs the module fails at once with the same error. The page must be reloaded.

The flasher page

The flasher page (https://alixanneraud.github.io/nanoflash/flasher/) flashes a board from a manifest that the visitor gives as an https: URL or as files on the computer. It writes firmware to hardware, so it treats every manifest as untrusted until the visitor confirms where it comes from.

  • The site hosts no firmware and no manifest. The visitor brings both.
  • A manifest of another origin needs a tick in a checkbox. The label shows the whole manifest URL, not only its origin: one origin, such as raw.githubusercontent.com, serves the files of many publishers. The page also shows the boards and the other origins that the manifest loads files from, and the label names them. The page gives the manifest to the button only after the tick.
  • The manifest is downloaded once. The button receives the manifest that the page fetched, as a blob: URL with every path resolved against the manifest URL. The button writes the text that the page showed, not a second download that could differ.
  • Cross-origin reads must be allowed. The server of the manifest and of its files must send Access-Control-Allow-Origin. A redirect to another origin is refused.
  • Files on the computer need no tick when every path of the manifest names a selected file. A manifest on the computer that names a URL needs the same tick, and the label names every origin of those URLs.
  • The page refuses to run inside a frame. Other sites can frame the page: GitHub Pages sends no frame-ancestors header, and a policy in a meta element cannot set it. start.ts therefore checks window.top and refuses to start inside a frame. This check is a second defence: the browser still asks the visitor to choose the serial port.
  • A Content Security Policy in a meta element of index.html contains the SHA-256 hash of the import map. flasher.test.mjs fails when the hash is wrong.
  • The footer shows the version and the commit that the site was built from (site.json), so a visitor can tell which code runs.

Use the flasher page gives the steps for visitors and for publishers of manifests.