All files / packages/dataflint-mcp/src request-context.ts

33.33% Statements 12/36
100% Branches 2/2
28.57% Functions 2/7
33.33% Lines 12/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 951x                       1x                                         1x                 1x                         1x                               1x                       1x 10x 10x           1x 10x 10x  
import { AsyncLocalStorage } from "async_hooks";
 
/**
 * Request context data stored per-request using AsyncLocalStorage.
 * This ensures session IDs and tenant IDs are isolated per async execution context,
 * preventing race conditions when concurrent requests arrive.
 */
interface RequestContext {
    sessionId: string | null;
    tenantId: string | null;
}
 
const REQUEST_CONTEXT_KEY = "__dataflint_request_context__";
 
/**
 * AsyncLocalStorage instance for request-scoped context.
 * Uses globalThis to survive bundling across multiple imports (same pattern as Logger).
 */
function getRequestContextStorage(): AsyncLocalStorage<RequestContext> {
    if (!(globalThis as any)[REQUEST_CONTEXT_KEY]) {
        (globalThis as any)[REQUEST_CONTEXT_KEY] =
            new AsyncLocalStorage<RequestContext>();
    }
    return (globalThis as any)[REQUEST_CONTEXT_KEY];
}
 
/**
 * Get the current session ID from the request context.
 * Returns null if called outside of a request context.
 *
 * This is the safe way to get session ID - it reads from the current
 * async execution context rather than a shared global variable.
 */
export function getCurrentSessionId(): string | null {
    const context = getRequestContextStorage().getStore();
    return context?.sessionId ?? null;
}
 
/**
 * Get the current tenant ID from the request context.
 * Returns null if called outside of a request context or if tenant ID not set.
 */
export function getCurrentTenantId(): string | null {
    const context = getRequestContextStorage().getStore();
    return context?.tenantId ?? null;
}
 
/**
 * Run an async operation with a specific session ID in context.
 * All async operations within the callback will have access to this session ID.
 *
 * @param sessionId - The MCP session ID for this request
 * @param fn - Async function to execute within the session context
 * @returns Promise resolving to the function's return value
 */
export function runWithSessionId<T>(
    sessionId: string | null,
    fn: () => T | Promise<T>
): T | Promise<T> {
    return getRequestContextStorage().run({ sessionId, tenantId: null }, fn);
}
 
/**
 * Run an async operation with session ID and tenant ID in context.
 * All async operations within the callback will have access to both.
 *
 * @param sessionId - The MCP session ID for this request
 * @param tenantId - The tenant ID for multi-tenant routing
 * @param fn - Async function to execute within the context
 * @returns Promise resolving to the function's return value
 */
export function runWithContext<T>(
    sessionId: string | null,
    tenantId: string | null,
    fn: () => T | Promise<T>
): T | Promise<T> {
    return getRequestContextStorage().run({ sessionId, tenantId }, fn);
}
 
/**
 * Create a session ID getter function for dependency injection.
 * This returns a function that safely reads from AsyncLocalStorage.
 */
export function createSessionIdGetter(): () => string | null {
    return getCurrentSessionId;
}
 
/**
 * Create a tenant ID getter function for dependency injection.
 * This returns a function that safely reads from AsyncLocalStorage.
 */
export function createTenantIdGetter(): () => string | null {
    return getCurrentTenantId;
}