Skip to content

Commit

Permalink
prefer undefined properties over deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Sep 14, 2022
1 parent 44aa80d commit 7874157
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
10 changes: 3 additions & 7 deletions src/utils.ts
Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down
18 changes: 9 additions & 9 deletions test/unit/connection_string.test.ts
Expand Up @@ -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 }
]);
});

Expand All @@ -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 }
]);
});
});
Expand Down
14 changes: 7 additions & 7 deletions test/unit/utils.test.ts
Expand Up @@ -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', () => {
Expand All @@ -627,16 +627,16 @@ 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', () => {
// "should determine unix socket usage based on .sock ending" can be worked around by putting
// 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);
});
Expand Down

0 comments on commit 7874157

Please sign in to comment.