# TypeScript reference

> API reference for every class, method, and type exported by the dvm-run npm package.

This is the Markdown of https://docs.dvm.run/reference/typescript/. Every page of the dvm.run docs is served as Markdown at its address plus `.md`; the list of pages is https://docs.dvm.run/llms.txt.

Reference for every export of the package (version 0.3.0), written by hand from `js/src` because this site’s TypeDoc plugin does not support the TypeScript 7 version the package builds with. Every async call accepts a trailing options object with an `AbortSignal` under `signal`.

## `Sandbox`

| Call                                   | Resolves to        |                                                                                                                                                                                                                                    |
| -------------------------------------- | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Sandbox.create(image, options?)`      | `Sandbox`          | Makes a sandbox; resolves once it runs, or has exited. Options: `command`, `entrypoint`, `cpus`, `memoryMib`, `seed`, `env`, `name`, `network`, `allow`, `apiKey`, `apiUrl`, `signal`. Aborting `signal` while it starts stops it. |
| `Sandbox.connect(sandboxId, options?)` | `Sandbox`          | A handle to a sandbox made elsewhere. Options: `apiKey`, `apiUrl`, `signal`.                                                                                                                                                       |
| `.id`, `.name`                         | `number`, `string` |                                                                                                                                                                                                                                    |
| `.spec`                                | `SandboxSpec`      | A copy of the sandbox’s spec.                                                                                                                                                                                                      |
| `.state()`                             | `RunState`         |                                                                                                                                                                                                                                    |
| `.pause()`, `.resume()`                | `void`             |                                                                                                                                                                                                                                    |
| `.setNetwork(network, allow?)`         | `void`             | `"public"`, `"none"` or `"allowlist"`. From now on; open connections stay.                                                                                                                                                         |
| `.stop()`                              | `void`             | One that already ended stays as it is.                                                                                                                                                                                             |
| `.wait()`                              | `number`           | The exit code; a stopped or failed run throws its error.                                                                                                                                                                           |
| `.run(command, options?)`              | `CommandResult`    | A session’s command, to its end. `command` is a `string[]` (argv) or a `string` (a script). Options: `env`, `cwd`, `stdin`, `user`.                                                                                                |
| `.start(command, options?)`            | `Command`          | Resolves once started. Without `stdin`, stdin stays open.                                                                                                                                                                          |
| `.writeFile(path, data, options?)`     | `void`             | `data` is a `Uint8Array`. Options: `mode`.                                                                                                                                                                                         |
| `.readFile(path)`                      | `Uint8Array`       |                                                                                                                                                                                                                                    |
| `.end()`                               | `number`           | Ends a session’s input; waits for the exit code.                                                                                                                                                                                   |
| `.output()`                            | `Output`           | What the first process wrote so far.                                                                                                                                                                                               |
| `.seek(options?)`                      | `SandboxPoint`     | Options: `atNs`, or `thread` and `event` together, and `watch`. No target: the end.                                                                                                                                                |
| `await using sandbox = …`              |                    | `[Symbol.asyncDispose]()`: ends a session’s input and waits, or stops a paused sandbox or a program.                                                                                                                               |

## `SandboxPoint`

| Member                                        |                                    |                                                              |
| --------------------------------------------- | ---------------------------------- | ------------------------------------------------------------ |
| `.sandboxId`, `.thread`, `.process`, `.event` | `number`                           | Where it stopped.                                            |
| `.ns`                                         | `number \| null`                   | About when, in the recording’s time.                         |
| `.at`                                         | `StopEvent`                        |                                                              |
| `.operations`, `.stats`                       | `number`, `Record<string, number>` |                                                              |
| `.step(options?)`                             | `SandboxPoint \| null`             | The next watched event; `null` at the end. Options: `watch`. |
| `.readFile(path)`                             | `Uint8Array`                       |                                                              |
| `.listDir(path)`                              | `DirEntry[]`                       |                                                              |
| `.stat(path, options?)`                       | `FileStat`                         | Options: `followSymlinks`, true unless given.                |

`watch` accepts a list of watch points, `"syscall"`, `"syscall:<name>"`, `"file-syscalls"`, `"socket-syscalls"`, `"processes"`, or `"page-fault"` (hyphens `-` and underscores `_` are interchangeable), or raw API objects.

## `Command`

| Call                | Resolves to     |                                         |
| ------------------- | --------------- | --------------------------------------- |
| `.wait()`           | `CommandResult` | Aborting it leaves the command running. |
| `.signal(number)`   | `void`          | 1 to 64, to its process group.          |
| `.writeStdin(data)` | `void`          | Queues a `Uint8Array`.                  |
| `.closeStdin()`     | `void`          | After the queued bytes.                 |

## Types

| Type              | Fields                                                                                                                                                                      |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `CommandResult`   | `exitCode`, `stdout: Uint8Array`, `stderr: Uint8Array`                                                                                                                      |
| `Output`          | `stdout: Uint8Array`, `stderr: Uint8Array`                                                                                                                                  |
| `RunState`        | `kind: RunKind`, `exitCode: number \| null`, `reason: string \| null`, `isActive`                                                                                           |
| `StopEvent`       | `kind` (`"syscall"`, `"page_fault"`, `"arrival"`); for a syscall `syscall`, `name`, `args`, `decoded`; for a page fault `address`, `write`, `present`; for an arrival `rip` |
| `DecodedArgument` | `arg`, `kind` (`"path"`, `"strings"`, `"address"`, `"bytes"`), `value`, `truncated`                                                                                         |
| `DirEntry`        | `name`, `rawName: Uint8Array`, `type`                                                                                                                                       |
| `FileStat`        | `type`, `mode`, `uid`, `gid`, `size`, `links`, `mtime` and `ctime` as `[seconds, nanoseconds]`, `target: Uint8Array \| null`, `mount: string \| null`                       |

Also exported: `SandboxOptions`, `ClientOptions`, `CallOptions`, `CommandOptions`, `WriteFileOptions`, `SeekOptions`, `StepOptions`, `StatOptions`, `WatchInput`, `NetworkMode`, and `RunKind`.

## Errors

All errors inherit from `DvmError`:

* `NetworkError`
* `ApiError` (`status`, `reason`), with subclasses `InvalidRequestError`, `AuthenticationError`, `NotFoundError`, and `ConflictError`
* `SandboxEndedError` (`sandboxId`, `state`), with subclass `SandboxFailedError` (`reason`)

See [Errors](/reference/errors/) for details.

## Command line

Run `npx dvm-run login [--api-url URL] [--no-browser]` ([Getting in](/start/access/)).
