API reference
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
throwOnUnknowndefaults tofalse.globalsdefaults toglobalThisand is mainly intended for tests.- Throws
UnsupportedRuntimeErroronly 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
| Class | Code | Extra property |
|---|---|---|
UnsupportedRuntimeError | ERR_UNKNOWN_RUNTIME | — |
UnsupportedCapabilityError | ERR_UNSUPPORTED_CAPABILITY | capability |
PermissionDeniedError | ERR_PERMISSION_DENIED | capability, operation |
RuntimeOperationError | ERR_RUNTIME_OPERATION | capability, operation |
TimeoutError | ERR_TIMEOUT | timeout |
OperationAbortedError | ERR_OPERATION_ABORTED | — |
HTTPResponseError | ERR_HTTP_RESPONSE | response, 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.