@ricsam/isolate-runtime 0.1.4 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,9 +2,9 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
4
  "sourcesContent": [
5
- "import ivm from \"isolated-vm\";\nimport { setupCore } from \"@ricsam/isolate-core\";\nimport { setupConsole } from \"@ricsam/isolate-console\";\nimport { setupEncoding } from \"@ricsam/isolate-encoding\";\nimport { setupTimers } from \"@ricsam/isolate-timers\";\nimport { setupPath } from \"@ricsam/isolate-path\";\nimport { setupCrypto } from \"@ricsam/isolate-crypto\";\nimport { setupFetch } from \"@ricsam/isolate-fetch\";\nimport { setupFs } from \"@ricsam/isolate-fs\";\n\nimport type { ConsoleOptions, ConsoleHandle } from \"@ricsam/isolate-console\";\nimport type { FetchOptions, FetchHandle } from \"@ricsam/isolate-fetch\";\nimport type { FsOptions, FsHandle } from \"@ricsam/isolate-fs\";\nimport type { CoreHandle } from \"@ricsam/isolate-core\";\nimport type { EncodingHandle } from \"@ricsam/isolate-encoding\";\nimport type { TimersHandle } from \"@ricsam/isolate-timers\";\nimport type { PathHandle } from \"@ricsam/isolate-path\";\nimport type { CryptoHandle } from \"@ricsam/isolate-crypto\";\n\nexport interface RuntimeOptions {\n /** Isolate memory limit in MB */\n memoryLimit?: number;\n /** Console options */\n console?: ConsoleOptions;\n /** Fetch options */\n fetch?: FetchOptions;\n /** File system options (optional - fs only set up if provided) */\n fs?: FsOptions;\n}\n\nexport interface RuntimeHandle {\n /** The isolate instance */\n readonly isolate: ivm.Isolate;\n /** The context instance */\n readonly context: ivm.Context;\n /** The fetch handle for serve() and WebSocket dispatching */\n readonly fetch: FetchHandle;\n /** Process pending timers */\n tick(ms?: number): Promise<void>;\n /** Dispose all resources */\n dispose(): void;\n}\n\n/**\n * Create a fully configured isolated-vm runtime\n *\n * Sets up all WHATWG APIs: fetch, fs, console, crypto, encoding, timers\n *\n * @example\n * const runtime = await createRuntime({\n * console: {\n * onLog: (level, ...args) => console.log(`[${level}]`, ...args)\n * },\n * fetch: {\n * onFetch: async (request) => fetch(request)\n * }\n * });\n *\n * await runtime.context.eval(`\n * console.log(\"Hello from sandbox!\");\n * const response = await fetch(\"https://example.com\");\n * `);\n *\n * runtime.dispose();\n */\nexport async function createRuntime(\n options?: RuntimeOptions\n): Promise<RuntimeHandle> {\n const opts = options ?? {};\n\n // Create isolate with optional memory limit\n const isolate = new ivm.Isolate({\n memoryLimit: opts.memoryLimit,\n });\n const context = await isolate.createContext();\n\n // Store all handles for disposal\n const handles: {\n core?: CoreHandle;\n console?: ConsoleHandle;\n encoding?: EncodingHandle;\n timers?: TimersHandle;\n path?: PathHandle;\n crypto?: CryptoHandle;\n fetch?: FetchHandle;\n fs?: FsHandle;\n } = {};\n\n // Setup all APIs in order\n // Core must be first as it provides Blob, File, streams, URL, etc.\n handles.core = await setupCore(context);\n\n // Console\n handles.console = await setupConsole(context, opts.console);\n\n // Encoding (btoa/atob)\n handles.encoding = await setupEncoding(context);\n\n // Timers (setTimeout, setInterval)\n handles.timers = await setupTimers(context);\n\n // Path module\n handles.path = await setupPath(context);\n\n // Crypto (randomUUID, getRandomValues)\n handles.crypto = await setupCrypto(context);\n\n // Fetch API\n handles.fetch = await setupFetch(context, opts.fetch);\n\n // File system (only if handler provided)\n if (opts.fs) {\n handles.fs = await setupFs(context, opts.fs);\n }\n\n return {\n isolate,\n context,\n fetch: handles.fetch!,\n async tick(ms?: number) {\n await handles.timers!.tick(ms);\n },\n dispose() {\n // Dispose all handles\n handles.fs?.dispose();\n handles.fetch?.dispose();\n handles.crypto?.dispose();\n handles.path?.dispose();\n handles.timers?.dispose();\n handles.encoding?.dispose();\n handles.console?.dispose();\n handles.core?.dispose();\n\n // Release context and dispose isolate\n context.release();\n isolate.dispose();\n },\n };\n}\n\n// Re-export all package types and functions\nexport { setupCore } from \"@ricsam/isolate-core\";\nexport type { CoreHandle, SetupCoreOptions } from \"@ricsam/isolate-core\";\n\nexport { setupConsole } from \"@ricsam/isolate-console\";\nexport type { ConsoleHandle, ConsoleOptions } from \"@ricsam/isolate-console\";\n\nexport { setupCrypto } from \"@ricsam/isolate-crypto\";\nexport type { CryptoHandle } from \"@ricsam/isolate-crypto\";\n\nexport { setupEncoding } from \"@ricsam/isolate-encoding\";\nexport type { EncodingHandle } from \"@ricsam/isolate-encoding\";\n\nexport { setupFetch } from \"@ricsam/isolate-fetch\";\nexport type { FetchHandle, FetchOptions, WebSocketCommand, UpgradeRequest } from \"@ricsam/isolate-fetch\";\n\nexport { setupFs, createNodeFileSystemHandler } from \"@ricsam/isolate-fs\";\nexport type { FsHandle, FsOptions, FileSystemHandler, NodeFileSystemHandlerOptions } from \"@ricsam/isolate-fs\";\n\nexport { setupPath } from \"@ricsam/isolate-path\";\nexport type { PathHandle } from \"@ricsam/isolate-path\";\n\nexport { setupTimers } from \"@ricsam/isolate-timers\";\nexport type { TimersHandle } from \"@ricsam/isolate-timers\";\n"
5
+ "import ivm from \"isolated-vm\";\nimport { setupCore } from \"@ricsam/isolate-core\";\nimport { setupConsole } from \"@ricsam/isolate-console\";\nimport { setupEncoding } from \"@ricsam/isolate-encoding\";\nimport { setupTimers } from \"@ricsam/isolate-timers\";\nimport { setupPath } from \"@ricsam/isolate-path\";\nimport { setupCrypto } from \"@ricsam/isolate-crypto\";\nimport { setupFetch } from \"@ricsam/isolate-fetch\";\nimport { setupFs } from \"@ricsam/isolate-fs\";\nimport {\n setupTestEnvironment,\n runTests as runTestsInContext,\n hasTests as hasTestsInContext,\n getTestCount as getTestCountInContext,\n} from \"@ricsam/isolate-test-environment\";\nimport { setupPlaywright } from \"@ricsam/isolate-playwright\";\n\nimport type { ConsoleOptions, ConsoleHandle } from \"@ricsam/isolate-console\";\nimport type { FetchOptions, FetchHandle, DispatchRequestOptions, UpgradeRequest, WebSocketCommand } from \"@ricsam/isolate-fetch\";\nimport type { FsOptions, FsHandle } from \"@ricsam/isolate-fs\";\nimport type { CoreHandle } from \"@ricsam/isolate-core\";\nimport type { EncodingHandle } from \"@ricsam/isolate-encoding\";\nimport type { TimersHandle } from \"@ricsam/isolate-timers\";\nimport type { PathHandle } from \"@ricsam/isolate-path\";\nimport type { CryptoHandle } from \"@ricsam/isolate-crypto\";\nimport type {\n TestEnvironmentHandle,\n RunResults,\n TestEnvironmentOptions,\n TestEvent,\n SuiteInfo,\n SuiteResult,\n TestInfo,\n TestResult,\n TestError,\n} from \"@ricsam/isolate-test-environment\";\nimport type {\n PlaywrightHandle,\n NetworkRequestInfo,\n NetworkResponseInfo,\n BrowserConsoleLogEntry,\n PlaywrightEvent,\n} from \"@ricsam/isolate-playwright\";\nimport type {\n ConsoleCallbacks,\n ConsoleEntry,\n FetchCallback,\n ModuleLoaderCallback,\n CustomFunctionDefinition,\n CustomFunctions,\n CustomAsyncGeneratorFunction,\n DispatchOptions,\n} from \"@ricsam/isolate-protocol\";\n\n// Re-export shared types from protocol\nexport type {\n ConsoleCallbacks,\n ConsoleEntry,\n FetchCallback,\n ModuleLoaderCallback,\n CustomFunction,\n CustomFunctionDefinition,\n CustomFunctions,\n DispatchOptions,\n} from \"@ricsam/isolate-protocol\";\n\n/**\n * Options for creating a runtime.\n */\nexport interface RuntimeOptions {\n /** Memory limit in megabytes (optional) */\n memoryLimitMB?: number;\n /** Console callback handlers */\n console?: ConsoleCallbacks;\n /** Fetch callback handler */\n fetch?: FetchCallback;\n /**\n * File system options.\n * Note: For local runtime, this uses FsOptions with getDirectory returning a FileSystemHandler.\n * For remote runtime (isolate-client), use FileSystemCallbacks instead.\n */\n fs?: FsOptions;\n /** Module loader callback for resolving dynamic imports */\n moduleLoader?: ModuleLoaderCallback;\n /** Custom functions callable from within the isolate */\n customFunctions?: CustomFunctions;\n /** Current working directory for path.resolve(). Defaults to \"/\" */\n cwd?: string;\n /** Enable test environment (describe, it, expect, etc.) */\n testEnvironment?: boolean | TestEnvironmentOptions;\n /** Playwright options - user provides page object */\n playwright?: PlaywrightOptions;\n}\n\n/**\n * Options for playwright in local runtime.\n */\nexport interface PlaywrightOptions {\n /** Playwright Page object - user launches browser and creates page */\n page: import(\"playwright\").Page;\n /** Default timeout for operations (default: 30000ms) */\n timeout?: number;\n /** Base URL for relative navigation */\n baseUrl?: string;\n /** If true, browser console logs are routed through console handler (or printed to stdout if no handler) */\n console?: boolean;\n /** Unified event callback for all playwright events */\n onEvent?: (event: PlaywrightEvent) => void;\n}\n\n/**\n * Runtime fetch handle - provides access to fetch/serve operations.\n */\nexport interface RuntimeFetchHandle {\n /** Dispatch HTTP request to serve() handler */\n dispatchRequest(request: Request, options?: DispatchRequestOptions): Promise<Response>;\n /** Check if isolate requested WebSocket upgrade */\n getUpgradeRequest(): UpgradeRequest | null;\n /** Dispatch WebSocket open event to isolate */\n dispatchWebSocketOpen(connectionId: string): void;\n /** Dispatch WebSocket message event to isolate */\n dispatchWebSocketMessage(connectionId: string, message: string | ArrayBuffer): void;\n /** Dispatch WebSocket close event to isolate */\n dispatchWebSocketClose(connectionId: string, code: number, reason: string): void;\n /** Dispatch WebSocket error event to isolate */\n dispatchWebSocketError(connectionId: string, error: Error): void;\n /** Register callback for WebSocket commands from isolate */\n onWebSocketCommand(callback: (cmd: WebSocketCommand) => void): () => void;\n /** Check if serve() has been called */\n hasServeHandler(): boolean;\n /** Check if there are active WebSocket connections */\n hasActiveConnections(): boolean;\n}\n\n/**\n * Runtime timers handle - provides access to timer operations.\n * Timers fire automatically based on real time.\n */\nexport interface RuntimeTimersHandle {\n /** Clear all pending timers */\n clearAll(): void;\n}\n\n/**\n * Runtime console handle - provides access to console state.\n */\nexport interface RuntimeConsoleHandle {\n /** Reset all console state (timers, counters, group depth) */\n reset(): void;\n /** Get console.time() timers */\n getTimers(): Map<string, number>;\n /** Get console.count() counters */\n getCounters(): Map<string, number>;\n /** Get current console.group() nesting depth */\n getGroupDepth(): number;\n}\n\n/**\n * Runtime test environment handle - provides access to test execution.\n */\nexport interface RuntimeTestEnvironmentHandle {\n /** Run all registered tests */\n runTests(timeout?: number): Promise<RunResults>;\n /** Check if any tests are registered */\n hasTests(): boolean;\n /** Get count of registered tests */\n getTestCount(): number;\n /** Reset test state */\n reset(): void;\n}\n\n/**\n * Runtime playwright handle - provides access to browser data collection.\n */\nexport interface RuntimePlaywrightHandle {\n /** Get collected browser data (console logs, network requests/responses) */\n getCollectedData(): CollectedData;\n /** Clear collected browser data */\n clearCollectedData(): void;\n}\n\n/**\n * Collected browser data from playwright.\n */\nexport interface CollectedData {\n /** Browser console logs (from the page, not sandbox) */\n browserConsoleLogs: BrowserConsoleLogEntry[];\n networkRequests: NetworkRequestInfo[];\n networkResponses: NetworkResponseInfo[];\n}\n\n/**\n * Options for eval() method.\n */\nexport interface EvalOptions {\n /** Filename for stack traces */\n filename?: string;\n /** Maximum execution time in milliseconds. If exceeded, throws a timeout error. */\n maxExecutionMs?: number;\n}\n\n/**\n * Runtime handle - the main interface for interacting with the isolate.\n */\nexport interface RuntimeHandle {\n /** Unique runtime identifier */\n readonly id: string;\n /** Execute code as ES module (supports top-level await) */\n eval(code: string, filenameOrOptions?: string | EvalOptions): Promise<void>;\n /** Dispose all resources */\n dispose(): Promise<void>;\n\n /** Fetch handle - access to fetch/serve operations */\n readonly fetch: RuntimeFetchHandle;\n /** Timers handle - access to timer operations */\n readonly timers: RuntimeTimersHandle;\n /** Console handle - access to console state */\n readonly console: RuntimeConsoleHandle;\n /** Test environment handle - access to test execution (throws if not enabled) */\n readonly testEnvironment: RuntimeTestEnvironmentHandle;\n /** Playwright handle - access to playwright operations (throws if not configured) */\n readonly playwright: RuntimePlaywrightHandle;\n}\n\n// Internal state for runtime\ninterface RuntimeState {\n isolate: ivm.Isolate;\n context: ivm.Context;\n handles: {\n core?: CoreHandle;\n console?: ConsoleHandle;\n encoding?: EncodingHandle;\n timers?: TimersHandle;\n path?: PathHandle;\n crypto?: CryptoHandle;\n fetch?: FetchHandle;\n fs?: FsHandle;\n testEnvironment?: TestEnvironmentHandle;\n playwright?: PlaywrightHandle;\n };\n moduleCache: Map<string, ivm.Module>;\n moduleLoader?: ModuleLoaderCallback;\n customFunctions?: CustomFunctions;\n customFnInvokeRef?: ivm.Reference<\n (name: string, argsJson: string) => Promise<string>\n >;\n}\n\n// Iterator session tracking for async iterator custom functions\ninterface IteratorSession {\n iterator: AsyncGenerator<unknown, unknown, unknown>;\n}\n\nconst iteratorSessions = new Map<number, IteratorSession>();\nlet nextIteratorId = 1;\n\n/**\n * Setup custom functions as globals in the isolate context.\n * Each function directly calls the host callback when invoked.\n */\nasync function setupCustomFunctions(\n context: ivm.Context,\n customFunctions: CustomFunctions\n): Promise<ivm.Reference<(name: string, argsJson: string) => Promise<string>>> {\n const global = context.global;\n\n // Reference that invokes the callback and returns the result\n const invokeCallbackRef = new ivm.Reference(\n async (name: string, argsJson: string): Promise<string> => {\n const def = customFunctions[name];\n if (!def) {\n return JSON.stringify({\n ok: false,\n error: { message: `Custom function '${name}' not found`, name: \"Error\" },\n });\n }\n const args = JSON.parse(argsJson) as unknown[];\n try {\n const result = def.type === 'async' ? await def.fn(...args) : def.fn(...args);\n return JSON.stringify({ ok: true, value: result });\n } catch (error: unknown) {\n const err = error as Error;\n return JSON.stringify({\n ok: false,\n error: { message: err.message, name: err.name },\n });\n }\n }\n );\n\n global.setSync(\"__customFn_invoke\", invokeCallbackRef);\n\n // Iterator start: creates iterator, stores in session, returns iteratorId\n const iterStartRef = new ivm.Reference(\n async (name: string, argsJson: string): Promise<string> => {\n const def = customFunctions[name];\n if (!def || def.type !== 'asyncIterator') {\n return JSON.stringify({\n ok: false,\n error: { message: `Async iterator function '${name}' not found`, name: \"Error\" },\n });\n }\n try {\n const args = JSON.parse(argsJson) as unknown[];\n const fn = def.fn as CustomAsyncGeneratorFunction;\n const iterator = fn(...args);\n const iteratorId = nextIteratorId++;\n iteratorSessions.set(iteratorId, { iterator });\n return JSON.stringify({ ok: true, iteratorId });\n } catch (error: unknown) {\n const err = error as Error;\n return JSON.stringify({\n ok: false,\n error: { message: err.message, name: err.name },\n });\n }\n }\n );\n\n global.setSync(\"__iter_start\", iterStartRef);\n\n // Iterator next: calls iterator.next(), returns {done, value}\n const iterNextRef = new ivm.Reference(\n async (iteratorId: number): Promise<string> => {\n const session = iteratorSessions.get(iteratorId);\n if (!session) {\n return JSON.stringify({\n ok: false,\n error: { message: `Iterator session ${iteratorId} not found`, name: \"Error\" },\n });\n }\n try {\n const result = await session.iterator.next();\n if (result.done) {\n iteratorSessions.delete(iteratorId);\n }\n return JSON.stringify({ ok: true, done: result.done, value: result.value });\n } catch (error: unknown) {\n const err = error as Error;\n iteratorSessions.delete(iteratorId);\n return JSON.stringify({\n ok: false,\n error: { message: err.message, name: err.name },\n });\n }\n }\n );\n\n global.setSync(\"__iter_next\", iterNextRef);\n\n // Iterator return: calls iterator.return(), cleans up session\n const iterReturnRef = new ivm.Reference(\n async (iteratorId: number, valueJson: string): Promise<string> => {\n const session = iteratorSessions.get(iteratorId);\n if (!session) {\n return JSON.stringify({ ok: true, done: true, value: undefined });\n }\n try {\n const value = valueJson ? JSON.parse(valueJson) : undefined;\n const result = await session.iterator.return?.(value);\n iteratorSessions.delete(iteratorId);\n return JSON.stringify({ ok: true, done: true, value: result?.value });\n } catch (error: unknown) {\n const err = error as Error;\n iteratorSessions.delete(iteratorId);\n return JSON.stringify({\n ok: false,\n error: { message: err.message, name: err.name },\n });\n }\n }\n );\n\n global.setSync(\"__iter_return\", iterReturnRef);\n\n // Iterator throw: calls iterator.throw(), cleans up session\n const iterThrowRef = new ivm.Reference(\n async (iteratorId: number, errorJson: string): Promise<string> => {\n const session = iteratorSessions.get(iteratorId);\n if (!session) {\n return JSON.stringify({\n ok: false,\n error: { message: `Iterator session ${iteratorId} not found`, name: \"Error\" },\n });\n }\n try {\n const errorData = JSON.parse(errorJson) as { message: string; name: string };\n const error = Object.assign(new Error(errorData.message), { name: errorData.name });\n const result = await session.iterator.throw?.(error);\n iteratorSessions.delete(iteratorId);\n return JSON.stringify({ ok: true, done: result?.done ?? true, value: result?.value });\n } catch (error: unknown) {\n const err = error as Error;\n iteratorSessions.delete(iteratorId);\n return JSON.stringify({\n ok: false,\n error: { message: err.message, name: err.name },\n });\n }\n }\n );\n\n global.setSync(\"__iter_throw\", iterThrowRef);\n\n // Create wrapper functions for each custom function\n for (const name of Object.keys(customFunctions)) {\n const def = customFunctions[name]!;\n\n if (def.type === 'async') {\n // Async function: use applySyncPromise and async function wrapper\n context.evalSync(`\n globalThis.${name} = async function(...args) {\n const resultJson = __customFn_invoke.applySyncPromise(\n undefined,\n [\"${name}\", JSON.stringify(args)]\n );\n const result = JSON.parse(resultJson);\n if (result.ok) {\n return result.value;\n } else {\n const error = new Error(result.error.message);\n error.name = result.error.name;\n throw error;\n }\n };\n `);\n } else if (def.type === 'sync') {\n // Sync function: use applySyncPromise (to await the host) but wrap in regular function\n // The function blocks until the host responds, but returns the value directly (not a Promise)\n context.evalSync(`\n globalThis.${name} = function(...args) {\n const resultJson = __customFn_invoke.applySyncPromise(\n undefined,\n [\"${name}\", JSON.stringify(args)]\n );\n const result = JSON.parse(resultJson);\n if (result.ok) {\n return result.value;\n } else {\n const error = new Error(result.error.message);\n error.name = result.error.name;\n throw error;\n }\n };\n `);\n } else if (def.type === 'asyncIterator') {\n // Async iterator function: returns an async iterable object\n context.evalSync(`\n globalThis.${name} = function(...args) {\n const startResult = JSON.parse(__iter_start.applySyncPromise(undefined, [\"${name}\", JSON.stringify(args)]));\n if (!startResult.ok) {\n throw Object.assign(new Error(startResult.error.message), { name: startResult.error.name });\n }\n const iteratorId = startResult.iteratorId;\n return {\n [Symbol.asyncIterator]() { return this; },\n async next() {\n const result = JSON.parse(__iter_next.applySyncPromise(undefined, [iteratorId]));\n if (!result.ok) {\n throw Object.assign(new Error(result.error.message), { name: result.error.name });\n }\n return { done: result.done, value: result.value };\n },\n async return(v) {\n const result = JSON.parse(__iter_return.applySyncPromise(undefined, [iteratorId, JSON.stringify(v)]));\n return { done: true, value: result.value };\n },\n async throw(e) {\n const result = JSON.parse(__iter_throw.applySyncPromise(undefined, [iteratorId, JSON.stringify({ message: e.message, name: e.name })]));\n if (!result.ok) {\n throw Object.assign(new Error(result.error.message), { name: result.error.name });\n }\n return { done: result.done, value: result.value };\n }\n };\n };\n `);\n }\n }\n\n return invokeCallbackRef;\n}\n\n/**\n * Create a module resolver function for local execution.\n */\nfunction createModuleResolver(\n state: RuntimeState\n): (specifier: string, referrer: ivm.Module) => Promise<ivm.Module> {\n return async (\n specifier: string,\n _referrer: ivm.Module\n ): Promise<ivm.Module> => {\n // Check cache first\n const cached = state.moduleCache.get(specifier);\n if (cached) return cached;\n\n if (!state.moduleLoader) {\n throw new Error(`No module loader registered. Cannot import: ${specifier}`);\n }\n\n // Invoke module loader to get source code\n const code = await state.moduleLoader(specifier);\n\n // Compile the module\n const mod = await state.isolate.compileModule(code, {\n filename: specifier,\n });\n\n // Cache before instantiation (for circular dependencies)\n state.moduleCache.set(specifier, mod);\n\n // Instantiate with recursive resolver\n const resolver = createModuleResolver(state);\n await mod.instantiate(state.context, resolver);\n\n return mod;\n };\n}\n\n/**\n * Convert FetchCallback to FetchOptions\n */\nfunction convertFetchCallback(callback?: FetchCallback): FetchOptions {\n if (!callback) {\n return {};\n }\n return {\n onFetch: async (request: Request): Promise<Response> => {\n // Wrap the result in a Promise to handle both sync and async callbacks\n return Promise.resolve(callback(request));\n },\n };\n}\n\n/**\n * Create a fully configured isolated-vm runtime\n *\n * Sets up all WHATWG APIs: fetch, fs, console, crypto, encoding, timers\n *\n * @example\n * const runtime = await createRuntime({\n * console: { log: (...args) => console.log(\"[isolate]\", ...args) },\n * fetch: async (request) => fetch(request),\n * });\n *\n * await runtime.eval(`\n * console.log(\"Hello from sandbox!\");\n * const response = await fetch(\"https://example.com\");\n * `);\n *\n * await runtime.dispose();\n */\nexport async function createRuntime(\n options?: RuntimeOptions\n): Promise<RuntimeHandle> {\n const opts = options ?? {};\n\n // Generate unique ID\n const id = crypto.randomUUID();\n\n // Create isolate with optional memory limit\n const isolate = new ivm.Isolate({\n memoryLimit: opts.memoryLimitMB,\n });\n const context = await isolate.createContext();\n\n // Initialize state\n const state: RuntimeState = {\n isolate,\n context,\n handles: {},\n moduleCache: new Map(),\n moduleLoader: opts.moduleLoader,\n customFunctions: opts.customFunctions,\n };\n\n // Setup all APIs in order\n // Core must be first as it provides Blob, File, streams, URL, etc.\n state.handles.core = await setupCore(context);\n\n // Console\n state.handles.console = await setupConsole(context, opts.console);\n\n // Encoding (btoa/atob)\n state.handles.encoding = await setupEncoding(context);\n\n // Timers (setTimeout, setInterval)\n state.handles.timers = await setupTimers(context);\n\n // Path module\n state.handles.path = await setupPath(context, { cwd: opts.cwd });\n\n // Crypto (randomUUID, getRandomValues)\n state.handles.crypto = await setupCrypto(context);\n\n // Fetch API - convert callback to options\n state.handles.fetch = await setupFetch(\n context,\n convertFetchCallback(opts.fetch)\n );\n\n // File system (only if handler provided)\n if (opts.fs) {\n state.handles.fs = await setupFs(context, opts.fs);\n }\n\n // Setup custom functions\n if (opts.customFunctions) {\n state.customFnInvokeRef = await setupCustomFunctions(\n context,\n opts.customFunctions\n );\n }\n\n // Setup test environment (if enabled)\n if (opts.testEnvironment) {\n const testEnvOptions: TestEnvironmentOptions | undefined =\n typeof opts.testEnvironment === \"object\" ? opts.testEnvironment : undefined;\n state.handles.testEnvironment = await setupTestEnvironment(context, testEnvOptions);\n }\n\n // Setup playwright (if page provided) - AFTER test environment so expect can be extended\n if (opts.playwright) {\n // Determine event handler\n // If console: true and we have a console handler, wrap onEvent to route browser logs\n let eventCallback = opts.playwright.onEvent;\n\n if (opts.playwright.console && opts.console?.onEntry) {\n const originalCallback = eventCallback;\n const consoleHandler = opts.console.onEntry;\n eventCallback = (event) => {\n // Call original callback if provided\n if (originalCallback) {\n originalCallback(event);\n }\n // Route browser console logs through console handler as browserOutput entry\n if (event.type === \"browserConsoleLog\") {\n consoleHandler({\n type: \"browserOutput\",\n level: event.level,\n args: event.args,\n timestamp: event.timestamp,\n });\n }\n };\n }\n\n state.handles.playwright = await setupPlaywright(context, {\n page: opts.playwright.page,\n timeout: opts.playwright.timeout,\n baseUrl: opts.playwright.baseUrl,\n // Don't print directly if routing through console handler\n console: opts.playwright.console && !opts.console?.onEntry,\n onEvent: eventCallback,\n });\n }\n\n // Create fetch handle wrapper\n const fetchHandle: RuntimeFetchHandle = {\n async dispatchRequest(request: Request, options?: DispatchRequestOptions): Promise<Response> {\n if (!state.handles.fetch) {\n throw new Error(\"Fetch handle not available\");\n }\n return state.handles.fetch.dispatchRequest(request, options);\n },\n getUpgradeRequest() {\n if (!state.handles.fetch) {\n throw new Error(\"Fetch handle not available\");\n }\n return state.handles.fetch.getUpgradeRequest();\n },\n dispatchWebSocketOpen(connectionId: string) {\n if (!state.handles.fetch) {\n throw new Error(\"Fetch handle not available\");\n }\n state.handles.fetch.dispatchWebSocketOpen(connectionId);\n },\n dispatchWebSocketMessage(connectionId: string, message: string | ArrayBuffer) {\n if (!state.handles.fetch) {\n throw new Error(\"Fetch handle not available\");\n }\n state.handles.fetch.dispatchWebSocketMessage(connectionId, message);\n },\n dispatchWebSocketClose(connectionId: string, code: number, reason: string) {\n if (!state.handles.fetch) {\n throw new Error(\"Fetch handle not available\");\n }\n state.handles.fetch.dispatchWebSocketClose(connectionId, code, reason);\n },\n dispatchWebSocketError(connectionId: string, error: Error) {\n if (!state.handles.fetch) {\n throw new Error(\"Fetch handle not available\");\n }\n state.handles.fetch.dispatchWebSocketError(connectionId, error);\n },\n onWebSocketCommand(callback: (cmd: WebSocketCommand) => void) {\n if (!state.handles.fetch) {\n throw new Error(\"Fetch handle not available\");\n }\n return state.handles.fetch.onWebSocketCommand(callback);\n },\n hasServeHandler() {\n if (!state.handles.fetch) {\n throw new Error(\"Fetch handle not available\");\n }\n return state.handles.fetch.hasServeHandler();\n },\n hasActiveConnections() {\n if (!state.handles.fetch) {\n throw new Error(\"Fetch handle not available\");\n }\n return state.handles.fetch.hasActiveConnections();\n },\n };\n\n // Create timers handle wrapper\n const timersHandle: RuntimeTimersHandle = {\n clearAll() {\n state.handles.timers?.clearAll();\n },\n };\n\n // Create console handle wrapper\n const consoleHandle: RuntimeConsoleHandle = {\n reset() {\n state.handles.console?.reset();\n },\n getTimers() {\n return state.handles.console?.getTimers() ?? new Map();\n },\n getCounters() {\n return state.handles.console?.getCounters() ?? new Map();\n },\n getGroupDepth() {\n return state.handles.console?.getGroupDepth() ?? 0;\n },\n };\n\n // Create test environment handle wrapper\n const testEnvironmentHandle: RuntimeTestEnvironmentHandle = {\n async runTests(_timeout?: number): Promise<RunResults> {\n if (!state.handles.testEnvironment) {\n throw new Error(\"Test environment not enabled. Set testEnvironment: true in createRuntime options.\");\n }\n // Note: timeout parameter reserved for future use\n return runTestsInContext(state.context);\n },\n hasTests(): boolean {\n if (!state.handles.testEnvironment) {\n throw new Error(\"Test environment not enabled. Set testEnvironment: true in createRuntime options.\");\n }\n return hasTestsInContext(state.context);\n },\n getTestCount(): number {\n if (!state.handles.testEnvironment) {\n throw new Error(\"Test environment not enabled. Set testEnvironment: true in createRuntime options.\");\n }\n return getTestCountInContext(state.context);\n },\n reset() {\n state.handles.testEnvironment?.dispose();\n },\n };\n\n // Create playwright handle wrapper\n const playwrightHandle: RuntimePlaywrightHandle = {\n getCollectedData(): CollectedData {\n if (!state.handles.playwright) {\n throw new Error(\"Playwright not configured. Provide playwright.page in createRuntime options.\");\n }\n return {\n browserConsoleLogs: state.handles.playwright.getBrowserConsoleLogs(),\n networkRequests: state.handles.playwright.getNetworkRequests(),\n networkResponses: state.handles.playwright.getNetworkResponses(),\n };\n },\n clearCollectedData() {\n state.handles.playwright?.clearCollected();\n },\n };\n\n return {\n id,\n\n // Module handles\n fetch: fetchHandle,\n timers: timersHandle,\n console: consoleHandle,\n testEnvironment: testEnvironmentHandle,\n playwright: playwrightHandle,\n\n async eval(code: string, filenameOrOptions?: string | EvalOptions): Promise<void> {\n // Parse options\n const options = typeof filenameOrOptions === 'string'\n ? { filename: filenameOrOptions }\n : filenameOrOptions;\n\n // Compile as ES module\n const mod = await state.isolate.compileModule(code, {\n filename: options?.filename ?? \"<eval>\",\n });\n\n // Instantiate with module resolver\n const resolver = createModuleResolver(state);\n await mod.instantiate(state.context, resolver);\n\n // Evaluate the module with optional timeout\n await mod.evaluate(options?.maxExecutionMs ? { timeout: options.maxExecutionMs } : undefined);\n },\n\n async dispose(): Promise<void> {\n // Dispose custom function reference\n if (state.customFnInvokeRef) {\n state.customFnInvokeRef.release();\n }\n\n // Dispose all handles (in reverse order of setup)\n state.handles.playwright?.dispose();\n state.handles.testEnvironment?.dispose();\n state.handles.fs?.dispose();\n state.handles.fetch?.dispose();\n state.handles.crypto?.dispose();\n state.handles.path?.dispose();\n state.handles.timers?.dispose();\n state.handles.encoding?.dispose();\n state.handles.console?.dispose();\n state.handles.core?.dispose();\n\n // Clear module cache\n state.moduleCache.clear();\n\n // Release context and dispose isolate\n state.context.release();\n state.isolate.dispose();\n },\n };\n}\n\n// Re-export all package types and functions\nexport { setupCore } from \"@ricsam/isolate-core\";\nexport type { CoreHandle, SetupCoreOptions } from \"@ricsam/isolate-core\";\n\nexport { setupConsole, simpleConsoleHandler } from \"@ricsam/isolate-console\";\nexport type { ConsoleHandle, ConsoleOptions, ConsoleEntry as ConsoleEntryFromConsole, SimpleConsoleCallbacks } from \"@ricsam/isolate-console\";\n\nexport { setupCrypto } from \"@ricsam/isolate-crypto\";\nexport type { CryptoHandle } from \"@ricsam/isolate-crypto\";\n\nexport { setupEncoding } from \"@ricsam/isolate-encoding\";\nexport type { EncodingHandle } from \"@ricsam/isolate-encoding\";\n\nexport { setupFetch } from \"@ricsam/isolate-fetch\";\nexport type { FetchHandle, FetchOptions, WebSocketCommand, UpgradeRequest } from \"@ricsam/isolate-fetch\";\n\nexport { setupFs, createNodeFileSystemHandler } from \"@ricsam/isolate-fs\";\nexport type { FsHandle, FsOptions, FileSystemHandler, NodeFileSystemHandlerOptions } from \"@ricsam/isolate-fs\";\n\nexport { setupPath } from \"@ricsam/isolate-path\";\nexport type { PathHandle, PathOptions } from \"@ricsam/isolate-path\";\n\nexport { setupTimers } from \"@ricsam/isolate-timers\";\nexport type { TimersHandle } from \"@ricsam/isolate-timers\";\n\nexport { setupTestEnvironment, runTests, hasTests, getTestCount } from \"@ricsam/isolate-test-environment\";\nexport type {\n TestEnvironmentHandle,\n TestEnvironmentOptions,\n RunResults,\n TestResult,\n TestInfo,\n TestError,\n TestEvent,\n SuiteInfo,\n SuiteResult,\n} from \"@ricsam/isolate-test-environment\";\n\nexport { setupPlaywright, createPlaywrightHandler } from \"@ricsam/isolate-playwright\";\nexport type {\n PlaywrightHandle,\n PlaywrightSetupOptions,\n PlaywrightCallback,\n PlaywrightEvent,\n NetworkRequestInfo,\n NetworkResponseInfo,\n BrowserConsoleLogEntry,\n} from \"@ricsam/isolate-playwright\";\n\nexport * from './internal.mjs';"
6
6
  ],
