From b4bfe53bfe598f5bbc6a7ea210050dfafa21610d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 19 May 2016 21:31:04 -0700 Subject: [PATCH] refactor utils. --- lib/bcoin/peer.js | 14 ++++++-------- lib/bcoin/utils.js | 8 ++++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 83cb6750..617d2d04 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -119,7 +119,7 @@ function Peer(pool, options) { assert(typeof this.host === 'string'); assert(typeof this.port === 'number'); - this.hostname = utils.stringifyHost(this); + this.hostname = utils.hostname(this.host, this.port); if (!this.socket) throw new Error('No socket'); @@ -247,28 +247,26 @@ Peer.prototype._init = function init() { Peer.prototype.createSocket = function createSocket(port, host) { var self = this; + var hostname = utils.hostname(host, port); var socket, net; if (this._createSocket) { socket = this._createSocket(port, host); - } else if (bcoin.isBrowser) { - throw new Error('Please include a `createSocket` callback.'); } else { + if (bcoin.isBrowser) + throw new Error('Please include a `createSocket` callback.'); net = require('n' + 'et'); socket = net.connect(port, host); } - if (utils.isIP(host) === 6) - host = '[' + host + ']'; - bcoin.debug( 'Connecting to %s:%d (priority=%s).', - host, port, this.priority); + hostname, this.priority); socket.on('connect', function() { bcoin.debug( 'Connected to %s:%d (priority=%s).', - host, port, self.priority); + hostname, self.priority); }); return socket; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 7521e222..063b950f 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -843,8 +843,8 @@ utils.isBTC = function isBTC(value) { * Parse a hostname. * @example * utils.parseHost('127.0.0.1:3000'); - * @param {String|Seed} addr - * @returns {Seed} + * @param {String} addr + * @returns {Object} Contains `host` and `port`. */ utils.parseHost = function parseHost(addr) { @@ -867,12 +867,12 @@ utils.parseHost = function parseHost(addr) { }; /** - * Stringify a host. + * Concatenate a host and port. * @param {Seed} seed * @returns {String} */ -utils.stringifyHost = function stringifyHost(seed) { +utils.hostname = function hostname(seed) { var host = seed.host; if (utils.isIP(host) === 6)