| 1 | // Guarded dynamic import of `cloudflare:workers` (the same trick waku's own |
| 2 | // adapter uses) so modules also load outside a Cloudflare environment. |
| 3 | const DO_NOT_BUNDLE = ""; |
| 4 | |
| 5 | export interface CounterStub { |
| 6 | get(): Promise<number>; |
| 7 | increment(): Promise<number>; |
| 8 | } |
| 9 | |
| 10 | export interface CounterNamespace { |
| 11 | getByName(name: string): CounterStub; |
| 12 | } |
| 13 | |
| 14 | export async function readEnv(): Promise<Record<string, unknown>> { |
| 15 | try { |
| 16 | const mod = await import( |
| 17 | /* @vite-ignore */ DO_NOT_BUNDLE + "cloudflare:workers" |
| 18 | ); |
| 19 | return mod.env as Record<string, unknown>; |
| 20 | } catch { |
| 21 | return {}; |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | export async function counterNamespace(): Promise< |
| 26 | CounterNamespace | undefined |
| 27 | > { |
| 28 | const env = await readEnv(); |
| 29 | return env.COUNTER as CounterNamespace | undefined; |
| 30 | } |