feat(connection): migrate to pull-streams

This commit is contained in:
dignifiedquire 2016-08-09 12:16:16 +02:00 committed by David Dias
parent d4d6d55f37
commit ed5727a0fe
7 changed files with 44 additions and 48 deletions

View File

@ -64,14 +64,6 @@ A valid (read: that follows this abstraction) connection, must implement the fol
- `conn.getObservedAddrs(callback)` - `conn.getObservedAddrs(callback)`
- `conn.getPeerInfo(callback)` - `conn.getPeerInfo(callback)`
- `conn.setPeerInfo(peerInfo)` - `conn.setPeerInfo(peerInfo)`
- `conn.destroy`
- `conn.write`
- `conn.read`
- `conn.pipe`
- `conn.end`
- `conn.pause`
- `conn.resume`
- `conn.destroy`
- `...` - `...`
### Get the Observed Addresses of the peer in the other end ### Get the Observed Addresses of the peer in the other end

View File

@ -2,7 +2,7 @@
"name": "interface-connection", "name": "interface-connection",
"version": "0.1.8", "version": "0.1.8",
"description": "A test suite and interface you can use to implement a connection interface.", "description": "A test suite and interface you can use to implement a connection interface.",
"main": "lib/index.js", "main": "src/index.js",
"jsnext:main": "src/index.js", "jsnext:main": "src/index.js",
"scripts": { "scripts": {
"lint": "aegir-lint", "lint": "aegir-lint",
@ -30,8 +30,8 @@
}, },
"homepage": "https://github.com/diasdavid/interface-connection", "homepage": "https://github.com/diasdavid/interface-connection",
"dependencies": { "dependencies": {
"duplexify": "diasdavid/duplexify#a22bcdf",
"timed-tape": "^0.1.1" "timed-tape": "^0.1.1"
"pull-defer": "^0.2.2",
}, },
"devDependencies": { "devDependencies": {
"aegir": "^6.0.1" "aegir": "^6.0.1"

View File

@ -1,56 +1,60 @@
'use strict' 'use strict'
const util = require('util') const defer = require('pull-defer/duplex')
const Duplexify = require('duplexify')
module.exports = Connection module.exports = class Connection {
constructor (conn, info) {
this.peerInfo = null
this.conn = defer()
util.inherits(Connection, Duplexify) if (conn) {
this.setInnerConn(conn, info)
function Connection (conn) { } else if (info) {
if (!(this instanceof Connection)) { this.info = info
return new Connection(conn) }
} }
Duplexify.call(this) get source () {
return this.conn.source
let peerInfo
this.getPeerInfo = (callback) => {
if (conn && conn.getPeerInfo) {
return conn.getPeerInfo(callback)
} }
if (!peerInfo) { get sink () {
return this.conn.sink
}
getPeerInfo (callback) {
if (this.info && this.info.getPeerInfo) {
return this.info.getPeerInfo(callback)
}
if (!this.peerInfo) {
return callback(new Error('Peer Info not set yet')) return callback(new Error('Peer Info not set yet'))
} }
callback(null, peerInfo) callback(null, this.peerInfo)
} }
this.setPeerInfo = (_peerInfo) => { setPeerInfo (peerInfo) {
if (conn && conn.setPeerInfo) { if (this.info && this.info.setPeerInfo) {
return conn.setPeerInfo(_peerInfo) return this.info.setPeerInfo(peerInfo)
}
peerInfo = _peerInfo
} }
this.getObservedAddrs = (callback) => { this.peerInfo = peerInfo
if (conn && conn.getObservedAddrs) { }
return conn.getObservedAddrs(callback)
getObservedAddrs (callback) {
if (this.info && this.info.getObservedAddrs) {
return this.info.getObservedAddrs(callback)
} }
callback(null, []) callback(null, [])
} }
this.setInnerConn = (_conn) => { setInnerConn (conn, info) {
conn = _conn this.conn.resolve(conn)
this.setReadable(conn) if (info) {
this.setWritable(conn) this.info = info
} } else {
this.info = conn
// .destroy is implemented by Duplexify }
if (conn) {
this.setInnerConn(conn)
} }
} }

View File

@ -1,4 +1,3 @@
'use strict' 'use strict'
exports.connection = require('./connection.js') exports.Connection = require('./connection')
exports.Connection = require('./connection.js')

View File

@ -1,3 +1,5 @@
'use strict'
module.exports.all = function (test, common) { module.exports.all = function (test, common) {
test('a test', function (t) { test('a test', function (t) {
common.setup(test, function (err, conn) { common.setup(test, function (err, conn) {

View File

@ -1,3 +1,5 @@
'use strict'
var timed = require('timed-tape') var timed = require('timed-tape')
module.exports = function (test, common) { module.exports = function (test, common) {

View File

@ -1,3 +0,0 @@
'use strict'
// so that aegir does not burp