Skip to content

Commit 229bc6d

Browse files
authored
Micro-optimize randomFill. NFC (#23309)
Only use the node fallback code when its actually needed, otherwise assume that `crypto.getRandomValues` is available out-of-the-box (which it is on node v19 and above.
1 parent e83c573 commit 229bc6d

23 files changed

+31
-39
lines changed

src/library_wasi.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -561,28 +561,20 @@ var WasiLibrary = {
561561
// random.h
562562

563563
$initRandomFill: () => {
564-
if (typeof crypto == 'object' && typeof crypto.getRandomValues == 'function') {
565-
#if SHARED_MEMORY
566-
// like with most Web APIs, we can't use Web Crypto API directly on shared memory,
567-
// so we need to create an intermediate buffer and copy it to the destination
568-
return (view) => view.set(crypto.getRandomValues(new Uint8Array(view.byteLength)));
569-
#else
570-
return (view) => crypto.getRandomValues(view);
571-
#endif
572-
}
573-
574-
#if ENVIRONMENT_MAY_BE_NODE
564+
#if ENVIRONMENT_MAY_BE_NODE && MIN_NODE_VERSION < 190000
565+
// This block is not needed on v19+ since crypto.getRandomValues is builtin
575566
if (ENVIRONMENT_IS_NODE) {
576-
return (view) => require('crypto').randomFillSync(view);
567+
var nodeCrypto = require('crypto');
568+
return (view) => nodeCrypto.randomFillSync(view);
577569
}
578570
#endif // ENVIRONMENT_MAY_BE_NODE
579571

580-
// we couldn't find a proper implementation, as Math.random() is not
581-
// suitable for /dev/random, see emscripten-core/emscripten/pull/7096
582-
#if ASSERTIONS
583-
abort('no cryptographic support found for random function. consider polyfilling it if you want to use something insecure like Math.random(), e.g. put this in a --pre-js: var crypto = { getRandomValues: (array) => { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } };');
572+
#if SHARED_MEMORY
573+
// like with most Web APIs, we can't use Web Crypto API directly on shared memory,
574+
// so we need to create an intermediate buffer and copy it to the destination
575+
return (view) => view.set(crypto.getRandomValues(new Uint8Array(view.byteLength)));
584576
#else
585-
abort();
577+
return (view) => crypto.getRandomValues(view);
586578
#endif
587579
},
588580

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8346
1+
8332
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20320
1+
20255
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8328
1+
8314
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20288
1+
20223
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9348
1+
9336
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24088
1+
24023
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8290
1+
8280
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20213
1+
20148
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8290
1+
8280

0 commit comments

Comments
 (0)