kopia lustrzana https://gitlab.com/rysiekpl/libresilient
very rough and PoC test implementation of JSON-based config (ref. #31)
rodzic
07693eeedd
commit
d426dea050
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"plugins": [{
|
||||||
|
"name": "fetch"
|
||||||
|
},{
|
||||||
|
"name": "cache"
|
||||||
|
},{
|
||||||
|
"name": "basic-integrity",
|
||||||
|
"integrity": [{
|
||||||
|
"/test.json": "sha384-kn5dhxz4RpBmx7xC7Dmq2N43PclV9U/niyh+4Km7oz5W0FaWdz3Op+3K0Qxz8y3z"
|
||||||
|
}],
|
||||||
|
"uses": [{
|
||||||
|
"name": "fetch"
|
||||||
|
}]
|
||||||
|
}],
|
||||||
|
"loggedComponents": ["service-worker", "fetch", "cache"]
|
||||||
|
}
|
|
@ -76,7 +76,7 @@ uses: config.uses
|
||||||
|
|
||||||
## Fetching a resource via LibResilient
|
## Fetching a resource via LibResilient
|
||||||
|
|
||||||
Whenever a resource is being fetched on a LibResilient-enabled site, the `service-worker.js` script dispatches plugins in the set order. This order is configured via the `plugins` key of the `LibResilientConfig` variable, usually set in `config.js` config file.
|
Whenever a resource is being fetched on a LibResilient-enabled site, the `service-worker.js` script dispatches plugins in the set order. This order is configured via the `plugins` key of the `LibResilientConfig` variable, usually set in `config.json` config file.
|
||||||
|
|
||||||
A minimal default configuration is hard-coded in case no site-specific configuration is provided. This default configuration runs these plugins:
|
A minimal default configuration is hard-coded in case no site-specific configuration is provided. This default configuration runs these plugins:
|
||||||
|
|
||||||
|
|
|
@ -499,7 +499,7 @@ if ('serviceWorker' in navigator) {
|
||||||
var serviceWorkerPath = scriptFolder + 'service-worker.js'
|
var serviceWorkerPath = scriptFolder + 'service-worker.js'
|
||||||
self.log('browser-side', 'Service Worker script at: ' + serviceWorkerPath)
|
self.log('browser-side', 'Service Worker script at: ' + serviceWorkerPath)
|
||||||
// TODO: is there a way to provide config params for the Service Worker here?
|
// TODO: is there a way to provide config params for the Service Worker here?
|
||||||
// TODO: it would be good if the config.js script could reside outside of the libresilient directory
|
// TODO: it would be good if the config.json file could reside outside of the libresilient directory
|
||||||
// TODO: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
|
// TODO: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
|
||||||
navigator.serviceWorker.register(serviceWorkerPath, {
|
navigator.serviceWorker.register(serviceWorkerPath, {
|
||||||
// TODO: what is the scope relative to? is it the HTML file that included it, or this script?
|
// TODO: what is the scope relative to? is it the HTML file that included it, or this script?
|
||||||
|
|
|
@ -34,7 +34,7 @@ if (typeof window === 'undefined') {
|
||||||
let defaultConfig = {
|
let defaultConfig = {
|
||||||
// Gun nodes to use
|
// Gun nodes to use
|
||||||
gunNodes: ['https://gunjs.herokuapp.com/gun'],
|
gunNodes: ['https://gunjs.herokuapp.com/gun'],
|
||||||
// the pubkey of the preconfigured Gun user; always needs to be set in config.js
|
// the pubkey of the preconfigured Gun user; always needs to be set in config.json
|
||||||
gunPubkey: null,
|
gunPubkey: null,
|
||||||
// the IPFS gateway we're using for verification when publishing; default is usually ok
|
// the IPFS gateway we're using for verification when publishing; default is usually ok
|
||||||
ipfsGateway: 'https://gateway.ipfs.io'
|
ipfsGateway: 'https://gateway.ipfs.io'
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
|
|
||||||
// sane defaults
|
// sane defaults
|
||||||
let defaultConfig = {
|
let defaultConfig = {
|
||||||
// the pubkey of the preconfigured IPNS node; always needs to be set in config.js
|
// the pubkey of the preconfigured IPNS node; always needs to be set in config.json
|
||||||
ipnsPubkey: null,
|
ipnsPubkey: null,
|
||||||
// the IPFS gateway we're using for verification when publishing; default is usually ok
|
// the IPFS gateway we're using for verification when publishing; default is usually ok
|
||||||
ipfsGateway: 'https://gateway.ipfs.io'
|
ipfsGateway: 'https://gateway.ipfs.io'
|
||||||
|
|
|
@ -39,7 +39,7 @@ if (!Array.isArray(self.LibResilientPlugins)) {
|
||||||
// initialize the LibResilientConfig array
|
// initialize the LibResilientConfig array
|
||||||
//
|
//
|
||||||
// this also sets some sane defaults,
|
// this also sets some sane defaults,
|
||||||
// which then can be modified via config.js
|
// which then can be modified via config.json
|
||||||
if (typeof self.LibResilientConfig !== 'object' || self.LibResilientConfig === null) {
|
if (typeof self.LibResilientConfig !== 'object' || self.LibResilientConfig === null) {
|
||||||
self.LibResilientConfig = {
|
self.LibResilientConfig = {
|
||||||
// how long do we wait before we decide that a plugin is unresponsive,
|
// how long do we wait before we decide that a plugin is unresponsive,
|
||||||
|
@ -93,119 +93,126 @@ self.log = function(component, ...items) {
|
||||||
//
|
//
|
||||||
// everything in a try-catch block
|
// everything in a try-catch block
|
||||||
// so that we get an informative message if there's an error
|
// so that we get an informative message if there's an error
|
||||||
try {
|
let initServiceWorker = async () => {
|
||||||
|
|
||||||
// get the config
|
|
||||||
//
|
|
||||||
// self.registration.scope contains the scope this service worker is registered for
|
|
||||||
// so it makes sense to pull config from `config.js` file directly under that location
|
|
||||||
//
|
|
||||||
// TODO: providing config directly from browser-side control script via postMessage?
|
|
||||||
// TODO: `updateViaCache=imports` allows at least config.js to be updated using the cache plugin?
|
|
||||||
try {
|
try {
|
||||||
self.importScripts(self.registration.scope + "config.js")
|
|
||||||
self.log('service-worker', 'config loaded.')
|
|
||||||
} catch (e) {
|
|
||||||
self.log('service-worker', 'config loading failed, using defaults')
|
|
||||||
}
|
|
||||||
|
|
||||||
// create the LibResilientPluginConstructors map
|
|
||||||
// the global... hack is here so that we can run tests; not the most elegant
|
|
||||||
// TODO: find a better way
|
|
||||||
var LibResilientPluginConstructors = self.LibResilientPluginConstructors || new Map()
|
|
||||||
|
|
||||||
// this is the stash for plugins that need dependencies instantiated first
|
|
||||||
var dependentPlugins = new Array()
|
|
||||||
|
|
||||||
// only now load the plugins (config.js could have changed the defaults)
|
|
||||||
while (self.LibResilientConfig.plugins.length > 0) {
|
|
||||||
|
|
||||||
// get the first plugin config from the array
|
|
||||||
let pluginConfig = self.LibResilientConfig.plugins.shift()
|
|
||||||
self.log('service-worker', `handling plugin type: ${pluginConfig.name}`)
|
|
||||||
|
|
||||||
// load the relevant plugin script (if not yet loaded)
|
|
||||||
if (!LibResilientPluginConstructors.has(pluginConfig.name)) {
|
|
||||||
self.log('service-worker', `${pluginConfig.name}: loading plugin's source`)
|
|
||||||
self.importScripts(`./plugins/${pluginConfig.name}.js`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// do we have any dependencies we should handle first?
|
|
||||||
if (typeof pluginConfig.uses !== "undefined") {
|
|
||||||
self.log('service-worker', `${pluginConfig.name}: ${pluginConfig.uses.length} dependencies found`)
|
|
||||||
|
|
||||||
// move the dependency plugin configs to LibResilientConfig to be worked on next
|
|
||||||
for (var i=(pluginConfig.uses.length); i--; i>=0) {
|
|
||||||
self.log('service-worker', `${pluginConfig.name}: dependency found: ${pluginConfig.uses[i].name}`)
|
|
||||||
// put the plugin config in front of the plugin configs array
|
|
||||||
self.LibResilientConfig.plugins.unshift(pluginConfig.uses[i])
|
|
||||||
// set each dependency plugin config to false so that we can keep track
|
|
||||||
// as we fill those gaps later with instantiated dependency plugins
|
|
||||||
pluginConfig.uses[i] = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// stash the plugin config until we have all the dependencies handled
|
|
||||||
self.log('service-worker', `${pluginConfig.name}: not instantiating until dependencies are ready`)
|
|
||||||
dependentPlugins.push(pluginConfig)
|
|
||||||
|
|
||||||
// move on to the next plugin config, which at this point will be
|
|
||||||
// the first of dependencies for the plugin whose config got stashed
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
|
|
||||||
// instantiate the plugin
|
|
||||||
let plugin = LibResilientPluginConstructors.get(pluginConfig.name)(self, pluginConfig)
|
|
||||||
self.log('service-worker', `${pluginConfig.name}: instantiated`)
|
|
||||||
|
|
||||||
// do we have a stashed plugin that requires dependencies?
|
|
||||||
if (dependentPlugins.length === 0) {
|
|
||||||
// no we don't; so, this plugin goes directly to the plugin list
|
|
||||||
self.LibResilientPlugins.push(plugin)
|
|
||||||
// we're done here
|
|
||||||
self.log('service-worker', `${pluginConfig.name}: no dependent plugins, pushing directly to LibResilientPlugins`)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// at this point clearly there is at least one element in dependentPlugins
|
|
||||||
// so we can safely assume that the freshly instantiated plugin is a dependency
|
|
||||||
//
|
|
||||||
// in that case let's find the first empty spot for a dependency
|
|
||||||
let didx = dependentPlugins[dependentPlugins.length - 1].uses.indexOf(false)
|
|
||||||
// assign the freshly instantiated plugin as that dependency
|
|
||||||
dependentPlugins[dependentPlugins.length - 1].uses[didx] = plugin
|
|
||||||
self.log('service-worker', `${pluginConfig.name}: assigning as dependency (#${didx}) to ${dependentPlugins[dependentPlugins.length - 1].name}`)
|
|
||||||
|
|
||||||
// was this the last one?
|
|
||||||
if (didx >= dependentPlugins[dependentPlugins.length - 1].uses.length - 1) {
|
|
||||||
// yup, last one!
|
|
||||||
self.log('service-worker', `${pluginConfig.name}: this was the last dependency of ${dependentPlugins[dependentPlugins.length - 1].name}`)
|
|
||||||
// we can now proceed to instantiate the last element of dependentPlugins
|
|
||||||
pluginConfig = dependentPlugins.pop()
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// it is not the last one, so there should be more dependency plugins to instantiate first
|
|
||||||
// before we can instantiate the last of element of dependentPlugins
|
|
||||||
// but that requires the full treatment, including checing the `uses` field for their configs
|
|
||||||
self.log('service-worker', `${pluginConfig.name}: not yet the last dependency of ${dependentPlugins[dependentPlugins.length - 1].name}`)
|
|
||||||
pluginConfig = false
|
|
||||||
|
|
||||||
// if pluginConfig is not false, rinse-repeat the plugin instantiation steps
|
|
||||||
// since we are dealing with the last element of dependentPlugins
|
|
||||||
} while (pluginConfig !== false)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// inform
|
|
||||||
self.log('service-worker', `DEBUG: Strategy in use: ${self.LibResilientPlugins.map(p=>p.name).join(', ')}`)
|
|
||||||
|
|
||||||
} catch(e) {
|
// get the config
|
||||||
// we only get a cryptic "Error while registering a service worker"
|
//
|
||||||
// unless we explicitly print the errors out in the console
|
// self.registration.scope contains the scope this service worker is registered for
|
||||||
console.error(e)
|
// so it makes sense to pull config from `config.json` file directly under that location
|
||||||
throw e
|
//
|
||||||
|
// TODO: providing config directly from browser-side control script via postMessage?
|
||||||
|
// TODO: `updateViaCache=imports` allows at least config.json to be updated using the cache plugin?
|
||||||
|
console.debug('* * * LOADING CONFIG * * *')
|
||||||
|
try {
|
||||||
|
//self.importScripts(self.registration.scope + "config.json")
|
||||||
|
var cdata = await fetch(self.registration.scope + "config.json")
|
||||||
|
cdata = await cdata.json()
|
||||||
|
self.LibResilientConfig.plugins = cdata.plugins
|
||||||
|
self.LibResilientConfig.loggedComponents = cdata.loggedComponents
|
||||||
|
self.log('service-worker', 'config loaded.')
|
||||||
|
} catch (e) {
|
||||||
|
self.log('service-worker', 'config loading failed, using defaults')
|
||||||
|
}
|
||||||
|
|
||||||
|
// create the LibResilientPluginConstructors map
|
||||||
|
// the global... hack is here so that we can run tests; not the most elegant
|
||||||
|
// TODO: find a better way
|
||||||
|
self.LibResilientPluginConstructors = self.LibResilientPluginConstructors || new Map()
|
||||||
|
|
||||||
|
// this is the stash for plugins that need dependencies instantiated first
|
||||||
|
var dependentPlugins = new Array()
|
||||||
|
|
||||||
|
// only now load the plugins (config.json could have changed the defaults)
|
||||||
|
while (self.LibResilientConfig.plugins.length > 0) {
|
||||||
|
|
||||||
|
// get the first plugin config from the array
|
||||||
|
let pluginConfig = self.LibResilientConfig.plugins.shift()
|
||||||
|
self.log('service-worker', `handling plugin type: ${pluginConfig.name}`)
|
||||||
|
|
||||||
|
// load the relevant plugin script (if not yet loaded)
|
||||||
|
if (!LibResilientPluginConstructors.has(pluginConfig.name)) {
|
||||||
|
self.log('service-worker', `${pluginConfig.name}: loading plugin's source`)
|
||||||
|
self.importScripts(`./plugins/${pluginConfig.name}.js`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// do we have any dependencies we should handle first?
|
||||||
|
if (typeof pluginConfig.uses !== "undefined") {
|
||||||
|
self.log('service-worker', `${pluginConfig.name}: ${pluginConfig.uses.length} dependencies found`)
|
||||||
|
|
||||||
|
// move the dependency plugin configs to LibResilientConfig to be worked on next
|
||||||
|
for (var i=(pluginConfig.uses.length); i--; i>=0) {
|
||||||
|
self.log('service-worker', `${pluginConfig.name}: dependency found: ${pluginConfig.uses[i].name}`)
|
||||||
|
// put the plugin config in front of the plugin configs array
|
||||||
|
self.LibResilientConfig.plugins.unshift(pluginConfig.uses[i])
|
||||||
|
// set each dependency plugin config to false so that we can keep track
|
||||||
|
// as we fill those gaps later with instantiated dependency plugins
|
||||||
|
pluginConfig.uses[i] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// stash the plugin config until we have all the dependencies handled
|
||||||
|
self.log('service-worker', `${pluginConfig.name}: not instantiating until dependencies are ready`)
|
||||||
|
dependentPlugins.push(pluginConfig)
|
||||||
|
|
||||||
|
// move on to the next plugin config, which at this point will be
|
||||||
|
// the first of dependencies for the plugin whose config got stashed
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
// instantiate the plugin
|
||||||
|
let plugin = LibResilientPluginConstructors.get(pluginConfig.name)(self, pluginConfig)
|
||||||
|
self.log('service-worker', `${pluginConfig.name}: instantiated`)
|
||||||
|
|
||||||
|
// do we have a stashed plugin that requires dependencies?
|
||||||
|
if (dependentPlugins.length === 0) {
|
||||||
|
// no we don't; so, this plugin goes directly to the plugin list
|
||||||
|
self.LibResilientPlugins.push(plugin)
|
||||||
|
// we're done here
|
||||||
|
self.log('service-worker', `${pluginConfig.name}: no dependent plugins, pushing directly to LibResilientPlugins`)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// at this point clearly there is at least one element in dependentPlugins
|
||||||
|
// so we can safely assume that the freshly instantiated plugin is a dependency
|
||||||
|
//
|
||||||
|
// in that case let's find the first empty spot for a dependency
|
||||||
|
let didx = dependentPlugins[dependentPlugins.length - 1].uses.indexOf(false)
|
||||||
|
// assign the freshly instantiated plugin as that dependency
|
||||||
|
dependentPlugins[dependentPlugins.length - 1].uses[didx] = plugin
|
||||||
|
self.log('service-worker', `${pluginConfig.name}: assigning as dependency (#${didx}) to ${dependentPlugins[dependentPlugins.length - 1].name}`)
|
||||||
|
|
||||||
|
// was this the last one?
|
||||||
|
if (didx >= dependentPlugins[dependentPlugins.length - 1].uses.length - 1) {
|
||||||
|
// yup, last one!
|
||||||
|
self.log('service-worker', `${pluginConfig.name}: this was the last dependency of ${dependentPlugins[dependentPlugins.length - 1].name}`)
|
||||||
|
// we can now proceed to instantiate the last element of dependentPlugins
|
||||||
|
pluginConfig = dependentPlugins.pop()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// it is not the last one, so there should be more dependency plugins to instantiate first
|
||||||
|
// before we can instantiate the last of element of dependentPlugins
|
||||||
|
// but that requires the full treatment, including checing the `uses` field for their configs
|
||||||
|
self.log('service-worker', `${pluginConfig.name}: not yet the last dependency of ${dependentPlugins[dependentPlugins.length - 1].name}`)
|
||||||
|
pluginConfig = false
|
||||||
|
|
||||||
|
// if pluginConfig is not false, rinse-repeat the plugin instantiation steps
|
||||||
|
// since we are dealing with the last element of dependentPlugins
|
||||||
|
} while (pluginConfig !== false)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// inform
|
||||||
|
self.log('service-worker', `DEBUG: Strategy in use: ${self.LibResilientPlugins.map(p=>p.name).join(', ')}`)
|
||||||
|
|
||||||
|
} catch(e) {
|
||||||
|
// we only get a cryptic "Error while registering a service worker"
|
||||||
|
// unless we explicitly print the errors out in the console
|
||||||
|
console.error(e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -672,7 +679,8 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas
|
||||||
|* === Setting up the event handlers === *|
|
|* === Setting up the event handlers === *|
|
||||||
\* ========================================================================= */
|
\* ========================================================================= */
|
||||||
|
|
||||||
self.addEventListener('install', event => {
|
self.addEventListener('install', async (event) => {
|
||||||
|
event.waitUntil(initServiceWorker())
|
||||||
// TODO: Might we want to have a local cache?
|
// TODO: Might we want to have a local cache?
|
||||||
// "COMMIT_UNKNOWN" will be replaced with commit ID
|
// "COMMIT_UNKNOWN" will be replaced with commit ID
|
||||||
self.log('service-worker', "0. Installed LibResilient Service Worker (commit: COMMIT_UNKNOWN).");
|
self.log('service-worker', "0. Installed LibResilient Service Worker (commit: COMMIT_UNKNOWN).");
|
||||||
|
|
Ładowanie…
Reference in New Issue