Files
electron/docs/api
Shelley Vohr 7e61137a70 feat: add protocol.registerSource() (#53220)
* feat: add protocol.registerSource()

A protocol.handle() handler runs on the main thread, so requests to an
app's own scheme wait whenever the main process is busy there, most
visibly at startup when the first window's HTML, scripts and styles are
served by that handler. For the common case of serving bundled files
there is nothing for JavaScript to decide, so let the app declare it:

  protocol.registerSource('app', {
    routes: [
      { match: { host: 'bundle', path: '/assets/' },
        source: { type: 'directory', root: assetsDir } },
      { match: { host: 'bundle' },
        source: { type: 'directory', root: distDir } }
    ]
  })

The routes are compiled into an immutable ProtocolSource held by the
session's ProtocolRegistry. The URLLoaderFactory the registry hands out
for the scheme (to renderers for subresources, to navigations, and to
net.fetch through ProtocolRegistry::CreateRegisteredFactory) is bound on
the IO thread; it picks the most specific route (a host match over a
wildcard, then the longest path prefix), resolves the rest of the path
inside that route's root, refusing anything that would leave it, and
hands the file to AsarURLLoader, which reads it on a blocking sequence
the way file: URLs are read, asar archives included, with Content-Type
from the extension and any headers the route adds. AsarURLLoader reads
plain files itself for this (read_plain_files) rather than delegating to
Chromium's file loader, whose Last-Modified formatting costs more than
the read for small files. A scheme has either a handler or a source;
unhandle() removes either and isProtocolHandled() reports both.

Linux x64, page with 60 stylesheets, median of 3 warm loads:
protocol.handle on the main thread 22 ms idle / 120 ms with the main
process busy for 100 ms after navigation; registerSource 13 ms / 14 ms.
Renderer fetch() round trip 0.33 -> 0.20 ms; browser-process CPU per
request 234 -> 200 us with no main-thread time at all.

* test: protocol.registerSource

* fix: address review of protocol.registerSource

- HEAD requests get the response head with Content-Type/Content-Length
  and an empty body, without opening or reading the file.
- A satisfiable Range is answered 206 with Content-Range and
  Content-Length; an unsatisfiable one 416 with `bytes */size`.
- Route roots are canonicalized at registration and each requested file
  is resolved (symlinks included) before it is opened; a file that is not
  under the root is not served.
- Plain-file sizes no longer pass through Archive::FileInfo's 32-bit
  size, so files over 4 GiB are served whole.
- registerSource rejects the full set of built-in schemes
  (ProtocolRegistry::IsBuiltinScheme), not just the legacy list.
- Reduce a few variable scopes, drop Route's out-of-line special members.

* fix: address second review of protocol.registerSource

* chore: oxfmt

* docs: mark protocol.registerSource() and getSource() as experimental

* fix: tighten registerSource() route validation and root containment

* docs: note that loader-computed headers override route headers
2026-09-28 16:53:39 +00:00
..
2022-12-05 13:18:57 -05:00
2026-05-20 10:03:40 -04:00