import { AsyncLocalStorage } from "node:async_hooks"; import { SerializeOptions } from "cookie"; type JSONValue = | string | number | boolean | { [x: string]: JSONValue } | Array; export type Store = { reqId: number; build: "development" | "production"; canReload: boolean; cookies: { all: () => Record; get: (key: string) => string | undefined; set(key: string, value: string, options?: SerializeOptions): void; destroy(key: string, options?: SerializeOptions): void; outgoingCookies(): { name: string; value: string; options?: SerializeOptions; }[]; }; encryption: { encrypt(value: any): Promise; decrypt(value: string): Promise; }; flash: { add(message: JSONValue): void; }; context: { type: "page"; request: Request; assets: string[]; } | null; assets: string[]; }; let asyncLocalStorage = new AsyncLocalStorage(); export function runStore(store: Store, fn: () => Promise) { return asyncLocalStorage.run(store, fn); } export function getStore() { let store = asyncLocalStorage.getStore(); if (!store) { throw new Error("store used outside of render"); } return store; }