@@ -5,7 +5,7 @@ import type {
5
5
MongoCryptOptions
6
6
} from 'mongodb-client-encryption' ;
7
7
8
- import { type Binary , type Document , type Long , serialize } from '../bson' ;
8
+ import { type Binary , type Document , type Long , serialize , type UUID } from '../bson' ;
9
9
import { type AnyBulkWriteOperation , type BulkWriteResult } from '../bulk/common' ;
10
10
import { type ProxyOptions } from '../cmap/connection' ;
11
11
import { type Collection } from '../collection' ;
@@ -16,8 +16,7 @@ import { type MongoClient } from '../mongo_client';
16
16
import { type Filter } from '../mongo_types' ;
17
17
import { type CreateCollectionOptions } from '../operations/create_collection' ;
18
18
import { type DeleteResult } from '../operations/delete' ;
19
- import { type Callback , MongoDBCollectionNamespace } from '../utils' ;
20
- import { maybeCallback , promiseOrCallback } from './common' ;
19
+ import { MongoDBCollectionNamespace } from '../utils' ;
21
20
import * as cryptoCallbacks from './crypto_callbacks' ;
22
21
import {
23
22
MongoCryptCreateDataKeyError ,
@@ -36,7 +35,7 @@ import { type CSFLEKMSTlsOptions, StateMachine } from './state_machine';
36
35
* The schema for a DataKey in the key vault collection.
37
36
*/
38
37
export interface DataKey {
39
- _id : Binary ;
38
+ _id : UUID ;
40
39
version ?: number ;
41
40
keyAltNames ?: string [ ] ;
42
41
keyMaterial : Binary ;
@@ -133,18 +132,6 @@ export class ClientEncryption {
133
132
*
134
133
* @example
135
134
* ```ts
136
- * // Using callbacks to create a local key
137
- * clientEncryption.createDataKey('local', (err, dataKey) => {
138
- * if (err) {
139
- * // This means creating the key failed.
140
- * } else {
141
- * // key creation succeeded
142
- * }
143
- * });
144
- * ```
145
- *
146
- * @example
147
- * ```ts
148
135
* // Using async/await to create a local key
149
136
* const dataKeyId = await clientEncryption.createDataKey('local');
150
137
* ```
@@ -172,21 +159,10 @@ export class ClientEncryption {
172
159
* });
173
160
* ```
174
161
*/
175
- createDataKey (
162
+ async createDataKey (
176
163
provider : ClientEncryptionDataKeyProvider ,
177
- options ?: ClientEncryptionCreateDataKeyProviderOptions ,
178
- callback ?: Callback < DataKey >
179
- ) {
180
- if ( typeof options === 'function' ) {
181
- callback = options ;
182
- options = { } ;
183
- }
184
- if ( options == null ) {
185
- options = { } ;
186
- }
187
-
188
- const dataKey = Object . assign ( { provider } , options . masterKey ) ;
189
-
164
+ options : ClientEncryptionCreateDataKeyProviderOptions = { }
165
+ ) : Promise < UUID > {
190
166
if ( options . keyAltNames && ! Array . isArray ( options . keyAltNames ) ) {
191
167
throw new MongoCryptInvalidArgumentError (
192
168
`Option "keyAltNames" must be an array of strings, but was of type ${ typeof options . keyAltNames } .`
@@ -211,42 +187,33 @@ export class ClientEncryption {
211
187
keyMaterial = serialize ( { keyMaterial : options . keyMaterial } ) ;
212
188
}
213
189
214
- const dataKeyBson = serialize ( dataKey ) ;
190
+ const dataKeyBson = serialize ( {
191
+ provider,
192
+ ...options . masterKey
193
+ } ) ;
194
+
215
195
const context = this . _mongoCrypt . makeDataKeyContext ( dataKeyBson , {
216
196
keyAltNames,
217
197
keyMaterial
218
198
} ) ;
199
+
219
200
const stateMachine = new StateMachine ( {
220
201
proxyOptions : this . _proxyOptions ,
221
202
tlsOptions : this . _tlsOptions
222
203
} ) ;
223
204
224
- // @ts -expect-error We did not convert promiseOrCallback to TS
225
- return promiseOrCallback ( callback , cb => {
226
- stateMachine . execute < DataKey > ( this , context , ( err , dataKey ) => {
227
- if ( err || ! dataKey ) {
228
- cb ( err , null ) ;
229
- return ;
230
- }
205
+ const dataKey = await stateMachine . executeAsync < DataKey > ( this , context ) ;
231
206
232
- const { db : dbName , collection : collectionName } = MongoDBCollectionNamespace . fromString (
233
- this . _keyVaultNamespace
234
- ) ;
207
+ const { db : dbName , collection : collectionName } = MongoDBCollectionNamespace . fromString (
208
+ this . _keyVaultNamespace
209
+ ) ;
235
210
236
- this . _keyVaultClient
237
- . db ( dbName )
238
- . collection < DataKey > ( collectionName )
239
- . insertOne ( dataKey , { writeConcern : { w : 'majority' } } )
240
- . then (
241
- result => {
242
- return cb ( null , result . insertedId ) ;
243
- } ,
244
- err => {
245
- cb ( err , null ) ;
246
- }
247
- ) ;
248
- } ) ;
249
- } ) ;
211
+ const { insertedId } = await this . _keyVaultClient
212
+ . db ( dbName )
213
+ . collection < DataKey > ( collectionName )
214
+ . insertOne ( dataKey , { writeConcern : { w : 'majority' } } ) ;
215
+
216
+ return insertedId ;
250
217
}
251
218
252
219
/**
@@ -601,21 +568,7 @@ export class ClientEncryption {
601
568
*
602
569
* @param value - The value that you wish to serialize. Must be of a type that can be serialized into BSON
603
570
* @param options -
604
- * @param callback - Optional callback to invoke when value is encrypted
605
- * @returns If no callback is provided, returns a Promise that either resolves with the encrypted value, or rejects with an error. If a callback is provided, returns nothing.
606
- *
607
- * @example
608
- * ```ts
609
- * // Encryption with callback API
610
- * function encryptMyData(value, callback) {
611
- * clientEncryption.createDataKey('local', (err, keyId) => {
612
- * if (err) {
613
- * return callback(err);
614
- * }
615
- * clientEncryption.encrypt(value, { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' }, callback);
616
- * });
617
- * }
618
- * ```
571
+ * @returns a Promise that either resolves with the encrypted value, or rejects with an error.
619
572
*
620
573
* @example
621
574
* ```ts
@@ -635,12 +588,8 @@ export class ClientEncryption {
635
588
* }
636
589
* ```
637
590
*/
638
- encrypt (
639
- value : unknown ,
640
- options : ClientEncryptionEncryptOptions ,
641
- callback : Callback < Binary >
642
- ) : Promise < Binary > | void {
643
- return maybeCallback ( ( ) => this . _encrypt ( value , false , options ) , callback ) ;
591
+ async encrypt ( value : unknown , options : ClientEncryptionEncryptOptions ) : Promise < Binary > {
592
+ return this . _encrypt ( value , false , options ) ;
644
593
}
645
594
646
595
/**
@@ -672,16 +621,7 @@ export class ClientEncryption {
672
621
* Explicitly decrypt a provided encrypted value
673
622
*
674
623
* @param value - An encrypted value
675
- * @param callback - Optional callback to invoke when value is decrypted
676
- * @returns If no callback is provided, returns a Promise that either resolves with the decrypted value, or rejects with an error. If a callback is provided, returns nothing.
677
- *
678
- * ```ts
679
- * @example
680
- * // Decrypting value with callback API
681
- * function decryptMyValue(value, callback) {
682
- * clientEncryption.decrypt(value, callback);
683
- * }
684
- * ```
624
+ * @returns a Promise that either resolves with the decrypted value, or rejects with an error
685
625
*
686
626
* @example
687
627
* ```ts
@@ -691,7 +631,7 @@ export class ClientEncryption {
691
631
* }
692
632
* ```
693
633
*/
694
- decrypt < T = any > ( value : Binary , callback ?: Callback < T > ) : Promise < T > | void {
634
+ async decrypt < T = any > ( value : Binary ) : Promise < T > {
695
635
const valueBuffer = serialize ( { v : value } ) ;
696
636
const context = this . _mongoCrypt . makeExplicitDecryptionContext ( valueBuffer ) ;
697
637
@@ -700,17 +640,9 @@ export class ClientEncryption {
700
640
tlsOptions : this . _tlsOptions
701
641
} ) ;
702
642
703
- // @ts -expect-error We did not convert promiseOrCallback to TS
704
- return promiseOrCallback ( callback , cb => {
705
- stateMachine . execute < { v : T } > ( this , context , ( err , result ) => {
706
- if ( err || ! result ) {
707
- cb ( err , null ) ;
708
- return ;
709
- }
643
+ const { v } = await stateMachine . executeAsync < { v : T } > ( this , context ) ;
710
644
711
- cb ( null , result . v ) ;
712
- } ) ;
713
- } ) ;
645
+ return v ;
714
646
}
715
647
716
648
/**
0 commit comments