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/utils.ts CHANGED
@@ -8,11 +8,12 @@ import type { Connection } from './cmap/connection';
8
8
  import { MAX_SUPPORTED_WIRE_VERSION } from './cmap/wire_protocol/constants';
9
9
  import type { Collection } from './collection';
10
10
  import { LEGACY_HELLO_COMMAND } from './constants';
11
+ import type { AbstractCursor } from './cursor/abstract_cursor';
12
+ import type { FindCursor } from './cursor/find_cursor';
11
13
  import type { Db } from './db';
12
14
  import {
13
15
  AnyError,
14
16
  MongoCompatibilityError,
15
- MongoExpiredSessionError,
16
17
  MongoInvalidArgumentError,
17
18
  MongoNotConnectedError,
18
19
  MongoParseError,
@@ -37,8 +38,6 @@ import { W, WriteConcern, WriteConcernOptions } from './write_concern';
37
38
  * @public
38
39
  */
39
40
  export type Callback<T = any> = (error?: AnyError, result?: T) => void;
40
- /** @public */
41
- export type CallbackWithType<E = AnyError, T0 = any> = (error?: E, result?: T0) => void;
42
41
 
43
42
  export const MAX_JS_INT = Number.MAX_SAFE_INTEGER + 1;
44
43
 
@@ -190,110 +189,6 @@ export function filterOptions(options: AnyOptions, names: string[]): AnyOptions
190
189
  return filterOptions;
191
190
  }
192
191
 
193
- /**
194
- * Executes the given operation with provided arguments.
195
- *
196
- * @remarks
197
- * This method reduces large amounts of duplication in the entire codebase by providing
198
- * a single point for determining whether callbacks or promises should be used. Additionally
199
- * it allows for a single point of entry to provide features such as implicit sessions, which
200
- * are required by the Driver Sessions specification in the event that a ClientSession is
201
- * not provided
202
- *
203
- * @internal
204
- *
205
- * @param topology - The topology to execute this operation on
206
- * @param operation - The operation to execute
207
- * @param args - Arguments to apply the provided operation
208
- * @param options - Options that modify the behavior of the method
209
- */
210
- export function executeLegacyOperation(
211
- topology: Topology,
212
- operation: (...args: any[]) => void | Promise<Document>,
213
- args: any[],
214
- options?: AnyOptions
215
- ): void | Promise<any> {
216
- const Promise = PromiseProvider.get();
217
-
218
- if (!Array.isArray(args)) {
219
- // TODO(NODE-3483)
220
- throw new MongoRuntimeError('This method requires an array of arguments to apply');
221
- }
222
-
223
- options = options ?? {};
224
-
225
- let callback = args[args.length - 1];
226
-
227
- // The driver sessions spec mandates that we implicitly create sessions for operations
228
- // that are not explicitly provided with a session.
229
- let session: ClientSession;
230
- let opOptions: any;
231
- let owner: any;
232
- if (!options.skipSessions && topology.hasSessionSupport()) {
233
- opOptions = args[args.length - 2];
234
- if (opOptions == null || opOptions.session == null) {
235
- owner = Symbol();
236
- session = topology.startSession({ owner });
237
- const optionsIndex = args.length - 2;
238
- args[optionsIndex] = Object.assign({}, args[optionsIndex], { session: session });
239
- } else if (opOptions.session && opOptions.session.hasEnded) {
240
- throw new MongoExpiredSessionError();
241
- }
242
- }
243
-
244
- function makeExecuteCallback(
245
- resolve: (value?: Document) => void,
246
- reject: (reason?: AnyError) => void
247
- ) {
248
- return function (err?: AnyError, result?: any) {
249
- if (session && session.owner === owner && !options?.returnsCursor) {
250
- session.endSession(() => {
251
- delete opOptions.session;
252
- if (err) return reject(err);
253
- resolve(result);
254
- });
255
- } else {
256
- if (err) return reject(err);
257
- resolve(result);
258
- }
259
- };
260
- }
261
-
262
- // Execute using callback
263
- if (typeof callback === 'function') {
264
- callback = args.pop();
265
- const handler = makeExecuteCallback(
266
- result => callback(undefined, result),
267
- err => callback(err, null)
268
- );
269
- args.push(handler);
270
-
271
- try {
272
- return operation(...args);
273
- } catch (e) {
274
- handler(e);
275
- throw e;
276
- }
277
- }
278
-
279
- // Return a Promise
280
- if (args[args.length - 1] != null) {
281
- // TODO(NODE-3483)
282
- throw new MongoRuntimeError('Final argument to `executeLegacyOperation` must be a callback');
283
- }
284
-
285
- return new Promise<any>((resolve, reject) => {
286
- const handler = makeExecuteCallback(resolve, reject);
287
- args[args.length - 1] = handler;
288
-
289
- try {
290
- return operation(...args);
291
- } catch (e) {
292
- handler(e);
293
- }
294
- });
295
- }
296
-
297
192
  interface HasRetryableWrites {
298
193
  retryWrites?: boolean;
299
194
  }
