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

Function handleFile

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

Source from the content-addressed store, hash-verified

129} satisfies ExportedHandler<Env>;
130
131async 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
169async function handleExec(request: Request, env: Env, name: string): Promise<Response> {
170 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