mongodb 4.1.2 → 4.2.1

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 (161) hide show
  1. package/README.md +17 -16
  2. package/lib/admin.js +5 -5
  3. package/lib/admin.js.map +1 -1
  4. package/lib/bson.js.map +1 -1
  5. package/lib/bulk/common.js +54 -56
  6. package/lib/bulk/common.js.map +1 -1
  7. package/lib/change_stream.js +13 -14
  8. package/lib/change_stream.js.map +1 -1
  9. package/lib/cmap/auth/gssapi.js +1 -1
  10. package/lib/cmap/auth/gssapi.js.map +1 -1
  11. package/lib/cmap/auth/mongocr.js +2 -2
  12. package/lib/cmap/auth/mongocr.js.map +1 -1
  13. package/lib/cmap/auth/mongodb_aws.js +3 -3
  14. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  15. package/lib/cmap/auth/plain.js +1 -1
  16. package/lib/cmap/auth/plain.js.map +1 -1
  17. package/lib/cmap/auth/scram.js +5 -5
  18. package/lib/cmap/auth/scram.js.map +1 -1
  19. package/lib/cmap/auth/x509.js +1 -1
  20. package/lib/cmap/auth/x509.js.map +1 -1
  21. package/lib/cmap/command_monitoring_events.js +15 -15
  22. package/lib/cmap/command_monitoring_events.js.map +1 -1
  23. package/lib/cmap/commands.js +10 -7
  24. package/lib/cmap/commands.js.map +1 -1
  25. package/lib/cmap/connect.js +2 -2
  26. package/lib/cmap/connect.js.map +1 -1
  27. package/lib/cmap/connection.js +15 -15
  28. package/lib/cmap/connection.js.map +1 -1
  29. package/lib/cmap/connection_pool.js +3 -3
  30. package/lib/cmap/connection_pool.js.map +1 -1
  31. package/lib/cmap/message_stream.js +2 -2
  32. package/lib/cmap/message_stream.js.map +1 -1
  33. package/lib/cmap/stream_description.js +1 -1
  34. package/lib/cmap/stream_description.js.map +1 -1
  35. package/lib/cmap/wire_protocol/constants.js +4 -4
  36. package/lib/collection.js +42 -41
  37. package/lib/collection.js.map +1 -1
  38. package/lib/connection_string.js +73 -45
  39. package/lib/connection_string.js.map +1 -1
  40. package/lib/cursor/abstract_cursor.js +16 -13
  41. package/lib/cursor/abstract_cursor.js.map +1 -1
  42. package/lib/cursor/aggregation_cursor.js +14 -14
  43. package/lib/cursor/aggregation_cursor.js.map +1 -1
  44. package/lib/cursor/find_cursor.js +23 -23
  45. package/lib/cursor/find_cursor.js.map +1 -1
  46. package/lib/db.js +20 -20
  47. package/lib/db.js.map +1 -1
  48. package/lib/deps.js +1 -1
  49. package/lib/deps.js.map +1 -1
  50. package/lib/gridfs/index.js +3 -3
  51. package/lib/gridfs/index.js.map +1 -1
  52. package/lib/gridfs/upload.js +1 -1
  53. package/lib/gridfs/upload.js.map +1 -1
  54. package/lib/logger.js +5 -5
  55. package/lib/logger.js.map +1 -1
  56. package/lib/mongo_client.js +7 -7
  57. package/lib/mongo_client.js.map +1 -1
  58. package/lib/mongo_types.js.map +1 -1
  59. package/lib/operations/add_user.js +3 -3
  60. package/lib/operations/add_user.js.map +1 -1
  61. package/lib/operations/aggregate.js +3 -4
  62. package/lib/operations/aggregate.js.map +1 -1
  63. package/lib/operations/bulk_write.js +1 -1
  64. package/lib/operations/bulk_write.js.map +1 -1
  65. package/lib/operations/command.js +7 -3
  66. package/lib/operations/command.js.map +1 -1
  67. package/lib/operations/common_functions.js +1 -1
  68. package/lib/operations/common_functions.js.map +1 -1
  69. package/lib/operations/connect.js +1 -1
  70. package/lib/operations/connect.js.map +1 -1
  71. package/lib/operations/count.js +1 -1
  72. package/lib/operations/count.js.map +1 -1
  73. package/lib/operations/create_collection.js +1 -1
  74. package/lib/operations/create_collection.js.map +1 -1
  75. package/lib/operations/delete.js +6 -6
  76. package/lib/operations/delete.js.map +1 -1
  77. package/lib/operations/distinct.js +4 -4
  78. package/lib/operations/distinct.js.map +1 -1
  79. package/lib/operations/drop.js +2 -2
  80. package/lib/operations/drop.js.map +1 -1
  81. package/lib/operations/estimated_document_count.js +2 -2
  82. package/lib/operations/estimated_document_count.js.map +1 -1
  83. package/lib/operations/execute_operation.js +24 -7
  84. package/lib/operations/execute_operation.js.map +1 -1
  85. package/lib/operations/find.js +8 -8
  86. package/lib/operations/find.js.map +1 -1
  87. package/lib/operations/find_and_modify.js +7 -7
  88. package/lib/operations/find_and_modify.js.map +1 -1
  89. package/lib/operations/get_more.js +28 -0
  90. package/lib/operations/get_more.js.map +1 -0
  91. package/lib/operations/indexes.js +14 -14
  92. package/lib/operations/indexes.js.map +1 -1
  93. package/lib/operations/insert.js +5 -5
  94. package/lib/operations/insert.js.map +1 -1
  95. package/lib/operations/list_collections.js +12 -7
  96. package/lib/operations/list_collections.js.map +1 -1
  97. package/lib/operations/list_databases.js +1 -1
  98. package/lib/operations/list_databases.js.map +1 -1
  99. package/lib/operations/map_reduce.js +6 -6
  100. package/lib/operations/map_reduce.js.map +1 -1
  101. package/lib/operations/operation.js +4 -2
  102. package/lib/operations/operation.js.map +1 -1
  103. package/lib/operations/remove_user.js +1 -1
  104. package/lib/operations/remove_user.js.map +1 -1
  105. package/lib/operations/rename.js +2 -2
  106. package/lib/operations/rename.js.map +1 -1
  107. package/lib/operations/set_profiling_level.js +1 -1
  108. package/lib/operations/set_profiling_level.js.map +1 -1
  109. package/lib/operations/stats.js +2 -2
  110. package/lib/operations/stats.js.map +1 -1
  111. package/lib/operations/update.js +12 -12
  112. package/lib/operations/update.js.map +1 -1
  113. package/lib/sdam/monitor.js +14 -14
  114. package/lib/sdam/monitor.js.map +1 -1
  115. package/lib/sdam/server.js +19 -12
  116. package/lib/sdam/server.js.map +1 -1
  117. package/lib/sdam/server_description.js +3 -3
  118. package/lib/sdam/server_description.js.map +1 -1
  119. package/lib/sdam/server_selection.js +36 -1
  120. package/lib/sdam/server_selection.js.map +1 -1
  121. package/lib/sdam/srv_polling.js +9 -9
  122. package/lib/sdam/srv_polling.js.map +1 -1
  123. package/lib/sdam/topology.js +29 -21
  124. package/lib/sdam/topology.js.map +1 -1
  125. package/lib/sdam/topology_description.js +34 -12
  126. package/lib/sdam/topology_description.js.map +1 -1
  127. package/lib/sessions.js +19 -19
  128. package/lib/sessions.js.map +1 -1
  129. package/lib/transactions.js.map +1 -1
  130. package/lib/utils.js +53 -46
  131. package/lib/utils.js.map +1 -1
  132. package/mongodb.d.ts +62 -30
  133. package/mongodb.ts34.d.ts +63 -30
  134. package/package.json +32 -30
  135. package/src/bson.ts +1 -0
  136. package/src/bulk/common.ts +48 -35
  137. package/src/change_stream.ts +10 -8
  138. package/src/cmap/commands.ts +11 -8
  139. package/src/cmap/connection.ts +1 -0
  140. package/src/cmap/wire_protocol/constants.ts +4 -4
  141. package/src/collection.ts +24 -16
  142. package/src/connection_string.ts +75 -36
  143. package/src/cursor/abstract_cursor.ts +9 -11
  144. package/src/db.ts +5 -6
  145. package/src/mongo_client.ts +12 -0
  146. package/src/mongo_types.ts +11 -14
  147. package/src/operations/aggregate.ts +1 -2
  148. package/src/operations/command.ts +5 -0
  149. package/src/operations/create_collection.ts +1 -1
  150. package/src/operations/execute_operation.ts +22 -2
  151. package/src/operations/get_more.ts +49 -0
  152. package/src/operations/list_collections.ts +12 -4
  153. package/src/operations/list_databases.ts +6 -1
  154. package/src/operations/operation.ts +5 -1
  155. package/src/sdam/server.ts +8 -0
  156. package/src/sdam/server_selection.ts +43 -0
  157. package/src/sdam/srv_polling.ts +12 -11
  158. package/src/sdam/topology.ts +27 -10
  159. package/src/sdam/topology_description.ts +35 -11
  160. package/src/transactions.ts +2 -2
  161. package/src/utils.ts +67 -56
