返回 CodeWhale
static-installer.ts
根目录 / web / lib / static-installer.ts
1 type AssetBinding = {
2 fetch(input: Request | string, init?: RequestInit): Promise<Response>;
3 };
4
5 type FallbackFetch = (
6 request: Request,
7 env: Record<string, unknown>,
8 ctx: unknown,
9 ) => Response | Promise<Response>;
10
11 const INSTALL_SCRIPT_PATH = "/install.sh";
12
13 export async function fetchWithStaticInstaller(
14 request: Request,
15 env: Record<string, unknown>,
16 ctx: unknown,
17 fallbackFetch: FallbackFetch,
18 ): Promise<Response> {
19 const url = new URL(request.url);
20 const assets = (env as { ASSETS?: AssetBinding }).ASSETS;
21
22 if (url.pathname === INSTALL_SCRIPT_PATH && assets) {
23 const assetResponse = await assets.fetch(request);
24 if (assetResponse.status !== 404) {
25 const headers = new Headers(assetResponse.headers);
26 headers.set("content-type", "text/x-shellscript; charset=utf-8");
27 headers.set("cache-control", "public, max-age=300");
28 return new Response(assetResponse.body, {
29 status: assetResponse.status,
30 statusText: assetResponse.statusText,
31 headers,
32 });
33 }
34 }
35
36 return fallbackFetch(request, env, ctx);
37 }
38
38 lines TYPESCRIPT