Merge pull request #10 from libp2p/pull

Migrate to pull-streams
This commit is contained in:
David Dias 2016-09-05 17:44:40 -04:00 committed by GitHub
commit fccbe56047
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.getPeerInfo(callback)`
- `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

View File

@ -2,7 +2,7 @@
"name": "interface-connection",
"version": "0.1.8",
"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",
"scripts": {
"lint": "aegir-lint",
@ -30,8 +30,8 @@
},
"homepage": "https://github.com/diasdavid/interface-connection",
"dependencies": {
"duplexify": "diasdavid/duplexify#a22bcdf",
"timed-tape": "^0.1.1"
"pull-defer": "^0.2.2",
},
"devDependencies": {
"aegir": "^6.0.1"
@ -40,4 +40,4 @@
"David Dias <daviddias.p@gmail.com>",
"Pau Ramon Revilla <masylum@gmail.com>"
]
}
}

View File

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

View File

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

View File

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

View File

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

View File

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