add some loggining
This commit is contained in:
parent
7307f008c7
commit
131f136b7a
@ -17,30 +17,38 @@
|
|||||||
let tracks = [];
|
let tracks = [];
|
||||||
let currentTrackIndex = 0;
|
let currentTrackIndex = 0;
|
||||||
let currentTrack = null;
|
let currentTrack = null;
|
||||||
|
let randomType = 'unknown';
|
||||||
|
|
||||||
function getSecureRandomIntExclusive(max) {
|
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);
|
const cryptoObj = typeof window !== 'undefined' && (window.crypto || window.msCrypto);
|
||||||
if (cryptoObj && cryptoObj.getRandomValues) {
|
if (cryptoObj && cryptoObj.getRandomValues) {
|
||||||
|
console.log('Using cryptographically secure randomness');
|
||||||
const maxUint32 = 4294967296;
|
const maxUint32 = 4294967296;
|
||||||
const threshold = maxUint32 - (maxUint32 % max);
|
const threshold = maxUint32 - (maxUint32 % max);
|
||||||
const buf = new Uint32Array(1);
|
const buf = new Uint32Array(1);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
cryptoObj.getRandomValues(buf);
|
cryptoObj.getRandomValues(buf);
|
||||||
const r = buf[0];
|
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) {
|
function shuffleInPlace(arr) {
|
||||||
|
console.log('Starting track shuffling...');
|
||||||
for (let i = arr.length - 1; i > 0; i--) {
|
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];
|
const tmp = arr[i];
|
||||||
arr[i] = arr[j];
|
arr[i] = arr[j];
|
||||||
arr[j] = tmp;
|
arr[j] = tmp;
|
||||||
}
|
}
|
||||||
|
console.log('Tracks shuffled:', arr);
|
||||||
|
console.log(`Type of randomness used: ${randomType === 'crypto' ? 'Cryptographic' : 'Math.random()'}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
@ -94,13 +102,24 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadingProgress = 90;
|
loadingProgress = 90;
|
||||||
currentTrackIndex = getSecureRandomIntExclusive(tracks.length);
|
const result = getSecureRandomIntExclusive(tracks.length);
|
||||||
|
currentTrackIndex = result.value;
|
||||||
|
randomType = result.type;
|
||||||
currentTrack = tracks[currentTrackIndex];
|
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;
|
loadingProgress = 100;
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
loading = false;
|
loading = false;
|
||||||
clearTimeout(timeoutId);
|
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);
|
}, 200);
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user