Repository navigation
Embed URLCode in another host: createEmbeddedHandler shares core's request/response rules (#889) - #891
Merged
Conversation
…r framework (#889) createEmbeddedHandler(runtime, options) turns a self-hosted runtime into a (Request, {peer, rawHeaders}) => Promise<Response> handler that answers exactly as startServer: both hosts now share one request pipeline (host-request.ts: header lines and counts, body limits, trusted-proxy client resolution, error format and policy headers) and the same prepareResponse/prepareStream/errorResponse answer. Cloudflare's fetch response builder is the same shared fetchResponse. - Trusted proxies: trustedProxies resolves the client from the vouched peer and exactly one X-Forwarded-For, as --trusted-proxies. - Repeated headers: the host passes raw header lines, or declares headerLines: 'unavailable'; joined values containing a comma then count as repeated. runtime.handle() without headerCounts applies the same rule instead of reading a missing count as zero. - basePath: a prefix-stripping mount keeps its prefix in a function's request.url and in every path-absolute Location. - loopbackHost: optional loopback Host admission (421), as the Node server. Docs: OPERATIONS "Hosting URLCode inside another framework" (what is provided and what stays server-only), RIM-EMBED-001, SECURITY, FRAMEWORK, llms. Package budget raised with measured sizes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The ecosystem fixture's Hono host drops its copied response bridge (hono/urlcode-fetch.mjs) and uses createEmbeddedHandler with trustedProxies, loopbackHost and, for the prefix-stripping mount, basePath. The integration test now asserts X-Forwarded-For resolution from a trusted proxy, 421 for a foreign Host and the kept /mounted prefix in both hosts; the README's gaps narrow to the server-owned operations, conservative counts on pure-fetch hosts, basePath scope, byte-level header joining, the function-route catch-all and review's app.fetch false positive. fetchResponse sends an empty body as no body, so a host does not give zero bytes a default content type. OPERATIONS keeps the fixture pointer and the reverse direction from #887. Core package budget re-measured on current main. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
Fixes #889 items 1, 2, 3, 5 and 6.
API:
createEmbeddedHandler(runtime, options?)Exported from
@jimhoyd/urlcodeand returns(request: Request, info?: {peer?, rawHeaders?}) => Promise<Response>. The name avoids a clash with Cloudflare'screateFetchHandler.Shared implementation (item 1)
host-request.tsholds the request side shared by the Node server, the Vercel adapter and this adapter: header counts, body limits, client resolution, error format andbasePath.fetchResponse/fetchHeadersbuild a fetchResponsefrom the existingprepareResponse/prepareStream/errorResponse. Cloudflare's private copy is removed.Options
trustedProxies(item 2): the same resolution as the server.X-Forwarded-Foris read only from a trusted peer, and only when sent exactly once.loopbackHost(item 3): the server's 421 Host check.headerLines(item 5):'provided'(the default) requiresrawHeadersand throws a TypeError without them.'unavailable'counts a comma-joined value as repeated, so duplicate-header refusals still fire. The cost: a single legitimate comma in a checked header is refused.runtime.handle()without counts uses the same rule instead of reading 0.basePath(item 6): prefixes a function'srequest.urland root-relativeLocationheaders.origin,maxBodyBytes.Core fix exposed by the fixture: an empty body is sent as no body, so Hono no longer stamps a content type on redirects.
The ecosystem fixture now uses it
proofs/ecosystem/hono/urlcode-fetch.mjsand its copied rules are deleted. The Hono glue is just the peer address andrawHeaders, plus a second handler withbasePath: '/mounted'.test/ecosystem.integration.tsnow asserts parity withurlcode serve:X-Forwarded-Forfrom a trusted proxy resolves the client under Hono;Hostgets 421 on URLCode's paths;request.url.X-Request-Id, reload/watch, and CLI policy/host-file loading;basePathdoesn't rewrite HTML links or absolute URLs;review'sapp.fetchflag (Embedding URLCode in another host: export a Response adapter; server-only protections #889 item 7).Evidence (macOS, Node 26)
npm run verifypasses, includingtest:ecosystem12/12.npm run test:packagepasses.test/embed.test.ts(8 tests): a 19-case matrix of adapter vsstartServer(status, headers, Set-Cookie lines, body; the status list is asserted), plus streaming, trusted proxies, header counts,basePaththrough a prefix-stripping mount,loopbackHostand limits.Docs: OPERATIONS "Hosting URLCode inside another framework",
RIM-EMBED-001,RIM-HOST-001, SECURITY, FRAMEWORK, llms.🤖 Generated with Claude Code