@@ -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 ,
@@ -35,7 +34,7 @@ import {
35
34
* The schema for a DataKey in the key vault collection.
36
35
*/
37
36
export interface DataKey {
38
- _id : Binary ;
37
+ _id : UUID ;
39
38
version ?: number ;
40
39
keyAltNames ?: string [ ] ;
41
40
keyMaterial : Binary ;
@@ -125,18 +124,6 @@ export class ClientEncryption implements StateMachineExecutable {
125
124
*
126
125
* @example
127
126
* ```ts
128
- * // Using callbacks to create a local key
129
- * clientEncryption.createDataKey('local', (err, dataKey) => {
130
- * if (err) {
131
- * // This means creating the key failed.
132
- * } else {
133
- * // key creation succeeded
134
- * }
135
- * });
136
- * ```
137
- *
138
- * @example
139
- * ```ts
140
127
* // Using async/await to create a local key
141
128
* const dataKeyId = await clientEncryption.createDataKey('local');
142
129
* ```
@@ -164,19 +151,10 @@ export class ClientEncryption implements StateMachineExecutable {
164
151
* });
165
152
* ```
166
153
*/
167
- createDataKey (
154
+ async createDataKey (
168
155
provider : KMSProvider ,
169
- options ?: ClientEncryptionCreateDataKeyProviderOptions ,
170
- callback ?: Callback < DataKey >
171
- ) {
172
- if ( typeof options === 'function' ) {
173
- callback = options ;
174
- options = { } ;
175
- }
176
- if ( options == null ) {
177
- options = { } ;
178
- }
179
-
156
+ options : ClientEncryptionCreateDataKeyProviderOptions = { }
157
+ ) : Promise < UUID > {
180
158
const dataKey = Object . assign ( { provider } , options . masterKey ) ;
181
159
182
160
if ( options . keyAltNames && ! Array . isArray ( options . keyAltNames ) ) {
@@ -213,32 +191,17 @@ export class ClientEncryption implements StateMachineExecutable {
213
191
tlsOptions : this . _tlsOptions
214
192
} ) ;
215
193
216
- // @ts -expect-error We did not convert promiseOrCallback to TS
217
- return promiseOrCallback ( callback , cb => {
218
- stateMachine . execute < DataKey > ( this , context , ( err , dataKey ) => {
219
- if ( err || ! dataKey ) {
220
- cb ( err , null ) ;
221
- return ;
222
- }
194
+ const updatedDataKey = await stateMachine . executeAsync < DataKey > ( this , context ) ;
223
195
224
- const { db : dbName , collection : collectionName } = MongoDBCollectionNamespace . fromString (
225
- this . _keyVaultNamespace
226
- ) ;
196
+ const { db : dbName , collection : collectionName } = MongoDBCollectionNamespace . fromString (
197
+ this . _keyVaultNamespace
198
+ ) ;
227
199
228
- this . _keyVaultClient
229
- . db ( dbName )
230
- . collection < DataKey > ( collectionName )
231
- . insertOne ( dataKey , { writeConcern : { w : 'majority' } } )
232
- . then (
233
- result => {
234
- return cb ( null , result . insertedId ) ;
235
- } ,
236
- err => {
237
- cb ( err , null ) ;
238
- }
239
- ) ;
240
- } ) ;
241
- } ) ;
200
+ const { insertedId } = await this . _keyVaultClient
201
+ . db ( dbName )
202
+ . collection < DataKey > ( collectionName )
203
+ . insertOne ( updatedDataKey , { writeConcern : { w : 'majority' } } ) ;
204
+ return insertedId ;
242
205
}
243
206
244
207
/**
@@ -590,21 +553,7 @@ export class ClientEncryption implements StateMachineExecutable {
590
553
*
591
554
* @param value - The value that you wish to serialize. Must be of a type that can be serialized into BSON
592
555
* @param options -
593
- * @param callback - Optional callback to invoke when value is encrypted
594
- * @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.
595
- *
596
- * @example
597
- * ```ts
598
- * // Encryption with callback API
599
- * function encryptMyData(value, callback) {
600
- * clientEncryption.createDataKey('local', (err, keyId) => {
601
- * if (err) {
602
- * return callback(err);
603
- * }
604
- * clientEncryption.encrypt(value, { keyId, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic' }, callback);
605
- * });
606
- * }
607
- * ```
556
+ * @returns a Promise that either resolves with the encrypted value, or rejects with an error.
608
557
*
609
558
* @example
610
559
* ```ts
@@ -624,12 +573,8 @@ export class ClientEncryption implements StateMachineExecutable {
624
573
* }
625
574
* ```
626
575
*/
627
- encrypt (
628
- value : unknown ,
629
- options : ClientEncryptionEncryptOptions ,
630
- callback : Callback < Binary >
631
- ) : Promise < Binary > | void {
632
- return maybeCallback ( ( ) => this . _encrypt ( value , false , options ) , callback ) ;
576
+ async encrypt ( value : unknown , options : ClientEncryptionEncryptOptions ) : Promise < Binary > {
577
+ return this . _encrypt ( value , false , options ) ;
633
578
}
634
579
635
580
/**
@@ -661,16 +606,7 @@ export class ClientEncryption implements StateMachineExecutable {
661
606
* Explicitly decrypt a provided encrypted value
662
607
*
663
608
* @param value - An encrypted value
664
- * @param callback - Optional callback to invoke when value is decrypted
665
- * @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.
666
- *
667
- * ```ts
668
- * @example
669
- * // Decrypting value with callback API
670
- * function decryptMyValue(value, callback) {
671
- * clientEncryption.decrypt(value, callback);
672
- * }
673
- * ```
609
+ * @returns a Promise that either resolves with the decrypted value, or rejects with an error
674
610
*
675
611
* @example
676
612
* ```ts
@@ -680,7 +616,7 @@ export class ClientEncryption implements StateMachineExecutable {
680
616
* }
681
617
* ```
682
618
*/
683
- decrypt < T = any > ( value : Binary , callback ?: Callback < T > ) : Promise < T > | void {
619
+ async decrypt < T = any > ( value : Binary ) : Promise < T > {
684
620
const valueBuffer = serialize ( { v : value } ) ;
685
621
const context = this . _mongoCrypt . makeExplicitDecryptionContext ( valueBuffer ) ;
686
622
@@ -689,17 +625,9 @@ export class ClientEncryption implements StateMachineExecutable {
689
625
tlsOptions : this . _tlsOptions
690
626
} ) ;
691
627
692
- // @ts -expect-error We did not convert promiseOrCallback to TS
693
- return promiseOrCallback ( callback , cb => {
694
- stateMachine . execute < { v : T } > ( this , context , ( err , result ) => {
695
- if ( err || ! result ) {
696
- cb ( err , null ) ;
697
- return ;
698
- }
628
+ const { v } = await stateMachine . executeAsync < { v : T } > ( this , context ) ;
699
629
700
- cb ( null , result . v ) ;
701
- } ) ;
702
- } ) ;
630
+ return v ;
703
631
}
704
632
705
633
/**
0 commit comments