|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (C) 2023 CoCreate and Contributors. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as published |
| 6 | + * by the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + ********************************************************************************/ |
| 17 | + |
| 18 | +/** |
| 19 | + * Commercial Licensing Information: |
| 20 | + * For commercial use of this software without the copyleft provisions of the AGPLv3, |
| 21 | + * you must obtain a commercial license from CoCreate LLC. |
| 22 | + * For details, visit <https://cocreate.app/licenses/> or contact us at [email protected]. |
| 23 | + */ |
| 24 | + |
| 25 | +const cacheName = "dynamic-v2"; |
| 26 | +let organization_id = "" |
| 27 | +let storage = true |
| 28 | +let returnedFromCache = {}; |
| 29 | + |
| 30 | +const queryString = self.location.search; |
| 31 | +const queryParams = new URLSearchParams(queryString); |
| 32 | +let cacheType = queryParams.get('cache'); |
| 33 | + |
| 34 | +self.addEventListener("install", (e) => { |
| 35 | + console.log('Service Worker Installing') |
| 36 | + self.skipWaiting(); |
| 37 | +}); |
| 38 | + |
| 39 | +self.addEventListener("activate", async (e) => { |
| 40 | + e.waitUntil(clients.claim()); |
| 41 | +}); |
| 42 | + |
| 43 | +self.addEventListener("fetch", async (e) => { |
| 44 | + if (!(e.request.url.indexOf('http') === 0) || e.request.method === 'POST') return; |
| 45 | + |
| 46 | + e.respondWith( |
| 47 | + caches |
| 48 | + .match(e.request) |
| 49 | + .then(async (cacheResponse) => { |
| 50 | + if (!navigator.onLine || !!cacheResponse && cacheType !== 'false') { |
| 51 | + const organization = cacheResponse.headers.get('organization') |
| 52 | + const lastModified = cacheResponse.headers.get('last-modified') |
| 53 | + |
| 54 | + returnedFromCache[e.request.url] = { organization, lastModified } |
| 55 | + return cacheResponse; |
| 56 | + } else { |
| 57 | + const networkResponse = await fetch(e.request); |
| 58 | + |
| 59 | + if (!organization_id) |
| 60 | + organization_id = networkResponse.headers.get('organization') |
| 61 | + |
| 62 | + let storageHeader = networkResponse.headers.get('storage') |
| 63 | + if (storageHeader) |
| 64 | + storage = storageHeader |
| 65 | + |
| 66 | + if (cacheType && cacheType !== 'false') { |
| 67 | + console.log('caching') |
| 68 | + |
| 69 | + caches.open(cacheName).then((cache) => { |
| 70 | + if (networkResponse.status !== 206 && networkResponse.status !== 502) { |
| 71 | + const networkModified = networkResponse.headers.get('last-modified'); |
| 72 | + // if (!networkModified) { |
| 73 | + // networkResponse.headers.set('Last-Modified', new Date().toISOString()); |
| 74 | + // } |
| 75 | + cache.put(e.request, networkResponse); |
| 76 | + if (cacheType === 'reload' || cacheType === 'prompt') { |
| 77 | + const cacheModified = cacheResponse.headers.get('last-modified'); |
| 78 | + if (networkModified !== cacheModified) { |
| 79 | + self.clients.matchAll().then((clients) => { |
| 80 | + clients.forEach((client) => { |
| 81 | + client.postMessage({ action: 'cacheType', cacheType }); // Send a custom message |
| 82 | + console.log(`file ${cacheType} has been triggered`) |
| 83 | + }); |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + }).catch(() => { |
| 89 | + |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + if (!cacheResponse || cacheType === 'false' || cacheType === 'offline') { |
| 94 | + return networkResponse.clone(); |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + if (!!cacheResponse && cacheType !== 'false' && cacheType !== 'offline') { |
| 100 | + return cacheResponse; |
| 101 | + } |
| 102 | + |
| 103 | + }) |
| 104 | + .catch(function () { |
| 105 | + return caches.match('./offline.html'); |
| 106 | + }) |
| 107 | + ); |
| 108 | +}); |
| 109 | + |
| 110 | +self.addEventListener('message', function (event) { |
| 111 | + if (event.data.action === 'getOrganization') |
| 112 | + event.source.postMessage({ action: 'getOrganization', organization_id }); |
| 113 | + else if (event.data.action === 'checkCache') { |
| 114 | + event.source.postMessage({ action: 'checkCache', returnedFromCache: { ...returnedFromCache } }); |
| 115 | + returnedFromCache = {} |
| 116 | + } |
| 117 | +}); |
| 118 | + |
| 119 | + |
| 120 | +self.addEventListener('push', (event) => { |
| 121 | + const pushData = event.data.json(); // Assuming the push payload is JSON data |
| 122 | + |
| 123 | + // Process the push notification data as needed |
| 124 | + const { socketUrl, _id } = pushData; |
| 125 | + |
| 126 | + // Establish a WebSocket connection |
| 127 | + const socket = new WebSocket(socketUrl); |
| 128 | + |
| 129 | + // Handle WebSocket events |
| 130 | + socket.addEventListener('open', () => { |
| 131 | + // The WebSocket connection is open, send the message to the server |
| 132 | + socket.send(JSON.stringify({ method: 'read.object', array: 'files', object: { _id }, uid: '' })); |
| 133 | + }); |
| 134 | + |
| 135 | + socket.addEventListener('message', (event) => { |
| 136 | + // Handle messages received from the server over the WebSocket |
| 137 | + const serverData = JSON.parse(event.data); |
| 138 | + if (serverData.uid === uid) { |
| 139 | + // TODO: add file to cache and close socket |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + socket.addEventListener('close', () => { |
| 144 | + // Handle the WebSocket connection being closed |
| 145 | + console.log('WebSocket connection closed.'); |
| 146 | + }); |
| 147 | + |
| 148 | + socket.addEventListener('error', (error) => { |
| 149 | + // Handle WebSocket errors |
| 150 | + console.error('WebSocket error:', error); |
| 151 | + }); |
| 152 | + |
| 153 | + // Ensure that the service worker stays active until the WebSocket connection is closed |
| 154 | + event.waitUntil( |
| 155 | + new Promise((resolve) => { |
| 156 | + socket.addEventListener('close', () => { |
| 157 | + resolve(); |
| 158 | + }); |
| 159 | + }) |
| 160 | + ); |
| 161 | +}); |
| 162 | + |
| 163 | +// self.addEventListener('backgroundfetchsuccess', (event) => { |
| 164 | +// const bgFetch = event.registration; |
| 165 | + |
| 166 | +// event.waitUntil(async function() { |
| 167 | +// // Create/open a cache. |
| 168 | +// const cache = await caches.open(cacheName); |
| 169 | +// // Get all the records. |
| 170 | +// const records = await bgFetch.matchAll(); |
| 171 | +// // Copy each request/response across. |
| 172 | +// const promises = records.map(async (record) => { |
| 173 | +// const response = await record.responseReady; |
| 174 | +// console.log('putting ') |
| 175 | +// await cache.put(record.request, response); |
| 176 | +// }); |
| 177 | + |
| 178 | +// // Wait for the copying to complete |
| 179 | +// await Promise.all(promises); |
| 180 | + |
| 181 | +// // Update the progress notification. |
| 182 | +// // event.updateUI({ title: 'Episode 5 ready to listen!' }); |
| 183 | +// }()); |
| 184 | +// }); |
0 commit comments