runtime:process
Host process information: environment, arguments, working directory, platform, signals, and exit. Aligned in spirit with the WinterTC CLI-API proposal.
Environment access is gated on Env; the signal exports on Signals. Exposed as an ES module under the runtime: scheme. Status: Available.
Import
import { env, args, platform, arch, cwd, exit, unmask } from "runtime:process"; // Or the default aggregate: import process from "runtime:process";
Exports
| Export | Type | Description |
|---|---|---|
env | Record<string, string | Secret> | Environment variables as a mutable in-process object, seeded from a host snapshot taken when the module is evaluated (plus any --env-file values). Reads, writes, and deletes work in-process; they do not propagate to the host or to child processes. Assigned values are coerced to strings (env.PORT = 8080 stores "8080"; a symbol throws). Secret-keyed values are Secret wrappers (see below). |
args | readonly string[] | Program arguments after the runtime binary and the script (or -e snippet). Frozen. Excludes the executable and script path. |
platform | string | Host operating system — the OS-native std value ("linux" | "macos" | "windows"). |
arch | string | Host CPU architecture — the OS-native std value ("x86_64" | "aarch64" | "arm"). |
cwd() | () => string | Current working directory. A function (not a value) because it can change during a run. |
exit(code = 0) | (code?: number) => never | Records the exit code and halts execution immediately — code after the call does not run. The embedder treats it as a clean exit, not an error. |
unmask(value) | (value: string | Secret) => string | Reveal the real value of a masked Secret. A plain string is returned unchanged, so unmask(env.ANY) is always safe. |
Secret | class | Opaque holder for a masked env value. Renders as "[redacted]" in console, string coercion, and JSON. Call unmask() to read it. |
signals() | () => SignalName[] | Signal names this platform can deliver. Signals. |
onSignal(sig, fn) | (SignalName, (SignalName) => void) => void | Run fn when sig arrives, suppressing its default action. Signals. |
offSignal(sig, fn) | (SignalName, (SignalName) => void) => void | Remove a handler; removing the last one restores the default action. Signals. |
stdout | StdStream | The process's own standard output — write(chunk), isTTY, columns, rows. No capability, like console.log. |
stderr | StdStream | The same, for standard error. |
permissions | object | What this run may reach (see below). No capability. |
memoryUsage() | () => object | { heapUsed, heapLimit, external } in bytes, for this agent's isolate. No capability. |
cpuTime() | () => number | CPU milliseconds this agent's thread has used. No capability. |
uptime() | () => number | Milliseconds since this agent started. No capability. |
env — reading and writing
import { env } from "runtime:process"; console.log(env.HOME); // read env.FEATURE_FLAG = "on"; // write (in-process only) delete env.SECRET; // delete (in-process only)
args — program arguments
// esrun app.js build --watch import { args } from "runtime:process"; console.log(args); // ["build", "--watch"]
signals — graceful shutdown
Watching a signal suppresses its default action, which is what lets a SIGTERM drain instead of kill.
import { onSignal, offSignal } from "runtime:process"; const shutdown = async (signal) => { offSignal(signal, shutdown); // a second ^C should kill, not queue await server.stop(); }; onSignal("SIGINT", shutdown); onSignal("SIGTERM", shutdown);
| Platform | Deliverable |
|---|---|
| Unix | SIGINT, SIGTERM, SIGHUP, SIGUSR1, SIGUSR2 |
| Windows | SIGINT, SIGBREAK |
While anything is watched the program stays running to receive it, as in Node and Deno; removing the last handler releases it. Repeated deliveries coalesce.
exit — stopping the run
import { exit } from "runtime:process"; if (failed) exit(1); // records the code and halts immediately exit(); // defaults to 0
stdout — drawing on a terminal
console.log formats a value, appends a newline, and goes wherever the host pointed it. That is right for a log line and wrong for a display: a spinner is a carriage return and no newline, and a progress bar rewrites the line it is already on.
import { stdout } from "runtime:process"; if (stdout.isTTY) { stdout.write(`\r${bar(done / total, stdout.columns ?? 60)}`); } else { console.log(`${done}/${total}`); }
| Member | |
|---|---|
write(chunk) | Exactly these bytes, flushed. No newline is added. A string is UTF-8; an ArrayBuffer or view is written as it is. |
isTTY | Whether this stream is attached to a terminal. |
columns / rows | The terminal's size, or undefined when there is no terminal or the host cannot say. |
Ask isTTY before you draw. A spinner redrawn with \r into a log file is a file of spinner frames, and colour escapes in a pipe are noise in somebody's grep.
The size comes from the terminal, not from $COLUMNS — a shell exports that to itself rather than to a child, and it is stale the moment the window is dragged. Where the host cannot answer it says so rather than reporting a plausible 80.
Neither needs a capability, for the reason console.log does not: writing to the stream this program was started with reaches nothing it was not already handed, so --deny-all still leaves a program able to say what it is doing.
permissions — what this run may reach
Fixed at launch by esrun's permission flags, so has() is a synchronous boolean: there is nothing to request and no prompt to await.
import { permissions } from "runtime:process"; permissions.denied; // ["read", "write"] — [] when nothing is denied permissions.has("net"); // true if (permissions.has("write")) await fs.write("cache.json", data);
| Export | Type | Description |
|---|---|---|
permissions.denied | readonly PermissionName[] | Names this run may not use, in capability order. |
permissions.has(name) | (PermissionName) => boolean | Whether name is available. Throws TypeError for any other name, and for a second argument. |
PermissionName is "read", "write", "imports", "net", "listen", "env", "run", "signals" — the same words the --deny-<name> flags use. Needs no capability, so it answers with everything denied too.
A scoped grant reports true: --allow-env=PORT grants the env capability and narrows what it yields, so has("env") means "you may read an environment variable", not "you may read this one". The same holds for --allow-net=<hosts> and the rest.
There is no per-value form — has("read", "/etc/passwd") throws. Which paths and hosts are allowed is set by the deployment, not asked about by the application, and the exact answer for one value is to make the call and catch ERR_PERMISSION_DENIED. A path is judged after the runtime resolves it, so an answer given in advance could be stale by the time the call happens.
Secrets — masked by default
Secret-bearing keys are exposed as a Secret that renders as [redacted] in console, strings, and JSON. A key qualifies if it ends in _KEY, _TOKEN, _SECRET, _PASS(WORD), or contains CREDENTIAL/AUTH.
import { env, unmask } from "runtime:process"; // Keys ending in *_SECRET(S) / *_PASSWORD(S) are masked by default. console.log(env.DB_PASSWORD); // [redacted] console.log(`${env.DB_PASSWORD}`); // [redacted] JSON.stringify(env); // ..."DB_PASSWORD":"[redacted]"... const pw = unmask(env.DB_PASSWORD); // real value, explicit unmask(env.DB_HOST); // plain strings pass through
Call unmask(value) for the real string; plain values pass through unchanged.
Masking stops accidental log leaks — code you run can still call unmask itself.
Load values from a file with esrun --env-file=.env (no auto-discovery; the OS environment wins unless --env-override is passed).
Reporting on yourself
import { cpuTime, memoryUsage, uptime } from "runtime:process"; const { heapUsed, heapLimit } = memoryUsage(); if (heapLimit - heapUsed < 32 * 1024 * 1024) shedLoad();
| Field | |
|---|---|
heapUsed | what V8 has allocated and not collected, for this isolate |
heapLimit | this isolate's ceiling — --max-heap, or a worker's memory option |
external | ArrayBuffers and other memory V8 holds outside its heap |
heapLimit - heapUsed is real headroom: exceed it and the heap guard ends this agent. Per isolate, so a worker answers for itself:
// in a worker started with { memory: 64 } memoryUsage().heapLimit; // 67108864, not the parent's cpuTime(); // CPU burned by *this worker's* thread uptime(); // ms since *this worker* started
uptime() is not performance.now(). A worker is handed its parent's clock, so performance.now() counts from when the process's runtime was built and reads the same in every agent.
Utilisation is two reads and a division:
const [cpu0, t0] = [cpuTime(), uptime()]; await work(); const busy = (cpuTime() - cpu0) / (uptime() - t0);
cpuTime() is total CPU, not a user/system split. The split needs Mach on macOS, where a wrong struct layout is a memory-safety bug rather than a wrong number, so it is reported nowhere rather than on two platforms out of three.
All three report only what the caller could discover about itself anyway — its ceiling by allocating until it is stopped, its CPU and age by counting — which is the same rule platform and args follow.
The process's resident set, total CPU and uptime are about the other agents in the process, so they are not here. They are metrics().process in runtime:diagnostics, behind diagnostics.
Errors
| Error | When |
|---|---|
TypeError | unmask() gets something other than a string or a Secret from env. |
DOMException | name "NotAllowedError" — the Env capability is not granted (env access), or Signals (signal access). |
TypeError | onSignal() gets a name this runtime does not know. |
Error | onSignal() gets a signal this platform cannot deliver — better than a handler that never fires. |
The default export is an object bundling all named exports — useful for a single import binding — but named imports are preferred for clarity and tree-shaking.