Skip to content

Commit 19ec62f

Browse files
authored
fix: IPv6 is not supported when using dns service discovery
Both server class and monitoring class split the address string on ":" and assumed it would return address and port tuple. To handle IPv6 addresses we need to use the number at the end of the address and keep the IPv6 address from being split. This consolidates the logic into ServerDescription getters. NODE-2671
1 parent 31ae3c9 commit 19ec62f

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

lib/core/sdam/monitor.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ class Monitor extends EventEmitter {
6969
});
7070

7171
// TODO: refactor this to pull it directly from the pool, requires new ConnectionPool integration
72-
const addressParts = server.description.address.split(':');
7372
const connectOptions = Object.assign(
7473
{
7574
id: '<monitor>',
76-
host: addressParts[0],
77-
port: parseInt(addressParts[1], 10),
75+
host: server.description.host,
76+
port: server.description.port,
7877
bson: server.s.bson,
7978
connectionType: Connection
8079
},

lib/core/sdam/server.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,8 @@ class Server extends EventEmitter {
115115

116116
// create the connection pool
117117
// NOTE: this used to happen in `connect`, we supported overriding pool options there
118-
const addressParts = this.description.address.split(':');
119118
const poolOptions = Object.assign(
120-
{ host: addressParts[0], port: parseInt(addressParts[1], 10), bson: this.s.bson },
119+
{ host: this.description.host, port: this.description.port, bson: this.s.bson },
121120
options
122121
);
123122

lib/core/sdam/server_description.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ class ServerDescription {
116116
return WRITABLE_SERVER_TYPES.has(this.type);
117117
}
118118

119+
get host() {
120+
const chopLength = `:${this.port}`.length;
121+
return this.address.slice(0, -chopLength);
122+
}
123+
124+
get port() {
125+
const port = this.address.split(':').pop();
126+
return port ? Number.parseInt(port, 10) : port;
127+
}
128+
119129
/**
120130
* Determines if another `ServerDescription` is equal to this one per the rules defined
121131
* in the {@link https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#serverdescription|SDAM spec}

test/unit/sdam/monitoring.test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const BSON = require('bson');
44
const Topology = require('../../../lib/core/sdam/topology').Topology;
55
const Monitor = require('../../../lib/core/sdam/monitor').Monitor;
66
const ServerType = require('../../../lib/core/sdam/common').ServerType;
7+
const ServerDescription = require('../../../lib/core/sdam/server_description').ServerDescription;
78
const expect = require('chai').expect;
89

910
class MockServer {
@@ -12,10 +13,8 @@ class MockServer {
1213
bson: new BSON()
1314
};
1415

15-
this.description = {
16-
type: ServerType.Unknown,
17-
address: `${options.host}:${options.port}`
18-
};
16+
this.description = new ServerDescription(`${options.host}:${options.port}`);
17+
this.description.type = ServerType.Unknown;
1918
}
2019
}
2120

test/unit/sdam/server_description.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ describe('ServerDescription', function() {
4141
});
4242
});
4343
});
44+
45+
it('should sensibly parse an ipv6 address', function() {
46+
const description = new ServerDescription('abcd:f::abcd:abcd:abcd:abcd:27017');
47+
expect(description.host).to.equal('abcd:f::abcd:abcd:abcd:abcd');
48+
expect(description.port).to.equal(27017);
49+
});
4450
});

0 commit comments

Comments
 (0)