very rough and PoC test implementation of JSON-based config (ref. #31)

merge-requests/23/head
Michał 'rysiek' Woźniak 2022-01-22 16:56:24 +00:00
rodzic 07693eeedd
commit d426dea050
6 zmienionych plików z 141 dodań i 117 usunięć

Wyświetl plik

@ -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"]
}

Wyświetl plik

@ -76,7 +76,7 @@ uses: config.uses
## 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:

Wyświetl plik

@ -499,7 +499,7 @@ if ('serviceWorker' in navigator) {
var serviceWorkerPath = scriptFolder + 'service-worker.js'
self.log('browser-side', 'Service Worker script at: ' + serviceWorkerPath)
// 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
navigator.serviceWorker.register(serviceWorkerPath, {
// TODO: what is the scope relative to? is it the HTML file that included it, or this script?

Wyświetl plik

@ -34,7 +34,7 @@ if (typeof window === 'undefined') {
let defaultConfig = {
// Gun nodes to use
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,
// the IPFS gateway we're using for verification when publishing; default is usually ok
ipfsGateway: 'https://gateway.ipfs.io'

Wyświetl plik

@ -26,7 +26,7 @@
// sane defaults
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,
// the IPFS gateway we're using for verification when publishing; default is usually ok
ipfsGateway: 'https://gateway.ipfs.io'

Wyświetl plik

@ -39,7 +39,7 @@ if (!Array.isArray(self.LibResilientPlugins)) {
// initialize the LibResilientConfig array
//
// 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) {
self.LibResilientConfig = {
// how long do we wait before we decide that a plugin is unresponsive,
@ -93,17 +93,23 @@ self.log = function(component, ...items) {
//
// everything in a try-catch block
// so that we get an informative message if there's an error
let initServiceWorker = async () => {
try {
// 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
// so it makes sense to pull config from `config.json` 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?
// 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.js")
//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')
@ -112,12 +118,12 @@ try {
// 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()
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.js could have changed the defaults)
// 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
@ -207,6 +213,7 @@ try {
console.error(e)
throw e
}
}
/**
* fetch counter per clientId
@ -672,7 +679,8 @@ let getResourceThroughLibResilient = (request, clientId, useStashed=true, doStas
|* === 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?
// "COMMIT_UNKNOWN" will be replaced with commit ID
self.log('service-worker', "0. Installed LibResilient Service Worker (commit: COMMIT_UNKNOWN).");