From 131f136b7a25f464334981334daeb12fbee2c597 Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Mon, 25 Aug 2025 22:59:54 +0300 Subject: [PATCH] add some loggining --- src/lib/AudioPlayer.svelte | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/lib/AudioPlayer.svelte b/src/lib/AudioPlayer.svelte index 05bc3fb..2aa3a8b 100644 --- a/src/lib/AudioPlayer.svelte +++ b/src/lib/AudioPlayer.svelte @@ -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) {