mirror of
https://github.com/fluencelabs/js-libp2p-interfaces
synced 2025-07-07 17:31:35 +00:00
Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
2803e64969 | |||
e979bc9d4e | |||
bdbd58e897 | |||
79bfcacb61 | |||
148f3c9f43 | |||
6641a5b0b4 | |||
34372e07ce | |||
d5dd256a21 |
20
CHANGELOG.md
20
CHANGELOG.md
@ -1,3 +1,23 @@
|
|||||||
|
<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>
|
<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)
|
## [0.1.5](https://github.com/libp2p/js-interfaces/compare/v0.1.4...v0.1.5) (2019-11-15)
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p-interfaces",
|
"name": "libp2p-interfaces",
|
||||||
"version": "0.1.5",
|
"version": "0.1.7",
|
||||||
"description": "Interfaces for JS Libp2p",
|
"description": "Interfaces for JS Libp2p",
|
||||||
|
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"files": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
|
@ -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).
|
- `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).
|
- `multiplexer` is a `string` with the connection multiplexing codec (optional).
|
||||||
- `encryption` is a `string` with the connection encryption method identifier (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
|
#### Create a new stream
|
||||||
|
|
||||||
@ -220,7 +221,17 @@ This getter returns an `Object` with the metadata of the connection, as follows:
|
|||||||
|
|
||||||
- `status`:
|
- `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`:
|
- `timeline`:
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ const withIs = require('class-is')
|
|||||||
|
|
||||||
const assert = require('assert')
|
const assert = require('assert')
|
||||||
const errCode = require('err-code')
|
const errCode = require('err-code')
|
||||||
|
const Status = require('./status')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An implementation of the js-libp2p connection.
|
* An implementation of the js-libp2p connection.
|
||||||
@ -75,7 +76,7 @@ class Connection {
|
|||||||
*/
|
*/
|
||||||
this._stat = {
|
this._stat = {
|
||||||
...stat,
|
...stat,
|
||||||
status: 'open'
|
status: Status.OPEN
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -126,11 +127,11 @@ class Connection {
|
|||||||
* @return {Promise<object>} with muxed+multistream-selected stream and selected protocol
|
* @return {Promise<object>} with muxed+multistream-selected stream and selected protocol
|
||||||
*/
|
*/
|
||||||
async newStream (protocols) {
|
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')
|
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')
|
throw errCode(new Error('the connection is closed'), 'ERR_CONNECTION_CLOSED')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,7 +176,7 @@ class Connection {
|
|||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
async close () {
|
async close () {
|
||||||
if (this.stat.status === 'closed') {
|
if (this.stat.status === Status.CLOSED) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,13 +184,13 @@ class Connection {
|
|||||||
return this._closing
|
return this._closing
|
||||||
}
|
}
|
||||||
|
|
||||||
this.stat.status = 'closing'
|
this.stat.status = Status.CLOSING
|
||||||
|
|
||||||
// Close raw connection
|
// Close raw connection
|
||||||
this._closing = await this._close()
|
this._closing = await this._close()
|
||||||
|
|
||||||
this._stat.timeline.close = Date.now()
|
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
|
const expect = chai.expect
|
||||||
chai.use(require('dirty-chai'))
|
chai.use(require('dirty-chai'))
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
|
const Status = require('../status')
|
||||||
|
|
||||||
module.exports = (test) => {
|
module.exports = (test) => {
|
||||||
describe('connection', () => {
|
describe('connection', () => {
|
||||||
@ -28,7 +29,7 @@ module.exports = (test) => {
|
|||||||
expect(connection.remotePeer).to.exist()
|
expect(connection.remotePeer).to.exist()
|
||||||
expect(connection.localAddr).to.exist()
|
expect(connection.localAddr).to.exist()
|
||||||
expect(connection.remoteAddr).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.open).to.exist()
|
||||||
expect(connection.stat.timeline.upgraded).to.exist()
|
expect(connection.stat.timeline.upgraded).to.exist()
|
||||||
expect(connection.stat.timeline.close).to.not.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', () => {
|
it('should get the metadata of an open connection', () => {
|
||||||
const stat = connection.stat
|
const stat = connection.stat
|
||||||
|
|
||||||
expect(stat.status).to.equal('open')
|
expect(stat.status).to.equal(Status.OPEN)
|
||||||
expect(stat.direction).to.exist()
|
expect(stat.direction).to.exist()
|
||||||
expect(stat.timeline.open).to.exist()
|
expect(stat.timeline.open).to.exist()
|
||||||
expect(stat.timeline.upgraded).to.exist()
|
expect(stat.timeline.upgraded).to.exist()
|
||||||
@ -103,7 +104,7 @@ module.exports = (test) => {
|
|||||||
await connection.close()
|
await connection.close()
|
||||||
|
|
||||||
expect(connection.stat.timeline.close).to.exist()
|
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 () => {
|
it('should be able to close the connection after opening a stream', async () => {
|
||||||
@ -116,7 +117,7 @@ module.exports = (test) => {
|
|||||||
await connection.close()
|
await connection.close()
|
||||||
|
|
||||||
expect(connection.stat.timeline.close).to.exist()
|
expect(connection.stat.timeline.close).to.exist()
|
||||||
expect(connection.stat.status).to.equal('closed')
|
expect(connection.stat.status).to.equal(Status.CLOSED)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support a proxy on the timeline', async () => {
|
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
|
# Modules that implement the interface
|
||||||
|
|
||||||
- [JavaScript libp2p-kad-dht](https://github.com/libp2p/js-libp2p-kad-dht)
|
- [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
|
# Badge
|
||||||
|
|
||||||
|
@ -77,9 +77,7 @@ class MulticodecTopology extends Topology {
|
|||||||
|
|
||||||
// Not supporting the protocol anymore?
|
// Not supporting the protocol anymore?
|
||||||
if (existingPeer && hasProtocol.length === 0) {
|
if (existingPeer && hasProtocol.length === 0) {
|
||||||
this._onDisconnect({
|
this._onDisconnect(peerInfo)
|
||||||
peerInfo
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New to protocol support
|
// New to protocol support
|
||||||
|
@ -86,6 +86,7 @@ module.exports = (test) => {
|
|||||||
|
|
||||||
expect(topology.peers.size).to.eql(1)
|
expect(topology.peers.size).to.eql(1)
|
||||||
expect(topology._onDisconnect.callCount).to.equal(1)
|
expect(topology._onDisconnect.callCount).to.equal(1)
|
||||||
|
expect(topology._onDisconnect.calledWith(peer2)).to.equal(true)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user