mongodb 6.3.0 → 6.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 (221) hide show
  1. package/lib/bson.js +2 -1
  2. package/lib/bson.js.map +1 -1
  3. package/lib/bulk/common.js +14 -9
  4. package/lib/bulk/common.js.map +1 -1
  5. package/lib/change_stream.js +4 -2
  6. package/lib/change_stream.js.map +1 -1
  7. package/lib/client-side-encryption/state_machine.js +58 -52
  8. package/lib/client-side-encryption/state_machine.js.map +1 -1
  9. package/lib/cmap/auth/auth_provider.js +4 -0
  10. package/lib/cmap/auth/auth_provider.js.map +1 -1
  11. package/lib/cmap/auth/gssapi.js +1 -1
  12. package/lib/cmap/auth/gssapi.js.map +1 -1
  13. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  14. package/lib/cmap/auth/mongocr.js +2 -2
  15. package/lib/cmap/auth/mongocr.js.map +1 -1
  16. package/lib/cmap/auth/mongodb_aws.js +58 -55
  17. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  18. package/lib/cmap/auth/mongodb_oidc/callback_workflow.js +2 -2
  19. package/lib/cmap/auth/mongodb_oidc/callback_workflow.js.map +1 -1
  20. package/lib/cmap/auth/mongodb_oidc/service_workflow.js +1 -1
  21. package/lib/cmap/auth/mongodb_oidc/service_workflow.js.map +1 -1
  22. package/lib/cmap/auth/plain.js +1 -1
  23. package/lib/cmap/auth/plain.js.map +1 -1
  24. package/lib/cmap/auth/scram.js +5 -7
  25. package/lib/cmap/auth/scram.js.map +1 -1
  26. package/lib/cmap/auth/x509.js +1 -1
  27. package/lib/cmap/auth/x509.js.map +1 -1
  28. package/lib/cmap/command_monitoring_events.js +6 -3
  29. package/lib/cmap/command_monitoring_events.js.map +1 -1
  30. package/lib/cmap/commands.js +2 -0
  31. package/lib/cmap/commands.js.map +1 -1
  32. package/lib/cmap/connect.js +88 -110
  33. package/lib/cmap/connect.js.map +1 -1
  34. package/lib/cmap/connection.js +280 -726
  35. package/lib/cmap/connection.js.map +1 -1
  36. package/lib/cmap/connection_pool.js +35 -95
  37. package/lib/cmap/connection_pool.js.map +1 -1
  38. package/lib/cmap/stream_description.js +19 -0
  39. package/lib/cmap/stream_description.js.map +1 -1
  40. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  41. package/lib/cmap/wire_protocol/on_data.js +112 -0
  42. package/lib/cmap/wire_protocol/on_data.js.map +1 -0
  43. package/lib/collection.js +11 -3
  44. package/lib/collection.js.map +1 -1
  45. package/lib/connection_string.js +34 -2
  46. package/lib/connection_string.js.map +1 -1
  47. package/lib/constants.js +22 -2
  48. package/lib/constants.js.map +1 -1
  49. package/lib/cursor/abstract_cursor.js +5 -1
  50. package/lib/cursor/abstract_cursor.js.map +1 -1
  51. package/lib/error.js +7 -7
  52. package/lib/error.js.map +1 -1
  53. package/lib/index.js +9 -1
  54. package/lib/index.js.map +1 -1
  55. package/lib/mongo_client.js +8 -3
  56. package/lib/mongo_client.js.map +1 -1
  57. package/lib/mongo_client_auth_providers.js +55 -0
  58. package/lib/mongo_client_auth_providers.js.map +1 -0
  59. package/lib/mongo_logger.js +205 -27
  60. package/lib/mongo_logger.js.map +1 -1
  61. package/lib/mongo_types.js +26 -0
  62. package/lib/mongo_types.js.map +1 -1
  63. package/lib/operations/aggregate.js +3 -0
  64. package/lib/operations/aggregate.js.map +1 -1
  65. package/lib/operations/bulk_write.js +3 -0
  66. package/lib/operations/bulk_write.js.map +1 -1
  67. package/lib/operations/collections.js +3 -0
  68. package/lib/operations/collections.js.map +1 -1
  69. package/lib/operations/command.js +1 -1
  70. package/lib/operations/command.js.map +1 -1
  71. package/lib/operations/count.js +3 -0
  72. package/lib/operations/count.js.map +1 -1
  73. package/lib/operations/create_collection.js +3 -0
  74. package/lib/operations/create_collection.js.map +1 -1
  75. package/lib/operations/delete.js +3 -0
  76. package/lib/operations/delete.js.map +1 -1
  77. package/lib/operations/distinct.js +3 -0
  78. package/lib/operations/distinct.js.map +1 -1
  79. package/lib/operations/drop.js +6 -0
  80. package/lib/operations/drop.js.map +1 -1
  81. package/lib/operations/estimated_document_count.js +3 -0
  82. package/lib/operations/estimated_document_count.js.map +1 -1
  83. package/lib/operations/execute_operation.js +35 -9
  84. package/lib/operations/execute_operation.js.map +1 -1
  85. package/lib/operations/find.js +4 -1
  86. package/lib/operations/find.js.map +1 -1
  87. package/lib/operations/find_and_modify.js +5 -1
  88. package/lib/operations/find_and_modify.js.map +1 -1
  89. package/lib/operations/get_more.js +4 -1
  90. package/lib/operations/get_more.js.map +1 -1
  91. package/lib/operations/indexes.js +21 -0
  92. package/lib/operations/indexes.js.map +1 -1
  93. package/lib/operations/insert.js +6 -0
  94. package/lib/operations/insert.js.map +1 -1
  95. package/lib/operations/is_capped.js +3 -0
  96. package/lib/operations/is_capped.js.map +1 -1
  97. package/lib/operations/kill_cursors.js +4 -1
  98. package/lib/operations/kill_cursors.js.map +1 -1
  99. package/lib/operations/list_collections.js +3 -0
  100. package/lib/operations/list_collections.js.map +1 -1
  101. package/lib/operations/list_databases.js +3 -0
  102. package/lib/operations/list_databases.js.map +1 -1
  103. package/lib/operations/operation.js.map +1 -1
  104. package/lib/operations/options_operation.js +3 -0
  105. package/lib/operations/options_operation.js.map +1 -1
  106. package/lib/operations/profiling_level.js +3 -0
  107. package/lib/operations/profiling_level.js.map +1 -1
  108. package/lib/operations/remove_user.js +3 -0
  109. package/lib/operations/remove_user.js.map +1 -1
  110. package/lib/operations/rename.js +3 -0
  111. package/lib/operations/rename.js.map +1 -1
  112. package/lib/operations/run_command.js +8 -2
  113. package/lib/operations/run_command.js.map +1 -1
  114. package/lib/operations/search_indexes/create.js +4 -1
  115. package/lib/operations/search_indexes/create.js.map +1 -1
  116. package/lib/operations/search_indexes/drop.js +4 -1
  117. package/lib/operations/search_indexes/drop.js.map +1 -1
  118. package/lib/operations/search_indexes/update.js +4 -1
  119. package/lib/operations/search_indexes/update.js.map +1 -1
  120. package/lib/operations/set_profiling_level.js +3 -0
  121. package/lib/operations/set_profiling_level.js.map +1 -1
  122. package/lib/operations/stats.js +3 -0
  123. package/lib/operations/stats.js.map +1 -1
  124. package/lib/operations/update.js +3 -0
  125. package/lib/operations/update.js.map +1 -1
  126. package/lib/operations/validate_collection.js +3 -0
  127. package/lib/operations/validate_collection.js.map +1 -1
  128. package/lib/sdam/events.js +18 -0
  129. package/lib/sdam/events.js.map +1 -1
  130. package/lib/sdam/monitor.js +86 -71
  131. package/lib/sdam/monitor.js.map +1 -1
  132. package/lib/sdam/server.js +92 -98
  133. package/lib/sdam/server.js.map +1 -1
  134. package/lib/sdam/server_selection.js +13 -5
  135. package/lib/sdam/server_selection.js.map +1 -1
  136. package/lib/sdam/server_selection_events.js +85 -0
  137. package/lib/sdam/server_selection_events.js.map +1 -0
  138. package/lib/sdam/topology.js +61 -18
  139. package/lib/sdam/topology.js.map +1 -1
  140. package/lib/sessions.js +11 -9
  141. package/lib/sessions.js.map +1 -1
  142. package/lib/utils.js +25 -17
  143. package/lib/utils.js.map +1 -1
  144. package/mongodb.d.ts +131 -57
  145. package/package.json +28 -27
  146. package/src/bson.ts +2 -0
  147. package/src/bulk/common.ts +29 -22
  148. package/src/change_stream.ts +11 -5
  149. package/src/client-side-encryption/state_machine.ts +77 -62
  150. package/src/cmap/auth/auth_provider.ts +4 -0
  151. package/src/cmap/auth/gssapi.ts +1 -1
  152. package/src/cmap/auth/mongo_credentials.ts +2 -2
  153. package/src/cmap/auth/mongocr.ts +2 -6
  154. package/src/cmap/auth/mongodb_aws.ts +69 -64
  155. package/src/cmap/auth/mongodb_oidc/callback_workflow.ts +2 -2
  156. package/src/cmap/auth/mongodb_oidc/service_workflow.ts +1 -1
  157. package/src/cmap/auth/plain.ts +1 -1
  158. package/src/cmap/auth/scram.ts +7 -9
  159. package/src/cmap/auth/x509.ts +1 -5
  160. package/src/cmap/command_monitoring_events.ts +32 -4
  161. package/src/cmap/commands.ts +4 -0
  162. package/src/cmap/connect.ts +119 -159
  163. package/src/cmap/connection.ts +408 -961
  164. package/src/cmap/connection_pool.ts +93 -155
  165. package/src/cmap/stream_description.ts +21 -1
  166. package/src/cmap/wire_protocol/compression.ts +1 -2
  167. package/src/cmap/wire_protocol/on_data.ts +132 -0
  168. package/src/collection.ts +14 -6
  169. package/src/connection_string.ts +49 -3
  170. package/src/constants.ts +20 -0
  171. package/src/cursor/abstract_cursor.ts +7 -1
  172. package/src/error.ts +9 -9
  173. package/src/index.ts +33 -12
  174. package/src/mongo_client.ts +20 -7
  175. package/src/mongo_client_auth_providers.ts +54 -0
  176. package/src/mongo_logger.ts +376 -50
  177. package/src/mongo_types.ts +58 -3
  178. package/src/operations/aggregate.ts +4 -0
  179. package/src/operations/bulk_write.ts +4 -0
  180. package/src/operations/collections.ts +4 -0
  181. package/src/operations/command.ts +1 -1
  182. package/src/operations/count.ts +4 -0
  183. package/src/operations/create_collection.ts +4 -0
  184. package/src/operations/delete.ts +4 -0
  185. package/src/operations/distinct.ts +4 -0
  186. package/src/operations/drop.ts +8 -0
  187. package/src/operations/estimated_document_count.ts +4 -0
  188. package/src/operations/execute_operation.ts +28 -31
  189. package/src/operations/find.ts +5 -1
  190. package/src/operations/find_and_modify.ts +7 -5
  191. package/src/operations/get_more.ts +4 -1
  192. package/src/operations/indexes.ts +29 -0
  193. package/src/operations/insert.ts +8 -0
  194. package/src/operations/is_capped.ts +4 -0
  195. package/src/operations/kill_cursors.ts +5 -1
  196. package/src/operations/list_collections.ts +4 -0
  197. package/src/operations/list_databases.ts +4 -0
  198. package/src/operations/operation.ts +4 -0
  199. package/src/operations/options_operation.ts +3 -0
  200. package/src/operations/profiling_level.ts +4 -0
  201. package/src/operations/remove_user.ts +4 -0
  202. package/src/operations/rename.ts +4 -0
  203. package/src/operations/run_command.ts +10 -2
  204. package/src/operations/search_indexes/create.ts +5 -1
  205. package/src/operations/search_indexes/drop.ts +5 -1
  206. package/src/operations/search_indexes/update.ts +5 -1
  207. package/src/operations/set_profiling_level.ts +4 -0
  208. package/src/operations/stats.ts +4 -0
  209. package/src/operations/update.ts +4 -0
  210. package/src/operations/validate_collection.ts +4 -0
  211. package/src/sdam/events.ts +31 -3
  212. package/src/sdam/monitor.ts +119 -97
  213. package/src/sdam/server.ts +113 -152
  214. package/src/sdam/server_selection.ts +21 -13
  215. package/src/sdam/server_selection_events.ts +142 -0
  216. package/src/sdam/topology.ts +200 -46
  217. package/src/sessions.ts +33 -32
  218. package/src/utils.ts +28 -32
  219. package/lib/cmap/message_stream.js +0 -149
  220. package/lib/cmap/message_stream.js.map +0 -1
  221. package/src/cmap/message_stream.ts +0 -220
