|
1 | 1 | /** |
2 | | - * Try to get runtime config from Nitro/Nuxt environment. |
3 | | - * Supports both Nitro v2 (nitropack/runtime) and Nitro v3 (nitro/runtime-config). |
4 | | - * Returns undefined if not in a Nitro context. |
| 2 | + * Nitro runtime modules resolved via dynamic `import()` (Workers-safe: avoids a bundler-injected |
| 3 | + * `createRequire` polyfill from sync `require()`). Module namespaces are cached after first |
| 4 | + * successful load; `useRuntimeConfig()` is still invoked on each call so config stays current. |
| 5 | + * |
| 6 | + * Drain handlers remain non-blocking for the HTTP response when the host provides `waitUntil` |
| 7 | + * (see Nitro plugin); the extra `await` here only sequences work inside that background drain. |
5 | 8 | */ |
6 | | -export function getRuntimeConfig(): Record<string, any> | undefined { |
7 | | - try { |
8 | | - // eslint-disable-next-line @typescript-eslint/no-require-imports |
9 | | - const { useRuntimeConfig } = require('nitropack/runtime') |
10 | | - return useRuntimeConfig() |
11 | | - } catch { |
12 | | - // nitropack not available — try Nitro v3 |
| 9 | +let nitropackRuntime: typeof import('nitropack/runtime') | null | undefined |
| 10 | +let nitroV3Runtime: typeof import('nitro/runtime-config') | null | undefined |
| 11 | + |
| 12 | +export async function getRuntimeConfig(): Promise<Record<string, any> | undefined> { |
| 13 | + if (nitropackRuntime === undefined) { |
| 14 | + try { |
| 15 | + nitropackRuntime = await import('nitropack/runtime') |
| 16 | + } catch { |
| 17 | + nitropackRuntime = null |
| 18 | + } |
| 19 | + } |
| 20 | + if (nitropackRuntime) { |
| 21 | + return nitropackRuntime.useRuntimeConfig() |
13 | 22 | } |
14 | 23 |
|
15 | | - try { |
16 | | - // eslint-disable-next-line @typescript-eslint/no-require-imports |
17 | | - const { useRuntimeConfig } = require('nitro/runtime-config') |
18 | | - return useRuntimeConfig() |
19 | | - } catch { |
20 | | - return undefined |
| 24 | + if (nitroV3Runtime === undefined) { |
| 25 | + try { |
| 26 | + nitroV3Runtime = await import('nitro/runtime-config') |
| 27 | + } catch { |
| 28 | + nitroV3Runtime = null |
| 29 | + } |
21 | 30 | } |
| 31 | + if (nitroV3Runtime) { |
| 32 | + return nitroV3Runtime.useRuntimeConfig() |
| 33 | + } |
| 34 | + return undefined |
22 | 35 | } |
23 | 36 |
|
24 | 37 | export interface ConfigField<T> { |
25 | 38 | key: keyof T & string |
26 | 39 | env?: string[] |
27 | 40 | } |
28 | 41 |
|
29 | | -export function resolveAdapterConfig<T>( |
| 42 | +export async function resolveAdapterConfig<T>( |
30 | 43 | namespace: string, |
31 | 44 | fields: ConfigField<T>[], |
32 | 45 | overrides?: Partial<T>, |
33 | | -): Partial<T> { |
34 | | - const runtimeConfig = getRuntimeConfig() |
| 46 | +): Promise<Partial<T>> { |
| 47 | + const runtimeConfig = await getRuntimeConfig() |
35 | 48 | const evlogNs = runtimeConfig?.evlog?.[namespace] |
36 | 49 | const rootNs = runtimeConfig?.[namespace] |
37 | 50 |
|
|
0 commit comments