@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 101 lines 3.04 KB
Raw
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/utils/cf-helpers.ts`).
5 *
6 * Pure Cloudflare helpers with no virtual module dependencies.
7 */
8 import { getValidatedIpFromHeader } from "@astrojs/internal-helpers/request";
9
10 export interface Runtime {
11 cfContext: ExecutionContext;
12 }
13
14 /** Minimal manifest shape needed by the Cloudflare helpers. */
15 export interface ManifestLike {
16 assets: Set<string>;
17 sessionConfig?: { options?: Record<string, unknown> } | undefined;
18 }
19
20 /**
21 * Returns a `Response` from the ASSETS binding if the request pathname
22 * is a known static asset. Returns `undefined` otherwise.
23 */
24 export function matchStaticAsset(
25 manifest: ManifestLike,
26 requestUrl: string,
27 env: Env,
28 ): Response | undefined {
29 const { pathname } = new URL(requestUrl);
30 if (manifest.assets.has(pathname)) {
31 return env.ASSETS.fetch(requestUrl.replace(/\.html$/, "")) as unknown as Response;
32 }
33 return undefined;
34 }
35
36 /**
37 * Tries the ASSETS binding as a fallback for an unmatched route.
38 * Returns the asset `Response` if found (non-404), `undefined` otherwise.
39 */
40 export async function fallbackToAssets(
41 requestUrl: string,
42 env: Env,
43 ): Promise<Response | undefined> {
44 const asset = await env.ASSETS.fetch(
45 requestUrl.replace(/index.html$/, "").replace(/\.html$/, ""),
46 );
47 if (asset.status !== 404) {
48 return asset as unknown as Response;
49 }
50 return undefined;
51 }
52
53 /**
54 * Creates a fetch function for prerendered error pages via the ASSETS binding.
55 */
56 export function createErrorPageFetch(env: Env): (url: string) => Promise<Response> {
57 return async (url: string) => env.ASSETS.fetch(url.replace(/\.html$/, "")) as unknown as Response;
58 }
59
60 /**
61 * Creates the Cloudflare-specific locals object with `cfContext`
62 * and deprecated `runtime` property getters.
63 */
64 export function createLocals(ctx: ExecutionContext): Runtime {
65 const locals: Runtime = {
66 cfContext: ctx,
67 };
68 Object.defineProperty(locals, "runtime", {
69 enumerable: false,
70 value: {
71 get env(): never {
72 throw new Error(
73 `Astro.locals.runtime.env has been removed in Astro v6. Use 'import { env } from "cloudflare:workers"' instead.`,
74 );
75 },
76 get cf(): never {
77 throw new Error(
78 `Astro.locals.runtime.cf has been removed in Astro v6. Use 'Astro.request.cf' instead.`,
79 );
80 },
81 get caches(): never {
82 throw new Error(
83 `Astro.locals.runtime.caches has been removed in Astro v6. Use the global 'caches' object instead.`,
84 );
85 },
86 get ctx(): never {
87 throw new Error(
88 `Astro.locals.runtime.ctx has been removed in Astro v6. Use 'Astro.locals.cfContext' instead.`,
89 );
90 },
91 },
92 });
93 return locals;
94 }
95
96 /**
97 * Extracts the client IP address from the `cf-connecting-ip` header.
98 */
99 export function getClientAddress(request: Request): string | undefined {
100 return getValidatedIpFromHeader(request.headers.get("cf-connecting-ip"));
101 }