| 1 | import subpathHtmlWithExt from "#modules/html-example.html"; |
| 2 | import bin from "./modules/bin-example.bin"; |
| 3 | import html from "./modules/html-example.html"; |
| 4 | import sql from "./modules/sql-example.sql"; |
| 5 | import text from "./modules/text-example.txt"; |
| 6 | import text2 from "./modules/text__example__2.txt"; |
| 7 | import wasm from "./modules/wasm-example.wasm"; |
| 8 | import init from "./modules/wasm-example.wasm?init"; |
| 9 | import wasmWithModuleParam from "./modules/wasm-example.wasm?module"; |
| 10 | |
| 11 | // Removed because this seems to be a Vite-specific feature, rather than a Rolldown feature. |
| 12 | // TODO: Find out if this works with Vite. |
| 13 | // import subpathHtml from "#html"; |
| 14 | |
| 15 | interface Instance { |
| 16 | exports: { |
| 17 | add(a: number, b: number): number; |
| 18 | }; |
| 19 | } |
| 20 | |
| 21 | export default { |
| 22 | async fetch(request: Request) { |
| 23 | const url = new URL(request.url); |
| 24 | |
| 25 | switch (url.pathname) { |
| 26 | case "/bin": |
| 27 | return Response.json({ byteLength: bin.byteLength }); |
| 28 | case "/html": |
| 29 | return new Response(html, { headers: { "Content-Type": "text/html" } }); |
| 30 | // case "/subpath-html": |
| 31 | // return new Response(subpathHtml, { |
| 32 | // headers: { "Content-Type": "text/html" }, |
| 33 | // }); |
| 34 | case "/subpath-html-with-ext": |
| 35 | return new Response(subpathHtmlWithExt, { |
| 36 | headers: { "Content-Type": "text/html" }, |
| 37 | }); |
| 38 | case "/text": |
| 39 | return new Response(text); |
| 40 | case "/text2": |
| 41 | return new Response(text2); |
| 42 | case "/sql": |
| 43 | return new Response(sql); |
| 44 | case "/wasm": { |
| 45 | const instance = (await WebAssembly.instantiate(wasm)) as Instance; |
| 46 | return Response.json({ result: instance.exports.add(3, 4) }); |
| 47 | } |
| 48 | case "/wasm-with-module-param": { |
| 49 | const instance = (await WebAssembly.instantiate( |
| 50 | wasmWithModuleParam, |
| 51 | )) as Instance; |
| 52 | return Response.json({ result: instance.exports.add(5, 6) }); |
| 53 | } |
| 54 | case "/wasm-with-init-param": { |
| 55 | const instance = (await init()) as Instance; |
| 56 | return Response.json({ result: instance.exports.add(7, 8) }); |
| 57 | } |
| 58 | default: |
| 59 | return new Response(null, { status: 404 }); |
| 60 | } |
| 61 | }, |
| 62 | } satisfies ExportedHandler; |