MCPcopy Create free account
hub / github.com/cloudflare/computer / handleFile

Function handleFile

examples/container/src/index.ts:171–207  ·  view source on GitHub ↗
(
  request: Request,
  env: Env,
  name: string,
  path: string,
)

Source from the content-addressed store, hash-verified

169} satisfies ExportedHandler<Env>;
170
171async 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
209async function handleExec(request: Request, env: Env, name: string): Promise<Response> {
210 if (request.method !== "POST") {

Callers 1

fetchFunction · 0.70

Calls 6

getWorkspaceFunction · 0.90
errorJSONFunction · 0.70
getMethod · 0.65
arrayBufferMethod · 0.65
writeFileMethod · 0.65
readFileMethod · 0.65

Tested by

no test coverage detected