66
77import { deserialize , type Document , serialize } from '../bson' ;
88import { type CommandOptions , type ProxyOptions } from '../cmap/connection' ;
9+ import { kDecorateResult } from '../constants' ;
910import { getMongoDBClientEncryption } from '../deps' ;
1011import { MongoRuntimeError } from '../error' ;
1112import { MongoClient , type MongoClientOptions } from '../mongo_client' ;
@@ -212,15 +213,6 @@ export const AutoEncryptionLoggerLevel = Object.freeze({
212213export type AutoEncryptionLoggerLevel =
213214 ( typeof AutoEncryptionLoggerLevel ) [ keyof typeof AutoEncryptionLoggerLevel ] ;
214215
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-
224216/**
225217 * @internal An internal class to be used by the driver for auto encryption
226218 * **NOTE**: Not meant to be instantiated directly, this is for internal use only.
@@ -467,16 +459,18 @@ export class AutoEncrypter {
467459 proxyOptions : this . _proxyOptions ,
468460 tlsOptions : this . _tlsOptions
469461 } ) ;
470- return await stateMachine . execute < Document > ( this , context ) ;
462+
463+ return deserialize ( await stateMachine . execute ( this , context ) , {
464+ promoteValues : false ,
465+ promoteLongs : false
466+ } ) ;
471467 }
472468
473469 /**
474470 * Decrypt a command response
475471 */
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 ) ;
480474
481475 context . id = this . _contextCounter ++ ;
482476
@@ -486,12 +480,7 @@ export class AutoEncrypter {
486480 tlsOptions : this . _tlsOptions
487481 } ) ;
488482
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 ) ;
495484 }
496485
497486 /**
@@ -518,53 +507,3 @@ export class AutoEncrypter {
518507 return AutoEncrypter . getMongoCrypt ( ) . libmongocryptVersion ;
519508 }
520509}
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