Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit ed5727a

Browse files
dignifiedquiredaviddias
authored andcommitted
feat(connection): migrate to pull-streams
1 parent d4d6d55 commit ed5727a

File tree

7 files changed

+44
-48
lines changed

7 files changed

+44
-48
lines changed

README.md

-8
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ A valid (read: that follows this abstraction) connection, must implement the fol
6464
- `conn.getObservedAddrs(callback)`
6565
- `conn.getPeerInfo(callback)`
6666
- `conn.setPeerInfo(peerInfo)`
67-
- `conn.destroy`
68-
- `conn.write`
69-
- `conn.read`
70-
- `conn.pipe`
71-
- `conn.end`
72-
- `conn.pause`
73-
- `conn.resume`
74-
- `conn.destroy`
7567
- `...`
7668

7769
### Get the Observed Addresses of the peer in the other end

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "interface-connection",
33
"version": "0.1.8",
44
"description": "A test suite and interface you can use to implement a connection interface.",
5-
"main": "lib/index.js",
5+
"main": "src/index.js",
66
"jsnext:main": "src/index.js",
77
"scripts": {
88
"lint": "aegir-lint",
@@ -30,8 +30,8 @@
3030
},
3131
"homepage": "https://github.com/diasdavid/interface-connection",
3232
"dependencies": {
33-
"duplexify": "diasdavid/duplexify#a22bcdf",
3433
"timed-tape": "^0.1.1"
34+
"pull-defer": "^0.2.2",
3535
},
3636
"devDependencies": {
3737
"aegir": "^6.0.1"
@@ -40,4 +40,4 @@
4040
"David Dias <[email protected]>",
4141
"Pau Ramon Revilla <[email protected]>"
4242
]
43-
}
43+
}

src/connection.js

+36-32
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,60 @@
11
'use strict'
22

3-
const util = require('util')
4-
const Duplexify = require('duplexify')
3+
const defer = require('pull-defer/duplex')
54

6-
module.exports = Connection
5+
module.exports = class Connection {
6+
constructor (conn, info) {
7+
this.peerInfo = null
8+
this.conn = defer()
79

8-
util.inherits(Connection, Duplexify)
9-
10-
function Connection (conn) {
11-
if (!(this instanceof Connection)) {
12-
return new Connection(conn)
10+
if (conn) {
11+
this.setInnerConn(conn, info)
12+
} else if (info) {
13+
this.info = info
14+
}
1315
}
1416

15-
Duplexify.call(this)
17+
get source () {
18+
return this.conn.source
19+
}
1620

17-
let peerInfo
21+
get sink () {
22+
return this.conn.sink
23+
}
1824

19-
this.getPeerInfo = (callback) => {
20-
if (conn && conn.getPeerInfo) {
21-
return conn.getPeerInfo(callback)
25+
getPeerInfo (callback) {
26+
if (this.info && this.info.getPeerInfo) {
27+
return this.info.getPeerInfo(callback)
2228
}
2329

24-
if (!peerInfo) {
30+
if (!this.peerInfo) {
2531
return callback(new Error('Peer Info not set yet'))
2632
}
2733

28-
callback(null, peerInfo)
34+
callback(null, this.peerInfo)
2935
}
3036

31-
this.setPeerInfo = (_peerInfo) => {
32-
if (conn && conn.setPeerInfo) {
33-
return conn.setPeerInfo(_peerInfo)
37+
setPeerInfo (peerInfo) {
38+
if (this.info && this.info.setPeerInfo) {
39+
return this.info.setPeerInfo(peerInfo)
3440
}
35-
peerInfo = _peerInfo
41+
42+
this.peerInfo = peerInfo
3643
}
3744

38-
this.getObservedAddrs = (callback) => {
39-
if (conn && conn.getObservedAddrs) {
40-
return conn.getObservedAddrs(callback)
45+
getObservedAddrs (callback) {
46+
if (this.info && this.info.getObservedAddrs) {
47+
return this.info.getObservedAddrs(callback)
4148
}
4249
callback(null, [])
4350
}
4451

45-
this.setInnerConn = (_conn) => {
46-
conn = _conn
47-
this.setReadable(conn)
48-
this.setWritable(conn)
49-
}
50-
51-
// .destroy is implemented by Duplexify
52-
53-
if (conn) {
54-
this.setInnerConn(conn)
52+
setInnerConn (conn, info) {
53+
this.conn.resolve(conn)
54+
if (info) {
55+
this.info = info
56+
} else {
57+
this.info = conn
58+
}
5559
}
5660
}

src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
'use strict'
22

3-
exports.connection = require('./connection.js')
4-
exports.Connection = require('./connection.js')
3+
exports.Connection = require('./connection')

tests/base-test.js test/base-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
module.exports.all = function (test, common) {
24
test('a test', function (t) {
35
common.setup(test, function (err, conn) {

tests/index.js test/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
var timed = require('timed-tape')
24

35
module.exports = function (test, common) {

test/test.spec.js

-3
This file was deleted.

0 commit comments

Comments
 (0)