Skip to Content
V8-wasm
API Reference

Realm Sandbox

Page-level guest script and eval execution sandbox backed by V8-wasm GuestRealm

Import path: @browser-ports/v8-wasm/realm-sandbox

@browser-ports/v8-wasm/realm-sandbox — Page-level script execution sandbox backed by a shared V8-wasm GuestRealm.

Intercepts neutralized guest <script> elements (both existing and dynamically added via MutationObserver) and optionally routes globalThis.eval into the isolated V8-wasm realm.

RealmSandbox

Document-level script execution sandbox backed by a single GuestRealm.

When installed via install (or sandboxDocument), sweeps existing <script> tags with type === GUEST_SCRIPT_TYPE, watches for newly appended guest <script> tags with a MutationObserver, and optionally intercepts globalThis.eval.

new RealmSandbox(options?: RealmSandboxOptions)

Constructs a RealmSandbox and injects any configured options.globals.

  • options (RealmSandboxOptions) — Sandbox globals, interception flags, and GuestRealmOptions.

Properties

  • readonly realm: GuestRealm

    Underlying isolated V8-wasm GuestRealm.

Methods

  • [dispose](): void

    Disposable protocol hook for using declarations.

  • dispose(): void

    Uninstalls DOM/eval hooks and disposes the underlying GuestRealm.

  • install(doc?: Document): void

    Activates eval interception (unless options.interceptEval === false), sweeps existing guest <script> tags (unless options.runExisting === false), and attaches a MutationObserver to doc (or global document).

    Idempotent if already installed.

    • doc (Document) — Target DOM Document to observe (defaults to globalThis.document).
  • runScript(source: string, opts?: ScriptOpts): unknown

    Executes a JavaScript script string directly inside the sandbox's GuestRealm.

    • source (string) — JavaScript source text to run.
    • opts (ScriptOpts) — Optional script URL and compilation type.
  • uninstall(): void

    Disconnects the MutationObserver and restores the original globalThis.eval.

Functions

sandboxDocument

function sandboxDocument(options?: RealmSandboxOptions, doc?: Document): RealmSandbox

Convenience helper that constructs a RealmSandbox and immediately installs it on doc (or global document).

Returns: The installed RealmSandbox instance.

Parameters

  • options (RealmSandboxOptions) — Sandbox configuration options.
  • doc (Document) — Optional target DOM Document.

Constants

GUEST_SCRIPT_TYPE

const GUEST_SCRIPT_TYPE: string;

Custom MIME type identifying neutralized <script> elements executed by RealmSandbox.

Interfaces & Types

RealmSandboxOptions

Configuration options for RealmSandbox and sandboxDocument.

interface RealmSandboxOptions extends GuestRealmOptions {
  compileHook?: CompileHook;
  globals?: Record<string, unknown>;
  interceptEval?: boolean;
  isolate?: Isolate;
  runExisting?: boolean;
}
  • compileHook — Optional source transform hook invoked before compilation.
  • globals — Initial globals/endowments to inject into the shared guest realm.
  • interceptEval — Intercept globalThis.eval while installed and route string arguments to the guest realm (default true).
  • isolate — Reuse an existing Isolate instead of creating a new dedicated Isolate.
  • runExisting — Sweep and execute existing neutralized guest <script> tags on install (default true).

On this page