( request: Request, env: Env, name: string, path: string, )
| 169 | } satisfies ExportedHandler<Env>; |
| 170 | |
| 171 | async function handleFile( |
| 172 | request: Request, |
| 173 | env: Env, |
| 174 | name: string, |
| 175 | path: string, |
| 176 | ): Promise<Response> { |
| 177 | const stub = env.ContainerExample.get(env.ContainerExample.idFromName(name)); |
| 178 | // `wrangler types` doesn't surface the accessor the withWorkspace |
| 179 | // mixin installs, so cast at the boundary. |
| 180 | const ws = await getWorkspace(stub as unknown as Parameters<typeof getWorkspace>[0]); |
| 181 | |
| 182 | if (request.method === "PUT") { |
| 183 | const body = new Uint8Array(await request.arrayBuffer()); |
| 184 | try { |
| 185 | await ws.fs.writeFile(path, body); |
| 186 | return new Response(null, { status: 204 }); |
| 187 | } catch (error) { |
| 188 | return errorJSON(error, 500); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (request.method === "GET") { |
| 193 | try { |
| 194 | const stream = await ws.fs.readFile(path, {}); |
| 195 | return new Response(stream, { |
| 196 | status: 200, |
| 197 | headers: { "content-type": "application/octet-stream" }, |
| 198 | }); |
| 199 | } catch (error) { |
| 200 | const code = (error as { code?: string }).code; |
| 201 | if (code === "ENOENT") return errorJSON(error, 404); |
| 202 | return errorJSON(error, 500); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return new Response("method not allowed", { status: 405, headers: { allow: "GET, PUT" } }); |
| 207 | } |
| 208 | |
| 209 | async function handleExec(request: Request, env: Env, name: string): Promise<Response> { |
| 210 | if (request.method !== "POST") { |
no test coverage detected