Skip to content

rfc: apiBridge - #32

Open
MarshallOfSound wants to merge 3 commits into
mainfrom
sam/rfc-0029-api-bridge
Open

MarshallOfSound wants to merge 3 commits into
mainfrom
sam/rfc-0029-api-bridge

Conversation

@MarshallOfSound

@MarshallOfSound MarshallOfSound commented Sep 11, 2026 •

Copy link
Copy Markdown
Member

This RFC proposes apiBridge, a way for the main process to pass an API object to web pages. The page gets it as navigator.electron.<name> before its first script runs, with no preload script, contextBridge or channel names. The main process checks the calling frame and its origin on every call.

Measured with the reference implementation (Linux testing build, median of five rounds). "Before" is ipcRenderer exposed through contextBridge.

Operation Before apiBridge Faster
Async call, one at a time 165 µs 115 µs 1.4×
Async call, 500 in flight 68 µs 27 µs 2.5×
Echo a ~5 KB object 1,880 µs 468 µs 4.0×
Sync call 147 µs 107 µs 1.4×
Read shared state (sendSync vs store.get()) 149 µs 0.47 µs ~320×
Event, main to page 24 µs 20 µs 1.2×

Rendered · Reference implementation (draft): electron/electron#53832

@TheOneTheOnlyJJ

Copy link
Copy Markdown

Amazing work! I was smiling and nodding while reading this all the way through.
Looking forward to seeing how this progresses. Many of the future possibilities mentioned are also very enticing.

Although this is something to think about later, for argument validation hooks, I can see ata (by @mertcanaltin) as a good fit for fast native validation. There were even some discussions about vendoring it in Node some time ago as part of a node:validator module, which, if it ever happens, would likely provide data validation primitives that Electron could make direct use of. While these discussions are on hold for now, it could be a relevant development to keep an eye on.

@mertcanaltin

Copy link
Copy Markdown

Thanks for the mention. One clarification since it matters for Electron: ata is pure JavaScript by default and the native addon is optional, so nothing here needs a binary. For a renderer the useful properties are that validation works with code generation blocked, which a strict CSP does, and that a schema can be compiled at build time into a module with no dependencies.

Happy to help if argument validation lands in scope

@MarshallOfSound

Copy link
Copy Markdown
Member Author

Things like validation will likely remain out-of-scope for landing this initially. In fact my ideal is simply to have built this in a way that people can ship very simply "shim" packages that wire up runtime type validation through their validator of choice. E.g. ata, zod, json schema, whatever

I don't think the runtime validation piece makes sense unless there is a standard built in to Chrome etc. for this (which there isn't today)

So keeping this RFC scoped to the transport. Layering on top of the transport is always possible in userland or by us later if we choose

@mertcanaltin

mertcanaltin commented Sep 12, 2026 •

Copy link
Copy Markdown

That scoping makes sense.

One thing that may help the shim idea: Standard Schema (standardschema.dev) is already the common interface for this in userland. zod, valibot, arktype and ata all implement it, so a shim can accept anything exposing ~standard.validate and never name a validator. That removes the need for apiBridge to pick one, or to wait for a platform standard.

@TheOneTheOnlyJJ

TheOneTheOnlyJJ commented Sep 13, 2026 •

Copy link
Copy Markdown

Regarding the other future possibilities, are these on the roadmap to be implemented or just stated as options for now?
Here are my quick thoughts on them.

A change event for the isolated world. This is the first thing that comes to mind, as having it in the main world but not in the isolated one is weird from the API shape perspective.

Shared workers and service workers can be highly useful. I can see managing local databases as a potential large use case (node:sqlite, which is always sync as of now) so the main process does not get blocked on long-running operations.

event.emitTo(frame, ...args) should consider what happens if dedicated worker support is implemented (they are supposed to share the same frame with their document, no?). Definitely useful, regardless.

MessagePortMain as a value to return streams. Should open up more use cases with less boilerplate and probably better performance, sounds great.

Events from the page to the main process. Interesting, I'm trying to think of any potential benefits. Maybe more natively hooking into document events in a way that bypasses more JS? Would it open up any new functionality that is not achievable through method calls alone? I'm not knowledgeable enough with the platform to have a strong opinion on this.