7
- "mappings": ";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAqIA,sBAAS;AAGT,yBAAS;AAGT,wBAAS;AAGT,0BAAS;AAGT,uBAAS;AAGT,oBAAS;AAGT,sBAAS;AAGT,wBAAS;AAjGT,eAAsB,aAAa,CACjC,SACwB;AAAA,EACxB,MAAM,OAAO,WAAW,CAAC;AAAA,EAGzB,MAAM,UAAU,IAAI,IAAI,QAAQ;AAAA,IAC9B,aAAa,KAAK;AAAA,EACpB,CAAC;AAAA,EACD,MAAM,UAAU,MAAM,QAAQ,cAAc;AAAA,EAG5C,MAAM,UASF,CAAC;AAAA,EAIL,QAAQ,OAAO,MAAM,UAAU,OAAO;AAAA,EAGtC,QAAQ,UAAU,MAAM,aAAa,SAAS,KAAK,OAAO;AAAA,EAG1D,QAAQ,WAAW,MAAM,cAAc,OAAO;AAAA,EAG9C,QAAQ,SAAS,MAAM,YAAY,OAAO;AAAA,EAG1C,QAAQ,OAAO,MAAM,UAAU,OAAO;AAAA,EAGtC,QAAQ,SAAS,MAAM,YA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,MAClC,MAAM,QAAQ,iBAAiB,QAAQ;AAAA,MACvC,MAAM,QAAQ,IAAI,QAAQ;AAAA,MAC1B,MAAM,QAAQ,OAAO,QAAQ;AAAA,MAC7B,MAAM,QAAQ,QAAQ,QAAQ;AAAA,MAC9B,MAAM,QAAQ,MAAM,QAAQ;AAAA,MAC5B,MAAM,QAAQ,QAAQ,QAAQ;AAAA,MAC9B,MAAM,QAAQ,UAAU,QAAQ;AAAA,MAChC,MAAM,QAAQ,SAAS,QAAQ;AAAA,MAC/B,MAAM,QAAQ,MAAM,QAAQ;AAAA,MAG5B,MAAM,YAAY,MAAM;AAAA,MAGxB,MAAM,QAAQ,QAAQ;AAAA,MACtB,MAAM,QAAQ,QAAQ;AAAA;AAAA,EAE1B;AAAA;",
8
+ "debugId": "727908D2ABC724D264756E2164756E21",
9
9
  "names": []
