This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathbitswap.js
51 lines (42 loc) · 1.46 KB
/
bitswap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict'
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
const promisify = require('promisify-es6')
const setImmediate = require('async/setImmediate')
const Big = require('big.js')
function formatWantlist (list) {
return Array.from(list).map((e) => e[1])
}
module.exports = function bitswap (self) {
return {
wantlist: () => {
if (!self.isOnline()) {
throw new Error(OFFLINE_ERROR)
}
const list = self._bitswap.getWantlist()
return formatWantlist(list)
},
stat: promisify((callback) => {
if (!self.isOnline()) {
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
}
const snapshot = self._bitswap.stat().snapshot
callback(null, {
provideBufLen: parseInt(snapshot.providesBufferLength.toString()),
blocksReceived: new Big(snapshot.blocksReceived),
wantlist: formatWantlist(self._bitswap.getWantlist()),
peers: self._bitswap.peers().map((id) => id.toB58String()),
dupBlksReceived: new Big(snapshot.dupBlksReceived),
dupDataReceived: new Big(snapshot.dupDataReceived),
dataReceived: new Big(snapshot.dataReceived),
blocksSent: new Big(snapshot.blocksSent),
dataSent: new Big(snapshot.dataSent)
})
}),
unwant: (key) => {
if (!self.isOnline()) {
throw new Error(OFFLINE_ERROR)
}
// TODO: implement when https://github.com/ipfs/js-ipfs-bitswap/pull/10 is merged
}
}
}