Skip to content

Commit 84add4d

Browse files
committed
feat: support for DHT and without DHT at the same time
refactor(tests): make tests independent without require global set up steps feat: move libp2p just to the network, take libp2p as a whole and not pieces docs: Update README Documentation
1 parent 1dbf264 commit 84add4d

34 files changed

+1121
-985
lines changed

README.md

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,7 @@
3434
> npm install ipfs-bitswap
3535
```
3636

37-
### Use in Node.js
38-
39-
```js
40-
const Bitswap = require('ipfs-bitswap')
41-
```
42-
43-
### Use in a browser with browserify, webpack or any other bundler
37+
### Use in Node.js or in the browser with browserify, webpack or any other bundler
4438

4539
```js
4640
const Bitswap = require('ipfs-bitswap')
@@ -56,10 +50,6 @@ Loading this module through a script tag will make the `IpfsBitswap` object avai
5650
<script src="https://unpkg.com/ipfs-bitswap/dist/index.js"></script>
5751
```
5852

59-
## Usage
60-
61-
See https://ipfs.github.io/js-ipfs-bitswap
62-
6353
## API
6454

6555
See https://ipfs.github.io/js-ipfs-bitswap
@@ -73,24 +63,21 @@ See https://ipfs.github.io/js-ipfs-bitswap
7363
```sh
7464
» tree src
7565
src
76-
├── components
77-
│   ├── decision
78-
│   │   ├── engine.js
79-
│   │   ├── index.js
80-
│   │   └── ledger.js
81-
│   ├── network # Handles peerSet and open new conns
82-
│   │   └── index.js
83-
│   └── want-manager # Keeps track of all blocks the peer wants (not the others which it is connected)
84-
│   ├── index.js
85-
│   └── msg-queue.js # Messages to send queue, one per peer
8666
├── constants.js
67+
├── decision-engine
68+
│   ├── index.js
69+
│   └── ledger.js
8770
├── index.js
88-
└── types
89-
├── message # (Type) message that is put in the wire
71+
├── network.js # Handles peerSet and open new conns
72+
├─── want-manager # Keeps track of all blocks the peer (self) wants
73+
│   ├── index.js
74+
│   └── msg-queue.js # Messages to send queue, one per peer
75+
└─── types
76+
├── message # (Type) message that is put in the wire
9077
│   ├── entry.js
9178
│   ├── index.js
9279
│   └── message.proto.js
93-
└── wantlist # (Type) track wanted blocks
80+
└── wantlist # (Type) track wanted blocks
9481
├── entry.js
9582
└── index.js
9683
```

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Node.js implementation of the Bitswap data exchange protocol used by IPFS",
55
"main": "src/index.js",
66
"browser": {
7-
"./test/libp2p-bundle": false
7+
"./test/utils/create-libp2p-node": false,
8+
"./test/utils/create-temp-repo-nodejs.js": "./test/utils/create-temp-repo-browser.js"
89
},
910
"scripts": {
1011
"test": "aegir-test",
@@ -39,10 +40,11 @@
3940
"devDependencies": {
4041
"aegir": "^11.0.2",
4142
"benchmark": "^2.1.4",
42-
"chai": "^4.1.0",
43+
"chai": "^4.1.1",
4344
"dirty-chai": "^2.0.1",
4445
"ipfs-repo": "~0.17.0",
4546
"libp2p": "^0.11.0",
47+
"libp2p-kad-dht": "^0.4.1",
4648
"libp2p-multiplex": "^0.4.4",
4749
"libp2p-secio": "^0.7.1",
4850
"libp2p-tcp": "^0.10.2",
@@ -58,7 +60,7 @@
5860
"dependencies": {
5961
"async": "^2.5.0",
6062
"cids": "~0.5.1",
61-
"debug": "^2.6.8",
63+
"debug": "^3.0.0",
6264
"ipfs-block": "~0.6.0",
6365
"lodash.debounce": "^4.0.8",
6466
"lodash.find": "^4.6.0",
@@ -75,6 +77,7 @@
7577
"pull-length-prefixed": "^1.3.0",
7678
"pull-pushable": "^2.1.1",
7779
"pull-stream": "^3.6.0",
80+
"safe-buffer": "^5.1.1",
7881
"varint-decoder": "^0.1.1"
7982
},
8083
"contributors": [

src/components/decision-engine/index.js renamed to src/decision-engine/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const debug = require('debug')
44
const each = require('async/each')
55
const eachSeries = require('async/eachSeries')
66
const waterfall = require('async/waterfall')
7+
const setImmediate = require('async/setImmediate')
8+
79
const map = require('async/map')
810
const debounce = require('lodash.debounce')
911
const uniqWith = require('lodash.uniqwith')
@@ -15,8 +17,8 @@ const pullAllWith = require('lodash.pullallwith')
1517
const log = debug('bitswap:engine')
1618
log.error = debug('bitswap:engine:error')
1719

18-
const Message = require('../../types/message')
19-
const Wantlist = require('../../types/wantlist')
20+
const Message = require('../types/message')
21+
const Wantlist = require('../types/wantlist')
2022
const Ledger = require('./ledger')
2123

2224
const MAX_MESSAGE_SIZE = 512 * 1024
@@ -271,12 +273,14 @@ class DecisionEngine {
271273
return l
272274
}
273275

274-
start () {
276+
start (callback) {
275277
this._running = true
278+
setImmediate(() => callback())
276279
}
277280

278-
stop () {
281+
stop (callback) {
279282
this._running = false
283+
setImmediate(() => callback())
280284
}
281285
}
282286

src/components/decision-engine/ledger.js renamed to src/decision-engine/ledger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const Wantlist = require('../../types/wantlist')
3+
const Wantlist = require('../types/wantlist')
44

55
class Ledger {
66
constructor (peerId) {

src/index.js

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,24 @@ const map = require('async/map')
1010
const once = require('once')
1111

1212
const CONSTANTS = require('./constants')
13-
const WantManager = require('./components/want-manager')
14-
const Network = require('./components/network')
15-
const DecisionEngine = require('./components/decision-engine')
13+
const WantManager = require('./want-manager')
14+
const Network = require('./network')
15+
const DecisionEngine = require('./decision-engine')
1616

1717
const log = debug('bitswap')
1818
log.error = debug('bitswap:error')
1919

20-
/**
21-
*
22-
*/
2320
class Bitswap {
2421
/**
2522
* Create a new bitswap instance.
2623
*
2724
* @param {Libp2p} libp2p
2825
* @param {Blockstore} blockstore
29-
* @param {PeerBook} peerBook
3026
* @returns {Bitswap}
3127
*/
32-
constructor (libp2p, blockstore, peerBook) {
33-
this.libp2p = libp2p
28+
constructor (libp2p, blockstore) {
3429
// the network delivers messages
35-
this.network = new Network(libp2p, peerBook, this)
30+
this.network = new Network(libp2p, this)
3631

3732
// local database
3833
this.blockstore = blockstore
@@ -54,6 +49,7 @@ class Bitswap {
5449
_receiveMessage (peerId, incoming, callback) {
5550
this.engine.messageReceived(peerId, incoming, (err) => {
5651
if (err) {
52+
// TODO: Q: why do we just log and not return here?
5753
log('failed to receive message', incoming)
5854
}
5955

@@ -272,7 +268,8 @@ class Bitswap {
272268
})
273269
}, cb),
274270
(cb) => {
275-
if (missing.length > 0) {
271+
if (missing.length > 0) { // TODO: Q: why do we just log and not return here?
272+
276273
addListeners(missing)
277274
this.wm.wantBlocks(missing)
278275

@@ -389,23 +386,31 @@ class Bitswap {
389386
/**
390387
* Start the bitswap node.
391388
*
389+
* @param {function(Error)} callback
390+
*
392391
* @returns {void}
393392
*/
394-
start () {
395-
this.wm.run()
396-
this.network.start()
397-
this.engine.start()
393+
start (callback) {
394+
series([
395+
(cb) => this.wm.start(cb),
396+
(cb) => this.network.start(cb),
397+
(cb) => this.engine.start(cb)
398+
], callback)
398399
}
399400

400401
/**
401-
* Stooop the bitswap node.
402+
* Stop the bitswap node.
403+
*
404+
* @param {function(Error)} callback
402405
*
403406
* @returns {void}
404407
*/
405-
stop () {
406-
this.wm.stop(this.libp2p.peerInfo.id)
407-
this.network.stop()
408-
this.engine.stop()
408+
stop (callback) {
409+
series([
410+
(cb) => this.wm.stop(cb),
411+
(cb) => this.network.stop(cb),
412+
(cb) => this.engine.stop(cb)
413+
], callback)
409414
}
410415
}
411416

0 commit comments

Comments
 (0)