The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { TestServices } from "alchemy/Contributions"; |
| 2 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 3 | import { DEFAULT_LOCAL_ENDPOINT } from "../AuthProvider.ts"; |
| 4 | import * as Floci from "./Floci.ts"; |
| 5 | import { flociServices } from "./FlociServices.ts"; |
| 6 | |
| 7 | /** |
| 8 | * Floci serves virtual-host data planes on the gateway when the Host |
| 9 | * header is the AWS hostname (`{bucket}.s3-website-{region}.amazonaws.com`, |
| 10 | * `{apiId}.appsync-api.{region}.amazonaws.com`, |
| 11 | * `{apiId}.execute-api.{region}.amazonaws.com`, |
| 12 | * `{distributionId}.cloudfront.net`). Live tests GET those hosts; under |
| 13 | * `ALCHEMY_TEST_DEV` rewrite the URL to the emulator and keep the Host so |
| 14 | * the virtual-host filter still fires. |
| 15 | */ |
| 16 | const rewriteVirtualHostToFloci = ( |
| 17 | request: HttpClientRequest.HttpClientRequest, |
| 18 | ): HttpClientRequest.HttpClientRequest => { |
| 19 | if (!URL.canParse(request.url)) return request; |
| 20 | const url = new URL(request.url); |
| 21 | if ( |
| 22 | !/\.s3-website-[a-z0-9-]+\.amazonaws\.com$/i.test(url.hostname) && |
| 23 | !/\.appsync-api\.[a-z0-9-]+\.amazonaws\.com$/i.test(url.hostname) && |
| 24 | !/\.execute-api\.[a-z0-9-]+\.amazonaws\.com$/i.test(url.hostname) && |
| 25 | !/\.cloudfront\.net$/i.test(url.hostname) |
| 26 | ) { |
| 27 | return request; |
| 28 | } |
| 29 | const rewritten = new URL(url.href); |
| 30 | const endpoint = new URL(DEFAULT_LOCAL_ENDPOINT); |
| 31 | rewritten.protocol = endpoint.protocol; |
| 32 | rewritten.hostname = endpoint.hostname; |
| 33 | rewritten.port = endpoint.port; |
| 34 | return request.pipe( |
| 35 | HttpClientRequest.setUrl(rewritten.toString()), |
| 36 | HttpClientRequest.setHeader("host", url.hostname), |
| 37 | ); |
| 38 | }; |
| 39 | |
| 40 | /** Tests under `ALCHEMY_TEST_DEV` run their AWS clients against Floci. */ |
| 41 | export const testServices = (): TestServices => ({ |
| 42 | layer: flociServices(), |
| 43 | mapRequest: rewriteVirtualHostToFloci, |
| 44 | // Local workerd trusts no self-signed certs and ignores |
| 45 | // NODE_TLS_REJECT_UNAUTHORIZED (it's C++, not Node). Its runtime DOES fold |
| 46 | // `NODE_EXTRA_CA_CERTS` into workerd's outbound `trustedCertificates` (see |
| 47 | // cloudflare-runtime Internet.ts), so point it at the emulator CA bundle |
| 48 | // that `ensureFloci` refreshes on every health check. |
| 49 | env: { NODE_EXTRA_CA_CERTS: Floci.FLOCI_CA_PATH }, |
| 50 | }); |