MCPcopy
hub / github.com/garrytan/gstack / fetch

Function fetch

design/test/feedback-roundtrip.test.ts:66–115  ·  view source on GitHub ↗
(req)

Source from the content-addressed store, hash-verified

64 server = Bun.serve({
65 port: 0,
66 fetch(req) {
67 const url = new URL(req.url);
68
69 if (req.method === 'GET' && (url.pathname === '/' || url.pathname === '/index.html')) {
70 return new Response(currentHtml, {
71 headers: { 'Content-Type': 'text/html; charset=utf-8' },
72 });
73 }
74
75 if (req.method === 'GET' && url.pathname === '/api/progress') {
76 return Response.json({ status: serverState });
77 }
78
79 if (req.method === 'POST' && url.pathname === '/api/feedback') {
80 return (async () => {
81 let body: any;
82 try { body = await req.json(); } catch {
83 return Response.json({ error: 'Invalid JSON' }, { status: 400 });
84 }
85 if (typeof body !== 'object' || body === null) {
86 return Response.json({ error: 'Expected JSON object' }, { status: 400 });
87 }
88
89 const isSubmit = body.regenerated === false;
90 const feedbackFile = isSubmit ? 'feedback.json' : 'feedback-pending.json';
91 fs.writeFileSync(path.join(tmpDir, feedbackFile), JSON.stringify(body, null, 2));
92
93 if (isSubmit) {
94 serverState = 'done';
95 return Response.json({ received: true, action: 'submitted' });
96 }
97 serverState = 'regenerating';
98 return Response.json({ received: true, action: 'regenerate' });
99 })();
100 }
101
102 if (req.method === 'POST' && url.pathname === '/api/reload') {
103 return (async () => {
104 const body = await req.json();
105 if (body.html && fs.existsSync(body.html)) {
106 currentHtml = fs.readFileSync(body.html, 'utf-8');
107 serverState = 'serving';
108 return Response.json({ reloaded: true });
109 }
110 return Response.json({ error: 'Not found' }, { status: 400 });
111 })();
112 }
113
114 return new Response('Not found', { status: 404 });
115 },
116 });
117
118 baseUrl = `http://localhost:${server.port}`;

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected