|
| 1 | +/** |
| 2 | + * The `t3x-update-relay` hostname, after #71 moved the real relay to `coil-update-relay`. |
| 3 | + * |
| 4 | + * This exists because `DEFAULT_RELAY_URL` is compiled into the app. Every build ever shipped has |
| 5 | + * `https://t3x-update-relay.businesses.workers.dev` baked in, the env override that could point |
| 6 | + * them elsewhere is never set on a normal install (a `.app` launched from Finder inherits |
| 7 | + * launchd's environment, not a shell's), and an app that stops finding its relay has no remaining |
| 8 | + * channel through which to be told. So this hostname has to keep answering for as long as any |
| 9 | + * build that predates the rename is still running — which is indefinitely, and certainly longer |
| 10 | + * than it will feel necessary. |
| 11 | + * |
| 12 | + * ## Why a proxy and not a redirect |
| 13 | + * |
| 14 | + * The obvious shim is a 301/307 to the new host. It is the wrong one here, and the reason is |
| 15 | + * specific rather than stylistic: `relayClient.ts` does not use `fetch`. It goes through Effect's |
| 16 | + * `HttpClient`, and it classifies any non-2xx response as `bad-status` on BOTH tiers — the |
| 17 | + * `/latest` poll and the long-lived `/events` stream. Whether that client transparently follows a |
| 18 | + * redirect, and whether it does so on a `text/event-stream` response it intends to keep open, is |
| 19 | + * not a property that can be tested against builds that are already installed on other people's |
| 20 | + * machines. Being wrong about it does not degrade those builds, it strands them, permanently and |
| 21 | + * without a symptom — which is the exact failure this whole file exists to prevent. |
| 22 | + * |
| 23 | + * A pass-through has no such question attached. From the client's side the bytes, the status and |
| 24 | + * the streaming behaviour are what they were before the rename, because they are literally the |
| 25 | + * upstream response. |
| 26 | + * |
| 27 | + * ## Why it forwards every method and path |
| 28 | + * |
| 29 | + * Including `/notify`, which only the release workflow calls. The workflow is *supposed* to post |
| 30 | + * to the new host, but that target lives in a repo variable rather than in this repository, so |
| 31 | + * "supposed to" is an assumption about a value nobody here can see. Forwarding it costs one line |
| 32 | + * and makes the shim correct whichever host the workflow was pointed at. The HMAC signature |
| 33 | + * covers the body and the timestamp, not the hostname, so it survives the hop unchanged. |
| 34 | + * |
| 35 | + * ## Why `UpdateChannel` is still exported |
| 36 | + * |
| 37 | + * It is unreachable from here — nothing below touches the binding. It stays because this Worker is |
| 38 | + * deployed OVER the existing `t3x-update-relay`, which already has the Durable Object class in its |
| 39 | + * migration history. Dropping the export would make the deploy an implicit class deletion, which |
| 40 | + * Cloudflare rejects without an explicit `deleted_classes` migration; keeping it makes the cutover |
| 41 | + * a plain code deploy, and makes rolling back to the real relay a plain code deploy too. |
| 42 | + */ |
| 43 | + |
| 44 | +export { UpdateChannel } from "./UpdateChannel.ts"; |
| 45 | + |
| 46 | +/** |
| 47 | + * Where the real relay now lives. |
| 48 | + * |
| 49 | + * Hardcoded rather than bound through `wrangler.legacy.jsonc`, so that the shim and the Worker it |
| 50 | + * fronts cannot be configured apart. This file and `wrangler.jsonc`'s `name` are the two halves of |
| 51 | + * one fact and they live in the same package for that reason. |
| 52 | + */ |
| 53 | +export const UPSTREAM_ORIGIN = "https://coil-update-relay.businesses.workers.dev"; |
| 54 | + |
| 55 | +/** The forwarded request: same method, headers, body and path — only the origin changes. */ |
| 56 | +export function rewriteToUpstream(requestUrl: string): string { |
| 57 | + const url = new URL(requestUrl); |
| 58 | + const target = new URL(UPSTREAM_ORIGIN); |
| 59 | + target.pathname = url.pathname; |
| 60 | + target.search = url.search; |
| 61 | + return target.toString(); |
| 62 | +} |
| 63 | + |
| 64 | +export default { |
| 65 | + fetch(request: Request): Promise<Response> { |
| 66 | + return fetch(new Request(rewriteToUpstream(request.url), request)); |
| 67 | + }, |
| 68 | +} satisfies ExportedHandler; |
0 commit comments