refactor utils.
This commit is contained in:
parent
39a8ecc398
commit
b4bfe53bfe
@ -119,7 +119,7 @@ function Peer(pool, options) {
|
|||||||
assert(typeof this.host === 'string');
|
assert(typeof this.host === 'string');
|
||||||
assert(typeof this.port === 'number');
|
assert(typeof this.port === 'number');
|
||||||
|
|
||||||
this.hostname = utils.stringifyHost(this);
|
this.hostname = utils.hostname(this.host, this.port);
|
||||||
|
|
||||||
if (!this.socket)
|
if (!this.socket)
|
||||||
throw new Error('No socket');
|
throw new Error('No socket');
|
||||||
@ -247,28 +247,26 @@ Peer.prototype._init = function init() {
|
|||||||
|
|
||||||
Peer.prototype.createSocket = function createSocket(port, host) {
|
Peer.prototype.createSocket = function createSocket(port, host) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
var hostname = utils.hostname(host, port);
|
||||||
var socket, net;
|
var socket, net;
|
||||||
|
|
||||||
if (this._createSocket) {
|
if (this._createSocket) {
|
||||||
socket = this._createSocket(port, host);
|
socket = this._createSocket(port, host);
|
||||||
} else if (bcoin.isBrowser) {
|
|
||||||
throw new Error('Please include a `createSocket` callback.');
|
|
||||||
} else {
|
} else {
|
||||||
|
if (bcoin.isBrowser)
|
||||||
|
throw new Error('Please include a `createSocket` callback.');
|
||||||
net = require('n' + 'et');
|
net = require('n' + 'et');
|
||||||
socket = net.connect(port, host);
|
socket = net.connect(port, host);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (utils.isIP(host) === 6)
|
|
||||||
host = '[' + host + ']';
|
|
||||||
|
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Connecting to %s:%d (priority=%s).',
|
'Connecting to %s:%d (priority=%s).',
|
||||||
host, port, this.priority);
|
hostname, this.priority);
|
||||||
|
|
||||||
socket.on('connect', function() {
|
socket.on('connect', function() {
|
||||||
bcoin.debug(
|
bcoin.debug(
|
||||||
'Connected to %s:%d (priority=%s).',
|
'Connected to %s:%d (priority=%s).',
|
||||||
host, port, self.priority);
|
hostname, self.priority);
|
||||||
});
|
});
|
||||||
|
|
||||||
return socket;
|
return socket;
|
||||||
|
|||||||
@ -843,8 +843,8 @@ utils.isBTC = function isBTC(value) {
|
|||||||
* Parse a hostname.
|
* Parse a hostname.
|
||||||
* @example
|
* @example
|
||||||
* utils.parseHost('127.0.0.1:3000');
|
* utils.parseHost('127.0.0.1:3000');
|
||||||
* @param {String|Seed} addr
|
* @param {String} addr
|
||||||
* @returns {Seed}
|
* @returns {Object} Contains `host` and `port`.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
utils.parseHost = function parseHost(addr) {
|
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
|
* @param {Seed} seed
|
||||||
* @returns {String}
|
* @returns {String}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
utils.stringifyHost = function stringifyHost(seed) {
|
utils.hostname = function hostname(seed) {
|
||||||
var host = seed.host;
|
var host = seed.host;
|
||||||
|
|
||||||
if (utils.isIP(host) === 6)
|
if (utils.isIP(host) === 6)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user