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

Impossible to use cloudflare worker nodejs-compatibility mode due to transitive imports #37

Open
dsumac opened this issue Nov 22, 2023 · 4 comments

Comments

@dsumac
Copy link

dsumac commented Nov 22, 2023

Context

Bug

  • to use native api, we have to activate compatibility mode without add polyfill
  • when we activate this option the worker expects an import like node:...
  • there is transitively an import without prefix

image

  • so we can not use your lib with native prefix import

Analysis

You use sha.js which use safe-buffer which uses native node Buffer api without node prefix import node:buffer

image

@kenny-statsig
Copy link
Contributor

Hey @dsumac, thanks for sharing these details. We are aware of this incompatibility and plan to replace the sha.js dependency. Until then are you able to workaround this by using polyfills instead of compatibility mode?

@dsumac
Copy link
Author

dsumac commented Nov 23, 2023

Thanks @kenny-statsig.
We use node_compat = true(in our wrangler.toml config file) option to use polyfill.
But it doesn't work. Error at deployement.

@cpreid
Copy link

cpreid commented Nov 27, 2023

@dsumac Can you try adding compatibility_date. This works fine for me:

compatibility_date = "2023-01-20"
node_compat = true

@dsumac
Copy link
Author

dsumac commented Dec 6, 2023

@cpreid , my explicate was maybe not clear.

Effectively this config it works.
Even with an older compat mode.
And this is this kind of config which works.

But I would like use native nodejs api in the same time, in the same worker code.
Ex:

import { createHash } from "node:crypto";

const hash = createHash("valueToHash");

....

To directly use node api like this without polyfill we must activate nodejs compatibility flag:
https://developers.cloudflare.com/workers/configuration/compatibility-dates/#nodejs-compatibility-flag

And when we activate this config, it doesn't work:

compatibility_date = "2023-01-20"
compatibility_flags = [ "nodejs_compat" ]

# we must disable node_compat to use compatibility_flags
# node_compat = true

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

No branches or pull requests

3 participants