/** * @file Environment variable rewiring utilities for testing. Uses * AsyncLocalStorage for context-isolated overrides that work with concurrent * tests. Features: * * - Context-isolated overrides via withEnv() for advanced use cases * - Test-friendly setEnv/clearEnv/resetEnv that work in beforeEach/afterEach * - Compatible with vi.stubEnv() - reads from process.env as final fallback * - Thread-safe for concurrent test execution */ import type { AsyncLocalStorage } from 'node:async_hooks'; import { getNodeAsyncHooks } from '../node/async-hooks.js'; export type EnvOverrides = Map; /** * Clear a specific environment variable override. * * @example * ;```typescript * import { setEnv, clearEnv } from '@socketsecurity/lib/env/rewire' * * setEnv('CI', '1') * clearEnv('CI') * ``` * * @param key - The environment variable name to clear. */ export declare function clearEnv(key: string): void; /** * Lazily load the async_hooks module. Aliases the canonical `node/async-hooks` * accessor, the single owner of the bundler-safe require; kept as an export so * this module's surface is unchanged. * * @private */ export declare const getAsyncHooks: typeof getNodeAsyncHooks; /** * Get an environment variable value, checking overrides first. * * Resolution order: 1. Isolated overrides (temporary - set via * withEnv/withEnvSync) 2. Shared overrides (persistent - set via setEnv in * beforeEach) 3. process.env (including vi.stubEnv modifications) * * @example * ;```typescript * import { getEnvValue } from '@socketsecurity/lib/env/rewire' * * const value = getEnvValue('NODE_ENV') * // e.g. 'production' or undefined * ``` * * @internal Used by env getters to support test rewiring */ export declare function getEnvValue(key: string): string | undefined; /** * Get the current isolated-override map, or undefined when none is active. * Off Node, in browser bundles, there is no AsyncLocalStorage and no isolated * context — env getters fall straight through to the other tiers. * * @private */ export declare function getIsolatedOverrides(): EnvOverrides | undefined; /** * Get the process-scoped AsyncLocalStorage used for nested env overrides * (withEnv/withEnvSync). * * Constructed LAZILY (memoized) rather than at module-eval: an * AsyncLocalStorage holds a live native handle, and constructing it at import * time pins that handle into every module transitively importing this leaf — * aborting V8 --build-snapshot serialization. Deferring to first use keeps the * single-store semantics while leaving module import snapshot-safe. * * @private */ export declare function getIsolatedOverridesStorage(): AsyncLocalStorage; /** * Check if an environment variable has been overridden. * * @example * ;```typescript * import { setEnv, hasOverride } from '@socketsecurity/lib/env/rewire' * * hasOverride('CI') // false * setEnv('CI', '1') * hasOverride('CI') // true * ``` * * @param key - The environment variable name to check. * * @returns `true` if the variable has been overridden, `false` otherwise */ export declare function hasOverride(key: string): boolean; /** * Check if an environment variable key exists, checking overrides first. * * Resolution order: 1. Isolated overrides (temporary - set via * withEnv/withEnvSync) 2. Shared overrides (persistent - set via setEnv in * beforeEach) 3. process.env (including vi.stubEnv modifications) * * @example * ;```typescript * import { isInEnv } from '@socketsecurity/lib/env/rewire' * * isInEnv('PATH') // true (usually set) * isInEnv('MISSING') // false * ``` * * @internal Used by env getters to check for key presence rather than value * truthiness. */ export declare function isInEnv(key: string): boolean; /** * Clear all environment variable overrides. Useful in afterEach hooks to ensure * clean test state. * * @example * ;```typescript * import { resetEnv } from './rewire.mjs' * * afterEach(() => { * resetEnv() * }) * ``` */ export declare function resetEnv(): void; /** * Read `process.env` without assuming a real Node `process`. Probes the * GLOBAL `process` via `typeof` (no `node:process` import — webpack throws * UnhandledSchemeError on `node:` specifiers before the `browser`-field stubs * apply), so browser bundles load this leaf cleanly and env getters read as * unset instead of throwing. * * @private */ export declare function safeProcessEnv(): Record | undefined; /** * Set an environment variable override for testing. This does not modify * process.env, only affects env getters. * * Works in test hooks (beforeEach) without needing AsyncLocalStorage context. * Vitest's module isolation ensures each test file has independent overrides. * * @example * ;```typescript * import { setEnv, resetEnv } from './rewire.mjs' * import { isCI } from './ci.mjs' * * beforeEach(() => { * setEnv('CI', '1') * }) * * afterEach(() => { * resetEnv() * }) * * it('should detect CI environment', () => { * expect(isCI()).toBe(true) * }) * ``` */ export declare function setEnv(key: string, value: string | undefined): void; /** * Run code with environment overrides in an isolated AsyncLocalStorage context. * Creates true context isolation - overrides don't leak to concurrent code. * * Useful for tests that need temporary overrides without affecting other tests * or for nested override scenarios. * * @example * ;```typescript * import { withEnv } from './rewire.mjs' * import { isCI } from './ci.mjs' * * // Temporary override in isolated context * await withEnv({ CI: '1' }, async () => { * expect(isCI()).toBe(true) * }) * expect(isCI()).toBe(false) // Override is gone * ``` * * @example * ;```typescript * // Nested overrides work correctly * setEnv('CI', '1') // Shared override (persistent) * * await withEnv({ CI: '0' }, async () => { * expect(isCI()).toBe(false) // Isolated override takes precedence * }) * * expect(isCI()).toBe(true) // Back to shared override * ``` */ export declare function withEnv(overrides: Record, fn: () => T | Promise): Promise; /** * Synchronous version of withEnv for non-async code. * * @example * ;```typescript * import { withEnvSync } from './rewire.mjs' * import { isCI } from './ci.mjs' * * const result = withEnvSync({ CI: '1' }, () => { * return isCI() * }) * expect(result).toBe(true) * ``` */ export declare function withEnvSync(overrides: Record, fn: () => T): T;