mongodb 4.2.1 → 4.4.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 (266) hide show
  1. package/README.md +2 -2
  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 +7 -7
  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 -6
  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 +52 -10
  16. package/lib/cmap/auth/gssapi.js.map +1 -1
  17. package/lib/cmap/auth/mongo_credentials.js +36 -27
  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 +3 -2
  32. package/lib/cmap/command_monitoring_events.js.map +1 -1
  33. package/lib/cmap/commands.js +14 -8
  34. package/lib/cmap/commands.js.map +1 -1
  35. package/lib/cmap/connect.js +119 -37
  36. package/lib/cmap/connect.js.map +1 -1
  37. package/lib/cmap/connection.js +36 -35
  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 +2 -1
  46. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  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 +93 -54
  52. package/lib/connection_string.js.map +1 -1
  53. package/lib/constants.js +113 -1
  54. package/lib/constants.js.map +1 -1
  55. package/lib/cursor/abstract_cursor.js +5 -5
  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 +2 -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 +83 -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 +3 -3
  83. package/lib/operations/add_user.js.map +1 -1
  84. package/lib/operations/aggregate.js +1 -1
  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 +5 -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 +5 -6
  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.map +1 -1
  116. package/lib/operations/indexes.js +4 -4
  117. package/lib/operations/indexes.js.map +1 -1
  118. package/lib/operations/insert.js +3 -3
  119. package/lib/operations/insert.js.map +1 -1
  120. package/lib/operations/is_capped.js +1 -1
  121. package/lib/operations/is_capped.js.map +1 -1
  122. package/lib/operations/list_collections.js +3 -3
  123. package/lib/operations/list_collections.js.map +1 -1
  124. package/lib/operations/list_databases.js +1 -1
  125. package/lib/operations/list_databases.js.map +1 -1
  126. package/lib/operations/map_reduce.js +4 -3
  127. package/lib/operations/map_reduce.js.map +1 -1
  128. package/lib/operations/operation.js +1 -1
  129. package/lib/operations/operation.js.map +1 -1
  130. package/lib/operations/options_operation.js +1 -1
  131. package/lib/operations/options_operation.js.map +1 -1
  132. package/lib/operations/profiling_level.js +1 -1
  133. package/lib/operations/profiling_level.js.map +1 -1
  134. package/lib/operations/remove_user.js +1 -1
  135. package/lib/operations/remove_user.js.map +1 -1
  136. package/lib/operations/rename.js +3 -3
  137. package/lib/operations/rename.js.map +1 -1
  138. package/lib/operations/run_command.js +1 -1
  139. package/lib/operations/run_command.js.map +1 -1
  140. package/lib/operations/set_profiling_level.js +2 -2
  141. package/lib/operations/set_profiling_level.js.map +1 -1
  142. package/lib/operations/stats.js +1 -1
  143. package/lib/operations/stats.js.map +1 -1
  144. package/lib/operations/update.js +2 -2
  145. package/lib/operations/update.js.map +1 -1
  146. package/lib/operations/validate_collection.js +1 -1
  147. package/lib/operations/validate_collection.js.map +1 -1
  148. package/lib/read_preference.js +15 -5
  149. package/lib/read_preference.js.map +1 -1
  150. package/lib/sdam/events.js +4 -4
  151. package/lib/sdam/monitor.js +19 -18
  152. package/lib/sdam/monitor.js.map +1 -1
  153. package/lib/sdam/server.js +24 -31
  154. package/lib/sdam/server.js.map +1 -1
  155. package/lib/sdam/server_description.js +38 -38
  156. package/lib/sdam/server_description.js.map +1 -1
  157. package/lib/sdam/server_selection.js +2 -2
  158. package/lib/sdam/server_selection.js.map +1 -1
  159. package/lib/sdam/srv_polling.js +2 -2
  160. package/lib/sdam/srv_polling.js.map +1 -1
  161. package/lib/sdam/topology.js +32 -62
  162. package/lib/sdam/topology.js.map +1 -1
  163. package/lib/sdam/topology_description.js +3 -3
  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 +46 -15
  170. package/lib/utils.js.map +1 -1
  171. package/mongodb.d.ts +205 -60
  172. package/mongodb.ts34.d.ts +230 -59
  173. package/package.json +40 -37
  174. package/src/admin.ts +9 -9
  175. package/src/bson.ts +25 -17
  176. package/src/bulk/common.ts +22 -23
  177. package/src/bulk/ordered.ts +4 -4
  178. package/src/bulk/unordered.ts +5 -5
  179. package/src/change_stream.ts +29 -28
  180. package/src/cmap/auth/auth_provider.ts +3 -3
  181. package/src/cmap/auth/gssapi.ts +65 -14
  182. package/src/cmap/auth/mongo_credentials.ts +28 -17
  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 +4 -3
  190. package/src/cmap/commands.ts +22 -14
  191. package/src/cmap/connect.ts +161 -45
  192. package/src/cmap/connection.ts +83 -63
  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 +4 -3
  198. package/src/cmap/wire_protocol/shared.ts +10 -9
  199. package/src/collection.ts +116 -93
  200. package/src/connection_string.ts +130 -59
  201. package/src/constants.ts +122 -0
  202. package/src/cursor/abstract_cursor.ts +14 -13
  203. package/src/cursor/aggregation_cursor.ts +6 -6
  204. package/src/cursor/find_cursor.ts +12 -4
  205. package/src/db.ts +44 -33
  206. package/src/deps.ts +44 -1
  207. package/src/encrypter.ts +12 -3
  208. package/src/error.ts +52 -24
  209. package/src/gridfs/download.ts +10 -8
  210. package/src/gridfs/index.ts +11 -11
  211. package/src/gridfs/upload.ts +13 -10
  212. package/src/index.ts +205 -199
  213. package/src/logger.ts +2 -1
  214. package/src/mongo_client.ts +46 -32
  215. package/src/mongo_types.ts +119 -21
  216. package/src/operations/add_user.ts +7 -6
  217. package/src/operations/aggregate.ts +5 -5
  218. package/src/operations/bulk_write.ts +5 -5
  219. package/src/operations/collections.ts +2 -2
  220. package/src/operations/command.ts +15 -9
  221. package/src/operations/common_functions.ts +4 -4
  222. package/src/operations/connect.ts +4 -14
  223. package/src/operations/count.ts +4 -4
  224. package/src/operations/count_documents.ts +3 -3
  225. package/src/operations/create_collection.ts +7 -6
  226. package/src/operations/delete.ts +5 -5
  227. package/src/operations/distinct.ts +4 -4
  228. package/src/operations/drop.ts +4 -4
  229. package/src/operations/estimated_document_count.ts +5 -5
  230. package/src/operations/eval.ts +5 -5
  231. package/src/operations/execute_operation.ts +10 -11
  232. package/src/operations/find.ts +14 -14
  233. package/src/operations/find_and_modify.ts +7 -7
  234. package/src/operations/get_more.ts +2 -2
  235. package/src/operations/indexes.ts +16 -16
  236. package/src/operations/insert.ts +9 -9
  237. package/src/operations/is_capped.ts +3 -3
  238. package/src/operations/list_collections.ts +6 -6
  239. package/src/operations/list_databases.ts +4 -4
  240. package/src/operations/map_reduce.ts +10 -9
  241. package/src/operations/operation.ts +3 -3
  242. package/src/operations/options_operation.ts +3 -3
  243. package/src/operations/profiling_level.ts +4 -4
  244. package/src/operations/remove_user.ts +4 -4
  245. package/src/operations/rename.ts +6 -6
  246. package/src/operations/run_command.ts +3 -3
  247. package/src/operations/set_profiling_level.ts +6 -5
  248. package/src/operations/stats.ts +4 -4
  249. package/src/operations/update.ts +10 -10
  250. package/src/operations/validate_collection.ts +4 -4
  251. package/src/read_preference.ts +18 -7
  252. package/src/sdam/common.ts +2 -2
  253. package/src/sdam/events.ts +5 -5
  254. package/src/sdam/monitor.ts +29 -32
  255. package/src/sdam/server.ts +64 -62
  256. package/src/sdam/server_description.ts +41 -48
  257. package/src/sdam/server_selection.ts +3 -3
  258. package/src/sdam/srv_polling.ts +3 -2
  259. package/src/sdam/topology.ts +71 -89
  260. package/src/sdam/topology_description.ts +5 -5
  261. package/src/sessions.ts +31 -48
  262. package/src/transactions.ts +4 -4
  263. package/src/utils.ts +64 -28
  264. package/lib/cmap/auth/defaultAuthProviders.js +0 -30
  265. package/lib/cmap/auth/defaultAuthProviders.js.map +0 -1
  266. package/src/cmap/auth/defaultAuthProviders.ts +0 -32
