libresilient/plugins/any-of.js

55 wiersze
1.6 KiB
JavaScript
Czysty Zwykły widok Historia

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
(function(LRPC){
// this never changes
const pluginName = "any-of"
LRPC.set(pluginName, (LR, init={})=>{
2021-04-06 17:18:37 +00:00
/*
* plugin config settings
*/
// sane defaults
let defaultConfig = {
// list of plugins to run simultaneously
plugins: [{
name: "alt-fetch"
},{
name: "gun-ipfs"
}]
}
// merge the defaults with settings from LibResilientConfig
let config = {...defaultConfig, ...init}
/**
* getting content using regular HTTP(S) fetch()
*/
let fetchContent = (url) => {
LR.log(pluginName, `using: [${config.plugins.map(p=>p.name).join(', ')}]!`)
return Promise.any(
config.plugins.map(p=>p.fetch(url))
)
2021-04-06 17:18:37 +00:00
}
// and add ourselves to it
// with some additional metadata
return {
name: pluginName,
description: `Running simultaneously: [${config.plugins.map(p=>p.name).join(', ')}]`,
version: 'COMMIT_UNKNOWN',
fetch: fetchContent,
uses: config.plugins
}
2021-04-06 17:18:37 +00:00
})
// done with not polluting the global namespace
})(LibResilientPluginConstructors)