Skip to content

Commit

Permalink
provided support for react-native
Browse files Browse the repository at this point in the history
  • Loading branch information
dioptre committed Jan 9, 2020
1 parent d101ffb commit 2b498ee
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/random/random-byte.js
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
module.exports = require('nanoid/random');
if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
module.exports = require('react-native-randombytes').randomBytes;
} else {
/*jshint -W061 */
var crypto = eval("require('crypto');"); //Required for React Native to work
if (crypto.randomFillSync) {
// We reuse buffers with the same size to avoid memory fragmentations
// for better performance
var buffers = {};
module.exports = function (bytes) {
var buffer = buffers[bytes];
if (!buffer) {
// `Buffer.allocUnsafe()` faster because it don’t clean memory.
// We do not need it, since we will fill memory with new bytes anyway.
buffer = Buffer.allocUnsafe(bytes);
if (bytes <= 255) {
buffers[bytes] = buffer;
}
}
return crypto.randomFillSync(buffer);
};
} else {
module.exports = crypto.randomBytes;
}
}

0 comments on commit 2b498ee

Please sign in to comment.