1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-03-12 21:48:39 +01:00
ipfs-boards/src/utils/ipfs.js
2019-03-10 19:53:18 +01:00

56 lines
1.5 KiB
JavaScript

export async function ipfsPut(content) {
const obj = {
content: Buffer.from(content),
path: '/',
};
const response = await window.ipfs.files.add(obj);
return response[0].hash;
}
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 && ipfs.isOnline()) {
stats.ipfsLoaded = true;
const peers = await ipfs.swarm.peers();
const id = await ipfs.id();
stats.peers = peers.map(p => p.peer.id._idB58String);
stats.id = id.id;
} else {
stats.ipfsLoaded = false;
}
if (stats.ipfsLoaded && orbitDb) {
stats.orbitDbLoaded = true;
stats.pubKey = await orbitDb.key.getPublic('hex');
Object.values(window.dbs || {}).forEach((db) => {
const 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: [],
};
const subscription = orbitDb._pubsub._subscriptions[db.address];
if (subscription && subscription.room) {
dbInfo.peers = [...(subscription.room._peers || [])];
}
dbs[db.address] = dbInfo;
});
} else {
stats.orbitDbLoaded = false;
}
stats.dbs = dbs;
return stats;
}