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

Function handleFile

examples/worker-javascript/src/index.ts:77–111  ·  view source on GitHub ↗
(
  request: Request,
  env: Env,
  name: string,
  path: string,
)

Source from the content-addressed store, hash-verified

75} satisfies ExportedHandler<Env>;
76
77async 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
113async function handleExec(request: Request, env: Env, name: string): Promise<Response> {
114 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