diff --git a/service-worker.js b/service-worker.js index 64d2a4e133eb6dc930703bb97ca047ccadd0c246..cea4d384654ab824a7235464a89d9cc307efc108 100644 --- a/service-worker.js +++ b/service-worker.js @@ -92,7 +92,7 @@ if (typeof self.SamizdatConfig !== 'object' || self.SamizdatConfig === null) { * items - the rest of arguments will be passed to console.debug() */ self.log = function(component, ...items) { - if (component in SamizdatConfig.loggedComponents) { + if (self.SamizdatConfig.loggedComponents.indexOf(component) >= 0) { console.debug(`Samizdat [COMMIT_UNKNOWN, ${component}] ::`, ...items) } } @@ -120,9 +120,9 @@ try { // make sure that the indirect flag is set if needed if (self.SamizdatConfig.plugins[plugin.name].indirect===true) { plugin.indirect=true - console.log(`Loaded plugin: ${plugin.name} (indirect)`) + self.log('service-worker', `Loaded plugin: ${plugin.name} (indirect)`) } else { - console.log(`Loaded plugin: ${plugin.name}`) + self.log('service-worker', `Loaded plugin: ${plugin.name}`) } // make sure plugins used by the just-loaded plugin are also added to the list // but don't load a plugin twice @@ -145,7 +145,7 @@ try { } // inform - console.log(`DEBUG: Strategy in use: ${SamizdatPlugins.filter(p=>(!p.indirect)).map(p=>p.name).join(', ')}`) + self.log('service-worker', `DEBUG: Strategy in use: ${SamizdatPlugins.filter(p=>(!p.indirect)).map(p=>p.name).join(', ')}`) } catch(e) { // we only get a cryptic "Error while registering a service worker" @@ -169,9 +169,9 @@ self.activeFetches = {} let decrementActiveFetches = (clientId) => { // decrement the fetch counter for the client self.activeFetches[clientId]-- - console.log('+-- activeFetches[' + clientId + ']:', self.activeFetches[clientId]) + self.log('service-worker', '+-- activeFetches[' + clientId + ']:', self.activeFetches[clientId]) if (self.activeFetches[clientId] === 0) { - console.log('(COMMIT_UNKNOWN) All fetches done!') + self.log('service-worker', 'All fetches done!') // inform the client // client has to be smart enough to know if that is just temporary // (and new fetches will fire in a moment, because a CSS file just @@ -182,7 +182,7 @@ let decrementActiveFetches = (clientId) => { }) }) .then(()=>{ - console.log('(COMMIT_UNKNOWN) all-fetched message sent.') + self.log('service-worker', 'all-fetched message sent.') }) } } @@ -267,7 +267,7 @@ let SamizdatResourceInfo = class { */ update(data) { // debug - var msg = '(COMMIT_UNKNOWN) Updated SamizdatResourceInfo for: ' + this.values.url + var msg = 'Updated SamizdatResourceInfo for: ' + this.values.url // was there a change? if not, no need to postMessage var changed = false // update the properties that are read-write @@ -284,7 +284,7 @@ let SamizdatResourceInfo = class { } this.values[k] = data[k] }) - console.log(msg) + self.log('service-worker', msg) // send the message to the client if (this.client && changed) { this.client.postMessage(this.values); @@ -355,7 +355,7 @@ let samizdatFetch = (plugin, url, reqInfo) => { }) // log stuff - console.log("(COMMIT_UNKNOWN) Samizdat Service Worker handling URL:", url, + self.log('service-worker', "Samizdat Service Worker handling URL:", url, '\n+-- using method(s):', plugin.name) // race the plugin(s) vs. a timeout @@ -380,7 +380,7 @@ let callOnSamizdatPlugin = (call, args) => { // find the first plugin implementing the method for (i=0; i<SamizdatPlugins.length; i++) { if (typeof SamizdatPlugins[i][call] === 'function') { - console.log('(COMMIT_UNKNOWN) Calling plugin ' + SamizdatPlugins[i].name + '.' + call + '()') + self.log('service-worker', 'Calling plugin ' + SamizdatPlugins[i].name + '.' + call + '()') // call it return SamizdatPlugins[i][call].apply(null, args) } @@ -434,7 +434,7 @@ let getResourceThroughSamizdat = (request, clientId, useStashed=true, doStash=tr .reduce( (prevPromise, currentPlugin)=>{ return prevPromise.catch((error)=>{ - console.log("(COMMIT_UNKNOWN) Samizdat plugin error for:", url, + self.log('service-worker', "Samizdat plugin error for:", url, '\n+-- method : ' + reqInfo.method, '\n+-- error : ' + error.toString()) // save info in reqInfo -- status of the previous method @@ -464,11 +464,11 @@ let getResourceThroughSamizdat = (request, clientId, useStashed=true, doStash=tr // if it's a stashing plugin... if (typeof plugin.stash === 'function') { // we obviously do not want to stash - console.log('(COMMIT_UNKNOWN) Not stashing, since resource is already retrieved by a stashing plugin:', url); + self.log('service-worker', 'Not stashing, since resource is already retrieved by a stashing plugin:', url); // since we got the data from a stashing plugin, // let's run the rest of plugins in the background to check if we can get a fresher resource // and stash it in cache for later use - console.log('(COMMIT_UNKNOWN) starting background no-stashed fetch for:', url); + self.log('service-worker', 'starting background no-stashed fetch for:', url); // event.waitUntil? // https://stackoverflow.com/questions/37902441/what-does-event-waituntil-do-in-service-worker-and-why-is-it-needed/37906330#37906330 getResourceThroughSamizdat(request, clientId, false, true, response.clone()) @@ -483,7 +483,7 @@ let getResourceThroughSamizdat = (request, clientId, useStashed=true, doStash=tr if (stashedResponse && stashedResponse.headers) { // this is where we check if the response from whatever plugin we got it from // is newer than what we've stashed - console.log('(COMMIT_UNKNOWN) checking freshness of stashed version of:', url, + self.log('service-worker', 'checking freshness of stashed version of:', url, '\n+-- stashed from :', stashedResponse.headers.get('X-Samizdat-Method'), '\n+-- fetched using :', response.headers.get('X-Samizdat-Method'), '\n+-- stashed X-Samizdat-ETag :', stashedResponse.headers.get('X-Samizdat-ETag'), @@ -494,7 +494,7 @@ let getResourceThroughSamizdat = (request, clientId, useStashed=true, doStash=tr if ( ( stashedResponse.headers.get('X-Samizdat-Method') !== response.headers.get('X-Samizdat-Method') ) || ( stashedResponse.headers.get('X-Samizdat-ETag') !== response.headers.get('X-Samizdat-ETag') ) ) { // inform! - console.log('(COMMIT_UNKNOWN) fetched version method or ETag differs from stashed for:', url) + self.log('service-worker', 'fetched version method or ETag differs from stashed for:', url) clients.get(reqInfo.clientId).then((client)=>{ client.postMessage({ url: url, @@ -511,11 +511,11 @@ let getResourceThroughSamizdat = (request, clientId, useStashed=true, doStash=tr if (typeof SamizdatPlugins[i].stash === 'function') { // ok, now we're in business - console.log('(COMMIT_UNKNOWN) Stashing a successful fetch of:', url, + self.log('service-worker', 'Stashing a successful fetch of:', url, '\n+-- fetched using :', response.headers.get('X-Samizdat-Method'), '\n+-- stashing using :', SamizdatPlugins[i].name) response.headers.forEach(function(v, k){ - console.log('+-- Stashing header: ', k, ' :: ', v) + self.log('service-worker', '+-- Stashing header: ', k, ' :: ', v) }); // working on clone()'ed response so that the original one is not touched @@ -537,7 +537,7 @@ let getResourceThroughSamizdat = (request, clientId, useStashed=true, doStash=tr }) // a final catch... in case all plugins fail .catch((err)=>{ - console.log("(COMMIT_UNKNOWN) Samizdat also failed completely: ", err, + self.log('service-worker', "Samizdat also failed completely: ", err, '\n+-- URL : ' + url) // cleanup @@ -562,12 +562,12 @@ let getResourceThroughSamizdat = (request, clientId, useStashed=true, doStash=tr self.addEventListener('install', event => { // TODO: Might we want to have a local cache? // "COMMIT_UNKNOWN" will be replaced with commit ID - console.log("0. Installed Samizdat Service Worker (commit: COMMIT_UNKNOWN)."); + self.log('service-worker', "0. Installed Samizdat Service Worker (commit: COMMIT_UNKNOWN)."); // TODO: should we do some plugin initialization here? }); self.addEventListener('activate', event => { - console.log("1. Activated Samizdat Service Worker (commit: COMMIT_UNKNOWN)."); + self.log('service-worker', "1. Activated Samizdat Service Worker (commit: COMMIT_UNKNOWN)."); // TODO: should we do some plugin initialization here? }); @@ -603,7 +603,7 @@ self.addEventListener('fetch', event => { } // info - console.log("(COMMIT_UNKNOWN) Fetching!", + self.log('service-worker', "Fetching!", "\n+-- url :", event.request.url, "\n+-- clientId :", event.clientId, "\n+-- resultingClientId:", event.resultingClientId, @@ -633,11 +633,11 @@ self.addEventListener('fetch', event => { self.addEventListener('message', (event) => { // inform - var msg = '(COMMIT_UNKNOWN) Message received!' + var msg = 'Message received!' Object.keys(event.data).forEach((k)=>{ msg += '\n+-- key: ' + k + " :: val: " + event.data[k] }) - console.log(msg); + self.log('service-worker', msg); /* * supporting stash(), unstash(), and publish() only