mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
fix(utils): replace getRandomValues with crypto module (#6788)
This commit is contained in:
+5
-2
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
import bind from './helpers/bind.js';
|
import bind from './helpers/bind.js';
|
||||||
|
import crypto from 'crypto';
|
||||||
|
|
||||||
// utils is a library of generic helper functions non-specific to axios
|
// utils is a library of generic helper functions non-specific to axios
|
||||||
|
|
||||||
@@ -615,8 +616,10 @@ const ALPHABET = {
|
|||||||
const generateString = (size = 16, alphabet = ALPHABET.ALPHA_DIGIT) => {
|
const generateString = (size = 16, alphabet = ALPHABET.ALPHA_DIGIT) => {
|
||||||
let str = '';
|
let str = '';
|
||||||
const {length} = alphabet;
|
const {length} = alphabet;
|
||||||
while (size--) {
|
const randomValues = new Uint32Array(size);
|
||||||
str += alphabet[Math.random() * length|0]
|
crypto.randomFillSync(randomValues);
|
||||||
|
for (let i = 0; i < size; i++) {
|
||||||
|
str += alphabet[randomValues[i] % length];
|
||||||
}
|
}
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
|
|||||||
@@ -80,4 +80,24 @@ describe('utils', function (){
|
|||||||
assert.strictEqual(JSON.stringify(jsonObject), JSON.stringify({x: 1, y:2, obj: {ok: 1}}))
|
assert.strictEqual(JSON.stringify(jsonObject), JSON.stringify({x: 1, y:2, obj: {ok: 1}}))
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('generateString', function () {
|
||||||
|
it('should generate a string of the specified length using the default alphabet', function () {
|
||||||
|
const size = 10;
|
||||||
|
const str = utils.generateString(size);
|
||||||
|
|
||||||
|
assert.strictEqual(str.length, size);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should generate a string using only characters from the default alphabet', function () {
|
||||||
|
const size = 10;
|
||||||
|
const alphabet = utils.ALPHABET.ALPHA_DIGIT;
|
||||||
|
|
||||||
|
const str = utils.generateString(size, alphabet);
|
||||||
|
|
||||||
|
for (let char of str) {
|
||||||
|
assert.ok(alphabet.includes(char), `Character ${char} is not in the alphabet`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user