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

Function fetch

examples/container/src/index.ts:137–168  ·  view source on GitHub ↗
(request: Request, env: Env)

Source from the content-addressed store, hash-verified

135
136export default {
137 async fetch(request: Request, env: Env): Promise<Response> {
138 const url = new URL(request.url);
139
140 const fileMatch = url.pathname.match(/^\/c\/([^/]+)\/file\/(.+)$/);
141 if (fileMatch) {
142 const resolved = resolveMountPath(fileMatch[2]);
143 if (resolved === null) {
144 return errorJSON(new Error(`path must sit under ${MOUNT_ROOT}; got /${fileMatch[2]}`), 400);
145 }
146 return handleFile(request, env, fileMatch[1], resolved);
147 }
148
149 const execMatch = url.pathname.match(/^\/c\/([^/]+)\/exec\/?$/);
150 if (execMatch) return handleExec(request, env, execMatch[1]);
151
152 if (url.pathname === "/" || url.pathname === "") {
153 return new Response(
154 [
155 "container example",
156 "",
157 ` PUT /c/<name>/file/workspace/<path> write file at ${MOUNT_ROOT}/<path>`,
158 ` GET /c/<name>/file/workspace/<path> read file at ${MOUNT_ROOT}/<path>`,
159 " POST /c/<name>/exec run a command (JSON result)",
160
161 "",
162 ].join("\n"),
163 { headers: { "content-type": "text/plain" } },
164 );
165 }
166
167 return new Response("not found", { status: 404 });
168 },
169} satisfies ExportedHandler<Env>;
170
171async function handleFile(

Callers

nothing calls this directly

Calls 4

resolveMountPathFunction · 0.70
errorJSONFunction · 0.70
handleFileFunction · 0.70
handleExecFunction · 0.70

Tested by

no test coverage detected