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

Function fetch

examples/worker-shell/src/index.ts:98–128  ·  view source on GitHub ↗
(request: Request, env: Env)

Source from the content-addressed store, hash-verified

96
97export default {
98 async fetch(request: Request, env: Env): Promise<Response> {
99 const url = new URL(request.url);
100
101 const fileMatch = url.pathname.match(/^\/c\/([^/]+)\/file\/(.+)$/);
102 if (fileMatch) {
103 const resolved = resolveMountPath(fileMatch[2]);
104 if (resolved === null) {
105 return errorJSON(new Error(`path must sit under ${MOUNT_ROOT}; got /${fileMatch[2]}`), 400);
106 }
107 return handleFile(request, env, fileMatch[1], resolved);
108 }
109
110 const execMatch = url.pathname.match(/^\/c\/([^/]+)\/exec\/?$/);
111 if (execMatch) return handleExec(request, env, execMatch[1]);
112
113 if (url.pathname === "/" || url.pathname === "") {
114 return new Response(
115 [
116 "worker example",
117 "",
118 ` PUT /c/<name>/file/workspace/<path> write file at ${MOUNT_ROOT}/<path>`,
119 ` GET /c/<name>/file/workspace/<path> read file at ${MOUNT_ROOT}/<path>`,
120 " POST /c/<name>/exec run a shell command (JSON result)",
121 "",
122 ].join("\n"),
123 { headers: { "content-type": "text/plain" } },
124 );
125 }
126
127 return new Response("not found", { status: 404 });
128 },
129} satisfies ExportedHandler<Env>;
130
131async 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