/** * Runtime-agnostic primitives that all portable code uses instead of calling * `Bun.*` directly. When running under Bun the fast native paths are used; * otherwise standard Web APIs are preferred, with Node built-ins as a last * resort (lazy-imported so browser bundles never pull in `node:*`). * * @module runtime/portable */ /** Identifies the JavaScript runtime hosting this process. */ export type RuntimeKind = 'bun' | 'node' | 'browser' | 'edge'; type PortableRuntimeTestOverrides = { bun?: typeof globalThis.Bun | undefined; process?: typeof globalThis.process | undefined; window?: typeof globalThis.window | undefined; document?: typeof globalThis.document | undefined; }; export declare function setPortableRuntimeTestOverridesForTesting(overrides?: PortableRuntimeTestOverrides): void; /** * Whether the current runtime is Bun. Honors * {@link setPortableRuntimeTestOverridesForTesting}, unlike a bare * `typeof Bun !== 'undefined'` check, so callers that need to simulate a * non-Bun runtime in tests (e.g. to exercise a Node or browser fallback * branch) can do so without a real environment change. * @internal */ export declare function isBunRuntime(): boolean; /** * Detect the current JavaScript runtime. * Detection precedence is bun → node → browser → edge: a Bun process running * through Node compatibility still reports 'bun'; the function never falls through if * `globalThis.Bun` is defined. * * @example * ```ts * import { detectRuntime } from '@lostgradient/weft'; * * const runtime = detectRuntime(); * // Returns 'bun' | 'node' | 'browser' | 'edge' * console.log(runtime); // e.g. 'bun' when running under Bun * ``` */ export declare function detectRuntime(): RuntimeKind; /** * Detect the current runtime's version string, matching {@link detectRuntime}'s * detection precedence. A browser or edge runtime exposes no version, so an * empty string is a truthful answer rather than a missing field. * * @example * ```ts * import { detectRuntimeVersion } from '@lostgradient/weft'; * * const version = detectRuntimeVersion(); * console.log(typeof version); // 'string' * ``` */ export declare function detectRuntimeVersion(): string; /** * Read an environment variable without assuming a runtime. * * Prefers `Bun.env` under Bun, falls back to `process.env` under Node, and * returns `undefined` anywhere else (browsers, edge runtimes) — where * neither global exists and a bare `Bun.env[...]` or `process.env[...]` * reference would throw a `ReferenceError`. * @internal */ export declare function readEnvironmentVariable(name: string): string | undefined; /** * Pause execution for the given number of milliseconds. * * Uses `Bun.sleep` when available (microtask-friendly), otherwise wraps * `setTimeout` in a `Promise`. * * @example * ```ts * import { sleep } from '@lostgradient/weft'; * * async function poll() { * for (let i = 0; i < 3; i++) { * await sleep(100); * console.log('tick', i); * } * } * await poll(); * ``` */ export declare function sleep(ms: number): Promise; /** * Hash a byte buffer to a 16-character hex string. * * Uses FNV-1a unconditionally across all runtimes for stable output. * Hashes may be persisted to durable storage (event-log chains, tool-effect * dedup), so runtime-specific algorithms would break cross-runtime reads. * * @example * ```ts * import { hashBytes } from '@lostgradient/weft'; * * const data = new TextEncoder().encode('hello'); * const hash = hashBytes(data); * console.log(hash.length); // 16 * console.log(hashBytes(data) === hash); // true (deterministic) * ``` */ export declare function hashBytes(data: Uint8Array): string; /** * Hash a string to a 16-character hex string. * * Uses FNV-1a unconditionally across all runtimes for stable output. * * @example * ```ts * import { hashString } from '@lostgradient/weft'; * * const h1 = hashString('workflow-key'); * const h2 = hashString('workflow-key'); * console.log(h1 === h2); // true (stable across calls) * console.log(h1.length); // 16 * ``` */ export declare function hashString(data: string): string; /** * Load a Node.js built-in module without a static `node:*` import. * * This package is ESM (`"type": "module"` in package.json), so `require` * is not defined in Node runtime. `process.getBuiltinModule` (Node 22.5+, * also implemented by Bun) is the correct way to load Node built-ins from * ESM code without needing `createRequire` — and, crucially, without a * static `import ... from 'node:*'` specifier that a browser bundler would * try to resolve or stub. Returns `undefined` in the browser or any runtime * lacking `process.getBuiltinModule`, so callers on a browser-reachable path * must treat the result as optional rather than throwing at import time. * * Overloaded per known specifier so callers get the correct module type from * the literal `id` argument alone, without an explicit type argument. Add a * new literal overload here when a new built-in specifier is loaded through * this helper. * @internal */ export declare function tryLoadNodeBuiltin(id: 'node:fs'): typeof import('node:fs') | undefined; export declare function tryLoadNodeBuiltin(id: 'node:fs/promises'): typeof import('node:fs/promises') | undefined; export declare function tryLoadNodeBuiltin(id: 'node:zlib'): typeof import('node:zlib') | undefined; export declare function tryLoadNodeBuiltin(id: 'node:module'): typeof import('node:module') | undefined; /** * Return the byte size of a file at the given path. * * - Bun: `Bun.file(path).size` * - Node 22.5+: `node:fs` `statSync` loaded via `process.getBuiltinModule` * - Missing files return `0` to match Bun's behavior (important for WAL * probing in the diagnostics module). * * Not available in browser/edge runtimes — throws if called there. */ export declare function fileSize(path: string): number; /** * Gzip-compress a byte buffer synchronously. * * - Bun: `Bun.gzipSync` * - Node 22.5+: `node:zlib` via `process.getBuiltinModule` */ export declare function gzipSync(data: Uint8Array): Uint8Array; /** * Gunzip-decompress a byte buffer synchronously. * * - Bun: `Bun.gunzipSync` * - Node 22.5+: `node:zlib` via `process.getBuiltinModule` */ export declare function gunzipSync(data: Uint8Array): Uint8Array; /** * Load `node:zlib` if available for Node-side callers (used by compression.ts * for brotli). Returns `undefined` in browsers so they can degrade cleanly. * @internal */ export declare function tryLoadNodeZlib(): typeof import('node:zlib') | undefined; export {};