2018-02-05 22:19:54 +01:00
|
|
|
export async function ipfsPut(content) {
|
2018-02-07 21:08:53 +01:00
|
|
|
const obj = {
|
|
|
|
content: Buffer.from(content),
|
|
|
|
path: '/'
|
|
|
|
}
|
|
|
|
const response = await window.ipfs.files.add(obj)
|
|
|
|
return response[0].hash
|
2018-02-08 23:52:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function readText(multihash) {
|
|
|
|
const buffer = await window.ipfs.object.get(multihash)
|
|
|
|
return buffer.toString('utf-8')
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getStats() {
|
|
|
|
const ipfs = window.ipfs;
|
|
|
|
const orbitDb = window.orbitDb
|
|
|
|
const dbs = {}
|
|
|
|
const stats = {}
|
|
|
|
if (ipfs) {
|
|
|
|
const peers = await ipfs.swarm.peers()
|
|
|
|
const id = await ipfs.id()
|
|
|
|
stats.peers = peers.map(p => p.peer.id._idB58String)
|
|
|
|
stats.id = id.id
|
|
|
|
}
|
|
|
|
if (orbitDb) {
|
|
|
|
stats.pubKey = await orbitDb.key.getPublic('hex')
|
|
|
|
Object.values(window.dbs || {}).forEach(db => {
|
|
|
|
let writeable = db.access.write.indexOf('*') >= 0 || db.access.write.indexOf(stats.pubKey) >= 0
|
|
|
|
const dbInfo = {
|
|
|
|
opLogLength: db._oplog.length,
|
|
|
|
access: {
|
|
|
|
admin: db.access.admin,
|
|
|
|
read: db.access.read,
|
|
|
|
write: db.access.write,
|
|
|
|
writeable
|
|
|
|
},
|
|
|
|
peers: []
|
|
|
|
}
|
2018-02-09 00:42:14 +01:00
|
|
|
const subscription = orbitDb._pubsub._subscriptions[db.address]
|
2018-02-08 23:52:06 +01:00
|
|
|
if (subscription && subscription.room) {
|
2018-02-09 00:42:14 +01:00
|
|
|
dbInfo.peers = [...(subscription.room._peers || [])]
|
2018-02-08 23:52:06 +01:00
|
|
|
}
|
|
|
|
dbs[db.address] = dbInfo
|
|
|
|
})
|
|
|
|
}
|
|
|
|
stats.dbs = dbs
|
|
|
|
return stats
|
2018-02-05 22:19:54 +01:00
|
|
|
}
|