Main
Own the runtime
Create ElectronRuntimeIntentBridge, handlers, routes, Data Lanes, and shutdown.
JavaScript app host · Electron 42+ · connector 3.3.2
Electron has its own integration door in the JavaScript family. The main process owns the native runtime; preload projects a small intent API; the renderer neither imports the connector nor owns lifecycle.
npm install [email protected]3.3.2, the first Electron release on Core generation 3.2.[email protected]; npm installs it automatically.Main
Create ElectronRuntimeIntentBridge, handlers, routes, Data Lanes, and shutdown.
Preload
Use the context bridge; bundle the preload helper and leave only electron external.
Renderer
Never import the connector, receive tokens/paths/credentials, or own a native handle.
import { BrowserWindow, ipcMain } from "electron";
import { pathToFileURL } from "node:url";
import {
ElectronRuntimeIntentBridge,
localRoute,
registerElectronIntentIpcHandler,
} from "coakka-runtime-electron";
const target = "samples.electron.intent.echo";
const bridge = ElectronRuntimeIntentBridge.start({
systemName: "electron-app",
nodeId: "electron-app-main",
routes: [localRoute(target)],
});
bridge.registerJsonIntentHandler(target, async (intent) => ({
handledBy: "electron-main",
echo: intent.payload,
}));
// After app.whenReady(); use absolute application-owned paths.
const window = new BrowserWindow({
webPreferences: {
preload: bundledPreloadPath,
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
},
});
const trustedUrl = pathToFileURL(rendererHtmlPath).href;
const unregister = registerElectronIntentIpcHandler(ipcMain, bridge, {
authorize: (event) => event.sender === window.webContents &&
event.senderFrame === window.webContents.mainFrame &&
event.senderFrame.url === trustedUrl,
});authorize permits only explicit true. Match the expected window, main frame, and trusted document URL; a renderer-supplied source is not authorization evidence.
const { contextBridge, ipcRenderer } = require("electron");
const { createCoAkkaPreloadApi } = require(
"coakka-runtime-electron/preload"
);
contextBridge.exposeInMainWorld(
"coakka",
createCoAkkaPreloadApi(ipcRenderer),
);const result = await window.coakka.intent({
intentId: "intent-1",
source: "electron-renderer",
target: "samples.electron.intent.echo",
operation: "echo",
payload: { message: "hello-electron-runtime" },
payloadIdentity: {
messageType: "samples.electron.intent.echo.request.v1",
payloadSchemaVersion: 1,
payloadFormat: "json",
},
timeoutMs: 2_000,
});Do not expose all of ipcRenderer. Do not disable the sandbox just so preload can require a package; bundle the application-owned preload source.
Create one folder and copy these five files. The hidden sandboxed renderer submits one intent, the main-process handler replies, and the app exits after printing the verified payload.
{
"name": "coakka-portal-electron-check",
"version": "0.0.0",
"private": true,
"type": "module",
"main": "main.mjs",
"scripts": {
"preload": "esbuild preload.cjs --bundle --platform=node --format=cjs --external:electron --outfile=preload.bundle.cjs",
"start": "npm run preload && electron ."
},
"dependencies": {
"coakka-runtime-electron": "3.3.2"
},
"devDependencies": {
"electron": "42.0.0",
"esbuild": "0.27.7"
}
}
import { app, BrowserWindow, ipcMain } from "electron";
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import {
ElectronRuntimeIntentBridge,
localRoute,
registerElectronIntentIpcHandler,
} from "coakka-runtime-electron";
const root = dirname(fileURLToPath(import.meta.url));
const rendererPath = join(root, "index.html");
const rendererUrl = pathToFileURL(rendererPath).href;
const target = "portal.electron.echo";
let bridge;
let window;
let unregister;
let stopping;
let allowQuit = false;
async function stop() {
if (stopping) return stopping;
stopping = (async () => {
unregister?.();
ipcMain.removeHandler("coakka:portal-finish");
await bridge?.close();
window?.destroy();
allowQuit = true;
app.quit();
})();
return stopping;
}
async function run() {
// Start evaluation now, then await readiness inside the function. Top-level
// await on app.whenReady() can deadlock Electron's ESM entrypoint.
await app.whenReady();
bridge = ElectronRuntimeIntentBridge.start({
systemName: "portal-electron",
nodeId: "portal-electron-main",
routes: [localRoute(target)],
});
bridge.registerJsonIntentHandler(target, (intent) => ({
handledBy: "electron-main",
echo: intent.payload,
}));
window = new BrowserWindow({
width: 720,
height: 480,
show: false,
webPreferences: {
preload: join(root, "preload.bundle.cjs"),
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
},
});
const authorize = (event) =>
event.sender === window.webContents &&
event.senderFrame === window.webContents.mainFrame &&
event.senderFrame.url === rendererUrl;
unregister = registerElectronIntentIpcHandler(ipcMain, bridge, { authorize });
const reported = new Promise((resolve, reject) => {
const timer = setTimeout(
() => reject(new Error("Electron verification timed out")),
10_000,
);
window.webContents.once("preload-error", (_event, _path, error) => {
clearTimeout(timer);
reject(error);
});
ipcMain.handle("coakka:portal-finish", (event, value) => {
if (!authorize(event)) {
throw new Error("Electron verification report was not authorized");
}
clearTimeout(timer);
resolve(value);
return true;
});
});
await Promise.all([reported, window.loadFile(rendererPath)]);
console.log(JSON.stringify(await reported));
}
app.on("window-all-closed", () => {});
app.on("before-quit", (event) => {
if (allowQuit) return;
event.preventDefault();
void stop();
});
void run()
.catch((error) => {
process.exitCode = 1;
console.error(error instanceof Error ? error.stack : error);
})
.finally(stop);
const { contextBridge, ipcRenderer } = require("electron");
const {
createCoAkkaPreloadApi,
} = require("coakka-runtime-electron/preload");
const runtime = createCoAkkaPreloadApi(ipcRenderer);
contextBridge.exposeInMainWorld("coakka", {
intent: runtime.intent,
finish(result) {
return ipcRenderer.invoke("coakka:portal-finish", result);
},
});
const result = await window.coakka.intent({
intentId: "portal-electron-intent-1",
source: "electron-renderer",
target: "portal.electron.echo",
operation: "echo",
payload: { message: "hello-electron-runtime" },
payloadIdentity: {
messageType: "portal.electron.echo.request.v1",
payloadSchemaVersion: 1,
payloadFormat: "json",
},
timeoutMs: 2000,
});
document.querySelector("output").textContent = JSON.stringify(result.payload);
await window.coakka.finish(result.payload);
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CoAkka Runtime Electron check</title>
</head>
<body>
<h1>CoAkka Runtime Electron check</h1>
<output>Waiting for the main-process handler…</output>
<script type="module" src="./renderer.js"></script>
</body>
</html>
npm install
npm startExpected terminal payload
{"handledBy":"electron-main","echo":{"message":"hello-electron-runtime"}}ESM startup rule. Invoke async run() immediately and await app.whenReady() inside it. Top-level-awaiting readiness can deadlock Electron's ESM entrypoint before a window is created.
runtimeStartupConfig(); it does not infer effective state from input.unregister(), then await bridge.close().intentBatch is bounded and ordered, but not atomic.Read the connection/TLS contract · Read the Data Lane contract