16
16
* limitations under the License.
17
17
*/
18
18
19
- import ParsedUrl from 'url-parse ' ;
19
+ import { parse as uriJsParse } from 'uri-js ' ;
20
20
import { assertString } from './util' ;
21
21
22
22
const DEFAULT_BOLT_PORT = 7687 ;
@@ -68,14 +68,14 @@ function parseDatabaseUrl(url) {
68
68
assertString ( url , 'URL' ) ;
69
69
70
70
const sanitized = sanitizeUrl ( url ) ;
71
- const parsedUrl = new ParsedUrl ( sanitized . url , { } , query => extractQuery ( query , url ) ) ;
71
+ const parsedUrl = uriJsParse ( sanitized . url ) ;
72
72
73
- const scheme = sanitized . schemeMissing ? null : extractScheme ( parsedUrl . protocol ) ;
74
- const rawHost = extractHost ( parsedUrl . hostname ) ; // has square brackets for IPv6
75
- const host = unescapeIPv6Address ( rawHost ) ; // no square brackets for IPv6
73
+ const scheme = sanitized . schemeMissing ? null : extractScheme ( parsedUrl . scheme ) ;
74
+ const host = extractHost ( parsedUrl . host ) ; // no square brackets for IPv6
75
+ const formattedHost = formatHost ( host ) ; // has square brackets for IPv6
76
76
const port = extractPort ( parsedUrl . port , scheme ) ;
77
- const hostAndPort = `${ rawHost } :${ port } ` ;
78
- const query = parsedUrl . query ;
77
+ const hostAndPort = `${ formattedHost } :${ port } ` ;
78
+ const query = extractQuery ( parsedUrl . query , url ) ;
79
79
80
80
return new Url ( scheme , host , port , hostAndPort , query ) ;
81
81
}
@@ -84,8 +84,8 @@ function sanitizeUrl(url) {
84
84
url = url . trim ( ) ;
85
85
86
86
if ( url . indexOf ( '://' ) === - 1 ) {
87
- // url does not contain scheme, add dummy 'http ://' to make parser work correctly
88
- return { schemeMissing : true , url : `http ://${ url } ` } ;
87
+ // url does not contain scheme, add dummy 'none ://' to make parser work correctly
88
+ return { schemeMissing : true , url : `none ://${ url } ` } ;
89
89
}
90
90
91
91
return { schemeMissing : false , url : url } ;
@@ -168,17 +168,12 @@ function escapeIPv6Address(address) {
168
168
}
169
169
}
170
170
171
- function unescapeIPv6Address ( address ) {
172
- const startsWithSquareBracket = address . charAt ( 0 ) === '[' ;
173
- const endsWithSquareBracket = address . charAt ( address . length - 1 ) === ']' ;
174
-
175
- if ( ! startsWithSquareBracket && ! endsWithSquareBracket ) {
176
- return address ;
177
- } else if ( startsWithSquareBracket && endsWithSquareBracket ) {
178
- return address . substring ( 1 , address . length - 1 ) ;
179
- } else {
180
- throw new Error ( `Illegal IPv6 address ${ address } ` ) ;
171
+ function formatHost ( host ) {
172
+ if ( ! host ) {
173
+ throw new Error ( `Illegal host ${ host } ` ) ;
181
174
}
175
+ const isIPv6Address = host . indexOf ( ':' ) >= 0 ;
176
+ return isIPv6Address ? escapeIPv6Address ( host ) : host ;
182
177
}
183
178
184
179
function formatIPv4Address ( address , port ) {
0 commit comments