Skip to content

Commit bdbd58e

Browse files
authored
feat: export connection status' (libp2p#15)
* feat: export the connection status' * docs(fix): correct status listing
1 parent 79bfcac commit bdbd58e

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

src/connection/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Creates a new Connection instance.
121121
- `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).
122122
- `multiplexer` is a `string` with the connection multiplexing codec (optional).
123123
- `encryption` is a `string` with the connection encryption method identifier (optional).
124+
- `status` is a `string` indicating the overall status of the connection. It is one of [`'open'`, `'closing'`, `'closed'`]
124125

125126
#### Create a new stream
126127

@@ -220,7 +221,17 @@ This getter returns an `Object` with the metadata of the connection, as follows:
220221

221222
- `status`:
222223

223-
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`.
224+
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:
225+
226+
```js
227+
const {
228+
OPEN, CLOSING, CLOSED
229+
} = require('libp2p-interfaces/src/connection/status')
230+
231+
if (connection.stat.status === OPEN) {
232+
// ...
233+
}
234+
```
224235

225236
- `timeline`:
226237

src/connection/connection.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const withIs = require('class-is')
77

88
const assert = require('assert')
99
const errCode = require('err-code')
10+
const Status = require('./status')
1011

1112
/**
1213
* An implementation of the js-libp2p connection.
@@ -75,7 +76,7 @@ class Connection {
7576
*/
7677
this._stat = {
7778
...stat,
78-
status: 'open'
79+
status: Status.OPEN
7980
}
8081

8182
/**
@@ -126,11 +127,11 @@ class Connection {
126127
* @return {Promise<object>} with muxed+multistream-selected stream and selected protocol
127128
*/
128129
async newStream (protocols) {
129-
if (this.stat.status === 'closing') {
130+
if (this.stat.status === Status.CLOSING) {
130131
throw errCode(new Error('the connection is being closed'), 'ERR_CONNECTION_BEING_CLOSED')
131132
}
132133

133-
if (this.stat.status === 'closed') {
134+
if (this.stat.status === Status.CLOSED) {
134135
throw errCode(new Error('the connection is closed'), 'ERR_CONNECTION_CLOSED')
135136
}
136137

@@ -175,21 +176,21 @@ class Connection {
175176
* @return {Promise}
176177
*/
177178
async close () {
178-
if (this.stat.status === 'closed') {
179+
if (this.stat.status === Status.CLOSED) {
179180
return
180181
}
181182

182183
if (this._closing) {
183184
return this._closing
184185
}
185186

186-
this.stat.status = 'closing'
187+
this.stat.status = Status.CLOSING
187188

188189
// Close raw connection
189190
this._closing = await this._close()
190191

191192
this._stat.timeline.close = Date.now()
192-
this.stat.status = 'closed'
193+
this.stat.status = Status.CLOSED
193194
}
194195
}
195196

src/connection/status.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = {
4+
OPEN: 'open',
5+
CLOSING: 'closing',
6+
CLOSED: 'closed'
7+
}

src/connection/tests/connection.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const chai = require('chai')
66
const expect = chai.expect
77
chai.use(require('dirty-chai'))
88
const sinon = require('sinon')
9+
const Status = require('../status')
910

1011
module.exports = (test) => {
1112
describe('connection', () => {
@@ -28,7 +29,7 @@ module.exports = (test) => {
2829
expect(connection.remotePeer).to.exist()
2930
expect(connection.localAddr).to.exist()
3031
expect(connection.remoteAddr).to.exist()
31-
expect(connection.stat.status).to.equal('open')
32+
expect(connection.stat.status).to.equal(Status.OPEN)
3233
expect(connection.stat.timeline.open).to.exist()
3334
expect(connection.stat.timeline.upgraded).to.exist()
3435
expect(connection.stat.timeline.close).to.not.exist()
@@ -40,7 +41,7 @@ module.exports = (test) => {
4041
it('should get the metadata of an open connection', () => {
4142
const stat = connection.stat
4243

43-
expect(stat.status).to.equal('open')
44+
expect(stat.status).to.equal(Status.OPEN)
4445
expect(stat.direction).to.exist()
4546
expect(stat.timeline.open).to.exist()
4647
expect(stat.timeline.upgraded).to.exist()
@@ -103,7 +104,7 @@ module.exports = (test) => {
103104
await connection.close()
104105

105106
expect(connection.stat.timeline.close).to.exist()
106-
expect(connection.stat.status).to.equal('closed')
107+
expect(connection.stat.status).to.equal(Status.CLOSED)
107108
})
108109

109110
it('should be able to close the connection after opening a stream', async () => {
@@ -116,7 +117,7 @@ module.exports = (test) => {
116117
await connection.close()
117118

118119
expect(connection.stat.timeline.close).to.exist()
119-
expect(connection.stat.status).to.equal('closed')
120+
expect(connection.stat.status).to.equal(Status.CLOSED)
120121
})
121122

122123
it('should support a proxy on the timeline', async () => {

0 commit comments

Comments
 (0)