@@ -434,17 +329,30 @@ export function decorateWithExplain(command: Document, explain: Explain): Docume
434
329
  return { explain: command, verbosity: explain.verbosity };
435
330
  }
436
331
 
332
+ /**
333
+ * @internal
334
+ */
335
+ export type TopologyProvider =
336
+ | MongoClient
337
+ | ClientSession
338
+ | FindCursor
339
+ | AbstractCursor
340
+ | Collection<any>
341
+ | Db;
342
+
437
343
  /**
438
344
  * A helper function to get the topology from a given provider. Throws
439
345
  * if the topology cannot be found.
346
+ * @throws MongoNotConnectedError
440
347
  * @internal
441
348
  */
442
- export function getTopology<T>(provider: MongoClient | Db | Collection<T>): Topology {
349
+ export function getTopology(provider: TopologyProvider): Topology {
350
+ // MongoClient or ClientSession or AbstractCursor
443
351
  if (`topology` in provider && provider.topology) {
444
352
  return provider.topology;
445
- } else if ('client' in provider.s && provider.s.client.topology) {
353
+ } else if ('s' in provider && 'client' in provider.s && provider.s.client.topology) {
446
354
  return provider.s.client.topology;
447
- } else if ('db' in provider.s && provider.s.db.s.client.topology) {
355
+ } else if ('s' in provider && 'db' in provider.s && provider.s.db.s.client.topology) {
448
356
  return provider.s.db.s.client.topology;
449
357
  }
450
358
 
@@ -1181,7 +1089,7 @@ export function deepCopy<T>(value: T): T {
1181
1089
  case 'set':
1182
1090
  return new Set(value as any) as unknown as T;
1183
1091
  case 'buffer':
1184
- return Buffer.from(value as Buffer) as unknown as T;
1092
+ return Buffer.from(value as unknown as Buffer) as unknown as T;
1185
1093
  }
1186
1094
  }
1187
1095
 
@@ -1419,13 +1327,25 @@ export function enumToString(en: Record<string, unknown>): string {
1419
1327
  *
1420
1328
  * @internal
1421
1329
  */
1422
- export function supportsRetryableWrites(server: Server): boolean {
1423
- return (
1424
- !!server.loadBalanced ||
1425
- (server.description.maxWireVersion >= 6 &&
1426
- !!server.description.logicalSessionTimeoutMinutes &&
1427
- server.description.type !== ServerType.Standalone)
1428
- );
1330
+ export function supportsRetryableWrites(server?: Server): boolean {
1331
+ if (!server) {
1332
+ return false;
1333
+ }
1334
+
1335
+ if (server.loadBalanced) {
1336
+ // Loadbalanced topologies will always support retry writes
1337
+ return true;
1338
+ }
1339
+
1340
+ if (server.description.logicalSessionTimeoutMinutes != null) {
1341
+ // that supports sessions
1342
+ if (server.description.type !== ServerType.Standalone) {
1343
+ // and that is not a standalone
1344
+ return true;
1345
+ }
1346
+ }
1347
+
1348
+ return false;
1429
1349
  }
1430
1350
 
1431
1351
  export function parsePackageVersion({ version }: { version: string }): {
package/tsconfig.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "compilerOptions": {
3
+ "allowJs": false,
4
+ "checkJs": false,
5
+ "strict": true,
6
+ "alwaysStrict": true,
7
+ "target": "ES2019",
8
+ "module": "commonJS",
9
+ "moduleResolution": "node",
10
+ "skipLibCheck": true,
11
+ "lib": ["es2020"],
12
+ // We don't make use of tslib helpers, all syntax used is supported by target engine
13
+ "importHelpers": false,
14
+ "noEmitHelpers": true,
15
+ // Never emit error filled code
16
+ "noEmitOnError": true,
17
+ "outDir": "lib",
18
+ "importsNotUsedAsValues": "error",
19
+ // We want the sourcemaps in a separate file
20
+ "inlineSourceMap": false,
21
+ "sourceMap": true,
22
+ // API-Extractor uses declaration maps to report problems in source, no need to distribute
23
+ "declaration": true,
24
+ "declarationMap": true,
25
+ // we include sources in the release
26
+ "inlineSources": false,
27
+ // Prevents web types from being suggested by vscode.
28
+ "types": ["node"],
29
+ "forceConsistentCasingInFileNames": true,
30
+ "noImplicitOverride": true,
31
+ "noImplicitReturns": true,
32
+ // TODO(NODE-3659): Enable useUnknownInCatchVariables and add type assertions or remove unnecessary catch blocks
33
+ "useUnknownInCatchVariables": false
34
+ },
35
+ "ts-node": {
36
+ "transpileOnly": true,
37
+ "compiler": "typescript-cached-transpile"
38
+ },
39
+ "include": ["src/**/*"]
40
+ }