next.js
8e95d01c - switchyard: send route.go requests with a query string directly (#102103)

Commit
1 day ago
switchyard: send route.go requests with a query string directly (#102103) ### About this stack These PRs let Switchyard, the experimental Go server in front of self-hosted `next start` (`__NEXT_EXPERIMENTAL_SWITCHYARD=1`, #101695 and #102035), send `route.go` requests straight to their Go worker instead of through Node. Switchyard goes direct only when Node would have passed the request to the worker unchanged, so clients get the same response either way, apart from the differences each PR lists (compression, #102278, differs from Node's in places); everything else keeps going to Node. Each PR adds one kind of request that can go direct. The stack splits #101981 for review; the first two PRs have merged. 1. **Skeleton (merged)** (#102101): Node tells Switchyard which exact paths are route.go handlers, and which route.go workers are running. Every request still goes to Node. 2. **Review fixes for the skeleton (merged)** (#102168): worker-gone messages name the worker by its token, incomplete worker messages are ignored, and tests cover the worker events and how the route table is collected. 3. **Happy path** (#102102): requests to exact route.go paths go straight to the worker, with any headers, built exactly as Node would build them. 4. **→ This PR:** **Query strings** (#102103): requests with a query that Node would pass on unchanged. 5. **Dynamic segments and catch-alls** (#102113): `/go/users/[id]`, `[...path]` and `[[...path]]`, matched in the router's order. 6. **Compression** (#102278): apps with `compress` on (the default), compressed the way Node does, so browser requests can go direct. 7. **basePath** (#102114): apps that set a `basePath`. 8. **Request bodies** (#102115): POST and PUT with a body, including answers sent before the upload ends. 9. **Early Hints** (#102116): 103 responses are dropped, as Node drops them. 10. **Worker threads** (#102118): each route.go worker runs Go code on at most 4 threads by default. 11. **Trailers and the response-header limit** (#102119): trailer values are dropped and Node's limit on a worker's response headers applies, as through Node. 12. **Hop-by-hop response headers** (#102120): the worker's `Keep-Alive`, `Proxy-Authenticate` and similar headers reach the client, as through Node. ### What? Requests with a query string, like `GET /go/echo?sort=asc&page=2`, can now go straight to the worker when Node would pass the query on unchanged. Other queries still go through Node. ### Why? Node parses the query and writes it out again before calling a worker, so the worker can see a different query than the client sent. The e2e suite records examples: `?a` becomes `?a=`, `?b=1&a=2&b=3` becomes `?b=1&b=3&a=2`, number-like keys are sorted first, `__proto__` disappears, `constructor` picks up a JavaScript built-in, and percent-encodings are rewritten. A direct request has to give the worker exactly what Node would. ### How? `checkQuery` accepts a query only when that rewrite gives back the same text. Node decodes the query and encodes it again with `querystring.escape`, which leaves letters, digits and `-_.!~*'()` as they are and writes every other byte as `%XX` with uppercase hex. So each key and value must already be in that form (`?q=hello%20world&tags=a%2Cb` and `?q=caf%C3%A9` go direct), with valid UTF-8 once decoded. On top of that: `key=value` pairs only, no number-like keys, none of the names every JavaScript object already has, and repeats of a key only next to each other. It replaces the previous PR's rule that sent every query to Node. Everything else still goes to Node, and a TODO in `checkQuery` lists it for when Switchyard no longer depends on Node: `+` as a space, characters Node encodes (`,`, `;`, ...), lowercase hex or `%7E` for a character Node leaves raw, invalid encodings and UTF-8, empty pairs and pairs without `=`, number-like keys, built-in object keys, and repeats of a key apart. ### Verification - `decide.json`: 21 query cases: five that go direct (plain, already encoded, an empty key, a key repeated side by side) and 16 that go to Node, at least one for each reason. - Node's own rewrite, checked against `checkQuery`: 178,209 generated queries run through `parseUrl` and `formatUrl` as Node does; `checkQuery` accepts 9,218, and every one of those comes back unchanged. - `go test -race ./...`: a query reaches the worker unchanged on the direct path. - `switchyard-go-direct.test.ts` sends `/go/echo?q=hello%20world&tags=a%2Cb` while Node is paused, so an encoded query is shown to reach the worker directly. The response-header limit is checked through Node outside the gate (with a query pair that always goes to Node), and gated on the direct path until #102119. - With `__NEXT_EXPERIMENTAL_SWITCHYARD` off and on: the Switchyard suites (`switchyard-go`, `switchyard-go-direct`, `switchyard`, `switchyard-process`, `native-route-go`) and `next-server-nft`, 127 of 128 both ways. The query rewrites recorded through Node, including the encoded ones, give the same `requestURI` when they go direct. One more edge-case test now reaches the direct path (`/go/extras?largeHeader=1`) and is gated `@gate !switchyard` until the response-header limit lands later in the stack. The one failure both ways is "keeps external callers out of loopback-only API routes", which connects to the machine's own network address and fails locally with Switchyard off too; CI runs it. <!-- NEXT_JS_LLM --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Parents
Loading