1919
2020import ParsedUrl from 'url-parse' ;
2121import { 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 ;
2326
2427class Url {
2528
@@ -39,8 +42,8 @@ class Url {
3942 this . host = host ;
4043
4144 /**
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 .
4447 * @type {number }
4548 */
4649 this . port = port ;
@@ -62,7 +65,7 @@ class Url {
6265 }
6366}
6467
65- function parseBoltUrl ( url ) {
68+ function parseDatabaseUrl ( url ) {
6669 assertString ( url , 'URL' ) ;
6770
6871 const sanitized = sanitizeUrl ( url ) ;
@@ -71,7 +74,7 @@ function parseBoltUrl(url) {
7174 const scheme = sanitized . schemeMissing ? null : extractScheme ( parsedUrl . protocol ) ;
7275 const rawHost = extractHost ( parsedUrl . hostname ) ; // has square brackets for IPv6
7376 const host = unescapeIPv6Address ( rawHost ) ; // no square brackets for IPv6
74- const port = extractPort ( parsedUrl . port ) ;
77+ const port = extractPort ( parsedUrl . port , scheme ) ;
7578 const hostAndPort = `${ rawHost } :${ port } ` ;
7679 const query = parsedUrl . query ;
7780
@@ -107,9 +110,9 @@ function extractHost(host, url) {
107110 return host . trim ( ) ;
108111}
109112
110- function extractPort ( portString ) {
113+ function extractPort ( portString , scheme ) {
111114 const port = parseInt ( portString , 10 ) ;
112- return ( port === 0 || port ) ? port : DEFAULT_PORT ;
115+ return ( port === 0 || port ) ? port : defaultPortForScheme ( scheme ) ;
113116}
114117
115118function extractQuery ( queryString , url ) {
@@ -188,8 +191,19 @@ function formatIPv6Address(address, port) {
188191 return `${ escapedAddress } :${ port } ` ;
189192}
190193
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+
191204export default {
192- parseBoltUrl : parseBoltUrl ,
205+ parseDatabaseUrl : parseDatabaseUrl ,
206+ defaultPortForScheme : defaultPortForScheme ,
193207 formatIPv4Address : formatIPv4Address ,
194208 formatIPv6Address : formatIPv6Address
195209} ;
0 commit comments