mongodb 4.1.4 → 4.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. package/README.md +16 -15
  2. package/lib/admin.js +3 -3
  3. package/lib/admin.js.map +1 -1
  4. package/lib/bson.js +16 -14
  5. package/lib/bson.js.map +1 -1
  6. package/lib/bulk/common.js +3 -4
  7. package/lib/bulk/common.js.map +1 -1
  8. package/lib/bulk/ordered.js +1 -1
  9. package/lib/bulk/ordered.js.map +1 -1
  10. package/lib/bulk/unordered.js +1 -1
  11. package/lib/bulk/unordered.js.map +1 -1
  12. package/lib/change_stream.js +6 -7
  13. package/lib/change_stream.js.map +1 -1
  14. package/lib/cmap/auth/auth_provider.js.map +1 -1
  15. package/lib/cmap/auth/gssapi.js +3 -3
  16. package/lib/cmap/auth/gssapi.js.map +1 -1
  17. package/lib/cmap/auth/mongo_credentials.js +16 -18
  18. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  19. package/lib/cmap/auth/mongocr.js +2 -2
  20. package/lib/cmap/auth/mongocr.js.map +1 -1
  21. package/lib/cmap/auth/mongodb_aws.js +6 -6
  22. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  23. package/lib/cmap/auth/plain.js +1 -1
  24. package/lib/cmap/auth/plain.js.map +1 -1
  25. package/lib/cmap/auth/providers.js +21 -0
  26. package/lib/cmap/auth/providers.js.map +1 -0
  27. package/lib/cmap/auth/scram.js +4 -4
  28. package/lib/cmap/auth/scram.js.map +1 -1
  29. package/lib/cmap/auth/x509.js +1 -1
  30. package/lib/cmap/auth/x509.js.map +1 -1
  31. package/lib/cmap/command_monitoring_events.js +1 -1
  32. package/lib/cmap/command_monitoring_events.js.map +1 -1
  33. package/lib/cmap/commands.js +20 -11
  34. package/lib/cmap/commands.js.map +1 -1
  35. package/lib/cmap/connect.js +102 -15
  36. package/lib/cmap/connect.js.map +1 -1
  37. package/lib/cmap/connection.js +27 -26
  38. package/lib/cmap/connection.js.map +1 -1
  39. package/lib/cmap/connection_pool.js +19 -30
  40. package/lib/cmap/connection_pool.js.map +1 -1
  41. package/lib/cmap/message_stream.js +3 -3
  42. package/lib/cmap/message_stream.js.map +1 -1
  43. package/lib/cmap/stream_description.js +4 -4
  44. package/lib/cmap/stream_description.js.map +1 -1
  45. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  46. package/lib/cmap/wire_protocol/constants.js +4 -4
  47. package/lib/cmap/wire_protocol/shared.js +4 -3
  48. package/lib/cmap/wire_protocol/shared.js.map +1 -1
  49. package/lib/collection.js +46 -47
  50. package/lib/collection.js.map +1 -1
  51. package/lib/connection_string.js +106 -49
  52. package/lib/connection_string.js.map +1 -1
  53. package/lib/constants.js +108 -1
  54. package/lib/constants.js.map +1 -1
  55. package/lib/cursor/abstract_cursor.js +10 -7
  56. package/lib/cursor/abstract_cursor.js.map +1 -1
  57. package/lib/cursor/aggregation_cursor.js +1 -1
  58. package/lib/cursor/aggregation_cursor.js.map +1 -1
  59. package/lib/cursor/find_cursor.js +1 -1
  60. package/lib/cursor/find_cursor.js.map +1 -1
  61. package/lib/db.js +23 -13
  62. package/lib/db.js.map +1 -1
  63. package/lib/deps.js +0 -1
  64. package/lib/deps.js.map +1 -1
  65. package/lib/encrypter.js +12 -4
  66. package/lib/encrypter.js.map +1 -1
  67. package/lib/error.js +47 -24
  68. package/lib/error.js.map +1 -1
  69. package/lib/gridfs/download.js +1 -1
  70. package/lib/gridfs/download.js.map +1 -1
  71. package/lib/gridfs/index.js +3 -3
  72. package/lib/gridfs/index.js.map +1 -1
  73. package/lib/gridfs/upload.js +3 -2
  74. package/lib/gridfs/upload.js.map +1 -1
  75. package/lib/index.js +81 -81
  76. package/lib/index.js.map +1 -1
  77. package/lib/logger.js +1 -1
  78. package/lib/logger.js.map +1 -1
  79. package/lib/mongo_client.js +5 -5
  80. package/lib/mongo_client.js.map +1 -1
  81. package/lib/mongo_types.js.map +1 -1
  82. package/lib/operations/add_user.js +2 -2
  83. package/lib/operations/add_user.js.map +1 -1
  84. package/lib/operations/aggregate.js +2 -3
  85. package/lib/operations/aggregate.js.map +1 -1
  86. package/lib/operations/bulk_write.js.map +1 -1
  87. package/lib/operations/collections.js +1 -1
  88. package/lib/operations/collections.js.map +1 -1
  89. package/lib/operations/command.js +9 -6
  90. package/lib/operations/command.js.map +1 -1
  91. package/lib/operations/common_functions.js.map +1 -1
  92. package/lib/operations/connect.js +4 -13
  93. package/lib/operations/connect.js.map +1 -1
  94. package/lib/operations/count.js +1 -1
  95. package/lib/operations/count.js.map +1 -1
  96. package/lib/operations/count_documents.js.map +1 -1
  97. package/lib/operations/create_collection.js +3 -2
  98. package/lib/operations/create_collection.js.map +1 -1
  99. package/lib/operations/delete.js +3 -3
  100. package/lib/operations/delete.js.map +1 -1
  101. package/lib/operations/distinct.js +3 -3
  102. package/lib/operations/distinct.js.map +1 -1
  103. package/lib/operations/drop.js +1 -1
  104. package/lib/operations/drop.js.map +1 -1
  105. package/lib/operations/estimated_document_count.js +2 -2
  106. package/lib/operations/estimated_document_count.js.map +1 -1
  107. package/lib/operations/eval.js +2 -2
  108. package/lib/operations/eval.js.map +1 -1
  109. package/lib/operations/execute_operation.js +23 -7
  110. package/lib/operations/execute_operation.js.map +1 -1
  111. package/lib/operations/find.js +5 -5
  112. package/lib/operations/find.js.map +1 -1
  113. package/lib/operations/find_and_modify.js +2 -2
  114. package/lib/operations/find_and_modify.js.map +1 -1
  115. package/lib/operations/get_more.js +28 -0
  116. package/lib/operations/get_more.js.map +1 -0
  117. package/lib/operations/indexes.js +4 -4
  118. package/lib/operations/indexes.js.map +1 -1
  119. package/lib/operations/insert.js +3 -3
  120. package/lib/operations/insert.js.map +1 -1
  121. package/lib/operations/is_capped.js +1 -1
  122. package/lib/operations/is_capped.js.map +1 -1
  123. package/lib/operations/list_collections.js +11 -6
  124. package/lib/operations/list_collections.js.map +1 -1
  125. package/lib/operations/list_databases.js +1 -1
  126. package/lib/operations/list_databases.js.map +1 -1
  127. package/lib/operations/map_reduce.js +4 -3
  128. package/lib/operations/map_reduce.js.map +1 -1
  129. package/lib/operations/operation.js +4 -2
  130. package/lib/operations/operation.js.map +1 -1
  131. package/lib/operations/options_operation.js +1 -1
  132. package/lib/operations/options_operation.js.map +1 -1
  133. package/lib/operations/profiling_level.js +1 -1
  134. package/lib/operations/profiling_level.js.map +1 -1
  135. package/lib/operations/remove_user.js +1 -1
  136. package/lib/operations/remove_user.js.map +1 -1
  137. package/lib/operations/rename.js +3 -3
  138. package/lib/operations/rename.js.map +1 -1
  139. package/lib/operations/run_command.js +1 -1
  140. package/lib/operations/run_command.js.map +1 -1
  141. package/lib/operations/set_profiling_level.js +2 -2
  142. package/lib/operations/set_profiling_level.js.map +1 -1
  143. package/lib/operations/stats.js +1 -1
  144. package/lib/operations/stats.js.map +1 -1
  145. package/lib/operations/update.js +2 -2
  146. package/lib/operations/update.js.map +1 -1
  147. package/lib/operations/validate_collection.js +1 -1
  148. package/lib/operations/validate_collection.js.map +1 -1
  149. package/lib/read_preference.js +15 -5
  150. package/lib/read_preference.js.map +1 -1
  151. package/lib/sdam/monitor.js +4 -4
  152. package/lib/sdam/monitor.js.map +1 -1
  153. package/lib/sdam/server.js +31 -31
  154. package/lib/sdam/server.js.map +1 -1
  155. package/lib/sdam/server_description.js +1 -1
  156. package/lib/sdam/server_description.js.map +1 -1
  157. package/lib/sdam/server_selection.js +38 -3
  158. package/lib/sdam/server_selection.js.map +1 -1
  159. package/lib/sdam/srv_polling.js +11 -11
  160. package/lib/sdam/srv_polling.js.map +1 -1
  161. package/lib/sdam/topology.js +40 -62
  162. package/lib/sdam/topology.js.map +1 -1
  163. package/lib/sdam/topology_description.js +36 -14
  164. package/lib/sdam/topology_description.js.map +1 -1
  165. package/lib/sessions.js +13 -28
  166. package/lib/sessions.js.map +1 -1
  167. package/lib/transactions.js +1 -1
  168. package/lib/transactions.js.map +1 -1
  169. package/lib/utils.js +90 -23
  170. package/lib/utils.js.map +1 -1
  171. package/mongodb.d.ts +159 -65
  172. package/mongodb.ts34.d.ts +184 -65
  173. package/package.json +26 -24
  174. package/src/admin.ts +9 -9
  175. package/src/bson.ts +26 -17
  176. package/src/bulk/common.ts +26 -20
  177. package/src/bulk/ordered.ts +4 -4
  178. package/src/bulk/unordered.ts +5 -5
  179. package/src/change_stream.ts +35 -32
  180. package/src/cmap/auth/auth_provider.ts +3 -3
  181. package/src/cmap/auth/gssapi.ts +8 -8
  182. package/src/cmap/auth/mongo_credentials.ts +2 -6
  183. package/src/cmap/auth/mongocr.ts +3 -2
  184. package/src/cmap/auth/mongodb_aws.ts +10 -11
  185. package/src/cmap/auth/plain.ts +1 -1
  186. package/src/cmap/auth/providers.ts +21 -0
  187. package/src/cmap/auth/scram.ts +9 -9
  188. package/src/cmap/auth/x509.ts +3 -3
  189. package/src/cmap/command_monitoring_events.ts +2 -2
  190. package/src/cmap/commands.ts +30 -19
  191. package/src/cmap/connect.ts +142 -22
  192. package/src/cmap/connection.ts +73 -52
  193. package/src/cmap/connection_pool.ts +39 -39
  194. package/src/cmap/connection_pool_events.ts +1 -1
  195. package/src/cmap/message_stream.ts +9 -8
  196. package/src/cmap/stream_description.ts +5 -5
  197. package/src/cmap/wire_protocol/compression.ts +2 -2
  198. package/src/cmap/wire_protocol/constants.ts +4 -4
  199. package/src/cmap/wire_protocol/shared.ts +10 -9
  200. package/src/collection.ts +133 -106
  201. package/src/connection_string.ts +141 -60
  202. package/src/constants.ts +116 -0
  203. package/src/cursor/abstract_cursor.ts +21 -22
  204. package/src/cursor/aggregation_cursor.ts +6 -6
  205. package/src/cursor/find_cursor.ts +2 -2
  206. package/src/db.ts +49 -39
  207. package/src/deps.ts +3 -1
  208. package/src/encrypter.ts +12 -3
  209. package/src/error.ts +52 -24
  210. package/src/gridfs/download.ts +10 -8
  211. package/src/gridfs/index.ts +11 -11
  212. package/src/gridfs/upload.ts +13 -10
  213. package/src/index.ts +204 -199
  214. package/src/logger.ts +2 -1
  215. package/src/mongo_client.ts +58 -32
  216. package/src/mongo_types.ts +98 -20
  217. package/src/operations/add_user.ts +6 -5
  218. package/src/operations/aggregate.ts +6 -7
  219. package/src/operations/bulk_write.ts +5 -5
  220. package/src/operations/collections.ts +2 -2
  221. package/src/operations/command.ts +19 -8
  222. package/src/operations/common_functions.ts +4 -4
  223. package/src/operations/connect.ts +4 -14
  224. package/src/operations/count.ts +4 -4
  225. package/src/operations/count_documents.ts +3 -3
  226. package/src/operations/create_collection.ts +8 -7
  227. package/src/operations/delete.ts +5 -5
  228. package/src/operations/distinct.ts +4 -4
  229. package/src/operations/drop.ts +4 -4
  230. package/src/operations/estimated_document_count.ts +5 -5
  231. package/src/operations/eval.ts +5 -5
  232. package/src/operations/execute_operation.ts +30 -11
  233. package/src/operations/find.ts +14 -14
  234. package/src/operations/find_and_modify.ts +7 -7
  235. package/src/operations/get_more.ts +49 -0
  236. package/src/operations/indexes.ts +16 -16
  237. package/src/operations/insert.ts +9 -9
  238. package/src/operations/is_capped.ts +3 -3
  239. package/src/operations/list_collections.ts +18 -10
  240. package/src/operations/list_databases.ts +4 -4
  241. package/src/operations/map_reduce.ts +10 -9
  242. package/src/operations/operation.ts +8 -4
  243. package/src/operations/options_operation.ts +3 -3
  244. package/src/operations/profiling_level.ts +4 -4
  245. package/src/operations/remove_user.ts +4 -4
  246. package/src/operations/rename.ts +6 -6
  247. package/src/operations/run_command.ts +3 -3
  248. package/src/operations/set_profiling_level.ts +6 -5
  249. package/src/operations/stats.ts +4 -4
  250. package/src/operations/update.ts +10 -10
  251. package/src/operations/validate_collection.ts +4 -4
  252. package/src/read_preference.ts +18 -7
  253. package/src/sdam/common.ts +2 -2
  254. package/src/sdam/events.ts +1 -1
  255. package/src/sdam/monitor.ts +13 -14
  256. package/src/sdam/server.ts +71 -61
  257. package/src/sdam/server_description.ts +4 -4
  258. package/src/sdam/server_selection.ts +46 -3
  259. package/src/sdam/srv_polling.ts +15 -13
  260. package/src/sdam/topology.ts +89 -90
  261. package/src/sdam/topology_description.ts +39 -15
  262. package/src/sessions.ts +31 -48
  263. package/src/transactions.ts +6 -6
  264. package/src/utils.ts +123 -43
  265. package/lib/cmap/auth/defaultAuthProviders.js +0 -30
  266. package/lib/cmap/auth/defaultAuthProviders.js.map +0 -1
  267. package/src/cmap/auth/defaultAuthProviders.ts +0 -32
