mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-07-23 02:32:12 +00:00
Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
c4be5eed4b | ||
|
9e0d23de9c | ||
|
4130e7f098 | ||
|
d6376377d3 | ||
|
a60ae091e5 | ||
|
4902542e78 | ||
|
4d5f56d06f | ||
|
13aa6cbfa0 | ||
|
a8ba13da4b | ||
|
75f6777d89 | ||
|
71b813ad3b | ||
|
46589ce3d0 | ||
|
f2a18818f2 | ||
|
1cc943e1b2 | ||
|
4adedcc4bf | ||
|
0628d708c4 | ||
|
e10a1545c8 | ||
|
9fbf9d0331 | ||
|
aa996d2054 | ||
|
507013a724 | ||
|
a55c7c454a | ||
|
87e2e89791 | ||
|
5bcfc966f7 | ||
|
c8c249de6e | ||
|
5b138ef0a0 | ||
|
bdd2502ef6 | ||
|
1bef8d5d78 | ||
|
9a8f375d40 |
79
CHANGELOG.md
79
CHANGELOG.md
@@ -1,3 +1,82 @@
|
|||||||
|
<a name="0.5.0"></a>
|
||||||
|
# [0.5.0](https://github.com/libp2p/js-interfaces/compare/v0.4.1...v0.5.0) (2020-08-24)
|
||||||
|
|
||||||
|
|
||||||
|
### Bug Fixes
|
||||||
|
|
||||||
|
* replace remaining Buffer usage with Uint8Array ([#62](https://github.com/libp2p/js-interfaces/issues/62)) ([4130e7f](https://github.com/libp2p/js-interfaces/commit/4130e7f))
|
||||||
|
|
||||||
|
|
||||||
|
### BREAKING CHANGES
|
||||||
|
|
||||||
|
* records now marshal as Uint8Array instead of Buffer
|
||||||
|
|
||||||
|
* fix: refactor remaining Buffer usage to Uint8Array
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.4.1"></a>
|
||||||
|
## [0.4.1](https://github.com/libp2p/js-interfaces/compare/v0.4.0...v0.4.1) (2020-08-11)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<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>
|
<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)
|
# [0.3.0](https://github.com/libp2p/js-interfaces/compare/v0.2.8...v0.3.0) (2020-04-21)
|
||||||
|
|
||||||
|
@@ -18,6 +18,7 @@
|
|||||||
- [Crypto](./src/crypto)
|
- [Crypto](./src/crypto)
|
||||||
- [Peer Discovery](./src/peer-discovery)
|
- [Peer Discovery](./src/peer-discovery)
|
||||||
- [Peer Routing](./src/peer-routing)
|
- [Peer Routing](./src/peer-routing)
|
||||||
|
- [Record](./src/record)
|
||||||
- [Stream Muxer](./src/stream-muxer)
|
- [Stream Muxer](./src/stream-muxer)
|
||||||
- [Topology](./src/topology)
|
- [Topology](./src/topology)
|
||||||
- [Transport](./src/transport)
|
- [Transport](./src/transport)
|
||||||
|
17
package.json
17
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-interfaces",
|
"name": "libp2p-interfaces",
|
||||||
"version": "0.3.0",
|
"version": "0.5.0",
|
||||||
"description": "Interfaces for JS Libp2p",
|
"description": "Interfaces for JS Libp2p",
|
||||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
@@ -35,7 +35,6 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"abort-controller": "^3.0.0",
|
"abort-controller": "^3.0.0",
|
||||||
"abortable-iterator": "^3.0.0",
|
"abortable-iterator": "^3.0.0",
|
||||||
"buffer": "^5.6.0",
|
|
||||||
"chai": "^4.2.0",
|
"chai": "^4.2.0",
|
||||||
"chai-checkmark": "^1.0.1",
|
"chai-checkmark": "^1.0.1",
|
||||||
"class-is": "^1.1.0",
|
"class-is": "^1.1.0",
|
||||||
@@ -45,19 +44,19 @@
|
|||||||
"err-code": "^2.0.0",
|
"err-code": "^2.0.0",
|
||||||
"it-goodbye": "^2.0.1",
|
"it-goodbye": "^2.0.1",
|
||||||
"it-pair": "^1.0.0",
|
"it-pair": "^1.0.0",
|
||||||
"it-pipe": "^1.0.1",
|
"it-pipe": "^1.1.0",
|
||||||
"libp2p-tcp": "^0.14.1",
|
"libp2p-tcp": "^0.15.0",
|
||||||
"multiaddr": "^7.4.3",
|
"multiaddr": "^8.0.0",
|
||||||
"p-defer": "^3.0.0",
|
"p-defer": "^3.0.0",
|
||||||
"p-limit": "^2.3.0",
|
"p-limit": "^2.3.0",
|
||||||
"p-wait-for": "^3.1.0",
|
"p-wait-for": "^3.1.0",
|
||||||
"peer-id": "^0.13.11",
|
"peer-id": "^0.14.0",
|
||||||
"peer-info": "^0.17.0",
|
|
||||||
"sinon": "^9.0.2",
|
"sinon": "^9.0.2",
|
||||||
"streaming-iterables": "^4.1.0"
|
"streaming-iterables": "^5.0.2",
|
||||||
|
"uint8arrays": "^1.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^21.9.0",
|
"aegir": "^25.0.0",
|
||||||
"it-handshake": "^1.0.1"
|
"it-handshake": "^1.0.1"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
|
@@ -43,7 +43,7 @@ Find peers in the network that can provide a specific value, given a key.
|
|||||||
|
|
||||||
It returns an `AsyncIterable` containing the identification and addresses of the peers providing the given key, as follows:
|
It returns an `AsyncIterable` containing the identification and addresses of the peers providing the given key, as follows:
|
||||||
|
|
||||||
`AsyncIterable<{ id: PeerId, addrs: Multiaddr[] }>`
|
`AsyncIterable<{ id: PeerId, multiaddrs: Multiaddr[] }>`
|
||||||
|
|
||||||
### provide
|
### provide
|
||||||
|
|
||||||
|
@@ -1,17 +1,19 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { Buffer } = require('buffer')
|
|
||||||
const duplexPair = require('it-pair/duplex')
|
|
||||||
const pipe = require('it-pipe')
|
|
||||||
const peers = require('../../utils/peers')
|
|
||||||
const { UnexpectedPeerError } = require('../errors')
|
|
||||||
const PeerId = require('peer-id')
|
|
||||||
const { collect } = require('streaming-iterables')
|
|
||||||
const chai = require('chai')
|
const chai = require('chai')
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
chai.use(require('dirty-chai'))
|
chai.use(require('dirty-chai'))
|
||||||
|
|
||||||
|
const duplexPair = require('it-pair/duplex')
|
||||||
|
const pipe = require('it-pipe')
|
||||||
|
const PeerId = require('peer-id')
|
||||||
|
const { collect } = require('streaming-iterables')
|
||||||
|
const uint8arrayFromString = require('uint8arrays/from-string')
|
||||||
|
|
||||||
|
const peers = require('../../utils/peers')
|
||||||
|
const { UnexpectedPeerError } = require('../errors')
|
||||||
|
|
||||||
module.exports = (common) => {
|
module.exports = (common) => {
|
||||||
describe('interface-crypto', () => {
|
describe('interface-crypto', () => {
|
||||||
let crypto
|
let crypto
|
||||||
@@ -55,7 +57,7 @@ module.exports = (common) => {
|
|||||||
pipe(inboundResult.conn, inboundResult.conn)
|
pipe(inboundResult.conn, inboundResult.conn)
|
||||||
|
|
||||||
// Send some data and collect the result
|
// Send some data and collect the result
|
||||||
const input = Buffer.from('data to encrypt')
|
const input = uint8arrayFromString('data to encrypt')
|
||||||
const result = await pipe(
|
const result = await pipe(
|
||||||
[input],
|
[input],
|
||||||
outboundResult.conn,
|
outboundResult.conn,
|
||||||
|
@@ -33,19 +33,19 @@ Include this badge in your readme if you make a new module that uses interface-p
|
|||||||
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:
|
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
|
```js
|
||||||
const test = require('interface-discovery')
|
const tests = require('libp2p-interfaces/src/peer-discovery/tests')
|
||||||
|
|
||||||
const common = {
|
describe('your discovery', () => {
|
||||||
setup () {
|
// use all of the test suits
|
||||||
return YourDiscovery
|
tests({
|
||||||
},
|
setup () {
|
||||||
teardown () {
|
return YourDiscovery
|
||||||
// Clean up any resources created by setup()
|
},
|
||||||
}
|
teardown () {
|
||||||
}
|
// Clean up any resources created by setup()
|
||||||
|
}
|
||||||
// use all of the test suits
|
})
|
||||||
test(common)
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
@@ -43,4 +43,4 @@ Query the network for all multiaddresses associated with a `PeerId`.
|
|||||||
|
|
||||||
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:
|
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, addrs: Multiaddr[] }>`
|
`Promise<{ id: PeerId, multiaddrs: Multiaddr[] }>`
|
||||||
|
76
src/record/README.md
Normal file
76
src/record/README.md
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
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 Uint8Array 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 fromString = require('uint8arrays/from-string')
|
||||||
|
// const Protobuf = require('./record.proto')
|
||||||
|
|
||||||
|
const ENVELOPE_DOMAIN_PEER_RECORD = 'libp2p-peer-record'
|
||||||
|
const ENVELOPE_PAYLOAD_TYPE_PEER_RECORD = fromString('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 {Uint8Array} 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(rawData).to.be.an.instanceof(Uint8Array)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('is able to compare two records', () => {
|
||||||
|
const equals = record.equals(record)
|
||||||
|
expect(equals).to.eql(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
@@ -2,7 +2,6 @@
|
|||||||
/* eslint max-nested-callbacks: ["error", 8] */
|
/* eslint max-nested-callbacks: ["error", 8] */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { Buffer } = require('buffer')
|
|
||||||
const pair = require('it-pair/duplex')
|
const pair = require('it-pair/duplex')
|
||||||
const pipe = require('it-pipe')
|
const pipe = require('it-pipe')
|
||||||
const { consume } = require('streaming-iterables')
|
const { consume } = require('streaming-iterables')
|
||||||
@@ -10,6 +9,7 @@ const Tcp = require('libp2p-tcp')
|
|||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
const abortable = require('abortable-iterator')
|
const abortable = require('abortable-iterator')
|
||||||
const AbortController = require('abort-controller')
|
const AbortController = require('abort-controller')
|
||||||
|
const uint8arrayFromString = require('uint8arrays/from-string')
|
||||||
|
|
||||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
|
const mh = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ function pause (ms) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function randomBuffer () {
|
function randomBuffer () {
|
||||||
return Buffer.from(Math.random().toString())
|
return uint8arrayFromString(Math.random().toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
const infiniteRandom = {
|
const infiniteRandom = {
|
||||||
|
@@ -43,11 +43,13 @@ class MulticodecTopology extends Topology {
|
|||||||
this._registrar = undefined
|
this._registrar = undefined
|
||||||
|
|
||||||
this._onProtocolChange = this._onProtocolChange.bind(this)
|
this._onProtocolChange = this._onProtocolChange.bind(this)
|
||||||
|
this._onPeerConnect = this._onPeerConnect.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
set registrar (registrar) {
|
set registrar (registrar) {
|
||||||
this._registrar = registrar
|
this._registrar = registrar
|
||||||
this._registrar.peerStore.on('change:protocols', this._onProtocolChange)
|
this._registrar.peerStore.on('change:protocols', this._onProtocolChange)
|
||||||
|
this._registrar.connectionManager.on('peer:connect', this._onPeerConnect)
|
||||||
|
|
||||||
// Update topology peers
|
// Update topology peers
|
||||||
this._updatePeers(this._registrar.peerStore.peers.values())
|
this._updatePeers(this._registrar.peerStore.peers.values())
|
||||||
@@ -97,6 +99,25 @@ class MulticodecTopology extends Topology {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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' })
|
module.exports = withIs(MulticodecTopology, { className: 'MulticodecTopology', symbolName: '@libp2p/js-interfaces/topology/multicodec-topology' })
|
||||||
|
@@ -96,5 +96,38 @@ module.exports = (test) => {
|
|||||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||||
expect(topology._onDisconnect.calledWith(id2)).to.equal(true)
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,6 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const { Buffer } = require('buffer')
|
|
||||||
const chai = require('chai')
|
const chai = require('chai')
|
||||||
const dirtyChai = require('dirty-chai')
|
const dirtyChai = require('dirty-chai')
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
@@ -11,6 +10,7 @@ const sinon = require('sinon')
|
|||||||
|
|
||||||
const pWaitFor = require('p-wait-for')
|
const pWaitFor = require('p-wait-for')
|
||||||
const pipe = require('it-pipe')
|
const pipe = require('it-pipe')
|
||||||
|
const uint8arrayFromString = require('uint8arrays/from-string')
|
||||||
const { isValidTick } = require('./utils')
|
const { isValidTick } = require('./utils')
|
||||||
|
|
||||||
module.exports = (common) => {
|
module.exports = (common) => {
|
||||||
@@ -76,7 +76,7 @@ module.exports = (common) => {
|
|||||||
// Wait for the data send and close to finish
|
// Wait for the data send and close to finish
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
pipe(
|
pipe(
|
||||||
[Buffer.from('Some data that is never handled')],
|
[uint8arrayFromString('Some data that is never handled')],
|
||||||
socket1
|
socket1
|
||||||
),
|
),
|
||||||
// Closer the listener (will take a couple of seconds to time out)
|
// Closer the listener (will take a couple of seconds to time out)
|
||||||
|
@@ -6,6 +6,9 @@ class MockPeerStore extends EventEmitter {
|
|||||||
constructor (peers) {
|
constructor (peers) {
|
||||||
super()
|
super()
|
||||||
this.peers = peers
|
this.peers = peers
|
||||||
|
this.protoBook = {
|
||||||
|
get: () => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get (peerId) {
|
get (peerId) {
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
const { EventEmitter } = require('events')
|
||||||
|
|
||||||
const tests = require('../../src/topology/tests/multicodec-topology')
|
const tests = require('../../src/topology/tests/multicodec-topology')
|
||||||
const MulticodecTopology = require('../../src/topology/multicodec-topology')
|
const MulticodecTopology = require('../../src/topology/multicodec-topology')
|
||||||
const MockPeerStore = require('./mock-peer-store')
|
const MockPeerStore = require('./mock-peer-store')
|
||||||
@@ -23,9 +25,11 @@ describe('multicodec topology compliance tests', () => {
|
|||||||
if (!registrar) {
|
if (!registrar) {
|
||||||
const peers = new Map()
|
const peers = new Map()
|
||||||
const peerStore = new MockPeerStore(peers)
|
const peerStore = new MockPeerStore(peers)
|
||||||
|
const connectionManager = new EventEmitter()
|
||||||
|
|
||||||
registrar = {
|
registrar = {
|
||||||
peerStore,
|
peerStore,
|
||||||
|
connectionManager,
|
||||||
getConnection: () => { }
|
getConnection: () => { }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user