Skip to content

Commit c47725d

Browse files
committed
Better handling of hostname/ip:port format (#837)
* Better handling of hostname/ip:port format * Updated test
1 parent d551112 commit c47725d

File tree

2 files changed

+92
-10
lines changed

2 files changed

+92
-10
lines changed

lib/ConnectionPool.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,14 @@ class ConnectionPool {
336336
// if we encounter the second case, we should
337337
// use the hostname instead of the ip
338338
var address = node.http.publish_address
339-
const hostAndIpRegex = /^[a-z0-9_.-]*\/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/gi
340-
const match = address.match(hostAndIpRegex)
341-
if (match !== null) {
342-
const ipRegex = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
343-
const ip = address.match(ipRegex)[0]
344-
// extract the hostname, the -1 at the end removes the final /
345-
const hostname = address.slice(0, address.indexOf(ip) - 1)
346-
const port = address.split(':')[1]
339+
const parts = address.split('/')
340+
// the url is in the form of hostname/ip:port
341+
if (parts.length > 1) {
342+
const hostname = parts[0]
343+
const port = parts[1].match(/((?::))(?:[0-9]+)$/g)[0].slice(1)
347344
address = `${hostname}:${port}`
348345
}
346+
349347
address = address.slice(0, 4) === 'http'
350348
? address
351349
: `${protocol}//${address}`

test/unit/connection-pool.test.js

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ test('API', t => {
282282
})
283283

284284
t.test('nodesToHost', t => {
285-
t.test('publish_address as ip address', t => {
285+
t.test('publish_address as ip address (IPv4)', t => {
286286
const pool = new ConnectionPool({ Connection })
287287
const nodes = {
288288
a1: {
@@ -324,7 +324,49 @@ test('API', t => {
324324
t.end()
325325
})
326326

327-
t.test('publish_address as host/ip', t => {
327+
t.test('publish_address as ip address (IPv6)', t => {
328+
const pool = new ConnectionPool({ Connection })
329+
const nodes = {
330+
a1: {
331+
http: {
332+
publish_address: '[::1]:9200'
333+
},
334+
roles: ['master', 'data', 'ingest']
335+
},
336+
a2: {
337+
http: {
338+
publish_address: '[::1]:9201'
339+
},
340+
roles: ['master', 'data', 'ingest']
341+
}
342+
}
343+
344+
t.deepEqual(pool.nodesToHost(nodes, 'http:'), [{
345+
url: new URL('http://[::1]:9200'),
346+
id: 'a1',
347+
roles: {
348+
master: true,
349+
data: true,
350+
ingest: true,
351+
ml: false
352+
}
353+
}, {
354+
url: new URL('http://[::1]:9201'),
355+
id: 'a2',
356+
roles: {
357+
master: true,
358+
data: true,
359+
ingest: true,
360+
ml: false
361+
}
362+
}])
363+
364+
t.strictEqual(pool.nodesToHost(nodes, 'http:')[0].url.host, '[::1]:9200')
365+
t.strictEqual(pool.nodesToHost(nodes, 'http:')[1].url.host, '[::1]:9201')
366+
t.end()
367+
})
368+
369+
t.test('publish_address as host/ip (IPv4)', t => {
328370
const pool = new ConnectionPool({ Connection })
329371
const nodes = {
330372
a1: {
@@ -366,6 +408,48 @@ test('API', t => {
366408
t.end()
367409
})
368410

411+
t.test('publish_address as host/ip (IPv6)', t => {
412+
const pool = new ConnectionPool({ Connection })
413+
const nodes = {
414+
a1: {
415+
http: {
416+
publish_address: 'example.com/[::1]:9200'
417+
},
418+
roles: ['master', 'data', 'ingest']
419+
},
420+
a2: {
421+
http: {
422+
publish_address: 'example.com/[::1]:9201'
423+
},
424+
roles: ['master', 'data', 'ingest']
425+
}
426+
}
427+
428+
t.deepEqual(pool.nodesToHost(nodes, 'http:'), [{
429+
url: new URL('http://example.com:9200'),
430+
id: 'a1',
431+
roles: {
432+
master: true,
433+
data: true,
434+
ingest: true,
435+
ml: false
436+
}
437+
}, {
438+
url: new URL('http://example.com:9201'),
439+
id: 'a2',
440+
roles: {
441+
master: true,
442+
data: true,
443+
ingest: true,
444+
ml: false
445+
}
446+
}])
447+
448+
t.strictEqual(pool.nodesToHost(nodes, 'http:')[0].url.host, 'example.com:9200')
449+
t.strictEqual(pool.nodesToHost(nodes, 'http:')[1].url.host, 'example.com:9201')
450+
t.end()
451+
})
452+
369453
t.test('Should use the configure protocol', t => {
370454
const pool = new ConnectionPool({ Connection })
371455
const nodes = {

0 commit comments

Comments
 (0)