Compare commits

..

8 Commits

Author SHA1 Message Date
2803e64969 chore: release version v0.1.7 2019-12-15 16:57:40 +01:00
e979bc9d4e chore: update contributors 2019-12-15 16:57:39 +01:00
bdbd58e897 feat: export connection status' (#15)
* feat: export the connection status'

* docs(fix): correct status listing
2019-12-15 16:54:43 +01:00
79bfcacb61 chore: add lead maintainer property (#14) 2019-12-12 13:05:12 +01:00
148f3c9f43 docs: corrected reference to delegated content routing (#13) 2019-12-04 17:19:16 +01:00
6641a5b0b4 chore: release version v0.1.6 2019-12-02 16:09:12 +01:00
34372e07ce chore: update contributors 2019-12-02 16:09:12 +01:00
d5dd256a21 fix: multicodec topology disconnect with peer param (#12) 2019-12-02 16:06:26 +01:00
9 changed files with 56 additions and 16 deletions

View File

@ -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)

View File

@ -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",

View File

@ -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`:

View File

@ -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
View File

@ -0,0 +1,7 @@
'use strict'
module.exports = {
OPEN: 'open',
CLOSING: 'closing',
CLOSED: 'closed'
}

View File

@ -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 () => {

View File

@ -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

View File

@ -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

View File

@ -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)
}) })
}) })
} }