Skip to content

Commit 7874157

Browse files
committed
prefer undefined properties over deletion
1 parent 44aa80d commit 7874157

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

src/utils.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,11 +1142,9 @@ export class BufferPool {
11421142

11431143
/** @public */
11441144
export class HostAddress {
1145-
host: string | undefined;
1146-
port: number | undefined;
1147-
// Driver only works with unix socket path to connect
1148-
// SDAM operates only on tcp addresses
1149-
socketPath: string | undefined;
1145+
host: string | undefined = undefined;
1146+
port: number | undefined = undefined;
1147+
socketPath: string | undefined = undefined;
11501148
isIPv6 = false;
11511149

11521150
constructor(hostString: string) {
@@ -1155,8 +1153,6 @@ export class HostAddress {
11551153
if (escapedHost.endsWith('.sock')) {
11561154
// heuristically determine if we're working with a domain socket
11571155
this.socketPath = decodeURIComponent(escapedHost);
1158-
delete this.port;
1159-
delete this.host;
11601156
return;
11611157
}
11621158

test/unit/connection_string.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,9 @@ describe('Connection String', function () {
595595
it('should allow multiple bracketed portless localhost IPv6 addresses', () => {
596596
const client = new MongoClient('mongodb://[::1],[::1],[::1]/test');
597597
expect(client.options.hosts).to.deep.equal([
598-
{ host: '::1', port: 27017, isIPv6: true },
599-
{ host: '::1', port: 27017, isIPv6: true },
600-
{ host: '::1', port: 27017, isIPv6: true }
598+
{ host: '::1', port: 27017, isIPv6: true, socketPath: undefined },
599+
{ host: '::1', port: 27017, isIPv6: true, socketPath: undefined },
600+
{ host: '::1', port: 27017, isIPv6: true, socketPath: undefined }
601601
]);
602602
});
603603

@@ -606,18 +606,18 @@ describe('Connection String', function () {
606606
'mongodb://[ABCD:f::abcd:abcd:abcd:abcd],[ABCD:f::abcd:abcd:abcd:abcd],[ABCD:f::abcd:abcd:abcd:abcd]/test'
607607
);
608608
expect(client.options.hosts).to.deep.equal([
609-
{ host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true },
610-
{ host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true },
611-
{ host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true }
609+
{ host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true, socketPath: undefined },
610+
{ host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true, socketPath: undefined },
611+
{ host: 'abcd:f::abcd:abcd:abcd:abcd', port: 27017, isIPv6: true, socketPath: undefined }
612612
]);
613613
});
614614

615615
it('should allow multiple bracketed port-full IPv6 addresses', () => {
616616
const client = new MongoClient('mongodb://[::1]:27018,[::1]:27019,[::1]:27020/test');
617617
expect(client.options.hosts).to.deep.equal([
618-
{ host: '::1', port: 27018, isIPv6: true },
619-
{ host: '::1', port: 27019, isIPv6: true },
620-
{ host: '::1', port: 27020, isIPv6: true }
618+
{ host: '::1', port: 27018, isIPv6: true, socketPath: undefined },
619+
{ host: '::1', port: 27019, isIPv6: true, socketPath: undefined },
620+
{ host: '::1', port: 27020, isIPv6: true, socketPath: undefined }
621621
]);
622622
});
623623
});

test/unit/utils.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -598,27 +598,27 @@ describe('driver utils', function () {
598598
it('should handle decoded unix socket path', () => {
599599
const ha = new HostAddress(socketPath);
600600
expect(ha).to.have.property('socketPath', socketPath);
601-
expect(ha).to.not.have.property('port');
601+
expect(ha).to.have.property('port', undefined);
602602
});
603603

604604
it('should handle encoded unix socket path', () => {
605605
const ha = new HostAddress(encodeURIComponent(socketPath));
606606
expect(ha).to.have.property('socketPath', socketPath);
607-
expect(ha).to.not.have.property('port');
607+
expect(ha).to.have.property('port', undefined);
608608
});
609609

610610
it('should handle encoded unix socket path with an unencoded space', () => {
611611
const socketPathWithSpaces = '/tmp/some directory/mongodb-27017.sock';
612612
const ha = new HostAddress(socketPathWithSpaces);
613613
expect(ha).to.have.property('socketPath', socketPathWithSpaces);
614-
expect(ha).to.not.have.property('port');
614+
expect(ha).to.have.property('port', undefined);
615615
});
616616

617617
it('should handle unix socket path that does not begin with a slash', () => {
618618
const socketPathWithoutSlash = 'my_local/directory/mustEndWith.sock';
619619
const ha = new HostAddress(socketPathWithoutSlash);
620620
expect(ha).to.have.property('socketPath', socketPathWithoutSlash);
621-
expect(ha).to.not.have.property('port');
621+
expect(ha).to.have.property('port', undefined);
622622
});
623623

624624
it('should only set the socketPath property on HostAddress when hostString ends in .sock', () => {
@@ -627,16 +627,16 @@ describe('driver utils', function () {
627627
const hostnameThatEndsWithSock = 'iLoveJavascript.sock';
628628
const ha = new HostAddress(hostnameThatEndsWithSock);
629629
expect(ha).to.have.property('socketPath', hostnameThatEndsWithSock);
630-
expect(ha).to.not.have.property('port');
631-
expect(ha).to.not.have.property('host');
630+
expect(ha).to.have.property('port', undefined);
631+
expect(ha).to.have.property('host', undefined);
632632
});
633633

634634
it('should set the host and port property on HostAddress even when hostname ends in .sock if there is a port number specified', () => {
635635
// "should determine unix socket usage based on .sock ending" can be worked around by putting
636636
// the port number at the end of the hostname (even if it is the default)
637637
const hostnameThatEndsWithSockHasPort = 'iLoveJavascript.sock:27017';
638638
const ha = new HostAddress(hostnameThatEndsWithSockHasPort);
639-
expect(ha).to.not.have.property('socketPath');
639+
expect(ha).to.have.property('socketPath', undefined);
640640
expect(ha).to.have.property('host', 'iLoveJavascript.sock'.toLowerCase());
641641
expect(ha).to.have.property('port', 27017);
642642
});

0 commit comments

Comments
 (0)