@@ -12,18 +12,26 @@ function parse(str) {
12
12
return { host : config [ 0 ] , database : config [ 1 ] }
13
13
}
14
14
15
+ const multihost = str . includes ( ',' )
16
+
15
17
// Check for empty host in URL
16
18
17
19
const config = { }
18
20
let result
19
21
let dummyHost = false
20
22
if ( / | % [ ^ a - f 0 - 9 ] | % [ a - f 0 - 9 ] [ ^ a - f 0 - 9 ] / i. test ( str ) ) {
21
23
// Ensure spaces are encoded as %20
22
- str = encodeURI ( str ) . replace ( / \ %2 5 ( \d \d ) / g, '%$1' )
24
+ str = encodeURI ( str ) . replace ( / % 2 5 ( \d \d ) / g, '%$1' )
23
25
}
24
26
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
+
25
33
try {
26
- result = new URL ( str , 'postgres://base' )
34
+ result = new URL ( str . replace ( host , multihosts [ 0 ] ) , 'postgres://base' )
27
35
} catch ( e ) {
28
36
// The URL is invalid so try again with a dummy host
29
37
result = new URL ( str . replace ( '@/' , '@___DUMMY___/' ) , 'postgres://base' )
@@ -34,11 +42,12 @@ function parse(str) {
34
42
for ( const entry of result . searchParams . entries ( ) ) {
35
43
config [ entry [ 0 ] ] = entry [ 1 ]
36
44
}
45
+ config . multihost = multihosts . length > 1 ? multihosts : null
37
46
38
47
config . user = config . user || decodeURIComponent ( result . username )
39
48
config . password = config . password || decodeURIComponent ( result . password )
40
49
41
- if ( result . protocol == 'socket:' ) {
50
+ if ( result . protocol === 'socket:' ) {
42
51
config . host = decodeURI ( result . pathname )
43
52
config . database = result . searchParams . get ( 'db' )
44
53
config . client_encoding = result . searchParams . get ( 'encoding' )
0 commit comments