kopia lustrzana https://gitlab.com/rysiekpl/libresilient
verifying config.json downloaded via other plugins than fetch (ref. #48)
rodzic
d40cf65714
commit
2409e716ef
|
@ -404,7 +404,7 @@ let executeConfig = (pluginsConfig) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// flag signifying the SW has been initialized already
|
// flag signifying if the SW has been initialized already
|
||||||
var initDone = false
|
var initDone = false
|
||||||
|
|
||||||
// load the plugins
|
// load the plugins
|
||||||
|
@ -476,10 +476,15 @@ let initServiceWorker = async () => {
|
||||||
// TODO: handle in a more elegant way
|
// TODO: handle in a more elegant way
|
||||||
let lrpcBackup = new Map(self.LibResilientPluginConstructors)
|
let lrpcBackup = new Map(self.LibResilientPluginConstructors)
|
||||||
|
|
||||||
// working on a copy of the plugins config so that
|
try {
|
||||||
// self.LibResilientConfig.plugins remains unmodified
|
// working on a copy of the plugins config so that
|
||||||
// in case we need it later (for example, when re-loading the config)
|
// self.LibResilientConfig.plugins remains unmodified
|
||||||
executeConfig([...self.LibResilientConfig.plugins])
|
// in case we need it later (for example, when re-loading the config)
|
||||||
|
executeConfig([...self.LibResilientConfig.plugins])
|
||||||
|
} catch (e) {
|
||||||
|
// cleanup after a failed config execution
|
||||||
|
self.LibResilientPluginConstructors = new Map(lrpcBackup)
|
||||||
|
}
|
||||||
|
|
||||||
// inform
|
// inform
|
||||||
self.log('service-worker', `service worker initialized.\nstrategy in use: ${self.LibResilientPlugins.map(p=>p.name).join(', ')}`)
|
self.log('service-worker', `service worker initialized.\nstrategy in use: ${self.LibResilientPlugins.map(p=>p.name).join(', ')}`)
|
||||||
|
@ -507,42 +512,41 @@ let initServiceWorker = async () => {
|
||||||
// which signifies that relevant code has been successfully loaded; but there are other failure modes!
|
// which signifies that relevant code has been successfully loaded; but there are other failure modes!
|
||||||
if (cresponse.headers.get('x-libresilient-method') != 'fetch') {
|
if (cresponse.headers.get('x-libresilient-method') != 'fetch') {
|
||||||
|
|
||||||
try {
|
// basic structure tests
|
||||||
|
if ( !verifyConfigData(cdata) ) {
|
||||||
// go through the plugins in the new config and check if we already have their constructors
|
self.log('service-worker', 'config.json loaded through transport other than fetch is invalid, ignoring')
|
||||||
// i.e. if the plugin scripts have already been loaded
|
return false
|
||||||
let plugins = [...cdata.plugins]
|
|
||||||
let currentPlugin = plugins.shift()
|
|
||||||
do {
|
|
||||||
// plugin constructor not available, meaning: we'd have to importScripts() it
|
|
||||||
// but we can't since this was not retrieved via fetch(), so we cannot assume
|
|
||||||
// that the main domain of the website is up and available
|
|
||||||
//
|
|
||||||
// if we cache this newly retrieved config.json, next time the service worker gets restarted
|
|
||||||
// we will end up with an error while trying to run importScripts() for this plugin
|
|
||||||
// which in turn would lead to the service worker being unregistered
|
|
||||||
//
|
|
||||||
// if the main domain is not available, this would mean the website stops working
|
|
||||||
// even though we *were* able to retrieve the new config.json via plugins!
|
|
||||||
// so, ignoring this new config.json.
|
|
||||||
if (!LibResilientPluginConstructors.has(currentPlugin.name)) {
|
|
||||||
throw new Error(`config.json loaded through transport other than fetch, but specifies not previously loaded plugin: "${currentPlugin.name}"`)
|
|
||||||
}
|
|
||||||
// push any dependencies into the array, at the very front
|
|
||||||
// thus gradually flattening the config
|
|
||||||
if ("uses" in currentPlugin) {
|
|
||||||
plugins.unshift(...currentPlugin.uses)
|
|
||||||
}
|
|
||||||
// get the next plugin to check
|
|
||||||
currentPlugin = plugins.shift()
|
|
||||||
} while ( (currentPlugin !== false) && (currentPlugin !== undefined) )
|
|
||||||
|
|
||||||
} catch(e) {
|
|
||||||
self.log(
|
|
||||||
'service-worker',
|
|
||||||
`warning: ${e.message}\nignoring the whole config.json.`)
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// go through the plugins in the new config and check if we already have their constructors
|
||||||
|
// i.e. if the plugin scripts have already been loaded
|
||||||
|
let currentPlugin = cdata.plugins.shift()
|
||||||
|
do {
|
||||||
|
// plugin constructor not available, meaning: we'd have to importScripts() it
|
||||||
|
// but we can't since this was not retrieved via fetch(), so we cannot assume
|
||||||
|
// that the main domain of the website is up and available
|
||||||
|
//
|
||||||
|
// if we cache this newly retrieved config.json, next time the service worker gets restarted
|
||||||
|
// we will end up with an error while trying to run importScripts() for this plugin
|
||||||
|
// which in turn would lead to the service worker being unregistered
|
||||||
|
//
|
||||||
|
// if the main domain is not available, this would mean the website stops working
|
||||||
|
// even though we *were* able to retrieve the new config.json via plugins!
|
||||||
|
// so, ignoring this new config.json.
|
||||||
|
if (!LibResilientPluginConstructors.has(currentPlugin.name)) {
|
||||||
|
self.log(
|
||||||
|
'service-worker',
|
||||||
|
`warning: config.json loaded through transport other than fetch, but specifies not previously loaded plugin: "${currentPlugin.name}"\nignoring the whole config.json.`)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// push any dependencies into the array, at the very front
|
||||||
|
// thus gradually flattening the config
|
||||||
|
if ("uses" in currentPlugin) {
|
||||||
|
cdata.plugins.unshift(...currentPlugin.uses)
|
||||||
|
}
|
||||||
|
// get the next plugin to check
|
||||||
|
currentPlugin = cdata.plugins.shift()
|
||||||
|
} while ( (currentPlugin !== false) && (currentPlugin !== undefined) )
|
||||||
}
|
}
|
||||||
|
|
||||||
self.log('service-worker', `config.json successfully retrieved through plugins; caching.`)
|
self.log('service-worker', `config.json successfully retrieved through plugins; caching.`)
|
||||||
|
|
Ładowanie…
Reference in New Issue