Comparing version 3.0.0 to 3.0.1
@@ -5,2 +5,6 @@ # Changelog | ||
## [3.0.1][] - 2021-01-18 | ||
- Optimize buffering crypto random generator | ||
## [3.0.0][] - 2021-01-06 | ||
@@ -16,3 +20,5 @@ | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v2.2.0...HEAD | ||
[unreleased]: https://github.com/metarhia/metautil/compare/v3.0.1...HEAD | ||
[3.0.1]: https://github.com/metarhia/metautil/compare/v3.0.0...v3.0.1 | ||
[3.0.0]: https://github.com/metarhia/metautil/compare/v2.2.0...v3.0.0 | ||
[2.2.0]: https://github.com/metarhia/common/releases/tag/v2.2.0 |
@@ -6,37 +6,2 @@ 'use strict'; | ||
class CryptoRandomPrefetcher { | ||
constructor(bufSize, valueSize) { | ||
if (bufSize % valueSize !== 0) { | ||
throw new RangeError('buffer size must be a multiple of value size'); | ||
} | ||
this.buf = crypto.randomBytes(bufSize); | ||
this.pos = 0; | ||
this.vsz = valueSize; | ||
} | ||
// Return Buffer with next `valueSize` random bytes. | ||
next() { | ||
if (this.pos === this.buf.length) { | ||
this.pos = 0; | ||
crypto.randomFillSync(this.buf); | ||
} | ||
const end = this.pos + this.vsz; | ||
const buf = this.buf.slice(this.pos, end); | ||
this.pos = end; | ||
return buf; | ||
} | ||
[Symbol.iterator]() { | ||
return { | ||
[Symbol.iterator]() { | ||
return this; | ||
}, | ||
next: () => ({ value: this.next(), done: false }), | ||
}; | ||
} | ||
} | ||
const cryptoPrefetcher = (bufSize, valueSize) => | ||
new CryptoRandomPrefetcher(bufSize, valueSize); | ||
const random = (min, max) => { | ||
@@ -50,8 +15,21 @@ if (max === undefined) { | ||
const randPrefetcher = cryptoPrefetcher(4096, 4); | ||
const UINT32_MAX = 0xffffffff; | ||
const BUF_LEN = 1024; | ||
const BUF_SIZE = BUF_LEN * Uint32Array.BYTES_PER_ELEMENT; | ||
const cryptoRandom = () => | ||
randPrefetcher.next().readUInt32LE(0, true) / (UINT32_MAX + 1); | ||
const randomPrefetcher = { | ||
buf: crypto.randomBytes(BUF_SIZE), | ||
pos: 0, | ||
}; | ||
const cryptoRandom = () => { | ||
if (randomPrefetcher.pos === randomPrefetcher.buf.length) { | ||
randomPrefetcher.pos = 0; | ||
crypto.randomFillSync(randomPrefetcher.buf); | ||
} | ||
const val = randomPrefetcher.buf.readUInt32LE(randomPrefetcher.pos); | ||
randomPrefetcher.pos += Uint32Array.BYTES_PER_ELEMENT; | ||
return val / (UINT32_MAX + 1); | ||
}; | ||
const sample = (arr) => { | ||
@@ -58,0 +36,0 @@ const index = Math.floor(Math.random() * arr.length); |
{ | ||
"name": "metautil", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"author": "Timur Shemsedinov <timur.shemsedinov@gmail.com>", | ||
@@ -17,3 +17,2 @@ "license": "MIT", | ||
}, | ||
"homepage": "https://github.com/metarhia/metautil#readme", | ||
"repository": { | ||
@@ -24,4 +23,10 @@ "type": "git", | ||
"bugs": { | ||
"url": "https://github.com/metarhia/metautil/issues" | ||
"url": "https://github.com/metarhia/metautil/issues", | ||
"email": "timur.shemsedinov@gmail.com" | ||
}, | ||
"homepage": "https://metarhia.com", | ||
"funding": { | ||
"type": "patreon", | ||
"url": "https://www.patreon.com/tshemsedinov" | ||
}, | ||
"scripts": { | ||
@@ -33,3 +38,3 @@ "lint": "eslint . && prettier --check \"**/*.js\" \"**/*.json\" \"**/*.md\" \"**/.*rc\"", | ||
"devDependencies": { | ||
"eslint": "^7.15.0", | ||
"eslint": "^7.18.0", | ||
"eslint-config-metarhia": "^7.0.1", | ||
@@ -36,0 +41,0 @@ "eslint-config-prettier": "^7.0.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7611
145