10
10
  }
@@ -0,0 +1,53 @@
1
+ // @bun
2
+ // packages/runtime/src/internal.ts
3
+ import ivm from "isolated-vm";
4
+ import { setupCore } from "@ricsam/isolate-core";
5
+ import { setupConsole } from "@ricsam/isolate-console";
6
+ import { setupEncoding } from "@ricsam/isolate-encoding";
7
+ import { setupTimers } from "@ricsam/isolate-timers";
8
+ import { setupPath } from "@ricsam/isolate-path";
9
+ import { setupCrypto } from "@ricsam/isolate-crypto";
10
+ import { setupFetch } from "@ricsam/isolate-fetch";
11
+ import { setupFs } from "@ricsam/isolate-fs";
12
+ async function createInternalRuntime(options) {
13
+ const opts = options ?? {};
14
+ const isolate = new ivm.Isolate({
15
+ memoryLimit: opts.memoryLimitMB
16
+ });
17
+ const context = await isolate.createContext();
18
+ const handles = {};
19
+ handles.core = await setupCore(context);
20
+ handles.console = await setupConsole(context, opts.console);
21
+ handles.encoding = await setupEncoding(context);
22
+ handles.timers = await setupTimers(context);
23
+ handles.path = await setupPath(context, { cwd: opts.cwd });
24
+ handles.crypto = await setupCrypto(context);
25
+ handles.fetch = await setupFetch(context, opts.fetch);
26
+ if (opts.fs) {
27
+ handles.fs = await setupFs(context, opts.fs);
28
+ }
29
+ return {
30
+ isolate,
31
+ context,
32
+ fetch: handles.fetch,
33
+ timers: handles.timers,
34
+ console: handles.console,
35
+ dispose() {
36
+ handles.fs?.dispose();
37
+ handles.fetch?.dispose();
38
+ handles.crypto?.dispose();
39
+ handles.path?.dispose();
40
+ handles.timers?.dispose();
41
+ handles.encoding?.dispose();
42
+ handles.console?.dispose();
43
+ handles.core?.dispose();
44
+ context.release();
45
+ isolate.dispose();
46
+ }
47
+ };
48
+ }
49
+ export {
50
+ createInternalRuntime
51
+ };
52
+
53
+ //# debugId=BAB9B34244E5F7FC64756E2164756E21
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/internal.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * Internal APIs for @ricsam/isolate-runtime\n *\n * These are not part of the public API and may change without notice.\n * Only used by @ricsam/isolate-daemon internally.\n */\n\nimport ivm from \"isolated-vm\";\nimport { setupCore } from \"@ricsam/isolate-core\";\nimport { setupConsole } from \"@ricsam/isolate-console\";\nimport { setupEncoding } from \"@ricsam/isolate-encoding\";\nimport { setupTimers } from \"@ricsam/isolate-timers\";\nimport { setupPath } from \"@ricsam/isolate-path\";\nimport { setupCrypto } from \"@ricsam/isolate-crypto\";\nimport { setupFetch } from \"@ricsam/isolate-fetch\";\nimport { setupFs } from \"@ricsam/isolate-fs\";\n\nimport type { ConsoleOptions, ConsoleHandle } from \"@ricsam/isolate-console\";\nimport type { FetchOptions, FetchHandle } from \"@ricsam/isolate-fetch\";\nimport type { FsOptions, FsHandle } from \"@ricsam/isolate-fs\";\nimport type { CoreHandle } from \"@ricsam/isolate-core\";\nimport type { EncodingHandle } from \"@ricsam/isolate-encoding\";\nimport type { TimersHandle } from \"@ricsam/isolate-timers\";\nimport type { PathHandle } from \"@ricsam/isolate-path\";\nimport type { CryptoHandle } from \"@ricsam/isolate-crypto\";\n\n/**\n * @internal Options for creating a legacy runtime.\n */\nexport interface InternalRuntimeOptions {\n memoryLimitMB?: number;\n console?: ConsoleOptions;\n fetch?: FetchOptions;\n fs?: FsOptions;\n /** Current working directory for path.resolve(). Defaults to \"/\" */\n cwd?: string;\n}\n\n/**\n * @internal Runtime handle with direct isolate/context access.\n * Used by isolate-daemon for low-level operations.\n */\nexport interface InternalRuntimeHandle {\n readonly isolate: ivm.Isolate;\n readonly context: ivm.Context;\n readonly fetch: FetchHandle;\n readonly timers: TimersHandle;\n readonly console: ConsoleHandle;\n dispose(): void;\n}\n\n/**\n * @internal Create a runtime with direct access to isolate and context.\n * This is for internal use by @ricsam/isolate-daemon only.\n */\nexport async function createInternalRuntime(\n options?: InternalRuntimeOptions\n): Promise<InternalRuntimeHandle> {\n const opts = options ?? {};\n\n const isolate = new ivm.Isolate({\n memoryLimit: opts.memoryLimitMB,\n });\n\n const context = await isolate.createContext();\n\n // Setup all APIs\n const handles: {\n core?: CoreHandle;\n console?: ConsoleHandle;\n encoding?: EncodingHandle;\n timers?: TimersHandle;\n path?: PathHandle;\n crypto?: CryptoHandle;\n fetch?: FetchHandle;\n fs?: FsHandle;\n } = {};\n\n handles.core = await setupCore(context);\n handles.console = await setupConsole(context, opts.console);\n handles.encoding = await setupEncoding(context);\n handles.timers = await setupTimers(context);\n handles.path = await setupPath(context, { cwd: opts.cwd });\n handles.crypto = await setupCrypto(context);\n handles.fetch = await setupFetch(context, opts.fetch);\n\n if (opts.fs) {\n handles.fs = await setupFs(context, opts.fs);\n }\n\n return {\n isolate,\n context,\n fetch: handles.fetch,\n timers: handles.timers!,\n console: handles.console!,\n dispose() {\n handles.fs?.dispose();\n handles.fetch?.dispose();\n handles.crypto?.dispose();\n handles.path?.dispose();\n handles.timers?.dispose();\n handles.encoding?.dispose();\n handles.console?.dispose();\n handles.core?.dispose();\n context.release();\n isolate.dispose();\n },\n };\n}\n"
6
+ ],
7
+ "mappings": ";;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAwCA,eAAsB,qBAAqB,CACz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{
2
2
  "name": "@ricsam/isolate-runtime",
