import type { Request, Response } from "./http/types"; import type { Transport } from "@connectrpc/connect"; import type {TypedArray} from "@encore.dev/internal-runtime/compat/types"; /** * The runtime interface is used by this package to interact with the javascript runtime * we are running in. * * There are two implementations of this interface, one in `@encore.dev/bun-runtime` and one in * `@encore.dev/node-runtime`. */ export interface Runtime { /** * The name of the runtime */ name: string; /** * Starts an HTTP server on the given port * * @param host The host to start the server on * @param port The port to start the server on * @param handler The handler to use for the server * * @returns The URL of the server */ startHTTPServer( host: string, port: number, handler: (req: Request, res: Response) => void, ): URL; /** * Returns the value of the given environment variable * * @param {string} name * @returns {string | undefined} */ env(name: string): string | undefined; /** * Creates a ConnectRPC transport for use by the sidecar-api package * @returns {Transport} */ apiTransport(): Transport; /** * Writes the given object to stdout as a JSON line */ objToStderr(obj: unknown): void; /** * Fills the array with cryptographically secure random values */ getRandomValues(array: T): T; } /** * A singleton instance of the runtime object */ let runtimeObj: Runtime | null = null; /** * Injects the runtime object */ export function inject(r: Runtime) { if (runtimeObj !== null) { throw new Error("The javascript runtime object has already been injected"); } runtimeObj = r; } /** * Returns the runtime object for this javascript runtime */ export function runtime(): Runtime { if (runtimeObj === null) { throw new Error("The javascript runtime object has not been injected yet"); } return runtimeObj; }