The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | // Alchemy modifications are licensed under Apache-2.0. |
| 2 | // This file includes third-party code; see /THIRD_PARTY_LICENSES.md. |
| 3 | /** |
| 4 | * Vendored from `@astrojs/cloudflare` v14.4.2 (`src/vite-plugin-config.ts`, |
| 5 | * which the published package does not export). |
| 6 | * |
| 7 | * Serializes adapter settings into the `virtual:astro-cloudflare:config` |
| 8 | * virtual module consumed by the vendored runtime handler. `isPrerender` is |
| 9 | * environment-dependent: it is only `true` when the module is loaded by the |
| 10 | * (build-only) `prerender` environment. |
| 11 | */ |
| 12 | import type * as vite from "vite"; |
| 13 | |
| 14 | const VIRTUAL_CONFIG_ID = "virtual:astro-cloudflare:config"; |
| 15 | const RESOLVED_VIRTUAL_CONFIG_ID = "\0" + VIRTUAL_CONFIG_ID; |
| 16 | |
| 17 | /** |
| 18 | * The prerender-only render-scope installer (upstream's |
| 19 | * `src/utils/prerender-scope.ts`), inlined into the virtual module: installs |
| 20 | * an AsyncLocalStorage-backed render scope so each concurrent prerender |
| 21 | * request collects its metadata (content entries, images) in its own store. |
| 22 | * `installRenderScope` is first-wins, so calling it per request is |
| 23 | * idempotent. Inlined rather than a separate runtime module because the |
| 24 | * runtime is bundled (tsdown), so there is no stable file to import. |
| 25 | */ |
| 26 | const LOAD_PRERENDER_SCOPE = `() => Promise.all([import("node:async_hooks"), import("astro/app")]).then(([{ AsyncLocalStorage }, { installRenderScope }]) => ({ ensurePrerenderScope: () => { installRenderScope(new AsyncLocalStorage()); } }))`; |
| 27 | |
| 28 | export interface CompileImageConfig { |
| 29 | transformWithBinding: boolean; |
| 30 | } |
| 31 | |
| 32 | export interface AstroCloudflareConfig { |
| 33 | sessionKVBindingName: string; |
| 34 | compileImageConfig: CompileImageConfig | null; |
| 35 | /** |
| 36 | * True when the Astro config uses the Cloudflare cache provider |
| 37 | * (`cacheCloudflare()`). The handler then defaults responses without |
| 38 | * cache intent to `Cloudflare-CDN-Cache-Control: no-store`. |
| 39 | */ |
| 40 | cacheProviderEnabled: boolean; |
| 41 | } |
| 42 | |
| 43 | export function createConfigPlugin(config: AstroCloudflareConfig): vite.Plugin { |
| 44 | return { |
| 45 | name: VIRTUAL_CONFIG_ID, |
| 46 | resolveId: { |
| 47 | filter: { id: new RegExp(`^${VIRTUAL_CONFIG_ID}$`) }, |
| 48 | handler() { |
| 49 | return RESOLVED_VIRTUAL_CONFIG_ID; |
| 50 | }, |
| 51 | }, |
| 52 | load: { |
| 53 | filter: { |
| 54 | id: new RegExp(`^${RESOLVED_VIRTUAL_CONFIG_ID.replace("\0", "\\0")}$`), |
| 55 | }, |
| 56 | handler() { |
| 57 | const isPrerender = this.environment?.name === "prerender"; |
| 58 | return [ |
| 59 | ...Object.entries(config).map(([k, v]) => `export const ${k} = ${JSON.stringify(v)};`), |
| 60 | `export const isPrerender = ${isPrerender};`, |
| 61 | // The import edge to the render-scope installer — the one module with |
| 62 | // a `node:async_hooks` reference — must only exist in the prerender |
| 63 | // worker's module graph, so the thunk is generated per environment. |
| 64 | `export const loadPrerenderScope = ${isPrerender ? LOAD_PRERENDER_SCOPE : "undefined"};`, |
| 65 | ].join("\n"); |
| 66 | }, |
| 67 | }, |
| 68 | }; |
| 69 | } |