| 1 | // Async (non-Effect) Worker that exercises `vpc_service` Fetcher bindings. |
| 2 | // The binding under test is selected with `?binding=NAME` (default `VPC`) so |
| 3 | // one worker can exercise several services — e.g. the managed resource and a |
| 4 | // `lookup` data source side by side. |
| 5 | interface FetcherLike { |
| 6 | fetch(input: string, init?: { signal?: AbortSignal }): Promise<Response>; |
| 7 | } |
| 8 | |
| 9 | type Env = Record<string, FetcherLike | undefined>; |
| 10 | |
| 11 | export default { |
| 12 | fetch: async (request: Request, env: Env) => { |
| 13 | const url = new URL(request.url); |
| 14 | const binding = env[url.searchParams.get("binding") ?? "VPC"]; |
| 15 | if (url.pathname === "/type") { |
| 16 | return Response.json({ type: typeof binding?.fetch }); |
| 17 | } |
| 18 | if (url.pathname === "/proxy") { |
| 19 | const target = url.searchParams.get("url") ?? "http://vpc/"; |
| 20 | try { |
| 21 | const res = await binding!.fetch(target, { |
| 22 | signal: AbortSignal.timeout(10_000), |
| 23 | }); |
| 24 | return Response.json({ status: res.status, body: await res.text() }); |
| 25 | } catch (e) { |
| 26 | return Response.json({ error: String(e) }); |
| 27 | } |
| 28 | } |
| 29 | return new Response("not found", { status: 404 }); |
| 30 | }, |
| 31 | }; |