Skip to content

Embed URLCode in another host: createEmbeddedHandler shares core's request/response rules (#889) - #891

Merged
jimhoyd merged 2 commits into
mainfrom
claude/embedding-adapter
Sep 29, 2026
Merged

jimhoyd merged 2 commits into
mainfrom
claude/embedding-adapter

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Fixes #889 items 1, 2, 3, 5 and 6.

API: createEmbeddedHandler(runtime, options?)

Exported from @jimhoyd/urlcode and returns (request: Request, info?: {peer?, rawHeaders?}) => Promise<Response>. The name avoids a clash with Cloudflare's createFetchHandler.

Shared implementation (item 1)

  • The new host-request.ts holds the request side shared by the Node server, the Vercel adapter and this adapter: header counts, body limits, client resolution, error format and basePath.
  • The new fetchResponse/fetchHeaders build a fetch Response from the existing prepareResponse/prepareStream/errorResponse. Cloudflare's private copy is removed.
  • Streaming starts on the first chunk. An early failure gives 502, and a later one ends the body with an error.

Options

  • trustedProxies (item 2): the same resolution as the server. X-Forwarded-For is 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) requires rawHeaders and 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's request.url and root-relative Location headers.
  • 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.mjs and its copied rules are deleted. The Hono glue is just the peer address and rawHeaders, plus a second handler with basePath: '/mounted'.
  • test/ecosystem.integration.ts now asserts parity with urlcode serve:
    • X-Forwarded-For from a trusted proxy resolves the client under Hono;
    • a foreign Host gets 421 on URLCode's paths;
    • the mounted prefix is kept on redirects and request.url.
  • Gaps closed in the README: copied response rules, trusted proxies, the Host check, repeated headers on Node, and base path.
  • Remaining:
    • the server-only list: probes and readiness drain, request log, admission and timeouts, stream limits, upstream X-Request-Id, reload/watch, and CLI policy/host-file loading;
    • the conservative comma rule on pure-fetch hosts;
    • basePath doesn't rewrite HTML links or absolute URLs;
    • repeated non-cookie response headers are joined into one line;
    • there is no function catch-all;
    • review's app.fetch flag (Embedding URLCode in another host: export a Response adapter; server-only protections #889 item 7).

Evidence (macOS, Node 26)

  • npm run verify passes, including test:ecosystem 12/12. npm run test:package passes.
  • New test/embed.test.ts (8 tests): a 19-case matrix of adapter vs startServer (status, headers, Set-Cookie lines, body; the status list is asserted), plus streaming, trusted proxies, header counts, basePath through a prefix-stripping mount, loopbackHost and limits.
  • Core budget raised to 911/3604 KiB, measured after rebasing: 929,130 packed / 3,686,818 unpacked bytes.

Docs: OPERATIONS "Hosting URLCode inside another framework", RIM-EMBED-001, RIM-HOST-001, SECURITY, FRAMEWORK, llms.

🤖 Generated with Claude Code

jimhoyd and others added 2 commits September 28, 2026 21:17
…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]>
@jimhoyd
jimhoyd enabled auto-merge (squash) September 29, 2026 02:28
@jimhoyd
jimhoyd merged commit a2647f6 into main Sep 29, 2026
28 checks passed
@jimhoyd
jimhoyd deleted the claude/embedding-adapter branch September 29, 2026 02:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Embedding URLCode in another host: export a Response adapter; server-only protections

1 participant