Custom protocols
import { convertFileSrc, protocolUrl } from "@tynd/core/client";Two ways to give the WebView URLs that are not your bundled frontend:
- Asset protocol:
convertFileSrc(path)turns a local file into a URL for<img>,<video>,<audio>, CSS orfetch. The host streams the file with HTTP Range support. Off untilapp.start({ assets })allow-lists the paths. - Backend protocols:
app.protocol(name, handler)servesname://...from your backend: each request runs a handler that returns aResponse.
Both are reachable only from app pages (the bundled frontend or tynd dev’s server), never from remote pages.
Asset protocol (convertFileSrc)
Section titled “Asset protocol (convertFileSrc)”Declare the files the WebView may load. The scope is a list of globs over absolute paths and is installed before any window opens:
// backendimport { homedir } from "node:os";import { join } from "node:path";import { app } from "@tynd/core";
app.start({ assets: { scope: [join(homedir(), "Videos", "**").replaceAll("\\", "/")] },});// frontendimport { convertFileSrc } from "@tynd/core/client";
video.src = convertFileSrc("/home/me/Videos/holiday.mp4");img.src = convertFileSrc("C:\\Users\\me\\Pictures\\cover.jpg");convertFileSrc encodes the whole path and returns asset://localhost/<path> on macOS and Linux, http://asset.localhost/<path> on Windows (WebView2 cannot load custom schemes directly).
assets field |
Type | Description |
|---|---|---|
scope |
string[] |
Globs over absolute paths (/home/me/Videos/**, C:/Users/me/Videos/**). * stays inside one folder, ** crosses folders. On Windows, matching ignores case and \ equals /. An empty list allows nothing. |
Behaviour:
- MIME type comes from the extension (video, audio, image, font, text and document types), then from the first bytes when the extension is unknown.
.gz/.zstfiles are served asapplication/octet-stream: the protocol never setsContent-Encoding. - Range requests (
bytes=a-b,bytes=a-,bytes=-n) answer206 Partial Content. One response reads at most 4 MiB at the requested offset, so seeking in a large video never loads the file into memory. A range past the end answers416. - Requests without
Rangeare served whole up to 32 MiB; larger files answer413and must be read withRange(<video>and<audio>always do this). GET,HEADand CORS preflights only. Other methods answer405.
Security
Section titled “Security”- Disabled unless
assetsis set; deny by default. - The URL is decoded once and must hold an absolute path:
..segments, NUL bytes, UNC shares (\\server\share), device paths (\\?\) and alternate data streams are refused with400. - The file is resolved by the OS (symlinks followed) and the resolved path must match
assets.scope: a symlink inside the scope that points outside it is refused (403). security.fs.denyrules still apply to assets.- Only app pages may load assets. A WebView that navigated to a remote site, or a remote iframe inside an app page, gets
403.Access-Control-Allow-Originnames only the app origin. - The app’s Content Security Policy allows
asset:andhttp://asset.localhostindefault-src,img-src,media-src,font-srcandconnect-srcwhenassetsis set, never inscript-src. A dev server sets its own CSP: add the same sources there if it sends one. - An HTML or SVG file opened as a page runs sandboxed (no scripts, no origin).
Backend protocols (app.protocol)
Section titled “Backend protocols (app.protocol)”// backendimport { app } from "@tynd/core";
app.protocol("thumbs", async (request, { webview }) => { const url = new URL(request.url); // thumbs://localhost/42?size=128 const id = url.pathname.slice(1); const png = await renderThumbnail(id, Number(url.searchParams.get("size") ?? 64)); return new Response(png, { headers: { "content-type": "image/png" } });});
app.start({ /* ... */ });// frontendimport { protocolUrl } from "@tynd/core/client";
img.src = protocolUrl("thumbs", `/${encodeURIComponent(id)}?size=128`);const res = await fetch(protocolUrl("thumbs", "/42"));| Signature | Description |
|---|---|
app.protocol(name, handler) |
Register before app.start(); the host registers the scheme on every WebView when it is created. Throws after app.start(). |
handler(request: Request, context: { webview: string }) |
Returns a Response or a Promise<Response>. request.url is always name://localhost/..., on every OS. |
protocolUrl(name, path = "/") |
URL of path on the scheme in the current WebView: name://localhost/path, or http://name.localhost/path on Windows. path is used as written. |
Rules:
- Names: 1-32 lowercase letters, digits or
-, starting with a letter (the name becomes a host name on Windows). Reserved:tynd,tynd-bin,asset,ipc,http,https,ws,wss,file,ftp,about,blob,data,javascript,mailto,tel,view-source,chrome,edge,webkit,localhost. A name a native plugin already registers fails the WebView creation. Registering the same name twice throws. - Bodies are buffered, not streamed: request and response bodies are each capped at 4 MiB (a larger request body answers
413, a larger response502). They travel base64-encoded on the backend channel. Serve large local files with the asset protocol instead. - Errors: a handler that throws or returns something other than a
Responseanswers500(the error is logged); no answer within 30 seconds answers504; a dead or reloading backend answers502. Content-Length,Transfer-Encodingand theAccess-Control-*headers are set by the host. When the handler returns afetch()response, itsContent-Encodingis dropped because the body is already decoded.- Methods: any; CORS preflights are answered by the host for app origins.
Security
Section titled “Security”- Only app pages reach a backend protocol; remote pages and remote iframes get
403before the backend is called.Access-Control-Allow-Originnames only the app origin. - The handler runs with full backend rights: validate
request.urland the body like any untrusted input, since they come from the renderer. - The app CSP allows the registered schemes (
name:andhttp://name.localhost) in the same directives as the asset protocol, never inscript-src.
Platform support
Section titled “Platform support”| Windows | macOS | Linux | |
|---|---|---|---|
convertFileSrc URL |
http://asset.localhost/... |
asset://localhost/... |
asset://localhost/... |
| Range seeking | Yes | Yes | Yes, when WebKitGTK sends Range (otherwise the 32 MiB whole-file limit applies) |
app.protocol |
Yes | Yes | Yes |