add some loggining

This commit is contained in:
Lain Iwakura 2025-08-25 22:59:54 +03:00
parent 7307f008c7
commit 131f136b7a
No known key found for this signature in database
GPG Key ID: C7C18257F2ADC6F8

View File

@ -17,30 +17,38 @@
let tracks = [];
let currentTrackIndex = 0;
let currentTrack = null;
let randomType = 'unknown';
function getSecureRandomIntExclusive(max) {
if (!max || max <= 0) return 0;
if (!max || max <= 0) return { value: 0, type: 'invalid' };
const cryptoObj = typeof window !== 'undefined' && (window.crypto || window.msCrypto);
if (cryptoObj && cryptoObj.getRandomValues) {
console.log('Using cryptographically secure randomness');
const maxUint32 = 4294967296;
const threshold = maxUint32 - (maxUint32 % max);
const buf = new Uint32Array(1);
for (;;) {
cryptoObj.getRandomValues(buf);
const r = buf[0];
if (r < threshold) return r % max;
if (r < threshold) return { value: r % max, type: 'crypto' };
}
}
return Math.floor(Math.random() * max);
console.warn('Fallback to Math.random() - cryptographic randomness unavailable');
return { value: Math.floor(Math.random() * max), type: 'math' };
}
function shuffleInPlace(arr) {
console.log('Starting track shuffling...');
for (let i = arr.length - 1; i > 0; i--) {
const j = getSecureRandomIntExclusive(i + 1);
const result = getSecureRandomIntExclusive(i + 1);
const j = result.value;
randomType = result.type;
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
console.log('Tracks shuffled:', arr);
console.log(`Type of randomness used: ${randomType === 'crypto' ? 'Cryptographic' : 'Math.random()'}`);
}
onMount(async () => {
@ -94,13 +102,24 @@
}
loadingProgress = 90;
currentTrackIndex = getSecureRandomIntExclusive(tracks.length);
const result = getSecureRandomIntExclusive(tracks.length);
currentTrackIndex = result.value;
randomType = result.type;
currentTrack = tracks[currentTrackIndex];
console.log(`Initial track selected: ${currentTrackIndex + 1}/${tracks.length} - "${currentTrack.title}"`);
console.log(`Type of randomness for track selection: ${randomType === 'crypto' ? 'Cryptographic' : 'Math.random()'}`);
loadingProgress = 100;
setTimeout(() => {
loading = false;
clearTimeout(timeoutId);
console.log('Loading complete!');
console.log(`Final randomness statistics:`);
console.log(`Track shuffling: ${randomType === 'crypto' ? 'Cryptographic' : 'Math.random()'}`);
console.log(`Initial track selection: ${randomType === 'crypto' ? 'Cryptographic' : 'Math.random()'}`);
if (randomType !== 'crypto') {
console.warn('WARNING: Fallback to Math.random() was used! Randomness may be predictable.');
}
}, 200);
} catch (err) {