package/mongodb.ts34.d.ts CHANGED
@@ -25,6 +25,7 @@ import { Readable } from 'stream';
25
25
  import { serialize as serialize_2 } from 'bson';
26
26
  import { SerializeOptions } from 'bson';
27
27
  import { Socket } from 'net';
28
+ import { SrvRecord } from 'dns';
28
29
  import { TcpNetConnectOpts } from 'net';
29
30
  import { Timestamp } from 'bson';
30
31
  import { TLSSocket } from 'tls';
@@ -649,7 +650,7 @@ export { BSONRegExp };
649
650
  * BSON Serialization options.
650
651
  * @public
651
652
  */
652
- export declare interface BSONSerializeOptions extends Pick<SerializeOptions, Exclude<keyof SerializeOptions, 'index'>>, Pick<DeserializeOptions, Exclude<keyof DeserializeOptions, 'evalFunctions' | 'cacheFunctions' | 'cacheFunctionsCrc32' | 'allowObjectSmallerThanBufferSize' | 'index'>> {
653
+ export declare interface BSONSerializeOptions extends Pick<SerializeOptions, Exclude<keyof SerializeOptions, 'index'>>, Pick<DeserializeOptions, Exclude<keyof DeserializeOptions, 'evalFunctions' | 'cacheFunctions' | 'cacheFunctionsCrc32' | 'allowObjectSmallerThanBufferSize' | 'index' | 'validation'>> {
653
654
  /** Return BSON filled buffers from operations */
654
655
  raw?: boolean;
655
656
  }
@@ -746,8 +747,10 @@ export declare abstract class BulkOperationBase {
746
747
  readonly bsonOptions: BSONSerializeOptions;
747
748
  readonly writeConcern: WriteConcern | undefined;
748
749
  readonly batches: Batch[];
749
- /** An internal helper method. Do not invoke directly. Will be going away in the future */
750
- execute(options?: BulkWriteOptions, callback?: Callback<BulkWriteResult>): Promise<BulkWriteResult> | void;
750
+ execute(options?: BulkWriteOptions): Promise<BulkWriteResult>;
751
+ execute(callback: Callback<BulkWriteResult>): void;
752
+ execute(options: BulkWriteOptions | undefined, callback: Callback<BulkWriteResult>): void;
753
+ execute(options?: BulkWriteOptions | Callback<BulkWriteResult>, callback?: Callback<BulkWriteResult>): Promise<BulkWriteResult> | void;
751
754
  /* Excluded from this release type: handleWriteError */
752
755
  abstract addToOperationsList(batchType: BatchType, document: Document | UpdateStatement | DeleteStatement): this;
753
756
  }
@@ -860,7 +863,7 @@ export declare class CancellationToken extends TypedEventEmitter<{
860
863
  * Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}.
861
864
  * @public
862
865
  */
863
- export declare class ChangeStream<TSchema extends Document = Document> extends TypedEventEmitter<ChangeStreamEvents> {
866
+ export declare class ChangeStream<TSchema extends Document = Document> extends TypedEventEmitter<ChangeStreamEvents<TSchema>> {
864
867
  pipeline: Document[];
865
868
  options: ChangeStreamOptions;
866
869
  parent: MongoClient | Db | Collection;
@@ -901,7 +904,8 @@ export declare class ChangeStream<TSchema extends Document = Document> extends T
901
904
  The cached resume token that is used to resume after the most recently returned change. */
902
905
  readonly resumeToken: ResumeToken;
903
906
  /** Check if there is any document still available in the Change Stream */
904
- hasNext(callback?: Callback): Promise<void> | void;
907
+ hasNext(): Promise<boolean>;
908
+ hasNext(callback: Callback<boolean>): void;
905
909
  /** Get the next available document from the Change Stream. */
906
910
  next(): Promise<ChangeStreamDocument<TSchema>>;
907
911
  next(callback: Callback<ChangeStreamDocument<TSchema>>): void;
@@ -974,14 +978,14 @@ export declare interface ChangeStreamDocument<TSchema extends Document = Documen
974
978
  fullDocument?: TSchema;
975
979
  }
976
980
  /** @public */
977
- export declare type ChangeStreamEvents = {
981
+ export declare type ChangeStreamEvents<TSchema extends Document = Document> = {
978
982
  resumeTokenChanged(token: ResumeToken): void;
979
- init(response: Document): void;
980
- more(response?: Document | undefined): void;
983
+ init(response: TSchema): void;
984
+ more(response?: TSchema | undefined): void;
981
985
  response(): void;
982
986
  end(): void;
983
987
  error(error: Error): void;
984
- change(change: ChangeStreamDocument): void;
988
+ change(change: ChangeStreamDocument<TSchema>): void;
985
989
  } & AbstractCursorEvents;
986
990
  /**
987
991
  * Options that can be passed to a ChangeStream. Note that startAfter, resumeAfter, and startAtOperationTime are all mutually exclusive, and the server will error if more than one is specified.
@@ -1381,12 +1385,12 @@ export declare class Collection<TSchema extends Document = Document> {
1381
1385
  * @param options - Optional settings for the command
1382
1386
  * @param callback - An optional callback, a Promise will be returned if none is provided
1383
1387
  */
1384
- findOne(): Promise<TSchema | null>;
1385
- findOne(callback: Callback<TSchema | null>): void;
1386
- findOne(filter: Filter<TSchema>): Promise<TSchema | null>;
1387
- findOne(filter: Filter<TSchema>, callback: Callback<TSchema | null>): void;
1388
- findOne(filter: Filter<TSchema>, options: FindOptions): Promise<TSchema | null>;
1389
- findOne(filter: Filter<TSchema>, options: FindOptions, callback: Callback<TSchema | null>): void;
1388
+ findOne(): Promise<WithId<TSchema> | null>;
1389
+ findOne(callback: Callback<WithId<TSchema> | null>): void;
1390
+ findOne(filter: Filter<TSchema>): Promise<WithId<TSchema> | null>;
1391
+ findOne(filter: Filter<TSchema>, callback: Callback<WithId<TSchema> | null>): void;
1392
+ findOne(filter: Filter<TSchema>, options: FindOptions): Promise<WithId<TSchema> | null>;
1393
+ findOne(filter: Filter<TSchema>, options: FindOptions, callback: Callback<WithId<TSchema> | null>): void;
1390
1394
  findOne<T = TSchema>(): Promise<T | null>;
1391
1395
  findOne<T = TSchema>(callback: Callback<T | null>): void;
1392
1396
  findOne<T = TSchema>(filter: Filter<TSchema>): Promise<T | null>;
@@ -1397,8 +1401,8 @@ export declare class Collection<TSchema extends Document = Document> {
1397
1401
  *
1398
1402
  * @param filter - The filter predicate. If unspecified, then all documents in the collection will match the predicate
1399
1403
  */
1400
- find(): FindCursor<TSchema>;
1401
- find(filter: Filter<TSchema>, options?: FindOptions): FindCursor<TSchema>;
1404
+ find(): FindCursor<WithId<TSchema>>;
1405
+ find(filter: Filter<TSchema>, options?: FindOptions): FindCursor<WithId<TSchema>>;
1402
1406
  find<T>(filter: Filter<TSchema>, options?: FindOptions): FindCursor<T>;
1403
1407
  /**
1404
1408
  * Returns the options of the collection.
@@ -1661,7 +1665,7 @@ export declare class Collection<TSchema extends Document = Document> {
1661
1665
  * @param pipeline - An array of aggregation pipelines to execute
1662
1666
  * @param options - Optional settings for the command
1663
1667
  */
1664
- aggregate<T = TSchema>(pipeline?: Document[], options?: AggregateOptions): AggregationCursor<T>;
1668
+ aggregate<T = Document>(pipeline?: Document[], options?: AggregateOptions): AggregationCursor<T>;
1665
1669
  /**
1666
1670
  * Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this collection.
1667
1671
  *
@@ -1673,6 +1677,7 @@ export declare class Collection<TSchema extends Document = Document> {
1673
1677
  /**
1674
1678
  * Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
1675
1679
  *
1680
+ * @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.
1676
1681
  * @param map - The mapping function.
1677
1682
  * @param reduce - The reduce function.
1678
1683
  * @param options - Optional settings for the command
@@ -2225,10 +2230,9 @@ export declare class Db {
2225
2230
  * @param options - Optional settings for the command
2226
2231
  * @param callback - An optional callback, a Promise will be returned if none is provided
2227
2232
  */
2228
- createCollection<TSchema extends Document = Document>(name: string): Promise<Collection<TSchema>>;
2233
+ createCollection<TSchema extends Document = Document>(name: string, options?: CreateCollectionOptions): Promise<Collection<TSchema>>;
2229
2234
  createCollection<TSchema extends Document = Document>(name: string, callback: Callback<Collection<TSchema>>): void;
2230
- createCollection<TSchema extends Document = Document>(name: string, options: CreateCollectionOptions): Promise<Collection<TSchema>>;
2231
- createCollection<TSchema extends Document = Document>(name: string, options: CreateCollectionOptions, callback: Callback<Collection<TSchema>>): void;
2235
+ createCollection<TSchema extends Document = Document>(name: string, options: CreateCollectionOptions | undefined, callback: Callback<Collection<TSchema>>): void;
2232
2236
  /**
2233
2237
  * Execute a command
2234
2238
  *
@@ -2579,8 +2583,8 @@ export declare type ExplainVerbosity = string;
2579
2583
  export declare type ExplainVerbosityLike = ExplainVerbosity | boolean;
2580
2584
  /** A MongoDB filter can be some portion of the schema or a set of operators @public */
2581
2585
  export declare type Filter<TSchema> = {
2582
- [P in keyof TSchema]?: Condition<TSchema[P]>;
2583
- } & RootFilterOperators<TSchema>;
2586
+ [P in keyof WithId<TSchema>]?: Condition<WithId<TSchema>[P]>;
2587
+ } & RootFilterOperators<WithId<TSchema>>;
2584
2588
  /** @public */
2585
2589
  export declare type FilterOperations<T> = T extends Record<string, any> ? {
2586
2590
  [key in keyof T]?: FilterOperators<T[key]>;
@@ -3181,11 +3185,13 @@ export declare class HostAddress {
3181
3185
  socketPath: string | undefined;
3182
3186
  isIPv6: boolean | undefined;
3183
3187
  constructor(hostString: string);
3188
+ inspect(): string;
3184
3189
  /**
3185
3190
  * @param ipv6Brackets - optionally request ipv6 bracket notation required for connection strings
3186
3191
  */
3187
3192
  toString(ipv6Brackets?: boolean): string;
3188
3193
  static fromString(s: string): HostAddress;
3194
+ static fromSrvRecord({ name, port }: SrvRecord): HostAddress;
3189
3195
  }
3190
3196
  /** @public */
3191
3197
  export declare interface IndexDescription extends Pick<CreateIndexesOptions, 'background' | 'unique' | 'partialFilterExpression' | 'sparse' | 'hidden' | 'expireAfterSeconds' | 'storageEngine' | 'version' | 'weights' | 'default_language' | 'language_override' | 'textIndexVersion' | '2dsphereIndexVersion' | 'bits' | 'min' | 'max' | 'bucketSize' | 'wildcardProjection'> {
@@ -3366,6 +3372,8 @@ export declare class ListCollectionsCursor<T extends Pick<CollectionInfo, 'name'
3366
3372
  export declare interface ListCollectionsOptions extends CommandOperationOptions {
3367
3373
  /** Since 4.0: If true, will only return the collection name in the response, and will omit additional info */
3368
3374
  nameOnly?: boolean;
3375
+ /** Since 4.0: If true and nameOnly is true, allows a user without the required privilege (i.e. listCollections action on the database) to run the command when access control is enforced. */
3376
+ authorizedCollections?: boolean;
3369
3377
  /** The batchSize for the returned command cursor or if pre 2.8 the systems batch collection */
3370
3378
  batchSize?: number;
3371
3379
  }
@@ -3379,7 +3387,16 @@ export declare interface ListDatabasesOptions extends CommandOperationOptions {
3379
3387
  authorizedDatabases?: boolean;
3380
3388
  }
3381
3389
  /** @public */
3382
- export declare type ListDatabasesResult = string[] | Document[];
3390
+ export declare interface ListDatabasesResult {
3391
+ databases: ({
3392
+ name: string;
3393
+ sizeOnDisk?: number;
3394
+ empty?: boolean;
3395
+ } & Document)[];
3396
+ totalSize?: number;
3397
+ totalSizeMb?: number;
3398
+ ok: 1 | 0;
3399
+ }
3383
3400
  /** @public */
3384
3401
  export declare class ListIndexesCursor extends AbstractCursor {
3385
3402
  parent: Collection;
@@ -3769,6 +3786,16 @@ export declare interface MongoClientOptions extends BSONSerializeOptions, Suppor
3769
3786
  compressors?: CompressorName[] | string;
3770
3787
  /** An integer that specifies the compression level if using zlib for network compression. */
3771
3788
  zlibCompressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | undefined;
3789
+ /** The maximum number of hosts to connect to when using an srv connection string, a setting of `0` means unlimited hosts */
3790
+ srvMaxHosts?: number;
3791
+ /**
3792
+ * Modifies the srv URI to look like:
3793
+ *
3794
+ * `_{srvServiceName}._tcp.{hostname}.{domainname}`
3795
+ *
3796
+ * Querying this DNS URI is expected to respond with SRV records
3797
+ */
3798
+ srvServiceName?: string;
3772
3799
  /** The maximum number of connections in the connection pool. */
3773
3800
  maxPoolSize?: number;
3774
3801
  /** The minimum number of connections in the connection pool. */
@@ -4127,7 +4154,7 @@ export declare class MongoNotConnectedError extends MongoAPIError {
4127
4154
  * Mongo Client Options
4128
4155
  * @public
4129
4156
  */
4130
- export declare interface MongoOptions extends Required<Pick<MongoClientOptions, 'autoEncryption' | 'connectTimeoutMS' | 'directConnection' | 'driverInfo' | 'forceServerObjectId' | 'minHeartbeatFrequencyMS' | 'heartbeatFrequencyMS' | 'keepAlive' | 'keepAliveInitialDelay' | 'localThresholdMS' | 'logger' | 'maxIdleTimeMS' | 'maxPoolSize' | 'minPoolSize' | 'monitorCommands' | 'noDelay' | 'pkFactory' | 'promiseLibrary' | 'raw' | 'replicaSet' | 'retryReads' | 'retryWrites' | 'serverSelectionTimeoutMS' | 'socketTimeoutMS' | 'tlsAllowInvalidCertificates' | 'tlsAllowInvalidHostnames' | 'tlsInsecure' | 'waitQueueTimeoutMS' | 'zlibCompressionLevel'>>, SupportedNodeConnectionOptions {
4157
+ export declare interface MongoOptions extends Required<Pick<MongoClientOptions, 'autoEncryption' | 'connectTimeoutMS' | 'directConnection' | 'driverInfo' | 'forceServerObjectId' | 'minHeartbeatFrequencyMS' | 'heartbeatFrequencyMS' | 'keepAlive' | 'keepAliveInitialDelay' | 'localThresholdMS' | 'logger' | 'maxIdleTimeMS' | 'maxPoolSize' | 'minPoolSize' | 'monitorCommands' | 'noDelay' | 'pkFactory' | 'promiseLibrary' | 'raw' | 'replicaSet' | 'retryReads' | 'retryWrites' | 'serverSelectionTimeoutMS' | 'socketTimeoutMS' | 'srvMaxHosts' | 'srvServiceName' | 'tlsAllowInvalidCertificates' | 'tlsAllowInvalidHostnames' | 'tlsInsecure' | 'waitQueueTimeoutMS' | 'zlibCompressionLevel'>>, SupportedNodeConnectionOptions {
4131
4158
  hosts: HostAddress[];
4132
4159
  srvHost?: string;
4133
4160
  credentials?: MongoCredentials;
@@ -4312,6 +4339,8 @@ export declare interface OperationOptions extends BSONSerializeOptions {
4312
4339
  willRetryWrites?: boolean;
4313
4340
  /** The preferred read preference (ReadPreference.primary, ReadPreference.primary_preferred, ReadPreference.secondary, ReadPreference.secondary_preferred, ReadPreference.nearest). */
4314
4341
  readPreference?: ReadPreferenceLike;
4342
+ /* Excluded from this release type: bypassPinningCheck */
4343
+ omitReadPreference?: boolean;
4315
4344
  }
4316
4345
  /* Excluded from this release type: OperationParent */
4317
4346
  /**
@@ -4332,11 +4361,13 @@ export declare type OperationTime = Timestamp;
4332
4361
  * `TSchema['_id'] extends ObjectId` which translated to "Is the _id property ObjectId?"
4333
4362
  * we instead ask "Does ObjectId look like (have the same shape) as the _id?"
4334
4363
  */
4335
- export declare type OptionalId<TSchema extends {
4364
+ export declare type OptionalId<TSchema> = TSchema extends {
4336
4365
  _id?: any;
4337
- }> = ObjectId extends TSchema['_id'] ? EnhancedOmit<TSchema, '_id'> & {
4366
+ } ? ObjectId extends TSchema['_id'] ? EnhancedOmit<TSchema, '_id'> & {
4367
+ _id?: InferIdType<TSchema>;
4368
+ } : WithId<TSchema> : EnhancedOmit<TSchema, '_id'> & {
4338
4369
  _id?: InferIdType<TSchema>;
4339
- } : WithId<TSchema>;
4370
+ };
4340
4371
  /** @public */
4341
4372
  export declare class OrderedBulkOperation extends BulkOperationBase {
4342
4373
  constructor(collection: Collection, options: BulkWriteOptions);
@@ -4927,7 +4958,7 @@ export declare type TagSet = {
4927
4958
  export declare interface TimeSeriesCollectionOptions extends Document {
4928
4959
  timeField: string;
4929
4960
  metaField?: string;
4930
- granularity?: string;
4961
+ granularity?: 'seconds' | 'minutes' | 'hours' | string;
4931
4962
  }
4932
4963
  export { Timestamp };
4933
4964
  /* Excluded from this release type: Topology */
@@ -5016,6 +5047,8 @@ export declare class TopologyOpeningEvent {
5016
5047
  }
5017
5048
  /** @public */
5018
5049
  export declare interface TopologyOptions extends BSONSerializeOptions, ServerOptions {
5050
+ srvMaxHosts: number;
5051
+ srvServiceName: string;
5019
5052
  hosts: HostAddress[];
5020
5053
  retryWrites: boolean;
5021
5054
  retryReads: boolean;
@@ -5079,7 +5112,7 @@ export declare class Transaction {
5079
5112
  */
5080
5113
  export declare interface TransactionOptions extends CommandOperationOptions {
5081
5114
  /** A default read concern for commands in this transaction */
5082
- readConcern?: ReadConcern;
5115
+ readConcern?: ReadConcernLike;
5083
5116
  /** A default writeConcern for commands in this transaction */
5084
5117
  writeConcern?: WriteConcern;
5085
5118
  /** A default read preference for commands in this transaction */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb",
3
- "version": "4.1.2",
3
+ "version": "4.2.1",
4
4
  "description": "The official MongoDB driver for Node.js",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -32,56 +32,54 @@
32
32
  "email": "dbx-node@mongodb.com"
33
33
  },
34
34
  "dependencies": {
35
- "bson": "^4.5.2",
35
+ "bson": "^4.6.0",
36
36
  "denque": "^2.0.1",
37
- "mongodb-connection-string-url": "^2.0.0"
37
+ "mongodb-connection-string-url": "^2.2.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@istanbuljs/nyc-config-typescript": "^1.0.1",
41
- "@microsoft/api-extractor": "^7.18.7",
41
+ "@microsoft/api-extractor": "^7.18.19",
42
42
  "@microsoft/tsdoc-config": "^0.15.2",
43
- "@types/chai": "^4.2.21",
43
+ "@types/chai": "^4.2.22",
44
44
  "@types/chai-subset": "^1.3.3",
45
45
  "@types/kerberos": "^1.1.1",
46
46
  "@types/mocha": "^9.0.0",
47
- "@types/node": "^16.7.13",
47
+ "@types/node": "^16.11.8",
48
48
  "@types/saslprep": "^1.0.1",
49
- "@types/semver": "^7.3.8",
49
+ "@types/semver": "^7.3.9",
50
+ "@types/sinon": "^10.0.6",
50
51
  "@types/whatwg-url": "^8.2.1",
51
- "@typescript-eslint/eslint-plugin": "^4.31.0",
52
- "@typescript-eslint/parser": "^4.31.0",
52
+ "@typescript-eslint/eslint-plugin": "^5.4.0",
53
+ "@typescript-eslint/parser": "^5.4.0",
53
54
  "bluebird": "^3.7.2",
54
55
  "chai": "^4.3.4",
55
56
  "chai-subset": "^1.6.0",
56
57
  "chalk": "^4.1.2",
57
58
  "co": "4.6.0",
58
59
  "downlevel-dts": "^0.7.0",
59
- "eslint": "^7.32.0",
60
+ "eslint": "^8.2.0",
60
61
  "eslint-config-prettier": "^8.3.0",
61
- "eslint-plugin-jsdoc": "^36.1.0",
62
62
  "eslint-plugin-prettier": "^4.0.0",
63
63
  "eslint-plugin-tsdoc": "^0.2.14",
64
64
  "js-yaml": "^4.1.0",
65
- "jsdoc": "^3.6.7",
66
65
  "lodash.camelcase": "^4.3.0",
67
- "mocha": "^9.1.1",
66
+ "mocha": "^9.1.3",
68
67
  "mocha-sinon": "^2.1.2",
69
68
  "mongodb-mock-server": "^2.0.1",
70
69
  "nyc": "^15.1.0",
71
- "prettier": "^2.3.2",
70
+ "prettier": "^2.4.1",
72
71
  "rimraf": "^3.0.2",
73
72
  "semver": "^7.3.5",
74
- "sinon": "^11.1.2",
73
+ "sinon": "^12.0.1",
75
74
  "sinon-chai": "^3.7.0",
76
- "source-map-support": "^0.5.19",
77
- "standard-version": "^9.3.1",
78
- "ts-node": "^10.2.1",
79
- "tsd": "^0.17.0",
80
- "typedoc": "^0.21.9",
81
- "typescript": "4.3.5",
75
+ "source-map-support": "^0.5.20",
76
+ "standard-version": "^9.3.2",
77
+ "ts-node": "^10.4.0",
78
+ "tsd": "^0.19.0",
79
+ "typescript": "^4.5.2",
82
80
  "typescript-cached-transpile": "^0.0.6",
83
81
  "xml2js": "^0.4.23",
84
- "yargs": "^17.1.1"
82
+ "yargs": "^17.2.1"
85
83
  },
86
84
  "license": "Apache-2.0",
87
85
  "engines": {
@@ -96,27 +94,31 @@
96
94
  },
97
95
  "scripts": {
98
96
  "build:evergreen": "node .evergreen/generate_evergreen_tasks.js",
99
- "build:ts": "rimraf lib && tsc",
97
+ "build:ts": "rimraf lib && ./node_modules/typescript/bin/tsc",
100
98
  "build:dts": "npm run build:ts && api-extractor run && rimraf 'lib/**/*.d.ts*' && downlevel-dts mongodb.d.ts mongodb.ts34.d.ts",
101
99
  "build:docs": "typedoc",
102
100
  "check:bench": "node test/benchmarks/driverBench",
103
- "check:coverage": "nyc npm run check:test",
101
+ "check:coverage": "nyc npm run test:all",
104
102
  "check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd",
105
103
  "check:eslint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
106
104
  "check:tsd": "tsd --version && tsd",
107
- "check:dts": "tsc --noEmit mongodb.d.ts && tsd",
108
- "check:test": "mocha --recursive test/functional test/unit",
109
- "check:ts": "tsc -v && tsc --noEmit",
105
+ "check:dts": "./node_modules/typescript/bin/tsc --noEmit mongodb.d.ts && tsd",
106
+ "check:test": "mocha --file test/tools/runner --recursive test/functional test/integration",
107
+ "check:unit": "mocha --recursive test/unit/",
108
+ "check:ts": "./node_modules/typescript/bin/tsc -v && ./node_modules/typescript/bin/tsc --noEmit",
110
109
  "check:atlas": "mocha --config \"test/manual/mocharc.json\" test/manual/atlas_connectivity.test.js",
111
- "check:adl": "mocha test/manual/data_lake.test.js",
110
+ "check:adl": "mocha --file test/tools/runner test/manual/data_lake.test.js",
111
+ "check:aws": "mocha --file test/tools/runner test/functional/mongodb_aws.test.js",
112
112
  "check:ocsp": "mocha --config \"test/manual/mocharc.json\" test/manual/ocsp_support.test.js",
113
113
  "check:kerberos": "mocha --config \"test/manual/mocharc.json\" test/manual/kerberos.test.js",
114
114
  "check:tls": "mocha --config \"test/manual/mocharc.json\" test/manual/tls_support.test.js",
115
115
  "check:ldap": "mocha --config \"test/manual/mocharc.json\" test/manual/ldap.test.js",
116
- "check:csfle": "mocha test/functional/client_side_encryption",
116
+ "check:csfle": "mocha --file test/tools/runner test/functional/client_side_encryption",
117
+ "check:snappy": "mocha --file test/tools/runner test/functional/unit_snappy.test.js",
117
118
  "prepare": "node etc/prepare.js",
118
119
  "release": "standard-version -i HISTORY.md",
119
- "test": "npm run check:lint && npm run check:test"
120
+ "test": "npm run check:lint && npm run test:all",
121
+ "test:all": "npm run check:unit && npm run check:test"
120
122
  },
121
123
  "tsd": {
122
124
  "directory": "test/types",
package/src/bson.ts CHANGED
@@ -51,6 +51,7 @@ export interface BSONSerializeOptions
51
51
  | 'cacheFunctionsCrc32'
52
52
  | 'allowObjectSmallerThanBufferSize'
53
53
  | 'index'
54
+ | 'validation'
54
55
  > {
55
56
  /** Return BSON filled buffers from operations */
56
57
  raw?: boolean;
@@ -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,
@@ -433,8 +440,13 @@ export class WriteError {
433
440
  }
434
441
  }
435
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
+
436
448
  /** Merges results into shared data structure */
437
- function mergeBatchResults(
449
+ export function mergeBatchResults(
438
450
  batch: Batch,
439
451
  bulkResult: BulkResult,
440
452
  err?: AnyError,
@@ -469,42 +481,37 @@ function mergeBatchResults(
469
481
  return;
470
482
  }
471
483
 
472
- // 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
473
492
  if (result.opTime || result.lastOp) {
474
- const opTime = result.lastOp || result.opTime;
475
- let lastOpTS = null;
476
- let lastOpT = null;
477
-
478
- // We have a time stamp
479
- if (opTime && opTime._bsontype === 'Timestamp') {
480
- if (bulkResult.opTime == null) {
481
- bulkResult.opTime = opTime;
482
- } else if (opTime.greaterThan(bulkResult.opTime)) {
483
- bulkResult.opTime = opTime;
484
- }
485
- } else {
486
- // Existing TS
487
- if (bulkResult.opTime) {
488
- lastOpTS =
489
- typeof bulkResult.opTime.ts === 'number'
490
- ? Long.fromNumber(bulkResult.opTime.ts)
491
- : bulkResult.opTime.ts;
492
- lastOpT =
493
- typeof bulkResult.opTime.t === 'number'
494
- ? Long.fromNumber(bulkResult.opTime.t)
495
- : bulkResult.opTime.t;
496
- }
493
+ let opTime = result.lastOp || result.opTime;
497
494
 
498
- // Current OpTime TS
499
- const opTimeTS = typeof opTime.ts === 'number' ? Long.fromNumber(opTime.ts) : opTime.ts;
500
- 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
+ }
501
501
 
502
- // Compare the opTime's
503
- if (bulkResult.opTime == null) {
504
- bulkResult.opTime = opTime;
505
- } 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)) {
506
510
  bulkResult.opTime = opTime;
507
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);
508
515
  if (opTimeT.greaterThan(lastOpT)) {
509
516
  bulkResult.opTime = opTime;
510
517
  }
@@ -1209,9 +1216,15 @@ export abstract class BulkOperationBase {
1209
1216
  return batches;
1210
1217
  }
1211
1218
 
1212
- /** 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;
1213
1226
  execute(
1214
- options?: BulkWriteOptions,
1227
+ options?: BulkWriteOptions | Callback<BulkWriteResult>,
1215
1228
  callback?: Callback<BulkWriteResult>
1216
1229
  ): Promise<BulkWriteResult> | void {
1217
1230
  if (typeof options === 'function') (callback = options), (options = {});
@@ -184,23 +184,23 @@ 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
  /**
198
198
  * Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}.
199
199
  * @public
200
200
  */
201
- export class ChangeStream<
202
- TSchema extends Document = Document
203
- > extends TypedEventEmitter<ChangeStreamEvents> {
201
+ export class ChangeStream<TSchema extends Document = Document> extends TypedEventEmitter<
202
+ ChangeStreamEvents<TSchema>
203
+ > {
204
204
  pipeline: Document[];
205
205
  options: ChangeStreamOptions;
206
206
  parent: MongoClient | Db | Collection;
@@ -310,7 +310,9 @@ export class ChangeStream<
310
310
  }
311
311
 
312
312
  /** Check if there is any document still available in the Change Stream */
313
- hasNext(callback?: Callback): Promise<void> | void {
313
+ hasNext(): Promise<boolean>;
314
+ hasNext(callback: Callback<boolean>): void;
315
+ hasNext(callback?: Callback): Promise<boolean> | void {
314
316
  setIsIterator(this);
315
317
  return maybePromise(callback, cb => {
316
318
  getCursor(this, (err, cursor) => {
@@ -469,6 +469,8 @@ export interface MessageHeader {
469
469
  export interface OpResponseOptions extends BSONSerializeOptions {
470
470
  raw?: boolean;
471
471
  documentsReturnedIn?: string | null;
472
+ // For now we use this internally to only prevent writeErrors from crashing the driver
473
+ validation?: { utf8: { writeErrors: boolean } };
472
474
  }
473
475
 
474
476
  /** @internal */
@@ -837,22 +839,24 @@ export class BinMsg {
837
839
  const promoteValues = options.promoteValues ?? this.opts.promoteValues;
838
840
  const promoteBuffers = options.promoteBuffers ?? this.opts.promoteBuffers;
839
841
  const bsonRegExp = options.bsonRegExp ?? this.opts.bsonRegExp;
842
+ const validation = options.validation ?? { utf8: { writeErrors: false } };
840
843
 
841
844
  // Set up the options
842
- const _options: BSONSerializeOptions = {
845
+ const bsonOptions: BSONSerializeOptions = {
843
846
  promoteLongs,
844
847
  promoteValues,
845
848
  promoteBuffers,
846
- bsonRegExp
847
- };
849
+ bsonRegExp,
850
+ validation
851
+ // Due to the strictness of the BSON libraries validation option we need this cast
852
+ } as BSONSerializeOptions & { validation: { utf8: { writeErrors: boolean } } };
848
853
 
849
854
  while (this.index < this.data.length) {
850
855
  const payloadType = this.data.readUInt8(this.index++);
851
856
  if (payloadType === 0) {
852
857
  const bsonSize = this.data.readUInt32LE(this.index);
853
858
  const bin = this.data.slice(this.index, this.index + bsonSize);
854
- this.documents.push(raw ? bin : BSON.deserialize(bin, _options));
855
-
859
+ this.documents.push(raw ? bin : BSON.deserialize(bin, bsonOptions));
856
860
  this.index += bsonSize;
857
861
  } else if (payloadType === 1) {
858
862
  // It was decided that no driver makes use of payload type 1
@@ -865,9 +869,8 @@ export class BinMsg {
865
869
  if (this.documents.length === 1 && documentsReturnedIn != null && raw) {
866
870
  const fieldsAsRaw: Document = {};
867
871
  fieldsAsRaw[documentsReturnedIn] = true;
868
- _options.fieldsAsRaw = fieldsAsRaw;
869
-
870
- const doc = BSON.deserialize(this.documents[0] as Buffer, _options);
872
+ bsonOptions.fieldsAsRaw = fieldsAsRaw;
873
+ const doc = BSON.deserialize(this.documents[0] as Buffer, bsonOptions);
871
874
  this.documents = [doc];
872
875
  }
873
876
 
@@ -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;