Compare commits

..

No commits in common. "aef022801e5f508a4382f87e419e07b661dd3e21" and "3213dc434366ed13fbb3a85bb78cfce04c7df79a" have entirely different histories.

View file

@ -3,23 +3,13 @@ import {Request, fetch} from 'cross-fetch';
import {parse as parseContentType} from 'content-type'; import {parse as parseContentType} from 'content-type';
import {HafasError} from './errors.js'; import {HafasError} from './errors.js';
const randomBytesHexString = length => [...Array(length)].map(() => Math.floor(Math.random() * 16) const randomBytesHex = (nBytes = 8) => {
.toString(16)) const array = new Uint8Array(nBytes);
.join(''); crypto.getRandomValues(array);
return Array.from(array)
const id = randomBytesHexString(6) .map((byte) => byte.toString(16)
.toString('hex'); .padStart(2, '0'))
const randomizeUserAgent = (userAgent) => { .join('');
let ua = userAgent;
for (
let i = Math.round(5 + Math.random() * 5);
i < ua.length;
i += Math.round(5 + Math.random() * 5)
) {
ua = ua.slice(0, i) + id + ua.slice(i);
i += id.length;
}
return ua;
}; };
const checkIfResponseIsOk = (_) => { const checkIfResponseIsOk = (_) => {
@ -75,9 +65,7 @@ const request = async (ctx, userAgent, reqData) => {
'Accept-Encoding': 'gzip, br, deflate', 'Accept-Encoding': 'gzip, br, deflate',
'Accept': 'application/json', 'Accept': 'application/json',
'Accept-Language': opt.language || profile.defaultLanguage || 'en', 'Accept-Language': opt.language || profile.defaultLanguage || 'en',
'user-agent': profile.randomizeUserAgent 'user-agent': userAgent,
? randomizeUserAgent(userAgent)
: userAgent,
...reqData.headers, ...reqData.headers,
}, },
redirect: 'follow', redirect: 'follow',
@ -88,7 +76,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 = randomBytesHexString(6); const reqId = randomBytesHex(3);
const fetchReq = new Request(url, reqOptions); const fetchReq = new Request(url, reqOptions);
profile.logRequest(ctx, fetchReq, reqId); profile.logRequest(ctx, fetchReq, reqId);