( request: Request, env: Env, name: string, path: string, )
| 129 | } satisfies ExportedHandler<Env>; |
| 130 | |
| 131 | async function handleFile( |
| 132 | request: Request, |
| 133 | env: Env, |
| 134 | name: string, |
| 135 | path: string, |
| 136 | ): Promise<Response> { |
| 137 | const stub = env.ContainerExample.get(env.ContainerExample.idFromName(name)); |
| 138 | // `wrangler types` doesn't surface the accessor the withWorkspace |
| 139 | // mixin installs, so cast at the boundary. |
| 140 | const ws = await getWorkspace(stub as unknown as Parameters<typeof getWorkspace>[0]); |
| 141 | |
| 142 | if (request.method === "PUT") { |
| 143 | const body = new Uint8Array(await request.arrayBuffer()); |
| 144 | try { |
| 145 | await ws.fs.writeFile(path, body); |
| 146 | return new Response(null, { status: 204 }); |
| 147 | } catch (error) { |
| 148 | return errorJSON(error, 500); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (request.method === "GET") { |
| 153 | try { |
| 154 | const stream = await ws.fs.readFile(path, {}); |
| 155 | return new Response(stream, { |
| 156 | status: 200, |
| 157 | headers: { "content-type": "application/octet-stream" }, |
| 158 | }); |
| 159 | } catch (error) { |
| 160 | const code = (error as { code?: string }).code; |
| 161 | if (code === "ENOENT") return errorJSON(error, 404); |
| 162 | return errorJSON(error, 500); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return new Response("method not allowed", { status: 405, headers: { allow: "GET, PUT" } }); |
| 167 | } |
| 168 | |
| 169 | async function handleExec(request: Request, env: Env, name: string): Promise<Response> { |
| 170 | if (request.method !== "POST") { |
no test coverage detected