Something went wrong. Try again.
A reusable desktop shell: your sidecar, your index, safe APIs, installers for every platform
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152// The `app://` scheme: the app directory served as a real origin, so fetch, ES// modules and CORS behave as on the web rather than as on a local file.import { createReadStream } from 'node:fs';import { stat } from 'node:fs/promises';import { extname, join, resolve, sep } from 'node:path';import { Readable } from 'node:stream';import { protocol } from 'electron';
export const SCHEME = 'app';
const MIME = { '.html': 'text/html; charset=utf-8', '.htm': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.mjs': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8', '.json': 'application/json; charset=utf-8', '.map': 'application/json', '.txt': 'text/plain; charset=utf-8', '.md': 'text/markdown; charset=utf-8', '.xml': 'application/xml', '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg', '.gif': 'image/gif', '.webp': 'image/webp', '.avif': 'image/avif', '.ico': 'image/x-icon', '.woff': 'font/woff', '.woff2': 'font/woff2', '.ttf': 'font/ttf', '.otf': 'font/otf', '.mp3': 'audio/mpeg', '.ogg': 'audio/ogg', '.wav': 'audio/wav', '.mp4': 'video/mp4', '.webm': 'video/webm', '.wasm': 'application/wasm', '.pdf': 'application/pdf', '.zip': 'application/zip',};
// Before the app is ready.export function registerScheme () { protocol.registerSchemesAsPrivileged([{ scheme: SCHEME, privileges: { standard: true, secure: true, supportFetchAPI: true, corsEnabled: true, stream: true }, }]);}
// After the app is ready: app://<host>/<path> maps to <root>/<path>, with// index.html for directories and nothing outside root.export function serveApp ({ root, host, log }) { const base = resolve(root); protocol.handle(SCHEME, async (req) => { const url = new URL(req.url); if (url.host !== host) return new Response('Not found', { status: 404 }); let file = resolve(base, `.${decodeURIComponent(url.pathname)}`); if (file !== base && !file.startsWith(base + sep)) return new Response('Forbidden', { status: 403 }); let info = await stat(file).catch(() => null); if (info?.isDirectory()) { file = join(file, 'index.html'); info = await stat(file).catch(() => null); } if (!info?.isFile()) { log?.warn(`app://${host}${url.pathname}: not found`); return new Response('Not found', { status: 404 }); } return new Response(Readable.toWeb(createReadStream(file)), { headers: { 'Content-Type': MIME[extname(file).toLowerCase()] || 'application/octet-stream', 'Content-Length': String(info.size) }, }); });}