Repository navigation
rfc: apiBridge - #32
MarshallOfSound wants to merge 3 commits into
Conversation
|
Amazing work! I was smiling and nodding while reading this all the way through. 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 |
|
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 |
|
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 |
|
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. |
|
Regarding the other future possibilities, are these on the roadmap to be implemented or just stated as options for now? 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 (
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 |
|
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 |
|
|
||
| ```js | ||
| // main.js | ||
| win.webContents.mainFrame.apiBridge.passToIsolatedWorld('auth', { |
There was a problem hiding this comment.
contextBridge has APIs for interacting with different isolated worlds other than the preload world. Is that something we want to expose here as well?
| ### 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. |
There was a problem hiding this comment.
Would it be simpler to always pass the caller, and let people ignore it if they don't care?
| 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 |
There was a problem hiding this comment.
So preload scripts can't subscribe?
| // Page | ||
| let notes = navigator.electron?.notes | ||
| window.addEventListener('electronapichange', (event) => { | ||
| const { name, change } = event.detail // change is 'added', 'removed' or 'replaced' |
There was a problem hiding this comment.
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?
| } | ||
|
|
||
| interface ApiBridgeFrameMain { | ||
| pass(name: string, api: object, options?: ApiBridgeFrameOptions): void |
There was a problem hiding this comment.
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.
| - 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 |
There was a problem hiding this comment.
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?
| 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. |
|
|
||
| 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`. |
There was a problem hiding this comment.
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.
| - 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. |
There was a problem hiding this comment.
Ok, this answers my question above about other isolated worlds, but I'm going to leave the comment anyway for discussion.
| `ipcRenderer` uses the same serializer. apiBridge does not support transfer: | ||
|
|
||
| - `ArrayBuffer`s are copied. | ||
| - `MessagePort`s and other transferable objects are refused. |
There was a problem hiding this comment.
I guess this would be one use case where someone would still reach for contextBridge?
This RFC proposes
apiBridge, a way for the main process to pass an API object to web pages. The page gets it asnavigator.electron.<name>before its first script runs, with no preload script,contextBridgeor 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
ipcRendererexposed throughcontextBridge.sendSyncvsstore.get())Rendered · Reference implementation (draft): electron/electron#53832