Repository navigation
Call the API through a service binding that names the visitor - #450
Merged
Merged
Conversation
Tested with a recording binding fixture, a mutation check (16 mutants, all killed), and under workerd with a stand-in API Worker.
Catalog reads and the sign-in, ORCID, news-media and /api/v1 proxies. Unchanged until a project declares the binding.
Deploying nemar-website with
|
| Latest commit: |
ad3d1ba
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://50712ab7.nemar-website.pages.dev |
| Branch Preview URL: | https://feature-issue-449-api-servic.nemar-website.pages.dev |
Five calls for one visitor in one window that leave the remaining count alone log one error per isolate. Mutation check: 24 mutants, all killed.
The count check no longer needs a reset header the API sends only on a refusal, ignores calls that race the counter, and logs binding faults, unreadable visitors and bad bucket kinds without addresses or queries. Requests with bodies get their redirect back unfollowed. Mutation check: 34 mutants, all killed.
The platform proxy supplies a stub that answers 503. A declared env.API that is not a service binding is logged once.
Notices feed, device authorization, docs and private grants. The news media proxy logs the status it turns into a 502.
4 of 5 tasks
neuromechanist
added a commit
that referenced
this pull request
Oct 12, 2026
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.
This was referenced Oct 12, 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.
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 callsapi.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'sCF-Connecting-IP) inX-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 samefetchas 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/v1proxies, 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 underastro 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/v1proxy turns such a 3xx into a logged 502, since it copies noLocationand 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:
X-RateLimit-Bucket:site-ipis logged quietly;ip,site-unattributed, and an answer with no rate-limit headers are warnings.X-RateLimit-Remainingdid 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
[[services]]entries inwrangler.tomlandwrangler.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.test/client-bundle-guard.test.tsfails if a client<script>ever reachesapi-fetch.tsorrequest-context.ts.Testing
bun run lint,typecheck(0 errors),test(3349 pass, 7 skipped),build, in a CI-faithful worktree outside the home directory.api-fetch.test.ts(80 tests): no binding, a binding, identity-header replacement for aHeadersinstance, 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).test/routes/api-binding-call-sites.test.ts(15 tests): every converted call site leaves over a recording binding naming the visitor, withglobalThis.fetchfailing the test. Reverting any one of the 16 sites to plainfetchfails a test.X-Forwarded-For/X-Real-IP/True-Client-IPnever becomes the visitor, a non-bindingAPIvalue is ignored.api-fetch.tsby the author, then 72 more (api-fetch, middleware and call sites) by a separate test-quality review. All killed except three equivalent mutants.wrangler pages dev --service API=<stand-in worker>. A binding call carries noCF-Connecting-IPand the visitor inX-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 nonode:async_hooks.[api-fetch]first-call lines on test.nemar.org and then nemar.org.