@@ -24,7 +24,8 @@ import { type MongoClient, type MongoClientOptions } from '../mongo_client';
2424import { type Filter , type WithId } from '../mongo_types' ;
2525import { type CreateCollectionOptions } from '../operations/create_collection' ;
2626import { type DeleteResult } from '../operations/delete' ;
27- import { MongoDBCollectionNamespace } from '../utils' ;
27+ import { TimeoutContext } from '../timeout' ;
28+ import { MongoDBCollectionNamespace , resolveTimeoutOptions } from '../utils' ;
2829import * as cryptoCallbacks from './crypto_callbacks' ;
2930import {
3031 MongoCryptCreateDataKeyError ,
@@ -74,6 +75,8 @@ export class ClientEncryption {
7475 _tlsOptions : CSFLEKMSTlsOptions ;
7576 /** @internal */
7677 _kmsProviders : KMSProviders ;
78+ /** @internal */
79+ _timeoutMS ?: number ;
7780
7881 /** @internal */
7982 _mongoCrypt : MongoCrypt ;
@@ -120,6 +123,8 @@ export class ClientEncryption {
120123 this . _proxyOptions = options . proxyOptions ?? { } ;
121124 this . _tlsOptions = options . tlsOptions ?? { } ;
122125 this . _kmsProviders = options . kmsProviders || { } ;
126+ const { timeoutMS } = resolveTimeoutOptions ( client , options ) ;
127+ this . _timeoutMS = timeoutMS ;
123128
124129 if ( options . keyVaultNamespace == null ) {
125130 throw new MongoCryptInvalidArgumentError ( 'Missing required option `keyVaultNamespace`' ) ;
@@ -212,7 +217,7 @@ export class ClientEncryption {
212217 const stateMachine = new StateMachine ( {
213218 proxyOptions : this . _proxyOptions ,
214219 tlsOptions : this . _tlsOptions ,
215- socketOptions : autoSelectSocketOptions ( this . _client . options )
220+ socketOptions : autoSelectSocketOptions ( this . _client . s . options )
216221 } ) ;
217222
218223 const dataKey = deserialize ( await stateMachine . execute ( this , context ) ) as DataKey ;
@@ -270,10 +275,14 @@ export class ClientEncryption {
270275 const stateMachine = new StateMachine ( {
271276 proxyOptions : this . _proxyOptions ,
272277 tlsOptions : this . _tlsOptions ,
273- socketOptions : autoSelectSocketOptions ( this . _client . options )
278+ socketOptions : autoSelectSocketOptions ( this . _client . s . options )
274279 } ) ;
275280
276- const { v : dataKeys } = deserialize ( await stateMachine . execute ( this , context ) ) ;
281+ const timeoutContext = TimeoutContext . create (
282+ resolveTimeoutOptions ( this . _client , { timeoutMS : this . _timeoutMS } )
283+ ) ;
284+
285+ const { v : dataKeys } = deserialize ( await stateMachine . execute ( this , context , timeoutContext ) ) ;
277286 if ( dataKeys . length === 0 ) {
278287 return { } ;
279288 }
@@ -303,7 +312,8 @@ export class ClientEncryption {
303312 . db ( dbName )
304313 . collection < DataKey > ( collectionName )
305314 . bulkWrite ( replacements , {
306- writeConcern : { w : 'majority' }
315+ writeConcern : { w : 'majority' } ,
316+ timeoutMS : timeoutContext . csotEnabled ( ) ? timeoutContext ?. remainingTimeMS : undefined
307317 } ) ;
308318
309319 return { bulkWriteResult : result } ;
@@ -332,7 +342,7 @@ export class ClientEncryption {
332342 return await this . _keyVaultClient
333343 . db ( dbName )
334344 . collection < DataKey > ( collectionName )
335- . deleteOne ( { _id } , { writeConcern : { w : 'majority' } } ) ;
345+ . deleteOne ( { _id } , { writeConcern : { w : 'majority' } , timeoutMS : this . _timeoutMS } ) ;
336346 }
337347
338348 /**
@@ -355,7 +365,7 @@ export class ClientEncryption {
355365 return this . _keyVaultClient
356366 . db ( dbName )
357367 . collection < DataKey > ( collectionName )
358- . find ( { } , { readConcern : { level : 'majority' } } ) ;
368+ . find ( { } , { readConcern : { level : 'majority' } , timeoutMS : this . _timeoutMS } ) ;
359369 }
360370
361371 /**
@@ -381,7 +391,7 @@ export class ClientEncryption {
381391 return await this . _keyVaultClient
382392 . db ( dbName )
383393 . collection < DataKey > ( collectionName )
384- . findOne ( { _id } , { readConcern : { level : 'majority' } } ) ;
394+ . findOne ( { _id } , { readConcern : { level : 'majority' } , timeoutMS : this . _timeoutMS } ) ;
385395 }
386396
387397 /**
@@ -408,7 +418,10 @@ export class ClientEncryption {
408418 return await this . _keyVaultClient
409419 . db ( dbName )
410420 . collection < DataKey > ( collectionName )
411- . findOne ( { keyAltNames : keyAltName } , { readConcern : { level : 'majority' } } ) ;
421+ . findOne (
422+ { keyAltNames : keyAltName } ,
423+ { readConcern : { level : 'majority' } , timeoutMS : this . _timeoutMS }
424+ ) ;
412425 }
413426
414427 /**
@@ -442,7 +455,7 @@ export class ClientEncryption {
442455 . findOneAndUpdate (
443456 { _id } ,
444457 { $addToSet : { keyAltNames : keyAltName } } ,
445- { writeConcern : { w : 'majority' } , returnDocument : 'before' }
458+ { writeConcern : { w : 'majority' } , returnDocument : 'before' , timeoutMS : this . _timeoutMS }
446459 ) ;
447460
448461 return value ;
@@ -503,7 +516,8 @@ export class ClientEncryption {
503516 . collection < DataKey > ( collectionName )
504517 . findOneAndUpdate ( { _id } , pipeline , {
505518 writeConcern : { w : 'majority' } ,
506- returnDocument : 'before'
519+ returnDocument : 'before' ,
520+ timeoutMS : this . _timeoutMS
507521 } ) ;
508522
509523 return value ;
@@ -650,7 +664,7 @@ export class ClientEncryption {
650664 const stateMachine = new StateMachine ( {
651665 proxyOptions : this . _proxyOptions ,
652666 tlsOptions : this . _tlsOptions ,
653- socketOptions : autoSelectSocketOptions ( this . _client . options )
667+ socketOptions : autoSelectSocketOptions ( this . _client . s . options )
654668 } ) ;
655669
656670 const { v } = deserialize ( await stateMachine . execute ( this , context ) ) ;
@@ -729,7 +743,7 @@ export class ClientEncryption {
729743 const stateMachine = new StateMachine ( {
730744 proxyOptions : this . _proxyOptions ,
731745 tlsOptions : this . _tlsOptions ,
732- socketOptions : autoSelectSocketOptions ( this . _client . options )
746+ socketOptions : autoSelectSocketOptions ( this . _client . s . options )
733747 } ) ;
734748 const context = this . _mongoCrypt . makeExplicitEncryptionContext ( valueBuffer , contextOptions ) ;
735749
@@ -818,6 +832,11 @@ export interface ClientEncryptionOptions {
818832 * TLS options for kms providers to use.
819833 */
820834 tlsOptions ?: CSFLEKMSTlsOptions ;
835+
836+ /**
837+ * The timeout setting to be used for all the operations on ClientEncryption.
838+ */
839+ timeoutMS ?: number ;
821840}
822841
823842/**
0 commit comments