diff --git a/src/utils.ts b/src/utils.ts index 2859132dc4..5e1abced23 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1142,11 +1142,9 @@ export class BufferPool { /** @public */ export class HostAddress { - host: string | undefined; - port: number | undefined; - // Driver only works with unix socket path to connect - // SDAM operates only on tcp addresses - socketPath: string | undefined; + host: string | undefined = undefined; + port: number | undefined = undefined; + socketPath: string | undefined = undefined; isIPv6 = false; constructor(hostString: string) { @@ -1155,8 +1153,6 @@ export class HostAddress { if (escapedHost.endsWith('.sock')) { // heuristically determine if we're working with a domain socket this.socketPath = decodeURIComponent(escapedHost); - delete this.port; - delete this.host; return; } diff --git a/test/unit/connection_string.test.ts b/test/unit/connection_string.test.ts index a04c64722b..5197f41846 100644 --- a/test/unit/connection_string.test.ts +++ b/test/unit/connection_string.test.ts @@ -595,9 +595,9 @@ describe('Connection String', function () { it('should allow multiple bracketed portless localhost IPv6 addresses', () => { const client = new MongoClient('mongodb://[::1],[::1],[::1]/test'); expect(client.options.hosts).to.deep.equal([ - { host: '::1', port: 27017, isIPv6: true }, - { host: '::1', port: 27017, isIPv6: true }, - { host: '::1', port: 27017, isIPv6: true } + { host: '::1', port: 27017, isIPv6: true, socketPath: undefined }, + { host: '::1', port: 27017, isIPv6: true, socketPath: undefined }, + { host: '::1', port: 27017, isIPv6: true, socketPath: undefined } ]); }); @@ -606,18 +606,18 @@ describe('Connection String', function () { 'mongodb://[ABCD:f::abcd:abcd:abcd:abcd],[ABCD:f::abcd:abcd:abcd:abcd],[ABCD:f::abcd:abcd:abcd:abcd]/test' ); expect(client.options.hosts).to.deep.equal([ - { host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true }, - { host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true }, - { host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true } + { host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true, socketPath: undefined }, + { host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true, socketPath: undefined }, + { host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true, socketPath: undefined } ]); }); it('should allow multiple bracketed port-full IPv6 addresses', () => { const client = new MongoClient('mongodb://[::1]:27018,[::1]:27019,[::1]:27020/test'); expect(client.options.hosts).to.deep.equal([ - { host: '::1', port: 27018, isIPv6: true }, - { host: '::1', port: 27019, isIPv6: true }, - { host: '::1', port: 27020, isIPv6: true } + { host: '::1', port: 27018, isIPv6: true, socketPath: undefined }, + { host: '::1', port: 27019, isIPv6: true, socketPath: undefined }, + { host: '::1', port: 27020, isIPv6: true, socketPath: undefined } ]); }); }); diff --git a/test/unit/utils.test.ts b/test/unit/utils.test.ts index fc175ebf92..1d37088b89 100644 --- a/test/unit/utils.test.ts +++ b/test/unit/utils.test.ts @@ -598,27 +598,27 @@ describe('driver utils', function () { it('should handle decoded unix socket path', () => { const ha = new HostAddress(socketPath); expect(ha).to.have.property('socketPath', socketPath); - expect(ha).to.not.have.property('port'); + expect(ha).to.have.property('port', undefined); }); it('should handle encoded unix socket path', () => { const ha = new HostAddress(encodeURIComponent(socketPath)); expect(ha).to.have.property('socketPath', socketPath); - expect(ha).to.not.have.property('port'); + expect(ha).to.have.property('port', undefined); }); it('should handle encoded unix socket path with an unencoded space', () => { const socketPathWithSpaces = '/tmp/some directory/mongodb-27017.sock'; const ha = new HostAddress(socketPathWithSpaces); expect(ha).to.have.property('socketPath', socketPathWithSpaces); - expect(ha).to.not.have.property('port'); + expect(ha).to.have.property('port', undefined); }); it('should handle unix socket path that does not begin with a slash', () => { const socketPathWithoutSlash = 'my_local/directory/mustEndWith.sock'; const ha = new HostAddress(socketPathWithoutSlash); expect(ha).to.have.property('socketPath', socketPathWithoutSlash); - expect(ha).to.not.have.property('port'); + expect(ha).to.have.property('port', undefined); }); it('should only set the socketPath property on HostAddress when hostString ends in .sock', () => { @@ -627,8 +627,8 @@ describe('driver utils', function () { const hostnameThatEndsWithSock = 'iLoveJavascript.sock'; const ha = new HostAddress(hostnameThatEndsWithSock); expect(ha).to.have.property('socketPath', hostnameThatEndsWithSock); - expect(ha).to.not.have.property('port'); - expect(ha).to.not.have.property('host'); + expect(ha).to.have.property('port', undefined); + expect(ha).to.have.property('host', undefined); }); it('should set the host and port property on HostAddress even when hostname ends in .sock if there is a port number specified', () => { @@ -636,7 +636,7 @@ describe('driver utils', function () { // the port number at the end of the hostname (even if it is the default) const hostnameThatEndsWithSockHasPort = 'iLoveJavascript.sock:27017'; const ha = new HostAddress(hostnameThatEndsWithSockHasPort); - expect(ha).to.not.have.property('socketPath'); + expect(ha).to.have.property('socketPath', undefined); expect(ha).to.have.property('host', 'iLoveJavascript.sock'.toLowerCase()); expect(ha).to.have.property('port', 27017); });