Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PageWorldBridge.ts

25 lines984 Bytes

1import { contextBridge, ipcRenderer } from "electron";
2​
3import {
4 PASSKEY_BRIDGE_ARGUMENT,
5 PASSKEY_CREATE_CHANNEL,
6 PASSKEY_GET_CHANNEL,
7} from "./GuestProtocol.ts";
8import { installPageWorld, type PageWorldHost } from "./PageWorld.ts";
9​
10// Electron hands webPreferences.additionalArguments to sandboxed preloads in process.argv.
11const host: PageWorldHost = process.argv.includes(PASSKEY_BRIDGE_ARGUMENT)
12 ? {
13 // Only the two passkey channels are reachable from the page's world.
14 invokePasskey: (channel, publicKey) =>
15 channel === PASSKEY_CREATE_CHANNEL || channel === PASSKEY_GET_CHANNEL
16 ? ipcRenderer.invoke(channel, publicKey)
17 : Promise.reject(new Error("Unsupported passkey channel.")),
18 }
19 : {};
20​
21/** Installed before any page script runs; calls cross into the page's world and back. */
22export const pageWorld: ReturnType<typeof installPageWorld> = contextBridge.executeInMainWorld({
23 func: installPageWorld,
24 args: [host],
25});