Guidance on moving apps from IPC through contextBridge to apiBridge. Probably a necessity rather than a future possibility, but I am wondering what is left for contextBridge over apiBridge. When should contextBridge be used over apiBridge once it's stable? Any place where it is preferred or where it still has a performance benefit, or is it just completely outmatched by the new apiBridge in all regards? Docs that answer these questions would be good once this is stable.

@MarshallOfSound

Copy link
Copy Markdown
Member Author

I'd rather avoid discussing the potential future additions. They were left out of scope intentionally but noted specifically as a "thought about this, not in scope for this discussion" thing

@erickzhao erickzhao added the pending-review Waiting for reviewers to give feedback on the PR specifications label Nov 12, 2026
Comment thread text/0029-api-bridge.md

```js
// main.js
win.webContents.mainFrame.apiBridge.passToIsolatedWorld('auth', {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contextBridge has APIs for interacting with different isolated worlds other than the preload world. Is that something we want to expose here as well?

Comment thread text/0029-api-bridge.md
### Find the frame that called

When one API serves many windows, some methods must know which window called them. Wrap these
methods with `apiBridgeMain.withCaller()`. The method gets a `caller` object as its first argument.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be simpler to always pass the caller, and let people ignore it if they don't care?

Comment thread text/0029-api-bridge.md
old API. Then `window` receives an `electronapichange` event. References to the old object stop
working.

Only changes to APIs in the main world fire the `electronapichange` event. See

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So preload scripts can't subscribe?

Comment thread text/0029-api-bridge.md
// Page
let notes = navigator.electron?.notes
window.addEventListener('electronapichange', (event) => {
const { name, change } = event.detail // change is 'added', 'removed' or 'replaced'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since session APIs can be shadowed by frame APIs, it maybe be worth explicitly documenting how this would fire when a shadowing frame API is added/removed. I think treating this as "changed" would be the most helpful?

Comment thread text/0029-api-bridge.md
}

interface ApiBridgeFrameMain {
pass(name: string, api: object, options?: ApiBridgeFrameOptions): void

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly about this, but I wonder if calling these grant and grantInIsolatedWorld would make the API feel more symmetrical with the revoke methods.

Comment thread text/0029-api-bridge.md
- Without `origin`, a frame grant uses the origin of the document that the frame shows at the
time of the call. If that origin is opaque, for example before the first navigation, `pass()`
throws.
- Each property of the API must be a function, an `ApiBridgeEvent` or an `ApiBridgeStore`. For other

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if I want to expose static data, I must wrap it in an ApiBridgeStore? That's not the end of the world, but I'm curious why not allow static values to be passed directly?

Comment thread text/0029-api-bridge.md
A session grant reaches a frame when all of these conditions are true:

- The page of the frame is the primary page of its `WebContents`. It is not prerendered, in the
back/forward cache, or in a fenced frame.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a fenced frame?

Comment thread text/0029-api-bridge.md

An opaque origin never matches an origin list. Thus, a document with an opaque origin never gets
an API. Examples are a `data:` URL, a sandboxed iframe without `allow-same-origin`, and an opaque
`about:blank`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious to talk about this since I've seen people use about:blank pages, so this seems like a noteworthy limitation, but I don't have enough context on why about:blank is used, so I don't know how much this will actually be a problem.

Comment thread text/0029-api-bridge.md
- Each world has its own names, its own precedence and its own `navigator.electron`.
- When a frame has no isolated world, isolated-world grants have no effect in that frame.
- Other isolated worlds get no APIs. Examples are Chrome extension content scripts and the worlds
that `webFrame.executeJavaScriptInIsolatedWorld()` uses.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this answers my question above about other isolated worlds, but I'm going to leave the comment anyway for discussion.

Comment thread text/0029-api-bridge.md
`ipcRenderer` uses the same serializer. apiBridge does not support transfer:

- `ArrayBuffer`s are copied.
- `MessagePort`s and other transferable objects are refused.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this would be one use case where someone would still reach for contextBridge?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pending-review Waiting for reviewers to give feedback on the PR specifications

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants