kopia lustrzana https://gitlab.com/rysiekpl/libresilient
any-of plugin ready for new plugin loading system (ref. #15)
rodzic
46b3334b25
commit
d02cbb7a9d
|
@ -54,32 +54,39 @@ describe("plugin: any-of", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
Object.assign(global, makeServiceWorkerEnv());
|
Object.assign(global, makeServiceWorkerEnv());
|
||||||
jest.resetModules();
|
jest.resetModules();
|
||||||
self.LibResilientPlugins = new Array()
|
global.LibResilientPluginConstructors = new Map()
|
||||||
self.LibResilientConfig = {
|
LR = {
|
||||||
plugins: {
|
log: (component, ...items)=>{
|
||||||
'any-of': {
|
console.debug(component + ' :: ', ...items)
|
||||||
plugins: {
|
|
||||||
'fetch': {},
|
|
||||||
'cache': {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
require("../../plugins/fetch.js");
|
||||||
|
init = {
|
||||||
|
name: 'any-of',
|
||||||
|
plugins: [
|
||||||
|
LibResilientPluginConstructors.get('fetch')(LR),
|
||||||
|
{
|
||||||
|
name: 'reject-all',
|
||||||
|
description: 'Rejects all',
|
||||||
|
version: '0.0.1',
|
||||||
|
fetch: url=>Promise.reject('Reject All!')
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
self.log = function(component, ...items) {
|
self.log = function(component, ...items) {
|
||||||
console.debug(component + ' :: ', ...items)
|
console.debug(component + ' :: ', ...items)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test("it should register in LibResilientPlugins", () => {
|
test("it should register in LibResilientPluginConstructors", () => {
|
||||||
require("../../plugins/any-of.js");
|
require("../../plugins/any-of.js");
|
||||||
expect(self.LibResilientPlugins[0].name).toEqual('any-of');
|
expect(LibResilientPluginConstructors.get('any-of')(LR, init).name).toEqual('any-of');
|
||||||
});
|
});
|
||||||
|
|
||||||
test("it should return data from fetch()", async () => {
|
test("it should return data from fetch()", async () => {
|
||||||
require("../../plugins/any-of.js");
|
require("../../plugins/any-of.js");
|
||||||
require("../../plugins/fetch.js");
|
|
||||||
|
|
||||||
const response = await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json');
|
const response = await LibResilientPluginConstructors.get('any-of')(LR, init).fetch('https://resilient.is/test.json');
|
||||||
|
|
||||||
expect(fetch).toHaveBeenCalled();
|
expect(fetch).toHaveBeenCalled();
|
||||||
expect(await response.json()).toEqual({test: "success"})
|
expect(await response.json()).toEqual({test: "success"})
|
||||||
|
@ -103,11 +110,10 @@ describe("plugin: any-of", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
require("../../plugins/any-of.js");
|
require("../../plugins/any-of.js");
|
||||||
require("../../plugins/fetch.js");
|
|
||||||
|
|
||||||
expect.assertions(2);
|
expect.assertions(2);
|
||||||
try {
|
try {
|
||||||
await self.LibResilientPlugins[0].fetch('https://resilient.is/test.json')
|
await LibResilientPluginConstructors.get('any-of')(LR, init).fetch('https://resilient.is/test.json')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Array) {
|
if (e instanceof Array) {
|
||||||
expect(e[0].toString()).toMatch('Error')
|
expect(e[0].toString()).toMatch('Error')
|
||||||
|
|
|
@ -7,49 +7,48 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// no polluting of the global namespace please
|
// no polluting of the global namespace please
|
||||||
(function () {
|
(function(LRPC){
|
||||||
|
// this never changes
|
||||||
|
const pluginName = "any-of"
|
||||||
|
LRPC.set(pluginName, (LR, init={})=>{
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* plugin config settings
|
* plugin config settings
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// sane defaults
|
// sane defaults
|
||||||
let defaultConfig = {
|
let defaultConfig = {
|
||||||
// name of this plugin
|
// list of plugins to run simultaneously
|
||||||
// should not be changed
|
plugins: [{
|
||||||
name: "any-of",
|
name: "alt-fetch"
|
||||||
// list of plugins to run simultaneously
|
},{
|
||||||
plugins: {
|
name: "gun-ipfs"
|
||||||
"alt-fetch": {},
|
}]
|
||||||
"gun-ipfs": {}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// merge the defaults with settings from LibResilientConfig
|
// merge the defaults with settings from LibResilientConfig
|
||||||
let config = {...defaultConfig, ...self.LibResilientConfig.plugins[defaultConfig.name]}
|
let config = {...defaultConfig, ...init}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getting content using regular HTTP(S) fetch()
|
* getting content using regular HTTP(S) fetch()
|
||||||
*/
|
*/
|
||||||
let fetchContent = (url) => {
|
let fetchContent = (url) => {
|
||||||
self.log(config.name, `using: [${Object.keys(config.plugins).join(', ')}]!`)
|
LR.log(pluginName, `using: [${config.plugins.map(p=>p.name).join(', ')}]!`)
|
||||||
return Promise.any(
|
return Promise.any(
|
||||||
self
|
config.plugins.map(p=>p.fetch(url))
|
||||||
.LibResilientPlugins
|
)
|
||||||
.filter(p=>Object.keys(config.plugins).includes(p.name))
|
}
|
||||||
.map(p=>p.fetch(url))
|
|
||||||
)
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// and add ourselves to it
|
|
||||||
// with some additional metadata
|
|
||||||
self.LibResilientPlugins.push({
|
|
||||||
name: config.name,
|
|
||||||
description: `Running simultaneously: [${Object.keys(config.plugins).join(', ')}]`,
|
|
||||||
version: 'COMMIT_UNKNOWN',
|
|
||||||
fetch: fetchContent,
|
|
||||||
uses: config.plugins
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// done with not polluting the global namespace
|
// done with not polluting the global namespace
|
||||||
})()
|
})(LibResilientPluginConstructors)
|
||||||
|
|
Ładowanie…
Reference in New Issue