Skip to content

Commit 293c23a

Browse files
committed
Use default HTTP(S) port when none specified
For driver created with either `http` or `https` url.
1 parent 9d9a092 commit 293c23a

11 files changed

+56
-29
lines changed

src/v1/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ const USER_AGENT = "neo4j-javascript/" + VERSION;
179179
*/
180180
function driver(url, authToken, config = {}) {
181181
assertString(url, 'Bolt URL');
182-
const parsedUrl = urlUtil.parseBoltUrl(url);
182+
const parsedUrl = urlUtil.parseDatabaseUrl(url);
183183
if (parsedUrl.scheme === 'bolt+routing') {
184184
return new RoutingDriver(parsedUrl.hostAndPort, parsedUrl.query, USER_AGENT, authToken, config);
185185
} else if (parsedUrl.scheme === 'bolt') {

src/v1/internal/ch-config.js

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import {SERVICE_UNAVAILABLE} from '../error';
2222

2323
const DEFAULT_CONNECTION_TIMEOUT_MILLIS = 5000; // 5 seconds by default
2424

25-
export const DEFAULT_PORT = 7687;
26-
2725
export default class ChannelConfig {
2826

2927
/**

src/v1/internal/connector.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ class ConnectionState {
586586
*/
587587
function connect(url, config = {}, connectionErrorCode = null) {
588588
const Ch = config.channel || Channel;
589-
const parsedUrl = urlUtil.parseBoltUrl(url);
589+
const parsedUrl = urlUtil.parseDatabaseUrl(url);
590590
const channelConfig = new ChannelConfig(parsedUrl, config, connectionErrorCode);
591591
return new Connection(new Ch(channelConfig), parsedUrl.hostAndPort, config.disableLosslessIntegers);
592592
}

src/v1/internal/host-name-resolvers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class DnsHostNameResolver extends HostNameResolver {
4141
}
4242

4343
resolve(seedRouter) {
44-
const parsedAddress = urlUtil.parseBoltUrl(seedRouter);
44+
const parsedAddress = urlUtil.parseDatabaseUrl(seedRouter);
4545

4646
return new Promise((resolve) => {
4747
this._dns.lookup(parsedAddress.host, {all: true}, (error, addresses) => {

src/v1/internal/http/http-data-converter.js

-7
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ export default class HttpDataConverter {
7171
* @return {object[][]} raw records from the response.
7272
*/
7373
extractRawRecords(response) {
74-
7574
const result = extractResult(response);
76-
77-
7875
if (result) {
7976
const data = result.data;
8077
if (data) {
@@ -153,8 +150,6 @@ function extractResult(response) {
153150
}
154151

155152
function extractRawRecord(data) {
156-
157-
158153
const row = data.row;
159154

160155
const nodesById = indexNodesById(data);
@@ -208,8 +203,6 @@ function indexRelationshipsById(data) {
208203
}
209204

210205
function extractRawRecordElement(index, data, nodesById, relationshipsById) {
211-
212-
213206
const element = data.row ? data.row[index] : null;
214207
const elementMetadata = data.meta ? data.meta[index] : null;
215208

src/v1/internal/url-util.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
import ParsedUrl from 'url-parse';
2121
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;
2326

2427
class 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

115118
function 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+
191204
export default {
192-
parseBoltUrl: parseBoltUrl,
205+
parseDatabaseUrl: parseDatabaseUrl,
206+
defaultPortForScheme: defaultPortForScheme,
193207
formatIPv4Address: formatIPv4Address,
194208
formatIPv6Address: formatIPv6Address
195209
};

test/internal/ch-config.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {SERVICE_UNAVAILABLE} from '../../src/v1/error';
2525
describe('ChannelConfig', () => {
2626

2727
it('should respect given Url', () => {
28-
const url = urlUtil.parseBoltUrl('bolt://neo4j.com:4242');
28+
const url = urlUtil.parseDatabaseUrl('bolt://neo4j.com:4242');
2929

3030
const config = new ChannelConfig(url, {}, '');
3131

test/internal/ch-websocket.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('WebSocketChannel', () => {
7575
};
7676
};
7777

78-
const url = urlUtil.parseBoltUrl('bolt://localhost:7687');
78+
const url = urlUtil.parseDatabaseUrl('bolt://localhost:7687');
7979
const driverConfig = {connectionTimeout: 4242};
8080
const channelConfig = new ChannelConfig(url, driverConfig, SERVICE_UNAVAILABLE);
8181

@@ -112,7 +112,7 @@ describe('WebSocketChannel', () => {
112112
};
113113
};
114114

115-
const url = urlUtil.parseBoltUrl(boltAddress);
115+
const url = urlUtil.parseDatabaseUrl(boltAddress);
116116
// disable connection timeout, so that WebSocketChannel does not set any timeouts
117117
const driverConfig = {connectionTimeout: 0};
118118
const channelConfig = new ChannelConfig(url, driverConfig, SERVICE_UNAVAILABLE);

test/internal/host-name-resolvers.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('DnsHostNameResolver', () => {
7272

7373
addresses.forEach(address => {
7474
expectToBeDefined(address);
75-
const parsedUrl = urlUtil.parseBoltUrl(address);
75+
const parsedUrl = urlUtil.parseDatabaseUrl(address);
7676
expect(parsedUrl.scheme).toBeNull();
7777
expectToBeDefined(parsedUrl.host);
7878
expect(parsedUrl.port).toEqual(7687); // default port should be appended
@@ -91,7 +91,7 @@ describe('DnsHostNameResolver', () => {
9191

9292
addresses.forEach(address => {
9393
expectToBeDefined(address);
94-
const parsedUrl = urlUtil.parseBoltUrl(address);
94+
const parsedUrl = urlUtil.parseDatabaseUrl(address);
9595
expect(parsedUrl.scheme).toBeNull();
9696
expectToBeDefined(parsedUrl.host);
9797
expect(parsedUrl.port).toEqual(7474);

test/internal/http-driver.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ describe('http driver', () => {
225225
});
226226
});
227227

228+
it('should use default HTTP port', done => {
229+
if (testUtils.isServer()) {
230+
done();
231+
return;
232+
}
233+
234+
const driver = neo4j.driver('http://localhost', sharedNeo4j.authToken);
235+
const session = driver.session();
236+
session.run('RETURN 4242').then(result => {
237+
expect(result.records[0].get(0)).toEqual(4242);
238+
expect(result.summary.server.address).toEqual('localhost:7474');
239+
done();
240+
});
241+
});
242+
228243
function testSendAndReceiveWithReturnQuery(values, done) {
229244
const query = 'RETURN $value';
230245

test/internal/url-util.test.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
import urlUtil from '../../src/v1/internal/url-util';
21-
import {DEFAULT_PORT} from '../../src/v1/internal/ch-config';
2221

2322
describe('url-util', () => {
2423

@@ -716,6 +715,14 @@ describe('url-util', () => {
716715
expect(() => urlUtil.formatIPv6Address('1afc:0:a33:85a3::ff2f]', 4000)).toThrow();
717716
});
718717

718+
it('should use default ports when no port specified', () => {
719+
expect(parse('bolt://localhost').port).toEqual(urlUtil.defaultPortForScheme('bolt'));
720+
expect(parse('bolt+routing://localhost').port).toEqual(urlUtil.defaultPortForScheme('bolt'));
721+
722+
expect(parse('http://localhost').port).toEqual(urlUtil.defaultPortForScheme('http'));
723+
expect(parse('https://localhost').port).toEqual(urlUtil.defaultPortForScheme('https'));
724+
});
725+
719726
function verifyUrl(urlString, expectedUrl) {
720727
const url = parse(urlString);
721728

@@ -732,7 +739,7 @@ describe('url-util', () => {
732739
if (expectedUrl.port) {
733740
expect(url.port).toEqual(expectedUrl.port);
734741
} else {
735-
expect(url.port).toEqual(DEFAULT_PORT);
742+
expect(url.port).toEqual(urlUtil.defaultPortForScheme(expectedUrl.scheme));
736743
}
737744

738745
verifyHostAndPort(url, expectedUrl);
@@ -745,7 +752,7 @@ describe('url-util', () => {
745752
}
746753

747754
function verifyHostAndPort(url, expectedUrl) {
748-
const port = expectedUrl.port === 0 || expectedUrl.port ? expectedUrl.port : DEFAULT_PORT;
755+
const port = expectedUrl.port === 0 || expectedUrl.port ? expectedUrl.port : urlUtil.defaultPortForScheme(expectedUrl.scheme);
749756

750757
if (expectedUrl.ipv6) {
751758
expect(url.hostAndPort).toEqual(`[${expectedUrl.host}]:${port}`);
@@ -755,7 +762,7 @@ describe('url-util', () => {
755762
}
756763

757764
function parse(url) {
758-
return urlUtil.parseBoltUrl(url);
765+
return urlUtil.parseDatabaseUrl(url);
759766
}
760767

761768
});

0 commit comments

Comments
 (0)