Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.

Commit 1671a9d

Browse files
authored
Merge pull request #126 from ipfs/feat/use-base-libp2p
Use the base class
2 parents 112a5bd + 21ab89f commit 1671a9d

File tree

7 files changed

+46
-29766
lines changed

7 files changed

+46
-29766
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
[![Sauce Test Status](https://saucelabs.com/browser-matrix/libp2p-ipfs-browser.svg)](https://saucelabs.com/u/libp2p-ipfs-browser)
1616

17-
> libp2p build (module) used in js-ipfs when running it on the browser.
17+
> libp2p build (module) used in js-ipfs when running it on the browser. If you are looking for the version to be used in Node.js, see [libp2p-ipfs-nodejs](https://github.com/ipfs/js-libp2p-ipfs-nodejs).
1818
1919
This libp2p build has support for:
2020

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const gulp = require('gulp')
44
const multiaddr = require('multiaddr')
5-
const Node = require('libp2p-ipfs').Node
5+
const Node = require('libp2p-ipfs-nodejs')
66
const PeerInfo = require('peer-info')
77
const PeerId = require('peer-id')
88
const pull = require('pull-stream')

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
"homepage": "https://github.com/ipfs/js-libp2p-ipfs-browser#readme",
3232
"devDependencies": {
3333
"aegir": "^9.1.2",
34-
"async": "^2.1.2",
34+
"async": "^2.1.4",
3535
"chai": "^3.5.0",
3636
"gulp": "^3.9.1",
37-
"libp2p-ipfs": "^0.15.0",
37+
"libp2p-ipfs-nodejs": "^0.16.0",
3838
"peer-id": "^0.8.0",
3939
"pre-commit": "^1.1.3",
4040
"pull-goodbye": "0.0.1",
@@ -45,19 +45,19 @@
4545
"dependencies": {
4646
"libp2p-secio": "^0.6.3",
4747
"libp2p-spdy": "^0.10.0",
48-
"libp2p-swarm": "^0.26.1",
48+
"libp2p-swarm": "^0.26.3",
4949
"libp2p-webrtc-star": "^0.6.0",
5050
"libp2p-websockets": "^0.9.1",
5151
"mafmt": "^2.1.2",
52-
"multiaddr": "^2.0.3",
52+
"multiaddr": "^2.1.1",
5353
"peer-book": "^0.3.0",
5454
"peer-id": "^0.8.0",
55-
"peer-info": "^0.8.0"
55+
"peer-info": "^0.8.1"
5656
},
5757
"contributors": [
5858
"David Dias <daviddias.p@gmail.com>",
5959
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
6060
"Richard Littauer <richard.littauer@gmail.com>",
6161
"greenkeeperio-bot <support@greenkeeper.io>"
6262
]
63-
}
63+
}

src/index.js

Lines changed: 26 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -1,206 +1,34 @@
11
'use strict'
22

3-
const Swarm = require('libp2p-swarm')
43
const WS = require('libp2p-websockets')
54
const WebRTCStar = require('libp2p-webrtc-star')
65
const spdy = require('libp2p-spdy')
76
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)
7831
}
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
20632
}
33+
34+
module.exports = Node

test/webrtc-star-only.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const PeerId = require('peer-id')
88
const parallel = require('async/parallel')
99
const pull = require('pull-stream')
1010

11-
const libp2p = require('../src')
11+
const Node = require('../src')
1212

1313
describe('libp2p-ipfs-browser (webrtc only)', function () {
1414
this.timeout(15 * 1000)
@@ -38,15 +38,19 @@ describe('libp2p-ipfs-browser (webrtc only)', function () {
3838
})
3939

4040
it('create two libp2p nodes with those peers', (done) => {
41-
node1 = new libp2p.Node(peer1)
42-
node2 = new libp2p.Node(peer2)
41+
node1 = new Node(peer1)
42+
node2 = new Node(peer2)
4343
done()
4444
})
4545

4646
it('listen on the two libp2p nodes', (done) => {
4747
parallel([
48-
node1.start,
49-
node2.start
48+
(cb) => {
49+
node1.start(cb)
50+
},
51+
(cb) => {
52+
node2.start(cb)
53+
}
5054
], done)
5155
})
5256

@@ -114,7 +118,7 @@ describe('libp2p-ipfs-browser (webrtc only)', function () {
114118
node2.dialByPeerInfo(peerInfo, () => {})
115119
})
116120

117-
const node3 = new libp2p.Node(peer3)
121+
const node3 = new Node(peer3)
118122
node3.start(() => {
119123
setTimeout(() => {
120124
expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1)

test/websockets-only.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const pull = require('pull-stream')
1010
const goodbye = require('pull-goodbye')
1111
const serializer = require('pull-serializer')
1212

13-
const libp2p = require('../src')
13+
const Node = require('../src')
1414
const rawPeer = require('./peer.json')
1515

1616
describe('libp2p-ipfs-browser (websockets only)', () => {
@@ -39,7 +39,7 @@ describe('libp2p-ipfs-browser (websockets only)', () => {
3939
PeerInfo.create((err, info) => {
4040
expect(err).to.not.exist
4141
info.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
42-
nodeA = new libp2p.Node(info)
42+
nodeA = new Node(info)
4343
done()
4444
})
4545
})

0 commit comments

Comments
 (0)