1self.addEventListener('install', () => { 2 self.skipWaiting(); 3}); 4 5self.addEventListener('activate', event => { 6 event.waitUntil(self.clients.claim()); 7}); 8 9 10function postManifestMessage(clientId, request, response) { 11 // deliver the manifest to the client 12 self.clients.get(clientId).then(client => { 13 if (client) { 14 client.postMessage({ 15 type: 'FETCH', 16 payload: { 17 request, 18 response, 19 }, 20 }); 21 } else { 22 console.warn('client not found clientId=' + clientId); 23 } 24 }); 25} 26 27function postSegmentMessage(clientId, segmentUrl) { 28 self.clients.get(clientId).then(client => { 29 if (client) { 30 client.postMessage({ 31 type: 'SEGMENT_URL', 32 payload: segmentUrl, 33 }); 34 } else { 35 console.warn('client not found clientId=' + clientId); 36 } 37 }); 38} 39 40self.addEventListener('fetch', (event) => { 41 // When the user opens the video app in multiple windows or tabs 42 // the same service worker is used by all. We must maintain 43 // seperate data for each of the clients. 44 // After page reload the clientId is not populated, but resultingClientId is 45 const clientId = event.clientId || event.resultingClientId; 46 47 // If it is an HLS manifest file. Our manifests have extension .m3u8, but spec says .m3u is acceptable as well. 48 if (event.request.url.match(/\.m3u/) !== null) { 49 event.respondWith(fetch(event.request.url).then(resp => resp.text().then(txt => { 50 // We need to change the protocol for the licence url for reasons shrouded in mystery 51 const updatedResp = txt.replaceAll('skds://', 'skd://'); 52 const { headers, statusText, status } = resp; 53 const { url, method } = event.request; 54 // Manifests are needed for parsing of DAI metadata 55 postManifestMessage(clientId, { url, method }, updatedResp); 56 57 return new Response(updatedResp, { headers, status, statusText }); 58 }))); 59 } else if (event.request.url.match(/\.ts|\.m4s/) !== null) { 60 postSegmentMessage(clientId, event.request.url); 61 } 62});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.