(request: Request, env: Env)
| 96 | |
| 97 | export default { |
| 98 | async fetch(request: Request, env: Env): Promise<Response> { |
| 99 | const url = new URL(request.url); |
| 100 | |
| 101 | const fileMatch = url.pathname.match(/^\/c\/([^/]+)\/file\/(.+)$/); |
| 102 | if (fileMatch) { |
| 103 | const resolved = resolveMountPath(fileMatch[2]); |
| 104 | if (resolved === null) { |
| 105 | return errorJSON(new Error(`path must sit under ${MOUNT_ROOT}; got /${fileMatch[2]}`), 400); |
| 106 | } |
| 107 | return handleFile(request, env, fileMatch[1], resolved); |
| 108 | } |
| 109 | |
| 110 | const execMatch = url.pathname.match(/^\/c\/([^/]+)\/exec\/?$/); |
| 111 | if (execMatch) return handleExec(request, env, execMatch[1]); |
| 112 | |
| 113 | if (url.pathname === "/" || url.pathname === "") { |
| 114 | return new Response( |
| 115 | [ |
| 116 | "worker example", |
| 117 | "", |
| 118 | ` PUT /c/<name>/file/workspace/<path> write file at ${MOUNT_ROOT}/<path>`, |
| 119 | ` GET /c/<name>/file/workspace/<path> read file at ${MOUNT_ROOT}/<path>`, |
| 120 | " POST /c/<name>/exec run a shell command (JSON result)", |
| 121 | "", |
| 122 | ].join("\n"), |
| 123 | { headers: { "content-type": "text/plain" } }, |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | return new Response("not found", { status: 404 }); |
| 128 | }, |
| 129 | } satisfies ExportedHandler<Env>; |
| 130 | |
| 131 | async function handleFile( |
nothing calls this directly
no test coverage detected