@@ -112,8 +112,25 @@ export type CSFLEKMSTlsOptions = {
112112 azure ?: ClientEncryptionTlsOptions ;
113113} ;
114114
115- /** `{ v: [] }` */
116- const EMPTY_V = Uint8Array . from ( [ 13 , 0 , 0 , 0 , 4 , 118 , 0 , 5 , 0 , 0 , 0 , 0 , 0 ] ) ;
115+ /**
116+ * This is kind of a hack. For `rewrapManyDataKey`, we have tests that
117+ * guarantee that when there are no matching keys, `rewrapManyDataKey` returns
118+ * nothing. We also have tests for auto encryption that guarantee for `encrypt`
119+ * we return an error when there are no matching keys. This error is generated in
120+ * subsequent iterations of the state machine.
121+ * Some apis (`encrypt`) throw if there are no filter matches and others (`rewrapManyDataKey`)
122+ * do not. We set the result manually here, and let the state machine continue. `libmongocrypt`
123+ * will inform us if we need to error by setting the state to `MONGOCRYPT_CTX_ERROR` but
124+ * otherwise we'll return `{ v: [] }`.
125+ */
126+ const EMPTY_V = Uint8Array . from ( [
127+ ...[ 13 , 0 , 0 , 0 ] , // document size = 13 bytes
128+ ...[
129+ ...[ 4 , 118 , 0 ] , // array type (4), "v\x00" basic latin "v"
130+ ...[ 5 , 0 , 0 , 0 , 0 ] // empty document (5 byte size, null terminator)
131+ ] ,
132+ 0 // null terminator
133+ ] ) ;
117134
118135/**
119136 * @internal
@@ -211,15 +228,7 @@ export class StateMachine {
211228 const keys = await this . fetchKeys ( keyVaultClient , keyVaultNamespace , filter ) ;
212229
213230 if ( keys . length === 0 ) {
214- // This is kind of a hack. For `rewrapManyDataKey`, we have tests that
215- // guarantee that when there are no matching keys, `rewrapManyDataKey` returns
216- // nothing. We also have tests for auto encryption that guarantee for `encrypt`
217- // we return an error when there are no matching keys. This error is generated in
218- // subsequent iterations of the state machine.
219- // Some apis (`encrypt`) throw if there are no filter matches and others (`rewrapManyDataKey`)
220- // do not. We set the result manually here, and let the state machine continue. `libmongocrypt`
221- // will inform us if we need to error by setting the state to `MONGOCRYPT_CTX_ERROR` but
222- // otherwise we'll return `{ v: [] }`.
231+ // See docs on EMPTY_V
223232 result = EMPTY_V ;
224233 }
225234 for await ( const key of keys ) {
0 commit comments