mongodb 4.1.1 → 4.2.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 (174) hide show
  1. package/README.md +24 -17
  2. package/lib/admin.js +5 -5
  3. package/lib/admin.js.map +1 -1
  4. package/lib/bulk/common.js +60 -56
  5. package/lib/bulk/common.js.map +1 -1
  6. package/lib/change_stream.js +15 -13
  7. package/lib/change_stream.js.map +1 -1
  8. package/lib/cmap/auth/gssapi.js +1 -1
  9. package/lib/cmap/auth/gssapi.js.map +1 -1
  10. package/lib/cmap/auth/mongo_credentials.js +2 -1
  11. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  12. package/lib/cmap/auth/mongocr.js +2 -2
  13. package/lib/cmap/auth/mongocr.js.map +1 -1
  14. package/lib/cmap/auth/mongodb_aws.js +23 -28
  15. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  16. package/lib/cmap/auth/plain.js +1 -1
  17. package/lib/cmap/auth/plain.js.map +1 -1
  18. package/lib/cmap/auth/scram.js +5 -5
  19. package/lib/cmap/auth/scram.js.map +1 -1
  20. package/lib/cmap/auth/x509.js +1 -1
  21. package/lib/cmap/auth/x509.js.map +1 -1
  22. package/lib/cmap/command_monitoring_events.js +15 -15
  23. package/lib/cmap/command_monitoring_events.js.map +1 -1
  24. package/lib/cmap/commands.js +1 -1
  25. package/lib/cmap/commands.js.map +1 -1
  26. package/lib/cmap/connect.js +2 -2
  27. package/lib/cmap/connect.js.map +1 -1
  28. package/lib/cmap/connection.js +15 -15
  29. package/lib/cmap/connection.js.map +1 -1
  30. package/lib/cmap/connection_pool.js +3 -3
  31. package/lib/cmap/connection_pool.js.map +1 -1
  32. package/lib/cmap/message_stream.js +2 -2
  33. package/lib/cmap/message_stream.js.map +1 -1
  34. package/lib/cmap/stream_description.js +1 -1
  35. package/lib/cmap/stream_description.js.map +1 -1
  36. package/lib/cmap/wire_protocol/constants.js +4 -4
  37. package/lib/collection.js +42 -41
  38. package/lib/collection.js.map +1 -1
  39. package/lib/connection_string.js +94 -58
  40. package/lib/connection_string.js.map +1 -1
  41. package/lib/cursor/abstract_cursor.js +11 -11
  42. package/lib/cursor/abstract_cursor.js.map +1 -1
  43. package/lib/cursor/aggregation_cursor.js +15 -15
  44. package/lib/cursor/aggregation_cursor.js.map +1 -1
  45. package/lib/cursor/find_cursor.js +23 -23
  46. package/lib/cursor/find_cursor.js.map +1 -1
  47. package/lib/db.js +20 -20
  48. package/lib/db.js.map +1 -1
  49. package/lib/deps.js +1 -1
  50. package/lib/deps.js.map +1 -1
  51. package/lib/gridfs/index.js +3 -3
  52. package/lib/gridfs/index.js.map +1 -1
  53. package/lib/gridfs/upload.js +1 -1
  54. package/lib/gridfs/upload.js.map +1 -1
  55. package/lib/index.js +8 -2
  56. package/lib/index.js.map +1 -1
  57. package/lib/logger.js +5 -5
  58. package/lib/logger.js.map +1 -1
  59. package/lib/mongo_client.js +7 -7
  60. package/lib/mongo_client.js.map +1 -1
  61. package/lib/mongo_types.js.map +1 -1
  62. package/lib/operations/add_user.js +3 -3
  63. package/lib/operations/add_user.js.map +1 -1
  64. package/lib/operations/aggregate.js +3 -4
  65. package/lib/operations/aggregate.js.map +1 -1
  66. package/lib/operations/bulk_write.js +1 -1
  67. package/lib/operations/bulk_write.js.map +1 -1
  68. package/lib/operations/command.js +7 -3
  69. package/lib/operations/command.js.map +1 -1
  70. package/lib/operations/common_functions.js +1 -1
  71. package/lib/operations/common_functions.js.map +1 -1
  72. package/lib/operations/connect.js +1 -1
  73. package/lib/operations/connect.js.map +1 -1
  74. package/lib/operations/count.js +1 -1
  75. package/lib/operations/count.js.map +1 -1
  76. package/lib/operations/count_documents.js.map +1 -1
  77. package/lib/operations/create_collection.js +1 -1
  78. package/lib/operations/create_collection.js.map +1 -1
  79. package/lib/operations/delete.js +6 -6
  80. package/lib/operations/delete.js.map +1 -1
  81. package/lib/operations/distinct.js +4 -4
  82. package/lib/operations/distinct.js.map +1 -1
  83. package/lib/operations/drop.js +2 -2
  84. package/lib/operations/drop.js.map +1 -1
  85. package/lib/operations/estimated_document_count.js +2 -2
  86. package/lib/operations/estimated_document_count.js.map +1 -1
  87. package/lib/operations/eval.js.map +1 -1
  88. package/lib/operations/execute_operation.js +17 -7
  89. package/lib/operations/execute_operation.js.map +1 -1
  90. package/lib/operations/find.js +8 -8
  91. package/lib/operations/find.js.map +1 -1
  92. package/lib/operations/find_and_modify.js +7 -7
  93. package/lib/operations/find_and_modify.js.map +1 -1
  94. package/lib/operations/indexes.js +14 -14
  95. package/lib/operations/indexes.js.map +1 -1
  96. package/lib/operations/insert.js +5 -5
  97. package/lib/operations/insert.js.map +1 -1
  98. package/lib/operations/list_collections.js +12 -7
  99. package/lib/operations/list_collections.js.map +1 -1
  100. package/lib/operations/list_databases.js +1 -1
  101. package/lib/operations/list_databases.js.map +1 -1
  102. package/lib/operations/map_reduce.js +6 -6
  103. package/lib/operations/map_reduce.js.map +1 -1
  104. package/lib/operations/operation.js +2 -1
  105. package/lib/operations/operation.js.map +1 -1
  106. package/lib/operations/remove_user.js +1 -1
  107. package/lib/operations/remove_user.js.map +1 -1
  108. package/lib/operations/rename.js +2 -2
  109. package/lib/operations/rename.js.map +1 -1
  110. package/lib/operations/set_profiling_level.js +1 -1
  111. package/lib/operations/set_profiling_level.js.map +1 -1
  112. package/lib/operations/stats.js +2 -2
  113. package/lib/operations/stats.js.map +1 -1
  114. package/lib/operations/update.js +12 -12
  115. package/lib/operations/update.js.map +1 -1
  116. package/lib/sdam/monitor.js +14 -14
  117. package/lib/sdam/monitor.js.map +1 -1
  118. package/lib/sdam/server.js +19 -12
  119. package/lib/sdam/server.js.map +1 -1
  120. package/lib/sdam/server_description.js +3 -3
  121. package/lib/sdam/server_description.js.map +1 -1
  122. package/lib/sdam/server_selection.js +20 -1
  123. package/lib/sdam/server_selection.js.map +1 -1
  124. package/lib/sdam/srv_polling.js +9 -9
  125. package/lib/sdam/srv_polling.js.map +1 -1
  126. package/lib/sdam/topology.js +29 -21
  127. package/lib/sdam/topology.js.map +1 -1
  128. package/lib/sdam/topology_description.js +34 -12
  129. package/lib/sdam/topology_description.js.map +1 -1
  130. package/lib/sessions.js +19 -19
  131. package/lib/sessions.js.map +1 -1
  132. package/lib/transactions.js.map +1 -1
  133. package/lib/utils.js +53 -46
  134. package/lib/utils.js.map +1 -1
  135. package/mongodb.d.ts +97 -50
  136. package/mongodb.ts34.d.ts +96 -50
  137. package/package.json +44 -47
  138. package/src/bulk/common.ts +56 -35
  139. package/src/change_stream.ts +8 -6
  140. package/src/cmap/auth/mongo_credentials.ts +14 -3
  141. package/src/cmap/auth/mongodb_aws.ts +32 -31
  142. package/src/cmap/command_monitoring_events.ts +1 -1
  143. package/src/cmap/connection.ts +1 -0
  144. package/src/cmap/wire_protocol/constants.ts +4 -4
  145. package/src/collection.ts +32 -24
  146. package/src/connection_string.ts +103 -52
  147. package/src/cursor/abstract_cursor.ts +6 -4
  148. package/src/cursor/aggregation_cursor.ts +3 -3
  149. package/src/cursor/find_cursor.ts +1 -1
  150. package/src/db.ts +5 -6
  151. package/src/deps.ts +56 -19
  152. package/src/gridfs/download.ts +1 -1
  153. package/src/gridfs/upload.ts +11 -7
  154. package/src/index.ts +12 -1
  155. package/src/mongo_client.ts +18 -11
  156. package/src/mongo_types.ts +10 -13
  157. package/src/operations/aggregate.ts +1 -2
  158. package/src/operations/command.ts +5 -0
  159. package/src/operations/count.ts +1 -1
  160. package/src/operations/count_documents.ts +1 -1
  161. package/src/operations/create_collection.ts +1 -1
  162. package/src/operations/eval.ts +1 -1
  163. package/src/operations/execute_operation.ts +13 -2
  164. package/src/operations/indexes.ts +2 -2
  165. package/src/operations/list_collections.ts +12 -4
  166. package/src/operations/list_databases.ts +6 -1
  167. package/src/operations/operation.ts +3 -0
  168. package/src/sdam/server.ts +8 -0
  169. package/src/sdam/server_selection.ts +25 -0
  170. package/src/sdam/srv_polling.ts +12 -11
  171. package/src/sdam/topology.ts +27 -10
  172. package/src/sdam/topology_description.ts +35 -11
  173. package/src/transactions.ts +2 -2
  174. package/src/utils.ts +67 -56
