|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const Swarm = require('libp2p-swarm') |
4 | 3 | const WS = require('libp2p-websockets')
|
5 | 4 | const WebRTCStar = require('libp2p-webrtc-star')
|
6 | 5 | const spdy = require('libp2p-spdy')
|
7 | 6 | const secio = require('libp2p-secio')
|
8 |
| -const PeerInfo = require('peer-info') |
9 |
| -const PeerId = require('peer-id') |
10 |
| -const EE = require('events').EventEmitter |
11 |
| -const multiaddr = require('multiaddr') |
12 |
| -const PeerBook = require('peer-book') |
13 |
| -const mafmt = require('mafmt') |
14 |
| - |
15 |
| -exports = module.exports |
16 |
| - |
17 |
| -const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet' |
18 |
| -const IPFS_CODE = 421 |
19 |
| - |
20 |
| -exports.Node = function Node (pInfo, pBook) { |
21 |
| - if (!(this instanceof Node)) { |
22 |
| - return new Node(pInfo, pBook) |
23 |
| - } |
24 |
| - |
25 |
| - if (!pInfo) { |
26 |
| - throw new Error('missing peer info') |
27 |
| - } |
28 |
| - |
29 |
| - if (!pBook) { |
30 |
| - pBook = new PeerBook() |
31 |
| - } |
32 |
| - |
33 |
| - this.peerInfo = pInfo |
34 |
| - this.peerBook = pBook |
35 |
| - |
36 |
| - // Swarm |
37 |
| - this.swarm = new Swarm(pInfo) |
38 |
| - this.swarm.connection.addStreamMuxer(spdy) |
39 |
| - this.swarm.connection.reuse() |
40 |
| - this.swarm.connection.crypto(secio.tag, secio.encrypt) |
41 |
| - |
42 |
| - this.swarm.on('peer-mux-established', (peerInfo) => { |
43 |
| - this.peerBook.put(peerInfo) |
44 |
| - }) |
45 |
| - |
46 |
| - this.swarm.on('peer-mux-closed', (peerInfo) => { |
47 |
| - this.peerBook.removeByB58String(peerInfo.id.toB58String()) |
48 |
| - }) |
49 |
| - |
50 |
| - let isOnline = false |
51 |
| - |
52 |
| - this.start = (callback) => { |
53 |
| - // if we have `webrtc-star` addrs, then add |
54 |
| - // the WebRTCStar transport |
55 |
| - const wstar = new WebRTCStar() |
56 |
| - |
57 |
| - if (wstar.filter(this.peerInfo.multiaddrs).length > 0) { |
58 |
| - this.swarm.transport.add('wstar', wstar) |
59 |
| - wstar.discovery.on('peer', (peerInfo) => { |
60 |
| - this.discovery.emit('peer', peerInfo) |
61 |
| - }) |
62 |
| - this.swarm.listen((err) => { |
63 |
| - if (err) { |
64 |
| - return callback(err) |
65 |
| - } |
66 |
| - // WebSockets needs to be added after because |
67 |
| - // it can't have a listener on the browser |
68 |
| - this.swarm.transport.add('ws', new WS()) |
69 |
| - isOnline = true |
70 |
| - callback() |
71 |
| - }) |
72 |
| - } else { |
73 |
| - // if just WebSockets, no thing to listen |
74 |
| - this.swarm.transport.add('ws', new WS()) |
75 |
| - isOnline = true |
76 |
| - callback() |
77 |
| - } |
| 7 | +const libp2p = require('libp2p') |
| 8 | + |
| 9 | +class Node extends libp2p { |
| 10 | + constructor (peerInfo, peerBook) { |
| 11 | + const webRTCStar = new WebRTCStar() |
| 12 | + |
| 13 | + const modules = { |
| 14 | + transport: [ |
| 15 | + new WS(), |
| 16 | + webRTCStar |
| 17 | + ], |
| 18 | + connection: { |
| 19 | + muxer: [ |
| 20 | + spdy |
| 21 | + ], |
| 22 | + crypto: [ |
| 23 | + secio |
| 24 | + ] |
| 25 | + }, |
| 26 | + discovery: [ |
| 27 | + webRTCStar.discovery |
| 28 | + ] |
| 29 | + } |
| 30 | + super(modules, peerInfo, peerBook) |
78 | 31 | }
|
79 |
| - |
80 |
| - this.stop = (callback) => { |
81 |
| - isOnline = false |
82 |
| - this.swarm.close(callback) |
83 |
| - } |
84 |
| - |
85 |
| - this.dialById = (id, protocol, callback) => { |
86 |
| - if (typeof protocol === 'function') { |
87 |
| - callback = protocol |
88 |
| - protocol = undefined |
89 |
| - } |
90 |
| - |
91 |
| - if (!isOnline) { |
92 |
| - return callback(new Error(OFFLINE_ERROR_MESSAGE)) |
93 |
| - } |
94 |
| - // NOTE, these dialById only works if a previous dial |
95 |
| - // was made until we have PeerRouting |
96 |
| - // TODO support PeerRouting when it is Ready |
97 |
| - callback(new Error('not implemented yet')) |
98 |
| - } |
99 |
| - |
100 |
| - this.dialByMultiaddr = (maddr, protocol, callback) => { |
101 |
| - if (typeof protocol === 'function') { |
102 |
| - callback = protocol |
103 |
| - protocol = undefined |
104 |
| - } |
105 |
| - |
106 |
| - if (!isOnline) { |
107 |
| - return callback(new Error(OFFLINE_ERROR_MESSAGE)) |
108 |
| - } |
109 |
| - |
110 |
| - if (typeof maddr === 'string') { |
111 |
| - maddr = multiaddr(maddr) |
112 |
| - } |
113 |
| - |
114 |
| - if (!mafmt.IPFS.matches(maddr.toString())) { |
115 |
| - return callback(new Error('multiaddr not valid')) |
116 |
| - } |
117 |
| - |
118 |
| - const ipfsIdB58String = maddr.stringTuples().filter((tuple) => { |
119 |
| - if (tuple[0] === IPFS_CODE) { |
120 |
| - return true |
121 |
| - } |
122 |
| - })[0][1] |
123 |
| - |
124 |
| - let peer |
125 |
| - try { |
126 |
| - peer = this.peerBook.getByB58String(ipfsIdB58String) |
127 |
| - } catch (err) { |
128 |
| - peer = new PeerInfo(PeerId.createFromB58String(ipfsIdB58String)) |
129 |
| - } |
130 |
| - |
131 |
| - peer.multiaddr.add(maddr) |
132 |
| - this.dialByPeerInfo(peer, protocol, callback) |
133 |
| - } |
134 |
| - |
135 |
| - this.dialByPeerInfo = (peer, protocol, callback) => { |
136 |
| - if (typeof protocol === 'function') { |
137 |
| - callback = protocol |
138 |
| - protocol = undefined |
139 |
| - } |
140 |
| - if (!isOnline) { |
141 |
| - return callback(new Error(OFFLINE_ERROR_MESSAGE)) |
142 |
| - } |
143 |
| - |
144 |
| - this.swarm.dial(peer, protocol, (err, conn) => { |
145 |
| - if (err) { |
146 |
| - return callback(err) |
147 |
| - } |
148 |
| - this.peerBook.put(peer) |
149 |
| - callback(null, conn) |
150 |
| - }) |
151 |
| - } |
152 |
| - |
153 |
| - this.hangUpById = (id, callback) => { |
154 |
| - callback(new Error('not implemented yet')) |
155 |
| - // TODO |
156 |
| - } |
157 |
| - |
158 |
| - this.hangUpByMultiaddr = (maddr, callback) => { |
159 |
| - if (!isOnline) { |
160 |
| - return callback(new Error(OFFLINE_ERROR_MESSAGE)) |
161 |
| - } |
162 |
| - |
163 |
| - if (typeof maddr === 'string') { |
164 |
| - maddr = multiaddr(maddr) |
165 |
| - } |
166 |
| - |
167 |
| - if (!mafmt.IPFS.matches(maddr.toString())) { |
168 |
| - return callback(new Error('multiaddr not valid')) |
169 |
| - } |
170 |
| - |
171 |
| - const ipfsIdB58String = maddr.stringTuples().filter((tuple) => { |
172 |
| - if (tuple[0] === IPFS_CODE) { |
173 |
| - return true |
174 |
| - } |
175 |
| - })[0][1] |
176 |
| - |
177 |
| - try { |
178 |
| - const pi = this.peerBook.getByB58String(ipfsIdB58String) |
179 |
| - this.hangUpByPeerInfo(pi, callback) |
180 |
| - } catch (err) { |
181 |
| - // already disconnected |
182 |
| - callback() |
183 |
| - } |
184 |
| - } |
185 |
| - |
186 |
| - this.hangUpByPeerInfo = (peer, callback) => { |
187 |
| - if (!isOnline) { |
188 |
| - return callback(new Error(OFFLINE_ERROR_MESSAGE)) |
189 |
| - } |
190 |
| - |
191 |
| - this.peerBook.removeByB58String(peer.id.toB58String()) |
192 |
| - this.swarm.hangUp(peer, callback) |
193 |
| - } |
194 |
| - |
195 |
| - this.handle = (protocol, handlerFunc, matchFunc) => { |
196 |
| - return this.swarm.handle(protocol, handlerFunc, matchFunc) |
197 |
| - } |
198 |
| - |
199 |
| - this.unhandle = (protocol) => { |
200 |
| - return this.swarm.unhandle(protocol) |
201 |
| - } |
202 |
| - |
203 |
| - this.discovery = new EE() |
204 |
| - this.routing = null |
205 |
| - this.records = null |
206 | 32 | }
|
| 33 | + |
| 34 | +module.exports = Node |
0 commit comments