Skip to content

Support IPv6 address #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions mongodb-uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,32 @@ MongodbUriParser.prototype._parseAddress = function _parseAddress(address, uriOb
address.split(',').forEach(function (h) {
var i = h.indexOf(':');
if (i >= 0) {
uriObject.hosts.push(
let _host = h.split(':');
if (_host[0].startsWith('[')) {
if (_host[_host.length - 1].endsWith(']')) {
// IPv6 address without port
uriObject.hosts.push({ host: decodeURIComponent(_host.join(':')) });
} else {
let p = _host.pop();
// IPv6 address with port
uriObject.hosts.push(
{
host: decodeURIComponent(_host.join(':')),
port: parseInt(p)
}
);
}
} else {
// IPv4 address with port
uriObject.hosts.push(
{
host: decodeURIComponent(h.substring(0, i)),
port: parseInt(h.substring(i + 1))
host: decodeURIComponent(_host[0]),
port: parseInt(_host[1])
}
);
);
}
} else {
// IPv4 address without port
uriObject.hosts.push({ host: decodeURIComponent(h) });
}
});
Expand Down