storacha / freeway

🛣 Experimental IPFS HTTP gateway providing access to UnixFS data via CAR CIDs.
Other
14 stars 5 forks source link

fix(test): enable nodejs compat for miniflare #127

Closed fforbeck closed 3 weeks ago

fforbeck commented 3 weeks ago

Fix for Node.js Built-in Module Resolution in Cloudflare Worker Miniflare Tests

Overview

This PR addresses an issue with resolving Node.js built-in modules (node:buffer, node:events, node:async_hooks) in a Cloudflare Worker Test environment. The Worker encountered errors in resolving these built-in modules, which aren’t natively supported in Workers.

This issue was introduced when the Opentelemetry PR was merged, but we didn't see that because the Github Action that runs the test:miniflare was not flagging the test failure.

Error Description

During testing, the following error appeared:

Unable to resolve "dist/worker.mjs" dependency "node:buffer": no matching module rules.
If you're trying to import a Node.js built-in module, or an npm package that uses Node.js built-ins, you'll either need to:
- Bundle your Worker, configuring your bundler to polyfill Node.js built-ins
- Configure your bundler to load Workers-compatible builds by changing the main fields/conditions
- Enable the `nodejs_compat` compatibility flag and use the `NodeJsCompatModule` module type
- Find an alternative package that doesn't require Node.js built-ins

Solution

  1. To resolve this, I've enabled the nodejs_compat compatibility flag in the test/miniflare/freeway.spec.js file - which allows the Cloudflare Miniflare Worker to utilize Node.js runtime APIs, and set the compatibility date to "2024-09-23", as suggested in their docs: https://developers.cloudflare.com/workers/runtime-apis/nodejs/#get-started

  2. Updated the Github Action to halt the execution in case of test failures

  3. Updated the wrangler package to clean up some warnings during the integration tests.

Note

As of September 23, 2024, enabling nodejs_compat applies the same behavior as nodejs_compat_v2 for compatibility dates set to September 23, 2024 or later, meaning this configuration is fully aligned with the latest runtime support for Node.js compatibility in Workers.