Files
yaak-mountain-loop/packages/plugin-sandbox/src/protocol.ts
T
Gregory Schier f8d6dfbdaa Run plugins in a QuickJS sandbox in the browser
Adds packages/plugin-sandbox: QuickJS-ng compiled to wasm, running in a
dedicated worker, with a runtime shell inside it that loads a plugin bundle
and answers the same InternalEventPayload events the Node runtime answers.
Plugins are unmodified.

Wires the browser host's template function, authentication, cURL import and
template render commands to it, and relaxes TemplateCallback's Send bound on
wasm32 so the engine's renderer can call back out to a plugin.
2026-08-18 15:22:49 -07:00

28 lines
1.2 KiB
TypeScript

/**
* The messages between a tab and its sandbox worker.
*
* Two request/reply flows in opposite directions. The tab asks the worker to
* load a module or send it an event; the worker asks the tab to answer a
* plugin's `ctx` call, because the tab is the only side with a database, a
* network and a user. Both carry payloads as JSON strings rather than objects:
* they have to be strings to cross into QuickJS anyway, and serializing once at
* the edge is cheaper than structured-cloning an object the worker will only
* stringify again.
*/
/** Tab → worker */
export type ToSandbox =
| { type: "load"; id: number; pluginRefId: string; source: string }
| { type: "unload"; id: number; pluginRefId: string }
| { type: "dispatch"; id: number; pluginRefId: string; envelope: string }
/** The tab's answer to a `host_call`. */
| { type: "host_result"; id: number; reply?: string; error?: string };
/** Worker → tab */
export type FromSandbox =
| { type: "result"; id: number; result: unknown }
| { type: "error"; id: number; message: string }
/** A plugin wants something only the tab can provide. */
| { type: "host_call"; id: number; envelope: string }
| { type: "log"; pluginRefId: string; level: string; message: string };