service-worker: cleaner handling of config.js (ref. #30)

merge-requests/12/merge
Michał 'rysiek' Woźniak 2022-02-06 23:06:43 +00:00
rodzic 4b02ddc0eb
commit 0a320d22f8
1 zmienionych plików z 54 dodań i 26 usunięć

Wyświetl plik

@ -140,6 +140,29 @@ let cacheConfigJSON = async (configURL, cresponse) => {
}
}
/**
* get config JSON and verify it's valid
*
* cresponse - the Response object to work with
*/
let getConfigJSON = async (cresponse) => {
if (cresponse == undefined) {
self.log('service-worker', 'config.json response is undefined')
return false;
}
if (cresponse.status != 200) {
self.log('service-worker', `config.json response status is not 200: ${cdata.status} ${cdata.statusText})`)
return false;
}
var cdata = await cresponse.clone().json()
if (verifyConfigData(cdata)) {
return cdata;
}
return false;
}
// flag signifying the SW has been initialized already
var initDone = false
@ -159,40 +182,45 @@ let initServiceWorker = async () => {
// self.registration.scope contains the scope this service worker is registered for
// so it makes sense to pull config from `config.json` file directly under that location
try {
// config.json URL
var configURL = self.registration.scope + "config.json"
// we need to know if the config was already cached
var wasCached = true
// get the config file, cached or otherwise
// get the config data from cache
var cresponse = await caches.match(configURL)
if (cresponse != undefined) {
self.log('service-worker', `config file retrieved from cache.`)
var cdata = await getConfigJSON(cresponse)
// did it work?
if (cdata != false) {
// we need to know if the config was already cached
self.log('service-worker', `valid config file retrieved from cache.`)
var wasCached = true
// cache failed to deliver
} else {
self.log('service-worker', `config file not found in cache, fetching.`)
wasCached = false
cresponse = await fetch(configURL)
}
// check for sanity
// TODO: check also after cache retrieval?
if (cresponse.status != 200) {
self.log('service-worker', `failed to fetch config (${cdata.status} ${cdata.statusText}).`)
} else {
// process the data
cdata = await cresponse.clone().json()
// verify the config
if (verifyConfigData(cdata)) {
// merge the config
self.LibResilientConfig = {...self.LibResilientConfig, ...cdata}
self.log('service-worker', 'config loaded.')
// cache the valid config.json
if (!wasCached) {
// was not retrieved from cache? cache, then!
cacheConfigJSON(configURL, cresponse)
}
// try fetching directly from the main domain
var cresponse = await fetch(configURL)
cdata = await getConfigJSON(cresponse)
// did that work?
if (cdata != false) {
self.log('service-worker', `config file not found in cache, fetching.`)
var wasCached = false
// cache it, asynchronously
cacheConfigJSON(configURL, cresponse)
} else {
// we ain't got nothing useful -- justs set cdata to an empty object
cdata = {}
self.log('service-worker', 'ignoring invalid config, using defaults.')
}
}
// merge configs
self.LibResilientConfig = {...self.LibResilientConfig, ...cdata}
self.log('service-worker', 'config loaded.')
} catch (e) {
self.log('service-worker', 'config loading failed, using defaults; error:', e)
}