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

WIP: making sure we can disable stashing plugins at will (ref: #17)

parent 1e73788d
Pipeline #1958 passed with stage
in 3 minutes and 29 seconds
......@@ -178,11 +178,12 @@ let getSamizdatPluginByName = (name) => {
* run a plugin's fetch() method
* while handling all the auxiliary stuff like saving info in SamizdatInfo
*
* plugin - the plugin to use
* url - string containing the URL to fetch
* lastError - error thrown by the previous plugin, if any (default: null)
* plugin - the plugin to use
* url - string containing the URL to fetch
* lastError - error thrown by the previous plugin, if any (default: null)
* useStashed - use stashed resources; if false, error out on any plugin that implements stash() (default: true)
*/
let samizdatFetch = (plugin, url, lastError=null) => {
let samizdatFetch = (plugin, url, lastError=null, useStashed=true) => {
// save info in SamizdatInfo
SamizdatInfo.resources[url].fetchError = lastError;
SamizdatInfo.resources[url].method = plugin.name
......@@ -190,6 +191,11 @@ let samizdatFetch = (plugin, url, lastError=null) => {
console.log("(COMMIT_UNKNOWN) Samizdat handling URL:", url);
console.log('+-- last error : ' + lastError)
console.log('+-- current method : ' + plugin.name)
// do we want to use stashed resources?
// TODO: there's probably a better way of handling that than throwing an Error()
if (typeof plugin.stash === 'function' && ! useStashed) {
throw new Error('Not supposed to use stashed resources.')
}
// run the plugin
return plugin.fetch(url)
}
......@@ -200,7 +206,7 @@ let samizdatFetch = (plugin, url, lastError=null) => {
* was able to get the resource
*
* url - string containing the URL we want to fetch
* useStashed - (not implemented yet; currently all plugins are used) use stashed resources; if false, only pull resources from live sources
* useStashed - use stashed resources; if false, only pull resources from live sources
* doStash - stash resources once fetched successfully; if false, do not stash pulled resources automagically
*/
let getResourceThroughSamizdat = (url, useStashed=true, doStash=true) => {
......@@ -223,14 +229,13 @@ let getResourceThroughSamizdat = (url, useStashed=true, doStash=true) => {
return SamizdatPlugins.reduce(
(prevPromise, currentPlugin)=>{
return prevPromise.catch((error)=>{
// Fall back to Samizdat:
return samizdatFetch(currentPlugin, url, error.toString())
return samizdatFetch(currentPlugin, url, error.toString(), useStashed)
})
},
// this samizdatFetch() will run first
// all other promises generated by SamizdatPlugins[] will be chained on it
// using the catch() in reduce() above
samizdatFetch(SamizdatPlugins[0], url)
samizdatFetch(SamizdatPlugins[0], url, null, useStashed)
)
.then((response)=>{
// do we want to stash at all?
......@@ -321,5 +326,5 @@ self.addEventListener('fetch', event => {
// GET requests to our own domain that are *not* #samizdat-info requests
// get handled by plugins in case of an error
return void event.respondWith(getResourceThroughSamizdat(cleanURL))
return void event.respondWith(getResourceThroughSamizdat(cleanURL, false))
});
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