The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /// <reference types="vite/client" /> |
| 2 | import vitePackageJson from "vite/package.json?raw"; |
| 3 | import vitePackageJsonUrl from "vite/package.json?url"; |
| 4 | // Mirrors `@cloudflare/vite-plugin`'s `playground/assets/src/index.ts`, plus |
| 5 | // the additional-module (`playground/build-output`), dependency-asset |
| 6 | // (`playground/deps-assets-importing`) and `?no-inline` |
| 7 | // (`playground/spa-with-api`) cases. |
| 8 | import additionalModule from "./additional-module.txt"; |
| 9 | import importedImage from "./imported-image.svg"; |
| 10 | import importedText from "./imported-text.txt?url"; |
| 11 | import inlineImage from "./inline-image.svg?inline"; |
| 12 | import noInlineText from "./no-inline-text.txt?no-inline"; |
| 13 | // Importing CSS must not break the Worker build. |
| 14 | import "./index.css"; |
| 15 | |
| 16 | interface Env { |
| 17 | ASSETS?: Fetcher; |
| 18 | } |
| 19 | |
| 20 | class ElementHandler { |
| 21 | element(element: Element) { |
| 22 | element.setInnerContent("Modified content"); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | const withHeader = (response: Response, value: string) => { |
| 27 | const modified = new Response(response.body, response); |
| 28 | modified.headers.append("additional-header", value); |
| 29 | return modified; |
| 30 | }; |
| 31 | |
| 32 | export default { |
| 33 | async fetch(request: Request, env: Env) { |
| 34 | const { origin, pathname } = new URL(request.url); |
| 35 | if (pathname === "/__assets") { |
| 36 | return Response.json({ assets: env.ASSETS !== undefined }); |
| 37 | } |
| 38 | if (pathname === "/additional-module") { |
| 39 | return new Response(additionalModule); |
| 40 | } |
| 41 | if (pathname === "/dependency-asset-raw") { |
| 42 | const json = JSON.parse(vitePackageJson) as { name: string }; |
| 43 | return new Response(`dependency:${json.name} url:${vitePackageJsonUrl}`); |
| 44 | } |
| 45 | if (!env.ASSETS) return new Response("no ASSETS binding", { status: 500 }); |
| 46 | switch (pathname) { |
| 47 | case "/public-directory-asset": |
| 48 | return withHeader( |
| 49 | await env.ASSETS.fetch(new URL("/public-image.svg", origin)), |
| 50 | "public-directory-asset", |
| 51 | ); |
| 52 | case "/imported-asset": |
| 53 | return withHeader(await env.ASSETS.fetch(new URL(importedImage, origin)), "imported-asset"); |
| 54 | case "/imported-asset-url-suffix": { |
| 55 | const response = await env.ASSETS.fetch(new URL(importedText, origin)); |
| 56 | return new Response(`The text content is "${await response.text()}"`); |
| 57 | } |
| 58 | case "/no-inline-asset": { |
| 59 | const response = await env.ASSETS.fetch(new URL(noInlineText, origin)); |
| 60 | return new Response(`Modified: ${await response.text()}`); |
| 61 | } |
| 62 | case "/inline-asset": |
| 63 | return withHeader(await env.ASSETS.fetch(new URL(inlineImage, origin)), "inline-asset"); |
| 64 | case "/dependency-asset": { |
| 65 | const response = await env.ASSETS.fetch(new URL(vitePackageJsonUrl, origin)); |
| 66 | return new Response(await response.text()); |
| 67 | } |
| 68 | case "/transformed-html-asset": |
| 69 | return new HTMLRewriter() |
| 70 | .on("h1", new ElementHandler()) |
| 71 | .transform(await env.ASSETS.fetch(new URL("/html-page", origin))); |
| 72 | default: |
| 73 | return new Response("vite-assets-worker"); |
| 74 | } |
| 75 | }, |
| 76 | }; |