Skip to content

Optional: replace the cf/ transpile with #-imports over a cloudflare:sockets adapter #15

Open
5 of 5 issues completed

Description

@okomarov

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

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

Sub-issues

5 of 5 Issues completed
Loading 5 sub-issues

Activity

  1. added
    enhancement
    on Oct 9, 2026
  2. okomarov commented on Oct 9, 2026

    @okomarov
    Author

    Blocks #16 and #17. No longer; see the decision comment below.

  3. moved this to Later in Roadmapon Oct 15, 2026
  4. added
    area: runtimes
    upstream
    on Oct 16, 2026
  5. changed the title [-]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[/+] on Oct 16, 2026
  6. moved this from Later to Next in Roadmapon Oct 16, 2026
  7. moved this from Next to In progress in Roadmapon Oct 18, 2026
  8. okomarov commented on Oct 19, 2026

    @okomarov
    Author

    Decision: Workers stays on cloudflare:sockets; node:net is rejected

    Route 1 of this issue, pointing the workerd condition at src/ over workerd's node:net and node: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/ over cloudflare:sockets, with no option to choose node:net.

    How it was measured. The new cpu job in workerd-edge.yml deploys the edge test worker as a Preview. Each request opens one connection (connect, select 1, end), and its cpuTimeMs comes 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:

    Supabase, fetch_types: false median p95 p99 within 10 ms
    4.5.1 from npm 6 8 9 99%
    cloudflare:sockets 6 8 10 99%
    node:net 7 8 9 100%
    Supabase, fetch_types: true median p95 p99 within 10 ms
    4.5.1 from npm 8 11 12 91%
    cloudflare:sockets 8 11 13 84%
    node:net 10 13 15 70%
    Neon, TLS, fetch_types: false median p95 p99 within 10 ms
    cloudflare:sockets 6 9 11 98%
    node:net 8 11 23 92%

    A 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:net is built on connect(), but adds Node's stream machinery, two Buffer copies per read, nextTick scheduling, and node:crypto's pbkdf2Sync in place of WebCrypto. Cloudflare recommends neither API; pg, which its Hyperdrive docs recommend, keeps its own adapter over cloudflare: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:sockets adapter), 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.

  9. changed the title [-]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[/+] on Oct 19, 2026
  10. moved this from In progress to Later in Roadmapon Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: runtimesenhancementupstream

    Type

    No type

    Projects

    • Status
      Later

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions