{"version":3,"file":"server.mjs","names":[],"sources":["../../src/adapters/next/server.ts"],"sourcesContent":["/**\n * Server-side Next.js App Router adapter for RSC render-scoped containers.\n *\n * @module\n */\n\nimport type { Container } from \"../../core/container\";\nimport { createChildContainer, createContainer } from \"../../core/container\";\nimport type { Provider } from \"../../core/provider\";\nimport type {\n\tCreateNextDiOptions,\n\tNextDiAdapter,\n\tRunWithRequestContainerOptions,\n} from \"./types\";\n\nexport { dehydrate } from \"./hydration\";\nexport type {\n\tCreateNextDiOptions,\n\tDehydrateOptions,\n\tHydratable,\n\tHydrationSchema,\n\tHydrationSnapshot,\n\tNextDiAdapter,\n\tRequestCache,\n\tRunWithRequestContainerOptions,\n\tSerializable,\n\tSerializablePrimitive,\n} from \"./types\";\n\nconst assertServerRuntime = (): void => {\n\tif (\"window\" in globalThis) {\n\t\tthrow new Error(\n\t\t\t\"di-craft/next/server can only be used in a server runtime.\",\n\t\t);\n\t}\n};\n\n/**\n * Creates a server adapter for Next.js App Router and React Server Components.\n *\n * The root container is created once. `getRequestContainer` creates a child\n * container through the provided React request cache, so repeated calls inside\n * one RSC render pass resolve through the same scoped dependency graph.\n *\n * Import this helper from a server-only composition file. The adapter does not\n * import React itself; pass `cache` from `react` so React/Next owns render\n * memoization.\n *\n * @example\n * ```ts\n * import \"server-only\";\n * import { cache } from \"react\";\n * import { createNextDi } from \"di-craft/next/server\";\n *\n * export const { getRequestContainer } = createNextDi({\n *   cache,\n *   providers,\n * });\n * ```\n */\nexport const createNextDi = ({\n\tcache,\n\tproviders = [],\n\trequestProviders,\n}: CreateNextDiOptions): NextDiAdapter => {\n\tassertServerRuntime();\n\n\tconst rootContainer = createContainer(providers);\n\tconst createRequestContainer = (\n\t\tproviders: readonly Provider[] = [],\n\t): Container =>\n\t\tcreateChildContainer(rootContainer, [\n\t\t\t...(requestProviders?.() ?? []),\n\t\t\t...providers,\n\t\t]);\n\tconst getRequestContainer = cache(() => createRequestContainer());\n\n\treturn {\n\t\tgetRootContainer: () => rootContainer,\n\t\tgetRequestContainer,\n\t\tcreateRequestContainer,\n\t\trunWithRequestContainer: async <TResult>({\n\t\t\tproviders = [],\n\t\t\trun,\n\t\t}: RunWithRequestContainerOptions<TResult>): Promise<Awaited<TResult>> => {\n\t\t\tconst container = createRequestContainer(providers);\n\n\t\t\ttry {\n\t\t\t\treturn await run(container);\n\t\t\t} finally {\n\t\t\t\tawait container.dispose();\n\t\t\t}\n\t\t},\n\t\tdisposeRootContainer: () => rootContainer.dispose(),\n\t};\n};\n"],"mappings":";;;;AA6BA,MAAM,4BAAkC;CACvC,IAAI,YAAY,YACf,MAAM,IAAI,MACT,4DACD;AAEF;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,MAAa,gBAAgB,EAC5B,OACA,YAAY,CAAC,GACb,uBACyC;CACzC,oBAAoB;CAEpB,MAAM,gBAAgB,gBAAgB,SAAS;CAC/C,MAAM,0BACL,YAAiC,CAAC,MAElC,qBAAqB,eAAe,CACnC,GAAI,mBAAmB,KAAK,CAAC,GAC7B,GAAG,SACJ,CAAC;CAGF,OAAO;EACN,wBAAwB;EACxB,qBAJ2B,YAAY,uBAAuB,CAI5C;EAClB;EACA,yBAAyB,OAAgB,EACxC,YAAY,CAAC,GACb,UACyE;GACzE,MAAM,YAAY,uBAAuB,SAAS;GAElD,IAAI;IACH,OAAO,MAAM,IAAI,SAAS;GAC3B,UAAU;IACT,MAAM,UAAU,QAAQ;GACzB;EACD;EACA,4BAA4B,cAAc,QAAQ;CACnD;AACD"}