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

Function fetch

examples/worker-javascript/src/index.ts:44–74  ·  view source on GitHub ↗
(request: Request, env: Env)

Source from the content-addressed store, hash-verified

42
43export default {
44 async fetch(request: Request, env: Env): Promise<Response> {
45 const url = new URL(request.url);
46
47 const fileMatch = url.pathname.match(/^\/c\/([^/]+)\/file\/(.+)$/);
48 if (fileMatch) {
49 const resolved = resolveMountPath(fileMatch[2]);
50 if (resolved === null) {
51 return errorJSON(new Error(`path must sit under ${MOUNT_ROOT}; got /${fileMatch[2]}`), 400);
52 }
53 return handleFile(request, env, fileMatch[1], resolved);
54 }
55
56 const execMatch = url.pathname.match(/^\/c\/([^/]+)\/exec\/?$/);
57 if (execMatch) return handleExec(request, env, execMatch[1]);
58
59 if (url.pathname === "/" || url.pathname === "") {
60 return new Response(
61 [
62 "worker-javascript example",
63 "",
64 ` PUT /c/<name>/file/workspace/<path> write file at ${MOUNT_ROOT}/<path>`,
65 ` GET /c/<name>/file/workspace/<path> read file at ${MOUNT_ROOT}/<path>`,
66 " POST /c/<name>/exec run an ECMAScript module (JSON result)",
67 "",
68 ].join("\n"),
69 { headers: { "content-type": "text/plain" } },
70 );
71 }
72
73 return new Response("not found", { status: 404 });
74 },
75} satisfies ExportedHandler<Env>;
76
77async 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