Remove private random.async API and clean up tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent 22542e2 · Browse files · 5 files·+50 added−131 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+50 added−131 removed
- tests:+30 added−120 removed
- other:+20 added−11 removed
Provenance
- Commit
3283318- Tree
b0f9de4- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
22542e2
5 files changed+50 added−131 removed
modified:test/async.test.js+20 added-16 removed
9 unmodified lines1011121314151617181920211617181922232425266 unmodified lines333435363738394041424344454647484938 unmodified lines88899091809293828384858694959697986 unmodified lines1051061071081011091101031041051061071111121131141152 unmodified lines9 unmodified lines } return array.map(callback)}
function mock (callback) { crypto.randomFill = callback jest.resetModules() async = require('../async')}
var originFill = crypto.randomFillafterEach(function () { crypto.randomFill = originFill
jest.resetModules() async = require('../async') // eslint-disable-line global-require mock(originFill)})
it('generates URL-friendly IDs', function () { return Promise.all(times(100, function () {6 unmodified lines }) }))})
it('supports old Node.js', function () { mock(undefined) return Promise.all(times(100, function () { return async().then(function (id) { expect(id).toHaveLength(21) }) }))})
it('changes ID length', function () { return async(10).then(function (id) { expect(id).toHaveLength(10) })38 unmodified lines})
it('rejects Promise on error', function () { var error = new Error('test') crypto.randomFill = function (buffer, callback) { mock(function (buffer, callback) { callback(error) }
jest.resetModules() async = require('../async') // eslint-disable-line global-require
}) return async().catch(function (e) { expect(e).toBe(error) })})6 unmodified lines})
it('sends error to callback API', function (done) { var error = new Error('test') crypto.randomFill = function (buf, callback) { mock(function (buffer, callback) { callback(error) }
jest.resetModules() async = require('../async') // eslint-disable-line global-require
}) async(10, function (err, id) { expect(err).toBe(error) expect(id).toBeUndefined() done()2 unmodified linesmodified:test/random.test.js+10 added-104 removed
1 var crypto = require('crypto')2 var random = require('../random')3 4-var originFill = crypto.randomFill5-var originFillSync = crypto.randomFillSync4+function mock (callback) {5+ crypto.randomFillSync = callback6+ jest.resetModules()7+ random = require('../random')8+}9 10+var originFillSync = crypto.randomFillSync11 afterEach(function () {8- crypto.randomFill = originFill9- crypto.randomFillSync = originFillSync10-11- jest.resetModules()12- random = require('../random') // eslint-disable-line global-require12+ mock(originFillSync)13 })14 15-it('generates random buffers (slow & sync)', function () {16- delete crypto.randomFillSync17-18- jest.resetModules()19- random = require('../random') // eslint-disable-line global-require20-15+it('generates random buffers', function () {16 var numbers = { }17 var bytes = random(10000)18 expect(bytes).toHaveLength(10000)19 for (var i = 0; i < bytes.length; i++) {4 unmodified lines24 expect(bytes[i]).toBeGreaterThanOrEqual(0)25 }26 })27 33-it('generates random buffers (slow & async)', function (done) {34- delete crypto.randomFill28+it('supports old Node.js', function () {29+ mock(undefined)30 36- jest.resetModules()37- random = require('../random') // eslint-disable-line global-require38-31 var numbers = { }40-41- random.async(10000, function (error, bytes) {42- expect(bytes).toHaveLength(10000)43- for (var i = 0; i < bytes.length; i++) {44- if (!numbers[bytes[i]]) numbers[bytes[i]] = 045- numbers[bytes[i]] += 146- expect(typeof bytes[i]).toEqual('number')47- expect(bytes[i]).toBeLessThanOrEqual(255)48- expect(bytes[i]).toBeGreaterThanOrEqual(0)49- done()50- }51- })52-})53-54-it('generates random buffers (fast & sync)', function () {55- crypto.randomFill = function (buffer) {56- var data = crypto.randomBytes(buffer.length)57- data.copy(buffer)58- return buffer59- }60-61- jest.resetModules()62- random = require('../random') // eslint-disable-line global-require63-64- var numbers = { }32 var bytes = random(10000)33 expect(bytes).toHaveLength(10000)34 for (var i = 0; i < bytes.length; i++) {35 if (!numbers[bytes[i]]) numbers[bytes[i]] = 02 unmodified lines38 expect(bytes[i]).toBeLessThanOrEqual(255)39 expect(bytes[i]).toBeGreaterThanOrEqual(0)40 }41 })75-76-it('generates random buffers (fast & async)', function (done) {77- crypto.randomFill = function (buffer, callback) {78- return crypto.randomBytes(buffer.length, function (err, data) {79- if (err) {80- return callback(err)81- }82-83- data.copy(buffer)84- callback(null, buffer)85- })86- }87-88- jest.resetModules()89- random = require('../random') // eslint-disable-line global-require90-91- var numbers = { }92-93- random.async(10000, function (error, bytes) {94- expect(bytes).toHaveLength(10000)95- for (var i = 0; i < bytes.length; i++) {96- if (!numbers[bytes[i]]) numbers[bytes[i]] = 097- numbers[bytes[i]] += 198- expect(typeof bytes[i]).toEqual('number')99- expect(bytes[i]).toBeLessThanOrEqual(255)100- expect(bytes[i]).toBeGreaterThanOrEqual(0)101- done()102- }103- })104-})105-106-it('passes error from crypto module (sync)', function () {107- var error = new Error('test')108- crypto.randomFillSync = function () {109- throw error110- }111-112- jest.resetModules()113- random = require('../random') // eslint-disable-line global-require114-115- try {116- random(10000)117- } catch (e) {118- expect(e).toBe(error)119- }120-})121-122-it('passes error from crypto module (async)', function (done) {123- var error = new Error('test')124- crypto.randomFill = function (buffer, callback) {125- callback(error)126- }127-128- jest.resetModules()129- random = require('../random') // eslint-disable-line global-require130-131- return random.async(10000, function (e) {132- expect(e).toBe(error)133- done()134- })135-})modified:async.js+11 added-2 removed
12234567891011121314151627 unmodified lines444546473948495051528 unmodified linesvar crypto = require('crypto')var url = require('./url')var random = require('./random')
var randomif (crypto.randomFill) { random = function (bytes, callback) { return crypto.randomFill(Buffer.allocUnsafe(bytes), callback) }} else { random = crypto.randomBytes}
/** * Generate secure URL-friendly unique ID. Non-blocking version. * * By default, ID will have 21 symbols to have a collision probability similar27 unmodified lines }) }) }
random.async(size, function (error, bytes) { random(size, function (error, bytes) { if (error) { return callback(error) }
8 unmodified linesmodified:package.json+9 added-1 removed
76 unmodified lines77 ],78 "eslintConfig": {79 "extends": "eslint-config-logux/browser",80 "rules": {81- "handle-callback-err": "off",81 "consistent-return": "off",82 "yoda": "off"83+ },84+ "overrides": {85+ "files": [86+ "test/*.js"87+ ],88+ "rules": {89+ "handle-callback-err": "off",90+ "global-require": "off"91+ }92 }93 },94 "lint-staged": {95 "*.md": "yaspeller-ci",65 unmodified linesmodified:random.js-8 removed
5 unmodified lines678910111213141516175 unmodified lines }} else { module.exports = crypto.randomBytes}
if (crypto.randomFill) { module.exports.async = function (bytes, callback) { return crypto.randomFill(Buffer.allocUnsafe(bytes), callback) }} else { module.exports.async = crypto.randomBytes}