@@ -23,9 +23,11 @@ import {
23
23
  ServerApiVersion
24
24
  } from './mongo_client';
25
25
  import {
26
+ MongoLoggableComponent,
26
27
  MongoLogger,
27
28
  type MongoLoggerEnvOptions,
28
- type MongoLoggerMongoClientOptions
29
+ type MongoLoggerMongoClientOptions,
30
+ SeverityLevel
29
31
  } from './mongo_logger';
30
32
  import { ReadConcern, type ReadConcernLevel } from './read_concern';
31
33
  import { ReadPreference, type ReadPreferenceMode } from './read_preference';
@@ -1090,6 +1092,9 @@ export const OPTIONS = {
1090
1092
  target: 'tls',
1091
1093
  type: 'boolean'
1092
1094
  },
1095
+ timeoutMS: {
1096
+ type: 'uint'
1097
+ },
1093
1098
  tls: {
1094
1099
  type: 'boolean'
1095
1100
  },
@@ -1243,12 +1248,53 @@ export const OPTIONS = {
1243
1248
  * @internal
1244
1249
  * TODO: NODE-5671 - remove internal flag
1245
1250
  */
1246
- mongodbLogPath: { type: 'any' },
1251
+ mongodbLogPath: {
1252
+ transform({ values: [value] }) {
1253
+ if (
1254
+ !(
1255
+ (typeof value === 'string' && ['stderr', 'stdout'].includes(value)) ||
1256
+ (value &&
1257
+ typeof value === 'object' &&
1258
+ 'write' in value &&
1259
+ typeof value.write === 'function')
1260
+ )
1261
+ ) {
1262
+ throw new MongoAPIError(
1263
+ `Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable`
1264
+ );
1265
+ }
1266
+ return value;
1267
+ }
1268
+ },
1247
1269
  /**
1248
1270
  * @internal
1249
1271
  * TODO: NODE-5671 - remove internal flag
1250
1272
  */
1251
- mongodbLogComponentSeverities: { type: 'any' },
1273
+ mongodbLogComponentSeverities: {
1274
+ transform({ values: [value] }) {
1275
+ if (typeof value !== 'object' || !value) {
1276
+ throw new MongoAPIError(`Option 'mongodbLogComponentSeverities' must be a non-null object`);
1277
+ }
1278
+ for (const [k, v] of Object.entries(value)) {
1279
+ if (typeof v !== 'string' || typeof k !== 'string') {
1280
+ throw new MongoAPIError(
1281
+ `User input for option 'mongodbLogComponentSeverities' object cannot include a non-string key or value`
1282
+ );
1283
+ }
1284
+ if (!Object.values(MongoLoggableComponent).some(val => val === k) && k !== 'default') {
1285
+ throw new MongoAPIError(
1286
+ `User input for option 'mongodbLogComponentSeverities' contains invalid key: ${k}`
1287
+ );
1288
+ }
1289
+ if (!Object.values(SeverityLevel).some(val => val === v)) {
1290
+ throw new MongoAPIError(
1291
+ `Option 'mongodbLogComponentSeverities' does not support ${v} as a value for ${k}`
1292
+ );
1293
+ }
1294
+ }
1295
+ return value;
1296
+ }
1297
+ },
1252
1298
  /**
1253
1299
  * @internal
1254
1300
  * TODO: NODE-5671 - remove internal flag
package/src/constants.ts CHANGED
@@ -17,13 +17,27 @@ export const MESSAGE = 'message' as const;
17
17
  export const PINNED = 'pinned' as const;
18
18
  export const UNPINNED = 'unpinned' as const;
19
19
  export const DESCRIPTION_RECEIVED = 'descriptionReceived';
20
+ /** @internal */
20
21
  export const SERVER_OPENING = 'serverOpening' as const;
22
+ /** @internal */
21
23
  export const SERVER_CLOSED = 'serverClosed' as const;
24
+ /** @internal */
22
25
  export const SERVER_DESCRIPTION_CHANGED = 'serverDescriptionChanged' as const;
26
+ /** @internal */
23
27
  export const TOPOLOGY_OPENING = 'topologyOpening' as const;
28
+ /** @internal */
24
29
  export const TOPOLOGY_CLOSED = 'topologyClosed' as const;
30
+ /** @internal */
25
31
  export const TOPOLOGY_DESCRIPTION_CHANGED = 'topologyDescriptionChanged' as const;
26
32
  /** @internal */
33
+ export const SERVER_SELECTION_STARTED = 'serverSelectionStarted' as const;
34
+ /** @internal */
35
+ export const SERVER_SELECTION_FAILED = 'serverSelectionFailed' as const;
36
+ /** @internal */
37
+ export const SERVER_SELECTION_SUCCEEDED = 'serverSelectionSucceeded' as const;
38
+ /** @internal */
39
+ export const WAITING_FOR_SUITABLE_SERVER = 'waitingForSuitableServer' as const;
40
+ /** @internal */
27
41
  export const CONNECTION_POOL_CREATED = 'connectionPoolCreated' as const;
28
42
  /** @internal */
29
43
  export const CONNECTION_POOL_CLOSED = 'connectionPoolClosed' as const;
@@ -46,11 +60,17 @@ export const CONNECTION_CHECKED_OUT = 'connectionCheckedOut' as const;
46
60
  /** @internal */
47
61
  export const CONNECTION_CHECKED_IN = 'connectionCheckedIn' as const;
48
62
  export const CLUSTER_TIME_RECEIVED = 'clusterTimeReceived' as const;
63
+ /** @internal */
49
64
  export const COMMAND_STARTED = 'commandStarted' as const;
65
+ /** @internal */
50
66
  export const COMMAND_SUCCEEDED = 'commandSucceeded' as const;
67
+ /** @internal */
51
68
  export const COMMAND_FAILED = 'commandFailed' as const;
69
+ /** @internal */
52
70
  export const SERVER_HEARTBEAT_STARTED = 'serverHeartbeatStarted' as const;
71
+ /** @internal */
53
72
  export const SERVER_HEARTBEAT_SUCCEEDED = 'serverHeartbeatSucceeded' as const;
73
+ /** @internal */
54
74
  export const SERVER_HEARTBEAT_FAILED = 'serverHeartbeatFailed' as const;
55
75
  export const RESPONSE = 'response' as const;
56
76
  export const MORE = 'more' as const;
@@ -337,7 +337,7 @@ export abstract class AbstractCursor<
337
337
  const transform = options.transform;
338
338
  const readable = new ReadableCursorStream(this);
339
339
 
340
- return readable.pipe(
340
+ const transformedStream = readable.pipe(
341
341
  new Transform({
342
342
  objectMode: true,
343
343
  highWaterMark: 1,
@@ -351,6 +351,12 @@ export abstract class AbstractCursor<
351
351
  }
352
352
  })
353
353
  );
354
+
355
+ // Bubble errors to transformed stream, because otherwise no way
356
+ // to handle this error.
357
+ readable.on('error', err => transformedStream.emit('error', err));
358
+
359
+ return transformedStream;
354
360
  }
