Repository navigation
Description
Low priority, optional cleanup. Workers keeps the cf/ build over cloudflare:sockets; running src/ over workerd's node:net was measured and rejected (see the decision comment below and #159).
Why there is a transpile
src/ imports Node's net, tls, crypto, os, fs and perf_hooks. transpile.cf.js copies src/ to cf/src/ at build time and swaps those imports, by exact text match, for cf/polyfills.js: a Node-style Socket over cloudflare:sockets with startTls, WebCrypto stand-ins for pbkdf2Sync, createHmac and createHash, stubs for process.env, os.userInfo and fs.readFile, and setImmediate. The workerd export condition serves cf/src/.
What the transpile costs: a second copy of the code in the tarball, stack traces that point at cf/src/, and replacements that match exact source text (a reworded import ships unpatched; the workerd CI jobs run the built cf/src/ and would fail).
The cleanup
A package.json imports map, with src/ importing #net, #tls and #crypto:
"#net": { "workerd": "./cf/net.js", "default": "node:net" }Only the socket must stay Workers-specific; crypto probably too, since workerd's node:crypto pbkdf2Sync showed up in the node:net profile and the polyfill uses WebCrypto deriveBits. Measure it rather than assume. process, os, fs, perf_hooks, setImmediate and Buffer would come from nodejs_compat; check each against the minimum compatibility date. Then transpile.cf.js and cf/src/ go.
When to do it
Only together with #16, if cjs/ stops being transpiled as well, so the build step goes rather than shrinks. Not for its own sake: users see no difference.
Checks
- The CPU job in
workerd-edge.yml(dispatch withcpu_rounds=60) shows no extra CPU against the latest release. The release process runs the same gate. - The local workerd matrix and the edge scenarios pass, including TLS upgrade after the SSLRequest,
subscribe()andCOPY TO, and no unhandled "Stream was cancelled" on close (Cloudflare Workers: closing a connection reports an unhandled rejection on compatibility dates before 2026-03-03 #1). - Bundlers that never target Workers still build ([NOT A DRIVER BUG] Bundlers fail on cloudflare:sockets imports in Next.js and other builds that never target Workers #71).
Done when
src/ runs on Workers through the imports map, transpile.cf.js and cf/src/ are gone, and the CPU gate passes.
Related upstream: porsager#1109 proposes dropping the custom net/tls polyfills to fix a Workers memory leak; not reproduced here.
Sub-issues
Collapse Sub-issuesSub-issues
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsLater
Activity
okomarov commentedon Oct 9, 2026
Blocks #16 and #17.No longer; see the decision comment below.[-]Run src/ directly on Cloudflare Workers and drop the cf/ transpile[/-][+]Run on Cloudflare Workers through workerd's node:net and node:tls, and drop the cf/ polyfill and transpile[/+]okomarov commentedon Oct 19, 2026
Decision: Workers stays on
cloudflare:sockets;node:netis rejectedRoute 1 of this issue, pointing the
workerdcondition atsrc/over workerd'snode:netandnode:tls, landed on main after 4.5.1. Before release it was measured on Cloudflare's network, and it costs more CPU per connection. #159 reverts it: Workers ships one build,cf/overcloudflare:sockets, with no option to choosenode:net.How it was measured. The new
cpujob inworkerd-edge.ymldeploys the edge test worker as a Preview. Each request opens one connection (connect,select 1, end), and itscpuTimeMscomes from Workers Logs. Targets: the Supabase transaction pooler in eu-west-1 over plaintext, and Neon over TLS. Two runs of 60 rounds, pooled; CPU ms, and the share of connections within the Workers Free 10 ms limit:fetch_types: falsefetch_types: truefetch_types: falseA third run sent the variants in a random order every round and added 4.5.1 on Neon (run). node:net cost more than cloudflare:sockets in 71 to 78% of request pairs; 4.5.1, which runs the same code as cloudflare:sockets, sat at 48 to 51%.
Where the extra goes. workerd's
node:netis built onconnect(), but adds Node's stream machinery, twoBuffercopies per read,nextTickscheduling, andnode:crypto'spbkdf2Syncin place of WebCrypto. Cloudflare recommends neither API;pg, which its Hyperdrive docs recommend, keeps its own adapter overcloudflare:sockets.From now on. Releases are gated on the same job: a release that costs more CPU on Workers than the latest one does not ship.
What this issue becomes. Dropping the transpile is now an optional cleanup (route 2, an imports map over a
cloudflare:socketsadapter), to do only together with #16. It no longer blocks #16 or #17, and it is no longer the parent for Workers issues; #1 stands on its own. The body is rewritten to match; the previous body is in this issue's edit history.[-]Run on Cloudflare Workers through workerd's node:net and node:tls, and drop the cf/ polyfill and transpile[/-][+]Optional: replace the cf/ transpile with #-imports over a cloudflare:sockets adapter[/+]