feat: use bson-objectid for id generation

This commit is contained in:
thecodrr
2021-05-21 19:19:48 +05:00
parent 161946928f
commit 2d6380fdbc
3 changed files with 371 additions and 24 deletions

View File

@@ -0,0 +1,26 @@
/**
*
* @param {number} size
* @returns {Buffer}
*/
module.exports.randomBytes = function randomBytes(size) {
if (!global.crypto || !crypto)
throw new Error("Crypto is not supported on this platform.");
if (crypto.randomBytes) return crypto.randomBytes(size);
if (!crypto.getRandomValues)
throw new Error(
"Crypto.getRandomValues is not available on this platform."
);
const buffer = Buffer.allocUnsafe(size);
crypto.getRandomValues(buffer);
return buffer;
};
module.exports.randomInt = function () {
const randomBuffer = module.exports.randomBytes(1);
let randomNumber = randomBuffer[0] / 0xff; // / (0xffffffff + 1);
return Math.floor(randomNumber * 0xffffff);
};