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,7 +1,7 @@
1
- import type { TagSet } from './sdam/server_description';
2
1
  import type { Document } from './bson';
3
- import type { ClientSession } from './sessions';
4
2
  import { MongoInvalidArgumentError } from './error';
3
+ import type { TagSet } from './sdam/server_description';
4
+ import type { ClientSession } from './sessions';
5
5
 
6
6
  /** @public */
7
7
  export type ReadPreferenceLike = ReadPreference | ReadPreferenceMode;
@@ -156,7 +156,10 @@ export class ReadPreference {
156
156
  }
157
157
 
158
158
  if (typeof readPreference === 'string') {
159
- return new ReadPreference(readPreference as ReadPreferenceMode, readPreferenceTags);
159
+ return new ReadPreference(readPreference as ReadPreferenceMode, readPreferenceTags, {
160
+ maxStalenessSeconds: options.maxStalenessSeconds,
161
+ hedge: options.hedge
162
+ });
160
163
  } else if (!(readPreference instanceof ReadPreference) && typeof readPreference === 'object') {
161
164
  const mode = readPreference.mode || readPreference.preference;
162
165
  if (mode && typeof mode === 'string') {
@@ -229,19 +232,27 @@ export class ReadPreference {
229
232
  }
230
233
 
231
234
  /**
232
- * Indicates that this readPreference needs the "slaveOk" bit when sent over the wire
233
- *
235
+ * Indicates that this readPreference needs the "secondaryOk" bit when sent over the wire
236
+ * @deprecated Use secondaryOk instead
234
237
  * @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
235
238
  */
236
239
  slaveOk(): boolean {
237
- const NEEDS_SLAVEOK = new Set<string>([
240
+ return this.secondaryOk();
241
+ }
242
+
243
+ /**
244
+ * Indicates that this readPreference needs the "SecondaryOk" bit when sent over the wire
245
+ * @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
246
+ */
247
+ secondaryOk(): boolean {
248
+ const NEEDS_SECONDARYOK = new Set<string>([
238
249
  ReadPreference.PRIMARY_PREFERRED,
239
250
  ReadPreference.SECONDARY,
240
251
  ReadPreference.SECONDARY_PREFERRED,
241
252
  ReadPreference.NEAREST
242
253
  ]);
243
254
 
244
- return NEEDS_SLAVEOK.has(this.mode);
255
+ return NEEDS_SECONDARYOK.has(this.mode);
245
256
  }
246
257
 
247
258
  /**
@@ -1,6 +1,6 @@
1
- import type { Timestamp, Binary, Long } from '../bson';
2
- import type { Topology } from './topology';
1
+ import type { Binary, Long, Timestamp } from '../bson';
3
2
  import type { ClientSession } from '../sessions';
3
+ import type { Topology } from './topology';
4
4
 
5
5
  // shared state names
6
6
  export const STATE_CLOSING = 'closing';
@@ -1,6 +1,6 @@
1
+ import type { Document } from '../bson';
1
2
  import type { ServerDescription } from './server_description';
2
3
  import type { TopologyDescription } from './topology_description';
3
- import type { Document } from '../bson';
4
4
 
5
5
  /**
6
6
  * Emitted when server description changes, but does NOT include changes to the RTT.
@@ -1,26 +1,25 @@
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 { AnyError, MongoNetworkError } from '../error';
5
+ import { CancellationToken, TypedEventEmitter } from '../mongo_types';
6
+ import type { Callback, InterruptibleAsyncInterval } from '../utils';
2
7
  import {
3
- now,
4
- makeStateMachine,
5
8
  calculateDurationInMs,
9
+ EventEmitterWithState,
6
10
  makeInterruptibleAsyncInterval,
7
- ns,
8
- EventEmitterWithState
11
+ makeStateMachine,
12
+ now,
13
+ ns
9
14
  } 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';
15
+ import { ServerType, STATE_CLOSED, STATE_CLOSING } from './common';
14
16
  import {
17
+ ServerHeartbeatFailedEvent,
15
18
  ServerHeartbeatStartedEvent,
16
- ServerHeartbeatSucceededEvent,
17
- ServerHeartbeatFailedEvent
19
+ ServerHeartbeatSucceededEvent
18
20
  } from './events';
19
-
20
21
  import { Server } from './server';
21
- import type { InterruptibleAsyncInterval, Callback } from '../utils';
22
22
  import type { TopologyVersion } from './server_description';
23
- import { CancellationToken, TypedEventEmitter } from '../mongo_types';
24
23
 
25
24
  /** @internal */
26
25
  const kServer = Symbol('server');
@@ -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
121
  ismaster?: 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
  }
@@ -299,6 +316,14 @@ export class Server extends TypedEventEmitter<ServerEvents> {
299
316
  // Clone the options
300
317
  const finalOptions = Object.assign({}, options, { wireProtocolCommand: false });
301
318
 
319
+ // There are cases where we need to flag the read preference not to get sent in
320
+ // the command, such as pre-5.0 servers attempting to perform an aggregate write
321
+ // with a non-primary read preference. In this case the effective read preference
322
+ // (primary) is not the same as the provided and must be removed completely.
323
+ if (finalOptions.omitReadPreference) {
324
+ delete finalOptions.readPreference;
325
+ }
326
+
302
327
  // error if collation not supported
303
328
  if (collationNotSupported(this, cmd)) {
304
329
  callback(new MongoCompatibilityError(`Server ${this.name} does not support collation`));
@@ -445,21 +470,6 @@ export class Server extends TypedEventEmitter<ServerEvents> {
445
470
  }
446
471
  }
447
472
 
448
- export const HEARTBEAT_EVENTS = [
449
- Server.SERVER_HEARTBEAT_STARTED,
450
- Server.SERVER_HEARTBEAT_SUCCEEDED,
451
- Server.SERVER_HEARTBEAT_FAILED
452
- ];
453
-
454
- Object.defineProperty(Server.prototype, 'clusterTime', {
455
- get() {
456
- return this.s.topology.clusterTime;
457
- },
458
- set(clusterTime: ClusterTime) {
459
- this.s.topology.clusterTime = clusterTime;
460
- }
461
- });
462
-
463
473
  function calculateRoundTripTime(oldRtt: number, duration: number): number {
464
474
  if (oldRtt === -1) {
465
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,
@@ -1,13 +1,16 @@
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;
9
9
  const SMALLEST_MAX_STALENESS_SECONDS = 90;
10
10
 
11
+ // Minimum version to try writes on secondaries.
12
+ export const MIN_SECONDARY_WRITE_WIRE_VERSION = 13;
13
+
11
14
  /** @public */
12
15
  export type ServerSelector = (
13
16
  topologyDescription: TopologyDescription,
@@ -28,6 +31,46 @@ export function writableServerSelector(): ServerSelector {
28
31
  );
29
32
  }
30
33
 
34
+ /**
35
+ * The purpose of this selector is to select the same server, only
36
+ * if it is in a state that it can have commands sent to it.
37
+ */
38
+ export function sameServerSelector(description?: ServerDescription): ServerSelector {
39
+ return (
40
+ topologyDescription: TopologyDescription,
41
+ servers: ServerDescription[]
42
+ ): ServerDescription[] => {
43
+ if (!description) return [];
44
+ // Filter the servers to match the provided description only if
45
+ // the type is not unknown.
46
+ return servers.filter(sd => {
47
+ return sd.address === description.address && sd.type !== ServerType.Unknown;
48
+ });
49
+ };
50
+ }
51
+
52
+ /**
53
+ * Returns a server selector that uses a read preference to select a
54
+ * server potentially for a write on a secondary.
55
+ */
56
+ export function secondaryWritableServerSelector(
57
+ wireVersion?: number,
58
+ readPreference?: ReadPreference
59
+ ): ServerSelector {
60
+ // If server version < 5.0, read preference always primary.
61
+ // If server version >= 5.0...
62
+ // - If read preference is supplied, use that.
63
+ // - If no read preference is supplied, use primary.
64
+ if (
65
+ !readPreference ||
66
+ !wireVersion ||
67
+ (wireVersion && wireVersion < MIN_SECONDARY_WRITE_WIRE_VERSION)
68
+ ) {
69
+ return readPreferenceServerSelector(ReadPreference.primary);
70
+ }
71
+ return readPreferenceServerSelector(readPreference);
72
+ }
73
+
31
74
  /**
32
75
  * Reduces the passed in array of servers by the rules of the "Max Staleness" specification
33
76
  * found here: https://github.com/mongodb/specifications/blob/master/source/max-staleness/max-staleness.rst
@@ -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
@@ -29,18 +30,15 @@ export class SrvPollingEvent {
29
30
  this.srvRecords = srvRecords;
30
31
  }
31
32
 
32
- addresses(): Map<string, HostAddress> {
33
- return new Map(
34
- this.srvRecords.map(record => {
35
- const host = new HostAddress(`${record.name}:${record.port}`);
36
- return [host.toString(), host];
37
- })
38
- );
33
+ hostnames(): Set<string> {
34
+ return new Set(this.srvRecords.map(r => HostAddress.fromSrvRecord(r).toString()));
39
35
  }
40
36
  }
41
37
 
42
38
  /** @internal */
43
39
  export interface SrvPollerOptions extends LoggerOptions {
40
+ srvServiceName: string;
41
+ srvMaxHosts: number;
44
42
  srvHost: string;
45
43
  heartbeatFrequencyMS: number;
46
44
  }
@@ -58,6 +56,8 @@ export class SrvPoller extends TypedEventEmitter<SrvPollerEvents> {
58
56
  logger: Logger;
59
57
  haMode: boolean;
60
58
  generation: number;
59
+ srvMaxHosts: number;
60
+ srvServiceName: string;
61
61
  _timeout?: NodeJS.Timeout;
62
62
 
63
63
  /** @event */
@@ -71,8 +71,10 @@ export class SrvPoller extends TypedEventEmitter<SrvPollerEvents> {
71
71
  }
72
72
 
73
73
  this.srvHost = options.srvHost;
74
+ this.srvMaxHosts = options.srvMaxHosts ?? 0;
75
+ this.srvServiceName = options.srvServiceName ?? 'mongodb';
74
76
  this.rescanSrvIntervalMS = 60000;
75
- this.heartbeatFrequencyMS = options.heartbeatFrequencyMS || 10000;
77
+ this.heartbeatFrequencyMS = options.heartbeatFrequencyMS ?? 10000;
76
78
  this.logger = new Logger('srvPoller', options);
77
79
 
78
80
  this.haMode = false;
@@ -82,7 +84,7 @@ export class SrvPoller extends TypedEventEmitter<SrvPollerEvents> {
82
84
  }
83
85
 
84
86
  get srvAddress(): string {
85
- return `_mongodb._tcp.${this.srvHost}`;
87
+ return `_${this.srvServiceName}._tcp.${this.srvHost}`;
86
88
  }
87
89
 
88
90
  get intervalMS(): number {
@@ -143,13 +145,13 @@ export class SrvPoller extends TypedEventEmitter<SrvPollerEvents> {
143
145
  }
144
146
 
145
147
  const finalAddresses: dns.SrvRecord[] = [];
146
- srvRecords.forEach((record: dns.SrvRecord) => {
148
+ for (const record of srvRecords) {
147
149
  if (matchesParentDomain(record.name, this.srvHost)) {
148
150
  finalAddresses.push(record);
149
151
  } else {
150
152
  this.parentDomainMismatch(record);
151
153
  }
152
- });
154
+ }
153
155
 
154
156
  if (!finalAddresses.length) {
155
157
  this.failure('No valid addresses found at host');