355
361
 
356
362
  return new ReadableCursorStream(this);
package/src/error.ts CHANGED
@@ -109,8 +109,8 @@ export interface ErrorDescription extends Document {
109
109
  errInfo?: Document;
110
110
  }
111
111
 
112
- function isAggregateError(e: Error): e is Error & { errors: Error[] } {
113
- return 'errors' in e && Array.isArray(e.errors);
112
+ function isAggregateError(e: unknown): e is Error & { errors: Error[] } {
113
+ return e != null && typeof e === 'object' && 'errors' in e && Array.isArray(e.errors);
114
114
  }
115
115
 
116
116
  /**
@@ -150,7 +150,7 @@ export class MongoError extends Error {
150
150
  }
151
151
 
152
152
  /** @internal */
153
- static buildErrorMessage(e: Error | string): string {
153
+ static buildErrorMessage(e: unknown): string {
154
154
  if (typeof e === 'string') {
155
155
  return e;
156
156
  }
@@ -160,7 +160,9 @@ export class MongoError extends Error {
160
160
  : e.errors.map(({ message }) => message).join(', ');
161
161
  }
162
162
 
163
- return e.message;
163
+ return e != null && typeof e === 'object' && 'message' in e && typeof e.message === 'string'
164
+ ? e.message
165
+ : 'empty error message';
164
166
  }
165
167
 
166
168
  override get name(): string {
@@ -1145,14 +1147,12 @@ const RETRYABLE_READ_ERROR_CODES = new Set<number>([
1145
1147
  MONGODB_ERROR_CODES.InterruptedAtShutdown,
1146
1148
  MONGODB_ERROR_CODES.InterruptedDueToReplStateChange,
1147
1149
  MONGODB_ERROR_CODES.NotPrimaryNoSecondaryOk,
1148
- MONGODB_ERROR_CODES.NotPrimaryOrSecondary
1150
+ MONGODB_ERROR_CODES.NotPrimaryOrSecondary,
1151
+ MONGODB_ERROR_CODES.ExceededTimeLimit
1149
1152
  ]);
1150
1153
 
1151
1154
  // see: https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst#terms
1152
- const RETRYABLE_WRITE_ERROR_CODES = new Set<number>([
1153
- ...RETRYABLE_READ_ERROR_CODES,
1154
- MONGODB_ERROR_CODES.ExceededTimeLimit
1155
- ]);
1155
+ const RETRYABLE_WRITE_ERROR_CODES = RETRYABLE_READ_ERROR_CODES;
1156
1156
 
1157
1157
  export function needsRetryableWriteLabel(error: Error, maxWireVersion: number): boolean {
1158
1158
  // pre-4.4 server, then the driver adds an error label for every valid case
package/src/index.ts CHANGED
@@ -147,6 +147,13 @@ export {
147
147
  TopologyDescriptionChangedEvent,
148
148
  TopologyOpeningEvent
149
149
  } from './sdam/events';
150
+ export {
151
+ ServerSelectionEvent,
152
+ ServerSelectionFailedEvent,
153
+ ServerSelectionStartedEvent,
154
+ ServerSelectionSucceededEvent,
155
+ WaitingForSuitableServerEvent
156
+ } from './sdam/server_selection_events';
150
157
  export { SrvPollingEvent } from './sdam/srv_polling';
151
158
 
152
159
  // type only exports below, these are removed from emitted JS
@@ -236,7 +243,7 @@ export type {
236
243
  CSFLEKMSTlsOptions,
237
244
  StateMachineExecutable
238
245
  } from './client-side-encryption/state_machine';
239
- export type { AuthContext } from './cmap/auth/auth_provider';
246
+ export type { AuthContext, AuthProvider } from './cmap/auth/auth_provider';
240
247
  export type {
241
248
  AuthMechanismProperties,
242
249
  MongoCredentials,
@@ -261,6 +268,7 @@ export type {
261
268
  OpResponseOptions,
262
269
  WriteProtocolMessageType
263
270
  } from './cmap/commands';
271
+ export type { HandshakeDocument } from './cmap/connect';
264
272
  export type { LEGAL_TCP_SOCKET_OPTIONS, LEGAL_TLS_SOCKET_OPTIONS, Stream } from './cmap/connect';
265
273
  export type {
266
274
  CommandOptions,
@@ -268,11 +276,7 @@ export type {
268
276
  ConnectionEvents,
269
277
  ConnectionOptions,
270
278
  DestroyOptions,
271
- ModernConnection,
272
- ProxyOptions,
273
- read,
274
- readMany,
275
- writeCommand
279
+ ProxyOptions
276
280
  } from './cmap/connection';
277
281
  export type {
278
282
  CloseOptions,
@@ -284,16 +288,14 @@ export type {
284
288
  WithConnectionCallback
285
289
  } from './cmap/connection_pool';
286
290
  export type { ClientMetadata, ClientMetadataOptions } from './cmap/handshake/client_metadata';
287
- export type {
288
- MessageStream,
289
- MessageStreamOptions,
290
- OperationDescription
291
- } from './cmap/message_stream';
292
291
  export type { ConnectionPoolMetrics } from './cmap/metrics';
293
292
  export type { StreamDescription, StreamDescriptionOptions } from './cmap/stream_description';
294
293
  export type { CompressorName } from './cmap/wire_protocol/compression';
295
294
  export type { CollectionOptions, CollectionPrivate, ModifyResult } from './collection';
296
295
  export type {
296
+ COMMAND_FAILED,
297
+ COMMAND_STARTED,
298
+ COMMAND_SUCCEEDED,
297
299
  CONNECTION_CHECK_OUT_FAILED,
298
300
  CONNECTION_CHECK_OUT_STARTED,
299
301
  CONNECTION_CHECKED_IN,
@@ -305,7 +307,20 @@ export type {
305
307
  CONNECTION_POOL_CREATED,
306
308
  CONNECTION_POOL_READY,
307
309
  CONNECTION_READY,
308
- MONGO_CLIENT_EVENTS
310
+ MONGO_CLIENT_EVENTS,
311
+ SERVER_CLOSED,
312
+ SERVER_DESCRIPTION_CHANGED,
313
+ SERVER_HEARTBEAT_FAILED,
314
+ SERVER_HEARTBEAT_STARTED,
315
+ SERVER_HEARTBEAT_SUCCEEDED,
316
+ SERVER_OPENING,
317
+ SERVER_SELECTION_FAILED,
318
+ SERVER_SELECTION_STARTED,
319
+ SERVER_SELECTION_SUCCEEDED,
320
+ TOPOLOGY_CLOSED,
321
+ TOPOLOGY_DESCRIPTION_CHANGED,
322
+ TOPOLOGY_OPENING,
323
+ WAITING_FOR_SUITABLE_SERVER
309
324
  } from './constants';
310
325
  export type {
311
326
  AbstractCursorEvents,
@@ -351,12 +366,18 @@ export type {
351
366
  SupportedTLSSocketOptions,
352
367
  WithSessionCallback
353
368
  } from './mongo_client';
369
+ export { MongoClientAuthProviders } from './mongo_client_auth_providers';
354
370
  export type {
355
371
  Log,
356
372
  LogComponentSeveritiesClientOptions,
357
373
  LogConvertible,
358
374
  Loggable,
375
+ LoggableCommandFailedEvent,
376
+ LoggableCommandSucceededEvent,
359
377
  LoggableEvent,
378
+ LoggableServerHeartbeatFailedEvent,
379
+ LoggableServerHeartbeatStartedEvent,
380
+ LoggableServerHeartbeatSucceededEvent,
360
381
  MongoDBLogWritable,
361
382
  MongoLoggableComponent,
362
383
  MongoLogger,
@@ -21,11 +21,13 @@ import { MONGO_CLIENT_EVENTS } from './constants';
21
21
  import { Db, type DbOptions } from './db';
22
22
  import type { Encrypter } from './encrypter';
23
23
  import { MongoInvalidArgumentError } from './error';
24
+ import { MongoClientAuthProviders } from './mongo_client_auth_providers';
24
25
  import {
25
26
  type LogComponentSeveritiesClientOptions,
26
27
  type MongoDBLogWritable,
27
28
  MongoLogger,
28
- type MongoLoggerOptions
29
+ type MongoLoggerOptions,
30
+ SeverityLevel
29
31
  } from './mongo_logger';
30
32
  import { TypedEventEmitter } from './mongo_types';
31
33
  import { executeOperation } from './operations/execute_operation';
@@ -118,6 +120,8 @@ export type SupportedNodeConnectionOptions = SupportedTLSConnectionOptions &
118
120
  export interface MongoClientOptions extends BSONSerializeOptions, SupportedNodeConnectionOptions {
119
121
  /** Specifies the name of the replica set, if the mongod is a member of a replica set. */
120
122
  replicaSet?: string;
123
+ /** @internal This option is in development and currently has no behaviour. */
124
+ timeoutMS?: number;
121
125
  /** Enables or disables TLS/SSL for the connection. */
122
126
  tls?: boolean;
123
127
  /** A boolean to enable or disables TLS/SSL for the connection. (The ssl option is equivalent to the tls option.) */
@@ -294,6 +298,7 @@ export interface MongoClientPrivate {
294
298
  bsonOptions: BSONSerializeOptions;
295
299
  namespace: MongoDBNamespace;
296
300
  hasBeenClosed: boolean;
301
+ authProviders: MongoClientAuthProviders;
297
302
  /**
298
303
  * We keep a reference to the sessions that are acquired from the pool.
299
304
  * - used to track and close all sessions in client.close() (which is non-standard behavior)
@@ -316,6 +321,7 @@ export type MongoClientEvents = Pick<TopologyEvents, (typeof MONGO_CLIENT_EVENTS
316
321
  };
317
322
 
318
323
  /** @internal */
324
+
319
325
  const kOptions = Symbol('options');
320
326
 
321
327
  /**
@@ -343,7 +349,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
343
349
  /** @internal */
344
350
  topology?: Topology;
345
351
  /** @internal */
346
- override readonly mongoLogger: MongoLogger;
352
+ override readonly mongoLogger: MongoLogger | undefined;
347
353
  /** @internal */
348
354
  private connectionLock?: Promise<this>;
349
355
 
@@ -357,7 +363,13 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
357
363
  super();
358
364
 
359
365
  this[kOptions] = parseOptions(url, this, options);
360
- this.mongoLogger = new MongoLogger(this[kOptions].mongoLoggerOptions);
366
+
367
+ const shouldSetLogger = Object.values(
368
+ this[kOptions].mongoLoggerOptions.componentSeverities
369
+ ).some(value => value !== SeverityLevel.OFF);
370
+ this.mongoLogger = shouldSetLogger
371
+ ? new MongoLogger(this[kOptions].mongoLoggerOptions)
372
+ : undefined;
361
373
 
362
374
  // eslint-disable-next-line @typescript-eslint/no-this-alias
363
375
  const client = this;
@@ -370,6 +382,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
370
382
  hasBeenClosed: false,
371
383
  sessionPool: new ServerSessionPool(this),
372
384
  activeSessions: new Set(),
385
+ authProviders: new MongoClientAuthProviders(),
373
386
 
374
387
  get options() {
375
388
  return client[kOptions];
@@ -403,9 +416,9 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> {
403
416
  const srvHostIsCosmosDB = isHostMatch(COSMOS_DB_CHECK, this[kOptions].srvHost);
404
417
 
405
418
  if (documentDBHostnames.length !== 0 || srvHostIsDocumentDB) {
406
- this.mongoLogger.info('client', DOCUMENT_DB_MSG);
419
+ this.mongoLogger?.info('client', DOCUMENT_DB_MSG);
407
420
  } else if (cosmosDBHostnames.length !== 0 || srvHostIsCosmosDB) {
408
- this.mongoLogger.info('client', COSMOS_DB_MSG);
421
+ this.mongoLogger?.info('client', COSMOS_DB_MSG);
409
422
  }
410
423
  }
411
424
 
@@ -820,10 +833,10 @@ export interface MongoOptions
820
833
  proxyUsername?: string;
821
834
  proxyPassword?: string;
822
835
  serverMonitoringMode: ServerMonitoringMode;
823
-
824
836
  /** @internal */
825
837
  connectionType?: typeof Connection;
826
-
838
+ /** @internal */
839
+ authProviders: MongoClientAuthProviders;
827
840
  /** @internal */
828
841
  encrypter: Encrypter;
829
842
  /** @internal */
@@ -0,0 +1,54 @@
1
+ import { type AuthProvider } from './cmap/auth/auth_provider';
2
+ import { GSSAPI } from './cmap/auth/gssapi';
3
+ import { MongoCR } from './cmap/auth/mongocr';
4
+ import { MongoDBAWS } from './cmap/auth/mongodb_aws';
5
+ import { MongoDBOIDC } from './cmap/auth/mongodb_oidc';
6
+ import { Plain } from './cmap/auth/plain';
7
+ import { AuthMechanism } from './cmap/auth/providers';
8
+ import { ScramSHA1, ScramSHA256 } from './cmap/auth/scram';
9
+ import { X509 } from './cmap/auth/x509';
10
+ import { MongoInvalidArgumentError } from './error';
11
+
12
+ /** @internal */
13
+ const AUTH_PROVIDERS = new Map<AuthMechanism | string, () => AuthProvider>([
14
+ [AuthMechanism.MONGODB_AWS, () => new MongoDBAWS()],
15
+ [AuthMechanism.MONGODB_CR, () => new MongoCR()],
16
+ [AuthMechanism.MONGODB_GSSAPI, () => new GSSAPI()],
17
+ [AuthMechanism.MONGODB_OIDC, () => new MongoDBOIDC()],
18
+ [AuthMechanism.MONGODB_PLAIN, () => new Plain()],
19
+ [AuthMechanism.MONGODB_SCRAM_SHA1, () => new ScramSHA1()],
20
+ [AuthMechanism.MONGODB_SCRAM_SHA256, () => new ScramSHA256()],
21
+ [AuthMechanism.MONGODB_X509, () => new X509()]
22
+ ]);
23
+
24
+ /**
25
+ * Create a set of providers per client
26
+ * to avoid sharing the provider's cache between different clients.
27
+ * @internal
28
+ */
29
+ export class MongoClientAuthProviders {
30
+ private existingProviders: Map<AuthMechanism | string, AuthProvider> = new Map();
31
+
32
+ /**
33
+ * Get or create an authentication provider based on the provided mechanism.
34
+ * We don't want to create all providers at once, as some providers may not be used.
35
+ * @param name - The name of the provider to get or create.
36
+ * @returns The provider.
37
+ * @throws MongoInvalidArgumentError if the mechanism is not supported.
38
+ * @internal
39
+ */
40
+ getOrCreateProvider(name: AuthMechanism | string): AuthProvider {
41
+ const authProvider = this.existingProviders.get(name);
42
+ if (authProvider) {
43
+ return authProvider;
44
+ }
45
+
46
+ const provider = AUTH_PROVIDERS.get(name)?.();
47
+ if (!provider) {
48
+ throw new MongoInvalidArgumentError(`authMechanism ${name} not supported`);
49
+ }
50
+
51
+ this.existingProviders.set(name, provider);
52
+ return provider;
53
+ }
54
+ }