This repository has been 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
/
swarm.js
84 lines (62 loc) · 1.9 KB
/
swarm.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict'
const promisify = require('promisify-es6')
const values = require('lodash/values')
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
module.exports = function swarm (self) {
return {
peers: promisify((opts, callback) => {
if (typeof opts === 'function') {
callback = opts
opts = {}
}
opts = opts || {}
if (!self.isOnline()) {
return callback(new Error(OFFLINE_ERROR))
}
const verbose = opts.v || opts.verbose
// TODO: return latency and streams when verbose is set
// we currently don't have this information
const peers = []
values(self._peerInfoBook.getAll()).forEach((peer) => {
const connectedAddr = peer.isConnected()
if (!connectedAddr) { return }
const tupple = {
addr: connectedAddr,
peer: peer.id
}
if (verbose) {
tupple.latency = 'unknown'
}
peers.push(tupple)
})
callback(null, peers)
}),
// all the addrs we know
addrs: promisify((callback) => {
if (!self.isOnline()) {
return callback(new Error(OFFLINE_ERROR))
}
const peers = values(self._peerInfoBook.getAll())
callback(null, peers)
}),
localAddrs: promisify((callback) => {
if (!self.isOnline()) {
return callback(new Error(OFFLINE_ERROR))
}
callback(null, self._libp2pNode.peerInfo.multiaddrs.toArray())
}),
connect: promisify((maddr, callback) => {
if (!self.isOnline()) {
return callback(new Error(OFFLINE_ERROR))
}
self._libp2pNode.dial(maddr, callback)
}),
disconnect: promisify((maddr, callback) => {
if (!self.isOnline()) {
return callback(new Error(OFFLINE_ERROR))
}
self._libp2pNode.hangUp(maddr, callback)
}),
filters: promisify((callback) => callback(new Error('Not implemented')))
}
}