Merge branch 'master' of https://github.com/libp2p/interface-peer-discovery into remote/connection

This commit is contained in:
Jacob Heun 2019-10-18 13:43:17 +02:00
commit b1fda8401a
No known key found for this signature in database
GPG Key ID: CA5A94C15809879F
12 changed files with 368 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
**/node_modules/
**/*.log
package-lock.json
# Coverage directory used by tools like istanbul
coverage
docs
dist

40
.travis.yml Normal file
View File

@ -0,0 +1,40 @@
language: node_js
cache: npm
stages:
- check
- test
- cov
node_js:
- '10'
- '12'
os:
- linux
- osx
- windows
script: npx nyc -s npm run test:node -- --bail
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
jobs:
include:
- stage: check
script:
- npx aegir dep-check
- npm run lint
- stage: test
name: chrome
addons:
chrome: stable
script: npx aegir test -t browser -t webworker
- stage: test
name: firefox
addons:
firefox: latest
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
notifications:
email: false

20
CHANGELOG.md Normal file
View File

@ -0,0 +1,20 @@
<a name="0.1.1"></a>
## [0.1.1](https://github.com/libp2p/interface-peer-discovery/compare/v0.1.0...v0.1.1) (2019-09-27)
### Bug Fixes
* add async support to setup ([#11](https://github.com/libp2p/interface-peer-discovery/issues/11)) ([2814c76](https://github.com/libp2p/interface-peer-discovery/commit/2814c76))
<a name="0.1.0"></a>
# [0.1.0](https://github.com/libp2p/interface-peer-discovery/compare/v0.0.1...v0.1.0) (2019-09-27)
<a name="0.0.1"></a>
## 0.0.1 (2017-02-09)

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2016 libp2p
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

83
README.md Normal file
View File

@ -0,0 +1,83 @@
interface-peer-discovery
========================
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://protocol.ai)
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](http://libp2p.io/)
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23libp2p)
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
> A test suite and interface you can use to implement a Peer Discovery module for libp2p.
The primary goal of this module is to enable developers to pick and/or swap their Peer Discovery modules as they see fit for their application, without having to go through shims or compatibility issues. This module and test suite was heavily inspired by [abstract-blob-store](https://github.com/maxogden/abstract-blob-store).
Publishing a test suite as a module lets multiple modules all ensure compatibility since they use the same test suite.
The API is presented with both Node.js and Go primitives, however, there is not actual limitations for it to be extended for any other language, pushing forward the cross compatibility and interop through diferent stacks.
## Lead Maintainer
[Vasco Santos](https://github.com/vasco-santos).
## Modules that implement the interface
- [JavaScript libp2p-mdns](https://github.com/libp2p/js-libp2p-mdns)
- [JavaScript libp2p-bootstrap](https://github.com/libp2p/js-libp2p-bootstrap)
- [JavaScript libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
- [JavaScript libp2p-webrtc-star](https://github.com/libp2p/js-libp2p-webrtc-star)
- [JavaScript libp2p-websocket-star](https://github.com/libp2p/js-libp2p-websocket-star)
Send a PR to add a new one if you happen to find or write one.
## Badge
Include this badge in your readme if you make a new module that uses interface-peer-discovery API.
![](/img/badge.png)
## Usage
### Node.js
Install `interface-discovery` as one of the dependencies of your project and as a test file. Then, using `mocha` (for JavaScript) or a test runner with compatible API, do:
```js
const test = require('interface-discovery')
const common = {
setup () {
return YourDiscovery
},
teardown () {
// Clean up any resources created by setup()
}
}
// use all of the test suits
test(common)
```
## API
A valid (read: that follows this abstraction) Peer Discovery module must implement the following API:
### `start` the service
- `await discovery.start()`
Start the discovery service.
It returns a `Promise`
### `stop` the service
- `await discovery.stop()`
Stop the discovery service.
It returns a `Promise`
### discoverying peers
- `discovery.on('peer', (peerInfo) => {})`
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).

BIN
img/badge.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

BIN
img/badge.sketch Normal file

Binary file not shown.

39
img/badge.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 88 KiB

58
package.json Normal file
View File

@ -0,0 +1,58 @@
{
"name": "interface-discovery",
"version": "0.1.1",
"description": "A test suite and interface you can use to implement a discovery interface.",
"leadMaintainer": "Vasco Santos <vasco.santos@moxy.studio>",
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"test": "aegir test",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser -t webworker",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major"
},
"pre-push": [
"lint"
],
"repository": {
"type": "git",
"url": "https://github.com/libp2p/interface-peer-discovery.git"
},
"keywords": [
"libp2p",
"network",
"p2p",
"peer",
"discovery",
"peer-to-peer",
"IPFS"
],
"author": "David Dias <daviddias@ipfs.io>",
"license": "MIT",
"bugs": {
"url": "https://github.com/libp2p/interface-peer-discovery/issues"
},
"homepage": "https://github.com/libp2p/interface-peer-discovery",
"dependencies": {},
"devDependencies": {
"aegir": "^20.3.1",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"peer-id": "^0.13.3",
"peer-info": "^0.17.0"
},
"engines": {
"node": ">=10.0.0",
"npm": ">=6.0.0"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"Jacob Heun <jacobheun@gmail.com>",
"Mike Goelzer <mgoelzer@docker.com>",
"Vasco Santos <vasco.santos@moxy.studio>",
"Vasco Santos <vasco.santos@ua.pt>"
]
}

34
src/index.js Normal file
View File

@ -0,0 +1,34 @@
/* eslint-env mocha */
'use strict'
module.exports = (common) => {
describe('interface-peer-discovery', () => {
let discovery
before(async () => {
discovery = await common.setup()
})
after(() => common.teardown && common.teardown())
afterEach('ensure discovery was stopped', () => discovery.stop())
it('can start the service', async () => {
await discovery.start()
})
it('can start and stop the service', async () => {
await discovery.start()
await discovery.stop()
})
it('should not fail to stop the service if it was not started', async () => {
await discovery.stop()
})
it('should not fail to start the service if it is already started', async () => {
await discovery.start()
await discovery.start()
})
})
}

17
test/compliance.spec.js Normal file
View File

@ -0,0 +1,17 @@
/* eslint-env mocha */
'use strict'
const tests = require('../src')
const MockDiscovery = require('./mock-discovery')
describe('compliance tests', () => {
tests({
async setup () {
await new Promise(resolve => setTimeout(resolve, 10))
return new MockDiscovery()
},
async teardown () {
await new Promise(resolve => setTimeout(resolve, 10))
}
})
})

48
test/mock-discovery.js Normal file
View File

@ -0,0 +1,48 @@
'use strict'
const { EventEmitter } = require('events')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
/**
* Emits 'peer' events on discovery.
*/
class MockDiscovery extends EventEmitter {
/**
* Constructs a new Bootstrap.
*
* @param {Object} options
* @param {number} options.discoveryDelay - the delay to find a peer (in milli-seconds)
*/
constructor (options = {}) {
super()
this.options = options
this._isRunning = false
this._timer = null
}
start () {
this._isRunning = true
this._discoverPeer()
}
stop () {
clearTimeout(this._timer)
this._isRunning = false
}
async _discoverPeer () {
if (!this._isRunning) return
const peerId = await PeerId.create({ bits: 512 })
const peerInfo = new PeerInfo(peerId)
this._timer = setTimeout(() => {
this.emit('peer', peerInfo)
}, this.options.discoveryDelay || 1000)
}
}
module.exports = MockDiscovery