diff --git a/lib/request.js b/lib/request.js index 1f0a5db7..120ee380 100644 --- a/lib/request.js +++ b/lib/request.js @@ -2,7 +2,6 @@ // import {isIP} from 'net'; // import {Agent as HttpsAgent} from 'https'; // import roundRobin from '@derhuerst/round-robin-scheduler'; -import {randomBytes} from 'crypto'; import {stringify} from 'qs'; import {Request, fetch} from 'cross-fetch'; import {parse as parseContentType} from 'content-type'; @@ -58,6 +57,14 @@ import {HafasError} from './errors.js'; // return ua; // }; +const randomBytesHex = (nBytes = 8) => { + const array = new Uint8Array(nBytes); + crypto.getRandomValues(array); + return Array.from(array) + .map((byte) => byte.toString(16).padStart(2, '0')) + .join(''); + }; + const checkIfResponseIsOk = (_) => { const { body, @@ -122,8 +129,7 @@ const request = async (ctx, userAgent, reqData) => { if (reqOptions.query) { url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true}); } - const reqId = randomBytes(3) - .toString('hex'); + const reqId = randomBytesHex(3); const fetchReq = new Request(url, reqOptions); profile.logRequest(ctx, fetchReq, reqId);