kopia lustrzana https://gitlab.com/rysiekpl/libresilient
service-worker.js linted (left only no-unused-vars errors)
rodzic
cf56570dbc
commit
f8aa190558
|
@ -113,9 +113,9 @@ try {
|
|||
// load a plugin
|
||||
self.importScripts(`./plugins/${plugins[i]}.js`)
|
||||
// check if it loaded properly
|
||||
var plugin = LibResilientPlugins.find(p=>p.name===plugins[i])
|
||||
var plugin = self.LibResilientPlugins.find(p=>p.name===plugins[i])
|
||||
if (plugin === undefined) {
|
||||
throw new Error(`Plugin not found: ${plugins[i]} (available plugins: ${LibResilientPlugins.map(p=>p.name).join(', ')})`)
|
||||
throw new Error(`Plugin not found: ${plugins[i]} (available plugins: ${self.LibResilientPlugins.map(p=>p.name).join(', ')})`)
|
||||
}
|
||||
// make sure that the indirect flag is set if needed
|
||||
if (self.LibResilientConfig.plugins[plugin.name].indirect===true) {
|
||||
|
@ -127,7 +127,7 @@ try {
|
|||
// make sure plugins used by the just-loaded plugin are also added to the list
|
||||
// but don't load a plugin twice
|
||||
if (typeof plugin.uses !== "undefined") {
|
||||
for (p in plugin.uses) {
|
||||
for (var p in plugin.uses) {
|
||||
if (plugins.indexOf(p) < 0) {
|
||||
// okay, this plugin has not been added to the plugins list yet
|
||||
// let's do that
|
||||
|
@ -145,7 +145,7 @@ try {
|
|||
}
|
||||
|
||||
// inform
|
||||
self.log('service-worker', `DEBUG: Strategy in use: ${LibResilientPlugins.filter(p=>(!p.indirect)).map(p=>p.name).join(', ')}`)
|
||||
self.log('service-worker', `DEBUG: Strategy in use: ${self.LibResilientPlugins.filter(p=>(!p.indirect)).map(p=>p.name).join(', ')}`)
|
||||
|
||||
} catch(e) {
|
||||
// we only get a cryptic "Error while registering a service worker"
|
||||
|
@ -176,7 +176,7 @@ let decrementActiveFetches = (clientId) => {
|
|||
// 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
|
||||
// got fetched) or not
|
||||
clients.get(clientId).then((client)=>{
|
||||
self.clients.get(clientId).then((client)=>{
|
||||
client.postMessage({
|
||||
allFetched: true
|
||||
})
|
||||
|
@ -227,31 +227,32 @@ let promiseTimeout = (time, timeout_resolves=false, error_message=false) => {
|
|||
* service worker restarts, if that's required
|
||||
*/
|
||||
let LibResilientResourceInfo = class {
|
||||
|
||||
// actual values of the fields
|
||||
// only used internally, and stored into the Indexed DB
|
||||
values = {
|
||||
url: '', // read only after initialization
|
||||
clientId: null,
|
||||
fetchError: null,
|
||||
method: null,
|
||||
state: null, // can be "error", "success", "running"
|
||||
serviceWorker: 'COMMIT_UNKNOWN' // this will be replaced by commit sha in CI/CD; read-only
|
||||
}
|
||||
client = null;
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* needed to set the URL and clientId
|
||||
*/
|
||||
constructor(url, clientId) {
|
||||
|
||||
// actual values of the fields
|
||||
// only used internally, and stored into the Indexed DB
|
||||
this.values = {
|
||||
url: '', // read only after initialization
|
||||
clientId: null,
|
||||
fetchError: null,
|
||||
method: null,
|
||||
state: null, // can be "error", "success", "running"
|
||||
serviceWorker: 'COMMIT_UNKNOWN' // this will be replaced by commit sha in CI/CD; read-only
|
||||
}
|
||||
this.client = null;
|
||||
|
||||
// set it
|
||||
this.values.url = url
|
||||
this.values.clientId = clientId
|
||||
// we might not have a non-empty clientId if it's a cross-origin fetch
|
||||
if (clientId) {
|
||||
// get the client from Client API based on clientId
|
||||
clients.get(clientId).then((client)=>{
|
||||
self.clients.get(clientId).then((client)=>{
|
||||
// set the client
|
||||
this.client = client
|
||||
// Send a message to the client.
|
||||
|
@ -378,11 +379,11 @@ let libresilientFetch = (plugin, url, reqInfo) => {
|
|||
*/
|
||||
let callOnLibResilientPlugin = (call, args) => {
|
||||
// find the first plugin implementing the method
|
||||
for (i=0; i<LibResilientPlugins.length; i++) {
|
||||
if (typeof LibResilientPlugins[i][call] === 'function') {
|
||||
self.log('service-worker', 'Calling plugin ' + LibResilientPlugins[i].name + '.' + call + '()')
|
||||
for (i=0; i<self.LibResilientPlugins.length; i++) {
|
||||
if (typeof self.LibResilientPlugins[i][call] === 'function') {
|
||||
self.log('service-worker', 'Calling plugin ' + self.LibResilientPlugins[i].name + '.' + call + '()')
|
||||
// call it
|
||||
return LibResilientPlugins[i][call].apply(null, args)
|
||||
return self.LibResilientPlugins[i][call].apply(null, args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -410,7 +411,7 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas
|
|||
self.activeFetches[clientId]++
|
||||
|
||||
// filter out stash plugins if need be
|
||||
var LibResilientPluginsRun = LibResilientPlugins.filter((plugin)=>{
|
||||
var LibResilientPluginsRun = self.LibResilientPlugins.filter((plugin)=>{
|
||||
return ( (!plugin.indirect) && (useStashed || typeof plugin.stash !== 'function') )
|
||||
})
|
||||
|
||||
|
@ -459,7 +460,7 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas
|
|||
reqInfo.update({state:"success"})
|
||||
|
||||
// get the plugin that was used to fetch content
|
||||
plugin = LibResilientPlugins.find(p=>p.name===reqInfo.method)
|
||||
plugin = self.LibResilientPlugins.find(p=>p.name===reqInfo.method)
|
||||
|
||||
// if it's a stashing plugin...
|
||||
if (typeof plugin.stash === 'function') {
|
||||
|
@ -495,7 +496,7 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas
|
|||
|| ( stashedResponse.headers.get('X-LibResilient-ETag') !== response.headers.get('X-LibResilient-ETag') ) ) {
|
||||
// inform!
|
||||
self.log('service-worker', 'fetched version method or ETag differs from stashed for:', url)
|
||||
clients.get(reqInfo.clientId).then((client)=>{
|
||||
self.clients.get(reqInfo.clientId).then((client)=>{
|
||||
client.postMessage({
|
||||
url: url,
|
||||
fetchedDiffers: true
|
||||
|
@ -507,8 +508,8 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas
|
|||
// do we want to stash?
|
||||
if (doStash) {
|
||||
// find the first stashing plugin
|
||||
for (i=0; i<LibResilientPlugins.length; i++) {
|
||||
if (typeof LibResilientPlugins[i].stash === 'function') {
|
||||
for (i=0; i<self.LibResilientPlugins.length; i++) {
|
||||
if (typeof self.LibResilientPlugins[i].stash === 'function') {
|
||||
|
||||
// ok, now we're in business
|
||||
var hdrs = '\n+-- headers:'
|
||||
|
@ -519,13 +520,13 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas
|
|||
'service-worker',
|
||||
`stashing a successful fetch of: ${url}`,
|
||||
`\n+-- fetched using : ${response.headers.get('X-LibResilient-Method')}`,
|
||||
`\n+-- stashing using : ${LibResilientPlugins[i].name}`,
|
||||
`\n+-- stashing using : ${self.LibResilientPlugins[i].name}`,
|
||||
hdrs
|
||||
)
|
||||
|
||||
// working on clone()'ed response so that the original one is not touched
|
||||
// TODO: should a failed stashing break the flow here? probably not!
|
||||
return LibResilientPlugins[i].stash(response.clone(), url)
|
||||
return self.LibResilientPlugins[i].stash(response.clone(), url)
|
||||
.then((res)=>{
|
||||
// original response will be needed further down
|
||||
return response
|
||||
|
@ -591,12 +592,12 @@ self.addEventListener('fetch', event => {
|
|||
// so let's also send the plugin list, why not
|
||||
//
|
||||
// *sigh* JS is great *sigh*
|
||||
clients
|
||||
self.clients
|
||||
.get(clientId)
|
||||
.then((client)=>{
|
||||
client.postMessage({
|
||||
clientId: clientId,
|
||||
plugins: LibResilientPlugins.filter(p=>(!p.indirect)).map((p)=>{return p.name}),
|
||||
plugins: self.LibResilientPlugins.filter(p=>(!p.indirect)).map((p)=>{return p.name}),
|
||||
serviceWorker: 'COMMIT_UNKNOWN'
|
||||
})
|
||||
})
|
||||
|
|
Ładowanie…
Reference in New Issue