Skip to content

Commit 809b80d

Browse files
fix lints and refactor
1 parent a7065e9 commit 809b80d

File tree

3 files changed

+38
-29
lines changed

3 files changed

+38
-29
lines changed

addon/mongocrypt.cc

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -558,17 +558,28 @@ Value MongoCrypt::MakeEncryptionContext(const CallbackInfo& info) {
558558
}
559559

560560
Value MongoCrypt::MakeExplicitEncryptionContext(const CallbackInfo& info) {
561-
std::unique_ptr<mongocrypt_ctx_t, MongoCryptContextDeleter> context(
562-
mongocrypt_ctx_new(_mongo_crypt.get()));
563-
564561
Uint8Array valueBuffer = Uint8ArrayFromValue(info[0], "value");
565562

566563
Object options = info.Length() > 1 ? info[1].ToObject() : Object::New(info.Env());
567564

568565
if (!options.Has("expressionMode") || !options.Get("expressionMode").IsBoolean()) {
569-
throw TypeError::New(Env(), "option `expressionMode` is required.");
566+
throw TypeError::New(Env(), "option `expressionMode` is required.");
570567
}
571568

569+
bool expression_mode = options.Get("expressionMode").ToBoolean();
570+
auto context_init_function = expression_mode ? mongocrypt_ctx_explicit_encrypt_expression_init
571+
: mongocrypt_ctx_explicit_encrypt_init;
572+
573+
return MakeExplicitEncryptionContextInternal(context_init_function, valueBuffer, options);
574+
}
575+
576+
Value MongoCrypt::MakeExplicitEncryptionContextInternal(bool (*init_fn)(mongocrypt_ctx_t*,
577+
mongocrypt_binary_t*),
578+
const Uint8Array& valueBuffer,
579+
const Object& options) {
580+
std::unique_ptr<mongocrypt_ctx_t, MongoCryptContextDeleter> context(
581+
mongocrypt_ctx_new(_mongo_crypt.get()));
582+
572583
if (!options.Get("keyId").IsUndefined()) {
573584
Uint8Array keyId = Uint8ArrayFromValue(options["keyId"], "keyId");
574585

@@ -631,12 +642,7 @@ Value MongoCrypt::MakeExplicitEncryptionContext(const CallbackInfo& info) {
631642
std::unique_ptr<mongocrypt_binary_t, MongoCryptBinaryDeleter> binaryValue(
632643
Uint8ArrayToBinary(valueBuffer));
633644

634-
const bool isExpressionMode = options.Get("expressionMode").ToBoolean();
635-
636-
const bool status =
637-
isExpressionMode
638-
? mongocrypt_ctx_explicit_encrypt_expression_init(context.get(), binaryValue.get())
639-
: mongocrypt_ctx_explicit_encrypt_init(context.get(), binaryValue.get());
645+
const bool status = init_fn(context.get(), binaryValue.get());
640646

641647
if (!status) {
642648
throw TypeError::New(Env(), errorStringFromStatus(context.get()));

addon/mongocrypt.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class MongoCrypt : public Napi::ObjectWrap<MongoCrypt> {
4848
Napi::Value MakeExplicitDecryptionContext(const Napi::CallbackInfo& info);
4949
Napi::Value MakeDataKeyContext(const Napi::CallbackInfo& info);
5050
Napi::Value MakeRewrapManyDataKeyContext(const Napi::CallbackInfo& info);
51+
Napi::Value MakeExplicitEncryptionContextInternal(bool (*init_fn)(mongocrypt_ctx_t*,
52+
mongocrypt_binary_t*),
53+
const Napi::Uint8Array& value,
54+
const Napi::Object& options);
5155

5256
Napi::Value Status(const Napi::CallbackInfo& info);
5357
Napi::Value CryptSharedLibVersionInfo(const Napi::CallbackInfo& info);

test/bindings.test.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ describe('MongoCryptConstructor', () => {
1919
const mc = new MongoCrypt({
2020
kmsProviders: serialize({ aws: {} }),
2121
cryptoCallbacks: {
22-
aes256CbcEncryptHook: () => { },
23-
aes256CbcDecryptHook: () => { },
24-
aes256CtrEncryptHook: () => { },
25-
aes256CtrDecryptHook: () => { },
22+
aes256CbcEncryptHook: () => {},
23+
aes256CbcDecryptHook: () => {},
24+
aes256CtrEncryptHook: () => {},
25+
aes256CtrDecryptHook: () => {},
2626
randomHook,
27-
hmacSha512Hook: () => { },
28-
hmacSha256Hook: () => { },
29-
sha256Hook: () => { },
30-
signRsaSha256Hook: () => { }
27+
hmacSha512Hook: () => {},
28+
hmacSha256Hook: () => {},
29+
sha256Hook: () => {},
30+
signRsaSha256Hook: () => {}
3131
}
3232
});
3333

@@ -41,17 +41,17 @@ describe('MongoCryptConstructor', () => {
4141
kmsProviders: serialize({ aws: {} }),
4242
schemaMap: serialize({}),
4343
encryptedFieldsMap: serialize({}),
44-
logger: () => { },
44+
logger: () => {},
4545
cryptoCallbacks: {
46-
aes256CbcEncryptHook: () => { },
47-
aes256CbcDecryptHook: () => { },
48-
aes256CtrEncryptHook: () => { },
49-
aes256CtrDecryptHook: () => { },
46+
aes256CbcEncryptHook: () => {},
47+
aes256CbcDecryptHook: () => {},
48+
aes256CtrEncryptHook: () => {},
49+
aes256CtrDecryptHook: () => {},
5050
randomHook,
51-
hmacSha512Hook: () => { },
52-
hmacSha256Hook: () => { },
53-
sha256Hook: () => { },
54-
signRsaSha256Hook: () => { }
51+
hmacSha512Hook: () => {},
52+
hmacSha256Hook: () => {},
53+
sha256Hook: () => {},
54+
signRsaSha256Hook: () => {}
5555
},
5656

5757
bypassQueryAnalysis: false
@@ -388,7 +388,6 @@ describe('MongoCryptConstructor', () => {
388388
});
389389
});
390390

391-
392391
describe('options.expressionMode', function () {
393392
it('throws if `expressionMode` is not defined', function () {
394393
expect(() =>
@@ -401,7 +400,7 @@ describe('MongoCryptConstructor', () => {
401400
.to.throw(/option `expressionMode` is required./)
402401
.to.be.instanceOf(TypeError);
403402
});
404-
})
403+
});
405404
});
406405
});
407406

0 commit comments

Comments
 (0)