mongodb 6.8.0-dev.20240731.sha.b26c3280 → 6.8.0-dev.20240808.sha.5565d500

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 (179) hide show
  1. package/lib/beta.d.ts +7875 -0
  2. package/lib/beta.js +22 -0
  3. package/lib/beta.js.map +1 -0
  4. package/lib/bson.js +4 -4
  5. package/lib/bson.js.map +1 -1
  6. package/lib/bulk/common.js +2 -2
  7. package/lib/bulk/common.js.map +1 -1
  8. package/lib/bulk/ordered.js.map +1 -1
  9. package/lib/bulk/unordered.js.map +1 -1
  10. package/lib/change_stream.js +10 -2
  11. package/lib/change_stream.js.map +1 -1
  12. package/lib/client-side-encryption/auto_encrypter.js +14 -3
  13. package/lib/client-side-encryption/auto_encrypter.js.map +1 -1
  14. package/lib/client-side-encryption/client_encryption.js +24 -4
  15. package/lib/client-side-encryption/client_encryption.js.map +1 -1
  16. package/lib/client-side-encryption/crypto_callbacks.js +6 -6
  17. package/lib/client-side-encryption/crypto_callbacks.js.map +1 -1
  18. package/lib/client-side-encryption/mongocryptd_manager.js +1 -1
  19. package/lib/client-side-encryption/mongocryptd_manager.js.map +1 -1
  20. package/lib/client-side-encryption/providers/aws.js +1 -2
  21. package/lib/client-side-encryption/providers/aws.js.map +1 -1
  22. package/lib/client-side-encryption/providers/azure.js +5 -5
  23. package/lib/client-side-encryption/providers/azure.js.map +1 -1
  24. package/lib/client-side-encryption/providers/gcp.js +1 -2
  25. package/lib/client-side-encryption/providers/gcp.js.map +1 -1
  26. package/lib/client-side-encryption/providers/index.js +2 -3
  27. package/lib/client-side-encryption/providers/index.js.map +1 -1
  28. package/lib/client-side-encryption/state_machine.js +9 -4
  29. package/lib/client-side-encryption/state_machine.js.map +1 -1
  30. package/lib/cmap/auth/auth_provider.js.map +1 -1
  31. package/lib/cmap/auth/aws_temporary_credentials.js.map +1 -1
  32. package/lib/cmap/auth/gssapi.js +3 -3
  33. package/lib/cmap/auth/gssapi.js.map +1 -1
  34. package/lib/cmap/auth/mongo_credentials.js.map +1 -1
  35. package/lib/cmap/auth/mongocr.js.map +1 -1
  36. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  37. package/lib/cmap/auth/mongodb_oidc/automated_callback_workflow.js.map +1 -1
  38. package/lib/cmap/auth/mongodb_oidc/azure_machine_workflow.js.map +1 -1
  39. package/lib/cmap/auth/mongodb_oidc/callback_workflow.js.map +1 -1
  40. package/lib/cmap/auth/mongodb_oidc/command_builders.js +2 -3
  41. package/lib/cmap/auth/mongodb_oidc/command_builders.js.map +1 -1
  42. package/lib/cmap/auth/mongodb_oidc/gcp_machine_workflow.js.map +1 -1
  43. package/lib/cmap/auth/mongodb_oidc/human_callback_workflow.js.map +1 -1
  44. package/lib/cmap/auth/mongodb_oidc/machine_workflow.js.map +1 -1
  45. package/lib/cmap/auth/mongodb_oidc/token_cache.js.map +1 -1
  46. package/lib/cmap/auth/mongodb_oidc/token_machine_workflow.js.map +1 -1
  47. package/lib/cmap/auth/mongodb_oidc.js.map +1 -1
  48. package/lib/cmap/auth/plain.js.map +1 -1
  49. package/lib/cmap/auth/scram.js.map +1 -1
  50. package/lib/cmap/auth/x509.js.map +1 -1
  51. package/lib/cmap/command_monitoring_events.js.map +1 -1
  52. package/lib/cmap/commands.js.map +1 -1
  53. package/lib/cmap/connect.js +8 -6
  54. package/lib/cmap/connect.js.map +1 -1
  55. package/lib/cmap/connection.js +3 -3
  56. package/lib/cmap/connection.js.map +1 -1
  57. package/lib/cmap/connection_pool.js +1 -1
  58. package/lib/cmap/connection_pool.js.map +1 -1
  59. package/lib/cmap/handshake/client_metadata.js +4 -4
  60. package/lib/cmap/handshake/client_metadata.js.map +1 -1
  61. package/lib/cmap/metrics.js +1 -1
  62. package/lib/cmap/metrics.js.map +1 -1
  63. package/lib/cmap/stream_description.js.map +1 -1
  64. package/lib/cmap/wire_protocol/compression.js +5 -5
  65. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  66. package/lib/cmap/wire_protocol/on_data.js +1 -2
  67. package/lib/cmap/wire_protocol/on_data.js.map +1 -1
  68. package/lib/cmap/wire_protocol/on_demand/document.js.map +1 -1
  69. package/lib/cmap/wire_protocol/responses.js +4 -4
  70. package/lib/cmap/wire_protocol/responses.js.map +1 -1
  71. package/lib/cmap/wire_protocol/shared.js +2 -3
  72. package/lib/cmap/wire_protocol/shared.js.map +1 -1
  73. package/lib/collection.js.map +1 -1
  74. package/lib/connection_string.js +10 -3
  75. package/lib/connection_string.js.map +1 -1
  76. package/lib/cursor/abstract_cursor.js +21 -1
  77. package/lib/cursor/abstract_cursor.js.map +1 -1
  78. package/lib/cursor/aggregation_cursor.js +1 -1
  79. package/lib/cursor/aggregation_cursor.js.map +1 -1
  80. package/lib/cursor/change_stream_cursor.js.map +1 -1
  81. package/lib/cursor/find_cursor.js +3 -3
  82. package/lib/cursor/find_cursor.js.map +1 -1
  83. package/lib/db.js +1 -1
  84. package/lib/db.js.map +1 -1
  85. package/lib/deps.js +8 -8
  86. package/lib/deps.js.map +1 -1
  87. package/lib/encrypter.js.map +1 -1
  88. package/lib/error.js +9 -9
  89. package/lib/error.js.map +1 -1
  90. package/lib/explain.js.map +1 -1
  91. package/lib/gridfs/download.js +1 -1
  92. package/lib/gridfs/download.js.map +1 -1
  93. package/lib/gridfs/index.js +1 -1
  94. package/lib/gridfs/index.js.map +1 -1
  95. package/lib/gridfs/upload.js.map +1 -1
  96. package/lib/index.js +4 -2
  97. package/lib/index.js.map +1 -1
  98. package/lib/mongo_client.js +15 -1
  99. package/lib/mongo_client.js.map +1 -1
  100. package/lib/mongo_client_auth_providers.js.map +1 -1
  101. package/lib/mongo_logger.js +5 -5
  102. package/lib/mongo_logger.js.map +1 -1
  103. package/lib/mongo_types.js.map +1 -1
  104. package/lib/operations/aggregate.js.map +1 -1
  105. package/lib/operations/bulk_write.js.map +1 -1
  106. package/lib/operations/collections.js.map +1 -1
  107. package/lib/operations/command.js.map +1 -1
  108. package/lib/operations/count.js.map +1 -1
  109. package/lib/operations/create_collection.js.map +1 -1
  110. package/lib/operations/delete.js +2 -2
  111. package/lib/operations/delete.js.map +1 -1
  112. package/lib/operations/distinct.js.map +1 -1
  113. package/lib/operations/drop.js.map +1 -1
  114. package/lib/operations/estimated_document_count.js.map +1 -1
  115. package/lib/operations/execute_operation.js +1 -2
  116. package/lib/operations/execute_operation.js.map +1 -1
  117. package/lib/operations/find.js.map +1 -1
  118. package/lib/operations/find_and_modify.js.map +1 -1
  119. package/lib/operations/get_more.js.map +1 -1
  120. package/lib/operations/indexes.js.map +1 -1
  121. package/lib/operations/insert.js.map +1 -1
  122. package/lib/operations/is_capped.js.map +1 -1
  123. package/lib/operations/kill_cursors.js.map +1 -1
  124. package/lib/operations/list_collections.js.map +1 -1
  125. package/lib/operations/list_databases.js.map +1 -1
  126. package/lib/operations/operation.js +2 -2
  127. package/lib/operations/operation.js.map +1 -1
  128. package/lib/operations/options_operation.js.map +1 -1
  129. package/lib/operations/profiling_level.js.map +1 -1
  130. package/lib/operations/search_indexes/drop.js.map +1 -1
  131. package/lib/operations/set_profiling_level.js.map +1 -1
  132. package/lib/operations/stats.js.map +1 -1
  133. package/lib/operations/update.js +2 -2
  134. package/lib/operations/update.js.map +1 -1
  135. package/lib/operations/validate_collection.js.map +1 -1
  136. package/lib/read_concern.js.map +1 -1
  137. package/lib/read_preference.js +1 -1
  138. package/lib/read_preference.js.map +1 -1
  139. package/lib/resource_management.js +58 -0
  140. package/lib/resource_management.js.map +1 -0
  141. package/lib/sdam/common.js +3 -3
  142. package/lib/sdam/common.js.map +1 -1
  143. package/lib/sdam/monitor.js.map +1 -1
  144. package/lib/sdam/server.js +1 -1
  145. package/lib/sdam/server.js.map +1 -1
  146. package/lib/sdam/server_description.js +3 -3
  147. package/lib/sdam/server_description.js.map +1 -1
  148. package/lib/sdam/server_selection.js +5 -5
  149. package/lib/sdam/server_selection.js.map +1 -1
  150. package/lib/sdam/srv_polling.js +1 -1
  151. package/lib/sdam/srv_polling.js.map +1 -1
  152. package/lib/sdam/topology.js +1 -1
  153. package/lib/sdam/topology.js.map +1 -1
  154. package/lib/sdam/topology_description.js.map +1 -1
  155. package/lib/sessions.js +14 -5
  156. package/lib/sessions.js.map +1 -1
  157. package/lib/sort.js +1 -2
  158. package/lib/sort.js.map +1 -1
  159. package/lib/timeout.js.map +1 -1
  160. package/lib/transactions.js +2 -2
  161. package/lib/transactions.js.map +1 -1
  162. package/lib/utils.js +49 -50
  163. package/lib/utils.js.map +1 -1
  164. package/lib/write_concern.js +2 -2
  165. package/lib/write_concern.js.map +1 -1
  166. package/mongodb.d.ts +55 -12
  167. package/package.json +5 -4
  168. package/src/beta.ts +22 -0
  169. package/src/change_stream.ts +23 -4
  170. package/src/client-side-encryption/auto_encrypter.ts +17 -3
  171. package/src/client-side-encryption/client_encryption.ts +32 -6
  172. package/src/client-side-encryption/state_machine.ts +28 -6
  173. package/src/cmap/connect.ts +2 -0
  174. package/src/connection_string.ts +7 -0
  175. package/src/cursor/abstract_cursor.ts +38 -8
  176. package/src/index.ts +3 -0
  177. package/src/mongo_client.ts +25 -3
  178. package/src/resource_management.ts +74 -0
  179. package/src/sessions.ts +21 -2
