11import { Document , Long , ObjectId } from '../bson' ;
22import type { MongoError } from '../error' ;
33import { arrayStrictEqual , errorStrictEqual , HostAddress , now } from '../utils' ;
4- import type { ClusterTime } from './common' ;
5- import { ServerType } from './common' ;
4+ import { ClusterTime , ServerType } from './common' ;
65
76const WRITABLE_SERVER_TYPES = new Set < ServerType > ( [
87 ServerType . RSPrimary ,
@@ -50,31 +49,27 @@ export interface ServerDescriptionOptions {
5049 * @public
5150 */
5251export class ServerDescription {
53- private _hostAddress : HostAddress ;
54- address : string ;
55- type : ServerType ;
56- hosts : string [ ] ;
57- passives : string [ ] ;
58- arbiters : string [ ] ;
59- tags : TagSet ;
60-
61- error ?: MongoError ;
62- topologyVersion ?: TopologyVersion ;
63- minWireVersion : number ;
64- maxWireVersion : number ;
65- roundTripTime : number ;
66- lastUpdateTime : number ;
67- lastWriteDate : number ;
68-
69- me ?: string ;
70- primary ?: string ;
71- setName ?: string ;
72- setVersion ?: number ;
73- electionId ?: ObjectId ;
74- logicalSessionTimeoutMinutes ?: number ;
75-
76- // NOTE: does this belong here? It seems we should gossip the cluster time at the CMAP level
77- $clusterTime ?: ClusterTime ;
52+ private readonly _hostAddress : HostAddress ;
53+ readonly address : string ;
54+ readonly type : ServerType ;
55+ readonly hosts : string [ ] ;
56+ readonly passives : string [ ] ;
57+ readonly arbiters : string [ ] ;
58+ readonly tags : TagSet ;
59+ readonly error : MongoError | undefined ;
60+ readonly topologyVersion : TopologyVersion | undefined ;
61+ readonly minWireVersion : number ;
62+ readonly maxWireVersion : number ;
63+ readonly roundTripTime : number ;
64+ readonly lastUpdateTime : number ;
65+ readonly lastWriteDate : number ;
66+ readonly me : string | undefined ;
67+ readonly primary : string | undefined ;
68+ readonly setName : string | undefined ;
69+ readonly setVersion : number | undefined ;
70+ readonly electionId : ObjectId | undefined ;
71+ readonly logicalSessionTimeoutMinutes : number | undefined ;
72+ readonly $clusterTime : ClusterTime | undefined ;
7873
7974 /**
8075 * Create a ServerDescription
@@ -83,7 +78,14 @@ export class ServerDescription {
8378 * @param address - The address of the server
8479 * @param hello - An optional hello response for this server
8580 */
86- constructor ( address : HostAddress | string , hello ?: Document , options ?: ServerDescriptionOptions ) {
81+ constructor (
82+ address : HostAddress | string ,
83+ hello : Document = { } ,
84+ options : ServerDescriptionOptions = { }
85+ ) {
86+ hello ??= { } ;
87+ options ??= { } ;
88+
8789 if ( typeof address === 'string' ) {
8890 this . _hostAddress = new HostAddress ( address ) ;
8991 this . address = this . _hostAddress . toString ( ) ;
@@ -102,43 +104,23 @@ export class ServerDescription {
102104 this . lastUpdateTime = now ( ) ;
103105 this . lastWriteDate = hello ?. lastWrite ?. lastWriteDate ?? 0 ;
104106
107+ this . topologyVersion = undefined ;
105108 if ( options ?. topologyVersion ) {
106109 this . topologyVersion = options . topologyVersion ;
107110 } else if ( hello ?. topologyVersion ) {
108111 this . topologyVersion = hello . topologyVersion ;
109112 }
110113
111- if ( options ?. error ) {
112- this . error = options . error ;
113- }
114-
115- if ( hello ?. primary ) {
116- this . primary = hello . primary ;
117- }
118-
119- if ( hello ?. me ) {
120- this . me = hello . me . toLowerCase ( ) ;
121- }
122-
123- if ( hello ?. setName ) {
124- this . setName = hello . setName ;
125- }
114+ this . error = options . error ;
115+ this . primary = hello . primary ;
116+ this . me = hello . me ?. toLowerCase ( ) ;
117+ this . setName = hello . setName ;
118+ this . setVersion = hello . setVersion ;
119+ this . electionId = hello . electionId ;
120+ this . logicalSessionTimeoutMinutes = hello . logicalSessionTimeoutMinutes ;
121+ this . $clusterTime = hello . $clusterTime ;
126122
127- if ( hello ?. setVersion ) {
128- this . setVersion = hello . setVersion ;
129- }
130-
131- if ( hello ?. electionId ) {
132- this . electionId = hello . electionId ;
133- }
134-
135- if ( hello ?. logicalSessionTimeoutMinutes ) {
136- this . logicalSessionTimeoutMinutes = hello . logicalSessionTimeoutMinutes ;
137- }
138-
139- if ( hello ?. $clusterTime ) {
140- this . $clusterTime = hello . $clusterTime ;
141- }
123+ // Object.freeze(this); - Mock server and tests edit serverDescriptions...
142124 }
143125
144126 get hostAddress ( ) : HostAddress {
0 commit comments