2021-04-06 17:18:37 +00:00
|
|
|
/* ========================================================================= *\
|
|
|
|
|* === Any-of: running multiple plugins simultaneously === *|
|
|
|
|
\* ========================================================================= */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this plugin does not implement any push method
|
|
|
|
*/
|
|
|
|
|
|
|
|
// no polluting of the global namespace please
|
2021-09-15 21:28:11 +00:00
|
|
|
(function(LRPC){
|
|
|
|
// this never changes
|
|
|
|
const pluginName = "any-of"
|
|
|
|
LRPC.set(pluginName, (LR, init={})=>{
|
2021-04-06 17:18:37 +00:00
|
|
|
|
2021-09-15 21:28:11 +00:00
|
|
|
/*
|
|
|
|
* plugin config settings
|
|
|
|
*/
|
|
|
|
|
|
|
|
// sane defaults
|
|
|
|
let defaultConfig = {
|
|
|
|
// list of plugins to run simultaneously
|
2021-09-18 22:52:54 +00:00
|
|
|
uses: [{
|
2021-09-15 21:28:11 +00:00
|
|
|
name: "alt-fetch"
|
|
|
|
},{
|
|
|
|
name: "gun-ipfs"
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
// merge the defaults with settings from LibResilientConfig
|
|
|
|
let config = {...defaultConfig, ...init}
|
|
|
|
|
2021-11-11 10:05:48 +00:00
|
|
|
// reality check: if no wrapped plugin configured, complain
|
|
|
|
if (config.uses.length < 1) {
|
|
|
|
throw new Error("No wrapped plugins configured!")
|
|
|
|
}
|
|
|
|
|
2021-09-15 21:28:11 +00:00
|
|
|
/**
|
2022-01-10 22:18:58 +00:00
|
|
|
* getting content using Promise.any() on all configured wrapped plugins
|
2021-09-15 21:28:11 +00:00
|
|
|
*/
|
2021-11-08 20:51:27 +00:00
|
|
|
let fetchContent = (url, init={}) => {
|
2021-09-18 22:52:54 +00:00
|
|
|
LR.log(pluginName, `using: [${config.uses.map(p=>p.name).join(', ')}]!`)
|
2021-09-15 21:28:11 +00:00
|
|
|
return Promise.any(
|
2021-11-08 20:51:27 +00:00
|
|
|
config.uses.map(p=>p.fetch(url, init))
|
2021-09-15 21:28:11 +00:00
|
|
|
)
|
2021-04-06 17:18:37 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 21:28:11 +00:00
|
|
|
// and add ourselves to it
|
|
|
|
// with some additional metadata
|
|
|
|
return {
|
|
|
|
name: pluginName,
|
2021-09-18 22:52:54 +00:00
|
|
|
description: `Running simultaneously: [${config.uses.map(p=>p.name).join(', ')}]`,
|
2021-09-15 21:28:11 +00:00
|
|
|
version: 'COMMIT_UNKNOWN',
|
|
|
|
fetch: fetchContent,
|
2021-09-18 22:52:54 +00:00
|
|
|
uses: config.uses
|
2021-09-15 21:28:11 +00:00
|
|
|
}
|
2021-04-06 17:18:37 +00:00
|
|
|
|
2021-09-15 21:28:11 +00:00
|
|
|
})
|
2021-09-01 02:51:58 +00:00
|
|
|
// done with not polluting the global namespace
|
2021-09-15 21:28:11 +00:00
|
|
|
})(LibResilientPluginConstructors)
|