Reference

API reference

Signatures and contracts for every public oruntime API.

API reference

Core types

Runtime

type Runtime = 'node' | 'bun' | 'deno' | 'workerd' | 'browser' | 'unknown'

RuntimeCapability

type RuntimeCapability =
  | 'environmentVariables'
  | 'commandLineArguments'
  | 'currentWorkingDirectory'
  | 'fileSystem'
  | 'subprocess'
  | 'signals'
  | 'timers'
  | 'url'
  | 'fetch'
  | 'webSocket'
  | 'webStreams'

RuntimeCapabilities

type RuntimeCapabilities = Readonly<Record<RuntimeCapability, boolean>>

Detection

detectRuntime()

function detectRuntime(options?: {
  throwOnUnknown?: boolean
  globals?: RuntimeGlobals
}): Runtime
  • throwOnUnknown defaults to false.
  • globals defaults to globalThis and is mainly intended for tests.
  • Throws UnsupportedRuntimeError only when strict mode is enabled and no runtime is recognized.

createRuntimeAdapter()

function createRuntimeAdapter(options?: {
  throwOnUnknown?: boolean
  globals?: RuntimeGlobals
}): RuntimeAdapter

interface RuntimeAdapter {
  readonly name: Runtime
  readonly version: string | undefined
  readonly capabilities: RuntimeCapabilities
  getEnv(): Record<string, string>
  getEnv(key: string): string | undefined
  cwd(): string | undefined
  argv(): string[]
}

Capabilities

runtimeCapabilities

A constant array containing every known capability name.

const runtimeCapabilities: readonly RuntimeCapability[]

detectRuntimeCapabilities()

function detectRuntimeCapabilities(options?: {
  runtime?: Runtime
  globals?: RuntimeGlobals
}): RuntimeCapabilities

Returns a frozen capability map.

hasRuntimeCapability()

function hasRuntimeCapability(
  capability: RuntimeCapability,
  capabilities?: RuntimeCapabilities
): boolean

Without a second argument, the function performs a new detection.

Fetch

runtimeFetch()

interface RuntimeFetchOptions extends RequestInit {
  timeout?: number
}

function runtimeFetch(
  input: RequestInfo | URL,
  options?: RuntimeFetchOptions
): Promise<Response>

Possible failures include UnsupportedCapabilityError, RangeError, TimeoutError, OperationAbortedError, and RuntimeOperationError.

fetchResponse()

function fetchResponse(
  input: RequestInfo | URL,
  options?: RuntimeFetchOptions
): Promise<Response>

Throws HTTPResponseError when response.ok is false.

fetchJSON()

function fetchJSON<T = unknown>(
  input: RequestInfo | URL,
  options?: RuntimeFetchOptions
): Promise<T>

Calls fetchResponse() and then response.json(). The generic type does not validate data at runtime.

fetchText()

function fetchText(
  input: RequestInfo | URL,
  options?: RuntimeFetchOptions
): Promise<string>

Timers

sleep()

function sleep(
  milliseconds: number,
  options?: { signal?: AbortSignal }
): Promise<void>

Throws RangeError synchronously for an invalid duration and rejects with OperationAbortedError when cancelled.

withTimeout()

function withTimeout<T>(
  promise: PromiseLike<T>,
  milliseconds: number,
  options?: { message?: string }
): Promise<T>

Throws RangeError for an invalid duration and rejects with TimeoutError when the deadline expires. It does not cancel the original promise.

URL

URLInput

type URLInput = string | URL

toURL()

function toURL(input: URLInput, base?: URLInput): URL

Always returns a new instance.

resolveURL()

function resolveURL(base: URLInput, ...segments: string[]): URL

URLQueryValue

type URLQueryPrimitive = string | number | boolean
type URLQueryValue =
  | URLQueryPrimitive
  | readonly URLQueryPrimitive[]
  | null
  | undefined

withURLQuery()

function withURLQuery(
  input: URLInput,
  query: Record<string, URLQueryValue>,
  base?: URLInput
): URL

Nullish values remove a parameter; arrays create repeated values.

Errors

RuntimeError

class RuntimeError extends Error {
  readonly code: RuntimeErrorCode
  readonly runtime?: Runtime
  override readonly cause?: unknown
}

Error classes

ClassCodeExtra property
UnsupportedRuntimeErrorERR_UNKNOWN_RUNTIME—
UnsupportedCapabilityErrorERR_UNSUPPORTED_CAPABILITYcapability
PermissionDeniedErrorERR_PERMISSION_DENIEDcapability, operation
RuntimeOperationErrorERR_RUNTIME_OPERATIONcapability, operation
TimeoutErrorERR_TIMEOUTtimeout
OperationAbortedErrorERR_OPERATION_ABORTED—
HTTPResponseErrorERR_HTTP_RESPONSEresponse, status, statusText

normalizeRuntimeError()

function normalizeRuntimeError(
  error: unknown,
  context: {
    runtime: Runtime
    capability?: RuntimeCapability
    operation?: string
  }
): RuntimeError

Preserves existing RuntimeError instances, converts recognized permission failures, and otherwise returns RuntimeOperationError.