3
- "version": "0.1.4",
3
+ "version": "0.1.7",
4
4
  "type": "module"
5
5
  }
@@ -1,28 +1,155 @@
1
- import ivm from "isolated-vm";
2
- import type { ConsoleOptions } from "@ricsam/isolate-console";
3
- import type { FetchOptions, FetchHandle } from "@ricsam/isolate-fetch";
1
+ import type { DispatchRequestOptions, UpgradeRequest, WebSocketCommand } from "@ricsam/isolate-fetch";
4
2
  import type { FsOptions } from "@ricsam/isolate-fs";
3
+ import type { RunResults, TestEnvironmentOptions } from "@ricsam/isolate-test-environment";
4
+ import type { NetworkRequestInfo, NetworkResponseInfo, BrowserConsoleLogEntry, PlaywrightEvent } from "@ricsam/isolate-playwright";
5
+ import type { ConsoleCallbacks, FetchCallback, ModuleLoaderCallback, CustomFunctions } from "@ricsam/isolate-protocol";
6
+ export type { ConsoleCallbacks, ConsoleEntry, FetchCallback, ModuleLoaderCallback, CustomFunction, CustomFunctionDefinition, CustomFunctions, DispatchOptions, } from "@ricsam/isolate-protocol";
7
+ /**
8
+ * Options for creating a runtime.
9
+ */
5
10
  export interface RuntimeOptions {
6
- /** Isolate memory limit in MB */
7
- memoryLimit?: number;
8
- /** Console options */
9
- console?: ConsoleOptions;
10
- /** Fetch options */
11
- fetch?: FetchOptions;
12
- /** File system options (optional - fs only set up if provided) */
11
+ /** Memory limit in megabytes (optional) */
12
+ memoryLimitMB?: number;
13
+ /** Console callback handlers */
14
+ console?: ConsoleCallbacks;
15
+ /** Fetch callback handler */
16
+ fetch?: FetchCallback;
17
+ /**
18
+ * File system options.
19
+ * Note: For local runtime, this uses FsOptions with getDirectory returning a FileSystemHandler.
20
+ * For remote runtime (isolate-client), use FileSystemCallbacks instead.
21
+ */
13
22
  fs?: FsOptions;
23
+ /** Module loader callback for resolving dynamic imports */
24
+ moduleLoader?: ModuleLoaderCallback;
25
+ /** Custom functions callable from within the isolate */
26
+ customFunctions?: CustomFunctions;
27
+ /** Current working directory for path.resolve(). Defaults to "/" */
28
+ cwd?: string;
29
+ /** Enable test environment (describe, it, expect, etc.) */
30
+ testEnvironment?: boolean | TestEnvironmentOptions;
31
+ /** Playwright options - user provides page object */
32
+ playwright?: PlaywrightOptions;
33
+ }
34
+ /**
35
+ * Options for playwright in local runtime.
36
+ */
37
+ export interface PlaywrightOptions {
38
+ /** Playwright Page object - user launches browser and creates page */
39
+ page: import("playwright").Page;
40
+ /** Default timeout for operations (default: 30000ms) */
41
+ timeout?: number;
42
+ /** Base URL for relative navigation */
43
+ baseUrl?: string;
44
+ /** If true, browser console logs are routed through console handler (or printed to stdout if no handler) */
45
+ console?: boolean;
46
+ /** Unified event callback for all playwright events */
47
+ onEvent?: (event: PlaywrightEvent) => void;
48
+ }
49
+ /**
50
+ * Runtime fetch handle - provides access to fetch/serve operations.
51
+ */
52
+ export interface RuntimeFetchHandle {
53
+ /** Dispatch HTTP request to serve() handler */
54
+ dispatchRequest(request: Request, options?: DispatchRequestOptions): Promise<Response>;
55
+ /** Check if isolate requested WebSocket upgrade */
56
+ getUpgradeRequest(): UpgradeRequest | null;
57
+ /** Dispatch WebSocket open event to isolate */
58
+ dispatchWebSocketOpen(connectionId: string): void;
59
+ /** Dispatch WebSocket message event to isolate */
60
+ dispatchWebSocketMessage(connectionId: string, message: string | ArrayBuffer): void;
61
+ /** Dispatch WebSocket close event to isolate */
62
+ dispatchWebSocketClose(connectionId: string, code: number, reason: string): void;
63
+ /** Dispatch WebSocket error event to isolate */
64
+ dispatchWebSocketError(connectionId: string, error: Error): void;
65
+ /** Register callback for WebSocket commands from isolate */
66
+ onWebSocketCommand(callback: (cmd: WebSocketCommand) => void): () => void;
67
+ /** Check if serve() has been called */
68
+ hasServeHandler(): boolean;
69
+ /** Check if there are active WebSocket connections */
70
+ hasActiveConnections(): boolean;
71
+ }
72
+ /**
73
+ * Runtime timers handle - provides access to timer operations.
74
+ * Timers fire automatically based on real time.
75
+ */
76
+ export interface RuntimeTimersHandle {
77
+ /** Clear all pending timers */
78
+ clearAll(): void;
79
+ }
80
+ /**
81
+ * Runtime console handle - provides access to console state.
82
+ */
83
+ export interface RuntimeConsoleHandle {
84
+ /** Reset all console state (timers, counters, group depth) */
85
+ reset(): void;
86
+ /** Get console.time() timers */
87
+ getTimers(): Map<string, number>;
88
+ /** Get console.count() counters */
89
+ getCounters(): Map<string, number>;
90
+ /** Get current console.group() nesting depth */
91
+ getGroupDepth(): number;
14
92
  }
