diff --git a/src/lib/nodes_to_host.js b/src/lib/nodes_to_host.js index 416d4beaf..4e6a01de3 100644 --- a/src/lib/nodes_to_host.js +++ b/src/lib/nodes_to_host.js @@ -1,35 +1,58 @@ var _ = require('./utils'); var extractHostPartsRE1x = /\[\/*([^:]+):(\d+)\]/; -var extractHostPartsRE = /^([\.:0-9a-f]*):([0-9]+)?$/; function makeNodeParser(hostProp) { return function (nodes) { return _.transform(nodes, function (hosts, node, id) { - if (!node[hostProp]) { - return; - } + var address = node[hostProp] + if (!address) return; - var hostnameMatches = extractHostPartsRE.exec(node[hostProp]); - if (!hostnameMatches) { - hostnameMatches = extractHostPartsRE1x.exec(node[hostProp]); - } - - if (!hostnameMatches) { - throw new Error('expected node\'s ' + hostProp + ' property (' + JSON.stringify(node[hostProp]) + - ') to match either ' + extractHostPartsRE + ' or ' + extractHostPartsRE1x + '.'); - } - - hosts.push({ - host: hostnameMatches[1], - port: parseInt(hostnameMatches[2], 10), + var host = { + host: undefined, + port: undefined, _meta: { id: id, name: node.name, hostname: node.hostname, version: node.version } - }); + }; + + var malformedError = new Error( + 'Malformed ' + hostProp + '.' + + ' Got ' + JSON.stringify(node[hostProp]) + + ' and expected it to match "{hostname?}/{ip}:{port}".' + ); + + var matches1x = extractHostPartsRE1x.exec(address); + if (matches1x) { + host.host = matches1x[1]; + host.port = parseInt(matches1x[2], 10); + hosts.push(host); + return; + } + + if (address.indexOf('/') > -1) { + var withHostParts = address.split('/'); + if (withHostParts.length !== 2) throw malformedError; + + host.host = withHostParts.shift(); + address = withHostParts.shift(); + } + + if (address.indexOf(':') < 0) { + throw malformedError; + } + + var addressParts = address.split(':'); + if (addressParts.length !== 2) { + throw malformedError; + } + + host.host = host.host || addressParts[0]; + host.port = parseInt(addressParts[1], 10); + hosts.push(host); }, []); }; } diff --git a/test/fixtures/short_node_list.2.0.json b/test/fixtures/short_node_list.2.0.json index 8d8248136..4d00bf04a 100644 --- a/test/fixtures/short_node_list.2.0.json +++ b/test/fixtures/short_node_list.2.0.json @@ -11,6 +11,6 @@ "transport_address": "10.10.10.101:9300", "hostname": "Johns-MacBook-Pro.local", "version": "0.90.5", - "http_address": "10.10.10.101:9205" + "http_address": "published.hostname/10.10.10.101:9205" } } diff --git a/test/unit/specs/nodes_to_host_callback.js b/test/unit/specs/nodes_to_host_callback.js index 6e6f4806d..2f52b4bd3 100644 --- a/test/unit/specs/nodes_to_host_callback.js +++ b/test/unit/specs/nodes_to_host_callback.js @@ -48,7 +48,7 @@ describe('Nodes to host callback', function () { } }); expect(hosts[1]).to.eql({ - host: '10.10.10.101', + host: 'published.hostname', port: 9205, _meta: { id: 'id2', @@ -79,7 +79,7 @@ describe('Nodes to host callback', function () { http_address: 'not actually an http host' } }); - }).to.throwException(/expected.*property.*match either/); + }).to.throwException(/^Malformed http_address/); }); });