unjs / unenv

🕊️ Convert javaScript code to be runtime agnostic
MIT License
358 stars 19 forks source link

feat(node/crypto): implement `randomBytes` #100

Closed BoxenOfDonuts closed 1 year ago

BoxenOfDonuts commented 1 year ago

Added export of randomBytes to the crypto polyfill. If another dependency requires it currently, nitro will fail to build. Currently webcrypto does not have a randomBytes method

[nitro 2:36:51 PM]  ERROR  RollupError: "randomBytes" is not exported by "node_modules/unenv/runtime/node/crypto/index.mjs", imported by "node_modules/@firebase/firestore/dist/lite/index.node.mjs".

5: import { FirebaseError, getDefaultEmulatorHostnameAndPort, createMockUserToken, getModularInstance, deepEqual } from ...
6: import nodeFetch from 'node-fetch';
7: import { randomBytes as randomBytes$1 } from 'crypto';
            ^
8:
9: const version$1 = "3.7.2";

 ERROR  "randomBytes" is not exported by "node_modules/unenv/runtime/node/crypto/index.mjs", imported by "node_modules/@firebase/firestore/dist/lite/index.node.mjs".
pi0 commented 1 year ago

Thanks. This looks good. We can also polyfill it (like this). Would you like to help on this in same PR?

BoxenOfDonuts commented 1 year ago

Yes, I can give it a shot

BoxenOfDonuts commented 1 year ago

Removed some guard rails, let me know if you want them back in.

Removed:

  1. Checking MAX_UINT32 as the internal Buffer will throw already.
  2. The If check for Size > 0
  3. The If check to either slice or return getRandomValues since slicing longer than the TypedArray should just return the TypedArray