Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudflare): enable native node.js compatibility #3064

Merged
merged 4 commits into from
Feb 4, 2025
Merged

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Feb 3, 2025

This PR enables cloudflare_pages and cloudflare_module (with recent compatibility date) to leverage from Native Node.js APIs implemented in workerd instead of using unenv mocks.

Due to versioning issues, breaking changes of unenv v2, and different implementations of @cloudflare/unenv-preset (WIP), this PR duplicates hybrid modules and overlay preset for now.

Utils related to direct fetch and node:http emulation is still used from unenv v1.

Workerd nodejs_compatibility can now be safely enabled using generated compatibility_flags thanks to #2949 and work by @petebacondarwin ❤️

Current issue: Hybrid modules need process.getBuiltinModule which needs nodejs_compat_v2 compatibility flag being passed to workerd however, it also affects wrangler behavior to do additional transforms which are incompatible with nitro. There is no_nodejs_compat_v2 however, it is mutually exclusive and nodejs_compat alone does not enables getBuiltinModule (workaround with an alternative method for hybrid handling) -- we miss some benefits of cloudflare/workerd#2147 (globals still injected from native)

🚀 Bundle size improvement

As we can use workerd native built-ins, a minimal project size reduces from 132 kB (41.1 kB gzip) to 98.1 kB (32.3 kB gzip) (-33.9KB (8.8kB gz) / -25.6% (21.4% gz))

📦 Modules

Hybrid modules:

  • node:async_hooks (missing asyncWrapProviders)
  • node:crypto (missing hash)
  • node:util / node:sys (missing parseEnv and styleText)
  • node:module (incompatible with unenv v1)
  • node:console (incompatible with unenv v1)
  • node:process (incompatible with unenv v1)

Native modules: (unenv polyfill will be replaced by workerd)

  • node:assert, node:assert/strict
  • node:buffer
  • node:diagnostics_channel
  • node:dns, node:dns/promises
  • node:events
  • node:net
  • node:path, node:path/posix, node:path/win32
  • node:querystring
  • node:stream, node:stream/consumers, node:stream/promises, node:stream/web
  • node:string_decoder
  • node:timers, node:timers/promises
  • node:url
  • node:util/types
  • node:zlib
  • node:_stream_duplex
  • node:_stream_passthrough
  • node:_stream_readable
  • node:_stream_transform
  • node:_stream_writable

@@ -1,3 +1,3 @@
name = "nitro-test"
compatibility_date = "2024-09-19"
assets = { directory = "./.output/public/", binding = "ASSETS"}
# assets = { directory = "./.output/public/", binding = "ASSETS"}
Copy link
Member Author

@pi0 pi0 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO (perhaps in other PR): when merging, we should transform relative paths. (or override assets)

src/presets/cloudflare/utils.ts Outdated Show resolved Hide resolved
@pi0 pi0 marked this pull request as ready for review February 4, 2025 10:46
@pi0 pi0 merged commit ccf5726 into v2 Feb 4, 2025
4 checks passed
@pi0 pi0 deleted the feat/cf-native-node2 branch February 4, 2025 10:46
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.

1 participant