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 {isIP} from 'net';
// import {Agent as HttpsAgent} from 'https'; // import {Agent as HttpsAgent} from 'https';
// import roundRobin from '@derhuerst/round-robin-scheduler'; // import roundRobin from '@derhuerst/round-robin-scheduler';
import {randomBytes} from 'crypto';
import {stringify} from 'qs'; import {stringify} from 'qs';
import {Request, fetch} from 'cross-fetch'; import {Request, fetch} from 'cross-fetch';
import {parse as parseContentType} from 'content-type'; import {parse as parseContentType} from 'content-type';
@ -58,6 +57,14 @@ import {HafasError} from './errors.js';
// return ua; // 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 checkIfResponseIsOk = (_) => {
const { const {
body, body,
@ -122,8 +129,7 @@ const request = async (ctx, userAgent, reqData) => {
if (reqOptions.query) { if (reqOptions.query) {
url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true}); url += '?' + stringify(reqOptions.query, {arrayFormat: 'brackets', encodeValuesOnly: true});
} }
const reqId = randomBytes(3) const reqId = randomBytesHex(3);
.toString('hex');
const fetchReq = new Request(url, reqOptions); const fetchReq = new Request(url, reqOptions);
profile.logRequest(ctx, fetchReq, reqId); profile.logRequest(ctx, fetchReq, reqId);