@@ -1,87 +1,83 @@
1
1
  import Denque = require('denque');
2
- import { ReadPreference, ReadPreferenceLike } from '../read_preference';
3
- import { compareTopologyVersion, ServerDescription } from './server_description';
4
- import { TopologyDescription } from './topology_description';
5
- import { Server, ServerEvents, ServerOptions } from './server';
2
+ import type { BSONSerializeOptions, Document } from '../bson';
3
+ import { deserialize, serialize } from '../bson';
4
+ import type { MongoCredentials } from '../cmap/auth/mongo_credentials';
5
+ import type { ConnectionEvents, DestroyOptions } from '../cmap/connection';
6
+ import type { CloseOptions, ConnectionPoolEvents } from '../cmap/connection_pool';
7
+ import { DEFAULT_OPTIONS } from '../connection_string';
6
8
  import {
7
- ClientSession,
8
- ServerSessionPool,
9
- ServerSessionId,
10
- ClientSessionOptions
11
- } from '../sessions';
12
- import { SrvPoller, SrvPollingEvent } from './srv_polling';
13
- import { CMAP_EVENTS, ConnectionPoolEvents } from '../cmap/connection_pool';
9
+ CLOSE,
10
+ CONNECT,
11
+ ERROR,
12
+ LOCAL_SERVER_EVENTS,
13
+ OPEN,
14
+ SERVER_CLOSED,
15
+ SERVER_DESCRIPTION_CHANGED,
16
+ SERVER_OPENING,
17
+ SERVER_RELAY_EVENTS,
18
+ TIMEOUT,
19
+ TOPOLOGY_CLOSED,
20
+ TOPOLOGY_DESCRIPTION_CHANGED,
21
+ TOPOLOGY_OPENING
22
+ } from '../constants';
14
23
  import {
15
- MongoServerSelectionError,
16
24
  MongoCompatibilityError,
17
25
  MongoDriverError,
18
- MongoTopologyClosedError,
19
- MongoRuntimeError
26
+ MongoRuntimeError,
27
+ MongoServerSelectionError,
28
+ MongoTopologyClosedError
20
29
  } from '../error';
