improved config.json error handling, including additional tests (ref. #48)

merge-requests/23/merge
Michał 'rysiek' Woźniak 2024-02-11 23:27:56 +00:00
rodzic 28c79a35e4
commit b50b76d0d5
2 zmienionych plików z 769 dodań i 366 usunięć

Wyświetl plik

@ -195,6 +195,11 @@ self.guessMimeType = async function(ext, content) {
* cdata - config data to verify
*/
let verifyConfigData = (cdata) => {
// cdata needs to be an object
if ( typeof cdata !== "object" || cdata === null ) {
self.log('service-worker', 'fetched config does not contain a valid JSON object')
return false;
}
// basic check for the plugins field
if ( !("plugins" in cdata) || ! Array.isArray(cdata.plugins) ) {
self.log('service-worker', 'fetched config does not contain a valid "plugins" field')
@ -284,9 +289,6 @@ let executeConfig = (config) => {
// in case we need it later (for example, when re-loading the config)
let pluginsConfig = [...config.plugins]
// we want to catch any and all possible errors here
try {
// this is the stash for plugins that need dependencies instantiated first
let dependentPlugins = new Array()
@ -406,17 +408,6 @@ let executeConfig = (config) => {
// we're good!
return true;
// exception? no bueno
} catch (e) {
// inform
self.log('service-worker', `error while executing config: ${e.message}`)
// cleanup after a failed config execution
self.LibResilientPluginConstructors = new Map(lrpcBackup)
self.LibResilientPlugins = new Array()
// we are not good
return false;
}
}
@ -530,8 +521,21 @@ let initServiceWorker = async () => {
config = {...self.LibResilientConfig, ...cdata}
// try executing the config
// we want to catch any and all possible errors here
try {
config_executed = executeConfig(config)
// exception? no bueno
} catch (e) {
// inform
self.log('service-worker', `error while executing config: ${e.message}`)
// cleanup after a failed config execution
self.LibResilientPluginConstructors = new Map(lrpcBackup)
self.LibResilientPlugins = new Array()
// we are not good
config_executed = false;
}
// if we're using the defaults, and yet loading of the config failed
// something is massively wrong
if ( ( cresponse === false ) && ( config_executed === false ) ) {
@ -552,15 +556,22 @@ let initServiceWorker = async () => {
// that is, if it comes from the "v1" cache...
if (use_cache === "v1") {
self.log('service-worker', `successfully loaded config.json; caching in cache: v1:verified`)
cacheConfigJSON(configURL, cresponse, 'v1:verified')
// or, was fetch()ed and valid (no caching if we're going with defaults, obviously)
await cacheConfigJSON(configURL, cresponse, 'v1:verified')
// we used the v1:verified cache; we should cache config.json into the v1 cache
// as that will speed things up a bit next time we need to load the service worker
} else if (use_cache === "v1:verified") {
self.log('service-worker', `successfully loaded config.json; caching in cache: v1`)
await cacheConfigJSON(configURL, cresponse, 'v1')
// or, was fetch()-ed and valid (no caching if we're going with defaults, obviously)
} else if ( (use_cache === undefined) && (cresponse !== false) ) {
self.log('service-worker', `successfully loaded config.json; caching in cache: v1, v1:verified`)
// we want to cache to both, so that:
// 1. we get the extra bit of performance from using the v1 cache that is checked first
// 2. but we get the verified config already in the v1:verified cache for later
cacheConfigJSON(configURL, await cresponse.clone(), 'v1')
cacheConfigJSON(configURL, cresponse, 'v1:verified')
await cacheConfigJSON(configURL, await cresponse.clone(), 'v1')
await cacheConfigJSON(configURL, cresponse, 'v1:verified')
}
// inform