/**
 * Router Context using AsyncLocalStorage
 *
 * Provides clean dependency injection for router middleware without parameter drilling.
 * All closure functions from createRouter() are made available via getRouterContext().
 */
import { AsyncLocalStorage } from "async_hooks";
// AsyncLocalStorage instance for router context
const routerContext = new AsyncLocalStorage();
/**
 * Get router dependencies from AsyncLocalStorage context
 *
 * @throws Error if called outside of router context (runWithRouterContext)
 */
export function getRouterContext() {
    const deps = routerContext.getStore();
    if (!deps) {
        throw new Error("getRouterContext() called outside of router context. " +
            "Ensure code is running inside runWithRouterContext().");
    }
    return deps;
}
/**
 * Run a function with router dependencies available via getRouterContext()
 *
 * All async code within fn() can call getRouterContext() to access router closures.
 * This works across async boundaries thanks to AsyncLocalStorage.
 *
 * @param deps Router dependencies to make available
 * @param fn Function to run with dependencies available
 * @returns Result of fn()
 */
export function runWithRouterContext(deps, fn) {
    return routerContext.run(deps, fn);
}
//# sourceMappingURL=router-context.js.map