Skip to content

App scheme ​

A page served from a scheme of its own, such as app://app, gets a real origin without a server. createAppScheme serves each host of the scheme from its folder and binds the scheme and origin to the navigation guards and the sender check.

The package registers nothing itself. Hand privileged to Electron before it is ready, and handle to the sessions you pick:

ts
import { join } from 'node:path';
import { app, protocol, session } from 'electron';
import { createAppScheme } from '@adecore/shell';

const scheme = createAppScheme({
    scheme: 'app',
    root: join(process.resourcesPath, 'client'),
    headers: { 'content-security-policy': CSP },
    pageUrl: process.env.DEV_SERVER_URL
});

protocol.registerSchemesAsPrivileged([scheme.privileged]);

app.whenReady().then(() => {
    session.defaultSession.protocol.handle(scheme.scheme, scheme.handle);
    window.loadURL(scheme.url);
});

Options ​

AppSchemeOptions:

OptionDefaultMeaning
schemerequiredThe scheme without its colon
hostappThe host of the page
rootrequiredThe folder of the built page
routestrueThe page routes itself: an address without an extension gets its index.html
headersnoneHeaders on every response of the page, such as its policy, or a function of the file
hostsnoneOther hosts on the same scheme, by name: a SchemeFolder or a SchemeResolver
pageUrlthe page's hostThe URL the window loads instead, such as a dev server; the guards take its origin
privilegesstandard and secureWhat Chromium allows the scheme
readreads the file wholeAnswers with a file it found; (file) => net.fetch(pathToFileURL(file).href) streams it

The default privileges give a real origin, a secure context, fetch and CORS, streaming, the code cache and service workers.

What it serves ​

The scheme answers each request from the folder of its host. / serves index.html, and so does an address without an extension when the folder routes itself. A missing file is a 404, so a chunk replaced by a newer build fails as a load error. A path that climbs out of the folder (.., an escaped slash, a symlink) is a 404 too. The content type follows the extension.

A SchemeFolder is { root, routes?, headers? }. A host whose files are not one folder, such as files found by an id, is a SchemeResolver: (request, url) => Response. It finds the file and answers through serveFile(root, file, request, headers?), which serves only files inside root. segmentsOf(pathname) returns the decoded segments of a path, or null when one climbs out.

ts
const scheme = createAppScheme({
    scheme: 'app',
    root: clientRoot,
    hosts: {
        render: { root: renderRoot, headers: { 'access-control-allow-origin': '*' } },
        attachment: async (request, url) => {
            const [folder, id] = segmentsOf(url.pathname) ?? [];
            const file = folder && id ? await findAttachment(folder, id) : null;

            return file === null ? new Response('Not found', { status: 404 }) : scheme.serveFile(attachmentsRoot, file, request);
        }
    }
});

The guards ​

createAppScheme returns an AppScheme. Its scheme, privileged and handle go to protocol.registerSchemesAsPrivileged and protocol.handle as above. origin is the app's origin for the guards, and url is what the window loads. The guards come bound to both: originOf(url), isAppUrl(url), navigation(url) for appWindowNavigation and isAppSender(isAppWindow, frame) for isAppSender. With pageUrl set, the app's origin is the dev server's, and the scheme's own page host no longer counts as the app.

ts
const fromApp = (event: Electron.IpcMainInvokeEvent): boolean => scheme.isAppSender(windows.fromPage(event.sender) !== null, event.senderFrame);

Licenses and third-party notices are listed in each package.