ya parece que cachea, pero hay un problema, el blob es accesible y no esta cifrado,…
- User
ya parece que cachea, pero hay un problema, el blob es accesible y no esta cifrado, antes lo estaba y cuando lo abría en la pestaña de red del navegador solo aparecían caracteres aleatoreos, pero no recuerdo que habia hecho
async function loadAndPlayAudioStream(retryCount = 0) { try { window.audioLoading = true; const finalAudioUrl = buildAudioUrl(audioUrl, audioSettings.nonce); const cache = await caches.open(CACHE_NAME); let response = await cache.match(finalAudioUrl);
if (!response) { // Si no está en caché, hacer la petición response = await fetch(finalAudioUrl, { method: 'GET', credentials: 'same-origin', headers: { 'X-WP-Nonce': audioSettings.nonce, 'X-Requested-With': 'XMLHttpRequest', 'Accept': 'audio/mpeg', 'Range': 'bytes=0-', 'Cache-Control': 'max-age=86400' } }); // Guardar en caché if (response.ok) { await cache.put(finalAudioUrl, response.clone()); } } if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const reader = response.body.getReader(); const iv = response.headers.get('X-Encryption-IV'); if (!iv) { throw new Error('No se recibió el IV en los encabezados de la respuesta'); } const ivArray = Uint8Array.from(atob(iv), c => c.charCodeAt(0)); let currentIV = ivArray; const contentLengthHeader = response.headers.get('Content-Length'); const contentLength = contentLengthHeader ? parseInt(contentLengthHeader) : -1; console.log(`Content-Length obtenido: ${contentLength}`); let buffer = new Uint8Array(0); let receivedLength = 0; // **Agrega esta línea para declarar decryptedChunks** let decryptedChunks = []; while (true) { const { done, value } = await reader.read(); console.log('Chunk leído:', { done, value }); if (done) { console.log('Transmisión completa'); break; } if (value && value.length > 0) { // Añadir los nuevos datos al buffer let tmp = new Uint8Array(buffer.length + value.length); tmp.set(buffer, 0); tmp.set(value, buffer.length); buffer = tmp; receivedLength += value.length; // Procesar el buffer let offset = 0; while (buffer.length - offset >= 4) { // Leer el prefijo de longitud const lengthView = new DataView(buffer.buffer, buffer.byteOffset + offset, 4); const chunkLength = lengthView.getUint32(0, false); // Big-endian // Verificar si tenemos suficientes datos para procesar el chunk if (buffer.length - offset >= 4 + chunkLength) { // Extraer los datos encriptados const encryptedData = buffer.subarray(offset + 4, offset + 4 + chunkLength); console.log('Procesando chunk:', { totalLength: buffer.byteLength, chunkLength: chunkLength, dataLength: encryptedData.byteLength }); try { // Desencriptar el chunk const decryptedData = await decryptAudioData(encryptedData, currentIV, audioSettings.key); decryptedChunks.push(new Uint8Array(decryptedData)); // Actualizar el IV (últimos 16 bytes de encryptedData) if (encryptedData.byteLength >= 16) { currentIV = encryptedData.subarray(encryptedData.byteLength - 16); } else { throw new Error('encryptedData es demasiado pequeño para obtener el IV'); } } catch (error) { console.error('Error desencriptando chunk:', error); throw error; } // Mover el offset más allá de este chunk offset += 4 + chunkLength; } else { // No tenemos suficientes datos aún break; } } // Eliminar los datos procesados del buffer if (offset > 0) { buffer = buffer.subarray(offset); } console.log(`Procesado ${receivedLength} bytes`); } } const audioBuffer = concatenateUint8Arrays(decryptedChunks); const audioBlob = new Blob([audioBuffer], { type: 'audio/mpeg' }); const blobUrl = URL.createObjectURL(audioBlob); await validateAudio(blobUrl); const wavesurfer = initWavesurfer(container); window.wavesurfers[postId] = wavesurfer; await new Promise(resolve => { wavesurfer.once('ready', resolve); wavesurfer.load(blobUrl); }); handleWaveSurferEvents(wavesurfer, container, postId, blobUrl); } catch (error) { console.error('Error en loadAndPlayAudioStream:', error); handleLoadError(error, retryCount, container); } - Assistant