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,17 +93,23 @@ 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
|
||||||
|
let initServiceWorker = async () => {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
// get the config
|
// get the config
|
||||||
//
|
//
|
||||||
// self.registration.scope contains the scope this service worker is registered for
|
// 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: 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 {
|
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.')
|
self.log('service-worker', 'config loaded.')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
self.log('service-worker', 'config loading failed, using defaults')
|
self.log('service-worker', 'config loading failed, using defaults')
|
||||||
|
@ -112,12 +118,12 @@ try {
|
||||||
// create the LibResilientPluginConstructors map
|
// create the LibResilientPluginConstructors map
|
||||||
// the global... hack is here so that we can run tests; not the most elegant
|
// the global... hack is here so that we can run tests; not the most elegant
|
||||||
// TODO: find a better way
|
// 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
|
// this is the stash for plugins that need dependencies instantiated first
|
||||||
var dependentPlugins = new Array()
|
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) {
|
while (self.LibResilientConfig.plugins.length > 0) {
|
||||||
|
|
||||||
// get the first plugin config from the array
|
// get the first plugin config from the array
|
||||||
|
@ -207,6 +213,7 @@ try {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
throw e
|
throw e
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fetch counter per clientId
|
* fetch counter per clientId
|
||||||
|
@ -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