improving plugin loading (ref. #15)

merge-requests/3/head
Michał 'rysiek' Woźniak 2021-09-15 20:06:04 +00:00
rodzic 295f3071cc
commit 0c7fb8c946
2 zmienionych plików z 55 dodań i 43 usunięć

Wyświetl plik

@ -7,34 +7,33 @@
*/
// plugins config
self.LibResilientConfig.plugins = {
'fetch':{},
'cache':{},
'any-of': {
plugins: {
'alt-fetch':{
// configuring the alternate endpoints plugin to use IPNS gateways
//
// NOTICE: we cannot use CIDv0 with gateways that use hash directly in the (sub)domain:
// https://github.com/node-fetch/node-fetch/issues/260
// we *can* use CIDv1 with such gateways, and that's suggested:
// https://docs.ipfs.io/how-to/address-ipfs-on-web/#path-gateway
// https://cid.ipfs.io/
endpoints: [
'https://<CIDv1>.ipns.dweb.link/', // USA
'https://ipfs.kxv.io/ipns/<CIDv0-or-CIDv1>/', // Hong Kong
'https://jorropo.net/ipns/<CIDv0-or-CIDv1>/', // France
'https://gateway.pinata.cloud/ipns/<CIDv0-or-CIDv1>/', // Germany
'https://<CIDv1>.ipns.bluelight.link/' // Singapore
self.LibResilientConfig.plugins = [{
name: 'fetch'
},{
name: 'cache'
},{
name: 'any-of',
plugins: [{
name: 'alt-fetch',
// configuring the alternate endpoints plugin to use IPNS gateways
//
// NOTICE: we cannot use CIDv0 with gateways that use hash directly in the (sub)domain:
// https://github.com/node-fetch/node-fetch/issues/260
// we *can* use CIDv1 with such gateways, and that's suggested:
// https://docs.ipfs.io/how-to/address-ipfs-on-web/#path-gateway
// https://cid.ipfs.io/
endpoints: [
'https://<CIDv1>.ipns.dweb.link/', // USA
'https://ipfs.kxv.io/ipns/<CIDv0-or-CIDv1>/', // Hong Kong
'https://jorropo.net/ipns/<CIDv0-or-CIDv1>/', // France
'https://gateway.pinata.cloud/ipns/<CIDv0-or-CIDv1>/', // Germany
'https://<CIDv1>.ipns.bluelight.link/' // Singapore
]
},
'gun-ipfs': {
]},{
name: 'gun-ipfs',
gunPubkey: '<your-gun-pubkey>'
}
}
}
}
}]
}]
// we need to explicitly list components we want to see debug messages from
self.LibResilientConfig.loggedComponents = ['service-worker', 'fetch', 'cache', 'any-of', 'alt-fetch', 'gun-ipfs']

Wyświetl plik

@ -60,12 +60,16 @@ if (typeof self.LibResilientConfig !== 'object' || self.LibResilientConfig === n
//
// this relies on JavaScript preserving the insertion order for properties
// https://stackoverflow.com/a/5525820
plugins: {
'fetch':{},
'cache':{},
'alt-fetch':{},
'gun-ipfs':{}
},
plugins: [{
name: 'fetch'
},{
name: 'cache'
},{
name: 'alt-fetch'
},{
name: 'gun-ipfs'
}
],
// which components should be logged?
// this is an array of strings, components not listed here
// will have their debug output disabled
@ -111,18 +115,27 @@ try {
var LibResilientPluginConstructors = new Map()
// only now load the plugins (config.js could have changed the defaults)
var plugins = Object.keys(self.LibResilientConfig.plugins)
for (var i=0; i<plugins.length; i++) {
// load a plugin
self.importScripts(`./plugins/${plugins[i]}.js`)
self.LibResilientPlugins.push(
LibResilientPluginConstructors.get(plugins[i])(self, self.LibResilientConfig.plugins[plugins[i]])
)
// check if it loaded properly
var plugin = self.LibResilientPlugins.find(p=>p.name===plugins[i])
if (plugin === undefined) {
throw new Error(`Plugin not found: ${plugins[i]} (available plugins: ${self.LibResilientPlugins.map(p=>p.name).join(', ')})`)
for (var pluginConfig of self.LibResilientConfig.plugins) {
// load a plugin script, only if not yet loaded
if (!LibResilientPluginConstructors.has(pluginConfig.name)) {
self.importScripts(`./plugins/${pluginConfig.name}.js`)
}
// instantiate the plugin
self.LibResilientPlugins.push(
LibResilientPluginConstructors.get(pluginConfig.name)(self, pluginConfig)
)
// check if it loaded properly
// TODO: should be done in a tad less resource-intensive way perhaps? then again, this is a short array anyway
var plugin = self.LibResilientPlugins.find(p=>p.name===pluginConfig.name)
if (plugin === undefined) {
throw new Error(`Plugin not found: ${pluginConfig.name} (available plugins: ${self.LibResilientPlugins.map(p=>p.name).join(', ')})`)
}
// handling "uses" plugins
// make sure that the indirect flag is set if needed
if (self.LibResilientConfig.plugins[plugin.name].indirect===true) {
plugin.indirect=true