93
+ /**
94
+ * Runtime test environment handle - provides access to test execution.
95
+ */
96
+ export interface RuntimeTestEnvironmentHandle {
97
+ /** Run all registered tests */
98
+ runTests(timeout?: number): Promise<RunResults>;
99
+ /** Check if any tests are registered */
100
+ hasTests(): boolean;
101
+ /** Get count of registered tests */
102
+ getTestCount(): number;
103
+ /** Reset test state */
104
+ reset(): void;
105
+ }
106
+ /**
107
+ * Runtime playwright handle - provides access to browser data collection.
108
+ */
109
+ export interface RuntimePlaywrightHandle {
110
+ /** Get collected browser data (console logs, network requests/responses) */
111
+ getCollectedData(): CollectedData;
112
+ /** Clear collected browser data */
113
+ clearCollectedData(): void;
114
+ }
115
+ /**
116
+ * Collected browser data from playwright.
117
+ */
118
+ export interface CollectedData {
119
+ /** Browser console logs (from the page, not sandbox) */
120
+ browserConsoleLogs: BrowserConsoleLogEntry[];
121
+ networkRequests: NetworkRequestInfo[];
122
+ networkResponses: NetworkResponseInfo[];
123
+ }
124
+ /**
125
+ * Options for eval() method.
126
+ */
127
+ export interface EvalOptions {
128
+ /** Filename for stack traces */
129
+ filename?: string;
130
+ /** Maximum execution time in milliseconds. If exceeded, throws a timeout error. */
131
+ maxExecutionMs?: number;
132
+ }
133
+ /**
134
+ * Runtime handle - the main interface for interacting with the isolate.
135
+ */
15
136
  export interface RuntimeHandle {
16
- /** The isolate instance */
17
- readonly isolate: ivm.Isolate;
18
- /** The context instance */
19
- readonly context: ivm.Context;
20
- /** The fetch handle for serve() and WebSocket dispatching */
21
- readonly fetch: FetchHandle;
22
- /** Process pending timers */
23
- tick(ms?: number): Promise<void>;
137
+ /** Unique runtime identifier */
138
+ readonly id: string;
139
+ /** Execute code as ES module (supports top-level await) */
140
+ eval(code: string, filenameOrOptions?: string | EvalOptions): Promise<void>;
24
141
  /** Dispose all resources */
25
- dispose(): void;
142
+ dispose(): Promise<void>;
143
+ /** Fetch handle - access to fetch/serve operations */
144
+ readonly fetch: RuntimeFetchHandle;
145
+ /** Timers handle - access to timer operations */
146
+ readonly timers: RuntimeTimersHandle;
147
+ /** Console handle - access to console state */
148
+ readonly console: RuntimeConsoleHandle;
149
+ /** Test environment handle - access to test execution (throws if not enabled) */
150
+ readonly testEnvironment: RuntimeTestEnvironmentHandle;
151
+ /** Playwright handle - access to playwright operations (throws if not configured) */
152
+ readonly playwright: RuntimePlaywrightHandle;
26
153
  }
