(request: Request, env: Env)
| 42 | |
| 43 | export default { |
| 44 | async fetch(request: Request, env: Env): Promise<Response> { |
| 45 | const url = new URL(request.url); |
| 46 | |
| 47 | const fileMatch = url.pathname.match(/^\/c\/([^/]+)\/file\/(.+)$/); |
| 48 | if (fileMatch) { |
| 49 | const resolved = resolveMountPath(fileMatch[2]); |
| 50 | if (resolved === null) { |
| 51 | return errorJSON(new Error(`path must sit under ${MOUNT_ROOT}; got /${fileMatch[2]}`), 400); |
| 52 | } |
| 53 | return handleFile(request, env, fileMatch[1], resolved); |
| 54 | } |
| 55 | |
| 56 | const execMatch = url.pathname.match(/^\/c\/([^/]+)\/exec\/?$/); |
| 57 | if (execMatch) return handleExec(request, env, execMatch[1]); |
| 58 | |
| 59 | if (url.pathname === "/" || url.pathname === "") { |
| 60 | return new Response( |
| 61 | [ |
| 62 | "worker-javascript example", |
| 63 | "", |
| 64 | ` PUT /c/<name>/file/workspace/<path> write file at ${MOUNT_ROOT}/<path>`, |
| 65 | ` GET /c/<name>/file/workspace/<path> read file at ${MOUNT_ROOT}/<path>`, |
| 66 | " POST /c/<name>/exec run an ECMAScript module (JSON result)", |
| 67 | "", |
| 68 | ].join("\n"), |
| 69 | { headers: { "content-type": "text/plain" } }, |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | return new Response("not found", { status: 404 }); |
| 74 | }, |
| 75 | } satisfies ExportedHandler<Env>; |
| 76 | |
| 77 | async function handleFile( |
nothing calls this directly
no test coverage detected