replaced node crypto with web crypto

This commit is contained in:
McToel 2025-02-13 16:01:29 +01:00
parent f7909aac29
commit 46e21f8d75

View file

@ -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);