An in-browser wisp.place site explorer

sw.js: update for un-base64'd html files

Going forward html files won't be base64 encoded, but this leaves us in a situation where we have to cover both

+28 -27
+28 -27
public/sw.js
··· 266 266 } 267 267 268 268 // Get the raw data 269 - const rawArrayBuffer = await response.arrayBuffer(); 269 + const rawArrayBuffer = await response.arrayBuffer(); 270 + const bytes = new Uint8Array(rawArrayBuffer); 270 271 271 - // Decompress if needed (wisp blobs are gzip + base64 encoded) 272 272 let decompressedData; 273 273 274 - // Check if it's likely base64-encoded data 275 - const textDecoder = new TextDecoder(); 276 - const textContent = textDecoder.decode(rawArrayBuffer); 277 - 278 - // Wisp blobs are stored as base64-encoded gzip data 279 - if (textContent.match(/^[A-Za-z0-9+/]+={0,2}$/) && textContent.length > 50) { 280 - // It's base64 encoded 281 - console.log('[Wisp SW] Detected base64-encoded blob, decompressing...'); 282 - 283 - // Decode base64 284 - const binaryString = atob(textContent); 285 - const bytes = new Uint8Array(binaryString.length); 286 - for (let i = 0; i < binaryString.length; i++) { 287 - bytes[i] = binaryString.charCodeAt(i); 274 + if (bytes[0] === 0x1f && bytes[1] === 0x8b) { 275 + // Raw gzip (new format: gzip without base64 encoding) 276 + try { 277 + decompressedData = await decompressGzip(bytes); 278 + } catch (error) { 279 + console.error('[Wisp SW] Failed to decompress gzip data:', error); 280 + throw new Error(`Failed to decompress blob data for CID ${cid}: ${error.message}`); 288 281 } 282 + } else { 283 + // Check if it's the legacy base64-encoded gzip format 284 + const textContent = new TextDecoder().decode(rawArrayBuffer); 285 + if (textContent.match(/^[A-Za-z0-9+/]+={0,2}$/) && textContent.length > 50) { 286 + const binaryString = atob(textContent); 287 + const b64Bytes = new Uint8Array(binaryString.length); 288 + for (let i = 0; i < binaryString.length; i++) { 289 + b64Bytes[i] = binaryString.charCodeAt(i); 290 + } 289 291 290 - // Check if it's gzip compressed (starts with 0x1f 0x8b) 291 - if (bytes[0] === 0x1f && bytes[1] === 0x8b) { 292 - // Decompress gzip 293 - try { 294 - decompressedData = await decompressGzip(bytes); 295 - } catch (error) { 296 - console.error('[Wisp SW] Failed to decompress gzip data:', error); 297 - throw new Error(`Failed to decompress blob data for CID ${cid}: ${error.message}`); 292 + if (b64Bytes[0] === 0x1f && b64Bytes[1] === 0x8b) { 293 + try { 294 + decompressedData = await decompressGzip(b64Bytes); 295 + } catch (error) { 296 + console.error('[Wisp SW] Failed to decompress gzip data:', error); 297 + throw new Error(`Failed to decompress blob data for CID ${cid}: ${error.message}`); 298 + } 299 + } else { 300 + decompressedData = b64Bytes; 298 301 } 299 302 } else { 303 + // Plain uncompressed data 300 304 decompressedData = bytes; 301 305 } 302 - } else { 303 - // Not base64, use as-is 304 - decompressedData = new Uint8Array(rawArrayBuffer); 305 306 } 306 307 307 308 // Create blob from decompressed data