@@ -24,7 +24,8 @@ import { type MongoClient, type MongoClientOptions } from '../mongo_client';
24
24
import { type Filter , type WithId } from '../mongo_types' ;
25
25
import { type CreateCollectionOptions } from '../operations/create_collection' ;
26
26
import { type DeleteResult } from '../operations/delete' ;
27
- import { MongoDBCollectionNamespace } from '../utils' ;
27
+ import { TimeoutContext } from '../timeout' ;
28
+ import { MongoDBCollectionNamespace , resolveTimeoutOptions } from '../utils' ;
28
29
import * as cryptoCallbacks from './crypto_callbacks' ;
29
30
import {
30
31
MongoCryptCreateDataKeyError ,
@@ -74,6 +75,8 @@ export class ClientEncryption {
74
75
_tlsOptions : CSFLEKMSTlsOptions ;
75
76
/** @internal */
76
77
_kmsProviders : KMSProviders ;
78
+ /** @internal */
79
+ _timeoutMS ?: number ;
77
80
78
81
/** @internal */
79
82
_mongoCrypt : MongoCrypt ;
@@ -120,6 +123,8 @@ export class ClientEncryption {
120
123
this . _proxyOptions = options . proxyOptions ?? { } ;
121
124
this . _tlsOptions = options . tlsOptions ?? { } ;
122
125
this . _kmsProviders = options . kmsProviders || { } ;
126
+ const { timeoutMS } = resolveTimeoutOptions ( client , options ) ;
127
+ this . _timeoutMS = timeoutMS ;
123
128
124
129
if ( options . keyVaultNamespace == null ) {
125
130
throw new MongoCryptInvalidArgumentError ( 'Missing required option `keyVaultNamespace`' ) ;
@@ -212,7 +217,7 @@ export class ClientEncryption {
212
217
const stateMachine = new StateMachine ( {
213
218
proxyOptions : this . _proxyOptions ,
214
219
tlsOptions : this . _tlsOptions ,
215
- socketOptions : autoSelectSocketOptions ( this . _client . options )
220
+ socketOptions : autoSelectSocketOptions ( this . _client . s . options )
216
221
} ) ;
217
222
218
223
const dataKey = deserialize ( await stateMachine . execute ( this , context ) ) as DataKey ;
@@ -270,10 +275,14 @@ export class ClientEncryption {
270
275
const stateMachine = new StateMachine ( {
271
276
proxyOptions : this . _proxyOptions ,
272
277
tlsOptions : this . _tlsOptions ,
273
- socketOptions : autoSelectSocketOptions ( this . _client . options )
278
+ socketOptions : autoSelectSocketOptions ( this . _client . s . options )
274
279
} ) ;
275
280
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 ) ) ;
277
286
if ( dataKeys . length === 0 ) {
278
287
return { } ;
279
288
}
@@ -303,7 +312,8 @@ export class ClientEncryption {
303
312
. db ( dbName )
304
313
. collection < DataKey > ( collectionName )
305
314
. bulkWrite ( replacements , {
306
- writeConcern : { w : 'majority' }
315
+ writeConcern : { w : 'majority' } ,
316
+ timeoutMS : timeoutContext . csotEnabled ( ) ? timeoutContext ?. remainingTimeMS : undefined
307
317
} ) ;
308
318
309
319
return { bulkWriteResult : result } ;
@@ -332,7 +342,7 @@ export class ClientEncryption {
332
342
return await this . _keyVaultClient
333
343
. db ( dbName )
334
344
. collection < DataKey > ( collectionName )
335
- . deleteOne ( { _id } , { writeConcern : { w : 'majority' } } ) ;
345
+ . deleteOne ( { _id } , { writeConcern : { w : 'majority' } , timeoutMS : this . _timeoutMS } ) ;
336
346
}
337
347
338
348
/**
@@ -355,7 +365,7 @@ export class ClientEncryption {
355
365
return this . _keyVaultClient
356
366
. db ( dbName )
357
367
. collection < DataKey > ( collectionName )
358
- . find ( { } , { readConcern : { level : 'majority' } } ) ;
368
+ . find ( { } , { readConcern : { level : 'majority' } , timeoutMS : this . _timeoutMS } ) ;
359
369
}
360
370
361
371
/**
@@ -381,7 +391,7 @@ export class ClientEncryption {
381
391
return await this . _keyVaultClient
382
392
. db ( dbName )
383
393
. collection < DataKey > ( collectionName )
384
- . findOne ( { _id } , { readConcern : { level : 'majority' } } ) ;
394
+ . findOne ( { _id } , { readConcern : { level : 'majority' } , timeoutMS : this . _timeoutMS } ) ;
385
395
}
386
396
387
397
/**
@@ -408,7 +418,10 @@ export class ClientEncryption {
408
418
return await this . _keyVaultClient
409
419
. db ( dbName )
410
420
. collection < DataKey > ( collectionName )
411
- . findOne ( { keyAltNames : keyAltName } , { readConcern : { level : 'majority' } } ) ;
421
+ . findOne (
422
+ { keyAltNames : keyAltName } ,
423
+ { readConcern : { level : 'majority' } , timeoutMS : this . _timeoutMS }
424
+ ) ;
412
425
}
413
426
414
427
/**
@@ -442,7 +455,7 @@ export class ClientEncryption {
442
455
. findOneAndUpdate (
443
456
{ _id } ,
444
457
{ $addToSet : { keyAltNames : keyAltName } } ,
445
- { writeConcern : { w : 'majority' } , returnDocument : 'before' }
458
+ { writeConcern : { w : 'majority' } , returnDocument : 'before' , timeoutMS : this . _timeoutMS }
446
459
) ;
447
460
448
461
return value ;
@@ -503,7 +516,8 @@ export class ClientEncryption {
503
516
. collection < DataKey > ( collectionName )
504
517
. findOneAndUpdate ( { _id } , pipeline , {
505
518
writeConcern : { w : 'majority' } ,
506
- returnDocument : 'before'
519
+ returnDocument : 'before' ,
520
+ timeoutMS : this . _timeoutMS
507
521
} ) ;
508
522
509
523
return value ;
@@ -650,7 +664,7 @@ export class ClientEncryption {
650
664
const stateMachine = new StateMachine ( {
651
665
proxyOptions : this . _proxyOptions ,
652
666
tlsOptions : this . _tlsOptions ,
653
- socketOptions : autoSelectSocketOptions ( this . _client . options )
667
+ socketOptions : autoSelectSocketOptions ( this . _client . s . options )
654
668
} ) ;
655
669
656
670
const { v } = deserialize ( await stateMachine . execute ( this , context ) ) ;
@@ -729,7 +743,7 @@ export class ClientEncryption {
729
743
const stateMachine = new StateMachine ( {
730
744
proxyOptions : this . _proxyOptions ,
731
745
tlsOptions : this . _tlsOptions ,
732
- socketOptions : autoSelectSocketOptions ( this . _client . options )
746
+ socketOptions : autoSelectSocketOptions ( this . _client . s . options )
733
747
} ) ;
734
748
const context = this . _mongoCrypt . makeExplicitEncryptionContext ( valueBuffer , contextOptions ) ;
735
749
@@ -818,6 +832,11 @@ export interface ClientEncryptionOptions {
818
832
* TLS options for kms providers to use.
819
833
*/
820
834
tlsOptions ?: CSFLEKMSTlsOptions ;
835
+
836
+ /**
837
+ * The timeout setting to be used for all the operations on ClientEncryption.
838
+ */
839
+ timeoutMS ?: number ;
821
840
}
822
841
823
842
/**
0 commit comments