package/src/utils.ts CHANGED
@@ -28,6 +28,7 @@ import type { CommandOperationOptions, OperationParent } from './operations/comm
28
28
  import { ReadPreference } from './read_preference';
29
29
  import { URL } from 'url';
30
30
  import { MAX_SUPPORTED_WIRE_VERSION } from './cmap/wire_protocol/constants';
31
+ import type { SrvRecord } from 'dns';
31
32
 
32
33
  /**
33
34
  * MongoDB Driver style callback
@@ -41,23 +42,6 @@ export const MAX_JS_INT = Number.MAX_SAFE_INTEGER + 1;
41
42
 
42
43
  export type AnyOptions = Document;
43
44
 
44
- /**
45
- * Add a readonly enumerable property.
46
- * @internal
47
- */
48
- export function getSingleProperty(
49
- obj: AnyOptions,
50
- name: string | number | symbol,
51
- value: unknown
52
- ): void {
53
- Object.defineProperty(obj, name, {
54
- enumerable: true,
55
- get() {
56
- return value;
57
- }
58
- });
59
- }
60
-
61
45
  /**
62
46
  * Throws if collectionName is not a valid mongodb collection namespace.
63
47
  * @internal
@@ -185,17 +169,6 @@ export function isObject(arg: unknown): arg is object {
185
169
  return '[object Object]' === Object.prototype.toString.call(arg);
186
170
  }
187
171
 
188
- /** @internal */
189
- export function decorateCommand(command: Document, options: Document, exclude: string[]): Document {
190
- for (const name in options) {
191
- if (!exclude.includes(name)) {
192
- command[name] = options[name];
193
- }
194
- }
195
-
196
- return command;
197
- }
198
-
199
172
  /** @internal */
