Compare commits

...

11 Commits

Author SHA1 Message Date
32ee3e18e2 chore: release version v0.2.1 2019-12-27 21:46:43 -07:00
557f6cab66 chore: update contributors 2019-12-27 21:46:43 -07:00
d98bb23fea feat: add crypto transmission error (#17)
* feat: add crypto transmission error

* docs: add error to crypto readme
2019-12-27 19:51:10 -08:00
6cf3723019 chore: release version v0.2.0 2019-12-20 08:14:50 -08:00
01e240420d chore: update contributors 2019-12-20 08:14:49 -08:00
ff0313721c fix: transport should not handle connection if upgradeInbound throws (#16)
* fix: transport should not handle connection if upgradeInbound throws

* chore: address review
2019-12-20 08:01:37 -08:00
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
10 changed files with 98 additions and 14 deletions

View File

@ -1,3 +1,33 @@
<a name="0.2.1"></a>
## [0.2.1](https://github.com/libp2p/js-interfaces/compare/v0.2.0...v0.2.1) (2019-12-28)
### Features
* add crypto transmission error ([#17](https://github.com/libp2p/js-interfaces/issues/17)) ([d98bb23](https://github.com/libp2p/js-interfaces/commit/d98bb23))
<a name="0.2.0"></a>
# [0.2.0](https://github.com/libp2p/js-interfaces/compare/v0.1.7...v0.2.0) (2019-12-20)
### Bug Fixes
* transport should not handle connection if upgradeInbound throws ([#16](https://github.com/libp2p/js-interfaces/issues/16)) ([ff03137](https://github.com/libp2p/js-interfaces/commit/ff03137))
<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)

View File

@ -1,7 +1,8 @@
{
"name": "libp2p-interfaces",
"version": "0.1.6",
"version": "0.2.1",
"description": "Interfaces for JS Libp2p",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
"files": [
"src",
@ -45,6 +46,7 @@
"libp2p-tcp": "^0.14.1",
"multiaddr": "^7.1.0",
"p-limit": "^2.2.1",
"p-wait-for": "^3.1.0",
"peer-id": "^0.13.3",
"sinon": "^7.5.0",
"streaming-iterables": "^4.1.0"

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).
- `multiplexer` is a `string` with the connection multiplexing codec (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
@ -220,7 +221,17 @@ This getter returns an `Object` with the metadata of the connection, as follows:
- `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`:

View File

@ -7,6 +7,7 @@ const withIs = require('class-is')
const assert = require('assert')
const errCode = require('err-code')
const Status = require('./status')
/**
* An implementation of the js-libp2p connection.
@ -75,7 +76,7 @@ class Connection {
*/
this._stat = {
...stat,
status: 'open'
status: Status.OPEN
}
/**
@ -126,11 +127,11 @@ class Connection {
* @return {Promise<object>} with muxed+multistream-selected stream and selected protocol
*/
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')
}
if (this.stat.status === 'closed') {
if (this.stat.status === Status.CLOSED) {
throw errCode(new Error('the connection is closed'), 'ERR_CONNECTION_CLOSED')
}
@ -175,7 +176,7 @@ class Connection {
* @return {Promise}
*/
async close () {
if (this.stat.status === 'closed') {
if (this.stat.status === Status.CLOSED) {
return
}
@ -183,13 +184,13 @@ class Connection {
return this._closing
}
this.stat.status = 'closing'
this.stat.status = Status.CLOSING
// Close raw connection
this._closing = await this._close()
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
chai.use(require('dirty-chai'))
const sinon = require('sinon')
const Status = require('../status')
module.exports = (test) => {
describe('connection', () => {
@ -28,7 +29,7 @@ module.exports = (test) => {
expect(connection.remotePeer).to.exist()
expect(connection.localAddr).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.upgraded).to.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', () => {
const stat = connection.stat
expect(stat.status).to.equal('open')
expect(stat.status).to.equal(Status.OPEN)
expect(stat.direction).to.exist()
expect(stat.timeline.open).to.exist()
expect(stat.timeline.upgraded).to.exist()
@ -103,7 +104,7 @@ module.exports = (test) => {
await connection.close()
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 () => {
@ -116,7 +117,7 @@ module.exports = (test) => {
await connection.close()
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 () => {

View File

@ -12,7 +12,7 @@ Publishing a test suite as a module lets multiple modules all ensure compatibili
# Modules that implement the interface
- [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

View File

@ -85,6 +85,7 @@ Common crypto errors come with the interface, and can be imported directly. All
```js
const {
InvalidCryptoExchangeError,
InvalidCryptoTransmissionError,
UnexpectedPeerError
} = require('libp2p-interfaces/src/crypto/errors')
@ -95,4 +96,5 @@ console.log(error.code === UnexpectedPeerError.code) // true
### Error Types
- `InvalidCryptoExchangeError` - Should be thrown when a peer provides data that is insufficient to finish the crypto exchange.
- `InvalidCryptoTransmissionError` - Should be thrown when an error occurs during encryption/decryption.
- `UnexpectedPeerError` - Should be thrown when the expected peer id does not match the peer id determined via the crypto exchange.

View File

@ -22,7 +22,19 @@ class InvalidCryptoExchangeError extends Error {
}
}
class InvalidCryptoTransmissionError extends Error {
constructor (message = 'Invalid crypto transmission') {
super(message)
this.code = InvalidCryptoTransmissionError.code
}
static get code () {
return 'ERR_INVALID_CRYPTO_TRANSMISSION'
}
}
module.exports = {
UnexpectedPeerError,
InvalidCryptoExchangeError
InvalidCryptoExchangeError,
InvalidCryptoTransmissionError
}

View File

@ -8,6 +8,7 @@ const expect = chai.expect
chai.use(dirtyChai)
const sinon = require('sinon')
const pWaitFor = require('p-wait-for')
const pipe = require('it-pipe')
const { isValidTick } = require('./utils')
@ -92,6 +93,23 @@ module.exports = (common) => {
expect(upgradeSpy.callCount).to.equal(2)
})
it('should not handle connection if upgradeInbound throws', async () => {
sinon.stub(upgrader, 'upgradeInbound').throws()
const listener = transport.createListener(() => {
throw new Error('should not handle the connection if upgradeInbound throws')
})
// Listen
await listener.listen(addrs[0])
// Create a connection to the listener
const socket = await transport.dial(addrs[0])
await pWaitFor(() => typeof socket.timeline.close === 'number')
await listener.close()
})
describe('events', () => {
it('connection', (done) => {
const upgradeSpy = sinon.spy(upgrader, 'upgradeInbound')