mongodb 4.3.1 → 4.5.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 (198) hide show
  1. package/README.md +7 -6
  2. package/lib/admin.js +5 -6
  3. package/lib/admin.js.map +1 -1
  4. package/lib/bulk/common.js +31 -7
  5. package/lib/bulk/common.js.map +1 -1
  6. package/lib/bulk/unordered.js.map +1 -1
  7. package/lib/change_stream.js +29 -20
  8. package/lib/change_stream.js.map +1 -1
  9. package/lib/cmap/auth/gssapi.js +49 -7
  10. package/lib/cmap/auth/gssapi.js.map +1 -1
  11. package/lib/cmap/auth/mongo_credentials.js +12 -1
  12. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  13. package/lib/cmap/auth/mongocr.js.map +1 -1
  14. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  15. package/lib/cmap/auth/plain.js.map +1 -1
  16. package/lib/cmap/auth/scram.js +1 -0
  17. package/lib/cmap/auth/scram.js.map +1 -1
  18. package/lib/cmap/auth/x509.js.map +1 -1
  19. package/lib/cmap/commands.js.map +1 -1
  20. package/lib/cmap/connect.js +0 -6
  21. package/lib/cmap/connect.js.map +1 -1
  22. package/lib/cmap/connection.js +111 -86
  23. package/lib/cmap/connection.js.map +1 -1
  24. package/lib/cmap/errors.js.map +1 -1
  25. package/lib/cmap/message_stream.js.map +1 -1
  26. package/lib/cmap/stream_description.js +3 -0
  27. package/lib/cmap/stream_description.js.map +1 -1
  28. package/lib/collection.js +29 -28
  29. package/lib/collection.js.map +1 -1
  30. package/lib/connection_string.js +53 -40
  31. package/lib/connection_string.js.map +1 -1
  32. package/lib/cursor/abstract_cursor.js +64 -42
  33. package/lib/cursor/abstract_cursor.js.map +1 -1
  34. package/lib/cursor/aggregation_cursor.js +2 -2
  35. package/lib/cursor/aggregation_cursor.js.map +1 -1
  36. package/lib/cursor/find_cursor.js +4 -3
  37. package/lib/cursor/find_cursor.js.map +1 -1
  38. package/lib/db.js +13 -13
  39. package/lib/db.js.map +1 -1
  40. package/lib/encrypter.js +17 -9
  41. package/lib/encrypter.js.map +1 -1
  42. package/lib/error.js +99 -48
  43. package/lib/error.js.map +1 -1
  44. package/lib/gridfs/download.js +2 -0
  45. package/lib/gridfs/download.js.map +1 -1
  46. package/lib/gridfs/index.js +42 -51
  47. package/lib/gridfs/index.js.map +1 -1
  48. package/lib/gridfs/upload.js.map +1 -1
  49. package/lib/index.js +9 -2
  50. package/lib/index.js.map +1 -1
  51. package/lib/mongo_client.js +14 -27
  52. package/lib/mongo_client.js.map +1 -1
  53. package/lib/operations/add_user.js +8 -1
  54. package/lib/operations/add_user.js.map +1 -1
  55. package/lib/operations/aggregate.js +5 -0
  56. package/lib/operations/aggregate.js.map +1 -1
  57. package/lib/operations/bulk_write.js.map +1 -1
  58. package/lib/operations/collections.js.map +1 -1
  59. package/lib/operations/command.js.map +1 -1
  60. package/lib/operations/common_functions.js +8 -1
  61. package/lib/operations/common_functions.js.map +1 -1
  62. package/lib/operations/count.js.map +1 -1
  63. package/lib/operations/count_documents.js.map +1 -1
  64. package/lib/operations/create_collection.js.map +1 -1
  65. package/lib/operations/delete.js +5 -3
  66. package/lib/operations/delete.js.map +1 -1
  67. package/lib/operations/distinct.js.map +1 -1
  68. package/lib/operations/drop.js.map +1 -1
  69. package/lib/operations/estimated_document_count.js.map +1 -1
  70. package/lib/operations/eval.js.map +1 -1
  71. package/lib/operations/execute_operation.js +70 -79
  72. package/lib/operations/execute_operation.js.map +1 -1
  73. package/lib/operations/find.js +3 -1
  74. package/lib/operations/find.js.map +1 -1
  75. package/lib/operations/find_and_modify.js +5 -0
  76. package/lib/operations/find_and_modify.js.map +1 -1
  77. package/lib/operations/get_more.js +5 -0
  78. package/lib/operations/get_more.js.map +1 -1
  79. package/lib/operations/indexes.js +8 -9
  80. package/lib/operations/indexes.js.map +1 -1
  81. package/lib/operations/insert.js +3 -1
  82. package/lib/operations/insert.js.map +1 -1
  83. package/lib/operations/is_capped.js.map +1 -1
  84. package/lib/operations/list_collections.js +10 -42
  85. package/lib/operations/list_collections.js.map +1 -1
  86. package/lib/operations/list_databases.js +5 -0
  87. package/lib/operations/list_databases.js.map +1 -1
  88. package/lib/operations/map_reduce.js +1 -2
  89. package/lib/operations/map_reduce.js.map +1 -1
  90. package/lib/operations/operation.js +1 -3
  91. package/lib/operations/operation.js.map +1 -1
  92. package/lib/operations/options_operation.js.map +1 -1
  93. package/lib/operations/profiling_level.js.map +1 -1
  94. package/lib/operations/remove_user.js.map +1 -1
  95. package/lib/operations/rename.js +1 -1
  96. package/lib/operations/rename.js.map +1 -1
  97. package/lib/operations/run_command.js.map +1 -1
  98. package/lib/operations/set_profiling_level.js.map +1 -1
  99. package/lib/operations/stats.js.map +1 -1
  100. package/lib/operations/update.js +5 -0
  101. package/lib/operations/update.js.map +1 -1
  102. package/lib/operations/validate_collection.js.map +1 -1
  103. package/lib/read_concern.js +1 -0
  104. package/lib/read_concern.js.map +1 -1
  105. package/lib/sdam/common.js +1 -7
  106. package/lib/sdam/common.js.map +1 -1
  107. package/lib/sdam/events.js +1 -1
  108. package/lib/sdam/events.js.map +1 -1
  109. package/lib/sdam/monitor.js +1 -2
  110. package/lib/sdam/monitor.js.map +1 -1
  111. package/lib/sdam/server.js +79 -57
  112. package/lib/sdam/server.js.map +1 -1
  113. package/lib/sdam/topology.js +16 -33
  114. package/lib/sdam/topology.js.map +1 -1
  115. package/lib/sdam/topology_description.js +1 -3
  116. package/lib/sdam/topology_description.js.map +1 -1
  117. package/lib/sessions.js +93 -68
  118. package/lib/sessions.js.map +1 -1
  119. package/lib/utils.js +21 -97
  120. package/lib/utils.js.map +1 -1
  121. package/mongodb.d.ts +188 -29
  122. package/package.json +46 -46
  123. package/src/admin.ts +6 -10
  124. package/src/bulk/common.ts +42 -14
  125. package/src/bulk/unordered.ts +1 -1
  126. package/src/change_stream.ts +58 -42
  127. package/src/cmap/auth/gssapi.ts +58 -7
  128. package/src/cmap/auth/mongo_credentials.ts +17 -2
  129. package/src/cmap/auth/mongocr.ts +1 -1
  130. package/src/cmap/auth/mongodb_aws.ts +1 -1
  131. package/src/cmap/auth/plain.ts +1 -1
  132. package/src/cmap/auth/scram.ts +3 -2
  133. package/src/cmap/auth/x509.ts +6 -2
  134. package/src/cmap/commands.ts +3 -0
  135. package/src/cmap/connect.ts +2 -20
  136. package/src/cmap/connection.ts +162 -111
  137. package/src/cmap/errors.ts +2 -2
  138. package/src/cmap/message_stream.ts +2 -2
  139. package/src/cmap/stream_description.ts +4 -1
  140. package/src/collection.ts +37 -33
  141. package/src/connection_string.ts +77 -45
  142. package/src/cursor/abstract_cursor.ts +85 -56
  143. package/src/cursor/aggregation_cursor.ts +5 -5
  144. package/src/cursor/find_cursor.ts +19 -11
  145. package/src/db.ts +15 -19
  146. package/src/deps.ts +52 -0
  147. package/src/encrypter.ts +18 -10
  148. package/src/error.ts +145 -76
  149. package/src/gridfs/download.ts +3 -1
  150. package/src/gridfs/index.ts +51 -68
  151. package/src/gridfs/upload.ts +12 -12
  152. package/src/index.ts +10 -1
  153. package/src/mongo_client.ts +19 -41
  154. package/src/operations/add_user.ts +14 -3
  155. package/src/operations/aggregate.ts +15 -5
  156. package/src/operations/bulk_write.ts +6 -2
  157. package/src/operations/collections.ts +6 -2
  158. package/src/operations/command.ts +23 -8
  159. package/src/operations/common_functions.ts +8 -1
  160. package/src/operations/count.ts +6 -2
  161. package/src/operations/count_documents.ts +5 -1
  162. package/src/operations/create_collection.ts +6 -2
  163. package/src/operations/delete.ts +19 -13
  164. package/src/operations/distinct.ts +6 -2
  165. package/src/operations/drop.ts +12 -4
  166. package/src/operations/estimated_document_count.ts +11 -3
  167. package/src/operations/eval.ts +6 -2
  168. package/src/operations/execute_operation.ts +102 -101
  169. package/src/operations/find.ts +9 -5
  170. package/src/operations/find_and_modify.ts +21 -2
  171. package/src/operations/get_more.ts +20 -6
  172. package/src/operations/indexes.ts +54 -36
  173. package/src/operations/insert.ts +20 -6
  174. package/src/operations/is_capped.ts +6 -2
  175. package/src/operations/list_collections.ts +24 -59
  176. package/src/operations/list_databases.ts +13 -3
  177. package/src/operations/map_reduce.ts +7 -6
  178. package/src/operations/operation.ts +10 -9
  179. package/src/operations/options_operation.ts +6 -2
  180. package/src/operations/profiling_level.ts +6 -2
  181. package/src/operations/remove_user.ts +6 -2
  182. package/src/operations/rename.ts +7 -3
  183. package/src/operations/run_command.ts +6 -2
  184. package/src/operations/set_profiling_level.ts +6 -2
  185. package/src/operations/stats.ts +12 -4
  186. package/src/operations/update.ts +21 -9
  187. package/src/operations/validate_collection.ts +6 -2
  188. package/src/read_concern.ts +1 -0
  189. package/src/sdam/common.ts +0 -6
  190. package/src/sdam/events.ts +2 -2
  191. package/src/sdam/monitor.ts +4 -5
  192. package/src/sdam/server.ts +95 -90
  193. package/src/sdam/topology.ts +9 -53
  194. package/src/sdam/topology_description.ts +1 -3
  195. package/src/sessions.ts +108 -78
  196. package/src/utils.ts +38 -118
  197. package/tsconfig.json +40 -0
  198. package/mongodb.ts34.d.ts +0 -5649
