Core concepts

Capabilities

Detect available APIs without assuming that a runtime implements them.

Capabilities

Why capabilities matter

Different versions of the same runtime may expose different APIs. Edge platforms and build tools can also add or remove globals. Asking “is Fetch available?” is therefore safer than asking only “am I running on Node.js?”

From the adapter

const runtime = createRuntimeAdapter()

if (runtime.capabilities.webSocket) {
  // WebSocket is available, or workerd provides its WebSocket model.
}

The map is frozen and cannot be modified.

Independent detection

import { detectRuntimeCapabilities, hasRuntimeCapability } from 'oruntime'

const capabilities = detectRuntimeCapabilities()
const canFetch = hasRuntimeCapability('fetch', capabilities)

Without a second argument, hasRuntimeCapability() performs a new detection.

Known capabilities

CapabilityDetection rule
environmentVariablesRuntime provides an environment API
commandLineArgumentsRuntime provides user arguments
currentWorkingDirectoryRuntime provides a current-directory API
fileSystemRuntime family provides server-side file access
subprocessRuntime family supports subprocesses
signalsRuntime family supports process signals
timerssetTimeout and clearTimeout are functions
urlURL is a constructor
fetchfetch and AbortController are functions
webSocketWebSocket exists or the runtime is workerd
webStreamsReadableStream and WritableStream exist

Capabilities versus permissions

A capability says that an API exists in this runtime family or context. It does not guarantee that one specific call is authorized:

if (runtime.capabilities.environmentVariables) {
  // Deno may still deny access.
  const token = runtime.getEnv('TOKEN')
}

Test injection

const capabilities = detectRuntimeCapabilities({
  runtime: 'node',
  globals: {
    fetch: () => {},
    AbortController,
    setTimeout,
    clearTimeout,
    URL
  }
})

Injection tests behavior without changing globalThis.