package/mongodb.d.ts CHANGED
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
-
3
1
  import type { DeserializeOptions , ObjectIdLike , SerializeOptions } from 'bson';
4
2
  import { Binary , BSON , BSONRegExp , BSONSymbol , BSONType , Code , DBRef , Decimal128 , deserialize , Document , Double , Int32 , Long , MaxKey , MinKey , ObjectId , serialize , Timestamp , UUID } from 'bson';
5
3
  import type { SrvRecord } from 'dns';
@@ -9,7 +7,7 @@ import { Readable , Writable } from 'stream';
9
7
  import type { ConnectionOptions as ConnectionOptions_2 , TLSSocket , TLSSocketOptions } from 'tls';
10
8
 
11
9
  /** @public */
12
- export declare abstract class AbstractCursor<TSchema = any, CursorEvents extends AbstractCursorEvents = AbstractCursorEvents> extends TypedEventEmitter<CursorEvents> {
10
+ export declare abstract class AbstractCursor<TSchema = any, CursorEvents extends AbstractCursorEvents = AbstractCursorEvents> extends TypedEventEmitter<CursorEvents> implements AsyncDisposable_2 {
13
11
  /* Excluded from this release type: cursorId */
14
12
  /* Excluded from this release type: cursorSession */
15
13
  /* Excluded from this release type: selectedServer */
@@ -50,10 +48,12 @@ export declare abstract class AbstractCursor<TSchema = any, CursorEvents extends
50
48
  */
51
49
  get killed(): boolean;
52
50
  get loadBalanced(): boolean;
51
+ /* Excluded from this release type: [Symbol.asyncDispose] */
52
+ /* Excluded from this release type: asyncDispose */
53
53
  /** Returns current buffered documents length */
54
54
  bufferedCount(): number;
55
55
  /** Returns current buffered documents */
56
- readBufferedDocuments(number?: number): TSchema[];
56
+ readBufferedDocuments(number?: number): NonNullable<TSchema>[];
57
57
  [Symbol.asyncIterator](): AsyncGenerator<TSchema, void, void>;
58
58
  stream(options?: CursorStreamOptions): Readable & AsyncIterable<TSchema>;
59
59
  hasNext(): Promise<boolean>;
@@ -72,6 +72,9 @@ export declare abstract class AbstractCursor<TSchema = any, CursorEvents extends
72
72
  * @deprecated - Will be removed in a future release. Use for await...of instead.
73
73
  */
74
74
  forEach(iterator: (doc: TSchema) => boolean | void): Promise<void>;
75
+ /**
76
+ * Frees any client-side resources used by the cursor.
77
+ */
75
78
  close(): Promise<void>;
76
79
  /**
77
80
  * Returns an array of documents. The caller is responsible for making sure that there
@@ -500,6 +503,15 @@ export declare type ArrayOperator<Type> = {
500
503
  $sort?: Sort;
501
504
  };
502
505
 
506
+ /**
507
+ * @public
508
+ */
509
+ declare interface AsyncDisposable_2 {
510
+ /* Excluded from this release type: [Symbol.asyncDispose] */
511
+ /* Excluded from this release type: asyncDispose */
512
+ }
513
+ export { AsyncDisposable_2 as AsyncDisposable }
514
+
503
515
  /** @public */
504
516
  export declare interface Auth {
505
517
  /** The username for auth */
@@ -988,7 +1000,9 @@ export declare class CancellationToken extends TypedEventEmitter<{
988
1000
  * Creates a new Change Stream instance. Normally created using {@link Collection#watch|Collection.watch()}.
989
1001
  * @public
990
1002
  */
991
- export declare class ChangeStream<TSchema extends Document = Document, TChange extends Document = ChangeStreamDocument<TSchema>> extends TypedEventEmitter<ChangeStreamEvents<TSchema, TChange>> {
1003
+ export declare class ChangeStream<TSchema extends Document = Document, TChange extends Document = ChangeStreamDocument<TSchema>> extends TypedEventEmitter<ChangeStreamEvents<TSchema, TChange>> implements AsyncDisposable_2 {
1004
+ /* Excluded from this release type: [Symbol.asyncDispose] */
1005
+ /* Excluded from this release type: asyncDispose */
992
1006
  pipeline: Document[];
993
1007
  /**
994
1008
  * @remarks WriteConcern can still be present on the options because
@@ -1047,7 +1061,9 @@ export declare class ChangeStream<TSchema extends Document = Document, TChange e
1047
1061
  [Symbol.asyncIterator](): AsyncGenerator<TChange, void, void>;
1048
1062
  /** Is the cursor closed */
1049
1063
  get closed(): boolean;
1050
- /** Close the Change Stream */
1064
+ /**
1065
+ * Frees the internal resources used by the change stream.
1066
+ */
1051
1067
  close(): Promise<void>;
1052
1068
  /**
1053
1069
  * Return a modified Readable stream including a possible transform method.
@@ -1843,6 +1859,13 @@ export declare interface ClientEncryptionRewrapManyDataKeyResult {
1843
1859
  bulkWriteResult?: BulkWriteResult;
1844
1860
  }
1845
1861
 
1862
+ /**
1863
+ * @public
1864
+ *
1865
+ * Socket options to use for KMS requests.
1866
+ */
1867
+ export declare type ClientEncryptionSocketOptions = Pick<MongoClientOptions, 'autoSelectFamily' | 'autoSelectFamilyAttemptTimeout'>;
1868
+
1846
1869
  /**
1847
1870
  * @public
1848
1871
  *
@@ -1902,7 +1925,7 @@ export declare interface ClientMetadataOptions {
1902
1925
  * NOTE: not meant to be instantiated directly.
1903
1926
  * @public
1904
1927
  */
1905
- export declare class ClientSession extends TypedEventEmitter<ClientSessionEvents> {
1928
+ export declare class ClientSession extends TypedEventEmitter<ClientSessionEvents> implements AsyncDisposable_2 {
1906
1929
  /* Excluded from this release type: client */
1907
1930
  /* Excluded from this release type: sessionPool */
1908
1931
  hasEnded: boolean;
@@ -1934,11 +1957,16 @@ export declare class ClientSession extends TypedEventEmitter<ClientSessionEvents
1934
1957
  /* Excluded from this release type: unpin */
1935
1958
  get isPinned(): boolean;
1936
1959
  /**
1937
- * Ends this session on the server
1960
+ * Frees any client-side resources held by the current session. If a session is in a transaction,
1961
+ * the transaction is aborted.
1962
+ *
1963
+ * Does not end the session on the server.
1938
1964
  *
1939
1965
  * @param options - Optional settings. Currently reserved for future use
1940
1966
  */
1941
1967
  endSession(options?: EndSessionOptions): Promise<void>;
1968
+ /* Excluded from this release type: [Symbol.asyncDispose] */
1969
+ /* Excluded from this release type: asyncDispose */
1942
1970
  /**
1943
1971
  * Advances the operationTime for a ClientSession.
1944
1972
  *
@@ -2780,6 +2808,8 @@ export declare type CompressorName = keyof typeof Compressor;
2780
2808
  /** @public */
2781
2809
  export declare type Condition<T> = AlternativeType<T> | FilterOperators<AlternativeType<T>>;
2782
2810
 
2811
+ /* Excluded from this release type: configureExplicitResourceManagement */
2812
+
2783
2813
  /* Excluded from this release type: Connection */
2784
2814
 
2785
2815
  /* Excluded from this release type: CONNECTION_CHECK_OUT_FAILED */
@@ -4591,7 +4621,7 @@ export declare interface KMSProviders {
4591
4621
  /* Excluded from this release type: kWaitQueue_2 */
4592
4622
 
4593
4623
  /** @public */
4594
- export declare const LEGAL_TCP_SOCKET_OPTIONS: readonly ["family", "hints", "localAddress", "localPort", "lookup"];
4624
+ export declare const LEGAL_TCP_SOCKET_OPTIONS: readonly ["autoSelectFamily", "autoSelectFamilyAttemptTimeout", "family", "hints", "localAddress", "localPort", "lookup"];
4595
4625
 
4596
4626
  /** @public */
4597
4627
  export declare const LEGAL_TLS_SOCKET_OPTIONS: readonly ["ALPNProtocols", "ca", "cert", "checkServerIdentity", "ciphers", "crl", "ecdhCurve", "key", "minDHSize", "passphrase", "pfx", "rejectUnauthorized", "secureContext", "secureProtocol", "servername", "session"];
@@ -4895,13 +4925,15 @@ export declare class MongoChangeStreamError extends MongoRuntimeError {
4895
4925
  * await client.insertOne({ name: 'spot', kind: 'dog' });
4896
4926
  * ```
4897
4927
  */
4898
- export declare class MongoClient extends TypedEventEmitter<MongoClientEvents> {
4928
+ export declare class MongoClient extends TypedEventEmitter<MongoClientEvents> implements AsyncDisposable_2 {
4899
4929
  /* Excluded from this release type: s */
4900
4930
  /* Excluded from this release type: topology */
4901
4931
  /* Excluded from this release type: mongoLogger */
4902
4932
  /* Excluded from this release type: connectionLock */
4903
4933
  /* Excluded from this release type: [kOptions] */
4904
4934
  constructor(url: string, options?: MongoClientOptions);
4935
+ /* Excluded from this release type: [Symbol.asyncDispose] */
4936
+ /* Excluded from this release type: asyncDispose */
4905
4937
  /* Excluded from this release type: checkForNonGenuineHosts */
4906
4938
  /** @see MongoOptions */
4907
4939
  get options(): Readonly<MongoOptions>;
@@ -4921,7 +4953,15 @@ export declare class MongoClient extends TypedEventEmitter<MongoClientEvents> {
4921
4953
  connect(): Promise<this>;
4922
4954
  /* Excluded from this release type: _connect */
4923
4955
  /**
4924
- * Close the client and its underlying connections
4956
+ * Cleans up client-side resources used by the MongoCLient and . This includes:
4957
+ *
4958
+ * - Closes all open, unused connections (see note).
4959
+ * - Ends all in-use sessions with {@link ClientSession#endSession|ClientSession.endSession()}.
4960
+ * - Ends all unused sessions server-side.
4961
+ * - Cleans up any resources being used for auto encryption if auto encryption is enabled.
4962
+ *
4963
+ * @remarks Any in-progress operations are not killed and any connections used by in progress operations
4964
+ * will be cleaned up lazily as operations finish.
4925
4965
  *
4926
4966
  * @param force - Force close, emitting no events
4927
4967
  */
@@ -7172,7 +7212,10 @@ export declare type StrictUpdateFilter<TSchema> = {
7172
7212
  export declare type SupportedNodeConnectionOptions = SupportedTLSConnectionOptions & SupportedTLSSocketOptions & SupportedSocketOptions;
7173
7213
 
7174
7214
  /** @public */
7175
- export declare type SupportedSocketOptions = Pick<TcpNetConnectOpts, (typeof LEGAL_TCP_SOCKET_OPTIONS)[number]>;
7215
+ export declare type SupportedSocketOptions = Pick<TcpNetConnectOpts & {
7216
+ autoSelectFamily?: boolean;
7217
+ autoSelectFamilyAttemptTimeout?: number;
7218
+ }, (typeof LEGAL_TCP_SOCKET_OPTIONS)[number]>;
7176
7219
 
7177
7220
  /** @public */
7178
7221
  export declare type SupportedTLSConnectionOptions = Pick<ConnectionOptions_2, Extract<keyof ConnectionOptions_2, (typeof LEGAL_TLS_SOCKET_OPTIONS)[number]>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongodb",
3
- "version": "6.8.0-dev.20240731.sha.b26c3280",
3
+ "version": "6.8.0-dev.20240808.sha.5565d500",
4
4
  "description": "The official MongoDB driver for Node.js",
5
5
  "main": "lib/index.js",
6
6
  "files": [
@@ -73,7 +73,7 @@
73
73
  "@types/express": "^4.17.21",
74
74
  "@types/kerberos": "^1.1.5",
75
75
  "@types/mocha": "^10.0.6",
76
- "@types/node": "^20.12.7",
76
+ "@types/node": "^20.14.10",
77
77
  "@types/saslprep": "^1.0.3",
78
78
  "@types/semver": "^7.5.8",
79
79
  "@types/sinon": "^17.0.3",
@@ -110,7 +110,7 @@
110
110
  "source-map-support": "^0.5.21",
111
111
  "ts-node": "^10.9.2",
112
112
  "tsd": "^0.31.0",
113
- "typescript": "5.0",
113
+ "typescript": "5.5",
114
114
  "typescript-cached-transpile": "^0.0.6",
115
115
  "v8-heapsnapshot": "^1.3.1",
116
116
  "yargs": "^17.7.2"
@@ -126,7 +126,7 @@
126
126
  "scripts": {
127
127
  "build:evergreen": "node .evergreen/generate_evergreen_tasks.js",
128
128
  "build:ts": "node ./node_modules/typescript/bin/tsc",
129
- "build:dts": "npm run build:ts && api-extractor run && node etc/clean_definition_files.cjs && eslint mongodb.d.ts --fix",
129
+ "build:dts": "npm run build:ts && api-extractor run && node etc/clean_definition_files.cjs && eslint --no-ignore --fix mongodb.d.ts lib/beta.d.ts",
130
130
  "build:docs": "./etc/docs/build.ts",
131
131
  "build:typedoc": "typedoc",
132
132
  "build:nightly": "node ./.github/scripts/nightly.mjs",
@@ -145,6 +145,7 @@
145
145
  "check:unit": "mocha test/unit",
146
146
  "check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
147
147
  "check:atlas": "mocha --config test/manual/mocharc.json test/manual/atlas_connectivity.test.ts",
148
+ "check:resource-management": "mocha --config test/manual/mocharc.json test/manual/resource_management.test.ts",
148
149
  "check:drivers-atlas-testing": "mocha --config test/mocha_mongodb.json test/atlas/drivers_atlas_testing.test.ts",
149
150
  "check:adl": "mocha --config test/mocha_mongodb.json test/manual/atlas-data-lake-testing",
150
151
  "check:aws": "nyc mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.ts",
package/src/beta.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { type Document } from 'bson';
2
+
3
+ export * from './index';
4
+
5
+ /**
6
+ * @internal
7
+ *
8
+ * Since we don't bundle tslib helpers, we need to polyfill this method.
9
+ *
10
+ * This is used in the generated JS. Adapted from https://github.com/microsoft/TypeScript/blob/aafdfe5b3f76f5c41abeec412ce73c86da94c75f/src/compiler/factory/emitHelpers.ts#L1202.
11
+ */
12
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
13
+ function __exportStar(mod: Document) {
14
+ for (const key of Object.keys(mod)) {
15
+ Object.defineProperty(exports, key, {
16
+ enumerable: true,
17
+ get: function () {
18
+ return mod[key];
19
+ }
20
+ });
21
+ }
22
+ }
@@ -18,6 +18,7 @@ import { type InferIdType, TypedEventEmitter } from './mongo_types';
18
18
  import type { AggregateOptions } from './operations/aggregate';
19
19
  import type { CollationOptions, OperationParent } from './operations/command';
20
20
  import type { ReadPreference } from './read_preference';
21
+ import { type AsyncDisposable, configureResourceManagement } from './resource_management';
21
22
  import type { ServerSessionId } from './sessions';
22
23
  import { filterOptions, getTopology, type MongoDBNamespace, squashError } from './utils';
23
24
 
@@ -544,9 +545,23 @@ export type ChangeStreamEvents<
544
545
  * @public
545
546
  */
546
547
  export class ChangeStream<
547
- TSchema extends Document = Document,
548
- TChange extends Document = ChangeStreamDocument<TSchema>
549
- > extends TypedEventEmitter<ChangeStreamEvents<TSchema, TChange>> {
548
+ TSchema extends Document = Document,
549
+ TChange extends Document = ChangeStreamDocument<TSchema>
550
+ >
551
+ extends TypedEventEmitter<ChangeStreamEvents<TSchema, TChange>>
552
+ implements AsyncDisposable
553
+ {
554
+ /**
555
+ * @beta
556
+ * @experimental
557
+ * An alias for {@link ChangeStream.close|ChangeStream.close()}.
558
+ */
559
+ declare [Symbol.asyncDispose]: () => Promise<void>;
560
+ /** @internal */
561
+ async asyncDispose() {
562
+ await this.close();
563
+ }
564
+
550
565
  pipeline: Document[];
551
566
  /**
552
567
  * @remarks WriteConcern can still be present on the options because
@@ -765,7 +780,9 @@ export class ChangeStream<
765
780
  return this[kClosed] || this.cursor.closed;
766
781
  }
767
782
 
768
- /** Close the Change Stream */
783
+ /**
784
+ * Frees the internal resources used by the change stream.
785
+ */
769
786
  async close(): Promise<void> {
770
787
  this[kClosed] = true;
771
788
 
@@ -986,3 +1003,5 @@ export class ChangeStream<
986
1003
  }
987
1004
  }
988
1005
  }
1006
+
1007
+ configureResourceManagement(ChangeStream.prototype);
@@ -3,6 +3,7 @@ import {
3
3
  type MongoCryptConstructor,
4
4
  type MongoCryptOptions
5
5
  } from 'mongodb-client-encryption';
6
+ import * as net from 'net';
6
7
 
7
8
  import { deserialize, type Document, serialize } from '../bson';
8
9
  import { type CommandOptions, type ProxyOptions } from '../cmap/connection';
@@ -11,6 +12,7 @@ import { getMongoDBClientEncryption } from '../deps';
11
12
  import { MongoRuntimeError } from '../error';
12
13
  import { MongoClient, type MongoClientOptions } from '../mongo_client';
13
14
  import { MongoDBCollectionNamespace } from '../utils';
15
+ import { autoSelectSocketOptions } from './client_encryption';
14
16
  import * as cryptoCallbacks from './crypto_callbacks';
15
17
  import { MongoCryptInvalidArgumentError } from './errors';
16
18
  import { MongocryptdManager } from './mongocryptd_manager';
@@ -297,10 +299,20 @@ export class AutoEncrypter {
297
299
  serverSelectionTimeoutMS: 10000
298
300
  };
299
301
 
300
- if (options.extraOptions == null || typeof options.extraOptions.mongocryptdURI !== 'string') {
302
+ if (
303
+ (options.extraOptions == null || typeof options.extraOptions.mongocryptdURI !== 'string') &&
304
+ !net.getDefaultAutoSelectFamily
305
+ ) {
306
+ // Only set family if autoSelectFamily options are not supported.
301
307
  clientOptions.family = 4;
302
308
  }
303
309
 
310
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
311
+ // @ts-ignore: TS complains as this always returns true on versions where it is present.
312
+ if (net.getDefaultAutoSelectFamily) {
313
+ Object.assign(clientOptions, autoSelectSocketOptions(this._client.options));
314
+ }
315
+
304
316
  this._mongocryptdClient = new MongoClient(this._mongocryptdManager.uri, clientOptions);
305
317
  }
306
318
  }
@@ -379,7 +391,8 @@ export class AutoEncrypter {
379
391
  promoteValues: false,
380
392
  promoteLongs: false,
381
393
  proxyOptions: this._proxyOptions,
382
- tlsOptions: this._tlsOptions
394
+ tlsOptions: this._tlsOptions,
395
+ socketOptions: autoSelectSocketOptions(this._client.options)
383
396
  });
384
397
 
385
398
  return deserialize(await stateMachine.execute(this, context), {
@@ -399,7 +412,8 @@ export class AutoEncrypter {
399
412
  const stateMachine = new StateMachine({
400
413
  ...options,
401
414
  proxyOptions: this._proxyOptions,
402
- tlsOptions: this._tlsOptions
415
+ tlsOptions: this._tlsOptions,
416
+ socketOptions: autoSelectSocketOptions(this._client.options)
403
417
  });
404
418
 
405
419
  return await stateMachine.execute(this, context);
@@ -12,7 +12,7 @@ import { type Collection } from '../collection';
12
12
  import { type FindCursor } from '../cursor/find_cursor';
13
13
  import { type Db } from '../db';
14
14
  import { getMongoDBClientEncryption } from '../deps';
15
- import { type MongoClient } from '../mongo_client';
15
+ import { type MongoClient, type MongoClientOptions } from '../mongo_client';
16
16
  import { type Filter, type WithId } from '../mongo_types';
17
17
  import { type CreateCollectionOptions } from '../operations/create_collection';
18
18
  import { type DeleteResult } from '../operations/delete';
@@ -28,7 +28,11 @@ import {
28
28
  type KMSProviders,
29
29
  refreshKMSCredentials
30
30
  } from './providers/index';
31
- import { type CSFLEKMSTlsOptions, StateMachine } from './state_machine';
31
+ import {
32
+ type ClientEncryptionSocketOptions,
33
+ type CSFLEKMSTlsOptions,
34
+ StateMachine
35
+ } from './state_machine';
32
36
 
33
37
  /**
34
38
  * @public
@@ -199,7 +203,8 @@ export class ClientEncryption {
199
203
 
200
204
  const stateMachine = new StateMachine({
201
205
  proxyOptions: this._proxyOptions,
202
- tlsOptions: this._tlsOptions
206
+ tlsOptions: this._tlsOptions,
207
+ socketOptions: autoSelectSocketOptions(this._client.options)
203
208
  });
204
209
 
205
210
  const dataKey = deserialize(await stateMachine.execute(this, context)) as DataKey;
@@ -256,7 +261,8 @@ export class ClientEncryption {
256
261
  const context = this._mongoCrypt.makeRewrapManyDataKeyContext(filterBson, keyEncryptionKeyBson);
257
262
  const stateMachine = new StateMachine({
258
263
  proxyOptions: this._proxyOptions,
259
- tlsOptions: this._tlsOptions
264
+ tlsOptions: this._tlsOptions,
265
+ socketOptions: autoSelectSocketOptions(this._client.options)
260
266
  });
261
267
 
262
268
  const { v: dataKeys } = deserialize(await stateMachine.execute(this, context));
@@ -637,7 +643,8 @@ export class ClientEncryption {
637
643
 
638
644
  const stateMachine = new StateMachine({
639
645
  proxyOptions: this._proxyOptions,
640
- tlsOptions: this._tlsOptions
646
+ tlsOptions: this._tlsOptions,
647
+ socketOptions: autoSelectSocketOptions(this._client.options)
641
648
  });
642
649
 
643
650
  const { v } = deserialize(await stateMachine.execute(this, context));
@@ -715,7 +722,8 @@ export class ClientEncryption {
715
722
  const valueBuffer = serialize({ v: value });
716
723
  const stateMachine = new StateMachine({
717
724
  proxyOptions: this._proxyOptions,
718
- tlsOptions: this._tlsOptions
725
+ tlsOptions: this._tlsOptions,
726
+ socketOptions: autoSelectSocketOptions(this._client.options)
719
727
  });
720
728
  const context = this._mongoCrypt.makeExplicitEncryptionContext(valueBuffer, contextOptions);
721
729
 
@@ -957,3 +965,21 @@ export interface RangeOptions {
957
965
  sparsity: Long;
958
966
  precision?: number;
959
967
  }
968
+
969
+ /**
970
+ * Get the socket options from the client.
971
+ * @param baseOptions - The mongo client options.
972
+ * @returns ClientEncryptionSocketOptions
973
+ */
974
+ export function autoSelectSocketOptions(
975
+ baseOptions: MongoClientOptions
976
+ ): ClientEncryptionSocketOptions {
977
+ const options: ClientEncryptionSocketOptions = { autoSelectFamily: true };
978
+ if ('autoSelectFamily' in baseOptions) {
979
+ options.autoSelectFamily = baseOptions.autoSelectFamily;
980
+ }
981
+ if ('autoSelectFamilyAttemptTimeout' in baseOptions) {
982
+ options.autoSelectFamilyAttemptTimeout = baseOptions.autoSelectFamilyAttemptTimeout;
983
+ }
984
+ return options;
985
+ }
@@ -14,7 +14,7 @@ import { type ProxyOptions } from '../cmap/connection';
14
14
  import { getSocks, type SocksLib } from '../deps';
15
15
  import { type MongoClient, type MongoClientOptions } from '../mongo_client';
16
16
  import { BufferPool, MongoDBCollectionNamespace, promiseWithResolvers } from '../utils';
17
- import { type DataKey } from './client_encryption';
17
+ import { autoSelectSocketOptions, type DataKey } from './client_encryption';
18
18
  import { MongoCryptError } from './errors';
19
19
  import { type MongocryptdManager } from './mongocryptd_manager';
20
20
  import { type KMSProviders } from './providers';
@@ -114,6 +114,16 @@ export type CSFLEKMSTlsOptions = {
114
114
  [key: string]: ClientEncryptionTlsOptions | undefined;
115
115
  };
116
116
 
117
+ /**
118
+ * @public
119
+ *
120
+ * Socket options to use for KMS requests.
121
+ */
122
+ export type ClientEncryptionSocketOptions = Pick<
123
+ MongoClientOptions,
124
+ 'autoSelectFamily' | 'autoSelectFamilyAttemptTimeout'
125
+ >;
126
+
117
127
  /**
118
128
  * This is kind of a hack. For `rewrapManyDataKey`, we have tests that
119
129
  * guarantee that when there are no matching keys, `rewrapManyDataKey` returns
@@ -153,6 +163,9 @@ export type StateMachineOptions = {
153
163
 
154
164
  /** TLS options for KMS requests, if set. */
155
165
  tlsOptions: CSFLEKMSTlsOptions;
166
+
167
+ /** Socket specific options we support. */
168
+ socketOptions: ClientEncryptionSocketOptions;
156
169
  } & Pick<BSONSerializeOptions, 'promoteLongs' | 'promoteValues'>;
157
170
 
158
171
  /**
@@ -289,10 +302,17 @@ export class StateMachine {
289
302
  async kmsRequest(request: MongoCryptKMSRequest): Promise<void> {
290
303
  const parsedUrl = request.endpoint.split(':');
291
304
  const port = parsedUrl[1] != null ? Number.parseInt(parsedUrl[1], 10) : HTTPS_PORT;
292
- const options: tls.ConnectionOptions & { host: string; port: number } = {
305
+ const socketOptions = autoSelectSocketOptions(this.options.socketOptions || {});
306
+ const options: tls.ConnectionOptions & {
307
+ host: string;
308
+ port: number;
309
+ autoSelectFamily?: boolean;
310
+ autoSelectFamilyAttemptTimeout?: number;
311
+ } = {
293
312
  host: parsedUrl[0],
294
313
  servername: parsedUrl[0],
295
- port
314
+ port,
315
+ ...socketOptions
296
316
  };
297
317
  const message = request.message;
298
318
  const buffer = new BufferPool();
@@ -351,10 +371,12 @@ export class StateMachine {
351
371
 
352
372
  try {
353
373
  if (this.options.proxyOptions && this.options.proxyOptions.proxyHost) {
354
- netSocket.connect({
374
+ const netSocketOptions = {
355
375
  host: this.options.proxyOptions.proxyHost,
356
- port: this.options.proxyOptions.proxyPort || 1080
357
- });
376
+ port: this.options.proxyOptions.proxyPort || 1080,
377
+ ...socketOptions
378
+ };
379
+ netSocket.connect(netSocketOptions);
358
380
  await willConnect;
359
381
 
360
382
  try {
@@ -269,6 +269,8 @@ export const LEGAL_TLS_SOCKET_OPTIONS = [
269
269
 
270
270
  /** @public */
271
271
  export const LEGAL_TCP_SOCKET_OPTIONS = [
272
+ 'autoSelectFamily',
273
+ 'autoSelectFamilyAttemptTimeout',
272
274
  'family',
273
275
  'hints',
274
276
  'localAddress',
@@ -740,6 +740,13 @@ export const OPTIONS = {
740
740
  autoEncryption: {
741
741
  type: 'record'
742
742
  },
743
+ autoSelectFamily: {
744
+ type: 'boolean',
745
+ default: true
746
+ },
747
+ autoSelectFamilyAttemptTimeout: {
748
+ type: 'uint'
749
+ },
743
750
  bsonRegExp: {
744
751
  type: 'boolean'
745
752
  },
@@ -17,6 +17,7 @@ import { GetMoreOperation } from '../operations/get_more';
17
17
  import { KillCursorsOperation } from '../operations/kill_cursors';
18
18
  import { ReadConcern, type ReadConcernLike } from '../read_concern';
19
19
  import { ReadPreference, type ReadPreferenceLike } from '../read_preference';
20
+ import { type AsyncDisposable, configureResourceManagement } from '../resource_management';
20
21
  import type { Server } from '../sdam/server';
21
22
  import { ClientSession, maybeClearPinnedConnection } from '../sessions';
22
23
  import { type MongoDBNamespace, squashError } from '../utils';
@@ -124,9 +125,12 @@ export type AbstractCursorEvents = {
124
125
 
125
126
  /** @public */
126
127
  export abstract class AbstractCursor<
127
- TSchema = any,
128
- CursorEvents extends AbstractCursorEvents = AbstractCursorEvents
129
- > extends TypedEventEmitter<CursorEvents> {
128
+ TSchema = any,
129
+ CursorEvents extends AbstractCursorEvents = AbstractCursorEvents
130
+ >
131
+ extends TypedEventEmitter<CursorEvents>
132
+ implements AsyncDisposable
133
+ {
130
134
  /** @internal */
131
135
  private cursorId: Long | null;
132
136
  /** @internal */
@@ -275,14 +279,25 @@ export abstract class AbstractCursor<
275
279
  return !!this.cursorClient.topology?.loadBalanced;
276
280
  }
277
281
 
282
+ /**
283
+ * @beta
284
+ * @experimental
285
+ * An alias for {@link AbstractCursor.close|AbstractCursor.close()}.
286
+ */
287
+ declare [Symbol.asyncDispose]: () => Promise<void>;
288
+ /** @internal */
289
+ async asyncDispose() {
290
+ await this.close();
291
+ }
292
+
278
293
  /** Returns current buffered documents length */
279
294
  bufferedCount(): number {
280
295
  return this.documents?.length ?? 0;
281
296
  }
282
297
 
283
298
  /** Returns current buffered documents */
284
- readBufferedDocuments(number?: number): TSchema[] {
285
- const bufferedDocs: TSchema[] = [];
299
+ readBufferedDocuments(number?: number): NonNullable<TSchema>[] {
300
+ const bufferedDocs: NonNullable<TSchema>[] = [];
286
301
  const documentsToRead = Math.min(
287
302
  number ?? this.documents?.length ?? 0,
288
303
  this.documents?.length ?? 0
@@ -297,6 +312,7 @@ export abstract class AbstractCursor<
297
312
 
298
313
  return bufferedDocs;
299
314
  }
315
+
300
316
  async *[Symbol.asyncIterator](): AsyncGenerator<TSchema, void, void> {
301
317
  if (this.isClosed) {
302
318
  return;
@@ -446,6 +462,9 @@ export abstract class AbstractCursor<
446
462
  }
447
463
  }
448
464
 
465
+ /**
466
+ * Frees any client-side resources used by the cursor.
467
+ */
449
468
  async close(): Promise<void> {
450
469
  await this.cleanup();
451
470
  }
@@ -457,13 +476,22 @@ export abstract class AbstractCursor<
457
476
  * cursor.rewind() can be used to reset the cursor.
458
477
  */
459
478
  async toArray(): Promise<TSchema[]> {
460
- const array = [];
479
+ const array: TSchema[] = [];
480
+ // at the end of the loop (since readBufferedDocuments is called) the buffer will be empty
481
+ // then, the 'await of' syntax will run a getMore call
461
482
  for await (const document of this) {
462
483
  array.push(document);
484
+ const docs = this.readBufferedDocuments();
485
+ if (this.transform != null) {
486
+ for (const doc of docs) {
487
+ array.push(await this.transformDocument(doc));
488
+ }
489
+ } else {
490
+ array.push(...docs);
491
+ }
463
492
  }
464
493
  return array;
465
494
  }
466
-
467
495
  /**
468
496
  * Add a cursor flag to the cursor
469
497
  *
@@ -804,7 +832,7 @@ export abstract class AbstractCursor<
804
832
  }
805
833
 
806
834
  /** @internal */
807
- private async transformDocument(document: NonNullable<TSchema>): Promise<TSchema> {
835
+ private async transformDocument(document: NonNullable<TSchema>): Promise<NonNullable<TSchema>> {
808
836
  if (this.transform == null) return document;
809
837
 
810
838
  try {
@@ -916,3 +944,5 @@ class ReadableCursorStream extends Readable {
916
944
  );
917
945
  }
918
946
  }
947
+
948
+ configureResourceManagement(AbstractCursor.prototype);
package/src/index.ts CHANGED
@@ -76,6 +76,7 @@ export {
76
76
  MongoWriteConcernError,
77
77
  WriteConcernErrorResult
78
78
  } from './error';
79
+ export { configureExplicitResourceManagement } from './resource_management';
79
80
  export {
80
81
  AbstractCursor,
81
82
  // Actual driver classes exported
@@ -248,6 +249,7 @@ export type {
248
249
  LocalKMSProviderConfiguration
249
250
  } from './client-side-encryption/providers/index';
250
251
  export type {
252
+ ClientEncryptionSocketOptions,
251
253
  ClientEncryptionTlsOptions,
252
254
  CSFLEKMSTlsOptions,
253
255
  StateMachineExecutable
@@ -521,6 +523,7 @@ export type {
521
523
  ReadPreferenceLikeOptions,
522
524
  ReadPreferenceOptions
523
525
  } from './read_preference';
526
+ export type { AsyncDisposable } from './resource_management';
524
527
  export type { ClusterTime, TimerQueue } from './sdam/common';
525
528
  export type {
526
529
  Monitor,