1 Commits

Author SHA1 Message Date
e80da2945c add open and create functions 2021-04-05 21:09:22 +02:00

175
bug.js
View File

@@ -1,144 +1,61 @@
const IPFS = require("ipfs-core")
const OrbitDB = require("orbit-db") const OrbitDB = require("orbit-db")
const IpfsClient = require("ipfs-http-client") const IpfsClient = require("ipfs-http-client")
var ipfs = undefined var ipfs
var orbitdb
const ipfs_config = { async function init() {
preload: { ipfs = IpfsClient({ host: 'localhost', port: '5001', protocol: 'http' })
enabled: false orbitdb = await OrbitDB.createInstance(ipfs)
}, }
EXPERIMENTAL: {
pubsub: true // Creates a certain number of feed databases
}, // and fills them up with three text values
config: { async function createManyDbs(number) {
Addresses: { await init()
API: '/ip4/127.0.0.1/tcp/0',
Swarm: ['/ip4/0.0.0.0/tcp/0'], let dbs = []
Gateway: '/ip4/0.0.0.0/tcp/0' for(let i = 0; i < number; i++) {
}, let db = await orbitdb.feed("Test" + i)
Bootstrap: [], await db.load()
Discovery: { dbs.push(db)
MDNS: {
Enabled: true, for(let j = 0; j<3; j++) {
Interval: 0 await db.add({text: "Tweet" + j})
},
webRTCStar: {
Enabled: false
}
} }
} }
// Log addresses
console.log("Addresses:")
console.log(dbs.map(db => db.id))
} }
const defaultIpfsConfig = { // Loads a number of feed dbs from their addresses
preload: { enabled: false }, async function openManyDbs(addresses) {
config: { await init()
Addresses: {
Swarm: [
// Use IPFS dev signal server
// Websocket:
// '/dns4/ws-star-signal-1.servep2p.com/tcp/443/wss/p2p-websocket-star',
// '/dns4/ws-star-signal-2.servep2p.com/tcp/443/wss/p2p-websocket-star',
// '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star',
// WebRTC:
// '/dns4/star-signal.cloud.ipfs.team/wss/p2p-webrtc-star',
'/dns4/wrtc-star1.par.dwebops.pub/tcp/443/wss/p2p-webrtc-star/',
'/dns4/wrtc-star2.sjc.dwebops.pub/tcp/443/wss/p2p-webrtc-star/',
'/dns4/webrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star/',
// Use local signal server
// '/ip4/0.0.0.0/tcp/9090/wss/p2p-webrtc-star',
]
}
},
EXPERIMENTAL: {
pubsub: true
}
}
let data = []
let dbs = []
var orbitdb = undefined for(let address of addresses) {
let db = await orbitdb.open(address)
await db.load()
dbs.push(db)
async function init_ipfs() { let elements = await db.iterator({ reverse: true, limit: -1}).collect()
try { let myData = elements.map((element) => element.payload.value.text)
ipfs = await IpfsClient({ host: 'localhost', port: '5001', protocol: 'http' }) data.push({name: db.dbname, data: myData})
await ipfs.config.getAll() // Necessary to do a call to make code throw in case of CORS error...
console.log("Succesfully connected to existing node.")
} catch (e) {
console.log("Error while trying to connect to local IPFS node.")
console.log("Now running js-ipfs node...")
try {
ipfs = await IPFS.create(ipfs_config)
} catch (e) {
console.log("Error initializing js-ipfs node.")
console.log(e)
}
}
}
async function init_orbitdb() {
try {
orbitdb = await OrbitDB.createInstance(ipfs)
} catch (e) {
console.log(e)
}
}
async function createDBs() {
let db1 = await orbitdb.feed("db1")
await db1.load()
let db2 = await orbitdb.feed("db2")
await db2.load()
return { db1: db1, db2: db2 }
}
async function openExternalDBs(name = "Test", initialize_tweets_in_tweetDB = false) {
await init_ipfs()
await init_orbitdb()
let dbs
if(initialize_tweets_in_tweetDB) {
await putTweets(dbs)
} }
if(!initialize_tweets_in_tweetDB){ console.log(data)
let opendb1 = await orbitdb.feed("/orbitdb/zdpuAtX8772p1ReNvov4Dwr6k3xkAnx62Kap2WdtkPcP4iJaj/bamberibok-tweetDB")
await opendb1.load()
let opendb2 = await orbitdb.feed("/orbitdb/zdpuAtsDVRVfuW8kckA3UirD7WBZBhZvzN6pexoKo3TL4U6pU/testiebok-tweetDB")
await opendb2.load()
let opendb3 = await orbitdb.feed("/orbitdb/zdpuAv7453HPrL3eDTQTTLubskR6wXfA44JTSCZog7jL5doZC/iloveduvels-tweetDB")
await opendb3.load()
let opendb4 = await orbitdb.feed("/orbitdb/zdpuAyFv1KDdcJUjKpWutpu93QDgAfWp5Taw5yxrgMD1N5WuZ/bbboy-tweetDB")
await opendb4.load()
let payload1 = await opendb1.iterator({ reverse: true, limit: -1 }).collect()
let data1 = payload1.map(element => element.payload.value)
console.log("opendb1 data:")
console.log(data1)
let payload2 = await opendb2.iterator({ reverse: true, limit: -1 }).collect()
let data2 = payload2.map(element => element.payload.value)
console.log("opendb2 data:")
console.log(data2)
let payload3 = await opendb3.iterator({ reverse: true, limit: -1 }).collect()
let data3 = payload3.map(element => element.payload.value)
console.log("opendb3 data:")
console.log(data3)
let payload4 = await opendb4.iterator({ reverse: true, limit: -1 }).collect()
let data4 = payload4.map(element => element.payload.value)
console.log("opendb4 data:")
console.log(data4)
}
} }
try { const addresses = [
openExternalDBs() '/orbitdb/zdpuAnrgVSgQu6FsgBMuM3HuvkCnatZCPZyDnd8tKNnTjTRV7/Test0',
} catch (e) { '/orbitdb/zdpuB3JC6THJ91VrHmdQb6WZAdhV2au91pCrzWN3fGLYZRCga/Test1',
console.log(e) '/orbitdb/zdpuAwSs3AXwd6EUekS254STCJQXP8wyajaLadwuLjYcncsZX/Test2',
} '/orbitdb/zdpuAtHT3VkRFQMPzRch7JJgJBeAWAgFMykvX6w4SnH5WDwF2/Test3'
]
// openManyDbs(addresses)
createManyDbs(4)