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
@@ -1,5 +1,12 @@
1
1
  import { PromiseProvider } from '../promise_provider';
2
- import { Long, ObjectId, Document, BSONSerializeOptions, resolveBSONOptions } from '../bson';
2
+ import {
3
+ Long,
4
+ ObjectId,
5
+ Document,
6
+ BSONSerializeOptions,
7
+ resolveBSONOptions,
8
+ Timestamp
9
+ } from '../bson';
3
10
  import {
4
11
  MongoWriteConcernError,
5
12
  AnyError,
@@ -383,6 +390,7 @@ export interface BulkWriteOperationError {
383
390
  index: number;
384
391
  code: number;
385
392
  errmsg: string;
393
+ errInfo: Document;
386
394
  op: Document | UpdateStatement | DeleteStatement;
387
395
  }
388
396
 
@@ -413,6 +421,11 @@ export class WriteError {
413
421
  return this.err.errmsg;
414
422
  }
415
423
 
424
+ /** WriteError details. */
425
+ get errInfo(): Document | undefined {
426
+ return this.err.errInfo;
427
+ }
428
+
416
429
  /** Returns the underlying operation that caused the error */
417
430
  getOperation(): Document {
418
431
  return this.err.op;
@@ -427,8 +440,13 @@ export class WriteError {
427
440
  }
428
441
  }
429
442
 
443
+ /** Converts the number to a Long or returns it. */
444
+ function longOrConvert(value: number | Long | Timestamp): Long | Timestamp {
445
+ return typeof value === 'number' ? Long.fromNumber(value) : value;
446
+ }
447
+
430
448
  /** Merges results into shared data structure */
431
- function mergeBatchResults(
449
+ export function mergeBatchResults(
432
450
  batch: Batch,
433
451
  bulkResult: BulkResult,
434
452
  err?: AnyError,
@@ -453,6 +471,7 @@ function mergeBatchResults(
453
471
  index: 0,
454
472
  code: result.code || 0,
455
473
  errmsg: result.message,
474
+ errInfo: result.errInfo,
456
475
  op: batch.operations[0]
457
476
  };
458
477
 
@@ -462,42 +481,37 @@ function mergeBatchResults(
462
481
  return;
463
482
  }
464
483
 
465
- // Deal with opTime if available
484
+ // The server write command specification states that lastOp is an optional
485
+ // mongod only field that has a type of timestamp. Across various scarce specs
486
+ // where opTime is mentioned, it is an "opaque" object that can have a "ts" and
487
+ // "t" field with Timestamp and Long as their types respectively.
488
+ // The "lastOp" field of the bulk write result is never mentioned in the driver
489
+ // specifications or the bulk write spec, so we should probably just keep its
490
+ // value consistent since it seems to vary.
491
+ // See: https://github.com/mongodb/specifications/blob/master/source/driver-bulk-update.rst#results-object
466
492
  if (result.opTime || result.lastOp) {
467
- const opTime = result.lastOp || result.opTime;
468
- let lastOpTS = null;
469
- let lastOpT = null;
493
+ let opTime = result.lastOp || result.opTime;
470
494
 
471
- // We have a time stamp
472
- if (opTime && opTime._bsontype === 'Timestamp') {
473
- if (bulkResult.opTime == null) {
474
- bulkResult.opTime = opTime;
475
- } else if (opTime.greaterThan(bulkResult.opTime)) {
476
- bulkResult.opTime = opTime;
477
- }
478
- } else {
479
- // Existing TS
480
- if (bulkResult.opTime) {
481
- lastOpTS =
482
- typeof bulkResult.opTime.ts === 'number'
483
- ? Long.fromNumber(bulkResult.opTime.ts)
484
- : bulkResult.opTime.ts;
485
- lastOpT =
486
- typeof bulkResult.opTime.t === 'number'
487
- ? Long.fromNumber(bulkResult.opTime.t)
488
- : bulkResult.opTime.t;
489
- }
490
-
491
- // Current OpTime TS
492
- const opTimeTS = typeof opTime.ts === 'number' ? Long.fromNumber(opTime.ts) : opTime.ts;
493
- const opTimeT = typeof opTime.t === 'number' ? Long.fromNumber(opTime.t) : opTime.t;
495
+ // If the opTime is a Timestamp, convert it to a consistent format to be
496
+ // able to compare easily. Converting to the object from a timestamp is
497
+ // much more straightforward than the other direction.
498
+ if (opTime._bsontype === 'Timestamp') {
499
+ opTime = { ts: opTime, t: Long.ZERO };
500
+ }
494
501
 
495
- // Compare the opTime's
496
- if (bulkResult.opTime == null) {
497
- bulkResult.opTime = opTime;
498
- } else if (opTimeTS.greaterThan(lastOpTS)) {
502
+ // If there's no lastOp, just set it.
503
+ if (!bulkResult.opTime) {
504
+ bulkResult.opTime = opTime;
505
+ } else {
506
+ // First compare the ts values and set if the opTimeTS value is greater.
507
+ const lastOpTS = longOrConvert(bulkResult.opTime.ts);
508
+ const opTimeTS = longOrConvert(opTime.ts);
509
+ if (opTimeTS.greaterThan(lastOpTS)) {
499
510
  bulkResult.opTime = opTime;
500
511
  } else if (opTimeTS.equals(lastOpTS)) {
512
+ // If the ts values are equal, then compare using the t values.
513
+ const lastOpT = longOrConvert(bulkResult.opTime.t);
514
+ const opTimeT = longOrConvert(opTime.t);
501
515
  if (opTimeT.greaterThan(lastOpT)) {
502
516
  bulkResult.opTime = opTime;
503
517
  }
@@ -555,6 +569,7 @@ function mergeBatchResults(
555
569
  index: batch.originalIndexes[result.writeErrors[i].index],
556
570
  code: result.writeErrors[i].code,
557
571
  errmsg: result.writeErrors[i].errmsg,
572
+ errInfo: result.writeErrors[i].errInfo,
558
573
  op: batch.operations[result.writeErrors[i].index]
559
574
  };
560
575
 
@@ -1201,9 +1216,15 @@ export abstract class BulkOperationBase {
1201
1216
  return batches;
1202
1217
  }
1203
1218
 
1204
- /** An internal helper method. Do not invoke directly. Will be going away in the future */
1219
+ execute(options?: BulkWriteOptions): Promise<BulkWriteResult>;
1220
+ execute(callback: Callback<BulkWriteResult>): void;
1221
+ execute(options: BulkWriteOptions | undefined, callback: Callback<BulkWriteResult>): void;
1222
+ execute(
1223
+ options?: BulkWriteOptions | Callback<BulkWriteResult>,
1224
+ callback?: Callback<BulkWriteResult>
1225
+ ): Promise<BulkWriteResult> | void;
1205
1226
  execute(
1206
- options?: BulkWriteOptions,
1227
+ options?: BulkWriteOptions | Callback<BulkWriteResult>,
1207
1228
  callback?: Callback<BulkWriteResult>
1208
1229
  ): Promise<BulkWriteResult> | void {
1209
1230
  if (typeof options === 'function') (callback = options), (options = {});
@@ -184,14 +184,14 @@ export interface UpdateDescription<TSchema extends Document = Document> {
184
184
  }
185
185
 
186
186
  /** @public */
187
- export type ChangeStreamEvents = {
187
+ export type ChangeStreamEvents<TSchema extends Document = Document> = {
188
188
  resumeTokenChanged(token: ResumeToken): void;
189
- init(response: Document): void;
190
- more(response?: Document | undefined): void;
189
+ init(response: TSchema): void;
190
+ more(response?: TSchema | undefined): void;
191
191
  response(): void;
192
192
  end(): void;
193
193
  error(error: Error): void;
194
- change(change: ChangeStreamDocument): void;
194
+ change(change: ChangeStreamDocument<TSchema>): void;
195
195
  } & AbstractCursorEvents;
196
196
 
197
197
  /**
@@ -199,7 +199,7 @@ export type ChangeStreamEvents = {
199
199
  * @public
200
200
  */
201
201
  export class ChangeStream<TSchema extends Document = Document> extends TypedEventEmitter<
202
- ChangeStreamEvents
202
+ ChangeStreamEvents<TSchema>
203
203
  > {
204
204
  pipeline: Document[];
205
205
  options: ChangeStreamOptions;
@@ -210,7 +210,7 @@ export class ChangeStream<TSchema extends Document = Document> extends TypedEven
210
210
  cursor?: ChangeStreamCursor<TSchema>;
211
211
  streamOptions?: CursorStreamOptions;
212
212
  /** @internal */
213
- [kResumeQueue]: Denque;
213
+ [kResumeQueue]: Denque<Callback<ChangeStreamCursor<TSchema>>>;
214
214
  /** @internal */
215
215
  [kCursorStream]?: Readable;
216
216
  /** @internal */
@@ -809,6 +809,8 @@ function getCursor<T>(changeStream: ChangeStream<T>, callback: Callback<ChangeSt
809
809
  function processResumeQueue<TSchema>(changeStream: ChangeStream<TSchema>, err?: Error) {
810
810
  while (changeStream[kResumeQueue].length) {
811
811
  const request = changeStream[kResumeQueue].pop();
812
+ if (!request) break; // Should never occur but TS can't use the length check in the while condition
813
+
812
814
  if (!err) {
813
815
  if (changeStream[kClosed]) {
814
816
  // TODO(NODE-3485): Replace with MongoChangeStreamClosedError
@@ -25,6 +25,14 @@ function getDefaultAuthMechanism(ismaster?: Document): AuthMechanism {
25
25
  return AuthMechanism.MONGODB_CR;
26
26
  }
27
27
 
28
+ /** @public */
29
+ export interface AuthMechanismProperties extends Document {
30
+ SERVICE_NAME?: string;
31
+ SERVICE_REALM?: string;
32
+ CANONICALIZE_HOST_NAME?: boolean;
33
+ AWS_SESSION_TOKEN?: string;
34
+ }
35
+
28
36
  /** @public */
29
37
  export interface MongoCredentialsOptions {
30
38
  username: string;
@@ -32,7 +40,7 @@ export interface MongoCredentialsOptions {
32
40
  source: string;
33
41
  db?: string;
34
42
  mechanism?: AuthMechanism;
35
- mechanismProperties: Document;
43
+ mechanismProperties: AuthMechanismProperties;
36
44
  }
37
45
 
38
46
  /**
@@ -49,7 +57,7 @@ export class MongoCredentials {
49
57
  /** The method used to authenticate */
50
58
  readonly mechanism: AuthMechanism;
51
59
  /** Special properties used by some types of auth mechanisms */
52
- readonly mechanismProperties: Document;
60
+ readonly mechanismProperties: AuthMechanismProperties;
53
61
 
54
62
  constructor(options: MongoCredentialsOptions) {
55
63
  this.username = options.username;
@@ -70,7 +78,10 @@ export class MongoCredentials {
70
78
  this.password = process.env.AWS_SECRET_ACCESS_KEY;
71
79
  }
72
80
 
73
- if (!this.mechanismProperties.AWS_SESSION_TOKEN && process.env.AWS_SESSION_TOKEN) {
81
+ if (
82
+ this.mechanismProperties.AWS_SESSION_TOKEN == null &&
83
+ process.env.AWS_SESSION_TOKEN != null
84
+ ) {
74
85
  this.mechanismProperties = {
75
86
  ...this.mechanismProperties,
76
87
  AWS_SESSION_TOKEN: process.env.AWS_SESSION_TOKEN
@@ -14,6 +14,7 @@ import type { BSONSerializeOptions } from '../../bson';
14
14
 
15
15
  import { aws4 } from '../../deps';
16
16
  import { AuthMechanism } from './defaultAuthProviders';
17
+ import type { Binary } from 'bson';
17
18
 
18
19
  const ASCII_N = 110;
19
20
  const AWS_RELATIVE_URI = 'http://169.254.170.2';
@@ -64,10 +65,19 @@ export class MongoDBAWS extends AuthProvider {
64
65
  return;
65
66
  }
66
67
 
67
- const username = credentials.username;
68
- const password = credentials.password;
68
+ const accessKeyId = credentials.username;
69
+ const secretAccessKey = credentials.password;
70
+ const sessionToken = credentials.mechanismProperties.AWS_SESSION_TOKEN;
71
+
72
+ // If all three defined, include sessionToken, else include username and pass, else no credentials
73
+ const awsCredentials =
74
+ accessKeyId && secretAccessKey && sessionToken
75
+ ? { accessKeyId, secretAccessKey, sessionToken }
76
+ : accessKeyId && secretAccessKey
77
+ ? { accessKeyId, secretAccessKey }
78
+ : undefined;
79
+
69
80
  const db = credentials.source;
70
- const token = credentials.mechanismProperties.AWS_SESSION_TOKEN;
71
81
  crypto.randomBytes(32, (err, nonce) => {
72
82
  if (err) {
73
83
  callback(err);
@@ -83,7 +93,10 @@ export class MongoDBAWS extends AuthProvider {
83
93
  connection.command(ns(`${db}.$cmd`), saslStart, undefined, (err, res) => {
84
94
  if (err) return callback(err);
85
95
 
86
- const serverResponse = BSON.deserialize(res.payload.buffer, bsonOptions);
96
+ const serverResponse = BSON.deserialize(res.payload.buffer, bsonOptions) as {
97
+ s: Binary;
98
+ h: string;
99
+ };
87
100
  const host = serverResponse.h;
88
101
  const serverNonce = serverResponse.s.buffer;
89
102
  if (serverNonce.length !== 64) {
@@ -123,18 +136,15 @@ export class MongoDBAWS extends AuthProvider {
123
136
  path: '/',
124
137
  body
125
138
  },
126
- {
127
- accessKeyId: username,
128
- secretAccessKey: password,
129
- token
130
- }
139
+ awsCredentials
131
140
  );
132
141
 
133
- const authorization = options.headers.Authorization;
134
- const date = options.headers['X-Amz-Date'];
135
- const payload: AWSSaslContinuePayload = { a: authorization, d: date };
136
- if (token) {
137
- payload.t = token;
142
+ const payload: AWSSaslContinuePayload = {
143
+ a: options.headers.Authorization,
144
+ d: options.headers['X-Amz-Date']
145
+ };
146
+ if (sessionToken) {
147
+ payload.t = sessionToken;
138
148
  }
139
149
 
140
150
  const saslContinue = {
@@ -149,14 +159,16 @@ export class MongoDBAWS extends AuthProvider {
149
159
  }
150
160
  }
151
161
 
152
- interface AWSCredentials {
162
+ interface AWSTempCredentials {
153
163
  AccessKeyId?: string;
154
164
  SecretAccessKey?: string;
155
165
  Token?: string;
166
+ RoleArn?: string;
167
+ Expiration?: Date;
156
168
  }
157
169
 
158
170
  function makeTempCredentials(credentials: MongoCredentials, callback: Callback<MongoCredentials>) {
159
- function done(creds: AWSCredentials) {
171
+ function done(creds: AWSTempCredentials) {
160
172
  if (!creds.AccessKeyId || !creds.SecretAccessKey || !creds.Token) {
161
173
  callback(
162
174
  new MongoMissingCredentialsError('Could not obtain temporary MONGODB-AWS credentials')
@@ -183,6 +195,7 @@ function makeTempCredentials(credentials: MongoCredentials, callback: Callback<M
183
195
  if (process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI) {
184
196
  request(
185
197
  `${AWS_RELATIVE_URI}${process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI}`,
198
+ undefined,
186
199
  (err, res) => {
187
200
  if (err) return callback(err);
188
201
  done(res);
@@ -239,27 +252,15 @@ interface RequestOptions {
239
252
  headers?: http.OutgoingHttpHeaders;
240
253
  }
241
254
 
242
- function request(uri: string, callback: Callback): void;
243
- function request(uri: string, options: RequestOptions, callback: Callback): void;
244
- function request(uri: string, _options: RequestOptions | Callback, _callback?: Callback) {
245
- let options = _options as RequestOptions;
246
- if ('function' === typeof _options) {
247
- options = {};
248
- }
249
-
250
- let callback: Callback = _options as Callback;
251
- if (_callback) {
252
- callback = _callback;
253
- }
254
-
255
- options = Object.assign(
255
+ function request(uri: string, _options: RequestOptions | undefined, callback: Callback) {
256
+ const options = Object.assign(
256
257
  {
257
258
  method: 'GET',
258
259
  timeout: 10000,
259
260
  json: true
260
261
  },
261
262
  url.parse(uri),
262
- options
263
+ _options
263
264
  );
264
265
 
265
266
  const req = http.request(options, res => {
@@ -274,7 +274,7 @@ function extractCommand(command: WriteProtocolMessageType): Document {
274
274
 
275
275
  for (const k in command) {
276
276
  if (k === 'query') continue;
277
- clonedCommand[k] = deepCopy(((command as unknown) as Record<string, unknown>)[k]);
277
+ clonedCommand[k] = deepCopy((command as unknown as Record<string, unknown>)[k]);
278
278
  }
279
279
  return command.query ? clonedQuery : clonedCommand;
280
280
  }
@@ -97,6 +97,7 @@ export interface CommandOptions extends BSONSerializeOptions {
97
97
  session?: ClientSession;
98
98
  documentsReturnedIn?: string;
99
99
  noResponse?: boolean;
100
+ omitReadPreference?: boolean;
100
101
 
101
102
  // FIXME: NODE-2802
102
103
  willRetryWrite?: boolean;
@@ -1,7 +1,7 @@
1
- export const MIN_SUPPORTED_SERVER_VERSION = '2.6';
2
- export const MAX_SUPPORTED_SERVER_VERSION = '5.0';
3
- export const MIN_SUPPORTED_WIRE_VERSION = 2;
4
- export const MAX_SUPPORTED_WIRE_VERSION = 13;
1
+ export const MIN_SUPPORTED_SERVER_VERSION = '3.6';
2
+ export const MAX_SUPPORTED_SERVER_VERSION = '5.1';
3
+ export const MIN_SUPPORTED_WIRE_VERSION = 6;
4
+ export const MAX_SUPPORTED_WIRE_VERSION = 14;
5
5
  export const OP_REPLY = 1;
6
6
  export const OP_UPDATE = 2001;
7
7
  export const OP_INSERT = 2002;
package/src/collection.ts CHANGED
@@ -676,12 +676,16 @@ export class Collection<TSchema extends Document = Document> {
676
676
  * @param options - Optional settings for the command
677
677
  * @param callback - An optional callback, a Promise will be returned if none is provided
678
678
  */
679
- findOne(): Promise<TSchema | null>;
680
- findOne(callback: Callback<TSchema | null>): void;
681
- findOne(filter: Filter<TSchema>): Promise<TSchema | null>;
682
- findOne(filter: Filter<TSchema>, callback: Callback<TSchema | null>): void;
683
- findOne(filter: Filter<TSchema>, options: FindOptions): Promise<TSchema | null>;
684
- findOne(filter: Filter<TSchema>, options: FindOptions, callback: Callback<TSchema | null>): void;
679
+ findOne(): Promise<WithId<TSchema> | null>;
680
+ findOne(callback: Callback<WithId<TSchema> | null>): void;
681
+ findOne(filter: Filter<TSchema>): Promise<WithId<TSchema> | null>;
682
+ findOne(filter: Filter<TSchema>, callback: Callback<WithId<TSchema> | null>): void;
683
+ findOne(filter: Filter<TSchema>, options: FindOptions): Promise<WithId<TSchema> | null>;
684
+ findOne(
685
+ filter: Filter<TSchema>,
686
+ options: FindOptions,
687
+ callback: Callback<WithId<TSchema> | null>
688
+ ): void;
685
689
 
686
690
  // allow an override of the schema.
687
691
  findOne<T = TSchema>(): Promise<T | null>;
@@ -695,10 +699,10 @@ export class Collection<TSchema extends Document = Document> {
695
699
  ): void;
696
700
 
697
701
  findOne(
698
- filter?: Filter<TSchema> | Callback<TSchema | null>,
699
- options?: FindOptions | Callback<TSchema | null>,
700
- callback?: Callback<TSchema | null>
701
- ): Promise<TSchema | null> | void {
702
+ filter?: Filter<TSchema> | Callback<WithId<TSchema> | null>,
703
+ options?: FindOptions | Callback<WithId<TSchema> | null>,
704
+ callback?: Callback<WithId<TSchema> | null>
705
+ ): Promise<WithId<TSchema> | null> | void {
702
706
  if (callback != null && typeof callback !== 'function') {
703
707
  throw new MongoInvalidArgumentError(
704
708
  'Third parameter to `findOne()` must be a callback or undefined'
@@ -706,7 +710,7 @@ export class Collection<TSchema extends Document = Document> {
706
710
  }
707
711
 
708
712
  if (typeof filter === 'function') {
709
- callback = filter as Callback<TSchema | null>;
713
+ callback = filter as Callback<WithId<TSchema> | null>;
710
714
  filter = {};
711
715
  options = {};
712
716
  }
@@ -725,10 +729,10 @@ export class Collection<TSchema extends Document = Document> {
725
729
  *
726
730
  * @param filter - The filter predicate. If unspecified, then all documents in the collection will match the predicate
727
731
  */
728
- find(): FindCursor<TSchema>;
729
- find(filter: Filter<TSchema>, options?: FindOptions): FindCursor<TSchema>;
730
- find<T>(filter: Filter<TSchema>, options?: FindOptions): FindCursor<T>;
731
- find(filter?: Filter<TSchema>, options?: FindOptions): FindCursor<TSchema> {
732
+ find(): FindCursor<WithId<TSchema>>;
733
+ find(filter: Filter<WithId<TSchema>>, options?: FindOptions): FindCursor<WithId<TSchema>>;
734
+ find<T>(filter: Filter<WithId<TSchema>>, options?: FindOptions): FindCursor<T>;
735
+ find(filter?: Filter<WithId<TSchema>>, options?: FindOptions): FindCursor<WithId<TSchema>> {
732
736
  if (arguments.length > 2) {
733
737
  throw new MongoInvalidArgumentError(
734
738
  'Method "collection.find()" accepts at most two arguments'
@@ -738,7 +742,7 @@ export class Collection<TSchema extends Document = Document> {
738
742
  throw new MongoInvalidArgumentError('Argument "options" must not be function');
739
743
  }
740
744
 
741
- return new FindCursor<TSchema>(
745
+ return new FindCursor<WithId<TSchema>>(
742
746
  getTopology(this),
743
747
  this.s.namespace,
744
748
  filter,
@@ -1190,14 +1194,14 @@ export class Collection<TSchema extends Document = Document> {
1190
1194
  * @param options - Optional settings for the command
1191
1195
  * @param callback - An optional callback, a Promise will be returned if none is provided
1192
1196
  */
1193
- indexes(): Promise<Document>;
1194
- indexes(callback: Callback<Document>): void;
1195
- indexes(options: IndexInformationOptions): Promise<Document>;
1196
- indexes(options: IndexInformationOptions, callback: Callback<Document>): void;
1197
+ indexes(): Promise<Document[]>;
1198
+ indexes(callback: Callback<Document[]>): void;
1199
+ indexes(options: IndexInformationOptions): Promise<Document[]>;
1200
+ indexes(options: IndexInformationOptions, callback: Callback<Document[]>): void;
1197
1201
  indexes(
1198
- options?: IndexInformationOptions | Callback<Document>,
1199
- callback?: Callback<Document>
1200
- ): Promise<Document> | void {
1202
+ options?: IndexInformationOptions | Callback<Document[]>,
1203
+ callback?: Callback<Document[]>
1204
+ ): Promise<Document[]> | void {
1201
1205
  if (typeof options === 'function') (callback = options), (options = {});
1202
1206
 
1203
1207
  return executeOperation(
@@ -1366,7 +1370,7 @@ export class Collection<TSchema extends Document = Document> {
1366
1370
  * @param pipeline - An array of aggregation pipelines to execute
1367
1371
  * @param options - Optional settings for the command
1368
1372
  */
1369
- aggregate<T = TSchema>(
1373
+ aggregate<T = Document>(
1370
1374
  pipeline: Document[] = [],
1371
1375
  options?: AggregateOptions
1372
1376
  ): AggregationCursor<T> {
@@ -1415,6 +1419,7 @@ export class Collection<TSchema extends Document = Document> {
1415
1419
  /**
1416
1420
  * Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
1417
1421
  *
1422
+ * @deprecated collection.mapReduce is deprecated. Use the aggregation pipeline instead. Visit https://docs.mongodb.com/manual/reference/map-reduce-to-aggregation-pipeline for more information on how to translate map-reduce operations to the aggregation pipeline.
1418
1423
  * @param map - The mapping function.
1419
1424
  * @param reduce - The reduce function.
1420
1425
  * @param options - Optional settings for the command
@@ -1446,6 +1451,9 @@ export class Collection<TSchema extends Document = Document> {
1446
1451
  options?: MapReduceOptions<TKey, TValue> | Callback<Document | Document[]>,
1447
1452
  callback?: Callback<Document | Document[]>
1448
1453
  ): Promise<Document | Document[]> | void {
1454
+ emitWarningOnce(
1455
+ 'collection.mapReduce is deprecated. Use the aggregation pipeline instead. Visit https://docs.mongodb.com/manual/reference/map-reduce-to-aggregation-pipeline for more information on how to translate map-reduce operations to the aggregation pipeline.'
1456
+ );
1449
1457
  if ('function' === typeof options) (callback = options), (options = {});
1450
1458
  // Out must always be defined (make sure we don't break weirdly on pre 1.8+ servers)
1451
1459
  // TODO NODE-3339: Figure out if this is still necessary given we no longer officially support pre-1.8