@@ -5,23 +5,24 @@ import {
5
5
WAITING_FOR_SUITABLE_SERVER
6
6
} from '../constants' ;
7
7
import { type ReadPreference } from '../read_preference' ;
8
+ import { HostAddress } from '../utils' ;
8
9
import { type ServerSelector } from './server_selection' ;
9
10
import type { TopologyDescription } from './topology_description' ;
10
11
11
12
/**
12
- * The base export class for all monitoring events published from server selection
13
- * @public
14
- * @category Event
13
+ * The base export class for all logs published from server selection
14
+ * @internal
15
+ * @category Log Type
15
16
*/
16
17
export abstract class ServerSelectionEvent {
17
18
/** String representation of the selector being used to select the server.
18
19
* Defaults to 'custom selector' for application-provided custom selector case.
19
20
*/
20
- selector : string ;
21
+ selector : string | ReadPreference | ServerSelector ;
21
22
/** The name of the operation for which a server is being selected. */
22
23
operation : string ;
23
- /** String representation of the current topology description. */
24
- topologyDescription : string ;
24
+ /** The current topology description. */
25
+ topologyDescription : TopologyDescription ;
25
26
26
27
/** @internal */
27
28
abstract name :
@@ -30,26 +31,23 @@ export abstract class ServerSelectionEvent {
30
31
| typeof SERVER_SELECTION_FAILED
31
32
| typeof WAITING_FOR_SUITABLE_SERVER ;
32
33
34
+ abstract message : string ;
35
+
33
36
/** @internal */
34
37
constructor (
35
38
selector : string | ReadPreference | ServerSelector ,
36
- operation : string | undefined ,
37
- topologyDescription : TopologyDescription
39
+ topologyDescription : TopologyDescription ,
40
+ operation ?: string
38
41
) {
39
- this . selector =
40
- typeof selector === 'string'
41
- ? selector
42
- : typeof selector === 'function'
43
- ? 'custom selector'
44
- : JSON . stringify ( selector . toJSON ( ) , null , 2 ) ;
45
- this . operation = operation ?? 'custom operation' ;
46
- this . topologyDescription = topologyDescription . toString ( ) ;
42
+ this . selector = selector ;
43
+ this . operation = operation ?? 'n/a' ;
44
+ this . topologyDescription = topologyDescription ;
47
45
}
48
46
}
49
47
50
48
/**
51
49
* An event published when server selection starts
52
- * @public
50
+ * @internal
53
51
* @category Event
54
52
*/
55
53
export class ServerSelectionStartedEvent extends ServerSelectionEvent {
@@ -60,43 +58,43 @@ export class ServerSelectionStartedEvent extends ServerSelectionEvent {
60
58
/** @internal */
61
59
constructor (
62
60
selector : string | ReadPreference | ServerSelector ,
63
- operation : string | undefined ,
64
- topologyDescription : TopologyDescription
61
+ topologyDescription : TopologyDescription ,
62
+ operation ?: string
65
63
) {
66
- super ( selector , operation , topologyDescription ) ;
64
+ super ( selector , topologyDescription , operation ) ;
67
65
}
68
66
}
69
67
70
68
/**
71
69
* An event published when a server selection fails
72
- * @public
70
+ * @internal
73
71
* @category Event
74
72
*/
75
73
export class ServerSelectionFailedEvent extends ServerSelectionEvent {
76
74
/** @internal */
77
75
name = SERVER_SELECTION_FAILED ;
78
76
message = 'Server selection failed' ;
79
77
/** Representation of the error the driver will throw regarding server selection failing. */
80
- failure : string ;
78
+ failure : Error ;
81
79
82
80
/** @internal */
83
81
constructor (
84
82
selector : string | ReadPreference | ServerSelector ,
85
- operation : string | undefined ,
86
83
topologyDescription : TopologyDescription ,
87
- errMsg : string
84
+ error : Error ,
85
+ operation ?: string
88
86
) {
89
- super ( selector , operation , topologyDescription ) ;
90
- this . failure = errMsg ;
87
+ super ( selector , topologyDescription , operation ) ;
88
+ this . failure = error ;
91
89
}
92
90
}
93
91
94
92
/**
95
93
* An event published when server selection succeeds
96
- * @public
94
+ * @internal
97
95
* @category Event
98
96
*/
99
- export class ServerSelectionSuccessEvent extends ServerSelectionEvent {
97
+ export class ServerSelectionSucceededEvent extends ServerSelectionEvent {
100
98
/** @internal */
101
99
name = SERVER_SELECTION_SUCCEEDED ;
102
100
message = 'Server selection succeeded' ;
@@ -110,39 +108,37 @@ export class ServerSelectionSuccessEvent extends ServerSelectionEvent {
110
108
/** @internal */
111
109
constructor (
112
110
selector : string | ReadPreference | ServerSelector ,
113
- operation : string | undefined ,
114
111
topologyDescription : TopologyDescription ,
115
- serverHost : string ,
116
- serverPort : number
112
+ address : string ,
113
+ operation ?: string
117
114
) {
118
- super ( selector , operation , topologyDescription ) ;
119
- this . serverHost = serverHost ;
120
- this . serverPort = serverPort ;
115
+ super ( selector , topologyDescription , operation ) ;
116
+ const { host, port } = HostAddress . fromString ( address ) . toHostPort ( ) ;
117
+ this . serverHost = host ;
118
+ this . serverPort = port ;
121
119
}
122
120
}
123
121
124
122
/**
125
123
* An event published when server selection is waiting for a suitable server to become available
126
- * @public
124
+ * @internal
127
125
* @category Event
128
126
*/
129
127
export class WaitingForSuitableServerEvent extends ServerSelectionEvent {
130
128
/** @internal */
131
129
name = WAITING_FOR_SUITABLE_SERVER ;
132
130
message = 'Waiting for suitable server to become available' ;
133
131
/** The remaining time left until server selection will time out. */
134
- remainingTimeMS ? : number ;
132
+ remainingTimeMS : number ;
135
133
136
134
/** @internal */
137
135
constructor (
138
136
selector : string | ReadPreference | ServerSelector ,
139
- operation : string | undefined ,
140
137
topologyDescription : TopologyDescription ,
141
- remainingTimeMS : number | undefined
138
+ remainingTimeMS : number ,
139
+ operation ?: string
142
140
) {
143
- super ( selector , operation , topologyDescription ) ;
144
- if ( remainingTimeMS ) {
145
- this . remainingTimeMS = remainingTimeMS ;
146
- }
141
+ super ( selector , topologyDescription , operation ) ;
142
+ this . remainingTimeMS = remainingTimeMS ;
147
143
}
148
144
}
0 commit comments