6
6
7
7
import { deserialize , type Document , serialize } from '../bson' ;
8
8
import { type CommandOptions , type ProxyOptions } from '../cmap/connection' ;
9
+ import { kDecorateResult } from '../constants' ;
9
10
import { getMongoDBClientEncryption } from '../deps' ;
10
11
import { MongoRuntimeError } from '../error' ;
11
12
import { MongoClient , type MongoClientOptions } from '../mongo_client' ;
@@ -212,15 +213,6 @@ export const AutoEncryptionLoggerLevel = Object.freeze({
212
213
export type AutoEncryptionLoggerLevel =
213
214
( typeof AutoEncryptionLoggerLevel ) [ keyof typeof AutoEncryptionLoggerLevel ] ;
214
215
215
- // Typescript errors if we index objects with `Symbol.for(...)`, so
216
- // to avoid TS errors we pull them out into variables. Then we can type
217
- // the objects (and class) that we expect to see them on and prevent TS
218
- // errors.
219
- /** @internal */
220
- const kDecorateResult = Symbol . for ( '@@mdb.decorateDecryptionResult' ) ;
221
- /** @internal */
222
- const kDecoratedKeys = Symbol . for ( '@@mdb.decryptedKeys' ) ;
223
-
224
216
/**
225
217
* @internal An internal class to be used by the driver for auto encryption
226
218
* **NOTE**: Not meant to be instantiated directly, this is for internal use only.
@@ -467,16 +459,18 @@ export class AutoEncrypter {
467
459
proxyOptions : this . _proxyOptions ,
468
460
tlsOptions : this . _tlsOptions
469
461
} ) ;
470
- return await stateMachine . execute < Document > ( this , context ) ;
462
+
463
+ return deserialize ( await stateMachine . execute ( this , context ) , {
464
+ promoteValues : false ,
465
+ promoteLongs : false
466
+ } ) ;
471
467
}
472
468
473
469
/**
474
470
* Decrypt a command response
475
471
*/
476
- async decrypt ( response : Uint8Array | Document , options : CommandOptions = { } ) : Promise < Document > {
477
- const buffer = Buffer . isBuffer ( response ) ? response : serialize ( response , options ) ;
478
-
479
- const context = this . _mongocrypt . makeDecryptionContext ( buffer ) ;
472
+ async decrypt ( response : Uint8Array , options : CommandOptions = { } ) : Promise < Uint8Array > {
473
+ const context = this . _mongocrypt . makeDecryptionContext ( response ) ;
480
474
481
475
context . id = this . _contextCounter ++ ;
482
476
@@ -486,12 +480,7 @@ export class AutoEncrypter {
486
480
tlsOptions : this . _tlsOptions
487
481
} ) ;
488
482
489
- const decorateResult = this [ kDecorateResult ] ;
490
- const result = await stateMachine . execute < Document > ( this , context ) ;
491
- if ( decorateResult ) {
492
- decorateDecryptionResult ( result , response ) ;
493
- }
494
- return result ;
483
+ return await stateMachine . execute ( this , context ) ;
495
484
}
496
485
497
486
/**
@@ -518,53 +507,3 @@ export class AutoEncrypter {
518
507
return AutoEncrypter . getMongoCrypt ( ) . libmongocryptVersion ;
519
508
}
520
509
}
521
-
522
- /**
523
- * Recurse through the (identically-shaped) `decrypted` and `original`
524
- * objects and attach a `decryptedKeys` property on each sub-object that
525
- * contained encrypted fields. Because we only call this on BSON responses,
526
- * we do not need to worry about circular references.
527
- *
528
- * @internal
529
- */
530
- function decorateDecryptionResult (
531
- decrypted : Document & { [ kDecoratedKeys ] ?: Array < string > } ,
532
- original : Document ,
533
- isTopLevelDecorateCall = true
534
- ) : void {
535
- if ( isTopLevelDecorateCall ) {
536
- // The original value could have been either a JS object or a BSON buffer
537
- if ( Buffer . isBuffer ( original ) ) {
538
- original = deserialize ( original ) ;
539
- }
540
- if ( Buffer . isBuffer ( decrypted ) ) {
541
- throw new MongoRuntimeError ( 'Expected result of decryption to be deserialized BSON object' ) ;
542
- }
543
- }
544
-
545
- if ( ! decrypted || typeof decrypted !== 'object' ) return ;
546
- for ( const k of Object . keys ( decrypted ) ) {
547
- const originalValue = original [ k ] ;
548
-
549
- // An object was decrypted by libmongocrypt if and only if it was
550
- // a BSON Binary object with subtype 6.
551
- if ( originalValue && originalValue . _bsontype === 'Binary' && originalValue . sub_type === 6 ) {
552
- if ( ! decrypted [ kDecoratedKeys ] ) {
553
- Object . defineProperty ( decrypted , kDecoratedKeys , {
554
- value : [ ] ,
555
- configurable : true ,
556
- enumerable : false ,
557
- writable : false
558
- } ) ;
559
- }
560
- // this is defined in the preceding if-statement
561
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
562
- decrypted [ kDecoratedKeys ] ! . push ( k ) ;
563
- // Do not recurse into this decrypted value. It could be a sub-document/array,
564
- // in which case there is no original value associated with its subfields.
565
- continue ;
566
- }
567
-
568
- decorateDecryptionResult ( decrypted [ k ] , originalValue , false ) ;
569
- }
570
- }
0 commit comments