Container

The Container class is the main interface for a jiki instance. It has methods for file operations, code execution, package installation, and snapshots.

Methods

File operations

MethodDescription
writeFile(path, content)Write content to a file
readFile(path, encoding?)Read a file’s contents
mkdir(path, options?)Create a directory
readdir(path)List directory contents
exists(path)Check if a path exists
rm(path, options?)Remove a file or directory

Execution

MethodDescription
execute(code)Execute a string of JavaScript
runFile(path)Run a JavaScript/TypeScript file
run(command)Run a shell command

Package management

MethodDescription
install()Install dependencies from package.json

Snapshots

MethodDescription
toSnapshot()Serialize the container state
fromSnapshot(data)Restore from a serialized snapshot

Plugins

Property / MethodDescription
pluginsThe PluginRegistry for this container
registerPolyfill(name, factory)Register a custom polyfill
registerPolyfills(map)Register multiple polyfills at once

Sandboxing

Property / MethodDescription
sandboxThe SandboxGuard enforcing resource limits

Metrics

Property / MethodDescription
getMetrics()Returns a MetricsSnapshot
metricsThe underlying Metrics instance

Network mocking

Property / MethodDescription
mockFetch(pattern, response)Mock a fetch URL
onFetch(handler)Register a dynamic fetch interceptor
networkThe NetworkInterceptor instance

Example

const container = await boot();

await container.writeFile("/index.js", 'console.log("hello")');
const result = await container.runFile("/index.js");

const snapshot = await container.toSnapshot();