From ed5727a0fe47add34a752f46094b8bf5835cab49 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Tue, 9 Aug 2016 12:16:16 +0200 Subject: [PATCH] feat(connection): migrate to pull-streams --- README.md | 8 ----- package.json | 6 ++-- src/connection.js | 68 +++++++++++++++++++----------------- src/index.js | 3 +- {tests => test}/base-test.js | 2 ++ {tests => test}/index.js | 2 ++ test/test.spec.js | 3 -- 7 files changed, 44 insertions(+), 48 deletions(-) rename {tests => test}/base-test.js (92%) rename {tests => test}/index.js (91%) delete mode 100644 test/test.spec.js 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 93aea36..a4ed3b7 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,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 ", "Pau Ramon Revilla " ] -} \ No newline at end of file +} diff --git a/src/connection.js b/src/connection.js index 03458d5..02f2786 100644 --- a/src/connection.js +++ b/src/connection.js @@ -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 + } } } diff --git a/src/index.js b/src/index.js index c15a2c7..c4c79fd 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,3 @@ 'use strict' -exports.connection = require('./connection.js') -exports.Connection = require('./connection.js') +exports.Connection = require('./connection') diff --git a/tests/base-test.js b/test/base-test.js similarity index 92% rename from tests/base-test.js rename to test/base-test.js index 4d42a06..b3f0a78 100644 --- a/tests/base-test.js +++ b/test/base-test.js @@ -1,3 +1,5 @@ +'use strict' + module.exports.all = function (test, common) { test('a test', function (t) { common.setup(test, function (err, conn) { diff --git a/tests/index.js b/test/index.js similarity index 91% rename from tests/index.js rename to test/index.js index b232406..5438379 100644 --- a/tests/index.js +++ b/test/index.js @@ -1,3 +1,5 @@ +'use strict' + var timed = require('timed-tape') module.exports = function (test, common) { diff --git a/test/test.spec.js b/test/test.spec.js deleted file mode 100644 index 68b07e9..0000000 --- a/test/test.spec.js +++ /dev/null @@ -1,3 +0,0 @@ -'use strict' - -// so that aegir does not burp