Skip to content

Commit 1eb4a9a

Browse files
Vitalii4ashackolade-bot
andauthored
Fix connection to neo4j when port is specified (#62)
* Fix connection to neo4j when port is specified * Fix replacement of ipv6 in host --------- Co-authored-by: hackolade-bot <[email protected]>
1 parent ef9b590 commit 1eb4a9a

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

reverse_engineering/escapeV6IPForURL.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function escapeV6IpForURL({ host }) {
3030
return host;
3131
}
3232

33-
const urlWithIpV6HostRegExp = new RegExp(/^http(s)?:\/\/(?<unescapedIpWithPort>([a-z0-9]{0,4}:?)+)/gim);
33+
const urlWithIpV6HostRegExp = new RegExp(
34+
/^(http(s)?|(neo4j(\+s|s|)(\+ssc|ssc|))|bolt):\/\/(?<unescapedIpWithPort>([a-z0-9]{0,4}:?)+)/gim,
35+
);
3436
const { unescapedIpWithPort } = urlWithIpV6HostRegExp.exec(host)?.groups ?? {};
3537

3638
if (!unescapedIpWithPort) {
@@ -42,7 +44,13 @@ function escapeV6IpForURL({ host }) {
4244
const port = separatedIpPortionsAndPort.at(-1);
4345
const escapedIpWithPort = `[${ipPortions.join(':')}]:${port}`;
4446

45-
return host.replace(unescapedIpWithPort, escapedIpWithPort);
47+
const replacedHost = host.replace(unescapedIpWithPort, escapedIpWithPort);
48+
49+
if (isValidURL(replacedHost)) {
50+
return replacedHost;
51+
}
52+
53+
return host.replace(unescapedIpWithPort, `[${unescapedIpWithPort}]`);
4654
}
4755

4856
/**
@@ -54,7 +62,8 @@ function isValidURL(url) {
5462
new URL(url);
5563

5664
return true;
57-
} catch {
65+
} catch (error) {
66+
console.error(error);
5867
return false;
5968
}
6069
}

reverse_engineering/neo4jHelper.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,13 @@ const getConnectionURI = info => {
431431
if (neo4jProtocolRegex.test(info.host)) {
432432
host = info.host;
433433
} else {
434-
host = `bolt://${escapeV6IpForURL({ host: info.host })}`;
434+
host = `bolt://${info.host}`;
435435
}
436436
if (info.port) {
437-
host = `${escapeV6IpForURL({ host: info.host })}:${info.port}`;
437+
host = `${host}:${info.port}`;
438438
}
439439

440-
return host;
440+
return escapeV6IpForURL({ host });
441441
};
442442

443443
module.exports = {

0 commit comments

Comments
 (0)