Skip to content

Commit 85c20ac

Browse files
committed
support multiple hosts in pg-connection-string
1 parent ddf6294 commit 85c20ac

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

packages/pg-connection-string/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface ConnectionOptions {
88
database: string | null | undefined
99
client_encoding?: string
1010
ssl?: boolean | string
11+
multihost?: string | null
1112

1213
application_name?: string
1314
fallback_application_name?: string

packages/pg-connection-string/index.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,26 @@ function parse(str) {
1212
return { host: config[0], database: config[1] }
1313
}
1414

15+
const multihost = str.includes(',')
16+
1517
// Check for empty host in URL
1618

1719
const config = {}
1820
let result
1921
let dummyHost = false
2022
if (/ |%[^a-f0-9]|%[a-f0-9][^a-f0-9]/i.test(str)) {
2123
// Ensure spaces are encoded as %20
22-
str = encodeURI(str).replace(/\%25(\d\d)/g, '%$1')
24+
str = encodeURI(str).replace(/%25(\d\d)/g, '%$1')
2325
}
2426

27+
let host = str
28+
host = host.slice(host.indexOf('://') + 3).split(/[?/]/)[0]
29+
host = decodeURIComponent(host.slice(host.indexOf('@') + 1))
30+
31+
const multihosts = host.split(',')
32+
2533
try {
26-
result = new URL(str, 'postgres://base')
34+
result = new URL(str.replace(host, multihosts[0]), 'postgres://base')
2735
} catch (e) {
2836
// The URL is invalid so try again with a dummy host
2937
result = new URL(str.replace('@/', '@___DUMMY___/'), 'postgres://base')
@@ -34,11 +42,12 @@ function parse(str) {
3442
for (const entry of result.searchParams.entries()) {
3543
config[entry[0]] = entry[1]
3644
}
45+
config.multihost = multihosts.length > 1 ? multihosts : null
3746

3847
config.user = config.user || decodeURIComponent(result.username)
3948
config.password = config.password || decodeURIComponent(result.password)
4049

41-
if (result.protocol == 'socket:') {
50+
if (result.protocol === 'socket:') {
4251
config.host = decodeURI(result.pathname)
4352
config.database = result.searchParams.get('db')
4453
config.client_encoding = result.searchParams.get('encoding')

packages/pg-connection-string/test/parse.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
var chai = require('chai')
4-
var expect = chai.expect
54
chai.should()
65

76
var parse = require('../').parse
@@ -16,6 +15,17 @@ describe('parse', function () {
1615
subject.database.should.equal('lala')
1716
})
1817

18+
it('using connection string with multiple hosts', () => {
19+
const subject = parse('postgres://brian:pw@host1:123,host2:456/lala')
20+
subject.user.should.equal('brian')
21+
subject.host.should.equal('host1')
22+
subject.multihost.should.deep.equal(['host1:123', 'host2:456'])
23+
24+
subject.port.should.equal('123')
25+
subject.password.should.equal('pw')
26+
subject.database.should.equal('lala')
27+
})
28+
1929
it('escape spaces if present', function () {
2030
var subject = parse('postgres://localhost/post gres')
2131
subject.database.should.equal('post gres')

0 commit comments

Comments
 (0)