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

Commit

Permalink
feat(connection): migrate to pull-streams
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Sep 5, 2016
1 parent d4d6d55 commit ed5727a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 48 deletions.
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand All @@ -40,4 +40,4 @@
"David Dias <[email protected]>",
"Pau Ramon Revilla <[email protected]>"
]
}
}
68 changes: 36 additions & 32 deletions src/connection.js
Original file line number Diff line number Diff line change
@@ -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
}
}
}
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
'use strict'

exports.connection = require('./connection.js')
exports.Connection = require('./connection.js')
exports.Connection = require('./connection')
2 changes: 2 additions & 0 deletions tests/base-test.js → test/base-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

module.exports.all = function (test, common) {
test('a test', function (t) {
common.setup(test, function (err, conn) {
Expand Down
2 changes: 2 additions & 0 deletions tests/index.js → test/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict'

var timed = require('timed-tape')

module.exports = function (test, common) {
Expand Down
3 changes: 0 additions & 3 deletions test/test.spec.js

This file was deleted.

0 comments on commit ed5727a

Please sign in to comment.