mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-07-09 13:11:54 +00:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
098390cd31 | |||
953e289e1a | |||
ba822856ef | |||
c77d8de2e7 |
10
CHANGELOG.md
10
CHANGELOG.md
@ -1,3 +1,13 @@
|
|||||||
|
<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>
|
<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)
|
## [0.2.5](https://github.com/libp2p/js-interfaces/compare/v0.2.4...v0.2.5) (2020-02-04)
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-interfaces",
|
"name": "libp2p-interfaces",
|
||||||
"version": "0.2.5",
|
"version": "0.2.6",
|
||||||
"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",
|
||||||
@ -54,7 +54,7 @@
|
|||||||
"streaming-iterables": "^4.1.0"
|
"streaming-iterables": "^4.1.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"aegir": "^20.5.0",
|
"aegir": "^21.0.2",
|
||||||
"it-handshake": "^1.0.1"
|
"it-handshake": "^1.0.1"
|
||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
|
@ -2,13 +2,56 @@
|
|||||||
|
|
||||||
const PeerId = require('peer-id')
|
const PeerId = require('peer-id')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
|
|
||||||
const withIs = require('class-is')
|
const withIs = require('class-is')
|
||||||
|
|
||||||
const assert = require('assert')
|
|
||||||
const errCode = require('err-code')
|
const errCode = require('err-code')
|
||||||
const Status = require('./status')
|
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.
|
* An implementation of the js-libp2p connection.
|
||||||
* Any libp2p transport should use an upgrader to return this connection.
|
* Any libp2p transport should use an upgrader to return this connection.
|
||||||
@ -33,17 +76,7 @@ class Connection {
|
|||||||
* @param {string} [properties.stat.encryption] connection encryption method identifier.
|
* @param {string} [properties.stat.encryption] connection encryption method identifier.
|
||||||
*/
|
*/
|
||||||
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
|
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
|
||||||
localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr must be an instance of multiaddr')
|
validateArgs(localAddr, localPeer, remotePeer, newStream, close, getStreams, stat)
|
||||||
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')
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connection identifier.
|
* Connection identifier.
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const assert = require('assert')
|
|
||||||
const withIs = require('class-is')
|
const withIs = require('class-is')
|
||||||
|
|
||||||
const Topology = require('./index')
|
const Topology = require('./index')
|
||||||
@ -24,12 +23,21 @@ class MulticodecTopology extends Topology {
|
|||||||
}) {
|
}) {
|
||||||
super({ min, max, handlers })
|
super({ min, max, handlers })
|
||||||
|
|
||||||
assert(multicodecs, 'one or more multicodec should be provided')
|
if (!multicodecs) {
|
||||||
assert(handlers, 'the handlers should be provided')
|
throw new Error('one or more multicodec should be provided')
|
||||||
assert(handlers.onConnect && typeof handlers.onConnect === 'function',
|
}
|
||||||
'the \'onConnect\' handler must be provided')
|
|
||||||
assert(handlers.onDisconnect && typeof handlers.onDisconnect === 'function',
|
if (!handlers) {
|
||||||
'the \'onDisconnect\' handler must be provided')
|
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.multicodecs = Array.isArray(multicodecs) ? multicodecs : [multicodecs]
|
||||||
this._registrar = undefined
|
this._registrar = undefined
|
||||||
|
Reference in New Issue
Block a user