19
19
20
20
import ParsedUrl from 'url-parse' ;
21
21
import { assertString } from './util' ;
22
- import { DEFAULT_PORT } from './ch-config' ;
22
+
23
+ const DEFAULT_BOLT_PORT = 7687 ;
24
+ const DEFAULT_HTTP_PORT = 7474 ;
25
+ const DEFAULT_HTTPS_PORT = 7473 ;
23
26
24
27
class Url {
25
28
@@ -39,8 +42,8 @@ class Url {
39
42
this . host = host ;
40
43
41
44
/**
42
- * Nonnull number representing port. Default port { @link DEFAULT_PORT} value is used if given URL string
43
- * does not contain port. Example: 7687, 12000, etc .
45
+ * Nonnull number representing port. Default port for the given scheme is used if given URL string
46
+ * does not contain port. Example: 7687 for bolt, 7474 for HTTP and 7473 for HTTPS .
44
47
* @type {number }
45
48
*/
46
49
this . port = port ;
@@ -62,7 +65,7 @@ class Url {
62
65
}
63
66
}
64
67
65
- function parseBoltUrl ( url ) {
68
+ function parseDatabaseUrl ( url ) {
66
69
assertString ( url , 'URL' ) ;
67
70
68
71
const sanitized = sanitizeUrl ( url ) ;
@@ -71,7 +74,7 @@ function parseBoltUrl(url) {
71
74
const scheme = sanitized . schemeMissing ? null : extractScheme ( parsedUrl . protocol ) ;
72
75
const rawHost = extractHost ( parsedUrl . hostname ) ; // has square brackets for IPv6
73
76
const host = unescapeIPv6Address ( rawHost ) ; // no square brackets for IPv6
74
- const port = extractPort ( parsedUrl . port ) ;
77
+ const port = extractPort ( parsedUrl . port , scheme ) ;
75
78
const hostAndPort = `${ rawHost } :${ port } ` ;
76
79
const query = parsedUrl . query ;
77
80
@@ -107,9 +110,9 @@ function extractHost(host, url) {
107
110
return host . trim ( ) ;
108
111
}
109
112
110
- function extractPort ( portString ) {
113
+ function extractPort ( portString , scheme ) {
111
114
const port = parseInt ( portString , 10 ) ;
112
- return ( port === 0 || port ) ? port : DEFAULT_PORT ;
115
+ return ( port === 0 || port ) ? port : defaultPortForScheme ( scheme ) ;
113
116
}
114
117
115
118
function extractQuery ( queryString , url ) {
@@ -188,8 +191,19 @@ function formatIPv6Address(address, port) {
188
191
return `${ escapedAddress } :${ port } ` ;
189
192
}
190
193
194
+ function defaultPortForScheme ( scheme ) {
195
+ if ( scheme === 'http' ) {
196
+ return DEFAULT_HTTP_PORT ;
197
+ } else if ( scheme === 'https' ) {
198
+ return DEFAULT_HTTPS_PORT ;
199
+ } else {
200
+ return DEFAULT_BOLT_PORT ;
201
+ }
202
+ }
203
+
191
204
export default {
192
- parseBoltUrl : parseBoltUrl ,
205
+ parseDatabaseUrl : parseDatabaseUrl ,
206
+ defaultPortForScheme : defaultPortForScheme ,
193
207
formatIPv4Address : formatIPv4Address ,
194
208
formatIPv6Address : formatIPv6Address
195
209
} ;
0 commit comments