Skip to content

Commit a1be593

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. I consolidated the logic into ServerDescription getters. NODE-2671
1 parent c3b36cd commit a1be593

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
@@ -66,13 +66,12 @@ class Monitor extends EventEmitter {
6666
});
6767

6868
// TODO: refactor this to pull it directly from the pool, requires new ConnectionPool integration
69-
const addressParts = server.description.address.split(':');
7069
this.connectOptions = Object.freeze(
7170
Object.assign(
7271
{
7372
id: '<monitor>',
74-
host: addressParts[0],
75-
port: parseInt(addressParts[1], 10),
73+
host: server.description.host,
74+
port: server.description.port,
7675
bson: server.s.bson,
7776
connectionType: Connection
7877
},

lib/core/sdam/server.js

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

111111
// create the connection pool
112112
// NOTE: this used to happen in `connect`, we supported overriding pool options there
113-
const addressParts = this.description.address.split(':');
114113
const poolOptions = Object.assign(
115-
{ host: addressParts[0], port: parseInt(addressParts[1], 10), bson: this.s.bson },
114+
{ host: this.description.host, port: this.description.port, bson: this.s.bson },
116115
options
117116
);
118117

lib/core/sdam/server_description.js

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

116+
get host() {
117+
const chopLength = `:${this.port}`.length;
118+
return this.address.slice(0, -chopLength);
119+
}
120+
121+
get port() {
122+
const port = this.address.split(':').pop();
123+
return port ? Number.parseInt(port, 10) : port;
124+
}
125+
116126
/**
117127
* Determines if another `ServerDescription` is equal to this one per the rules defined
118128
* 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
@@ -5,17 +5,16 @@ 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;
77
const expect = require('chai').expect;
8+
const ServerDescription = require('../../../lib/core/sdam/server_description').ServerDescription;
89

910
class MockServer {
1011
constructor(options) {
1112
this.s = {
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)