Skip to content

Fetch: Retry pages whose response headers exceed Node's 16 KB limit - #557

Open
kyrisu wants to merge 1 commit into
kepano:mainfrom
kyrisu:fix/large-response-headers
Open

kyrisu wants to merge 1 commit into
kepano:mainfrom
kyrisu:fix/large-response-headers

Conversation

@kyrisu

@kyrisu kyrisu commented Dec 2, 2026

Copy link
Copy Markdown

Fixes #556

YouTube watch pages now send about 18.5 KB of response headers, and Node's fetch rejects anything over 16 KB with UND_ERR_HEADERS_OVERFLOW, so defuddle parse fails on every YouTube URL with only fetch failed.

fetchPage still tries the global fetch first. Only on that error code does it retry once through node:http/node:https with maxHeaderSize set to 128 KB, reusing the request code the proxy path already had:

 fetchPage
   fetch(url)
+  catch cause.code === 'UND_ERR_HEADERS_OVERFLOW'
+    fetchPageViaNode(url, null)   # direct http/https, same request headers

-proxyGet(url, proxy)
+nodeGet(url, proxy | null)        # maxHeaderSize 128 KB on target responses
  • Pages that work today are fetched exactly as before. The Worker never reaches the new path, since only Node's fetch produces that code, and node:https is required lazily, so the browser bundles don't reference it.
  • Proxied requests get the larger header limit too.
  • On the Node path (the new fallback and the existing proxy path), a non-2xx, non-HTML or oversized response now fails as soon as its headers arrive, instead of after its body is downloaded. Same error messages. Without this, a server that keeps streaming an error body held the CLI open until the timeout.
  • Like the proxy path, the fallback has its own 10 s timeout and does no decompression (defuddle sends no Accept-Encoding).

New tests, all against local servers (none calls YouTube):

  • a page with 20 KB of headers loads
  • a redirect from one such page to another is followed
  • a large-header response that isn't HTML fails with "Not an HTML page"
  • a large-header 503 fails with "Failed to fetch"
  • a large-header page loads through a local HTTP proxy
  • a page with more than 128 KB of headers is still rejected
  • a page with normal headers loads as before
  • the CLI exits cleanly on a 502 with large headers and a body that never ends

The first five and the CLI test fail on main. npm test, the build, the lint scripts and size pass on Node 24, and the fetch and CLI tests also pass on Node 22. The Worker still bundles (wrangler deploy --dry-run).

The HTTPS code paths have no automated test, because that would need a test certificate. I checked the direct one by hand: the built CLI fetches a real YouTube URL and prints the transcript. HTTPS through a proxy is untested.

Node's fetch rejects any response with more than 16 KB of headers
(UND_ERR_HEADERS_OVERFLOW) before the body is read. YouTube watch pages
now send about 18.5 KB, 13.4 KB of it in a connection-allowlist-report-only
header, so the CLI printed only "fetch failed" for every YouTube URL.

fetchPage still tries the global fetch first. Only on that error code does
it retry once through node:http/node:https with a 128 KB header limit,
reusing the Node request code the proxy path already had (now nodeGet and
fetchPageViaNode, with an optional proxy). Proxied target responses get
the same limit. node:https is required lazily, so the Worker and browser
bundles are unaffected; only Node's fetch produces that error code.

The Node path now settles on the headers alone for non-2xx, non-HTML and
oversized responses, so a rejected response that keeps streaming cannot
hold the CLI open. Header checks live in one validateHeaders helper shared
by both paths.

Rejected: adding undici for setGlobalDispatcher, a 2.2 MB dependency whose v8 drops Node 20
Rejected: routing every Node page fetch through node:http, changes requests for sites that work today
Rejected: a browser user agent for YouTube, still 12.4 KB of headers and fixes one site only

This branch has not been deployed

No deployments
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.

CLI: "fetch failed" on YouTube URLs, response headers exceed Node's 16 KB limit

1 participant