@@ -1,26 +1,26 @@
1
- import { ServerType, STATE_CLOSED, STATE_CLOSING } from './common';
1
+ import { Document, Long } from '../bson';
2
+ import { connect } from '../cmap/connect';
3
+ import { Connection, ConnectionOptions } from '../cmap/connection';
4
+ import { LEGACY_HELLO_COMMAND } from '../constants';
5
+ import { AnyError, MongoNetworkError } from '../error';
6
+ import { CancellationToken, TypedEventEmitter } from '../mongo_types';
7
+ import type { Callback, InterruptibleAsyncInterval } from '../utils';
2
8
  import {
3
- now,
4
- makeStateMachine,
5
9
  calculateDurationInMs,
10
+ EventEmitterWithState,
6
11
  makeInterruptibleAsyncInterval,
7
- ns,
8
- EventEmitterWithState
12
+ makeStateMachine,
13
+ now,
14
+ ns
9
15
  } from '../utils';
10
- import { connect } from '../cmap/connect';
11
- import { Connection, ConnectionOptions } from '../cmap/connection';
12
- import { MongoNetworkError, AnyError } from '../error';
13
- import { Long, Document } from '../bson';
16
+ import { ServerType, STATE_CLOSED, STATE_CLOSING } from './common';
14
17
  import {
18
+ ServerHeartbeatFailedEvent,
15
19
  ServerHeartbeatStartedEvent,
16
- ServerHeartbeatSucceededEvent,
17
- ServerHeartbeatFailedEvent
20
+ ServerHeartbeatSucceededEvent
18
21
  } from './events';
