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

Commit 9a30487

Browse files
authored
refactor: async
2 parents 360dcfb + b0b54db commit 9a30487

File tree

8 files changed

+180
-64
lines changed

8 files changed

+180
-64
lines changed

.gitignore

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,8 @@
1-
dist
2-
# Logs
3-
logs
4-
*.log
5-
npm-debug.log*
6-
7-
# Runtime data
8-
pids
9-
*.pid
10-
*.seed
11-
12-
# Directory for instrumented libs generated by jscoverage/JSCover
13-
lib-cov
1+
**/node_modules/
2+
**/*.log
3+
package-lock.json
144

155
# Coverage directory used by tools like istanbul
166
coverage
17-
18-
# nyc test coverage
19-
.nyc_output
20-
21-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
22-
.grunt
23-
24-
# node-waf configuration
25-
.lock-wscript
26-
27-
# Compiled binary addons (http://nodejs.org/api/addons.html)
28-
build/Release
29-
30-
# Dependency directories
31-
node_modules
32-
jspm_packages
33-
34-
# Optional npm cache directory
35-
.npm
36-
37-
# Optional REPL history
38-
.node_repl_history
7+
docs
8+
dist

.travis.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
language: node_js
2+
cache: npm
3+
stages:
4+
- check
5+
- test
6+
- cov
7+
8+
node_js:
9+
- '10'
10+
- '12'
11+
12+
os:
13+
- linux
14+
- osx
15+
- windows
16+
17+
script: npx nyc -s npm run test:node -- --bail
18+
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
19+
20+
jobs:
21+
include:
22+
- stage: check
23+
script:
24+
- npx aegir dep-check
25+
- npm run lint
26+
27+
- stage: test
28+
name: chrome
29+
addons:
30+
chrome: stable
31+
script: npx aegir test -t browser -t webworker
32+
33+
- stage: test
34+
name: firefox
35+
addons:
36+
firefox: latest
37+
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
38+
39+
notifications:
40+
email: false

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The API is presented with both Node.js and Go primitives, however, there is not
2121
## Modules that implement the interface
2222

2323
- [JavaScript libp2p-mdns](https://github.com/libp2p/js-libp2p-mdns)
24-
- [JavaScript libp2p-railing](https://github.com/libp2p/js-libp2p-railing)
24+
- [JavaScript libp2p-bootstrap](https://github.com/libp2p/js-libp2p-bootstrap)
2525
- [JavaScript libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
2626
- [JavaScript libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star)
2727
- [JavaScript libp2p-websocket-star](https://github.com/libp2p/js-libp2p-websocket-star)
@@ -44,37 +44,40 @@ Install `interface-peer-discovery` as one of the dependencies of your project an
4444
const test = require('interface-peer-discovery')
4545

4646
const common = {
47-
setup (cb) {
48-
cb(null, yourMuxer)
47+
setup () {
48+
return YourDiscovery
4949
},
50-
teardown (cb) {
51-
cb()
50+
teardown () {
51+
// Clean up any resources created by setup()
5252
}
5353
}
5454

5555
// use all of the test suits
5656
test(common)
5757
```
5858

59-
### Go
60-
61-
> WIP - go-libp2p does not have a test suite available for Peer Discovery yet.
62-
6359
## API
6460

6561
A valid (read: that follows this abstraction) Peer Discovery module must implement the following API:
6662

6763
### `start` the service
6864

69-
- `JavaScript` discovery.start(callback)
70-
- `Go` NA
65+
- `await discovery.start()`
66+
67+
Start the discovery service.
68+
69+
It returns a `Promise`
7170

7271
### `stop` the service
7372

74-
- `JavaScript` discovery.stop(callback)
75-
- `Go` NA
73+
- `await discovery.stop()`
74+
75+
Stop the discovery service.
76+
77+
It returns a `Promise`
7678

7779
### discoverying peers
7880

79-
- `JavaScript` discovery.on('peer', function (peerInfo) {})
80-
- `Go` NA
81+
- `discovery.on('peer', (peerInfo) => {})`
82+
83+
Everytime a peer is discovered by a discovery service, it emmits a `peer` event with the discover peer's [PeerInfo](https://github.com/libp2p/js-peer-info).

package.json

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,29 @@
55
"leadMaintainer": "Vasco Santos <vasco.santos@moxy.studio>",
66
"main": "src/index.js",
77
"scripts": {
8-
"lint": "aegir-lint",
9-
"build": "aegir-build",
10-
"test": "exit 0",
11-
"release": "aegir-release",
12-
"release-minor": "aegir-release --type minor",
13-
"release-major": "aegir-release --type major"
8+
"lint": "aegir lint",
9+
"build": "aegir build",
10+
"test": "aegir test",
11+
"test:node": "aegir test -t node",
12+
"test:browser": "aegir test -t browser -t webworker",
13+
"release": "aegir release",
14+
"release-minor": "aegir release --type minor",
15+
"release-major": "aegir release --type major"
1416
},
15-
"pre-commit": [
16-
"lint",
17-
"test"
17+
"pre-push": [
18+
"lint"
1819
],
1920
"repository": {
2021
"type": "git",
2122
"url": "https://github.com/libp2p/interface-peer-discovery.git"
2223
},
2324
"keywords": [
25+
"libp2p",
26+
"network",
27+
"p2p",
28+
"peer",
29+
"discovery",
30+
"peer-to-peer",
2431
"IPFS"
2532
],
2633
"author": "David Dias <daviddias@ipfs.io>",
@@ -31,13 +38,17 @@
3138
"homepage": "https://github.com/libp2p/interface-peer-discovery",
3239
"dependencies": {},
3340
"devDependencies": {
34-
"aegir": "^18.2.2"
41+
"aegir": "^20.3.1",
42+
"chai": "^4.2.0",
43+
"dirty-chai": "^2.0.1",
44+
"peer-id": "^0.13.3",
45+
"peer-info": "^0.17.0"
3546
},
3647
"engines": {
37-
"node": ">=4.0.0",
38-
"npm": ">=3.0.0"
48+
"node": ">=10.0.0",
49+
"npm": ">=6.0.0"
3950
},
4051
"contributors": [
4152
"David Dias <daviddias.p@gmail.com>"
4253
]
43-
}
54+
}

src/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
module.exports = (common) => {
5+
describe('interface-peer-discovery', () => {
6+
let discovery
7+
8+
before(() => {
9+
discovery = common.setup()
10+
})
11+
12+
after(() => common.teardown && common.teardown())
13+
14+
it('can start the service', async () => {
15+
await discovery.start()
16+
})
17+
18+
it('can start and stop the service', async () => {
19+
await discovery.start()
20+
await discovery.stop()
21+
})
22+
23+
it('should not fail to stop the service if it was not started', async () => {
24+
await discovery.stop()
25+
})
26+
27+
it('should not fail to start the service if it is already started', async () => {
28+
await discovery.start()
29+
await discovery.start()
30+
})
31+
})
32+
}

test/compliance.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const tests = require('../src')
5+
const MockDiscovery = require('./mock-discovery')
6+
7+
describe('compliance tests', () => {
8+
tests({
9+
setup () {
10+
return new MockDiscovery()
11+
}
12+
})
13+
})

test/index.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/mock-discovery.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict'
2+
3+
const { EventEmitter } = require('events')
4+
5+
const PeerId = require('peer-id')
6+
const PeerInfo = require('peer-info')
7+
8+
/**
9+
* Emits 'peer' events on discovery.
10+
*/
11+
class MockDiscovery extends EventEmitter {
12+
/**
13+
* Constructs a new Bootstrap.
14+
*
15+
* @param {Object} options
16+
* @param {number} options.discoveryDelay - the delay to find a peer (in milli-seconds)
17+
*/
18+
constructor (options = {}) {
19+
super()
20+
21+
this.options = options
22+
this._isRunning = false
23+
this._timer = null
24+
}
25+
26+
start () {
27+
this._isRunning = true
28+
this._discoverPeer()
29+
}
30+
31+
stop () {
32+
clearTimeout(this._timer)
33+
this._isRunning = false
34+
}
35+
36+
async _discoverPeer () {
37+
if (!this._isRunning) return
38+
39+
const peerId = await PeerId.create({ bits: 512 })
40+
const peerInfo = new PeerInfo(peerId)
41+
42+
this._timer = setTimeout(() => {
43+
this.emit('peer', peerInfo)
44+
}, this.options.discoveryDelay || 1000)
45+
}
46+
}
47+
48+
module.exports = MockDiscovery

0 commit comments

Comments
 (0)