diff --git a/README.md b/README.md index 868ee83..07f962f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 62a2973..a59d4d3 100644 --- a/package.json +++ b/package.json @@ -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,14 +30,14 @@ }, "homepage": "https://github.com/diasdavid/interface-connection", "dependencies": { - "duplexify": "diasdavid/duplexify#a22bcdf", + "pull-defer": "^0.2.2", "timed-tape": "^0.1.0" }, "devDependencies": { - "aegir": "^4.0.0" + "aegir": "^6.0.1" }, "contributors": [ "David Dias ", "Pau Ramon Revilla " ] -} \ No newline at end of file +} diff --git a/src/connection.js b/src/connection.js index 63a5fe0..98f5955 100644 --- a/src/connection.js +++ b/src/connection.js @@ -1,22 +1,28 @@ 'use strict' +const defer = require('pull-defer/duplex') + module.exports = class Connection { - constructor (conn) { + constructor (conn, info) { this.peerInfo = null - this.conn = conn + this.conn = defer() + + if (conn) { + this.setInnerConn(conn, info) + } } get source () { - return this.conn && this.conn.source + return this.conn.source } get sink () { - return this.conn && this.conn.sink + return this.conn.sink } getPeerInfo (callback) { - if (this.conn && this.conn.getPeerInfo) { - return this.conn.getPeerInfo(callback) + if (this.info && this.info.getPeerInfo) { + return this.info.getPeerInfo(callback) } if (!this.peerInfo) { @@ -26,22 +32,27 @@ module.exports = class Connection { callback(null, this.peerInfo) } - setPeerInfo (_peerInfo) { - if (this.conn && this.conn.setPeerInfo) { - return this.conn.setPeerInfo(_peerInfo) + setPeerInfo (peerInfo) { + if (this.info && this.info.setPeerInfo) { + return this.info.setPeerInfo(peerInfo) } - this.peerInfo = _peerInfo + this.peerInfo = peerInfo } getObservedAddrs (callback) { - if (this.conn && this.conn.getObservedAddrs) { - return this.conn.getObservedAddrs(callback) + if (this.info && this.info.getObservedAddrs) { + return this.info.getObservedAddrs(callback) } callback(null, []) } - setInnerConn (_conn) { - this.conn = _conn + setInnerConn (conn, info) { + this.conn.resolve(conn) + if (info) { + this.info = info + } else { + this.info = conn + } } }