@@ -589,13 +589,29 @@ Value MongoCrypt::MakeEncryptionContext(const CallbackInfo& info) {
589589}
590590
591591Value MongoCrypt::MakeExplicitEncryptionContext (const CallbackInfo& info) {
592- std::unique_ptr<mongocrypt_ctx_t , MongoCryptContextDeleter> context (
593- mongocrypt_ctx_new (_mongo_crypt.get ()));
594-
595592 Uint8Array valueBuffer = Uint8ArrayFromValue (info[0 ], " value" );
596593
597594 Object options = info.Length () > 1 ? info[1 ].ToObject () : Object::New (info.Env ());
598595
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+
599615 if (!options.Get (" keyId" ).IsUndefined ()) {
600616 Uint8Array keyId = Uint8ArrayFromValue (options[" keyId" ], " keyId" );
601617
@@ -658,12 +674,7 @@ Value MongoCrypt::MakeExplicitEncryptionContext(const CallbackInfo& info) {
658674 std::unique_ptr<mongocrypt_binary_t , MongoCryptBinaryDeleter> binaryValue (
659675 Uint8ArrayToBinary (valueBuffer));
660676
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 ());
667678
668679 if (!status) {
669680 throw TypeError::New (Env (), errorStringFromStatus (context.get ()));
0 commit comments