mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-07-06 18:31:35 +00:00
Compare commits
20 Commits
Author | SHA1 | Date | |
---|---|---|---|
de64a49007 | |||
3cfd9714bd | |||
78e015cef3 | |||
bd5a8b9337 | |||
098390cd31 | |||
953e289e1a | |||
ba822856ef | |||
c77d8de2e7 | |||
6203109751 | |||
25eeedd20e | |||
5c88d77aaa | |||
f6871afd76 | |||
734c491bb0 | |||
c43a52d176 | |||
af1ba5a409 | |||
c5b724ab93 | |||
6fb45b775b | |||
85c68b5df4 | |||
27ac0a5670 | |||
2de533e15b |
1
.gitignore
vendored
1
.gitignore
vendored
@ -13,3 +13,4 @@ docs
|
||||
node_modules
|
||||
# Lock files
|
||||
package-lock.json
|
||||
yarn.lock
|
50
CHANGELOG.md
50
CHANGELOG.md
@ -1,3 +1,53 @@
|
||||
<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)
|
||||
|
||||
|
21
package.json
21
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p-interfaces",
|
||||
"version": "0.2.2",
|
||||
"version": "0.2.7",
|
||||
"description": "Interfaces for JS Libp2p",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
@ -34,8 +34,8 @@
|
||||
"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.5.0",
|
||||
"chai": "^4.2.0",
|
||||
"chai-checkmark": "^1.0.1",
|
||||
"class-is": "^1.1.0",
|
||||
@ -43,20 +43,20 @@
|
||||
"dirty-chai": "^2.0.1",
|
||||
"err-code": "^2.0.0",
|
||||
"it-goodbye": "^2.0.1",
|
||||
"it-pair": "^1.0.0",
|
||||
"it-pipe": "^1.0.1",
|
||||
"libp2p-tcp": "^0.14.1",
|
||||
"multiaddr": "^7.1.0",
|
||||
"p-limit": "^2.2.1",
|
||||
"p-limit": "^2.2.2",
|
||||
"p-wait-for": "^3.1.0",
|
||||
"peer-id": "^0.13.3",
|
||||
"sinon": "^7.5.0",
|
||||
"peer-info": "^0.17.0",
|
||||
"sinon": "^9.0.0",
|
||||
"streaming-iterables": "^4.1.0"
|
||||
},
|
||||
"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": "^21.0.2",
|
||||
"it-handshake": "^1.0.1"
|
||||
},
|
||||
"contributors": [
|
||||
"Alan Shaw <alan.shaw@protocol.ai>",
|
||||
@ -79,6 +79,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>",
|
||||
|
@ -2,13 +2,56 @@
|
||||
|
||||
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.
|
||||
* 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.
|
||||
*/
|
||||
constructor ({ localAddr, remoteAddr, localPeer, remotePeer, newStream, close, getStreams, stat }) {
|
||||
localAddr && assert(multiaddr.isMultiaddr(localAddr), 'localAddr 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.
|
||||
@ -138,7 +171,7 @@ class Connection {
|
||||
|
||||
const { stream, protocol } = await this._newStream(protocols)
|
||||
|
||||
this.addStream(stream, protocol)
|
||||
this.addStream(stream, { protocol })
|
||||
|
||||
return {
|
||||
stream,
|
||||
|
@ -120,6 +120,19 @@ module.exports = (test) => {
|
||||
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 () => {
|
||||
sinon.spy(proxyHandler, 'set')
|
||||
expect(connection.stat.timeline.close).to.not.exist()
|
||||
|
@ -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')
|
||||
|
@ -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')
|
||||
|
@ -1,6 +1,5 @@
|
||||
'use strict'
|
||||
|
||||
const assert = require('assert')
|
||||
const withIs = require('class-is')
|
||||
|
||||
const Topology = require('./index')
|
||||
@ -24,12 +23,21 @@ class MulticodecTopology extends Topology {
|
||||
}) {
|
||||
super({ min, max, handlers })
|
||||
|
||||
assert(multicodecs, 'one or more multicodec should be provided')
|
||||
assert(handlers, 'the handlers should be provided')
|
||||
assert(handlers.onConnect && typeof handlers.onConnect === 'function',
|
||||
'the \'onConnect\' handler must be provided')
|
||||
assert(handlers.onDisconnect && typeof handlers.onDisconnect === 'function',
|
||||
'the \'onDisconnect\' handler must be provided')
|
||||
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
|
||||
|
@ -2,6 +2,7 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const { Buffer } = require('buffer')
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
@ -70,7 +71,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([
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user