package/src/collection.ts CHANGED
@@ -21,7 +21,7 @@ import type {
21
21
  import type { AggregateOptions } from './operations/aggregate';
22
22
  import { BulkWriteOperation } from './operations/bulk_write';
23
23
  import type { IndexInformationOptions } from './operations/common_functions';
24
- import type { CountOptions } from './operations/count';
24
+ import { CountOperation, CountOptions } from './operations/count';
25
25
  import { CountDocumentsOperation, CountDocumentsOptions } from './operations/count_documents';
26
26
  import {
27
27
  DeleteManyOperation,
@@ -296,7 +296,7 @@ export class Collection<TSchema extends Document = Document> {
296
296
  }
297
297
 
298
298
  return executeOperation(
299
- getTopology(this),
299
+ this,
300
300
  new InsertOneOperation(
301
301
  this as TODO_NODE_3286,
302
302
  doc,
@@ -338,7 +338,7 @@ export class Collection<TSchema extends Document = Document> {
338
338
  options = options ? Object.assign({}, options) : { ordered: true };
339
339
 
340
340
  return executeOperation(
341
- getTopology(this),
341
+ this,
342
342
  new InsertManyOperation(
343
343
  this as TODO_NODE_3286,
344
344
  docs,
@@ -406,7 +406,7 @@ export class Collection<TSchema extends Document = Document> {
406
406
  }
407
407
 
408
408
  return executeOperation(
409
- getTopology(this),
409
+ this,
410
410
  new BulkWriteOperation(
411
411
  this as TODO_NODE_3286,
412
412
  operations as TODO_NODE_3286,
@@ -453,7 +453,7 @@ export class Collection<TSchema extends Document = Document> {
453
453
  if (typeof options === 'function') (callback = options), (options = {});
454
454
 
455
455
  return executeOperation(
456
- getTopology(this),
456
+ this,
457
457
  new UpdateOneOperation(
458
458
  this as TODO_NODE_3286,
459
459
  filter,
@@ -501,7 +501,7 @@ export class Collection<TSchema extends Document = Document> {
501
501
  if (typeof options === 'function') (callback = options), (options = {});
502
502
 
503
503
  return executeOperation(
504
- getTopology(this),
504
+ this,
505
505
  new ReplaceOneOperation(
506
506
  this as TODO_NODE_3286,
507
507
  filter,
@@ -549,7 +549,7 @@ export class Collection<TSchema extends Document = Document> {
549
549
  if (typeof options === 'function') (callback = options), (options = {});
550
550
 
551
551
  return executeOperation(
552
- getTopology(this),
552
+ this,
553
553
  new UpdateManyOperation(
554
554
  this as TODO_NODE_3286,
555
555
  filter,
@@ -583,7 +583,7 @@ export class Collection<TSchema extends Document = Document> {
583
583
  if (typeof options === 'function') (callback = options), (options = {});
584
584
 
585
585
  return executeOperation(
586
- getTopology(this),
586
+ this,
587
587
  new DeleteOneOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options)),
588
588
  callback
589
589
  );
@@ -623,7 +623,7 @@ export class Collection<TSchema extends Document = Document> {
623
623
  }
624
624
 
625
625
  return executeOperation(
626
- getTopology(this),
626
+ this,
627
627
  new DeleteManyOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options)),
628
628
  callback
629
629
  );
@@ -652,7 +652,7 @@ export class Collection<TSchema extends Document = Document> {
652
652
 
653
653
  // Intentionally, we do not inherit options from parent for this operation.
654
654
  return executeOperation(
655
- getTopology(this),
655
+ this,
656
656
  new RenameOperation(this as TODO_NODE_3286, newName, {
657
657
  ...options,
658
658
  readPreference: ReadPreference.PRIMARY
@@ -679,7 +679,7 @@ export class Collection<TSchema extends Document = Document> {
679
679
  options = options ?? {};
680
680
 
681
681
  return executeOperation(
682
- getTopology(this),
682
+ this,
683
683
  new DropCollectionOperation(this.s.db, this.collectionName, options),
684
684
  callback
685
685
  );
@@ -747,7 +747,7 @@ export class Collection<TSchema extends Document = Document> {
747
747
  */
748
748
  find(): FindCursor<WithId<TSchema>>;
749
749
  find(filter: Filter<TSchema>, options?: FindOptions): FindCursor<WithId<TSchema>>;
750
- find<T>(filter: Filter<TSchema>, options?: FindOptions): FindCursor<T>;
750
+ find<T extends Document>(filter: Filter<TSchema>, options?: FindOptions): FindCursor<T>;
751
751
  find(filter?: Filter<TSchema>, options?: FindOptions): FindCursor<WithId<TSchema>> {
752
752
  if (arguments.length > 2) {
753
753
  throw new MongoInvalidArgumentError(
@@ -783,7 +783,7 @@ export class Collection<TSchema extends Document = Document> {
783
783
  if (typeof options === 'function') (callback = options), (options = {});
784
784
 
785
785
  return executeOperation(
786
- getTopology(this),
786
+ this,
787
787
  new OptionsOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
788
788
  callback
789
789
  );
@@ -806,7 +806,7 @@ export class Collection<TSchema extends Document = Document> {
806
806
  if (typeof options === 'function') (callback = options), (options = {});
807
807
 
808
808
  return executeOperation(
809
- getTopology(this),
809
+ this,
810
810
  new IsCappedOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
811
811
  callback
812
812
  );
@@ -857,7 +857,7 @@ export class Collection<TSchema extends Document = Document> {
857
857
  if (typeof options === 'function') (callback = options), (options = {});
858
858
 
859
859
  return executeOperation(
860
- getTopology(this),
860
+ this,
861
861
  new CreateIndexOperation(
862
862
  this as TODO_NODE_3286,
863
863
  this.collectionName,
@@ -918,7 +918,7 @@ export class Collection<TSchema extends Document = Document> {
918
918
  if (typeof options.maxTimeMS !== 'number') delete options.maxTimeMS;
919
919
 
920
920
  return executeOperation(
921
- getTopology(this),
921
+ this,
922
922
  new CreateIndexesOperation(
923
923
  this as TODO_NODE_3286,
924
924
  this.collectionName,
@@ -952,7 +952,7 @@ export class Collection<TSchema extends Document = Document> {
952
952
  options.readPreference = ReadPreference.primary;
953
953
 
954
954
  return executeOperation(
955
- getTopology(this),
955
+ this,
956
956
  new DropIndexOperation(this as TODO_NODE_3286, indexName, options),
957
957
  callback
958
958
  );
@@ -975,7 +975,7 @@ export class Collection<TSchema extends Document = Document> {
975
975
  if (typeof options === 'function') (callback = options), (options = {});
976
976
 
977
977
  return executeOperation(
978
- getTopology(this),
978
+ this,
979
979
  new DropIndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
980
980
  callback
981
981
  );
@@ -1013,7 +1013,7 @@ export class Collection<TSchema extends Document = Document> {
1013
1013
  if (typeof options === 'function') (callback = options), (options = {});
1014
1014
 
1015
1015
  return executeOperation(
1016
- getTopology(this),
1016
+ this,
1017
1017
  new IndexExistsOperation(this as TODO_NODE_3286, indexes, resolveOptions(this, options)),
1018
1018
  callback
1019
1019
  );
@@ -1036,7 +1036,7 @@ export class Collection<TSchema extends Document = Document> {
1036
1036
  if (typeof options === 'function') (callback = options), (options = {});
1037
1037
 
1038
1038
  return executeOperation(
1039
- getTopology(this),
1039
+ this,
1040
1040
  new IndexInformationOperation(this.s.db, this.collectionName, resolveOptions(this, options)),
1041
1041
  callback
1042
1042
  );
@@ -1058,7 +1058,7 @@ export class Collection<TSchema extends Document = Document> {
1058
1058
  ): Promise<number> | void {
1059
1059
  if (typeof options === 'function') (callback = options), (options = {});
1060
1060
  return executeOperation(
1061
- getTopology(this),
1061
+ this,
1062
1062
  new EstimatedDocumentCountOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
1063
1063
  callback
1064
1064
  );
@@ -1118,7 +1118,7 @@ export class Collection<TSchema extends Document = Document> {
1118
1118
 
1119
1119
  filter ??= {};
1120
1120
  return executeOperation(
1121
- getTopology(this),
1121
+ this,
1122
1122
  new CountDocumentsOperation(
1123
1123
  this as TODO_NODE_3286,
1124
1124
  filter as Document,
@@ -1193,7 +1193,7 @@ export class Collection<TSchema extends Document = Document> {
1193
1193
 
1194
1194
  filter ??= {};
1195
1195
  return executeOperation(
1196
- getTopology(this),
1196
+ this,
1197
1197
  new DistinctOperation(
1198
1198
  this as TODO_NODE_3286,
1199
1199
  key as TODO_NODE_3286,
@@ -1221,7 +1221,7 @@ export class Collection<TSchema extends Document = Document> {
1221
1221
  if (typeof options === 'function') (callback = options), (options = {});
1222
1222
 
1223
1223
  return executeOperation(
1224
- getTopology(this),
1224
+ this,
1225
1225
  new IndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options)),
1226
1226
  callback
1227
1227
  );
@@ -1245,7 +1245,7 @@ export class Collection<TSchema extends Document = Document> {
1245
1245
  options = options ?? {};
1246
1246
 
1247
1247
  return executeOperation(
1248
- getTopology(this),
1248
+ this,
1249
1249
  new CollStatsOperation(this as TODO_NODE_3286, options),
1250
1250
  callback
1251
1251
  );
@@ -1277,7 +1277,7 @@ export class Collection<TSchema extends Document = Document> {
1277
1277
  if (typeof options === 'function') (callback = options), (options = {});
1278
1278
 
1279
1279
  return executeOperation(
1280
- getTopology(this),
1280
+ this,
1281
1281
  new FindOneAndDeleteOperation(
1282
1282
  this as TODO_NODE_3286,
1283
1283
  filter,
@@ -1324,7 +1324,7 @@ export class Collection<TSchema extends Document = Document> {
1324
1324
  if (typeof options === 'function') (callback = options), (options = {});
1325
1325
 
1326
1326
  return executeOperation(
1327
- getTopology(this),
1327
+ this,
1328
1328
  new FindOneAndReplaceOperation(
1329
1329
  this as TODO_NODE_3286,
1330
1330
  filter,
@@ -1372,7 +1372,7 @@ export class Collection<TSchema extends Document = Document> {
1372
1372
  if (typeof options === 'function') (callback = options), (options = {});
1373
1373
 
1374
1374
  return executeOperation(
1375
- getTopology(this),
1375
+ this,
1376
1376
  new FindOneAndUpdateOperation(
1377
1377
  this as TODO_NODE_3286,
1378
1378
  filter,
@@ -1389,7 +1389,7 @@ export class Collection<TSchema extends Document = Document> {
1389
1389
  * @param pipeline - An array of aggregation pipelines to execute
1390
1390
  * @param options - Optional settings for the command
1391
1391
  */
1392
- aggregate<T = Document>(
1392
+ aggregate<T extends Document = Document>(
1393
1393
  pipeline: Document[] = [],
1394
1394
  options?: AggregateOptions
1395
1395
  ): AggregationCursor<T> {
@@ -1422,7 +1422,7 @@ export class Collection<TSchema extends Document = Document> {
1422
1422
  * @param pipeline - An array of {@link https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/|aggregation pipeline stages} through which to pass change stream documents. This allows for filtering (using $match) and manipulating the change stream documents.
1423
1423
  * @param options - Optional settings for the command
1424
1424
  */
1425
- watch<TLocal = TSchema>(
1425
+ watch<TLocal extends Document = TSchema>(
1426
1426
  pipeline: Document[] = [],
1427
1427
  options: ChangeStreamOptions = {}
1428
1428
  ): ChangeStream<TLocal> {
@@ -1495,7 +1495,7 @@ export class Collection<TSchema extends Document = Document> {
1495
1495
  }
1496
1496
 
1497
1497
  return executeOperation(
1498
- getTopology(this),
1498
+ this,
1499
1499
  new MapReduceOperation(
1500
1500
  this as TODO_NODE_3286,
1501
1501
  map,
@@ -1636,8 +1636,12 @@ export class Collection<TSchema extends Document = Document> {
1636
1636
 
1637
1637
  filter ??= {};
1638
1638
  return executeOperation(
1639
- getTopology(this),
1640
- new CountDocumentsOperation(this as TODO_NODE_3286, filter, resolveOptions(this, options)),
1639
+ this,
1640
+ new CountOperation(
1641
+ MongoDBNamespace.fromString(this.namespace),
1642
+ filter,
1643
+ resolveOptions(this, options)
1644
+ ),
1641
1645
  callback
1642
1646
  );
1643
1647
  }
@@ -19,6 +19,7 @@ import {
19
19
  ServerApi,
20
20
  ServerApiVersion
21
21
  } from './mongo_client';
22
+ import type { OneOrMore } from './mongo_types';
22
23
  import { PromiseProvider } from './promise_provider';
23
24
  import { ReadConcern, ReadConcernLevel } from './read_concern';
24
25
  import { ReadPreference, ReadPreferenceMode } from './read_preference';
@@ -28,6 +29,7 @@ import {
28
29
  Callback,
29
30
  DEFAULT_PK_FACTORY,
30
31
  emitWarning,
32
+ emitWarningOnce,
31
33
  HostAddress,
32
34
  isRecord,
33
35
  makeClientMetadata,
@@ -184,8 +186,22 @@ const FALSEHOODS = new Set(['false', 'f', '0', 'n', 'no', '-1']);
184
186
  function getBoolean(name: string, value: unknown): boolean {
185
187
  if (typeof value === 'boolean') return value;
186
188
  const valueString = String(value).toLowerCase();
187
- if (TRUTHS.has(valueString)) return true;
188
- if (FALSEHOODS.has(valueString)) return false;
189
+ if (TRUTHS.has(valueString)) {
190
+ if (valueString !== 'true') {
191
+ emitWarningOnce(
192
+ `deprecated value for ${name} : ${valueString} - please update to ${name} : true instead`
193
+ );
194
+ }
195
+ return true;
196
+ }
197
+ if (FALSEHOODS.has(valueString)) {
198
+ if (valueString !== 'false') {
199
+ emitWarningOnce(
200
+ `deprecated value for ${name} : ${valueString} - please update to ${name} : false instead`
201
+ );
202
+ }
203
+ return false;
204
+ }
189
205
  throw new MongoParseError(`Expected ${name} to be stringified boolean value, got: ${value}`);
190
206
  }
191
207
 
@@ -204,44 +220,37 @@ function getUint(name: string, value: unknown): number {
204
220
  return parsedValue;
205
221
  }
206
222
 
207
- function toRecord(value: string): Record<string, any> {
208
- const record = Object.create(null);
223
+ /** Wrap a single value in an array if the value is not an array */
224
+ function toArray<T>(value: OneOrMore<T>): T[] {
225
+ return Array.isArray(value) ? value : [value];
226
+ }
227
+
228
+ function* entriesFromString(value: string) {
209
229
  const keyValuePairs = value.split(',');
210
230
  for (const keyValue of keyValuePairs) {
211
231
  const [key, value] = keyValue.split(':');
212
232
  if (value == null) {
213
233
  throw new MongoParseError('Cannot have undefined values in key value pairs');
214
234
  }
215
- try {
216
- // try to get a boolean
217
- record[key] = getBoolean('', value);
218
- } catch {
219
- try {
220
- // try to get a number
221
- record[key] = getInt('', value);
222
- } catch {
223
- // keep value as a string
224
- record[key] = value;
225
- }
226
- }
235
+
236
+ yield [key, value];
227
237
  }
228
- return record;
229
238
  }
230
239
 
231
- class CaseInsensitiveMap extends Map<string, any> {
240
+ class CaseInsensitiveMap<Value = any> extends Map<string, Value> {
232
241
  constructor(entries: Array<[string, any]> = []) {
233
242
  super(entries.map(([k, v]) => [k.toLowerCase(), v]));
234
243
  }
235
- has(k: string) {
244
+ override has(k: string) {
236
245
  return super.has(k.toLowerCase());
237
246
  }
238
- get(k: string) {
247
+ override get(k: string) {
239
248
  return super.get(k.toLowerCase());
240
249
  }
241
- set(k: string, v: any) {
250
+ override set(k: string, v: any) {
242
251
  return super.set(k.toLowerCase(), v);
243
252
  }
244
- delete(k: string): boolean {
253
+ override delete(k: string): boolean {
245
254
  return super.delete(k.toLowerCase());
246
255
  }
247
256
  }
@@ -262,7 +271,7 @@ export function parseOptions(
262
271
  const mongoOptions = Object.create(null);
263
272
  mongoOptions.hosts = isSRV ? [] : hosts.map(HostAddress.fromString);
264
273
 
265
- const urlOptions = new CaseInsensitiveMap();
274
+ const urlOptions = new CaseInsensitiveMap<any[]>();
266
275
 
267
276
  if (url.pathname !== '/' && url.pathname !== '') {
268
277
  const dbName = decodeURIComponent(
@@ -324,16 +333,11 @@ export function parseOptions(
324
333
  ]);
325
334
 
326
335
  for (const key of allKeys) {
327
- const values = [];
328
- if (objectOptions.has(key)) {
329
- values.push(objectOptions.get(key));
330
- }
331
- if (urlOptions.has(key)) {
332
- values.push(...urlOptions.get(key));
333
- }
334
- if (DEFAULT_OPTIONS.has(key)) {
335
- values.push(DEFAULT_OPTIONS.get(key));
336
- }
336
+ const values = [objectOptions, urlOptions, DEFAULT_OPTIONS].flatMap(optionsObject => {
337
+ const options = optionsObject.get(key) ?? [];
338
+ return toArray(options);
339
+ });
340
+
337
341
  allOptions.set(key, values);
338
342
  }
339
343
 
@@ -394,6 +398,16 @@ export function parseOptions(
394
398
  }
395
399
 
396
400
  mongoOptions.credentials.validate();
401
+
402
+ // Check if the only auth related option provided was authSource, if so we can remove credentials
403
+ if (
404
+ mongoOptions.credentials.password === '' &&
405
+ mongoOptions.credentials.username === '' &&
406
+ mongoOptions.credentials.mechanism === AuthMechanism.MONGODB_DEFAULT &&
407
+ Object.keys(mongoOptions.credentials.mechanismProperties).length === 0
408
+ ) {
409
+ delete mongoOptions.credentials;
410
+ }
397
411
  }
398
412
 
399
413
  if (!mongoOptions.dbName) {
@@ -454,6 +468,10 @@ export function parseOptions(
454
468
  }
455
469
  }
456
470
 
471
+ if (mongoOptions.directConnection && mongoOptions.hosts.length !== 1) {
472
+ throw new MongoParseError('directConnection option requires exactly one host');
473
+ }
474
+
457
475
  if (
458
476
  !mongoOptions.proxyHost &&
459
477
  (mongoOptions.proxyPort || mongoOptions.proxyUsername || mongoOptions.proxyPassword)
@@ -468,12 +486,11 @@ export function parseOptions(
468
486
  throw new MongoParseError('Can only specify both of proxy username/password or neither');
469
487
  }
470
488
 
471
- if (
472
- urlOptions.get('proxyHost')?.length > 1 ||
473
- urlOptions.get('proxyPort')?.length > 1 ||
474
- urlOptions.get('proxyUsername')?.length > 1 ||
475
- urlOptions.get('proxyPassword')?.length > 1
476
- ) {
489
+ const proxyOptions = ['proxyHost', 'proxyPort', 'proxyUsername', 'proxyPassword'].map(
490
+ key => urlOptions.get(key) ?? []
491
+ );
492
+
493
+ if (proxyOptions.some(options => options.length > 1)) {
477
494
  throw new MongoParseError(
478
495
  'Proxy options cannot be specified multiple times in the connection string'
479
496
  );
@@ -502,6 +519,7 @@ function validateLoadBalancedOptions(
502
519
  return new MongoParseError('Cannot limit srv hosts with loadBalanced enabled');
503
520
  }
504
521
  }
522
+ return;
505
523
  }
506
524
 
507
525
  function setOption(
@@ -619,14 +637,26 @@ export const OPTIONS = {
619
637
  },
620
638
  authMechanismProperties: {
621
639
  target: 'credentials',
622
- transform({ options, values: [value] }): MongoCredentials {
623
- if (typeof value === 'string') {
624
- value = toRecord(value);
640
+ transform({ options, values: [optionValue] }): MongoCredentials {
641
+ if (typeof optionValue === 'string') {
642
+ const mechanismProperties = Object.create(null);
643
+
644
+ for (const [key, value] of entriesFromString(optionValue)) {
645
+ try {
646
+ mechanismProperties[key] = getBoolean(key, value);
647
+ } catch {
648
+ mechanismProperties[key] = value;
649
+ }
650
+ }
651
+
652
+ return MongoCredentials.merge(options.credentials, {
653
+ mechanismProperties
654
+ });
625
655
  }
626
- if (!isRecord(value)) {
656
+ if (!isRecord(optionValue)) {
627
657
  throw new MongoParseError('AuthMechanismProperties must be an object');
628
658
  }
629
- return MongoCredentials.merge(options.credentials, { mechanismProperties: value });
659
+ return MongoCredentials.merge(options.credentials, { mechanismProperties: optionValue });
630
660
  }
631
661
  },
632
662
  authSource: {
@@ -807,6 +837,7 @@ export const OPTIONS = {
807
837
  emitWarning('Alternative loggers might not be supported');
808
838
  // TODO: make Logger an interface that others can implement, make usage consistent in driver
809
839
  // DRIVERS-1204
840
+ return;
810
841
  }
811
842
  },
812
843
  loggerLevel: {
@@ -946,6 +977,7 @@ export const OPTIONS = {
946
977
  rpOpts
947
978
  );
948
979
  }
980
+ throw new MongoParseError(`Unknown ReadPreference value: ${value}`);
949
981
  }
950
982
  },
951
983
  readPreferenceTags: {
@@ -955,7 +987,7 @@ export const OPTIONS = {
955
987
  for (const tag of values) {
956
988
  const readPreferenceTag: TagSet = Object.create(null);
957
989
  if (typeof tag === 'string') {
958
- for (const [k, v] of Object.entries(toRecord(tag))) {
990
+ for (const [k, v] of entriesFromString(tag)) {
959
991
  readPreferenceTag[k] = v;
960
992
  }
961
993
  }