Repository navigation
Release 0.2.31 - #457
Merged
Merged
Release 0.2.31#457
Conversation
Part of #449. Depends on nemar-cli#1710 only for the effect, not for merging. ## What changes A new `apiFetch` (`src/lib/api-fetch.ts`) is the way server-only code calls `api.nemar.org`. When the Pages project declares a service binding named `API`, a call to the API's origin goes over it with the visitor's address (this request's `CF-Connecting-IP`) in `X-Forwarded-For`, which nemar-cli#1710 counts as that visitor's own. When there is no binding (`astro dev`, previews, and every project until it is wired) it is the same `fetch` as before. Call sites moved: the catalog reads (`api.ts`, `api-cache.ts`), the session lookup in the middleware, the sign-in, ORCID, news-media and `/api/v1` proxies, the notices feed (`/api/notices`, the highest-volume server call after the catalog), and the device-authorization, docs-grant and private-grant clients. The middleware puts the binding and the visitor into the existing request context (`request-context.ts`) and leaves the binding alone under `astro dev`, where the platform proxy's stand-in answers 503. ADR 0026 records the decisions: only the API's origin takes the binding, redirects are followed here for a GET or HEAD (a binding sends a redirect target back to the bound Worker whatever its host), a request with a body gets its 3xx back unfollowed, and a binding failure is not retried publicly. The `/api/v1` proxy turns such a 3xx into a logged 502, since it copies no `Location` and the browser would otherwise see a bare redirect. Three ways the API's counting can fail without an error are watched for, none of them with an address in the log: - The first call of each kind in an isolate is classified by `X-RateLimit-Bucket`: `site-ip` is logged quietly; `ip`, `site-unattributed`, and an answer with no rate-limit headers are warnings. - A missing or malformed visitor, and a failing binding, are logged at most once a minute with a count. - If the API's counter does not persist for binding-invoked calls, nothing is limited and every answer looks healthy. For one visitor, calls one to ten seconds apart are compared; three comparisons in a row in which `X-RateLimit-Remaining` did not fall log one error per isolate. (The API sends the reset time only on a refusal, so the check does not use it.) ## Not in this PR - The `[[services]]` entries in `wrangler.toml` and `wrangler.test.toml`, and the checks that belong with them (a once-per-isolate warning when a production host has no binding, the `[env.preview]` restatement the non-inheritable binding needs). They go in after nemar-cli#1710 is deployed to the matching environment: wiring first would put every call in the API's shared unattributed bucket, which is worse than today. - Modules that also run in the browser (dashboard, account, collaborators, admin, news, users-admin) and the data and Zarr hosts, which need a look at zone-level caching first. Tracked in #449 follow-ups. `test/client-bundle-guard.test.ts` fails if a client `<script>` ever reaches `api-fetch.ts` or `request-context.ts`. ## Testing - [x] `bun run lint`, `typecheck` (0 errors), `test` (3349 pass, 7 skipped), `build`, in a CI-faithful worktree outside the home directory. - [x] `api-fetch.test.ts` (80 tests): no binding, a binding, identity-header replacement for a `Headers` instance, tuples and mixed-case records, lookalike origins, every redirect status and verb, the 20/21 hop ceiling, binding failure not retried, first-call report, the stuck-counter detector (parallel bursts, out-of-order answers, per-visitor keying, the 200-visitor bound). - [x] `test/routes/api-binding-call-sites.test.ts` (15 tests): every converted call site leaves over a recording binding naming the visitor, with `globalThis.fetch` failing the test. Reverting any one of the 16 sites to plain `fetch` fails a test. - [x] Middleware tests: the binding and visitor reach the render, the session lookup goes over the binding, a client-set `X-Forwarded-For`/`X-Real-IP`/`True-Client-IP` never becomes the visitor, a non-binding `API` value is ignored. - [x] Mutation check: 34 mutants of `api-fetch.ts` by the author, then 72 more (api-fetch, middleware and call sites) by a separate test-quality review. All killed except three equivalent mutants. - [x] Real workerd: `wrangler pages dev --service API=<stand-in worker>`. A binding call carries no `CF-Connecting-IP` and the visitor in `X-Forwarded-For` (a spoofed browser header was replaced), cookies pass, an in-API redirect stays on the binding, an off-API redirect goes out publicly without the visitor or cookie, a POST redirect comes back unfollowed. The client bundle has no `node:async_hooks`. - [ ] Production keying can only be confirmed after nemar-cli#1710 is deployed and the binding is wired (the API's development Worker bypasses the limiter). The wiring PR reads the `[api-fetch]` first-call lines on test.nemar.org and then nemar.org.
Part of #449. Follows #450. nemar-cli#1710 (the API side) is merged to `dev`; this waits only for its deploy to `nemar-api-dev` before it merges to `staging`. ## What changes - `wrangler.test.toml`: `[[services]] API -> nemar-api-dev`, the Worker behind api-test.nemar.org. `deploy-test.yml` swaps this file into `wrangler.toml`, so it applies to test.nemar.org. - `wrangler.toml`: `[[services]] API -> nemar-api`, the Worker behind api.nemar.org. Previews are left without a binding on purpose (services are not inherited into `[env.preview]`, and a branch build runs unreviewed code that must not be able to name a visitor at the production API). Wrangler warns about the missing restatement; the comment says to leave the warning. - `src/middleware.ts`: a production host (`nemar.org`, `www`, `ww2`, `app`) whose project has no `API` binding logs one warning per isolate, the way `EMBED_ANALYTICS` does, because the site would otherwise work and quietly stay in the API's shared pool. Staging has a binding but is not a production host, so a missing binding there is not warned about. - `test/wrangler-bindings.test.ts`: production names `nemar-api`, staging names `nemar-api-dev`, and neither file restates `services` under `env.preview`. - ADR 0026, the changelog, AGENTS.md (promotion gate and `wrangler pages dev` note), `.rules/astro.md` (manual staging deploys) and the handoff say all of this. ## Order 1. nemar-cli `dev` deployed to `nemar-api-dev` (the Deploy Backend run for the #1710 merge). This PR is then merged to `staging`, which deploys test.nemar.org with the binding. 2. Read the `[api-fetch]` lines on test.nemar.org. The development API bypasses its limiter and sends no rate-limit headers, so the line to expect is the warning that an answer carried none. It shows the binding applied (the watching runs only for calls over a binding) and nothing about the visitor's address. 3. nemar-cli released to `main` and deployed to `nemar-api`. Only then is the `staging` to `main` release of the website promoted (the gate is in AGENTS.md step 8 and the handoff), so production never has the binding in front of an API that does not read the visitor. 4. After the release, check on nemar.org: `X-RateLimit-Bucket: site-ip` with a falling `X-RateLimit-Remaining` across two calls, and the `[api-fetch]` first-call line in the production deployment's tail (`site-ip`, info). ## Testing - [x] Middleware tests: each production host (`nemar.org`, `app`, `www`, `ww2`) warns on its first request; a repeat does not; test.nemar.org, a `*.pages.dev` preview and localhost say nothing; a production host with a binding says nothing. Mutants of the condition (app host only, apex only, no www or ww2, wrong host class, no once-flag) are killed. - [x] `test/wrangler-bindings.test.ts`: three tests, each killed by swapping the Worker names or restating `services` under `env.preview`. - [x] `bun run lint`, `typecheck` (0 errors), `test` (3365 pass). - [x] Two Sonnet reviewers' findings (a test that did not cover every host, wording about staging, the staging expectation, the promotion gate living only in this body, the manual-deploy and `wrangler pages dev` notes, the edge-protection clause in the ADR) are fixed here. - [ ] CI, then the checks under Order on test.nemar.org and nemar.org.
The home page, the news pages, the RSS feed, the sitemap and the news OG cards now pass apiFetch as the fetch of the news readers, so each request forwards its own visitor to the API Worker over the service binding and is counted per visitor (ADR 0026), instead of leaving from the shared egress pool. A guard test fails if any of these six call sites goes back to the global fetch. Refs #449.
This was referenced Oct 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Promotes
stagingtomainfor 0.2.31. The release makes the site's server-side calls to the API go over a Cloudflare service binding that names the visitor, so the API's rate limiter counts each visitor on their own instead of the site's few shared egress addresses as one (the cause of the/discover429 errors, nemar-cli#1709; website#449, ADR 0026). Everything else on staging since 0.2.30 is the version bumps and the sync commit.The API side is live: nemar-cli 0.10.17 (ADR 0098 and 0099) was released and deployed to
nemar-apifirst, which this release requires (the gate in AGENTS.md). Verified on the production API before opening this PR: first deploy of the 0.10.17 build is serving,x-ratelimit-limitis 1000,x-ratelimit-callerisedge, the rate-limit headers are inaccess-control-expose-headers.What changes in production
wrangler.tomldeclares[[services]] binding = "API", service = "nemar-api". Previews deliberately have none, so a branch build cannot name a visitor at the production API;test/wrangler-bindings.test.tsfails if the binding is restated there.src/lib/api-fetch.ts(apiFetch): over the binding when the request has one and the URL is on the API's origin, otherwise plainfetch. It strips caller-supplied identity headers, setsX-Forwarded-ForfromCF-Connecting-IP, follows redirects itself (a binding sends a redirect back to the bound Worker, measured under workerd), never retries a failed binding call on the public network, and watches the API's rate-limit headers and logs once per isolate if a call is counted under the shared address, in the unattributed bucket, or not at all./api/v1proxy, and (website#456) the news pages, the RSS feed, the sitemap and the social-card redirect.test/routes/api-binding-call-sites.test.tsfails if a server caller of those reads passes plainfetch, andtest/client-bundle-guard.test.tskeeps the request context out of the browser bundle.APIbinding logs one warning per isolate; a binding that is not a service binding logs one error.Checks
3a01ba9c), staging deployed from it,https://test.nemar.org/version.jsonreports 0.2.31,/discoverand/newsanswer 200 there.nemar-api-dev, which bypasses the rate limiter, so staging proves the path, not the keying. The keying can only be seen on production, from the[api-fetch]log lines the site writes (counted per visitor, once per isolate).After the merge
release.ymltagsv0.2.31and reopens-dev0. Production builds frommainthrough the Cloudflare integration. Verify withcurl -s https://nemar.org/version.json(0.2.31),/discover, a dataset page and/news, then read the first[api-fetch]line from the production site's logs.Rollback
Remove the
[[services]]block (the site returns to plainfetchand the shared addresses, which the 0.10.17 API still serves, with the higher cap), or roll back the Pages deployment to 0.2.30. Nothing here writes data.