( request: Request, env: Env, name: string, path: string, )
| 75 | } satisfies ExportedHandler<Env>; |
| 76 | |
| 77 | async function handleFile( |
| 78 | request: Request, |
| 79 | env: Env, |
| 80 | name: string, |
| 81 | path: string, |
| 82 | ): Promise<Response> { |
| 83 | const stub = env.ContainerExample.get(env.ContainerExample.idFromName(name)); |
| 84 | const ws = await getWorkspace(stub as unknown as Parameters<typeof getWorkspace>[0]); |
| 85 | |
| 86 | if (request.method === "PUT") { |
| 87 | const body = new Uint8Array(await request.arrayBuffer()); |
| 88 | try { |
| 89 | await ws.fs.writeFile(path, body); |
| 90 | return new Response(null, { status: 204 }); |
| 91 | } catch (error) { |
| 92 | return errorJSON(error, 500); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (request.method === "GET") { |
| 97 | try { |
| 98 | const stream = await ws.fs.readFile(path, {}); |
| 99 | return new Response(stream, { |
| 100 | status: 200, |
| 101 | headers: { "content-type": "application/octet-stream" }, |
| 102 | }); |
| 103 | } catch (error) { |
| 104 | const code = (error as { code?: string }).code; |
| 105 | if (code === "ENOENT") return errorJSON(error, 404); |
| 106 | return errorJSON(error, 500); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return new Response("method not allowed", { status: 405, headers: { allow: "GET, PUT" } }); |
| 111 | } |
| 112 | |
| 113 | async function handleExec(request: Request, env: Env, name: string): Promise<Response> { |
| 114 | if (request.method !== "POST") { |
no test coverage detected