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, andGuestRealmOptions.
Properties
-
readonly realm: GuestRealmUnderlying isolated V8-wasm
GuestRealm.
Methods
-
[dispose](): voidDisposable protocol hook for
usingdeclarations. -
dispose(): voidUninstalls DOM/eval hooks and disposes the underlying
GuestRealm. -
install(doc?: Document): voidActivates
evalinterception (unlessoptions.interceptEval === false), sweeps existing guest<script>tags (unlessoptions.runExisting === false), and attaches aMutationObservertodoc(or globaldocument).Idempotent if already installed.
doc(Document) — Target DOMDocumentto observe (defaults toglobalThis.document).
-
runScript(source: string, opts?: ScriptOpts): unknownExecutes 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(): voidDisconnects the
MutationObserverand restores the originalglobalThis.eval.
Functions
sandboxDocument
function sandboxDocument(options?: RealmSandboxOptions, doc?: Document): RealmSandboxConvenience 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 DOMDocument.
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— InterceptglobalThis.evalwhile installed and route string arguments to the guest realm (defaulttrue).isolate— Reuse an existingIsolateinstead of creating a new dedicatedIsolate.runExisting— Sweep and execute existing neutralized guest<script>tags on install (defaulttrue).