19
-
20
22
  import { Server } from './server';
21
- import type { InterruptibleAsyncInterval, Callback } from '../utils';
22
23
  import type { TopologyVersion } from './server_description';
23
- import { CancellationToken, TypedEventEmitter } from '../mongo_types';
24
24
 
25
25
  /** @internal */
26
26
  const kServer = Symbol('server');
@@ -234,7 +234,7 @@ function checkServer(monitor: Monitor, callback: Callback<Document>) {
234
234
  const isAwaitable = topologyVersion != null;
235
235
 
236
236
  const cmd = {
237
- [serverApi?.version || helloOk ? 'hello' : 'ismaster']: true,
237
+ [serverApi?.version || helloOk ? 'hello' : LEGACY_HELLO_COMMAND]: true,
238
238
  ...(isAwaitable && topologyVersion
239
239
  ? { maxAwaitTimeMS, topologyVersion: makeTopologyVersion(topologyVersion) }
240
240
  : {})
@@ -257,14 +257,15 @@ function checkServer(monitor: Monitor, callback: Callback<Document>) {
257
257
  );
258
258
  }
259
259
 
260
- connection.command(ns('admin.$cmd'), cmd, options, (err, isMaster) => {
260
+ connection.command(ns('admin.$cmd'), cmd, options, (err, hello) => {
261
261
  if (err) {
262
262
  failureHandler(err);
263
263
  return;
264
264
  }
265
- if ('isWritablePrimary' in isMaster) {
266
- // Provide pre-hello-style response document.
267
- isMaster.ismaster = isMaster.isWritablePrimary;
265
+
266
+ if (!('isWritablePrimary' in hello)) {
267
+ // Provide hello-style response document.
268
+ hello.isWritablePrimary = hello[LEGACY_HELLO_COMMAND];
268
269
  }
269
270
 
270
271
  const rttPinger = monitor[kRTTPinger];
@@ -273,12 +274,12 @@ function checkServer(monitor: Monitor, callback: Callback<Document>) {
273
274
 
274
275
  monitor.emit(
275
276
  Server.SERVER_HEARTBEAT_SUCCEEDED,
276
- new ServerHeartbeatSucceededEvent(monitor.address, duration, isMaster)
277
+ new ServerHeartbeatSucceededEvent(monitor.address, duration, hello)
277
278
  );
278
279
 
279
280
  // if we are using the streaming protocol then we immediately issue another `started`
280
281
  // event, otherwise the "check" is complete and return to the main monitor loop
281
- if (isAwaitable && isMaster.topologyVersion) {
282
+ if (isAwaitable && hello.topologyVersion) {
282
283
  monitor.emit(
283
284
  Server.SERVER_HEARTBEAT_STARTED,
284
285
  new ServerHeartbeatStartedEvent(monitor.address)
@@ -288,14 +289,14 @@ function checkServer(monitor: Monitor, callback: Callback<Document>) {
288
289
  monitor[kRTTPinger]?.close();
289
290
  monitor[kRTTPinger] = undefined;
290
291
 
291
- callback(undefined, isMaster);
292
+ callback(undefined, hello);
292
293
  }
293
294
  });
294
295
 
295
296
  return;
296
297
  }
297
298
 
298
- // connecting does an implicit `ismaster`
299
+ // connecting does an implicit `hello`
299
300
  connect(monitor.connectOptions, (err, conn) => {
300
301
  if (err) {
301
302
  monitor[kConnection] = undefined;
@@ -318,14 +319,10 @@ function checkServer(monitor: Monitor, callback: Callback<Document>) {
318
319
  monitor[kConnection] = conn;
319
320
  monitor.emit(
320
321
  Server.SERVER_HEARTBEAT_SUCCEEDED,
321
- new ServerHeartbeatSucceededEvent(
322
- monitor.address,
323
- calculateDurationInMs(start),
324
- conn.ismaster
325
- )
322
+ new ServerHeartbeatSucceededEvent(monitor.address, calculateDurationInMs(start), conn.hello)
326
323
  );
327
324
 
328
- callback(undefined, conn.ismaster);
325
+ callback(undefined, conn.hello);
329
326
  }
330
327
  });
331
328
  }
@@ -341,7 +338,7 @@ function monitorServer(monitor: Monitor) {
341
338
  callback();
342
339
  }
343
340
 
344
- checkServer(monitor, (err, isMaster) => {
341
+ checkServer(monitor, (err, hello) => {
345
342
  if (err) {
346
343
  // otherwise an error occurred on initial discovery, also bail
347
344
  if (monitor[kServer].description.type === ServerType.Unknown) {
@@ -351,7 +348,7 @@ function monitorServer(monitor: Monitor) {
351
348
  }
352
349
 
353
350
  // if the check indicates streaming is supported, immediately reschedule monitoring
354
- if (isMaster && isMaster.topologyVersion) {
351
+ if (hello && hello.topologyVersion) {
355
352
  setTimeout(() => {
356
353
  if (!isInCloseState(monitor)) {
357
354
  monitor[kMonitorId]?.wake();
@@ -453,7 +450,7 @@ function measureRoundTripTime(rttPinger: RTTPinger, options: RTTPingerOptions) {
453
450
  return;
454
451
  }
455
452
 
456
- connection.command(ns('admin.$cmd'), { ismaster: 1 }, undefined, err => {
453
+ connection.command(ns('admin.$cmd'), { [LEGACY_HELLO_COMMAND]: 1 }, undefined, err => {
457
454
  if (err) {
458
455
  rttPinger[kConnection] = undefined;
459
456
  rttPinger[kRoundTripTime] = 0;
@@ -1,62 +1,72 @@
1
- import { Logger } from '../logger';
1
+ import type { Document, Long } from '../bson';
2
+ import {
3
+ CommandOptions,
4
+ Connection,
5
+ DestroyOptions,
6
+ GetMoreOptions,
7
+ QueryOptions
8
+ } from '../cmap/connection';
2
9
  import {
3
10
  ConnectionPool,
4
- ConnectionPoolOptions,
5
- CMAP_EVENTS,
6
- ConnectionPoolEvents
11
+ ConnectionPoolEvents,
12
+ ConnectionPoolOptions
7
13
  } from '../cmap/connection_pool';
8
- import { ServerDescription, compareTopologyVersion } from './server_description';
9
- import { Monitor, MonitorOptions } from './monitor';
14
+ import {
15
+ APM_EVENTS,
16
+ CLOSED,
17
+ CMAP_EVENTS,
18
+ CONNECT,
19
+ DESCRIPTION_RECEIVED,
20
+ ENDED,
21
+ HEARTBEAT_EVENTS,
22
+ SERVER_HEARTBEAT_FAILED,
23
+ SERVER_HEARTBEAT_STARTED,
24
+ SERVER_HEARTBEAT_SUCCEEDED
25
+ } from '../constants';
26
+ import type { AutoEncrypter } from '../deps';
27
+ import {
28
+ isNetworkErrorBeforeHandshake,
29
+ isNodeShuttingDownError,
30
+ isRetryableWriteError,
31
+ isSDAMUnrecoverableError,
32
+ MongoCompatibilityError,
33
+ MongoError,
34
+ MongoInvalidArgumentError,
35
+ MongoNetworkError,
36
+ MongoNetworkTimeoutError,
37
+ MongoServerClosedError
38
+ } from '../error';
39
+ import { Logger } from '../logger';
40
+ import type { ServerApi } from '../mongo_client';
41
+ import { TypedEventEmitter } from '../mongo_types';
42
+ import type { ClientSession } from '../sessions';
10
43
  import { isTransactionCommand } from '../transactions';
11
44
  import {
45
+ Callback,
46
+ CallbackWithType,
12
47
  collationNotSupported,
48
+ EventEmitterWithState,
13
49
  makeStateMachine,
14
50
  maxWireVersion,
15
- Callback,
16
- CallbackWithType,
17
51
  MongoDBNamespace,
18
- EventEmitterWithState
52
+ supportsRetryableWrites
19
53
  } from '../utils';
20
54
  import {
55
+ ClusterTime,
21
56
  STATE_CLOSED,
22
57
  STATE_CLOSING,
23
- STATE_CONNECTING,
24
58
  STATE_CONNECTED,
25
- ClusterTime,
59
+ STATE_CONNECTING,
26
60
  TopologyType
27
61
  } from './common';
28
- import {
29
- MongoError,
30
- MongoNetworkError,
31
- MongoNetworkTimeoutError,
32
- isSDAMUnrecoverableError,
33
- isRetryableWriteError,
34
- isNodeShuttingDownError,
35
- isNetworkErrorBeforeHandshake,
36
- MongoCompatibilityError,
37
- MongoInvalidArgumentError,
38
- MongoServerClosedError
39
- } from '../error';
40
- import {
41
- Connection,
42
- DestroyOptions,
43
- QueryOptions,
44
- GetMoreOptions,
45
- CommandOptions,
46
- APM_EVENTS
47
- } from '../cmap/connection';
48
- import type { Topology } from './topology';
49
62
  import type {
50
63
  ServerHeartbeatFailedEvent,
51
64
  ServerHeartbeatStartedEvent,
52
65
  ServerHeartbeatSucceededEvent
53
66
  } from './events';
54
- import type { ClientSession } from '../sessions';
55
- import type { Document, Long } from '../bson';
56
- import type { AutoEncrypter } from '../deps';
57
- import type { ServerApi } from '../mongo_client';
58
- import { TypedEventEmitter } from '../mongo_types';
59
- import { supportsRetryableWrites } from '../utils';
67
+ import { Monitor, MonitorOptions } from './monitor';
68
+ import { compareTopologyVersion, ServerDescription } from './server_description';
69
+ import type { Topology } from './topology';
60
70
 
61
71
  const stateTransition = makeStateMachine({
62
72
  [STATE_CLOSED]: [STATE_CLOSED, STATE_CONNECTING],
@@ -108,24 +118,23 @@ export class Server extends TypedEventEmitter<ServerEvents> {
108
118
  /** @internal */
109
119
  s: ServerPrivate;
110
120
  serverApi?: ServerApi;
111
- clusterTime?: ClusterTime;
112
- ismaster?: Document;
121
+ hello?: Document;
113
122
  [kMonitor]: Monitor;
114
123
 
115
124
  /** @event */
116
- static readonly SERVER_HEARTBEAT_STARTED = 'serverHeartbeatStarted' as const;
125
+ static readonly SERVER_HEARTBEAT_STARTED = SERVER_HEARTBEAT_STARTED;
117
126
  /** @event */
118
- static readonly SERVER_HEARTBEAT_SUCCEEDED = 'serverHeartbeatSucceeded' as const;
127
+ static readonly SERVER_HEARTBEAT_SUCCEEDED = SERVER_HEARTBEAT_SUCCEEDED;
119
128
  /** @event */
120
- static readonly SERVER_HEARTBEAT_FAILED = 'serverHeartbeatFailed' as const;
129
+ static readonly SERVER_HEARTBEAT_FAILED = SERVER_HEARTBEAT_FAILED;
121
130
  /** @event */
122
- static readonly CONNECT = 'connect' as const;
131
+ static readonly CONNECT = CONNECT;
123
132
  /** @event */
124
- static readonly DESCRIPTION_RECEIVED = 'descriptionReceived' as const;
133
+ static readonly DESCRIPTION_RECEIVED = DESCRIPTION_RECEIVED;
125
134
  /** @event */
126
- static readonly CLOSED = 'closed' as const;
135
+ static readonly CLOSED = CLOSED;
127
136
  /** @event */
128
- static readonly ENDED = 'ended' as const;
137
+ static readonly ENDED = ENDED;
129
138
 
130
139
  /**
131
140
  * Create a server
@@ -184,6 +193,14 @@ export class Server extends TypedEventEmitter<ServerEvents> {
184
193
  });
185
194
  }
186
195
 
196
+ get clusterTime(): ClusterTime | undefined {
197
+ return this.s.topology.clusterTime;
198
+ }
199
+
200
+ set clusterTime(clusterTime: ClusterTime | undefined) {
201
+ this.s.topology.clusterTime = clusterTime;
202
+ }
203
+
187
204
  get description(): ServerDescription {
188
205
  return this.s.description;
189
206
  }
@@ -453,21 +470,6 @@ export class Server extends TypedEventEmitter<ServerEvents> {
453
470
  }
454
471
  }
455
472
 
456
- export const HEARTBEAT_EVENTS = [
457
- Server.SERVER_HEARTBEAT_STARTED,
458
- Server.SERVER_HEARTBEAT_SUCCEEDED,
459
- Server.SERVER_HEARTBEAT_FAILED
460
- ];
461
-
462
- Object.defineProperty(Server.prototype, 'clusterTime', {
463
- get() {
464
- return this.s.topology.clusterTime;
465
- },
466
- set(clusterTime: ClusterTime) {
467
- this.s.topology.clusterTime = clusterTime;
468
- }
469
- });
470
-
471
473
  function calculateRoundTripTime(oldRtt: number, duration: number): number {
472
474
  if (oldRtt === -1) {
473
475
  return duration;
@@ -1,8 +1,8 @@
1
- import { arrayStrictEqual, errorStrictEqual, now, HostAddress } from '../utils';
2
- import { ServerType } from './common';
3
- import { ObjectId, Long, Document } from '../bson';
4
- import type { ClusterTime } from './common';
1
+ import { Document, Long, ObjectId } from '../bson';
5
2
  import type { MongoError } from '../error';
3
+ import { arrayStrictEqual, errorStrictEqual, HostAddress, now } from '../utils';
4
+ import type { ClusterTime } from './common';
5
+ import { ServerType } from './common';
6
6
 
7
7
  const WRITABLE_SERVER_TYPES = new Set<ServerType>([
8
8
  ServerType.RSPrimary,
@@ -44,7 +44,7 @@ export interface ServerDescriptionOptions {
44
44
  }
45
45
 
46
46
  /**
47
- * The client's view of a single server, based on the most recent ismaster outcome.
47
+ * The client's view of a single server, based on the most recent hello outcome.
48
48
  *
49
49
  * Internal type, not meant to be directly instantiated
50
50
  * @public
@@ -81,13 +81,9 @@ export class ServerDescription {
81
81
  * @internal
82
82
  *
83
83
  * @param address - The address of the server
84
- * @param ismaster - An optional ismaster response for this server
84
+ * @param hello - An optional hello response for this server
85
85
  */
86
- constructor(
87
- address: HostAddress | string,
88
- ismaster?: Document,
89
- options?: ServerDescriptionOptions
90
- ) {
86
+ constructor(address: HostAddress | string, hello?: Document, options?: ServerDescriptionOptions) {
91
87
  if (typeof address === 'string') {
92
88
  this._hostAddress = new HostAddress(address);
93
89
  this.address = this._hostAddress.toString();
@@ -95,53 +91,53 @@ export class ServerDescription {
95
91
  this._hostAddress = address;
96
92
  this.address = this._hostAddress.toString();
97
93
  }
98
- this.type = parseServerType(ismaster, options);
99
- this.hosts = ismaster?.hosts?.map((host: string) => host.toLowerCase()) ?? [];
100
- this.passives = ismaster?.passives?.map((host: string) => host.toLowerCase()) ?? [];
101
- this.arbiters = ismaster?.arbiters?.map((host: string) => host.toLowerCase()) ?? [];
102
- this.tags = ismaster?.tags ?? {};
103
- this.minWireVersion = ismaster?.minWireVersion ?? 0;
104
- this.maxWireVersion = ismaster?.maxWireVersion ?? 0;
94
+ this.type = parseServerType(hello, options);
95
+ this.hosts = hello?.hosts?.map((host: string) => host.toLowerCase()) ?? [];
96
+ this.passives = hello?.passives?.map((host: string) => host.toLowerCase()) ?? [];
97
+ this.arbiters = hello?.arbiters?.map((host: string) => host.toLowerCase()) ?? [];
98
+ this.tags = hello?.tags ?? {};
99
+ this.minWireVersion = hello?.minWireVersion ?? 0;
100
+ this.maxWireVersion = hello?.maxWireVersion ?? 0;
105
101
  this.roundTripTime = options?.roundTripTime ?? -1;
106
102
  this.lastUpdateTime = now();
107
- this.lastWriteDate = ismaster?.lastWrite?.lastWriteDate ?? 0;
103
+ this.lastWriteDate = hello?.lastWrite?.lastWriteDate ?? 0;
108
104
 
109
105
  if (options?.topologyVersion) {
110
106
  this.topologyVersion = options.topologyVersion;
111
- } else if (ismaster?.topologyVersion) {
112
- this.topologyVersion = ismaster.topologyVersion;
107
+ } else if (hello?.topologyVersion) {
108
+ this.topologyVersion = hello.topologyVersion;
113
109
  }
114
110
 
115
111
  if (options?.error) {
116
112
  this.error = options.error;
117
113
  }
118
114
 
119
- if (ismaster?.primary) {
120
- this.primary = ismaster.primary;
115
+ if (hello?.primary) {
116
+ this.primary = hello.primary;
121
117
  }
122
118
 
123
- if (ismaster?.me) {
124
- this.me = ismaster.me.toLowerCase();
119
+ if (hello?.me) {
120
+ this.me = hello.me.toLowerCase();
125
121
  }
126
122
 
127
- if (ismaster?.setName) {
128
- this.setName = ismaster.setName;
123
+ if (hello?.setName) {
124
+ this.setName = hello.setName;
129
125
  }
130
126
 
131
- if (ismaster?.setVersion) {
132
- this.setVersion = ismaster.setVersion;
127
+ if (hello?.setVersion) {
128
+ this.setVersion = hello.setVersion;
133
129
  }
134
130
 
135
- if (ismaster?.electionId) {
136
- this.electionId = ismaster.electionId;
131
+ if (hello?.electionId) {
132
+ this.electionId = hello.electionId;
137
133
  }
138
134
 
139
- if (ismaster?.logicalSessionTimeoutMinutes) {
140
- this.logicalSessionTimeoutMinutes = ismaster.logicalSessionTimeoutMinutes;
135
+ if (hello?.logicalSessionTimeoutMinutes) {
136
+ this.logicalSessionTimeoutMinutes = hello.logicalSessionTimeoutMinutes;
141
137
  }
142
138
 
143
- if (ismaster?.$clusterTime) {
144
- this.$clusterTime = ismaster.$clusterTime;
139
+ if (hello?.$clusterTime) {
140
+ this.$clusterTime = hello.$clusterTime;
145
141
  }
146
142
  }
147
143
 
@@ -210,35 +206,32 @@ export class ServerDescription {
210
206
  }
211
207
  }
212
208
 
213
- // Parses an `ismaster` message and determines the server type
214
- export function parseServerType(
215
- ismaster?: Document,
216
- options?: ServerDescriptionOptions
217
- ): ServerType {
209
+ // Parses a `hello` message and determines the server type
210
+ export function parseServerType(hello?: Document, options?: ServerDescriptionOptions): ServerType {
218
211
  if (options?.loadBalanced) {
219
212
  return ServerType.LoadBalancer;
220
213
  }
221
214
 
222
- if (!ismaster || !ismaster.ok) {
215
+ if (!hello || !hello.ok) {
223
216
  return ServerType.Unknown;
224
217
  }
225
218
 
226
- if (ismaster.isreplicaset) {
219
+ if (hello.isreplicaset) {
227
220
  return ServerType.RSGhost;
228
221
  }
229
222
 
230
- if (ismaster.msg && ismaster.msg === 'isdbgrid') {
223
+ if (hello.msg && hello.msg === 'isdbgrid') {
231
224
  return ServerType.Mongos;
232
225
  }
233
226
 
234
- if (ismaster.setName) {
235
- if (ismaster.hidden) {
227
+ if (hello.setName) {
228
+ if (hello.hidden) {
236
229
  return ServerType.RSOther;
237
- } else if (ismaster.ismaster || ismaster.isWritablePrimary) {
230
+ } else if (hello.isWritablePrimary) {
238
231
  return ServerType.RSPrimary;
239
- } else if (ismaster.secondary) {
232
+ } else if (hello.secondary) {
240
233
  return ServerType.RSSecondary;
241
- } else if (ismaster.arbiterOnly) {
234
+ } else if (hello.arbiterOnly) {
242
235
  return ServerType.RSArbiter;
243
236
  } else {
244
237
  return ServerType.RSOther;
@@ -1,8 +1,8 @@
1
- import { ServerType, TopologyType } from './common';
2
- import { ReadPreference } from '../read_preference';
3
1
  import { MongoCompatibilityError, MongoInvalidArgumentError } from '../error';
4
- import type { TopologyDescription } from './topology_description';
2
+ import { ReadPreference } from '../read_preference';
3
+ import { ServerType, TopologyType } from './common';
5
4
  import type { ServerDescription, TagSet } from './server_description';
5
+ import type { TopologyDescription } from './topology_description';
6
6
 
7
7
  // max staleness constants
8
8
  const IDLE_WRITE_PERIOD = 10000;
@@ -1,8 +1,9 @@
1
1
  import * as dns from 'dns';
2
+
3
+ import { MongoRuntimeError } from '../error';
2
4
  import { Logger, LoggerOptions } from '../logger';
3
- import { HostAddress } from '../utils';
4
5
  import { TypedEventEmitter } from '../mongo_types';
5
- import { MongoRuntimeError } from '../error';
6
+ import { HostAddress } from '../utils';
6
7
 
7
8
  /**
8
9
  * Determines whether a provided address matches the provided parent domain in order