21
- import { readPreferenceServerSelector, ServerSelector } from './server_selection';
30
+ import type { MongoOptions, ServerApi } from '../mongo_client';
31
+ import { TypedEventEmitter } from '../mongo_types';
32
+ import { ReadPreference, ReadPreferenceLike } from '../read_preference';
33
+ import {
34
+ ClientSession,
35
+ ClientSessionOptions,
36
+ ServerSessionId,
37
+ ServerSessionPool
38
+ } from '../sessions';
39
+ import type { Transaction } from '../transactions';
22
40
  import {
23
- makeStateMachine,
24
- eachAsync,
25
- ClientMetadata,
26
41
  Callback,
42
+ ClientMetadata,
43
+ eachAsync,
44
+ emitWarning,
45
+ EventEmitterWithState,
27
46
  HostAddress,
47
+ makeStateMachine,
28
48
  ns,
29
- emitWarning,
30
- EventEmitterWithState
49
+ shuffle
31
50
  } from '../utils';
32
51
  import {
33
- TopologyType,
34
- ServerType,
35
- ClusterTime,
36
- TimerQueue,
37
52
  _advanceClusterTime,
38
- drainTimerQueue,
39
53
  clearAndRemoveTimerFrom,
54
+ ClusterTime,
55
+ drainTimerQueue,
56
+ ServerType,
40
57
  STATE_CLOSED,
41
58
  STATE_CLOSING,
59
+ STATE_CONNECTED,
42
60
  STATE_CONNECTING,
43
- STATE_CONNECTED
61
+ TimerQueue,
62
+ TopologyType
44
63
  } from './common';
45
64
  import {
46
- ServerOpeningEvent,
47
65
  ServerClosedEvent,
48
66
  ServerDescriptionChangedEvent,
49
- TopologyOpeningEvent,
67
+ ServerOpeningEvent,
50
68
  TopologyClosedEvent,
51
- TopologyDescriptionChangedEvent
69
+ TopologyDescriptionChangedEvent,
70
+ TopologyOpeningEvent
52
71
  } from './events';
53
- import type { Document, BSONSerializeOptions } from '../bson';
54
- import type { MongoCredentials } from '../cmap/auth/mongo_credentials';
55
- import type { Transaction } from '../transactions';
56
- import type { CloseOptions } from '../cmap/connection_pool';
57
- import { DestroyOptions, Connection, ConnectionEvents } from '../cmap/connection';
58
- import type { MongoOptions, ServerApi } from '../mongo_client';
59
- import { DEFAULT_OPTIONS } from '../connection_string';
60
- import { serialize, deserialize } from '../bson';
61
- import { TypedEventEmitter } from '../mongo_types';
72
+ import { Server, ServerEvents, ServerOptions } from './server';
73
+ import { compareTopologyVersion, ServerDescription } from './server_description';
74
+ import { readPreferenceServerSelector, ServerSelector } from './server_selection';
75
+ import { SrvPoller, SrvPollingEvent } from './srv_polling';
76
+ import { TopologyDescription } from './topology_description';
62
77
 
