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

2.10.0 breaks provider in nodejs #787

Open
tobowers opened this issue Feb 8, 2024 · 7 comments
Open

2.10.0 breaks provider in nodejs #787

tobowers opened this issue Feb 8, 2024 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@tobowers
Copy link

tobowers commented Feb 8, 2024

Description
The compiled code in 2.10.0 of the provider has these lines:

/* eslint-env browser */
const getRandomValues = crypto.getRandomValues.bind(crypto);

/**
 * Isomorphic module for true random numbers / buffers / uuids.
 *
 * Attention: falls back to Math.random if the browser does not support crypto.
 *
 * @module random
 */

const uint32 = () => getRandomValues(new Uint32Array(1))[0];

crypto does not appear in the 2.9.0 package.

crypto is not defined in the global scope so a simple `import {...} from "@hocuspocus/provider" errors with "ReferenceError: crypto is not defined"

Steps to reproduce the bug
Simply import 2.10.0 provider in a nodejs environment

Expected behavior
Continue to work :).

Environment?

  • operating system: macos / nodejs 18
  • browser: N/A
  • mobile/desktop:
  • Hocuspocus version: 2.10.0

Additional context
Add any other context about the problem here.

@tobowers tobowers added the bug Something isn't working label Feb 8, 2024
@janthurau
Copy link
Collaborator

hmm, the mentioned code is coming from lib0/random and uses the crypto API (https://nodejs.org/api/crypto.html#cryptogetrandomvaluestypedarray), which should be available since node v17. I've just tried this in node20 and node18, both works, just v16 fails.

@tobowers
Copy link
Author

tobowers commented Feb 8, 2024

hmm, the mentioned code is coming from lib0/random and uses the crypto API (https://nodejs.org/api/crypto.html#cryptogetrandomvaluestypedarray), which should be available since node v17. I've just tried this in node20 and node18, both works, just v16 fails.

The problem is "crypto" isn't imported.

@tobowers
Copy link
Author

tobowers commented Feb 8, 2024

I should note that I'm using the esm compiled code. I didn't check the cjs

@sbittmann
Copy link

@tobowers: Did you find a solution for this?
@janthurau: I have the same problem in context with node18 and nuxt ssr...

@tobowers
Copy link
Author

Right it's a bug and no, I just locked our version at 2.9.0

@tobowers: Did you find a solution for this? @janthurau: I have the same problem in context with node18 and nuxt ssr...

@janthurau
Copy link
Collaborator

janthurau commented Feb 12, 2024

It looks like node18 only has the crypto global if the --experimental-global-webcrypto flag is used (https://nodejs.org/docs/latest-v18.x/api/globals.html#crypto_1). In 20 it's global by default.

Need to check how to tell the bundler to add this import for backward compatibility 🤔

@tobowers
Copy link
Author

It looks like node18 only has the crypto global if the --experimental-global-webcrypto flag is used (https://nodejs.org/docs/latest-v18.x/api/globals.html#crypto_1). In 20 it's global by default.

Need to check how to tell the bundler to add this import for backward compatibility 🤔

I think you can just import crypto from "crypto" at the top of the file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants