Skip to content
Snippets Groups Projects
Commit 0bf6c921 authored by Michał "rysiek" Woźniak's avatar Michał "rysiek" Woźniak :lock:
Browse files

Merge branch 'wip-config' into 'master'

Service Worker and plugin config in a separate config file

See merge request !27
parents fddef86e d9f4f80b
Branches
No related tags found
1 merge request!27Service Worker and plugin config in a separate config file
Pipeline #46526 failed
/*
* Samizdat config
*
* This is the config for Samizdat as deployed on the https://samizdat.is/ site
*
* When deploying Samizdat on your website you will need to create your own config,
* using this one as a template.
*/
// plugins config
self.SamizdatConfig.plugins["gateway-ipns"] = {
// the pubkey of the preconfigured IPNS node
ipnsPubkey: 'QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ'
}
self.SamizdatConfig.plugins["gun+ipfs"] = {
// the pubkey of the preconfigured Gun user
gunPubkey: 'WUK5ylwqqgUorceQRa84qfbBFhk7eNRDUoPbGK05SyE.-yohFhTzWPpDT-UDMuKGgemOUrw_cMMYWpy6plILqrg'
}
self.SamizdatConfig.plugins["ipns+ipfs"] = {
// the pubkey of the preconfigured Gun user
ipnsPubkey: 'QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ'
}
......@@ -2,6 +2,13 @@
|* === Stashing plugin using the Cache API === *|
\* ========================================================================= */
// no polluting of the global namespace please
(function () {
/*
* this plugin has no config settings
*/
/**
* getting content from cache
*/
......@@ -128,3 +135,6 @@ self.SamizdatPlugins.push({
stash: cacheContent,
unstash: clearCachedContent
})
// done with not poluting the global namespace
})()
......@@ -6,6 +6,13 @@
* this plugin does not implement any push method
*/
// no polluting of the global namespace please
(function () {
/*
* this plugin has no config settings
*/
/**
* getting content using regular HTTP(S) fetch()
*/
......@@ -62,3 +69,6 @@ self.SamizdatPlugins.push({
version: 'COMMIT_UNKNOWN',
fetch: fetchContent
})
// done with not poluting the global namespace
})()
......@@ -6,22 +6,40 @@
* this plugin does not implement any push method
*/
// the pubkey of the preconfigured IPNS node
const ipnsPubKey = 'QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ'
// no polluting of the global namespace please
(function () {
// this will become useful later
//const gatewaysJSONUrl = "https://ipfs.github.io/public-gateway-checker/gateways.json";
/*
* plugin config settings
*/
// sane defaults
let defaultConfig = {
// the pubkey of the preconfigured IPNS node; always needs to be set in config.js
ipnsPubkey: null,
// some default IPFS gateways to use
//
// important:
// we cannot use gateways that use hash directly in the (sub)domain:
// https://github.com/node-fetch/node-fetch/issues/260
const gateways = [
ipfsGateways: [
'https://ninetailed.ninja/ipns/', // Russia
'https://10.via0.com/ipns/', // USA
'https://ipfs.sloppyta.co/ipns/', // UK
'https://gateway.temporal.cloud/ipns/', // Germany
'https://ipfs.best-practice.se/ipns/' // Sweden
]
}
// merge the defaults with settings from SamizdatConfig
let config = {...defaultConfig, ...self.SamizdatConfig.plugins["gateway-ipns"]}
// reality check: Gun pubkey needs to be set to a non-empty string
if (typeof(config.ipnsPubkey) !== "string" || config.ipnsPubkey === "") {
let err = new Error("ipnsPubkey not confgured")
console.error(err)
throw err
}
/*
* to do this right we need a Promise.any() polyfill
......@@ -52,14 +70,12 @@ Promise.any = async (promises) => {
*/
let fetchContentFromGatewayIPNS = (url) => {
console.log(`Samizdat: pre-configured gateways:\n ${gateways.join('\n ')}`)
// we're going to try a random gateway and building an URL of the form:
// https://<gateway_address>/<ipnsPubkey>/<rest_of_URL>
var ipnsUrl = ipnsPubKey + url.replace(/https?:\/\/[^/]+/, '')
// https://<gateway_address>/<pubkey>/<rest_of_URL>
var ipnsUrl = config.ipnsPubkey + url.replace(/https?:\/\/[^/]+/, '')
// pick 3 gateways, at random
var sourceGateways = [...gateways]
var sourceGateways = [...config.ipfsGateways]
// if we have fewer than 3 gateways configured, use all of them
if (sourceGateways.length <= 3) {
var useGateways = sourceGateways
......@@ -132,3 +148,6 @@ self.SamizdatPlugins.push({
version: 'COMMIT_UNKNOWN',
fetch: fetchContentFromGatewayIPNS
})
// done with not poluting the global namespace
})()
......@@ -11,15 +11,30 @@
|* === General stuff and setup === *|
\* ========================================================================= */
// no polluting of the global namespace please
(function () {
var ipfs;
var gun;
var gunUser;
// the pubkey of the preconfigured Gun user
const gunPubKey = 'WUK5ylwqqgUorceQRa84qfbBFhk7eNRDUoPbGK05SyE.-yohFhTzWPpDT-UDMuKGgemOUrw_cMMYWpy6plILqrg'
// sane defaults
let defaultConfig = {
// the pubkey of the preconfigured Gun user; always needs to be set in config.js
gunPubkey: null,
// the IPFS gateway we're using for verification when publishing; default is usually ok
ipfsGateway: 'https://gateway.ipfs.io'
}
// merge the defaults with settings from SamizdatConfig
let config = {...defaultConfig, ...self.SamizdatConfig.plugins["gun+ipfs"]}
// the IPFS gateway we're using for verification
const IPFSGateway = 'https://gateway.ipfs.io'
// reality check: Gun pubkey needs to be set to a non-empty string
if (typeof(config.gunPubkey) !== "string" || config.gunPubkey === "") {
let err = new Error("gunPubkey not confgured")
console.error(err)
throw err
}
/**
* this is apparently needed by Gun
......@@ -73,7 +88,7 @@ async function setup_gun() {
if ( (gun !== false) && (gun !== undefined) && (gunUser === undefined) ) {
gunUser = false // we don't want to start a few times over
console.log('(COMMIT_UNKNOWN) Setting up gunUser...')
gunUser = gun.user(gunPubKey)
gunUser = gun.user(config.gunPubkey)
console.log('+-- Gun init complete :: gunUser is: ' + typeof gunUser);
}
}
......@@ -210,6 +225,7 @@ async function getContentFromGunAndIPFS(url) {
/* ========================================================================= *\
|* === Publishing stuff === *|
\* ========================================================================= */
/*
* these are used for adding content to IPFS and Gun
*/
......@@ -271,14 +287,14 @@ let addToIPFS = (resources) => {
let verifyInIPFS = (ipfs_addresses) => {
return new Promise((resolve, reject) => {
console.log('Checking IPFS content against a gateway...')
console.log('+-- gateway in use: ' + IPFSGateway)
console.log('+-- gateway in use: ' + config.ipfsGateway)
// get the list of IPFS addresses
var updatedPaths = Object.values(ipfs_addresses)
for (path in ipfs_addresses) {
// start the fetch
fetch(IPFSGateway + ipfs_addresses[path])
fetch(config.ipfsGateway + ipfs_addresses[path])
.then((response) => {
ipfsaddr = response.url.replace(IPFSGateway, '')
ipfsaddr = response.url.replace(config.ipfsGateway, '')
if (response.ok) {
console.log('+-- verified: ' + ipfsaddr)
var pathIndex = updatedPaths.indexOf(ipfsaddr)
......@@ -305,7 +321,7 @@ let verifyInIPFS = (ipfs_addresses) => {
/**
* auth a Gun admin user
* (and verify it's the correct one with regards to the configured gunPubKey)
* (and verify it's the correct one with regards to the configured config.gunPubkey)
*/
let authGunAdmin = (user, pass) => {
return new Promise((resolve, reject) => {
......@@ -317,7 +333,7 @@ let authGunAdmin = (user, pass) => {
if (userReference.err) {
reject(new Error(userReference.err))
// reality check -- does it match our preconfigured pubkey?
} else if (gu._.soul.slice(1) === gunPubKey) {
} else if (gu._.soul.slice(1) === config.gunPubkey) {
console.log('Gun Admin user authenticated using password.');
// we need to keep the reference to g, otherwise gu becomes unusable
var gApi = {
......@@ -439,3 +455,6 @@ self.SamizdatPlugins.push({
fetch: getContentFromGunAndIPFS,
publish: publishContent
})
// done with not poluting the global namespace
})()
......@@ -18,13 +18,28 @@
|* === General stuff and setup === *|
\* ========================================================================= */
// no polluting of the global namespace please
(function () {
var ipfs;
// the pubkey of the preconfigured IPNS node
const ipnsPubKey = 'QmYGVgGGfD5N4Xcc78CcMJ99dKcH6K6myhd4Uenv5yJwiJ'
// sane defaults
let defaultConfig = {
// the pubkey of the preconfigured IPNS node; always needs to be set in config.js
ipnsPubkey: null,
// the IPFS gateway we're using for verification when publishing; default is usually ok
ipfsGateway: 'https://gateway.ipfs.io'
}
// the IPFS gateway we're using for verification
const IPFSGateway = 'https://gateway.ipfs.io'
// merge the defaults with settings from SamizdatConfig
let config = {...defaultConfig, ...self.SamizdatConfig.plugins["ipns+ipfs"]}
// reality check: Gun pubkey needs to be set to a non-empty string
if (typeof(config.ipnsPubkey) !== "string" || config.ipnsPubkey === "") {
let err = new Error("ipnsPubkey not confgured")
console.error(err)
throw err
}
/**
* importing stuff works differently between a browser window context
......@@ -232,14 +247,14 @@ let verifyInIPFS = (ipfs_addresses) => {
return new Promise((resolve, reject) => {
console.log('Checking IPFS content against a gateway...')
console.log('+-- gateway in use: ' + IPFSGateway)
console.log('+-- gateway in use: ' + config.ipfsGateway)
// get the list of IPFS addresses
var updatedPaths = Object.values(ipfs_addresses)
for (path in ipfs_addresses) {
// start the fetch
fetch(IPFSGateway + ipfs_addresses[path])
fetch(config.ipfsGateway + ipfs_addresses[path])
.then((response) => {
ipfsaddr = response.url.replace(IPFSGateway, '')
ipfsaddr = response.url.replace(config.ipfsGateway, '')
if (response.ok) {
console.log('+-- verified: ' + ipfsaddr)
var pathIndex = updatedPaths.indexOf(ipfsaddr)
......@@ -323,3 +338,6 @@ self.SamizdatPlugins.push({
fetch: getContentFromIPNSAndIPFS,
publish: publishContent
})
// done with not poluting the global namespace
})()
/*
* Samizdat Service Worker.
*
* Strategy (not fully implemented yet):
* Default strategy:
* 1. Try to load from main website.
* 2. If loading fails, load from Samizdat.
* 3. If loading is too slow, load from Samizdat.
......@@ -14,17 +14,37 @@ if (!Array.isArray(self.SamizdatPlugins)) {
self.SamizdatPlugins = new Array()
}
// initialize the SamizdatConfig array
//
// this also sets some sane defaults,
// which then can be modified via config.js
if (typeof self.SamizdatConfig !== 'object' || self.SamizdatConfig === null) {
self.SamizdatConfig = {
// how long do we wait before we decide that a plugin is unresponsive,
// and move on?
defaultPluginTimeout: 10000,
// plugins settings namespace
plugins: {}
}
}
// load the plugins
//
// order in which plugins are loaded defines the order
// in which they are called!
try {
self.importScripts(
"./config.js",
"./plugins/fetch.js",
"./plugins/cache.js",
"./plugins/gateway-ipns.js",
"./plugins/gun-ipfs.js");
console.log('(COMMIT_UNKNOWN) SamizdatPlugins.length:', self.SamizdatPlugins.length)
} 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
}
/**
* fetch counter per clientId
......@@ -246,7 +266,11 @@ let samizdatFetch = (plugin, url, reqInfo) => {
// run the plugin
return Promise.race([
plugin.fetch(url),
promiseTimeout(10000, false, `Samizdat request using ${plugin.name} timed out.`)
promiseTimeout(
self.SamizdatConfig.defaultPluginTimeout,
false,
`Samizdat request using ${plugin.name} timed out after ${self.SamizdatConfig.defaultPluginTimeout}ms.`
)
])
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment