mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-07-06 18:51:54 +00:00
Compare commits
75 Commits
v0.1.2
...
chore/skip
Author | SHA1 | Date | |
---|---|---|---|
82ed140b42 | |||
13aa6cbfa0 | |||
a8ba13da4b | |||
75f6777d89 | |||
71b813ad3b | |||
46589ce3d0 | |||
f2a18818f2 | |||
1cc943e1b2 | |||
4adedcc4bf | |||
0628d708c4 | |||
e10a1545c8 | |||
9fbf9d0331 | |||
aa996d2054 | |||
507013a724 | |||
a55c7c454a | |||
87e2e89791 | |||
5bcfc966f7 | |||
c8c249de6e | |||
5b138ef0a0 | |||
bdd2502ef6 | |||
1bef8d5d78 | |||
9a8f375d40 | |||
afc2aa6891 | |||
424a0c081e | |||
e824b15032 | |||
dba3b9932f | |||
c56dd3e3fa | |||
de64a49007 | |||
3cfd9714bd | |||
78e015cef3 | |||
bd5a8b9337 | |||
098390cd31 | |||
953e289e1a | |||
ba822856ef | |||
c77d8de2e7 | |||
6203109751 | |||
25eeedd20e | |||
5c88d77aaa | |||
f6871afd76 | |||
734c491bb0 | |||
c43a52d176 | |||
af1ba5a409 | |||
c5b724ab93 | |||
6fb45b775b | |||
85c68b5df4 | |||
27ac0a5670 | |||
2de533e15b | |||
aacb0e27d0 | |||
c00f94dfaf | |||
5967834a2f | |||
32ee3e18e2 | |||
557f6cab66 | |||
d98bb23fea | |||
6cf3723019 | |||
01e240420d | |||
ff0313721c | |||
2803e64969 | |||
e979bc9d4e | |||
bdbd58e897 | |||
79bfcacb61 | |||
148f3c9f43 | |||
6641a5b0b4 | |||
34372e07ce | |||
d5dd256a21 | |||
3f6c1cbee1 | |||
64c79d92e0 | |||
a67abccabf | |||
21d8ae6d96 | |||
5969270ce6 | |||
432da545b4 | |||
b00bcd453d | |||
8bee747c7c | |||
b960f29757 | |||
5206862504 | |||
749a8d035d |
1
.gitignore
vendored
1
.gitignore
vendored
@ -13,3 +13,4 @@ docs
|
||||
node_modules
|
||||
# Lock files
|
||||
package-lock.json
|
||||
yarn.lock
|
220
CHANGELOG.md
220
CHANGELOG.md
@ -1,3 +1,223 @@
|
||||
<a name="0.4.0"></a>
|
||||
# [0.4.0](https://github.com/libp2p/js-interfaces/compare/v0.3.2...v0.4.0) (2020-08-10)
|
||||
|
||||
|
||||
### Chores
|
||||
|
||||
* update deps ([#57](https://github.com/libp2p/js-interfaces/issues/57)) ([75f6777](https://github.com/libp2p/js-interfaces/commit/75f6777))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* - The peer id dep of this module has replaced node Buffers with Uint8Arrays
|
||||
|
||||
* chore: update gh deps
|
||||
|
||||
|
||||
|
||||
<a name="0.3.2"></a>
|
||||
## [0.3.2](https://github.com/libp2p/js-interfaces/compare/v0.3.1...v0.3.2) (2020-07-15)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* record interface ([#52](https://github.com/libp2p/js-interfaces/issues/52)) ([1cc943e](https://github.com/libp2p/js-interfaces/commit/1cc943e))
|
||||
|
||||
|
||||
|
||||
<a name="0.3.1"></a>
|
||||
## [0.3.1](https://github.com/libp2p/js-interfaces/compare/v0.2.8...v0.3.1) (2020-07-03)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* content and peer routing multiaddrs property ([#49](https://github.com/libp2p/js-interfaces/issues/49)) ([9fbf9d0](https://github.com/libp2p/js-interfaces/commit/9fbf9d0))
|
||||
* peer-routing typo ([#47](https://github.com/libp2p/js-interfaces/issues/47)) ([9a8f375](https://github.com/libp2p/js-interfaces/commit/9a8f375))
|
||||
* reconnect should trigger topology on connect if protocol stored ([#54](https://github.com/libp2p/js-interfaces/issues/54)) ([e10a154](https://github.com/libp2p/js-interfaces/commit/e10a154))
|
||||
|
||||
|
||||
### Chores
|
||||
|
||||
* remove peer-info usage on topology ([#42](https://github.com/libp2p/js-interfaces/issues/42)) ([a55c7c4](https://github.com/libp2p/js-interfaces/commit/a55c7c4))
|
||||
* update content and peer routing interfaces removing peer-info ([#43](https://github.com/libp2p/js-interfaces/issues/43)) ([87e2e89](https://github.com/libp2p/js-interfaces/commit/87e2e89))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* peer-discovery not using peer-info ([bdd2502](https://github.com/libp2p/js-interfaces/commit/bdd2502))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* topology api now uses peer-id instead of peer-info
|
||||
* content-routing and peer-routing APIs return an object with relevant properties instead of peer-info
|
||||
* peer-discovery emits object with id and multiaddrs properties
|
||||
|
||||
|
||||
|
||||
<a name="0.3.0"></a>
|
||||
# [0.3.0](https://github.com/libp2p/js-interfaces/compare/v0.2.8...v0.3.0) (2020-04-21)
|
||||
|
||||
|
||||
### Chores
|
||||
|
||||
* remove peer-info usage on topology ([#42](https://github.com/libp2p/js-interfaces/issues/42)) ([79a7843](https://github.com/libp2p/js-interfaces/commit/79a7843))
|
||||
* update content and peer routing interfaces removing peer-info ([#43](https://github.com/libp2p/js-interfaces/issues/43)) ([d2032e6](https://github.com/libp2p/js-interfaces/commit/d2032e6))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* peer-discovery not using peer-info ([5792b13](https://github.com/libp2p/js-interfaces/commit/5792b13))
|
||||
|
||||
|
||||
### BREAKING CHANGES
|
||||
|
||||
* topology api now uses peer-id instead of peer-info
|
||||
* content-routing and peer-routing APIs return an object with relevant properties instead of peer-info
|
||||
* peer-discovery emits object with id and multiaddrs properties
|
||||
|
||||
|
||||
|
||||
<a name="0.2.8"></a>
|
||||
## [0.2.8](https://github.com/libp2p/js-interfaces/compare/v0.2.7...v0.2.8) (2020-04-21)
|
||||
|
||||
|
||||
|
||||
<a name="0.2.7"></a>
|
||||
## [0.2.7](https://github.com/libp2p/js-interfaces/compare/v0.2.6...v0.2.7) (2020-03-20)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* add buffer ([#39](https://github.com/libp2p/js-interfaces/issues/39)) ([78e015c](https://github.com/libp2p/js-interfaces/commit/78e015c))
|
||||
|
||||
|
||||
|
||||
<a name="0.2.6"></a>
|
||||
## [0.2.6](https://github.com/libp2p/js-interfaces/compare/v0.2.5...v0.2.6) (2020-02-17)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* remove use of assert module ([#34](https://github.com/libp2p/js-interfaces/issues/34)) ([c77d8de](https://github.com/libp2p/js-interfaces/commit/c77d8de))
|
||||
|
||||
|
||||
|
||||
<a name="0.2.5"></a>
|
||||
## [0.2.5](https://github.com/libp2p/js-interfaces/compare/v0.2.4...v0.2.5) (2020-02-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **connection:** tracks streams properly ([#25](https://github.com/libp2p/js-interfaces/issues/25)) ([5c88d77](https://github.com/libp2p/js-interfaces/commit/5c88d77))
|
||||
|
||||
|
||||
|
||||
<a name="0.2.4"></a>
|
||||
## [0.2.4](https://github.com/libp2p/js-interfaces/compare/v0.2.3...v0.2.4) (2020-02-04)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* dependencies for tests should not be needed by who requires the tests ([#18](https://github.com/libp2p/js-interfaces/issues/18)) ([c5b724a](https://github.com/libp2p/js-interfaces/commit/c5b724a))
|
||||
|
||||
|
||||
|
||||
<a name="0.2.3"></a>
|
||||
## [0.2.3](https://github.com/libp2p/js-interfaces/compare/v0.2.2...v0.2.3) (2020-01-21)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **transport:** make close listener test more resilient ([#21](https://github.com/libp2p/js-interfaces/issues/21)) ([2de533e](https://github.com/libp2p/js-interfaces/commit/2de533e))
|
||||
|
||||
|
||||
|
||||
<a name="0.2.2"></a>
|
||||
## [0.2.2](https://github.com/libp2p/js-interfaces/compare/v0.2.1...v0.2.2) (2020-01-17)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* **connection:** dont require remoteAddr on creation ([#20](https://github.com/libp2p/js-interfaces/issues/20)) ([5967834](https://github.com/libp2p/js-interfaces/commit/5967834))
|
||||
|
||||
|
||||
|
||||
<a name="0.2.1"></a>
|
||||
## [0.2.1](https://github.com/libp2p/js-interfaces/compare/v0.2.0...v0.2.1) (2019-12-28)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add crypto transmission error ([#17](https://github.com/libp2p/js-interfaces/issues/17)) ([d98bb23](https://github.com/libp2p/js-interfaces/commit/d98bb23))
|
||||
|
||||
|
||||
|
||||
<a name="0.2.0"></a>
|
||||
# [0.2.0](https://github.com/libp2p/js-interfaces/compare/v0.1.7...v0.2.0) (2019-12-20)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* transport should not handle connection if upgradeInbound throws ([#16](https://github.com/libp2p/js-interfaces/issues/16)) ([ff03137](https://github.com/libp2p/js-interfaces/commit/ff03137))
|
||||
|
||||
|
||||
|
||||
<a name="0.1.7"></a>
|
||||
## [0.1.7](https://github.com/libp2p/js-interfaces/compare/v0.1.6...v0.1.7) (2019-12-15)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* export connection status' ([#15](https://github.com/libp2p/js-interfaces/issues/15)) ([bdbd58e](https://github.com/libp2p/js-interfaces/commit/bdbd58e))
|
||||
|
||||
|
||||
|
||||
<a name="0.1.6"></a>
|
||||
## [0.1.6](https://github.com/libp2p/js-interfaces/compare/v0.1.5...v0.1.6) (2019-12-02)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* multicodec topology disconnect with peer param ([#12](https://github.com/libp2p/js-interfaces/issues/12)) ([d5dd256](https://github.com/libp2p/js-interfaces/commit/d5dd256))
|
||||
|
||||
|
||||
|
||||
<a name="0.1.5"></a>
|
||||
## [0.1.5](https://github.com/libp2p/js-interfaces/compare/v0.1.4...v0.1.5) (2019-11-15)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* multicodec topology update peers with multicodec ([#10](https://github.com/libp2p/js-interfaces/issues/10)) ([21d8ae6](https://github.com/libp2p/js-interfaces/commit/21d8ae6))
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add class-is to topology ([#11](https://github.com/libp2p/js-interfaces/issues/11)) ([a67abcc](https://github.com/libp2p/js-interfaces/commit/a67abcc))
|
||||
|
||||
|
||||
|
||||
<a name="0.1.4"></a>
|
||||
## [0.1.4](https://github.com/libp2p/js-interfaces/compare/v0.1.3...v0.1.4) (2019-11-14)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* add topology interfaces ([#7](https://github.com/libp2p/js-interfaces/issues/7)) ([8bee747](https://github.com/libp2p/js-interfaces/commit/8bee747))
|
||||
|
||||
|
||||
|
||||
<a name="0.1.3"></a>
|
||||
## [0.1.3](https://github.com/libp2p/js-interfaces/compare/v0.1.2...v0.1.3) (2019-10-30)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* localAddr should be optional ([#6](https://github.com/libp2p/js-interfaces/issues/6)) ([749a8d0](https://github.com/libp2p/js-interfaces/commit/749a8d0))
|
||||
|
||||
|
||||
|
||||
<a name="0.1.2"></a>
|
||||
## [0.1.2](https://github.com/libp2p/js-interfaces/compare/v0.1.1...v0.1.2) (2019-10-29)
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
# JS Libp2p Interfaces
|
||||
# JS libp2p Interfaces
|
||||
|
||||
[](http://protocol.ai)
|
||||
[](http://libp2p.io/)
|
||||
@ -18,7 +18,9 @@
|
||||
- [Crypto](./src/crypto)
|
||||
- [Peer Discovery](./src/peer-discovery)
|
||||
- [Peer Routing](./src/peer-routing)
|
||||
- [Record](./src/record)
|
||||
- [Stream Muxer](./src/stream-muxer)
|
||||
- [Topology](./src/topology)
|
||||
- [Transport](./src/transport)
|
||||
|
||||
### Origin Repositories
|
||||
|
33
package.json
33
package.json
@ -1,7 +1,8 @@
|
||||
{
|
||||
"name": "libp2p-interfaces",
|
||||
"version": "0.1.2",
|
||||
"version": "0.4.0",
|
||||
"description": "Interfaces for JS Libp2p",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
"files": [
|
||||
"src",
|
||||
@ -33,28 +34,30 @@
|
||||
"homepage": "https://github.com/libp2p/js-interfaces#readme",
|
||||
"dependencies": {
|
||||
"abort-controller": "^3.0.0",
|
||||
"abortable-iterator": "^2.1.0",
|
||||
"async-iterator-to-pull-stream": "^1.3.0",
|
||||
"abortable-iterator": "^3.0.0",
|
||||
"buffer": "^5.6.0",
|
||||
"chai": "^4.2.0",
|
||||
"chai-checkmark": "^1.0.1",
|
||||
"class-is": "^1.1.0",
|
||||
"delay": "^4.3.0",
|
||||
"detect-node": "^2.0.4",
|
||||
"dirty-chai": "^2.0.1",
|
||||
"err-code": "^2.0.0",
|
||||
"it-goodbye": "^2.0.1",
|
||||
"libp2p-tcp": "^0.14.1",
|
||||
"multiaddr": "^7.1.0",
|
||||
"p-limit": "^2.2.1",
|
||||
"peer-id": "^0.13.3",
|
||||
"sinon": "^7.5.0",
|
||||
"streaming-iterables": "^4.1.0"
|
||||
"it-pair": "^1.0.0",
|
||||
"it-pipe": "^1.1.0",
|
||||
"libp2p-tcp": "^0.14.5",
|
||||
"multiaddr": "^8.0.0",
|
||||
"p-defer": "^3.0.0",
|
||||
"p-limit": "^2.3.0",
|
||||
"p-wait-for": "^3.1.0",
|
||||
"peer-id": "^0.14.0",
|
||||
"sinon": "^9.0.2",
|
||||
"streaming-iterables": "^5.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"aegir": "^20.4.1",
|
||||
"it-handshake": "^1.0.0",
|
||||
"it-pair": "^1.0.0",
|
||||
"it-pipe": "^1.0.1",
|
||||
"peer-info": "^0.17.0"
|
||||
"aegir": "^25.0.0",
|
||||
"it-handshake": "^1.0.1"
|
||||
},
|
||||
"contributors": [
|
||||
"Alan Shaw <alan.shaw@protocol.ai>",
|
||||
@ -64,6 +67,7 @@
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Greg Zuro <gregzuro@users.noreply.github.com>",
|
||||
"Jacob Heun <jacobheun@gmail.com>",
|
||||
"Jacob Heun <jacobheun@Jacobs-MacBook-Pro.local>",
|
||||
"James Ray <16969914+jamesray1@users.noreply.github.com>",
|
||||
"Jeffrey Hulten <jhulten@gmail.com>",
|
||||
"João Santos <joaosantos15@users.noreply.github.com>",
|
||||
@ -76,6 +80,7 @@
|
||||
"Sathya Narrayanan <plasmashadowx@gmail.com>",
|
||||
"Vasco Santos <vasco.santos@moxy.studio>",
|
||||
"Vasco Santos <vasco.santos@ua.pt>",
|
||||
"dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>",
|
||||
"dirkmc <dirkmdev@gmail.com>",
|
||||
"dmitriy ryajov <dryajov@dmitriys-MBP.HomeNET>",
|
||||
"greenkeeperio-bot <support@greenkeeper.io>",
|
||||
|
@ -109,8 +109,8 @@ const conn = new Connection({
|
||||
|
||||
Creates a new Connection instance.
|
||||
|
||||
`localAddr` is the [multiaddr](https://github.com/multiformats/multiaddr) address used by the local peer to reach the remote.
|
||||
`remoteAddr` is the [multiaddr](https://github.com/multiformats/multiaddr) address used to communicate with the remote peer.
|
||||
`localAddr` is the optional [multiaddr](https://github.com/multiformats/multiaddr) address used by the local peer to reach the remote.
|
||||
`remoteAddr` is the optional [multiaddr](https://github.com/multiformats/multiaddr) address used to communicate with the remote peer.
|
||||
`localPeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the local peer.
|
||||
`remotePeer` is the [PeerId](https://github.com/libp2p/js-peer-id) of the remote peer.
|
||||
`newStream` is the `function` responsible for getting a new muxed+multistream-selected stream.
|
||||
@ -121,6 +121,7 @@ Creates a new Connection instance.
|
||||
- `timeline` is an `object` with the relevant events timestamps of the connection (`open`, `upgraded` and `closed`; the `closed` will be added when the connection is closed).
|
||||
- `multiplexer` is a `string` with the connection multiplexing codec (optional).
|
||||
- `encryption` is a `string` with the connection encryption method identifier (optional).
|
||||
- `status` is a `string` indicating the overall status of the connection. It is one of [`'open'`, `'closing'`, `'closed'`]
|
||||
|
||||
#### Create a new stream
|
||||
|
||||
@ -220,7 +221,17 @@ This getter returns an `Object` with the metadata of the connection, as follows:
|
||||
|
||||
- `status`:
|
||||
|
||||
This property contains the status of the connection. It can be either `open`, `closing` or `closed`. Once the connection is created it is in an `open` status. When a `conn.close()` happens, the status will change to `closing` and finally, after all the connection streams are properly closed, the status will be `closed`.
|
||||
This property contains the status of the connection. It can be either `open`, `closing` or `closed`. Once the connection is created it is in an `open` status. When a `conn.close()` happens, the status will change to `closing` and finally, after all the connection streams are properly closed, the status will be `closed`. These values can also be directly referenced by importing the `status` file:
|
||||
|
||||
```js
|
||||
const {
|
||||
OPEN, CLOSING, CLOSED
|
||||
} = require('libp2p-interfaces/src/connection/status')
|
||||
|
||||
if (connection.stat.status === OPEN) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
- `timeline`:
|
||||
|
||||
|
@ -2,11 +2,55 @@
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const withIs = require('class-is')
|
||||
|
||||
const assert = require('assert')
|
||||
const errCode = require('err-code')
|
||||
const Status = require('./status')
|
||||
|
||||
function validateArgs (localAddr, localPeer, remotePeer, newStream, close, getStreams, stat) {
|
||||
if (localAddr && !multiaddr.isMultiaddr(localAddr)) {
|
||||
throw errCode(new Error('localAddr must be an instance of multiaddr'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!PeerId.isPeerId(localPeer)) {
|
||||
throw errCode(new Error('localPeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!PeerId.isPeerId(remotePeer)) {
|
||||
throw errCode(new Error('remotePeer must be an instance of peer-id'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (typeof newStream !== 'function') {
|
||||
throw errCode(new Error('new stream must be a function'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (typeof close !== 'function') {
|
||||
throw errCode(new Error('close must be a function'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (typeof getStreams !== 'function') {
|
||||
throw errCode(new Error('getStreams must be a function'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!stat) {
|
||||
throw errCode(new Error('connection metadata object must be provided'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (stat.direction !== 'inbound' && stat.direction !== 'outbound') {
|
||||
throw errCode(new Error('direction must be "inbound" or "outbound"'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!stat.timeline) {
|
||||
throw errCode(new Error('connection timeline object must be provided in the stat object'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!stat.timeline.open) {
|
||||
throw errCode(new Error('connection open timestamp must be provided'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
|
||||
if (!stat.timeline.upgraded) {
|
||||
throw errCode(new Error('connection upgraded timestamp must be provided'), 'ERR_INVALID_PARAMETERS')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An implementation of the js-libp2p connection.
|
||||
@ -16,8 +60,8 @@ class Connection {
|
||||
/**
|
||||
* Creates an instance of Connection.
|
||||
* @param {object} properties properties of the connection.
|
||||
* @param {multiaddr} properties.localAddr local multiaddr of the connection.
|
||||
* @param {multiaddr} properties.remoteAddr remote multiaddr of the connection.
|
||||
* @param {multiaddr} [properties.localAddr] local multiaddr of the connection if known.
|
||||
* @param {multiaddr} [properties.remoteAddr] remote multiaddr of the connection.
|
||||
* @param {PeerId} properties.localPeer local peer-id.
|
||||
* @param {PeerId} properties.remotePeer remote peer-id.
|
||||
* @param {function} properties.newStream new stream muxer function.
|
||||
@ -32,18 +76,7 @@ class Connection {
|
||||
* @param {string} [properties.stat.encryption] connection encryption method identifier.
|
||||
*/
|
||||
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
|
||||
assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
|
||||
assert(multiaddr.isMultiaddr(remoteAddr), 'remoteAddr must be an instance of multiaddr')
|
||||
assert(PeerId.isPeerId(localPeer), 'localPeer must be an instance of peer-id')
|
||||
assert(PeerId.isPeerId(remotePeer), 'remotePeer must be an instance of peer-id')
|
||||
assert(typeof newStream === 'function', 'new stream must be a function')
|
||||
assert(typeof close === 'function', 'close must be a function')
|
||||
assert(typeof getStreams === 'function', 'getStreams must be a function')
|
||||
assert(stat, 'connection metadata object must be provided')
|
||||
assert(stat.direction === 'inbound' || stat.direction === 'outbound', 'direction must be "inbound" or "outbound"')
|
||||
assert(stat.timeline, 'connection timeline object must be provided in the stat object')
|
||||
assert(stat.timeline.open, 'connection open timestamp must be provided')
|
||||
assert(stat.timeline.upgraded, 'connection upgraded timestamp must be provided')
|
||||
validateArgs(localAddr, localPeer, remotePeer, newStream, close, getStreams, stat)
|
||||
|
||||
/**
|
||||
* Connection identifier.
|
||||
@ -75,7 +108,7 @@ class Connection {
|
||||
*/
|
||||
this._stat = {
|
||||
...stat,
|
||||
status: 'open'
|
||||
status: Status.OPEN
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,11 +159,11 @@ class Connection {
|
||||
* @return {Promise<object>} with muxed+multistream-selected stream and selected protocol
|
||||
*/
|
||||
async newStream (protocols) {
|
||||
if (this.stat.status === 'closing') {
|
||||
if (this.stat.status === Status.CLOSING) {
|
||||
throw errCode(new Error('the connection is being closed'), 'ERR_CONNECTION_BEING_CLOSED')
|
||||
}
|
||||
|
||||
if (this.stat.status === 'closed') {
|
||||
if (this.stat.status === Status.CLOSED) {
|
||||
throw errCode(new Error('the connection is closed'), 'ERR_CONNECTION_CLOSED')
|
||||
}
|
||||
|
||||
@ -138,7 +171,7 @@ class Connection {
|
||||
|
||||
const { stream, protocol } = await this._newStream(protocols)
|
||||
|
||||
this.addStream(stream, protocol)
|
||||
this.addStream(stream, { protocol })
|
||||
|
||||
return {
|
||||
stream,
|
||||
@ -175,7 +208,7 @@ class Connection {
|
||||
* @return {Promise}
|
||||
*/
|
||||
async close () {
|
||||
if (this.stat.status === 'closed') {
|
||||
if (this.stat.status === Status.CLOSED) {
|
||||
return
|
||||
}
|
||||
|
||||
@ -183,13 +216,13 @@ class Connection {
|
||||
return this._closing
|
||||
}
|
||||
|
||||
this.stat.status = 'closing'
|
||||
this.stat.status = Status.CLOSING
|
||||
|
||||
// Close raw connection
|
||||
this._closing = await this._close()
|
||||
|
||||
this._stat.timeline.close = Date.now()
|
||||
this.stat.status = 'closed'
|
||||
this.stat.status = Status.CLOSED
|
||||
}
|
||||
}
|
||||
|
||||
|
7
src/connection/status.js
Normal file
7
src/connection/status.js
Normal file
@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
module.exports = {
|
||||
OPEN: 'open',
|
||||
CLOSING: 'closing',
|
||||
CLOSED: 'closed'
|
||||
}
|
@ -6,6 +6,7 @@ const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
chai.use(require('dirty-chai'))
|
||||
const sinon = require('sinon')
|
||||
const Status = require('../status')
|
||||
|
||||
module.exports = (test) => {
|
||||
describe('connection', () => {
|
||||
@ -28,7 +29,7 @@ module.exports = (test) => {
|
||||
expect(connection.remotePeer).to.exist()
|
||||
expect(connection.localAddr).to.exist()
|
||||
expect(connection.remoteAddr).to.exist()
|
||||
expect(connection.stat.status).to.equal('open')
|
||||
expect(connection.stat.status).to.equal(Status.OPEN)
|
||||
expect(connection.stat.timeline.open).to.exist()
|
||||
expect(connection.stat.timeline.upgraded).to.exist()
|
||||
expect(connection.stat.timeline.close).to.not.exist()
|
||||
@ -40,7 +41,7 @@ module.exports = (test) => {
|
||||
it('should get the metadata of an open connection', () => {
|
||||
const stat = connection.stat
|
||||
|
||||
expect(stat.status).to.equal('open')
|
||||
expect(stat.status).to.equal(Status.OPEN)
|
||||
expect(stat.direction).to.exist()
|
||||
expect(stat.timeline.open).to.exist()
|
||||
expect(stat.timeline.upgraded).to.exist()
|
||||
@ -103,7 +104,7 @@ module.exports = (test) => {
|
||||
await connection.close()
|
||||
|
||||
expect(connection.stat.timeline.close).to.exist()
|
||||
expect(connection.stat.status).to.equal('closed')
|
||||
expect(connection.stat.status).to.equal(Status.CLOSED)
|
||||
})
|
||||
|
||||
it('should be able to close the connection after opening a stream', async () => {
|
||||
@ -116,7 +117,20 @@ module.exports = (test) => {
|
||||
await connection.close()
|
||||
|
||||
expect(connection.stat.timeline.close).to.exist()
|
||||
expect(connection.stat.status).to.equal('closed')
|
||||
expect(connection.stat.status).to.equal(Status.CLOSED)
|
||||
})
|
||||
|
||||
it('should properly track streams', async () => {
|
||||
// Open stream
|
||||
const protocol = '/echo/0.0.1'
|
||||
const { stream } = await connection.newStream(protocol)
|
||||
const trackedStream = connection.registry.get(stream.id)
|
||||
expect(trackedStream).to.have.property('protocol', protocol)
|
||||
|
||||
// Close stream
|
||||
await stream.close()
|
||||
|
||||
expect(connection.registry.get(stream.id)).to.not.exist()
|
||||
})
|
||||
|
||||
it('should support a proxy on the timeline', async () => {
|
||||
|
@ -12,7 +12,7 @@ Publishing a test suite as a module lets multiple modules all ensure compatibili
|
||||
# Modules that implement the interface
|
||||
|
||||
- [JavaScript libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
|
||||
- [JavaScript libp2p-delegated-peer-routing](https://github.com/libp2p/js-libp2p-delegated-peer-routing)
|
||||
- [JavaScript libp2p-delegated-content-routing](https://github.com/libp2p/js-libp2p-delegated-content-routing)
|
||||
|
||||
# Badge
|
||||
|
||||
@ -30,6 +30,32 @@ TBD
|
||||
|
||||
A valid (read: that follows this abstraction) Content Routing module must implement the following API.
|
||||
|
||||
### `.findProviders`
|
||||
### findProviders
|
||||
|
||||
### `.provide`
|
||||
- `findProviders(cid)`
|
||||
|
||||
Find peers in the network that can provide a specific value, given a key.
|
||||
|
||||
**Parameters**
|
||||
- [CID](https://github.com/multiformats/js-cid)
|
||||
|
||||
**Returns**
|
||||
|
||||
It returns an `AsyncIterable` containing the identification and addresses of the peers providing the given key, as follows:
|
||||
|
||||
`AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>`
|
||||
|
||||
### provide
|
||||
|
||||
- `provide(cid)`
|
||||
|
||||
Announce to the network that we are providing the given value.
|
||||
|
||||
**Parameters**
|
||||
- [CID](https://github.com/multiformats/js-cid)
|
||||
|
||||
**Returns**
|
||||
|
||||
It returns a promise that is resolved on the success of the operation.
|
||||
|
||||
`Promise<void>`
|
||||
|
@ -85,6 +85,7 @@ Common crypto errors come with the interface, and can be imported directly. All
|
||||
```js
|
||||
const {
|
||||
InvalidCryptoExchangeError,
|
||||
InvalidCryptoTransmissionError,
|
||||
UnexpectedPeerError
|
||||
} = require('libp2p-interfaces/src/crypto/errors')
|
||||
|
||||
@ -95,4 +96,5 @@ console.log(error.code === UnexpectedPeerError.code) // true
|
||||
### Error Types
|
||||
|
||||
- `InvalidCryptoExchangeError` - Should be thrown when a peer provides data that is insufficient to finish the crypto exchange.
|
||||
- `InvalidCryptoTransmissionError` - Should be thrown when an error occurs during encryption/decryption.
|
||||
- `UnexpectedPeerError` - Should be thrown when the expected peer id does not match the peer id determined via the crypto exchange.
|
||||
|
@ -22,7 +22,19 @@ class InvalidCryptoExchangeError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
class InvalidCryptoTransmissionError extends Error {
|
||||
constructor (message = 'Invalid crypto transmission') {
|
||||
super(message)
|
||||
this.code = InvalidCryptoTransmissionError.code
|
||||
}
|
||||
|
||||
static get code () {
|
||||
return 'ERR_INVALID_CRYPTO_TRANSMISSION'
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
UnexpectedPeerError,
|
||||
InvalidCryptoExchangeError
|
||||
InvalidCryptoExchangeError,
|
||||
InvalidCryptoTransmissionError
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const duplexPair = require('it-pair/duplex')
|
||||
const pipe = require('it-pipe')
|
||||
const peers = require('../../utils/peers')
|
||||
|
@ -16,6 +16,7 @@ The API is presented with both Node.js and Go primitives, however, there is not
|
||||
- [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)
|
||||
- [TypeScript discv5](https://github.com/chainsafe/discv5)
|
||||
|
||||
Send a PR to add a new one if you happen to find or write one.
|
||||
|
||||
@ -69,6 +70,9 @@ It returns a `Promise`
|
||||
|
||||
### discoverying peers
|
||||
|
||||
- `discovery.on('peer', (peerInfo) => {})`
|
||||
- `discovery.on('peer', (peerData) => {})`
|
||||
|
||||
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).
|
||||
Every time a peer is discovered by a discovery service, it emits a `peer` event with the discovered peer's information, which must contain the following properties:
|
||||
|
||||
- `<`[`PeerId`](https://github.com/libp2p/js-peer-id)`>` `peerData.id`
|
||||
- `<Array<`[`Multiaddr`](https://github.com/multiformats/js-multiaddr)`>>` `peerData.multiaddrs`
|
||||
|
@ -1,17 +1,31 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
chai.use(require('dirty-chai'))
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const delay = require('delay')
|
||||
const pDefer = require('p-defer')
|
||||
|
||||
module.exports = (common) => {
|
||||
describe('interface-peer-discovery', () => {
|
||||
let discovery
|
||||
|
||||
before(async () => {
|
||||
beforeEach(async () => {
|
||||
discovery = await common.setup()
|
||||
})
|
||||
|
||||
after(() => common.teardown && common.teardown())
|
||||
afterEach('ensure discovery was stopped', async () => {
|
||||
await discovery.stop()
|
||||
|
||||
afterEach('ensure discovery was stopped', () => discovery.stop())
|
||||
discovery.removeAllListeners()
|
||||
|
||||
common.teardown && common.teardown()
|
||||
})
|
||||
|
||||
it('can start the service', async () => {
|
||||
await discovery.start()
|
||||
@ -30,5 +44,49 @@ module.exports = (common) => {
|
||||
await discovery.start()
|
||||
await discovery.start()
|
||||
})
|
||||
|
||||
it('should emit a peer event after start', async () => {
|
||||
const defer = pDefer()
|
||||
await discovery.start()
|
||||
|
||||
discovery.on('peer', ({ id, multiaddrs }) => {
|
||||
expect(id).to.exist()
|
||||
expect(PeerId.isPeerId(id)).to.eql(true)
|
||||
expect(multiaddrs).to.exist()
|
||||
|
||||
multiaddrs.forEach((m) => expect(multiaddr.isMultiaddr(m)).to.eql(true))
|
||||
|
||||
defer.resolve()
|
||||
})
|
||||
|
||||
await defer.promise
|
||||
})
|
||||
|
||||
it('should not receive a peer event before start', async () => {
|
||||
discovery.on('peer', () => {
|
||||
throw new Error('should not receive a peer event before start')
|
||||
})
|
||||
|
||||
await delay(2000)
|
||||
})
|
||||
|
||||
it('should not receive a peer event after stop', async () => {
|
||||
const deferStart = pDefer()
|
||||
|
||||
await discovery.start()
|
||||
|
||||
discovery.on('peer', () => {
|
||||
deferStart.resolve()
|
||||
})
|
||||
|
||||
await deferStart.promise
|
||||
await discovery.stop()
|
||||
|
||||
discovery.on('peer', () => {
|
||||
throw new Error('should not receive a peer event after stop')
|
||||
})
|
||||
|
||||
await delay(2000)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -30,12 +30,17 @@ TBD
|
||||
|
||||
A valid (read: that follows this abstraction) Peer Routing module must implement the following API.
|
||||
|
||||
### `.findPeers` - Find peers 'responsible' or 'closest' to a given key
|
||||
### findPeer
|
||||
|
||||
- `Node.js` peerRouting.findPeers(key, function (err, peersPriorityQueue) {})
|
||||
- `findPeer(peerId)`
|
||||
|
||||
In a peer to peer context, the concept of 'responsability' or 'closeness' for a given key translates to having a way to find deterministically or that at least there is a significant overlap between searches, the same group of peers when searching for the same given key.
|
||||
Query the network for all multiaddresses associated with a `PeerId`.
|
||||
|
||||
This method will query the network (route it) and return a Priority Queue datastructe with a list of PeerInfo objects, ordered by 'closeness'.
|
||||
**Parameters**
|
||||
- [peerId](https://github.com/libp2p/js-peer-id).
|
||||
|
||||
key is a multihash
|
||||
**Returns**
|
||||
|
||||
It returns the [peerId](https://github.com/libp2p/js-peer-id) together with the known peers [multiaddrs](https://github.com/multiformats/js-multiaddr), as follows:
|
||||
|
||||
`Promise<{ id: PeerId, multiaddrs: Multiaddr[] }>`
|
||||
|
75
src/record/README.md
Normal file
75
src/record/README.md
Normal file
@ -0,0 +1,75 @@
|
||||
interface-record
|
||||
==================
|
||||
|
||||
A libp2p node needs to store data in a public location (e.g. a DHT), or rely on potentially untrustworthy intermediaries to relay information. Libp2p provides an all-purpose data container called **envelope**, which includes a signature of the data, so that it its authenticity can be verified.
|
||||
|
||||
The record represents the data that will be stored inside the **envelope** when distributing records across the network. The `interface-record` aims to guarantee that any type of record created is compliant with the libp2p **envelope**.
|
||||
|
||||
Taking into account that a record might be used in different contexts, an **envelope** signature made for a specific purpose **must not** be considered valid for a different purpose. Accordingly, each record has a short and descriptive string representing the record use case, known as **domain**. The data to be signed will be prepended with the domain string, in order to create a domain signature.
|
||||
|
||||
A record can also contain a Buffer codec (ideally registered as a [multicodec](https://github.com/multiformats/multicodec)). This codec will prefix the record data in the **envelope** , so that it can be deserialized deterministically.
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const tests = require('libp2p-interfaces/src/record/tests')
|
||||
describe('your record', () => {
|
||||
tests({
|
||||
async setup () {
|
||||
return YourRecord
|
||||
},
|
||||
async teardown () {
|
||||
// cleanup resources created by setup()
|
||||
}
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
## Create Record
|
||||
|
||||
```js
|
||||
const multicodec = require('multicodec')
|
||||
const Record = require('libp2p-interfaces/src/record')
|
||||
// const Protobuf = require('./record.proto')
|
||||
|
||||
const ENVELOPE_DOMAIN_PEER_RECORD = 'libp2p-peer-record'
|
||||
const ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = Buffer.from('0301', 'hex')
|
||||
|
||||
class PeerRecord extends Record {
|
||||
constructor (peerId, multiaddrs, seqNumber) {
|
||||
super (ENVELOPE_DOMAIN_PEER_RECORD, ENVELOPE_PAYLOAD_TYPE_PEER_RECORD)
|
||||
}
|
||||
|
||||
marshal () {
|
||||
// Implement and return using Protobuf
|
||||
}
|
||||
|
||||
equals (other) {
|
||||
// Verify
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### marshal
|
||||
|
||||
- `record.marshal()`
|
||||
|
||||
Marshal a record to be used in a libp2p envelope.
|
||||
|
||||
**Returns**
|
||||
|
||||
It returns a `Protobuf` containing the record data.
|
||||
|
||||
### equals
|
||||
|
||||
- `record.equals(other)`
|
||||
|
||||
Verifies if the other Record is identical to this one.
|
||||
|
||||
**Parameters**
|
||||
- other is a `Record` to compare with the current instance.
|
||||
|
||||
**Returns**
|
||||
- `boolean`
|
35
src/record/index.js
Normal file
35
src/record/index.js
Normal file
@ -0,0 +1,35 @@
|
||||
'use strict'
|
||||
|
||||
const errcode = require('err-code')
|
||||
|
||||
/**
|
||||
* Record is the base implementation of a record that can be used as the payload of a libp2p envelope.
|
||||
*/
|
||||
class Record {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {String} domain signature domain
|
||||
* @param {Buffer} codec identifier of the type of record
|
||||
*/
|
||||
constructor (domain, codec) {
|
||||
this.domain = domain
|
||||
this.codec = codec
|
||||
}
|
||||
|
||||
/**
|
||||
* Marshal a record to be used in an envelope.
|
||||
*/
|
||||
marshal () {
|
||||
throw errcode(new Error('marshal must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if the other provided Record is identical to this one.
|
||||
* @param {Record} other
|
||||
*/
|
||||
equals (other) {
|
||||
throw errcode(new Error('equals must be implemented by the subclass'), 'ERR_NOT_IMPLEMENTED')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Record
|
35
src/record/tests/index.js
Normal file
35
src/record/tests/index.js
Normal file
@ -0,0 +1,35 @@
|
||||
/* eslint-env mocha */
|
||||
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
chai.use(require('dirty-chai'))
|
||||
|
||||
module.exports = (test) => {
|
||||
describe('record', () => {
|
||||
let record
|
||||
|
||||
beforeEach(async () => {
|
||||
record = await test.setup()
|
||||
if (!record) throw new Error('missing record')
|
||||
})
|
||||
|
||||
afterEach(() => test.teardown())
|
||||
|
||||
it('has domain and codec', () => {
|
||||
expect(record.domain).to.exist()
|
||||
expect(record.codec).to.exist()
|
||||
})
|
||||
|
||||
it('is able to marshal', () => {
|
||||
const rawData = record.marshal()
|
||||
expect(Buffer.isBuffer(rawData)).to.eql(true)
|
||||
})
|
||||
|
||||
it('is able to compare two records', () => {
|
||||
const equals = record.equals(record)
|
||||
expect(equals).to.eql(true)
|
||||
})
|
||||
})
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
/* eslint max-nested-callbacks: ["error", 8] */
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const pair = require('it-pair/duplex')
|
||||
const pipe = require('it-pipe')
|
||||
const { consume } = require('streaming-iterables')
|
||||
|
143
src/topology/README.md
Normal file
143
src/topology/README.md
Normal file
@ -0,0 +1,143 @@
|
||||
interface-topology
|
||||
========================
|
||||
|
||||
> Implementation of the topology interface used by the `js-libp2p` registrar.
|
||||
|
||||
Topologies can be used in conjunction with `js-libp2p` to help shape its network and the overlays of its subsystems, such as pubsub and the DHT.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Implementations](#implementations)
|
||||
- [Install](#install)
|
||||
- [Modules using the interface](#modulesUsingTheInterface)
|
||||
- [Usage](#usage)
|
||||
- [Api](#api)
|
||||
|
||||
## Implementations
|
||||
|
||||
### Topology
|
||||
|
||||
A libp2p topology with a group of common peers.
|
||||
|
||||
### Multicodec Topology
|
||||
|
||||
A libp2p topology with a group of peers that support the same protocol.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
$ npm install libp2p-interfaces
|
||||
```
|
||||
|
||||
## Modules using the interface
|
||||
|
||||
TBA
|
||||
|
||||
## Usage
|
||||
|
||||
### Topology
|
||||
|
||||
```js
|
||||
const Topology = require('libp2p-interfaces/src/topology')
|
||||
|
||||
const toplogy = new Topology({
|
||||
min: 0,
|
||||
max: 50
|
||||
})
|
||||
```
|
||||
|
||||
### Multicodec Topology
|
||||
|
||||
```js
|
||||
const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology')
|
||||
|
||||
const toplogy = new MulticodecTopology({
|
||||
min: 0,
|
||||
max: 50,
|
||||
multicodecs: ['/echo/1.0.0'],
|
||||
handlers: {
|
||||
onConnect: (peerId, conn) => {},
|
||||
onDisconnect: (peerId) => {}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
The `MulticodecTopology` extends the `Topology`, which makes the `Topology` API a subset of the `MulticodecTopology` API.
|
||||
|
||||
### Topology
|
||||
|
||||
- `Topology`
|
||||
- `peers<Map<string, PeerId>>`: A Map of peers belonging to the topology.
|
||||
- `disconnect<function(PeerId)>`: Called when a peer has been disconnected
|
||||
|
||||
#### Constructor
|
||||
|
||||
```js
|
||||
const toplogy = new Topology({
|
||||
min: 0,
|
||||
max: 50,
|
||||
handlers: {
|
||||
onConnect: (peerId, conn) => {},
|
||||
onDisconnect: (peerId) => {}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**Parameters**
|
||||
- `properties` is an `Object` containing the properties of the topology.
|
||||
- `min` is a `number` with the minimum needed connections (default: 0)
|
||||
- `max` is a `number` with the maximum needed connections (default: Infinity)
|
||||
- `handlers` is an optional `Object` containing the handler called when a peer is connected or disconnected.
|
||||
- `onConnect` is a `function` called everytime a peer is connected in the topology context.
|
||||
- `onDisconnect` is a `function` called everytime a peer is disconnected in the topology context.
|
||||
|
||||
#### Set a peer
|
||||
|
||||
- `topology.peers.set(id, peerId)`
|
||||
|
||||
Add a peer to the topology.
|
||||
|
||||
**Parameters**
|
||||
- `id` is the `string` that identifies the peer to add.
|
||||
- `peerId` is the [PeerId][peer-id] of the peer to add.
|
||||
|
||||
#### Notify about a peer disconnected event
|
||||
|
||||
- `topology.disconnect(peerId)`
|
||||
|
||||
**Parameters**
|
||||
- `peerId` is the [PeerId][peer-id] of the peer disconnected.
|
||||
|
||||
### Multicodec Topology
|
||||
|
||||
- `MulticodecTopology`
|
||||
- `registrar<Registrar>`: The `Registrar` of the topology. This is set by the `Registrar` during registration.
|
||||
- `peers<Map<string, PeerId>>`: The Map of peers that belong to the topology
|
||||
- `disconnect<function(PeerId)>`: Disconnects a peer from the topology.
|
||||
|
||||
#### Constructor
|
||||
|
||||
```js
|
||||
const toplogy = new MulticodecTopology({
|
||||
min: 0,
|
||||
max: 50,
|
||||
multicodecs: ['/echo/1.0.0'],
|
||||
handlers: {
|
||||
onConnect: (peerId, conn) => {},
|
||||
onDisconnect: (peerId) => {}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
**Parameters**
|
||||
- `properties` is an `Object` containing the properties of the topology.
|
||||
- `min` is a `number` with the minimum needed connections (default: 0)
|
||||
- `max` is a `number` with the maximum needed connections (default: Infinity)
|
||||
- `multicodecs` is a `Array<String>` with the multicodecs associated with the topology.
|
||||
- `handlers` is an optional `Object` containing the handler called when a peer is connected or disconnected.
|
||||
- `onConnect` is a `function` called everytime a peer is connected in the topology context.
|
||||
- `onDisconnect` is a `function` called everytime a peer is disconnected in the topology context.
|
||||
|
||||
[peer-id]: https://github.com/libp2p/js-peer-id
|
50
src/topology/index.js
Normal file
50
src/topology/index.js
Normal file
@ -0,0 +1,50 @@
|
||||
'use strict'
|
||||
|
||||
const withIs = require('class-is')
|
||||
|
||||
const noop = () => {}
|
||||
|
||||
class Topology {
|
||||
/**
|
||||
* @param {Object} props
|
||||
* @param {number} props.min minimum needed connections (default: 0)
|
||||
* @param {number} props.max maximum needed connections (default: Infinity)
|
||||
* @param {Object} [props.handlers]
|
||||
* @param {function} [props.handlers.onConnect] protocol "onConnect" handler
|
||||
* @param {function} [props.handlers.onDisconnect] protocol "onDisconnect" handler
|
||||
* @constructor
|
||||
*/
|
||||
constructor ({
|
||||
min = 0,
|
||||
max = Infinity,
|
||||
handlers = {}
|
||||
}) {
|
||||
this.min = min
|
||||
this.max = max
|
||||
|
||||
// Handlers
|
||||
this._onConnect = handlers.onConnect || noop
|
||||
this._onDisconnect = handlers.onDisconnect || noop
|
||||
|
||||
/**
|
||||
* Set of peers that support the protocol.
|
||||
* @type {Set<string>}
|
||||
*/
|
||||
this.peers = new Set()
|
||||
}
|
||||
|
||||
set registrar (registrar) {
|
||||
this._registrar = registrar
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify about peer disconnected event.
|
||||
* @param {PeerId} peerId
|
||||
* @returns {void}
|
||||
*/
|
||||
disconnect (peerId) {
|
||||
this._onDisconnect(peerId)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = withIs(Topology, { className: 'Topology', symbolName: '@libp2p/js-interfaces/topology' })
|
123
src/topology/multicodec-topology.js
Normal file
123
src/topology/multicodec-topology.js
Normal file
@ -0,0 +1,123 @@
|
||||
'use strict'
|
||||
|
||||
const withIs = require('class-is')
|
||||
|
||||
const Topology = require('./index')
|
||||
|
||||
class MulticodecTopology extends Topology {
|
||||
/**
|
||||
* @param {Object} props
|
||||
* @param {number} props.min minimum needed connections (default: 0)
|
||||
* @param {number} props.max maximum needed connections (default: Infinity)
|
||||
* @param {Array<string>} props.multicodecs protocol multicodecs
|
||||
* @param {Object} props.handlers
|
||||
* @param {function} props.handlers.onConnect protocol "onConnect" handler
|
||||
* @param {function} props.handlers.onDisconnect protocol "onDisconnect" handler
|
||||
* @constructor
|
||||
*/
|
||||
constructor ({
|
||||
min,
|
||||
max,
|
||||
multicodecs,
|
||||
handlers
|
||||
}) {
|
||||
super({ min, max, handlers })
|
||||
|
||||
if (!multicodecs) {
|
||||
throw new Error('one or more multicodec should be provided')
|
||||
}
|
||||
|
||||
if (!handlers) {
|
||||
throw new Error('the handlers should be provided')
|
||||
}
|
||||
|
||||
if (typeof handlers.onConnect !== 'function') {
|
||||
throw new Error('the \'onConnect\' handler must be provided')
|
||||
}
|
||||
|
||||
if (typeof handlers.onDisconnect !== 'function') {
|
||||
throw new Error('the \'onDisconnect\' handler must be provided')
|
||||
}
|
||||
|
||||
this.multicodecs = Array.isArray(multicodecs) ? multicodecs : [multicodecs]
|
||||
this._registrar = undefined
|
||||
|
||||
this._onProtocolChange = this._onProtocolChange.bind(this)
|
||||
this._onPeerConnect = this._onPeerConnect.bind(this)
|
||||
}
|
||||
|
||||
set registrar (registrar) {
|
||||
this._registrar = registrar
|
||||
this._registrar.peerStore.on('change:protocols', this._onProtocolChange)
|
||||
this._registrar.connectionManager.on('peer:connect', this._onPeerConnect)
|
||||
|
||||
// Update topology peers
|
||||
this._updatePeers(this._registrar.peerStore.peers.values())
|
||||
}
|
||||
|
||||
/**
|
||||
* Update topology.
|
||||
* @param {Array<{id: PeerId, multiaddrs: Array<Multiaddr>, protocols: Array<string>}>} peerDataIterable
|
||||
* @returns {void}
|
||||
*/
|
||||
_updatePeers (peerDataIterable) {
|
||||
for (const { id, protocols } of peerDataIterable) {
|
||||
if (this.multicodecs.filter(multicodec => protocols.includes(multicodec)).length) {
|
||||
// Add the peer regardless of whether or not there is currently a connection
|
||||
this.peers.add(id.toB58String())
|
||||
// If there is a connection, call _onConnect
|
||||
const connection = this._registrar.getConnection(id)
|
||||
connection && this._onConnect(id, connection)
|
||||
} else {
|
||||
// Remove any peers we might be tracking that are no longer of value to us
|
||||
this.peers.delete(id.toB58String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a new peer support the multicodecs for this topology.
|
||||
* @param {Object} props
|
||||
* @param {PeerId} props.peerId
|
||||
* @param {Array<string>} props.protocols
|
||||
*/
|
||||
_onProtocolChange ({ peerId, protocols }) {
|
||||
const hadPeer = this.peers.has(peerId.toB58String())
|
||||
const hasProtocol = protocols.filter(protocol => this.multicodecs.includes(protocol))
|
||||
|
||||
// Not supporting the protocol anymore?
|
||||
if (hadPeer && hasProtocol.length === 0) {
|
||||
this._onDisconnect(peerId)
|
||||
}
|
||||
|
||||
// New to protocol support
|
||||
for (const protocol of protocols) {
|
||||
if (this.multicodecs.includes(protocol)) {
|
||||
const peerData = this._registrar.peerStore.get(peerId)
|
||||
this._updatePeers([peerData])
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if a new connected peer has a topology multicodec and call _onConnect.
|
||||
* @param {Connection} connection
|
||||
* @returns {void}
|
||||
*/
|
||||
_onPeerConnect (connection) {
|
||||
const peerId = connection.remotePeer
|
||||
const protocols = this._registrar.peerStore.protoBook.get(peerId)
|
||||
|
||||
if (!protocols) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this.multicodecs.find(multicodec => protocols.includes(multicodec))) {
|
||||
this.peers.add(peerId.toB58String())
|
||||
this._onConnect(peerId, connection)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = withIs(MulticodecTopology, { className: 'MulticodecTopology', symbolName: '@libp2p/js-interfaces/topology/multicodec-topology' })
|
133
src/topology/tests/multicodec-topology.js
Normal file
133
src/topology/tests/multicodec-topology.js
Normal file
@ -0,0 +1,133 @@
|
||||
/* eslint-env mocha */
|
||||
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
chai.use(require('dirty-chai'))
|
||||
const sinon = require('sinon')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const peers = require('../../utils/peers')
|
||||
|
||||
module.exports = (test) => {
|
||||
describe('multicodec topology', () => {
|
||||
let topology, id
|
||||
|
||||
beforeEach(async () => {
|
||||
topology = await test.setup()
|
||||
if (!topology) throw new Error('missing multicodec topology')
|
||||
|
||||
id = await PeerId.createFromJSON(peers[0])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
sinon.restore()
|
||||
await test.teardown()
|
||||
})
|
||||
|
||||
it('should have properties set', () => {
|
||||
expect(topology.multicodecs).to.exist()
|
||||
expect(topology._onConnect).to.exist()
|
||||
expect(topology._onDisconnect).to.exist()
|
||||
expect(topology.peers).to.exist()
|
||||
expect(topology._registrar).to.exist()
|
||||
})
|
||||
|
||||
it('should trigger "onDisconnect" on peer disconnected', () => {
|
||||
sinon.spy(topology, '_onDisconnect')
|
||||
topology.disconnect(id)
|
||||
|
||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||
})
|
||||
|
||||
it('should update peers on protocol change', async () => {
|
||||
sinon.spy(topology, '_updatePeers')
|
||||
expect(topology.peers.size).to.eql(0)
|
||||
|
||||
const peerStore = topology._registrar.peerStore
|
||||
|
||||
const id2 = await PeerId.createFromJSON(peers[1])
|
||||
peerStore.peers.set(id2.toB58String(), {
|
||||
id: id2,
|
||||
protocols: Array.from(topology.multicodecs)
|
||||
})
|
||||
|
||||
peerStore.emit('change:protocols', {
|
||||
peerId: id2,
|
||||
protocols: Array.from(topology.multicodecs)
|
||||
})
|
||||
|
||||
expect(topology._updatePeers.callCount).to.equal(1)
|
||||
expect(topology.peers.size).to.eql(1)
|
||||
})
|
||||
|
||||
it('should disconnect if peer no longer supports a protocol', async () => {
|
||||
sinon.spy(topology, '_onDisconnect')
|
||||
expect(topology.peers.size).to.eql(0)
|
||||
|
||||
const peerStore = topology._registrar.peerStore
|
||||
|
||||
const id2 = await PeerId.createFromJSON(peers[1])
|
||||
peerStore.peers.set(id2.toB58String(), {
|
||||
id: id2,
|
||||
protocols: Array.from(topology.multicodecs)
|
||||
})
|
||||
|
||||
peerStore.emit('change:protocols', {
|
||||
peerId: id2,
|
||||
protocols: Array.from(topology.multicodecs)
|
||||
})
|
||||
|
||||
expect(topology.peers.size).to.eql(1)
|
||||
|
||||
peerStore.peers.set(id2.toB58String(), {
|
||||
id: id2,
|
||||
protocols: []
|
||||
})
|
||||
// Peer does not support the protocol anymore
|
||||
peerStore.emit('change:protocols', {
|
||||
peerId: id2,
|
||||
protocols: []
|
||||
})
|
||||
|
||||
expect(topology.peers.size).to.eql(1)
|
||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||
expect(topology._onDisconnect.calledWith(id2)).to.equal(true)
|
||||
})
|
||||
|
||||
it('should trigger "onConnect" when a peer connects and has one of the topology multicodecs in its known protocols', () => {
|
||||
sinon.spy(topology, '_onConnect')
|
||||
sinon.stub(topology._registrar.peerStore.protoBook, 'get').returns(topology.multicodecs)
|
||||
|
||||
topology._registrar.connectionManager.emit('peer:connect', {
|
||||
remotePeer: id
|
||||
})
|
||||
|
||||
expect(topology._onConnect.callCount).to.equal(1)
|
||||
})
|
||||
|
||||
it('should not trigger "onConnect" when a peer connects and has none of the topology multicodecs in its known protocols', () => {
|
||||
sinon.spy(topology, '_onConnect')
|
||||
sinon.stub(topology._registrar.peerStore.protoBook, 'get').returns([])
|
||||
|
||||
topology._registrar.connectionManager.emit('peer:connect', {
|
||||
remotePeer: id
|
||||
})
|
||||
|
||||
expect(topology._onConnect.callCount).to.equal(0)
|
||||
})
|
||||
|
||||
it('should not trigger "onConnect" when a peer connects and its protocols are not known', () => {
|
||||
sinon.spy(topology, '_onConnect')
|
||||
sinon.stub(topology._registrar.peerStore.protoBook, 'get').returns(undefined)
|
||||
|
||||
topology._registrar.connectionManager.emit('peer:connect', {
|
||||
remotePeer: id
|
||||
})
|
||||
|
||||
expect(topology._onConnect.callCount).to.equal(0)
|
||||
})
|
||||
})
|
||||
}
|
44
src/topology/tests/topology.js
Normal file
44
src/topology/tests/topology.js
Normal file
@ -0,0 +1,44 @@
|
||||
/* eslint-env mocha */
|
||||
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const expect = chai.expect
|
||||
chai.use(require('dirty-chai'))
|
||||
const sinon = require('sinon')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const peers = require('../../utils/peers')
|
||||
|
||||
module.exports = (test) => {
|
||||
describe('topology', () => {
|
||||
let topology, id
|
||||
|
||||
beforeEach(async () => {
|
||||
topology = await test.setup()
|
||||
if (!topology) throw new Error('missing multicodec topology')
|
||||
|
||||
id = await PeerId.createFromJSON(peers[0])
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
sinon.restore()
|
||||
await test.teardown()
|
||||
})
|
||||
|
||||
it('should have properties set', () => {
|
||||
expect(topology.min).to.exist()
|
||||
expect(topology.max).to.exist()
|
||||
expect(topology._onConnect).to.exist()
|
||||
expect(topology._onDisconnect).to.exist()
|
||||
expect(topology.peers).to.exist()
|
||||
})
|
||||
|
||||
it('should trigger "onDisconnect" on peer disconnected', () => {
|
||||
sinon.spy(topology, '_onDisconnect')
|
||||
topology.disconnect(id)
|
||||
|
||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||
})
|
||||
})
|
||||
}
|
@ -31,7 +31,8 @@ module.exports = (common) => {
|
||||
}
|
||||
}
|
||||
|
||||
describe('dial', () => {
|
||||
describe('dial', function () {
|
||||
this.timeout(20 * 1000)
|
||||
let addrs
|
||||
let transport
|
||||
let connector
|
||||
@ -129,7 +130,7 @@ module.exports = (common) => {
|
||||
expect.fail('Did not throw error with code ' + AbortError.code)
|
||||
})
|
||||
|
||||
it('abort while reading throws AbortError', async () => {
|
||||
it.skip('abort while reading throws AbortError', async () => {
|
||||
// Add a delay to the response from the server
|
||||
async function * delayedResponse (source) {
|
||||
for await (const val of source) {
|
||||
|
@ -2,12 +2,14 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
const sinon = require('sinon')
|
||||
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const pipe = require('it-pipe')
|
||||
const { isValidTick } = require('./utils')
|
||||
|
||||
@ -29,7 +31,8 @@ module.exports = (common) => {
|
||||
}
|
||||
}
|
||||
|
||||
describe('listen', () => {
|
||||
describe('listen', function () {
|
||||
this.timeout(20 * 1000)
|
||||
let addrs
|
||||
let transport
|
||||
|
||||
@ -49,7 +52,7 @@ module.exports = (common) => {
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
it('close listener with connections, through timeout', async () => {
|
||||
it.skip('close listener with connections, through timeout', async () => {
|
||||
const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound')
|
||||
const listenerConns = []
|
||||
|
||||
@ -69,7 +72,7 @@ module.exports = (common) => {
|
||||
])
|
||||
|
||||
// Give the listener a chance to finish its upgrade
|
||||
await new Promise(resolve => setTimeout(resolve, 0))
|
||||
await pWaitFor(() => listenerConns.length === 2)
|
||||
|
||||
// Wait for the data send and close to finish
|
||||
await Promise.all([
|
||||
@ -92,6 +95,23 @@ module.exports = (common) => {
|
||||
expect(upgradeSpy.callCount).to.equal(2)
|
||||
})
|
||||
|
||||
it.skip('should not handle connection if upgradeInbound throws', async () => {
|
||||
sinon.stub(upgrader, 'upgradeInbound').throws()
|
||||
|
||||
const listener = transport.createListener(() => {
|
||||
throw new Error('should not handle the connection if upgradeInbound throws')
|
||||
})
|
||||
|
||||
// Listen
|
||||
await listener.listen(addrs[0])
|
||||
|
||||
// Create a connection to the listener
|
||||
const socket = await transport.dial(addrs[0])
|
||||
|
||||
await pWaitFor(() => typeof socket.timeline.close === 'number')
|
||||
await listener.close()
|
||||
})
|
||||
|
||||
describe('events', () => {
|
||||
it('connection', (done) => {
|
||||
const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound')
|
||||
|
@ -25,7 +25,7 @@ describe('compliance tests', () => {
|
||||
const openStreams = []
|
||||
let streamId = 0
|
||||
|
||||
return new Connection({
|
||||
const connection = new Connection({
|
||||
localPeer,
|
||||
remotePeer,
|
||||
localAddr,
|
||||
@ -43,7 +43,10 @@ describe('compliance tests', () => {
|
||||
const id = streamId++
|
||||
const stream = pair()
|
||||
|
||||
stream.close = () => stream.sink([])
|
||||
stream.close = async () => {
|
||||
await stream.sink([])
|
||||
connection.removeStream(stream.id)
|
||||
}
|
||||
stream.id = id
|
||||
|
||||
openStreams.push(stream)
|
||||
@ -57,6 +60,7 @@ describe('compliance tests', () => {
|
||||
getStreams: () => openStreams,
|
||||
...properties
|
||||
})
|
||||
return connection
|
||||
},
|
||||
async teardown () {
|
||||
// cleanup resources created by setup()
|
||||
|
48
test/connection/index.spec.js
Normal file
48
test/connection/index.spec.js
Normal file
@ -0,0 +1,48 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const { Connection } = require('../../src/connection')
|
||||
const peers = require('../../src/utils/peers')
|
||||
const PeerId = require('peer-id')
|
||||
const pair = require('it-pair')
|
||||
|
||||
describe('connection tests', () => {
|
||||
it('should not require local or remote addrs', async () => {
|
||||
const [localPeer, remotePeer] = await Promise.all([
|
||||
PeerId.createFromJSON(peers[0]),
|
||||
PeerId.createFromJSON(peers[1])
|
||||
])
|
||||
const openStreams = []
|
||||
let streamId = 0
|
||||
|
||||
return new Connection({
|
||||
localPeer,
|
||||
remotePeer,
|
||||
stat: {
|
||||
timeline: {
|
||||
open: Date.now() - 10,
|
||||
upgraded: Date.now()
|
||||
},
|
||||
direction: 'outbound',
|
||||
encryption: '/secio/1.0.0',
|
||||
multiplexer: '/mplex/6.7.0'
|
||||
},
|
||||
newStream: (protocols) => {
|
||||
const id = streamId++
|
||||
const stream = pair()
|
||||
|
||||
stream.close = () => stream.sink([])
|
||||
stream.id = id
|
||||
|
||||
openStreams.push(stream)
|
||||
|
||||
return {
|
||||
stream,
|
||||
protocol: protocols[0]
|
||||
}
|
||||
},
|
||||
close: () => {},
|
||||
getStreams: () => openStreams
|
||||
})
|
||||
})
|
||||
})
|
@ -5,13 +5,20 @@ const tests = require('../../src/peer-discovery/tests')
|
||||
const MockDiscovery = require('./mock-discovery')
|
||||
|
||||
describe('compliance tests', () => {
|
||||
let intervalId
|
||||
|
||||
tests({
|
||||
async setup () {
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
return new MockDiscovery()
|
||||
setup () {
|
||||
const mockDiscovery = new MockDiscovery({
|
||||
discoveryDelay: 1
|
||||
})
|
||||
|
||||
intervalId = setInterval(mockDiscovery._discoverPeer, 1000)
|
||||
|
||||
return mockDiscovery
|
||||
},
|
||||
async teardown () {
|
||||
await new Promise(resolve => setTimeout(resolve, 10))
|
||||
teardown () {
|
||||
clearInterval(intervalId)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
@ -2,8 +2,8 @@
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
|
||||
/**
|
||||
* Emits 'peer' events on discovery.
|
||||
@ -37,10 +37,12 @@ class MockDiscovery extends EventEmitter {
|
||||
if (!this._isRunning) return
|
||||
|
||||
const peerId = await PeerId.create({ bits: 512 })
|
||||
const peerInfo = new PeerInfo(peerId)
|
||||
|
||||
this._timer = setTimeout(() => {
|
||||
this.emit('peer', peerInfo)
|
||||
this.emit('peer', {
|
||||
id: peerId,
|
||||
multiaddrs: [multiaddr('/ip4/127.0.0.1/tcp/8000')]
|
||||
})
|
||||
}, this.options.discoveryDelay || 1000)
|
||||
}
|
||||
}
|
||||
|
19
test/topology/mock-peer-store.js
Normal file
19
test/topology/mock-peer-store.js
Normal file
@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
|
||||
class MockPeerStore extends EventEmitter {
|
||||
constructor (peers) {
|
||||
super()
|
||||
this.peers = peers
|
||||
this.protoBook = {
|
||||
get: () => {}
|
||||
}
|
||||
}
|
||||
|
||||
get (peerId) {
|
||||
return this.peers.get(peerId.toB58String())
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MockPeerStore
|
45
test/topology/multicodec-topology.spec.js
Normal file
45
test/topology/multicodec-topology.spec.js
Normal file
@ -0,0 +1,45 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
|
||||
const tests = require('../../src/topology/tests/multicodec-topology')
|
||||
const MulticodecTopology = require('../../src/topology/multicodec-topology')
|
||||
const MockPeerStore = require('./mock-peer-store')
|
||||
|
||||
describe('multicodec topology compliance tests', () => {
|
||||
tests({
|
||||
setup (properties, registrar) {
|
||||
const multicodecs = ['/echo/1.0.0']
|
||||
const handlers = {
|
||||
onConnect: () => { },
|
||||
onDisconnect: () => { }
|
||||
}
|
||||
|
||||
const topology = new MulticodecTopology({
|
||||
multicodecs,
|
||||
handlers,
|
||||
...properties
|
||||
})
|
||||
|
||||
if (!registrar) {
|
||||
const peers = new Map()
|
||||
const peerStore = new MockPeerStore(peers)
|
||||
const connectionManager = new EventEmitter()
|
||||
|
||||
registrar = {
|
||||
peerStore,
|
||||
connectionManager,
|
||||
getConnection: () => { }
|
||||
}
|
||||
}
|
||||
|
||||
topology.registrar = registrar
|
||||
|
||||
return topology
|
||||
},
|
||||
teardown () {
|
||||
// cleanup resources created by setup()
|
||||
}
|
||||
})
|
||||
})
|
26
test/topology/topology.spec.js
Normal file
26
test/topology/topology.spec.js
Normal file
@ -0,0 +1,26 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const tests = require('../../src/topology/tests/topology')
|
||||
const Topology = require('../../src/topology')
|
||||
|
||||
describe('topology compliance tests', () => {
|
||||
tests({
|
||||
setup (properties) {
|
||||
const handlers = {
|
||||
onConnect: () => { },
|
||||
onDisconnect: () => { }
|
||||
}
|
||||
|
||||
const topology = new Topology({
|
||||
handlers,
|
||||
...properties
|
||||
})
|
||||
|
||||
return topology
|
||||
},
|
||||
teardown () {
|
||||
// cleanup resources created by setup()
|
||||
}
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user