gun-ipfs plugin ready for new plugin loading system (ref. #15)

merge-requests/9/merge
Michał 'rysiek' Woźniak 2021-09-15 22:27:01 +00:00
rodzic 05e7997e89
commit d8b6f64d4a
2 zmienionych plików z 513 dodań i 504 usunięć

Wyświetl plik

@ -4,17 +4,16 @@ describe("plugin: gun-ipfs", () => {
beforeEach(() => { beforeEach(() => {
Object.assign(global, makeServiceWorkerEnv()); Object.assign(global, makeServiceWorkerEnv());
jest.resetModules(); jest.resetModules();
self.LibResilientPlugins = new Array() init = {
self.LibResilientConfig = { name: 'gun-ipfs',
plugins: { gunPubkey: 'stub'
'gun-ipfs':{
'gunPubkey': 'stub'
} }
} global.LibResilientPluginConstructors = new Map()
} LR = {
self.log = jest.fn((component, ...items)=>{ log: jest.fn((component, ...items)=>{
console.debug(component + ' :: ', ...items) console.debug(component + ' :: ', ...items)
}) })
}
global.Ipfs = { global.Ipfs = {
create: ()=>{ create: ()=>{
return Promise.resolve({ return Promise.resolve({
@ -71,18 +70,24 @@ describe("plugin: gun-ipfs", () => {
test("it should register in LibResilientPlugins", () => { test("it should register in LibResilientPlugins", () => {
require("../../plugins/gun-ipfs.js"); require("../../plugins/gun-ipfs.js");
expect(self.LibResilientPlugins[0].name).toEqual('gun-ipfs'); expect(LibResilientPluginConstructors.get('gun-ipfs')(LR, init).name).toEqual('gun-ipfs');
}); });
test("IPFS setup should be initiated", ()=>{ test("IPFS setup should be initiated", async ()=>{
self.importScripts = jest.fn() self.importScripts = jest.fn()
require("../../plugins/gun-ipfs.js"); require("../../plugins/gun-ipfs.js");
try {
await LibResilientPluginConstructors.get('gun-ipfs')(LR, init).fetch('/test.json')
} catch {}
expect(self.importScripts).toHaveBeenNthCalledWith(1, './lib/ipfs.js') expect(self.importScripts).toHaveBeenNthCalledWith(1, './lib/ipfs.js')
}) })
test("Gun setup should be initiated", ()=>{ test("Gun setup should be initiated", async ()=>{
self.importScripts = jest.fn() self.importScripts = jest.fn()
require("../../plugins/gun-ipfs.js"); require("../../plugins/gun-ipfs.js");
try {
await LibResilientPluginConstructors.get('gun-ipfs')(LR, init).fetch('/test.json')
} catch {}
expect(self.importScripts).toHaveBeenNthCalledWith(2, "./lib/gun.js", "./lib/sea.js", "./lib/webrtc.js") expect(self.importScripts).toHaveBeenNthCalledWith(2, "./lib/gun.js", "./lib/sea.js", "./lib/webrtc.js")
}) })
@ -91,7 +96,7 @@ describe("plugin: gun-ipfs", () => {
expect.assertions(1) expect.assertions(1)
try { try {
await self.LibResilientPlugins[0].fetch(self.location.origin + '/test.json') await LibResilientPluginConstructors.get('gun-ipfs')(LR, init).fetch(self.location.origin + '/test.json')
} catch(e) { } catch(e) {
expect(e).toEqual(new Error('IPFS address is undefined for: /test.json')) expect(e).toEqual(new Error('IPFS address is undefined for: /test.json'))
} }
@ -102,7 +107,7 @@ describe("plugin: gun-ipfs", () => {
expect.assertions(1) expect.assertions(1)
try { try {
await self.LibResilientPlugins[0].fetch(self.location.origin + '/test/') await LibResilientPluginConstructors.get('gun-ipfs')(LR, init).fetch(self.location.origin + '/test/')
} catch(e) { } catch(e) {
expect(e).toEqual(new Error('IPFS address is undefined for: /test/index.html')) expect(e).toEqual(new Error('IPFS address is undefined for: /test/index.html'))
} }
@ -110,47 +115,47 @@ describe("plugin: gun-ipfs", () => {
test("content types should be guessed correctly when fetching", async ()=>{ test("content types should be guessed correctly when fetching", async ()=>{
require("../../plugins/gun-ipfs.js"); require("../../plugins/gun-ipfs.js");
var gunipfsPlugin = LibResilientPluginConstructors.get('gun-ipfs')(LR, init)
try {
await gunipfsPlugin.fetch(self.location.origin + '/test/')
} catch(e) {}
expect(LR.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : text/html")
LR.log.mockClear()
try { try {
await self.LibResilientPlugins[0].fetch(self.location.origin + '/test/') await gunipfsPlugin.fetch(self.location.origin + '/test.htm')
} catch(e) {} } catch(e) {}
expect(self.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : text/html") expect(LR.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : text/html")
self.log.mockClear() LR.log.mockClear()
try { try {
await self.LibResilientPlugins[0].fetch(self.location.origin + '/test.htm') await gunipfsPlugin.fetch(self.location.origin + '/test.css')
} catch(e) {} } catch(e) {}
expect(self.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : text/html") expect(LR.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : text/css")
self.log.mockClear() LR.log.mockClear()
try { try {
await self.LibResilientPlugins[0].fetch(self.location.origin + '/test.css') await gunipfsPlugin.fetch(self.location.origin + '/test.js')
} catch(e) {} } catch(e) {}
expect(self.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : text/css") expect(LR.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : text/javascript")
self.log.mockClear() LR.log.mockClear()
try { try {
await self.LibResilientPlugins[0].fetch(self.location.origin + '/test.js') await gunipfsPlugin.fetch(self.location.origin + '/test.json')
} catch(e) {} } catch(e) {}
expect(self.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : text/javascript") expect(LR.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : application/json")
self.log.mockClear() LR.log.mockClear()
try { try {
await self.LibResilientPlugins[0].fetch(self.location.origin + '/test.json') await gunipfsPlugin.fetch(self.location.origin + '/test.svg')
} catch(e) {} } catch(e) {}
expect(self.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : application/json") expect(LR.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : image/svg+xml")
self.log.mockClear() LR.log.mockClear()
try { try {
await self.LibResilientPlugins[0].fetch(self.location.origin + '/test.svg') await gunipfsPlugin.fetch(self.location.origin + '/test.ico')
} catch(e) {} } catch(e) {}
expect(self.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : image/svg+xml") expect(LR.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : image/x-icon")
self.log.mockClear()
try {
await self.LibResilientPlugins[0].fetch(self.location.origin + '/test.ico')
} catch(e) {}
expect(self.log).toHaveBeenCalledWith('gun-ipfs', " +-- guessed contentType : image/x-icon")
}) })
test("fetching should work (stub!)", async ()=>{ test("fetching should work (stub!)", async ()=>{
@ -169,24 +174,25 @@ describe("plugin: gun-ipfs", () => {
}) })
require("../../plugins/gun-ipfs.js"); require("../../plugins/gun-ipfs.js");
await self.Ipfs.create() //await self.Ipfs.create()
let response = await self.LibResilientPlugins[0].fetch(self.location.origin + '/test.json') let response = await LibResilientPluginConstructors.get('gun-ipfs')(LR, init).fetch(self.location.origin + '/test.json')
expect(response.body.type).toEqual('application/json') expect(response.body.type).toEqual('application/json')
expect(String.fromCharCode.apply(null, response.body.parts[0])).toEqual('{test: "success"}') expect(String.fromCharCode.apply(null, response.body.parts[0])).toEqual('{test: "success"}')
}) })
test("publishContent should error out if passed anything else than string or array of string", async ()=>{ test("publishContent should error out if passed anything else than string or array of string", async ()=>{
require("../../plugins/gun-ipfs.js"); require("../../plugins/gun-ipfs.js");
var gunipfsPlugin = LibResilientPluginConstructors.get('gun-ipfs')(LR, init)
expect(()=>{ expect(()=>{
self.LibResilientPlugins[0].publish({ gunipfsPlugin.publish({
url: self.location.origin + '/test.json' url: self.location.origin + '/test.json'
}) })
}).toThrow('Handling a Response: not implemented yet') }).toThrow('Handling a Response: not implemented yet')
expect(()=>{ expect(()=>{
self.LibResilientPlugins[0].publish(true) gunipfsPlugin.publish(true)
}).toThrow('Only accepts: string, Array of string, Response.') }).toThrow('Only accepts: string, Array of string, Response.')
expect(()=>{ expect(()=>{
self.LibResilientPlugins[0].publish([true, 5]) gunipfsPlugin.publish([true, 5])
}).toThrow('Only accepts: string, Array of string, Response.') }).toThrow('Only accepts: string, Array of string, Response.')
}) })
}); });

Wyświetl plik

@ -21,7 +21,10 @@ if (typeof window === 'undefined') {
\* ========================================================================= */ \* ========================================================================= */
// no polluting of the global namespace please // no polluting of the global namespace please
(function () { (function(LRPC){
// this never changes
const pluginName = "gun-ipfs"
LRPC.set(pluginName, (LR, init={})=>{
var ipfs; var ipfs;
var gun; var gun;
@ -29,9 +32,6 @@ if (typeof window === 'undefined') {
// sane defaults // sane defaults
let defaultConfig = { let defaultConfig = {
// name of this plugin
// should not be changed
name: "gun-ipfs",
// 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.js
@ -40,8 +40,8 @@ if (typeof window === 'undefined') {
ipfsGateway: 'https://gateway.ipfs.io' ipfsGateway: 'https://gateway.ipfs.io'
} }
// merge the defaults with settings from LibResilientConfig // merge the defaults with settings from init
let config = {...defaultConfig, ...self.LibResilientConfig.plugins[defaultConfig.name]} let config = {...defaultConfig, ...init}
// reality check: Gun pubkey needs to be set to a non-empty string // reality check: Gun pubkey needs to be set to a non-empty string
if (typeof(config.gunPubkey) !== "string" || config.gunPubkey === "") { if (typeof(config.gunPubkey) !== "string" || config.gunPubkey === "") {
@ -56,13 +56,13 @@ if (typeof window === 'undefined') {
*/ */
function doImport() { function doImport() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
self.log(config.name, `doImport()\n+-- ${args.join('\n+-- ')}`) LR.log(pluginName, `doImport()\n+-- ${args.join('\n+-- ')}`)
if (typeof self.importScripts !== 'undefined') { if (typeof self.importScripts !== 'undefined') {
self.log(config.name, `+-- self.importScripts.apply()`) LR.log(pluginName, `+-- self.importScripts.apply()`)
self.importScripts.apply(self, args) self.importScripts.apply(self, args)
} else { } else {
self.log( LR.log(
config.name, pluginName,
'assuming these scripts are already included:\n', 'assuming these scripts are already included:\n',
args.join('\n+-- ') args.join('\n+-- ')
) )
@ -72,13 +72,13 @@ if (typeof window === 'undefined') {
async function setup_ipfs() { async function setup_ipfs() {
if (ipfs === undefined) { if (ipfs === undefined) {
ipfs = false // we don't want to start a few times over ipfs = false // we don't want to start a few times over
self.log(config.name, 'importing IPFS-related libraries...'); LR.log(pluginName, 'importing IPFS-related libraries...');
doImport( doImport(
"./lib/ipfs.js"); "./lib/ipfs.js");
self.log(config.name, 'setting up IPFS...') LR.log(pluginName, 'setting up IPFS...')
try { try {
ipfs = await self.Ipfs.create(); ipfs = await self.Ipfs.create();
self.log(config.name, '+-- IPFS loaded :: ipfs is : ' + typeof ipfs) LR.log(pluginName, '+-- IPFS loaded :: ipfs is : ' + typeof ipfs)
} catch(e) { } catch(e) {
console.error('+-- Error loading IPFS: ' + e) console.error('+-- Error loading IPFS: ' + e)
} }
@ -86,10 +86,10 @@ if (typeof window === 'undefined') {
} }
async function setup_gun() { async function setup_gun() {
self.log(config.name, 'setup_gun()') LR.log(pluginName, 'setup_gun()')
if (gun === undefined) { if (gun === undefined) {
gun = false // we don't want to start a few times over gun = false // we don't want to start a few times over
self.log(config.name, 'importing Gun-related libraries...'); LR.log(pluginName, 'importing Gun-related libraries...');
try { try {
doImport( doImport(
"./lib/gun.js", "./lib/gun.js",
@ -98,20 +98,20 @@ if (typeof window === 'undefined') {
} catch(e) { } catch(e) {
console.error(e) console.error(e)
} }
self.log(config.name, 'setting up Gun...') LR.log(pluginName, 'setting up Gun...')
try { try {
gun = Gun(config.gunNodes); gun = Gun(config.gunNodes);
self.log(config.name, '+-- gun loaded :: gun is : ' + typeof gun); LR.log(pluginName, '+-- gun loaded :: gun is : ' + typeof gun);
} catch(e) { } catch(e) {
self.log(config.name, 'Error setting up Gun: ' + e); LR.log(pluginName, 'Error setting up Gun: ' + e);
} }
} }
if ( (gun !== false) && (gun !== undefined) ) { if ( (gun !== false) && (gun !== undefined) ) {
if (gunUser === undefined) { if (gunUser === undefined) {
gunUser = false // we don't want to start a few times over gunUser = false // we don't want to start a few times over
self.log(config.name, 'setting up gunUser...') LR.log(pluginName, 'setting up gunUser...')
gunUser = gun.user(config.gunPubkey) gunUser = gun.user(config.gunPubkey)
self.log(config.name, '+-- gun init complete :: gunUser is: ' + typeof gunUser); LR.log(pluginName, '+-- gun init complete :: gunUser is: ' + typeof gunUser);
} }
} else { } else {
console.error("at this point Gun should have been set up already, but isn't!") console.error("at this point Gun should have been set up already, but isn't!")
@ -119,13 +119,13 @@ if (typeof window === 'undefined') {
} }
async function setup_gun_ipfs() { async function setup_gun_ipfs() {
self.log(config.name, 'setup_gun_ipfs()') LR.log(pluginName, 'setup_gun_ipfs()')
if (ipfs === undefined || gun === undefined) { if (ipfs === undefined || gun === undefined) {
self.log(config.name, 'setting up...') LR.log(pluginName, 'setting up...')
setup_ipfs(); await setup_ipfs();
setup_gun(); await setup_gun();
} else { } else {
self.log(config.name, 'setup already underway (ipfs: ' + ( (ipfs) ? 'done' : 'loading' ) + ', gun: ' + ( (gun) ? 'done' : 'loading' ) + ')') LR.log(pluginName, 'setup already underway (ipfs: ' + ( (ipfs) ? 'done' : 'loading' ) + ', gun: ' + ( (gun) ? 'done' : 'loading' ) + ')')
} }
} }
@ -137,8 +137,8 @@ if (typeof window === 'undefined') {
let getGunData = (gunaddr) => { let getGunData = (gunaddr) => {
return new Promise( return new Promise(
(resolve, reject) => { (resolve, reject) => {
self.log( LR.log(
config.name, pluginName,
'getGunData()\n', 'getGunData()\n',
`+-- gunUser : ${typeof gunUser}\n`, `+-- gunUser : ${typeof gunUser}\n`,
`+-- gunaddr[] : ${gunaddr}` `+-- gunaddr[] : ${gunaddr}`
@ -150,7 +150,7 @@ if (typeof window === 'undefined') {
.get(gunaddr[1]) .get(gunaddr[1])
.once(function(addr){ .once(function(addr){
if (typeof addr !== 'undefined') { if (typeof addr !== 'undefined') {
self.log(config.name, `IPFS address: "${addr}"`); LR.log(pluginName, `IPFS address: "${addr}"`);
resolve(addr); resolve(addr);
} else { } else {
// looks like we didn't get anything // looks like we didn't get anything
@ -166,6 +166,9 @@ if (typeof window === 'undefined') {
* the workhorse of this plugin * the workhorse of this plugin
*/ */
async function getContentFromGunAndIPFS(url) { async function getContentFromGunAndIPFS(url) {
await setup_gun_ipfs();
var urlArray = url.replace(/https?:\/\//, '').split('/') var urlArray = url.replace(/https?:\/\//, '').split('/')
var gunaddr = [urlArray[0], '/' + urlArray.slice(1).join('/')] var gunaddr = [urlArray[0], '/' + urlArray.slice(1).join('/')]
@ -173,13 +176,13 @@ if (typeof window === 'undefined') {
* if the gunaddr[1] ends in '/', append 'index.html' to it * if the gunaddr[1] ends in '/', append 'index.html' to it
*/ */
if (gunaddr[1].charAt(gunaddr[1].length - 1) === '/') { if (gunaddr[1].charAt(gunaddr[1].length - 1) === '/') {
self.log(config.name, "path ends in '/', assuming 'index.html' should be appended."); LR.log(pluginName, "path ends in '/', assuming 'index.html' should be appended.");
gunaddr[1] += 'index.html'; gunaddr[1] += 'index.html';
} }
// inform // inform
self.log( LR.log(
config.name, pluginName,
`starting Gun lookup of: ${gunaddr.join(', ')}\n`, `starting Gun lookup of: ${gunaddr.join(', ')}\n`,
`+-- gun : ${typeof gun}\n`, `+-- gun : ${typeof gun}\n`,
`+-- gunUser : ${typeof gunUser}` `+-- gunUser : ${typeof gunUser}`
@ -211,10 +214,11 @@ if (typeof window === 'undefined') {
contentType = 'image/x-icon'; contentType = 'image/x-icon';
break; break;
} }
self.log(config.name, " +-- guessed contentType : " + contentType); LR.log(pluginName, " +-- guessed contentType : " + contentType);
return getGunData(gunaddr).then(ipfsaddr => { return getGunData(gunaddr).then(ipfsaddr => {
self.log(config.name, `starting IPFS lookup of: '${ipfsaddr}'`); LR.log(pluginName, `starting IPFS lookup of: '${ipfsaddr}'`);
LR.log(pluginName, `ipfs is: '${ipfs}'`);
return ipfs.get(ipfsaddr).next(); return ipfs.get(ipfsaddr).next();
}).then(file => { }).then(file => {
// we only need one // we only need one
@ -232,7 +236,7 @@ if (typeof window === 'undefined') {
return content return content
} }
return getContent(file.value.content).then((content)=>{ return getContent(file.value.content).then((content)=>{
self.log(config.name, `got a Gun-addressed IPFS-stored file: ${file.value.path}\n+-- content is: ${typeof content}`); LR.log(pluginName, `got a Gun-addressed IPFS-stored file: ${file.value.path}\n+-- content is: ${typeof content}`);
// creating and populating the blob // creating and populating the blob
var blob = new Blob( var blob = new Blob(
[content], [content],
@ -247,7 +251,7 @@ if (typeof window === 'undefined') {
'headers': { 'headers': {
'Content-Type': contentType, 'Content-Type': contentType,
'ETag': file.value.path, 'ETag': file.value.path,
'X-LibResilient-Method': config.name, 'X-LibResilient-Method': pluginName,
'X-LibResilient-ETag': file.value.path 'X-LibResilient-ETag': file.value.path
} }
} }
@ -276,12 +280,12 @@ if (typeof window === 'undefined') {
let addToIPFS = (resources) => { let addToIPFS = (resources) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.log(config.name, "adding to IPFS...") LR.log(pluginName, "adding to IPFS...")
self.log(config.name, "+-- number of resources:", resources.length) LR.log(pluginName, "+-- number of resources:", resources.length)
var ipfs_addresses = new Map(); var ipfs_addresses = new Map();
resources.forEach(function(res){ resources.forEach(function(res){
self.log(config.name, " +-- handling internal resource:", res) LR.log(pluginName, " +-- handling internal resource:", res)
ipfs.add(Ipfs.urlSource(res)) ipfs.add(Ipfs.urlSource(res))
.then((result) => { .then((result) => {
@ -291,7 +295,7 @@ if (typeof window === 'undefined') {
// what we need in ipfs_addresses is in fact the absolute path (no domain, no scheme) // what we need in ipfs_addresses is in fact the absolute path (no domain, no scheme)
var abs_path = res.replace(window.location.origin, '') var abs_path = res.replace(window.location.origin, '')
ipfs_addresses.set(abs_path, '/ipfs/' + result.cid.string) ipfs_addresses.set(abs_path, '/ipfs/' + result.cid.string)
self.log(config.name, "added to IPFS: " + abs_path + ' as ' + ipfs_addresses.get(abs_path)) LR.log(pluginName, "added to IPFS: " + abs_path + ' as ' + ipfs_addresses.get(abs_path))
// if we seem to have all we need, resolve! // if we seem to have all we need, resolve!
if (ipfs_addresses.size === resources.length) resolve(ipfs_addresses); if (ipfs_addresses.size === resources.length) resolve(ipfs_addresses);
}) })
@ -322,8 +326,8 @@ if (typeof window === 'undefined') {
*/ */
let verifyInIPFS = (ipfs_addresses) => { let verifyInIPFS = (ipfs_addresses) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
self.log(config.name, 'checking IPFS content against a gateway...') LR.log(pluginName, 'checking IPFS content against a gateway...')
self.log(config.name, '+-- gateway in use: ' + config.ipfsGateway) LR.log(pluginName, '+-- gateway in use: ' + config.ipfsGateway)
// get the list of IPFS addresses // get the list of IPFS addresses
var updatedPaths = Array.from(ipfs_addresses.values()) var updatedPaths = Array.from(ipfs_addresses.values())
for (const path of ipfs_addresses.keys()) { for (const path of ipfs_addresses.keys()) {
@ -332,13 +336,13 @@ if (typeof window === 'undefined') {
.then((response) => { .then((response) => {
ipfsaddr = response.url.replace(config.ipfsGateway, '') ipfsaddr = response.url.replace(config.ipfsGateway, '')
if (response.ok) { if (response.ok) {
self.log(config.name, '+-- verified: ' + ipfsaddr) LR.log(pluginName, '+-- verified: ' + ipfsaddr)
var pathIndex = updatedPaths.indexOf(ipfsaddr) var pathIndex = updatedPaths.indexOf(ipfsaddr)
if (pathIndex > -1) { if (pathIndex > -1) {
updatedPaths.splice(pathIndex, 1) updatedPaths.splice(pathIndex, 1)
} }
if (updatedPaths.length === 0) { if (updatedPaths.length === 0) {
self.log(config.name, 'all updates confirmed successful!') LR.log(pluginName, 'all updates confirmed successful!')
resolve(ipfs_addresses); resolve(ipfs_addresses);
} }
} else { } else {
@ -370,7 +374,7 @@ if (typeof window === 'undefined') {
reject(new Error(userReference.err)) reject(new Error(userReference.err))
// reality check -- does it match our preconfigured pubkey? // reality check -- does it match our preconfigured pubkey?
} else if (gu._.soul.slice(1) === config.gunPubkey) { } else if (gu._.soul.slice(1) === config.gunPubkey) {
self.log(config.name, 'Gun admin user authenticated using password.'); LR.log(pluginName, 'Gun admin user authenticated using password.');
// we need to keep the reference to g, otherwise gu becomes unusable // we need to keep the reference to g, otherwise gu becomes unusable
var gApi = { var gApi = {
user: gu, user: gu,
@ -391,7 +395,7 @@ if (typeof window === 'undefined') {
// we need an authenticated Gun user // we need an authenticated Gun user
return authGunAdmin(user, pass) return authGunAdmin(user, pass)
.then((gunAPI) => { .then((gunAPI) => {
self.log(config.name, '+-- adding new IPFS addresses to Gun...') LR.log(pluginName, '+-- adding new IPFS addresses to Gun...')
// TODO BUG: this will probably fail, or cause failures down the line // TODO BUG: this will probably fail, or cause failures down the line
// TODO BUG: we're setting a Map() object here; it's unclear how GunDB // TODO BUG: we're setting a Map() object here; it's unclear how GunDB
// TODO BUG: will handle this // TODO BUG: will handle this
@ -409,10 +413,10 @@ if (typeof window === 'undefined') {
*/ */
.then((gunAPI) => { .then((gunAPI) => {
// get the paths // get the paths
self.log(config.name, '+-- starting verification of updated Gun data...') LR.log(pluginName, '+-- starting verification of updated Gun data...')
var updatedPaths = Array.from(ipfs_addresses.values()) var updatedPaths = Array.from(ipfs_addresses.values())
for (const path of ipfs_addresses) { for (const path of ipfs_addresses) {
self.log(config.name, ' +-- watching: ' + path) LR.log(pluginName, ' +-- watching: ' + path)
//debuglog('watching path for updates:', path) //debuglog('watching path for updates:', path)
// using the global gunUser to check if updates propagated // using the global gunUser to check if updates propagated
gunUser.get(window.location.host).get(path).on(function(updaddr, updpath){ gunUser.get(window.location.host).get(path).on(function(updaddr, updpath){
@ -422,13 +426,13 @@ if (typeof window === 'undefined') {
if (ipfs_addresses.get(updpath) == updaddr) { if (ipfs_addresses.get(updpath) == updaddr) {
// update worked! // update worked!
gunUser.get(window.location.host).get(updpath).off() gunUser.get(window.location.host).get(updpath).off()
self.log(config.name, '+-- update confirmed for:', updpath, '[' + updaddr + ']') LR.log(pluginName, '+-- update confirmed for:', updpath, '[' + updaddr + ']')
var pathIndex = updatedPaths.indexOf(updpath) var pathIndex = updatedPaths.indexOf(updpath)
if (pathIndex > -1) { if (pathIndex > -1) {
updatedPaths.splice(pathIndex, 1) updatedPaths.splice(pathIndex, 1)
} }
if (updatedPaths.length === 0) { if (updatedPaths.length === 0) {
self.log(config.name, 'all updates confirmed successful!') LR.log(pluginName, 'all updates confirmed successful!')
return true; return true;
} }
} }
@ -483,18 +487,17 @@ if (typeof window === 'undefined') {
|* === Initialization === *| |* === Initialization === *|
\* ========================================================================= */ \* ========================================================================= */
// we probably need to handle this better
setup_gun_ipfs();
// and add ourselves to it // and add ourselves to it
// with some additional metadata // with some additional metadata
self.LibResilientPlugins.push({ return {
name: config.name, name: pluginName,
description: 'Decentralized resource fetching using Gun for address resolution and IPFS for content delivery.', description: 'Decentralized resource fetching using Gun for address resolution and IPFS for content delivery.',
version: 'COMMIT_UNKNOWN', version: 'COMMIT_UNKNOWN',
fetch: getContentFromGunAndIPFS, fetch: getContentFromGunAndIPFS,
publish: publishContent publish: publishContent
}) }
})
// done with not polluting the global namespace // done with not polluting the global namespace
})() })(LibResilientPluginConstructors)