1
- 'use strict' ;
1
+ 'use strict'
2
2
3
- var url = require ( 'url' ) ;
4
- var fs = require ( 'fs' ) ;
3
+ var url = require ( 'url' )
4
+ var fs = require ( 'fs' )
5
5
6
6
//Parse method copied from https://github.com/brianc/node-postgres
7
7
//Copyright (c) 2010-2014 Brian Carlson (brian.m.carlson@gmail.com)
@@ -10,78 +10,80 @@ var fs = require('fs');
10
10
//parses a connection string
11
11
function parse ( str ) {
12
12
//unix socket
13
- if ( str . charAt ( 0 ) === '/' ) {
14
- var config = str . split ( ' ' ) ;
15
- return { host : config [ 0 ] , database : config [ 1 ] } ;
13
+ if ( str . charAt ( 0 ) === '/' ) {
14
+ var config = str . split ( ' ' )
15
+ return { host : config [ 0 ] , database : config [ 1 ] }
16
16
}
17
17
18
18
// url parse expects spaces encoded as %20
19
- var result = url . parse ( / | % [ ^ a - f 0 - 9 ] | % [ a - f 0 - 9 ] [ ^ a - f 0 - 9 ] / i. test ( str ) ? encodeURI ( str ) . replace ( / \% 2 5 ( \d \d ) / g, "%$1" ) : str , true ) ;
20
- var config = result . query ;
19
+ var result = url . parse (
20
+ / | % [ ^ a - f 0 - 9 ] | % [ a - f 0 - 9 ] [ ^ a - f 0 - 9 ] / i. test ( str ) ? encodeURI ( str ) . replace ( / \% 2 5 ( \d \d ) / g, '%$1' ) : str ,
21
+ true
22
+ )
23
+ var config = result . query
21
24
for ( var k in config ) {
22
25
if ( Array . isArray ( config [ k ] ) ) {
23
- config [ k ] = config [ k ] [ config [ k ] . length - 1 ] ;
26
+ config [ k ] = config [ k ] [ config [ k ] . length - 1 ]
24
27
}
25
28
}
26
29
27
- var auth = ( result . auth || ':' ) . split ( ':' ) ;
28
- config . user = auth [ 0 ] ;
29
- config . password = auth . splice ( 1 ) . join ( ':' ) ;
30
+ var auth = ( result . auth || ':' ) . split ( ':' )
31
+ config . user = auth [ 0 ]
32
+ config . password = auth . splice ( 1 ) . join ( ':' )
30
33
31
- config . port = result . port ;
32
- if ( result . protocol == 'socket:' ) {
33
- config . host = decodeURI ( result . pathname ) ;
34
- config . database = result . query . db ;
35
- config . client_encoding = result . query . encoding ;
36
- return config ;
34
+ config . port = result . port
35
+ if ( result . protocol == 'socket:' ) {
36
+ config . host = decodeURI ( result . pathname )
37
+ config . database = result . query . db
38
+ config . client_encoding = result . query . encoding
39
+ return config
37
40
}
38
41
if ( ! config . host ) {
39
42
// Only set the host if there is no equivalent query param.
40
- config . host = result . hostname ;
43
+ config . host = result . hostname
41
44
}
42
45
43
46
// If the host is missing it might be a URL-encoded path to a socket.
44
- var pathname = result . pathname ;
47
+ var pathname = result . pathname
45
48
if ( ! config . host && pathname && / ^ % 2 f / i. test ( pathname ) ) {
46
- var pathnameSplit = pathname . split ( '/' ) ;
47
- config . host = decodeURIComponent ( pathnameSplit [ 0 ] ) ;
48
- pathname = pathnameSplit . splice ( 1 ) . join ( '/' ) ;
49
+ var pathnameSplit = pathname . split ( '/' )
50
+ config . host = decodeURIComponent ( pathnameSplit [ 0 ] )
51
+ pathname = pathnameSplit . splice ( 1 ) . join ( '/' )
49
52
}
50
53
// result.pathname is not always guaranteed to have a '/' prefix (e.g. relative urls)
51
54
// only strip the slash if it is present.
52
55
if ( pathname && pathname . charAt ( 0 ) === '/' ) {
53
- pathname = pathname . slice ( 1 ) || null ;
56
+ pathname = pathname . slice ( 1 ) || null
54
57
}
55
- config . database = pathname && decodeURI ( pathname ) ;
58
+ config . database = pathname && decodeURI ( pathname )
56
59
57
60
if ( config . ssl === 'true' || config . ssl === '1' ) {
58
- config . ssl = true ;
61
+ config . ssl = true
59
62
}
60
63
61
64
if ( config . ssl === '0' ) {
62
- config . ssl = false ;
65
+ config . ssl = false
63
66
}
64
67
65
68
if ( config . sslcert || config . sslkey || config . sslrootcert ) {
66
- config . ssl = { } ;
69
+ config . ssl = { }
67
70
}
68
71
69
72
if ( config . sslcert ) {
70
- config . ssl . cert = fs . readFileSync ( config . sslcert ) . toString ( ) ;
73
+ config . ssl . cert = fs . readFileSync ( config . sslcert ) . toString ( )
71
74
}
72
75
73
76
if ( config . sslkey ) {
74
- config . ssl . key = fs . readFileSync ( config . sslkey ) . toString ( ) ;
77
+ config . ssl . key = fs . readFileSync ( config . sslkey ) . toString ( )
75
78
}
76
79
77
80
if ( config . sslrootcert ) {
78
- config . ssl . ca = fs . readFileSync ( config . sslrootcert ) . toString ( ) ;
81
+ config . ssl . ca = fs . readFileSync ( config . sslrootcert ) . toString ( )
79
82
}
80
83
81
- return config ;
84
+ return config
82
85
}
83
86
87
+ module . exports = parse
84
88
85
- module . exports = parse ;
86
-
87
- parse . parse = parse ;
89
+ parse . parse = parse
0 commit comments