|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const debug = require('debug')('skiff.peer') |
| 4 | +const Duplex = require('stream').Duplex |
| 5 | +const Multiaddr = require('multiaddr') |
| 6 | +const Msgpack = require('msgpack5') |
| 7 | + |
| 8 | +const reconnect = require('./reconnect') |
| 9 | + |
| 10 | +const defaultOptions = { |
| 11 | + objectMode: true, |
| 12 | + highWaterMark: 50 |
| 13 | +} |
| 14 | + |
| 15 | +const interestingEvents = [ |
| 16 | + 'connect', |
| 17 | + 'reconnect', |
| 18 | + 'disconnect', |
| 19 | + 'error' |
| 20 | +] |
| 21 | + |
| 22 | +const reconnectOptions = { |
| 23 | + immediate: true |
| 24 | +} |
| 25 | + |
| 26 | +const OK_ERRORS = [ |
| 27 | + 'ECONNREFUSED' |
| 28 | +] |
| 29 | + |
| 30 | +class Peer extends Duplex { |
| 31 | + |
| 32 | + constructor (address, options) { |
| 33 | + debug('constructing peer from address %j', address) |
| 34 | + super(Object.assign({}, options || {}, defaultOptions)) |
| 35 | + this._address = Multiaddr(address) |
| 36 | + |
| 37 | + this.once('finish', this._finish.bind(this)) |
| 38 | + this._connect() |
| 39 | + } |
| 40 | + |
| 41 | + _connect () { |
| 42 | + debug('connecting to %s', this._address) |
| 43 | + |
| 44 | + this._reconnect = reconnect(reconnectOptions, (peer) => { |
| 45 | + debug('got stream to peer %s', this._address) |
| 46 | + const msgpack = Msgpack() |
| 47 | + |
| 48 | + const toPeer = msgpack.encoder() |
| 49 | + toPeer.pipe(peer) |
| 50 | + |
| 51 | + const fromPeer = this._out = msgpack.decoder() |
| 52 | + fromPeer.pipe(this, { end: false }) |
| 53 | + fromPeer.on('data', (data) => this.push(data)) |
| 54 | + |
| 55 | + peer.on('error', (err) => { |
| 56 | + if (OK_ERRORS.indexOf(err.code) === -1) { |
| 57 | + this.emit('error', err) |
| 58 | + } |
| 59 | + }) |
| 60 | + }) |
| 61 | + .connect(this._address) |
| 62 | + |
| 63 | + interestingEvents.forEach((event) => { |
| 64 | + this._reconnect.on(event, (payload) => { |
| 65 | + this.emit(event, payload) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + this._reconnect |
| 70 | + .on('connect', (con) => { |
| 71 | + debug('connected to %s', this._address) |
| 72 | + con.on('error', (err) => { |
| 73 | + debug('error from peer:\n%s', err.stack) |
| 74 | + this.emit('warning', err) |
| 75 | + }) |
| 76 | + }) |
| 77 | + .on('disconnect', () => { |
| 78 | + debug('disconnected from %s', this._address) |
| 79 | + this._out = undefined |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + _read (size) { |
| 84 | + // do nothing, we'll emit data when the peer emits data |
| 85 | + } |
| 86 | + |
| 87 | + _write (message, encoding, callback) { |
| 88 | + debug('writing %j to %s', message, this._address) |
| 89 | + if (this._out) { |
| 90 | + return this._out.write(message, callback) |
| 91 | + } else { |
| 92 | + debug('not connected yet to peer %s', this._address) |
| 93 | + // if we're not connected we discard the message |
| 94 | + callback() |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + _finish () { |
| 99 | + debug('finishing connection to peer %s', this._address) |
| 100 | + this._reconnect.disconnect() |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +module.exports = Peer |
0 commit comments