63
78
  // Global state
64
79
  let globalTopologyCounter = 0;
65
80
 
66
- // events that we relay to the `Topology`
67
- const SERVER_RELAY_EVENTS = [
68
- Server.SERVER_HEARTBEAT_STARTED,
69
- Server.SERVER_HEARTBEAT_SUCCEEDED,
70
- Server.SERVER_HEARTBEAT_FAILED,
71
- Connection.COMMAND_STARTED,
72
- Connection.COMMAND_SUCCEEDED,
73
- Connection.COMMAND_FAILED,
74
- ...CMAP_EVENTS
75
- ];
76
-
77
- // all events we listen to from `Server` instances
78
- const LOCAL_SERVER_EVENTS = [
79
- Server.CONNECT,
80
- Server.DESCRIPTION_RECEIVED,
81
- Server.CLOSED,
82
- Server.ENDED
83
- ];
84
-
85
81
  const stateTransition = makeStateMachine({
86
82
  [STATE_CLOSED]: [STATE_CLOSED, STATE_CONNECTING],
87
83
  [STATE_CONNECTING]: [STATE_CONNECTING, STATE_CLOSING, STATE_CONNECTED, STATE_CLOSED],
@@ -140,6 +136,8 @@ export interface TopologyPrivate {
140
136
 
141
137
  /** @public */
142
138
  export interface TopologyOptions extends BSONSerializeOptions, ServerOptions {
139
+ srvMaxHosts: number;
140
+ srvServiceName: string;
143
141
  hosts: HostAddress[];
144
142
  retryWrites: boolean;
145
143
  retryReads: boolean;
@@ -182,8 +180,8 @@ export type TopologyEvents = {
182
180
  topologyOpening(event: TopologyOpeningEvent): void;
183
181
  topologyDescriptionChanged(event: TopologyDescriptionChangedEvent): void;
184
182
  error(error: Error): void;
185
- /** TODO(NODE-3273) - remove error @internal */
186
- open(error: undefined, topology: Topology): void;
183
+ /** @internal */
184
+ open(topology: Topology): void;
187
185
  close(): void;
188
186
  timeout(): void;
189
187
  } & Omit<ServerEvents, 'connect'> &
@@ -205,27 +203,27 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
205
203
  _type?: string;
206
204
 
207
205
  /** @event */
208
- static readonly SERVER_OPENING = 'serverOpening' as const;
206
+ static readonly SERVER_OPENING = SERVER_OPENING;
209
207
  /** @event */
210
- static readonly SERVER_CLOSED = 'serverClosed' as const;
208
+ static readonly SERVER_CLOSED = SERVER_CLOSED;
211
209
  /** @event */
212
- static readonly SERVER_DESCRIPTION_CHANGED = 'serverDescriptionChanged' as const;
210
+ static readonly SERVER_DESCRIPTION_CHANGED = SERVER_DESCRIPTION_CHANGED;
213
211
  /** @event */
214
- static readonly TOPOLOGY_OPENING = 'topologyOpening' as const;
212
+ static readonly TOPOLOGY_OPENING = TOPOLOGY_OPENING;
215
213
  /** @event */
216
- static readonly TOPOLOGY_CLOSED = 'topologyClosed' as const;
214
+ static readonly TOPOLOGY_CLOSED = TOPOLOGY_CLOSED;
217
215
  /** @event */
218
- static readonly TOPOLOGY_DESCRIPTION_CHANGED = 'topologyDescriptionChanged' as const;
216
+ static readonly TOPOLOGY_DESCRIPTION_CHANGED = TOPOLOGY_DESCRIPTION_CHANGED;
219
217
  /** @event */
220
- static readonly ERROR = 'error' as const;
218
+ static readonly ERROR = ERROR;
221
219
  /** @event */
222
- static readonly OPEN = 'open' as const;
220
+ static readonly OPEN = OPEN;
223
221
  /** @event */
224
- static readonly CONNECT = 'connect' as const;
222
+ static readonly CONNECT = CONNECT;
225
223
  /** @event */
226
- static readonly CLOSE = 'close' as const;
224
+ static readonly CLOSE = CLOSE;
227
225
  /** @event */
228
- static readonly TIMEOUT = 'timeout' as const;
226
+ static readonly TIMEOUT = TIMEOUT;
229
227
 
230
228
  /**
231
229
  * @internal
@@ -290,8 +288,15 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
290
288
  const topologyType = topologyTypeFromOptions(options);
291
289
  const topologyId = globalTopologyCounter++;
292
290
 
291
+ const selectedHosts =
292
+ options.srvMaxHosts == null ||
293
+ options.srvMaxHosts === 0 ||
294
+ options.srvMaxHosts >= seedlist.length
295
+ ? seedlist
296
+ : shuffle(seedlist, options.srvMaxHosts);
297
+
293
298
  const serverDescriptions = new Map();
294
- for (const hostAddress of seedlist) {
299
+ for (const hostAddress of selectedHosts) {
295
300
  serverDescriptions.set(hostAddress.toString(), new ServerDescription(hostAddress));
296
301
  }
297
302
 
@@ -339,7 +344,9 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
339
344
  options.srvPoller ??
340
345
  new SrvPoller({
341
346
  heartbeatFrequencyMS: this.s.heartbeatFrequencyMS,
342
- srvHost: options.srvHost
347
+ srvHost: options.srvHost,
348
+ srvMaxHosts: options.srvMaxHosts,
349
+ srvServiceName: options.srvServiceName
343
350
  });
344
351
 
345
352
  this.on(Topology.TOPOLOGY_DESCRIPTION_CHANGED, this.s.detectShardedTopology);
@@ -363,7 +370,10 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
363
370
 
364
371
  private detectSrvRecords(ev: SrvPollingEvent) {
365
372
  const previousTopologyDescription = this.s.description;
366
- this.s.description = this.s.description.updateFromSrvPollingEvent(ev);
373
+ this.s.description = this.s.description.updateFromSrvPollingEvent(
374
+ ev,
375
+ this.s.options.srvMaxHosts
376
+ );
367
377
  if (this.s.description === previousTopologyDescription) {
368
378
  // Nothing changed, so return
369
379
  return;
@@ -456,8 +466,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
456
466
  }
457
467
 
458
468
  stateTransition(this, STATE_CONNECTED);
459
- // TODO(NODE-3273) - remove err
460
- this.emit(Topology.OPEN, err, this);
469
+ this.emit(Topology.OPEN, this);
461
470
  this.emit(Topology.CONNECT, this);
462
471
 
463
472
  if (typeof callback === 'function') callback(undefined, this);
@@ -467,8 +476,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
467
476
  }
468
477
 
469
478
  stateTransition(this, STATE_CONNECTED);
470
- // TODO(NODE-3273) - remove err
471
- this.emit(Topology.OPEN, err, this);
479
+ this.emit(Topology.OPEN, this);
472
480
  this.emit(Topology.CONNECT, this);
473
481
 
474
482
  if (typeof callback === 'function') callback(undefined, this);
@@ -797,6 +805,10 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
797
805
  return result;
798
806
  }
799
807
 
808
+ get commonWireVersion(): number | undefined {
809
+ return this.description.commonWireVersion;
810
+ }
811
+
800
812
  get logicalSessionTimeoutMinutes(): number | undefined {
801
813
  return this.description.logicalSessionTimeoutMinutes;
802
814
  }
@@ -810,19 +822,6 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
810
822
  }
811
823
  }
812
824
 
813
- /** @public */
814
- export const TOPOLOGY_EVENTS = [
815
- Topology.SERVER_OPENING,
816
- Topology.SERVER_CLOSED,
817
- Topology.SERVER_DESCRIPTION_CHANGED,
818
- Topology.TOPOLOGY_OPENING,
819
- Topology.TOPOLOGY_CLOSED,
820
- Topology.TOPOLOGY_DESCRIPTION_CHANGED,
821
- Topology.ERROR,
822
- Topology.TIMEOUT,
823
- Topology.CLOSE
824
- ];
825
-
826
825
  /** Destroys a server, and removes all event listeners from the instance */
827
826
  function destroyServer(
828
827
  server: Server,
@@ -1,9 +1,10 @@
1
- import { ServerDescription } from './server_description';
1
+ import type { Document, ObjectId } from '../bson';
2
2
  import * as WIRE_CONSTANTS from '../cmap/wire_protocol/constants';
3
- import { TopologyType, ServerType } from './common';
4
- import type { ObjectId, Document } from '../bson';
5
- import type { SrvPollingEvent } from './srv_polling';
6
3
  import { MongoError, MongoRuntimeError } from '../error';
4
+ import { shuffle } from '../utils';
5
+ import { ServerType, TopologyType } from './common';
6
+ import { ServerDescription } from './server_description';
7
+ import type { SrvPollingEvent } from './srv_polling';
7
8
 
8
9
  // constants related to compatibility checks
9
10
  const MIN_SUPPORTED_SERVER_VERSION = WIRE_CONSTANTS.MIN_SUPPORTED_SERVER_VERSION;
@@ -139,23 +140,46 @@ export class TopologyDescription {
139
140
  * Returns a new TopologyDescription based on the SrvPollingEvent
140
141
  * @internal
141
142
  */
142
- updateFromSrvPollingEvent(ev: SrvPollingEvent): TopologyDescription {
143
- const newAddresses = ev.addresses();
144
- const serverDescriptions = new Map(this.servers);
145
- for (const address of this.servers.keys()) {
146
- if (newAddresses.has(address)) {
147
- newAddresses.delete(address);
148
- } else {
149
- serverDescriptions.delete(address);
143
+ updateFromSrvPollingEvent(ev: SrvPollingEvent, srvMaxHosts = 0): TopologyDescription {
144
+ /** The SRV addresses defines the set of addresses we should be using */
145
+ const incomingHostnames = ev.hostnames();
146
+ const currentHostnames = new Set(this.servers.keys());
147
+
148
+ const hostnamesToAdd = new Set<string>(incomingHostnames);
149
+ const hostnamesToRemove = new Set<string>();
150
+ for (const hostname of currentHostnames) {
151
+ // filter hostnamesToAdd (made from incomingHostnames) down to what is *not* present in currentHostnames
152
+ hostnamesToAdd.delete(hostname);
153
+ if (!incomingHostnames.has(hostname)) {
154
+ // If the SRV Records no longer include this hostname
155
+ // we have to stop using it
156
+ hostnamesToRemove.add(hostname);
150
157
  }
151
158
  }
152
159
 
153
- if (serverDescriptions.size === this.servers.size && newAddresses.size === 0) {
160
+ if (hostnamesToAdd.size === 0 && hostnamesToRemove.size === 0) {
161
+ // No new hosts to add and none to remove
154
162
  return this;
155
163
  }
156
164
 
157
- for (const [address, host] of newAddresses) {
158
- serverDescriptions.set(address, new ServerDescription(host));
165
+ const serverDescriptions = new Map(this.servers);
166
+ for (const removedHost of hostnamesToRemove) {
167
+ serverDescriptions.delete(removedHost);
168
+ }
169
+
170
+ if (hostnamesToAdd.size > 0) {
171
+ if (srvMaxHosts === 0) {
172
+ // Add all!
173
+ for (const hostToAdd of hostnamesToAdd) {
174
+ serverDescriptions.set(hostToAdd, new ServerDescription(hostToAdd));
175
+ }
176
+ } else if (serverDescriptions.size < srvMaxHosts) {
177
+ // Add only the amount needed to get us back to srvMaxHosts
178
+ const selectedHosts = shuffle(hostnamesToAdd, srvMaxHosts - serverDescriptions.size);
179
+ for (const selectedHostToAdd of selectedHosts) {
180
+ serverDescriptions.set(selectedHostToAdd, new ServerDescription(selectedHostToAdd));
181
+ }
182
+ }
159
183
  }
160
184
 
161
185
  return new TopologyDescription(
package/src/sessions.ts CHANGED
@@ -1,45 +1,47 @@
1
- import { PromiseProvider } from './promise_provider';
2
- import { Binary, Long, Timestamp, Document } from './bson';
3
- import { ReadPreference } from './read_preference';
4
- import { isTransactionCommand, TxnState, Transaction, TransactionOptions } from './transactions';
5
- import { _advanceClusterTime, ClusterTime, TopologyType } from './sdam/common';
1
+ import { Binary, Document, Long, Timestamp } from './bson';
2
+ import type { CommandOptions, Connection } from './cmap/connection';
3
+ import { ConnectionPoolMetrics } from './cmap/metrics';
6
4
  import { isSharded } from './cmap/wire_protocol/shared';
5
+ import { PINNED, UNPINNED } from './constants';
6
+ import type { AbstractCursor } from './cursor/abstract_cursor';
7
7
  import {
8
- MongoError,
9
- MongoInvalidArgumentError,
8
+ AnyError,
9
+ isRetryableEndTransactionError,
10
10
  isRetryableError,
11
+ MongoAPIError,
11
12
  MongoCompatibilityError,
12
- MongoNetworkError,
13
- MongoWriteConcernError,
14
13
  MONGODB_ERROR_CODES,
15
- MongoServerError,
16
14
  MongoDriverError,
17
- MongoAPIError,
18
- AnyError,
15
+ MongoError,
19
16
  MongoExpiredSessionError,
17
+ MongoInvalidArgumentError,
18
+ MongoNetworkError,
19
+ MongoRuntimeError,
20
+ MongoServerError,
20
21
  MongoTransactionError,
21
- MongoRuntimeError
22
+ MongoWriteConcernError
22
23
  } from './error';
24
+ import type { MongoOptions } from './mongo_client';
25
+ import { TypedEventEmitter } from './mongo_types';
26
+ import { executeOperation } from './operations/execute_operation';
27
+ import { RunAdminCommandOperation } from './operations/run_command';
28
+ import { PromiseProvider } from './promise_provider';
29
+ import { ReadConcernLevel } from './read_concern';
30
+ import { ReadPreference } from './read_preference';
31
+ import { _advanceClusterTime, ClusterTime, TopologyType } from './sdam/common';
32
+ import type { Topology } from './sdam/topology';
33
+ import { isTransactionCommand, Transaction, TransactionOptions, TxnState } from './transactions';
23
34
  import {
24
- now,
25
35
  calculateDurationInMs,
26
36
  Callback,
37
+ commandSupportsReadConcern,
27
38
  isPromiseLike,
28
- uuidV4,
29
39
  maxWireVersion,
30
- maybePromise
40
+ maybePromise,
41
+ now,
42
+ uuidV4
31
43
  } from './utils';
32
- import type { Topology } from './sdam/topology';
33
- import type { MongoOptions } from './mongo_client';
34
- import { executeOperation } from './operations/execute_operation';
35
- import { RunAdminCommandOperation } from './operations/run_command';
36
- import type { AbstractCursor } from './cursor/abstract_cursor';
37
- import type { CommandOptions } from './cmap/connection';
38
- import { Connection } from './cmap/connection';
39
- import { ConnectionPoolMetrics } from './cmap/metrics';
40
44
  import type { WriteConcern } from './write_concern';
41
- import { TypedEventEmitter } from './mongo_types';
42
- import { ReadConcernLevel } from './read_concern';
43
45
 
44
46
  const minWireVersionForShardedTransactions = 8;
45
47
 
@@ -224,7 +226,7 @@ export class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
224
226
 
225
227
  this[kPinnedConnection] = conn;
226
228
  conn.emit(
227
- Connection.PINNED,
229
+ PINNED,
228
230
  this.inTransaction() ? ConnectionPoolMetrics.TXN : ConnectionPoolMetrics.CURSOR
229
231
  );
230
232
  }
@@ -519,7 +521,7 @@ export function maybeClearPinnedConnection(
519
521
  if (options?.error == null || options?.force) {
520
522
  loadBalancer.s.pool.checkIn(conn);
521
523
  conn.emit(
522
- Connection.UNPINNED,
524
+ UNPINNED,
523
525
  session.transaction.state !== TxnState.NO_TRANSACTION
524
526
  ? ConnectionPoolMetrics.TXN
525
527
  : ConnectionPoolMetrics.CURSOR
@@ -767,7 +769,7 @@ function endTransaction(session: ClientSession, commandName: string, callback: C
767
769
  session.unpin();
768
770
  }
769
771
 
770
- if (err && isRetryableError(err as MongoError)) {
772
+ if (err && isRetryableEndTransactionError(err as MongoError)) {
771
773
  // SPEC-1185: apply majority write concern when retrying commitTransaction
772
774
  if (command.commitTransaction) {
773
775
  // per txns spec, must unpin session in this case
@@ -928,25 +930,6 @@ export class ServerSessionPool {
928
930
  }
929
931
  }
930
932
 
931
- // TODO: this should be codified in command construction
932
- // @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.rst#read-concern
933
- export function commandSupportsReadConcern(command: Document, options?: Document): boolean {
934
- if (command.aggregate || command.count || command.distinct || command.find || command.geoNear) {
935
- return true;
936
- }
937
-
938
- if (
939
- command.mapReduce &&
940
- options &&
941
- options.out &&
942
- (options.out.inline === 1 || options.out === 'inline')
943
- ) {
944
- return true;
945
- }
946
-
947
- return false;
948
- }
949
-
950
933
  /**
951
934
  * Optionally decorate a command with sessions specific keys
952
935
  *
@@ -1,10 +1,10 @@
1
- import { ReadPreference } from './read_preference';
1
+ import type { Document } from './bson';
2
2
  import { MongoRuntimeError, MongoTransactionError } from './error';
3
- import { ReadConcern } from './read_concern';
4
- import { WriteConcern } from './write_concern';
5
- import type { Server } from './sdam/server';
6
3
  import type { CommandOperationOptions } from './operations/command';
7
- import type { Document } from './bson';
4
+ import { ReadConcern, ReadConcernLike } from './read_concern';
5
+ import { ReadPreference } from './read_preference';
6
+ import type { Server } from './sdam/server';
7
+ import { WriteConcern } from './write_concern';
8
8
 
9
9
  /** @internal */
10
10
  export const TxnState = Object.freeze({
@@ -63,7 +63,7 @@ const COMMITTED_STATES: Set<TxnState> = new Set([
63
63
  export interface TransactionOptions extends CommandOperationOptions {
64
64
  // TODO(NODE-3344): These options use the proper class forms of these settings, it should accept the basic enum values too
65
65
  /** A default read concern for commands in this transaction */
66
- readConcern?: ReadConcern;
66
+ readConcern?: ReadConcernLike;
67
67
  /** A default writeConcern for commands in this transaction */
68
68
  writeConcern?: WriteConcern;
69
69
  /** A default read preference for commands in this transaction */