Repository navigation
refactor(server): one route handler for REST, MCP, download links and serve - #739
Merged
Merged
Conversation
… serve The request's scope, the refused line of an unlogged error answer, X-Request-Id and the answer to an unreadable body were written out in rest/handler.js, pivotkit-mcp's http.js, downloads.js and serve.js. createRouteHandler in route-handler.js is now the one copy; each family supplies its check, its answer and its unreadable-body answer. Hosts pass their info on to the download links, so both name one request id. listen.js reads socket addresses with request-id.js's plainAddress instead of a copy of its own. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es and plainAddress Review follow-ups: serve takes only precheck and fetch from its route handler, the info typedef matches the hosts', tests cover a hosted family's id and line, release on a thrown route, and plainAddress. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Step 3 of the layering rework: one route handler instead of four copies of the per-request wrapper.
What changed
createRouteHandler(context, family)lives inpackages/pivotkit-server/src/route-handler.js. pivotkit-mcp gets it throughpivotkit-server/internal. It is the one copy of what every route family did per request:requestScopeFor), kept ininforefusedline of an error answer that wrote none (logUnanswered)X-Request-Idon every answercheckandanswerare required.familyOf,finish(REST: the id in a problem body),release(REST: the large-body place) andunreadableare optional.trustProxyand the log come from the context.rest/handler.js, pivotkit-mcp'shttp.jsandserve.jskeep only their check and answer.downloads.jsbecame a family of its own. Its hosts (serve, pivotkit-mcp) now pass theirinfoon todownloads.precheck,fetchandproblem, so both name one request id. The download family's entry in the hosts'routeFamilyis gone. The download links name their family once (DOWNLOAD_FAMILY).plainAddress. As you asked, the two copies are now one.listen.jsreads socket addresses withrequest-id.js'splainAddress, now exported.::ffff:prefix. The request-id.js copy strips it only before a dotted IPv4.socket.remoteAddress. Node writes a mapped address there in dotted form, so both copies gave the same result.::ffff:7f00:1would differ, but no socket gives it. Even thenisLoopbackrefused both results.TRANSIENT,NETWORK_CODES,NETWORK_TEXTS) go into step 4. They are about which errors are transient and how they are worded, which is that step's topic.Behaviour: no change for any request through
serve, pivotkit-mcp or RESTpeekSignIn, then the log line.serve: the same answers and lines. A download 404 is still logged once at info. The inner family marks the request's line as written, so the host writes none.serve's defaultinfowas{ address: "" }, now{}. Both give a scope without an address.trustProxyis read fromcontext.options. That is the object each family read before,serveincluded:createContextkeeps the options it is given.A difference that only shows outside these hosts:
createDownloadHandler(...).fetch(request)called without a host now makes a scope of its own, with an address and anX-Request-Id. Before, it ran underrequestId() ?? newRequestId(). Every caller in this repository passesinfo, andpivotkit-server/internalis not public interface (docs/versioning.md).Release note
pivotkit-mcp now imports
createRouteHandlerfrompivotkit-server/internal. A pivotkit-mcp of this release needs the pivotkit-server of this release.servealready refuses a pivotkit-mcp whose imports this pivotkit-server lacks, and names both versions. Thepivotkit-serverrange of pivotkit-mcp must be raised when the release is cut. RELEASING.md already says to release pivotkit-server first.Not part of this change (say what does not fit)
serve.jsproblemand pivotkit-mcp'slisten().fetch. That keeps a host that skips the precheck safe. Before this change it ran twice too.Growth (scripts/growth.js measures, before → after)
createServerHandleris below the limit now)createHttpHandler(mcp)createRestHandlercreateDownloadHandlerscripts/growth-baseline.jsonis lowered in this pull request.Checks
npm run lint,npm run types,npm test(all workspaces andtests/),npm run growth,npm run pack:check: green locally.npm test.test/route-handler.test.jscovers the handler:releaseon success, on an unreadable body and on a thrown routetrustProxyplainAddressservetakes onlyprecheckandfetchinfotypedefPlaces checked for docs:
docs/reference/service-exports.mdneeds no change, because onlyinternalgrew. pivotkit-mcp's AGENTS.md needs no change.🤖 Generated with Claude Code