@@ -589,13 +589,29 @@ Value MongoCrypt::MakeEncryptionContext(const CallbackInfo& info) {
589
589
}
590
590
591
591
Value MongoCrypt::MakeExplicitEncryptionContext (const CallbackInfo& info) {
592
- std::unique_ptr<mongocrypt_ctx_t , MongoCryptContextDeleter> context (
593
- mongocrypt_ctx_new (_mongo_crypt.get ()));
594
-
595
592
Uint8Array valueBuffer = Uint8ArrayFromValue (info[0 ], " value" );
596
593
597
594
Object options = info.Length () > 1 ? info[1 ].ToObject () : Object::New (info.Env ());
598
595
596
+ if (!options.Get (" expressionMode" ).IsBoolean ()) {
597
+ throw TypeError::New (Env (), " option `expressionMode` is required." );
598
+ }
599
+
600
+ bool expression_mode = options.Get (" expressionMode" ).ToBoolean ();
601
+ ExplicitEncryptionContextInitFunction context_init_function =
602
+ expression_mode ? mongocrypt_ctx_explicit_encrypt_expression_init
603
+ : mongocrypt_ctx_explicit_encrypt_init;
604
+
605
+ return MakeExplicitEncryptionContextInternal (context_init_function, valueBuffer, options);
606
+ }
607
+
608
+ Value MongoCrypt::MakeExplicitEncryptionContextInternal (
609
+ ExplicitEncryptionContextInitFunction context_init_function,
610
+ const Uint8Array& valueBuffer,
611
+ const Object& options) {
612
+ std::unique_ptr<mongocrypt_ctx_t , MongoCryptContextDeleter> context (
613
+ mongocrypt_ctx_new (_mongo_crypt.get ()));
614
+
599
615
if (!options.Get (" keyId" ).IsUndefined ()) {
600
616
Uint8Array keyId = Uint8ArrayFromValue (options[" keyId" ], " keyId" );
601
617
@@ -658,12 +674,7 @@ Value MongoCrypt::MakeExplicitEncryptionContext(const CallbackInfo& info) {
658
674
std::unique_ptr<mongocrypt_binary_t , MongoCryptBinaryDeleter> binaryValue (
659
675
Uint8ArrayToBinary (valueBuffer));
660
676
661
- const bool isExpressionMode = options.Get (" expressionMode" ).ToBoolean ();
662
-
663
- const bool status =
664
- isExpressionMode
665
- ? mongocrypt_ctx_explicit_encrypt_expression_init (context.get (), binaryValue.get ())
666
- : mongocrypt_ctx_explicit_encrypt_init (context.get (), binaryValue.get ());
677
+ const bool status = context_init_function (context.get (), binaryValue.get ());
667
678
668
679
if (!status) {
669
680
throw TypeError::New (Env (), errorStringFromStatus (context.get ()));
0 commit comments