function createRuntimeServices
Since 0.2.0
createRuntimeServices(options?: CreateRuntimeServicesOptions): IRuntimeServices

Creates runtime services for the current platform.

Use this when runtime services are needed before an application is constructed — reading environment variables to build configuration, for example. Inside an application, resolve CAPABILITIES.RUNTIME from the service registry instead: RuntimePlugin registers the instance it builds through this same function.

The returned services are a stateless facade over platform globals, so building a second instance alongside the application's own is safe: they hold no connection, no cache, and no handle registry, and nothing compares them by identity.

One caveat worth knowing rather than discovering: env is a snapshot taken at construction, not a live view. Two instances built at different moments observe the environment as it was at each of those moments, so a variable set in between is visible only to the later one.

On Cloudflare Workers there is no ambient environment to snapshot, so env stays empty unless CreateRuntimeServicesOptions.env is supplied.

Examples

Resolving configuration before choosing plugins

import { createRuntimeServices } from '@setu-ts/runtime';
import { loadConfig } from '@setu-ts/config-plugin';

const config = await loadConfig(createRuntimeServices());
const url = config.getOrThrow<string>('DATABASE_URL');

The same, on Cloudflare Workers

import { env } from 'cloudflare:workers';
import { createRuntimeServices } from '@setu-ts/runtime';

const config = await loadConfig(createRuntimeServices({ env }));

Parameters

Platform override and adapter injection

Return Type

IRuntimeServices

Runtime services for the resolved platform

Throws

Error

If no factory is registered for the resolved platform

Usage

import { createRuntimeServices } from "runtime/src/index.ts";