diff options
-rw-r--r-- | bench/snippets/crypto.mjs | 8 | ||||
-rw-r--r-- | src/node-fallbacks/crypto.js | 10 |
2 files changed, 12 insertions, 6 deletions
diff --git a/bench/snippets/crypto.mjs b/bench/snippets/crypto.mjs index 132e6595d..6c0a74eaa 100644 --- a/bench/snippets/crypto.mjs +++ b/bench/snippets/crypto.mjs @@ -1,10 +1,10 @@ // so it can run in environments without node module resolution import { bench, run } from "../node_modules/mitata/src/cli.mjs"; -var crypto = globalThis.crypto; -// web crypto is not a global in node -if (!crypto) { - crypto = await import("crypto"); +var crypto = require("crypto"); + +if (!crypto.getRandomValues) { + crypto.getRandomValues = globalThis.crypto.getRandomValues; } var foo = new Uint8Array(65536); diff --git a/src/node-fallbacks/crypto.js b/src/node-fallbacks/crypto.js index 2ab82cc89..7b72c4ea1 100644 --- a/src/node-fallbacks/crypto.js +++ b/src/node-fallbacks/crypto.js @@ -1,4 +1,10 @@ export * from "crypto-browserify"; -const { randomUUID } = crypto; -export { randomUUID }; +// we deliberately reference crypto. directly here because we want to preserve the This binding +export var getRandomValues = (array) => { + return crypto.getRandomValues(array); +}; + +export var randomUUID = () => { + return crypto.randomUUID(); +}; |