Commit bff5a6a8 authored by Michał "rysiek" Woźniak's avatar Michał "rysiek" Woźniak
Browse files

regular fetch() implemented as a plugin (fixes #21)

parent 14e920dd
Pipeline #1948 passed with stage
in 4 minutes and 11 seconds
/* ========================================================================= *\
|* === Regular HTTP(S) fetch() plugin === *|
\* ========================================================================= */
/**
* this plugin does not implement any push method
*/
/**
* getting content using regular HTTP(S) fetch()
*/
let fetchContent = (url) => {
console.log('Samizdat: regular fetch!')
return fetch(url)
.then((response) => {
// 4xx? 5xx? that's a paddlin'
if (response.status >= 400) {
// throw an Error to fall back to Samizdat:
throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText);
}
// all good, it seems
console.log("(COMMIT_UNKNOWN) Fetched:", response.url);
return response;
})
}
// initialize the SamizdatPlugins array
if (!Array.isArray(self.SamizdatPlugins)) {
self.SamizdatPlugins = new Array()
}
// and add ourselves to it
// with some additional metadata
self.SamizdatPlugins.push({
name: 'fetch',
description: 'Just a regular HTTP(S) fetch()',
version: 'COMMIT_UNKNOWN',
fetch: fetchContent,
push: null
})
/*
* Samizdat Service Worker.
*
* Strategy:
* Strategy (not fully implemented yet):
* 1. Try to load from main website.
* 2. If loading fails, load from Samizdat.
* 3. If loading is too slow, load from Samizdat.
......@@ -212,42 +212,33 @@ self.addEventListener('fetch', event => {
// use the plugin
return nextPlugin.fetch(cleanURL)
})
// this fetch() will run first
},
// this SamizdatPlugin[].fetch() will run first
// all other promises generated by SamizdatPlugins[] will be chained using catch() on it
}, fetch(event.request)
.then((response) => {
// that's a paddlin'
if (response.status >= 400) {
// throw an Error to fall back to Samizdat:
throw new Error('HTTP Error: ' + response.status + ' ' + response.statusText);
}
// all good, it seems
console.log("Fetched:", response.url);
return response;
})
)
/*.then((response)=>{
// TODO: this assumes the cache plugin is the first one!
// TODO: we should not be caching a response fetched from cache!
console.log("(COMMIT_UNKNOWN) Caching successful fetch: ", cleanURL);
return SamizdatPlugins[0].push(response.clone(), cleanURL).then((res)=>{
return response
})
})*/
// a final catch... in case all plugins fail
.catch((err)=>{
console.log("(COMMIT_UNKNOWN) Samizdat also failed completely: ", err);
console.log('+-- URL : ' + cleanURL)
// cleanup
SamizdatInfo.resources[cleanURL].fetchError = err.toString()
// this is very naïve and should in fact be handled inside the relevant plugin, probably
SamizdatInfo.resources[cleanURL].method = null
// rethrow
throw err
SamizdatPlugins[0].fetch(cleanURL)
)
/*.then((response)=>{
// TODO: this assumes the cache plugin is the first one!
// TODO: we should not be caching a response fetched from cache!
console.log("(COMMIT_UNKNOWN) Caching successful fetch: ", cleanURL);
return SamizdatPlugins[0].push(response.clone(), cleanURL).then((res)=>{
return response
})
// this is where we can attach a .then() that will run regardless of the result
// which could then:
// - cache the results or perform some other housekeeping in case of success
// - handle the error in case of failure
})*/
// a final catch... in case all plugins fail
.catch((err)=>{
console.log("(COMMIT_UNKNOWN) Samizdat also failed completely: ", err);
console.log('+-- URL : ' + cleanURL)
// cleanup
SamizdatInfo.resources[cleanURL].fetchError = err.toString()
// this is very naïve and should in fact be handled inside the relevant plugin, probably
SamizdatInfo.resources[cleanURL].method = null
// rethrow
throw err
})
// this is where we can attach a .then() that will run regardless of the result
// which could then:
// - cache the results or perform some other housekeeping in case of success
// - handle the error in case of failure
);
});
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment