js-libp2p-interfaces/src/connection.js

57 lines
1.0 KiB
JavaScript
Raw Normal View History

'use strict'
const util = require('util')
const Duplexify = require('duplexify')
module.exports = Connection
util.inherits(Connection, Duplexify)
function Connection (conn) {
if (!(this instanceof Connection)) {
return new Connection(conn)
}
Duplexify.call(this)
let peerInfo
this.getPeerInfo = (callback) => {
2016-06-27 10:06:09 +01:00
if (conn && conn.getPeerInfo) {
return conn.getPeerInfo(callback)
}
if (!peerInfo) {
return callback(new Error('Peer Info not set yet'))
}
callback(null, peerInfo)
}
this.setPeerInfo = (_peerInfo) => {
2016-06-27 10:06:09 +01:00
if (conn && conn.setPeerInfo) {
return conn.setPeerInfo(_peerInfo)
}
peerInfo = _peerInfo
}
this.getObservedAddrs = (callback) => {
2016-06-27 10:06:09 +01:00
if (conn && conn.getObservedAddrs) {
return conn.getObservedAddrs(callback)
}
callback(null, [])
}
2016-06-27 10:06:09 +01:00
this.setInnerConn = (_conn) => {
conn = _conn
this.setReadable(conn)
this.setWritable(conn)
}
2016-06-23 10:33:40 +01:00
// .destroy is implemented by Duplexify
2016-06-23 10:22:20 +01:00
if (conn) {
this.setInnerConn(conn)
}
}