200
173
  export function mergeOptions<T, S>(target: T, source: S): T & S {
201
174
  return { ...target, ...source };
@@ -657,11 +630,6 @@ export function databaseNamespace(ns: string): string {
657
630
  return ns.split('.')[0];
658
631
  }
659
632
 
660
- /** @internal */
661
- export function collectionNamespace(ns: string): string {
662
- return ns.split('.').slice(1).join('.');
663
- }
664
-
665
633
  /**
666
634
  * Synchronously Generate a UUIDv4
667
635
  * @internal
@@ -987,7 +955,7 @@ export function makeInterruptibleAsyncInterval(
987
955
  ): InterruptibleAsyncInterval {
988
956
  let timerId: NodeJS.Timeout | undefined;
989
957
  let lastCallTime: number;
990
- let lastWakeTime: number;
958
+ let cannotBeExpedited = false;
991
959
  let stopped = false;
992
960
 
993
961
  options = options ?? {};
@@ -998,10 +966,8 @@ export function makeInterruptibleAsyncInterval(
998
966
 
999
967
  function wake() {
1000
968
  const currentTime = clock();
1001
- const timeSinceLastWake = currentTime - lastWakeTime;
1002
- const timeSinceLastCall = currentTime - lastCallTime;
1003
- const timeUntilNextCall = interval - timeSinceLastCall;
1004
- lastWakeTime = currentTime;
969
+ const nextScheduledCallTime = lastCallTime + interval;
970
+ const timeUntilNextCall = nextScheduledCallTime - currentTime;
1005
971
 
1006
972
  // For the streaming protocol: there is nothing obviously stopping this
1007
973
  // interval from being woken up again while we are waiting "infinitely"
@@ -1009,8 +975,17 @@ export function makeInterruptibleAsyncInterval(
1009
975
  // never completes, the `timeUntilNextCall` will continue to grow
1010
976
  // negatively unbounded, so it will never trigger a reschedule here.
1011
977
 
978
+ // This is possible in virtualized environments like AWS Lambda where our
979
+ // clock is unreliable. In these cases the timer is "running" but never
980
+ // actually completes, so we want to execute immediately and then attempt
981
+ // to reschedule.
982
+ if (timeUntilNextCall < 0) {
983
+ executeAndReschedule();
984
+ return;
985
+ }
986
+
1012
987
  // debounce multiple calls to wake within the `minInterval`
1013
- if (timeSinceLastWake < minInterval) {
988
+ if (cannotBeExpedited) {
1014
989
  return;
1015
990
  }
1016
991
 
@@ -1018,14 +993,7 @@ export function makeInterruptibleAsyncInterval(
1018
993
  // faster than the `minInterval`
1019
994
  if (timeUntilNextCall > minInterval) {
1020
995
  reschedule(minInterval);
1021
- }
1022
-
1023
- // This is possible in virtualized environments like AWS Lambda where our
1024
- // clock is unreliable. In these cases the timer is "running" but never
1025
- // actually completes, so we want to execute immediately and then attempt
1026
- // to reschedule.
1027
- if (timeUntilNextCall < 0) {
1028
- executeAndReschedule();
996
+ cannotBeExpedited = true;
1029
997
  }
1030
998
  }
1031
999
 
@@ -1037,7 +1005,7 @@ export function makeInterruptibleAsyncInterval(
1037
1005
  }
1038
1006
 
1039
1007
  lastCallTime = 0;
1040
- lastWakeTime = 0;
1008
+ cannotBeExpedited = false;
1041
1009
  }
1042
1010
 
1043
1011
  function reschedule(ms?: number) {
@@ -1050,7 +1018,7 @@ export function makeInterruptibleAsyncInterval(
1050
1018
  }
1051
1019
 
1052
1020
  function executeAndReschedule() {
1053
- lastWakeTime = 0;
1021
+ cannotBeExpedited = false;
1054
1022
  lastCallTime = clock();
1055
1023
 
1056
1024
  fn(err => {
@@ -1128,8 +1096,9 @@ export function isSuperset(set: Set<any> | any[], subset: Set<any> | any[]): boo
1128
1096
  return true;
1129
1097
  }
1130
1098
 
1131
- export function setDifference(setA: Iterable<any>, setB: Iterable<any>): Set<any> {
1132
- const difference = new Set(setA);
1099
+ /** Returns the items that are uniquely in setA */
1100
+ export function setDifference<T>(setA: Iterable<T>, setB: Iterable<T>): Set<T> {
1101
+ const difference = new Set<T>(setA);
1133
1102
  for (const elem of setB) {
1134
1103
  difference.delete(elem);
1135
1104
  }
@@ -1352,6 +1321,14 @@ export class HostAddress {
1352
1321
  Object.freeze(this);
1353
1322
  }
1354
1323
 
1324
+ [Symbol.for('nodejs.util.inspect.custom')](): string {
1325
+ return this.inspect();
1326
+ }
1327
+
1328
+ inspect(): string {
1329
+ return `new HostAddress('${this.toString(true)}')`;
1330
+ }
1331
+
1355
1332
  /**
1356
1333
  * @param ipv6Brackets - optionally request ipv6 bracket notation required for connection strings
1357
1334
  */
@@ -1368,6 +1345,10 @@ export class HostAddress {
1368
1345
  static fromString(s: string): HostAddress {
1369
1346
  return new HostAddress(s);
1370
1347
  }
1348
+
1349
+ static fromSrvRecord({ name, port }: SrvRecord): HostAddress {
1350
+ return HostAddress.fromString(`${name}:${port}`);
1351
+ }
1371
1352
  }
1372
1353
 
1373
1354
  export const DEFAULT_PK_FACTORY = {
@@ -1430,11 +1411,41 @@ export function supportsRetryableWrites(server: Server): boolean {
1430
1411
  );
1431
1412
  }
1432
1413
 
1433
- export function parsePackageVersion({
1434
- version
1435
- }: {
1436
- version: string;
1437
- }): { major: number; minor: number; patch: number } {
1414
+ export function parsePackageVersion({ version }: { version: string }): {
1415
+ major: number;
1416
+ minor: number;
1417
+ patch: number;
1418
+ } {
1438
1419
  const [major, minor, patch] = version.split('.').map((n: string) => Number.parseInt(n, 10));
1439
1420
  return { major, minor, patch };
1440
1421
  }
1422
+
1423
+ /**
1424
+ * Fisher–Yates Shuffle
1425
+ *
1426
+ * Reference: https://bost.ocks.org/mike/shuffle/
1427
+ * @param sequence - items to be shuffled
1428
+ * @param limit - Defaults to `0`. If nonzero shuffle will slice the randomized array e.g, `.slice(0, limit)` otherwise will return the entire randomized array.
1429
+ */
1430
+ export function shuffle<T>(sequence: Iterable<T>, limit = 0): Array<T> {
1431
+ const items = Array.from(sequence); // shallow copy in order to never shuffle the input
1432
+
1433
+ if (limit > items.length) {
1434
+ throw new MongoRuntimeError('Limit must be less than the number of items');
1435
+ }
1436
+
1437
+ let remainingItemsToShuffle = items.length;
1438
+ const lowerBound = limit % items.length === 0 ? 1 : items.length - limit;
1439
+ while (remainingItemsToShuffle > lowerBound) {
1440
+ // Pick a remaining element
1441
+ const randomIndex = Math.floor(Math.random() * remainingItemsToShuffle);
1442
+ remainingItemsToShuffle -= 1;
1443
+
1444
+ // And swap it with the current element
1445
+ const swapHold = items[remainingItemsToShuffle];
1446
+ items[remainingItemsToShuffle] = items[randomIndex];
1447
+ items[randomIndex] = swapHold;
1448
+ }
1449
+
1450
+ return limit % items.length === 0 ? items : items.slice(lowerBound);
1451
+ }