27
154
  /**
28
155
  * Create a fully configured isolated-vm runtime
@@ -31,26 +158,22 @@ export interface RuntimeHandle {
31
158
  *
32
159
  * @example
33
160
  * const runtime = await createRuntime({
34
- * console: {
35
- * onLog: (level, ...args) => console.log(`[${level}]`, ...args)
36
- * },
37
- * fetch: {
38
- * onFetch: async (request) => fetch(request)
39
- * }
161
+ * console: { log: (...args) => console.log("[isolate]", ...args) },
162
+ * fetch: async (request) => fetch(request),
40
163
  * });
41
164
  *
42
- * await runtime.context.eval(`
165
+ * await runtime.eval(`
43
166
  * console.log("Hello from sandbox!");
44
167
  * const response = await fetch("https://example.com");
45
168
  * `);
46
169
  *
47
- * runtime.dispose();
170
+ * await runtime.dispose();
48
171
  */
49
172
  export declare function createRuntime(options?: RuntimeOptions): Promise<RuntimeHandle>;
50
173
  export { setupCore } from "@ricsam/isolate-core";
51
174
  export type { CoreHandle, SetupCoreOptions } from "@ricsam/isolate-core";
52
- export { setupConsole } from "@ricsam/isolate-console";
53
- export type { ConsoleHandle, ConsoleOptions } from "@ricsam/isolate-console";
175
+ export { setupConsole, simpleConsoleHandler } from "@ricsam/isolate-console";
176
+ export type { ConsoleHandle, ConsoleOptions, ConsoleEntry as ConsoleEntryFromConsole, SimpleConsoleCallbacks } from "@ricsam/isolate-console";
54
177
  export { setupCrypto } from "@ricsam/isolate-crypto";
55
178
  export type { CryptoHandle } from "@ricsam/isolate-crypto";
56
179
  export { setupEncoding } from "@ricsam/isolate-encoding";
@@ -60,6 +183,11 @@ export type { FetchHandle, FetchOptions, WebSocketCommand, UpgradeRequest } from
60
183
  export { setupFs, createNodeFileSystemHandler } from "@ricsam/isolate-fs";
61
184
  export type { FsHandle, FsOptions, FileSystemHandler, NodeFileSystemHandlerOptions } from "@ricsam/isolate-fs";
62
185
  export { setupPath } from "@ricsam/isolate-path";
63
- export type { PathHandle } from "@ricsam/isolate-path";
186
+ export type { PathHandle, PathOptions } from "@ricsam/isolate-path";
64
187
  export { setupTimers } from "@ricsam/isolate-timers";
65
188
  export type { TimersHandle } from "@ricsam/isolate-timers";
189
+ export { setupTestEnvironment, runTests, hasTests, getTestCount } from "@ricsam/isolate-test-environment";
190
+ export type { TestEnvironmentHandle, TestEnvironmentOptions, RunResults, TestResult, TestInfo, TestError, TestEvent, SuiteInfo, SuiteResult, } from "@ricsam/isolate-test-environment";
191
+ export { setupPlaywright, createPlaywrightHandler } from "@ricsam/isolate-playwright";
192
+ export type { PlaywrightHandle, PlaywrightSetupOptions, PlaywrightCallback, PlaywrightEvent, NetworkRequestInfo, NetworkResponseInfo, BrowserConsoleLogEntry, } from "@ricsam/isolate-playwright";
193
+ export * from './internal.ts';
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Internal APIs for @ricsam/isolate-runtime
3
+ *
4
+ * These are not part of the public API and may change without notice.
5
+ * Only used by @ricsam/isolate-daemon internally.
6
+ */
7
+ import ivm from "isolated-vm";
8
+ import type { ConsoleOptions, ConsoleHandle } from "@ricsam/isolate-console";
9
+ import type { FetchOptions, FetchHandle } from "@ricsam/isolate-fetch";
10
+ import type { FsOptions } from "@ricsam/isolate-fs";
11
+ import type { TimersHandle } from "@ricsam/isolate-timers";
12
+ /**
13
+ * @internal Options for creating a legacy runtime.
14
+ */
15
+ export interface InternalRuntimeOptions {
16
+ memoryLimitMB?: number;
17
+ console?: ConsoleOptions;
18
+ fetch?: FetchOptions;
19
+ fs?: FsOptions;
20
+ /** Current working directory for path.resolve(). Defaults to "/" */
21
+ cwd?: string;
22
+ }
23
+ /**
24
+ * @internal Runtime handle with direct isolate/context access.
25
+ * Used by isolate-daemon for low-level operations.
26
+ */
27
+ export interface InternalRuntimeHandle {
28
+ readonly isolate: ivm.Isolate;
29
+ readonly context: ivm.Context;
30
+ readonly fetch: FetchHandle;
31
+ readonly timers: TimersHandle;
32
+ readonly console: ConsoleHandle;
33
+ dispose(): void;
34
+ }
35
+ /**
36
+ * @internal Create a runtime with direct access to isolate and context.
37
+ * This is for internal use by @ricsam/isolate-daemon only.
38
+ */
39
+ export declare function createInternalRuntime(options?: InternalRuntimeOptions): Promise<InternalRuntimeHandle>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ricsam/isolate-runtime",
3
- "version": "0.1.4",
3
+ "version": "0.1.7",
4
4
  "main": "./dist/cjs/index.cjs",
5
5
  "types": "./dist/types/index.d.ts",
6
6
  "exports": {
@@ -22,11 +22,21 @@
22
22
  "@ricsam/isolate-encoding": "*",
23
23
  "@ricsam/isolate-fetch": "*",
24
24
  "@ricsam/isolate-fs": "*",
25
+ "@ricsam/isolate-path": "*",
26
+ "@ricsam/isolate-playwright": "*",
27
+ "@ricsam/isolate-protocol": "*",
28
+ "@ricsam/isolate-test-environment": "*",
25
29
  "@ricsam/isolate-timers": "*",
26
30
  "isolated-vm": "^6"
27
31
  },
28
32
  "peerDependencies": {
29
- "isolated-vm": "^6"
33
+ "isolated-vm": "^6",
34
+ "playwright": ">=1.40.0"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "playwright": {
38
+ "optional": true
39
+ }
30
40
  },
31
41
  "author": "Richard Samuelsson",
32
42
  "license": "MIT",