@wasm-idle/node
v1.4.0
Published
Node.js host helpers for wasm-idle core
Readme
@wasm-idle/node
Node.js host helpers for wasm-idle sandboxes. They load a sandbox through a @wasm-idle/core
playground binding, run one program with optional stdin, stream its output, and clean the sandbox
up afterwards.
This package does not include or select runtimes. Pass a loadSandbox function that returns
Node-capable Sandbox implementations, or an existing playground binding.
pnpm add @wasm-idle/node @wasm-idle/coreUsage
import { executeWasmIdleInNode, fileAssetUrl, runWasmIdleInNode } from '@wasm-idle/node';
import { loadSandbox } from './sandboxes'; // your Node-capable SandboxLoader
const runtimeAssets = fileAssetUrl('/opt/wasm-idle/runtimes');
// Legacy load/run path: output is streamed, failures are returned rather than thrown.
const run = await runWasmIdleInNode({
language: 'PYTHON3',
code: 'print(input().upper())',
stdin: 'hello\n',
runtimeAssets,
loadSandbox
});
if (!run.ok) process.exitCode = 1;
// Structured path: requires a sandbox that implements execute().
const result = await executeWasmIdleInNode({
language: 'PYTHON3',
request: { code: 'print(input().upper())', stdin: 'hello\n' },
runtimeAssets,
loadSandbox
});
console.log(result.exitCode, result.stdout);API
runWasmIdleInNode(options)loads the sandbox, writesstdinfollowed by EOF when provided, and runscode. It resolves to{ ok, result, elapsedMs, error?, cleanupErrors? }.okis false whenrun()returnsfalseor whenload()orrun()throws. A thrown failure is written tostderr; its message is returned inresultand the original error inerror. Errors from the loader itself still reject.executeWasmIdleInNode(options)calls the sandbox's structuredexecute(request)and resolves to itsExecutionResult. It rejects with the original error on failure, or with aNodeExecutionCleanupError(anAggregateErrorwhosecauseis the original error) when cleanup also failed. A sandbox withoutexecute()rejects withRuntimeConfigurationError.fileAssetUrl(path)converts a local path into afile:URL string forruntimeAssets.
Options worth knowing
- Provide either
playground(an existingPlaygroundBinding, whose lifetime stays with you) orloadSandboxwith optionalruntimeAssets. - Sandbox output goes to the
stdoutcallback (defaultprocess.stdout); failure messages go to thestderrcallback (defaultprocess.stderr). - For
runWasmIdleInNode,args,executionOptions,log, andprogressare forwarded to the sandbox'sload()andrun()calls. - The sandbox is disposed after each call unless
disposeAfterRun: false.runWasmIdleInNodecan also clear it first withclearAfterRun: true. Cleanup failures are reported incleanupErrorsinstead of replacing a returned result.
See the wasm-idle repository for the runtime contracts and the runtime execution and cleanup contract.
