Skip to content

Commit ff3effc

Browse files
author
Yuriy Petrov
committed
fix the dns resolution of multiple hosts
1 parent b6d69d5 commit ff3effc

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

packages/pg/lib/connection-parameters.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

3-
var dns = require('dns')
3+
var { promisify } = require('util')
4+
5+
var dnsLookup = promisify(require('dns').lookup)
46

57
var defaults = require('./defaults')
68

@@ -150,11 +152,15 @@ class ConnectionParameters {
150152
if (this.client_encoding) {
151153
params.push('client_encoding=' + quoteParamValue(this.client_encoding))
152154
}
153-
dns.lookup(this.host, function (err, address) {
154-
if (err) return cb(err, null)
155-
params.push('hostaddr=' + quoteParamValue(address))
156-
return cb(null, params.join(' '))
157-
})
155+
Promise.all(this.host.split(',').map((host) => dnsLookup(host))).then(
156+
(results) => {
157+
params.push('hostaddr=' + quoteParamValue(results.map(({ address }) => address).join(',')))
158+
cb(null, params.join(' '))
159+
},
160+
(err) => {
161+
cb(err, null)
162+
}
163+
)
158164
}
159165
}
160166

packages/pg/test/unit/connection-parameters/creation-tests.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ test('libpq connection string building', function () {
177177
user: 'brian',
178178
password: 'asdf',
179179
port: 5432,
180-
host: 'localhost',
180+
host: 'localhost,localhost',
181181
}
182182
var subject = new ConnectionParameters(config)
183183
subject.getLibpqConnectionString(
184184
assert.calls(function (err, constring) {
185185
assert(!err)
186186
var parts = constring.split(' ')
187187
checkForPart(parts, "user='brian'")
188-
checkForPart(parts, "hostaddr='127.0.0.1'")
188+
checkForPart(parts, "hostaddr='127.0.0.1,127.0.0.1'")
189189
})
190190
)
191191
})
@@ -195,7 +195,7 @@ test('libpq connection string building', function () {
195195
user: 'brian',
196196
password: 'asf',
197197
port: 5432,
198-
host: 'asdlfkjasldfkksfd#!$!!!!..com',
198+
host: 'localhost,invalid',
199199
}
200200
var subject = new ConnectionParameters(config)
201201
subject.getLibpqConnectionString(

0 commit comments

Comments
 (0)