ueberdosis/hocuspocus

2.10.0 breaks provider in nodejs

Opened this issue ยท 8 comments

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.

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.

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.

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

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

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...

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 ๐Ÿค”

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?

Facing the same issue, pinned version to 2.9.0 for now