diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd34c26 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +**/node_modules/ +**/*.log +package-lock.json + +# Coverage directory used by tools like istanbul +coverage +docs +dist diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c7f74c1 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..79bc3d7 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,20 @@ + +## [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)) + + + + +# [0.1.0](https://github.com/libp2p/interface-peer-discovery/compare/v0.0.1...v0.1.0) (2019-09-27) + + + + +## 0.0.1 (2017-02-09) + + + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7789aab --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..265cf45 --- /dev/null +++ b/README.md @@ -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). diff --git a/img/badge.png b/img/badge.png new file mode 100644 index 0000000..785abc4 Binary files /dev/null and b/img/badge.png differ diff --git a/img/badge.sketch b/img/badge.sketch new file mode 100644 index 0000000..51a982d Binary files /dev/null and b/img/badge.sketch differ diff --git a/img/badge.svg b/img/badge.svg new file mode 100644 index 0000000..8ecfc6d --- /dev/null +++ b/img/badge.svg @@ -0,0 +1,39 @@ + + + + badge + Created with Sketch. + + + + + + + + + + + + Peer Discovery + + + Compatibl + e + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..e897719 --- /dev/null +++ b/package.json @@ -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 ", + "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 ", + "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 ", + "Jacob Heun ", + "Mike Goelzer ", + "Vasco Santos ", + "Vasco Santos " + ] +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..343a7db --- /dev/null +++ b/src/index.js @@ -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() + }) + }) +} diff --git a/test/compliance.spec.js b/test/compliance.spec.js new file mode 100644 index 0000000..5b54fba --- /dev/null +++ b/test/compliance.spec.js @@ -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)) + } + }) +}) diff --git a/test/mock-discovery.js b/test/mock-discovery.js new file mode 100644 index 0000000..c64b878 --- /dev/null +++ b/test/mock-discovery.js @@ -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