Skip to content

Commit d7fe9b8

Browse files
committed
chore: fix FLE, may revert.
1 parent 069d80e commit d7fe9b8

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/client-side-encryption/auto_encrypter.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66

77
import { deserialize, type Document, serialize } from '../bson';
88
import { type CommandOptions, type ProxyOptions } from '../cmap/connection';
9+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
910
import { getMongoDBClientEncryption } from '../deps';
1011
import { MongoRuntimeError } from '../error';
1112
import { MongoClient, type MongoClientOptions } from '../mongo_client';
@@ -473,8 +474,15 @@ export class AutoEncrypter {
473474
/**
474475
* Decrypt a command response
475476
*/
476-
async decrypt(response: Uint8Array | Document, options: CommandOptions = {}): Promise<Document> {
477-
const buffer = Buffer.isBuffer(response) ? response : serialize(response, options);
477+
async decrypt(
478+
response: Uint8Array | Document | MongoDBResponse,
479+
options: CommandOptions = {}
480+
): Promise<Document> {
481+
const buffer = MongoDBResponse.is(response)
482+
? response.toBytes()
483+
: Buffer.isBuffer(response)
484+
? response
485+
: serialize(response, options);
478486

479487
const context = this._mongocrypt.makeDecryptionContext(buffer);
480488

@@ -487,10 +495,11 @@ export class AutoEncrypter {
487495
});
488496

489497
const decorateResult = this[kDecorateResult];
490-
const result = await stateMachine.execute<Document>(this, context);
498+
const result = await stateMachine.execute(this, context, response.constructor);
491499
if (decorateResult) {
492500
decorateDecryptionResult(result, response);
493501
}
502+
494503
return result;
495504
}
496505

src/client-side-encryption/state_machine.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
serialize
1212
} from '../bson';
1313
import { type ProxyOptions } from '../cmap/connection';
14+
import { type MongoDBResponseConstructor } from '../cmap/wire_protocol/responses';
1415
import { getSocks, type SocksLib } from '../deps';
1516
import { type MongoClient, type MongoClientOptions } from '../mongo_client';
1617
import { BufferPool, MongoDBCollectionNamespace, promiseWithResolvers } from '../utils';
@@ -156,14 +157,15 @@ export class StateMachine {
156157
*/
157158
async execute<T extends Document>(
158159
executor: StateMachineExecutable,
159-
context: MongoCryptContext
160+
context: MongoCryptContext,
161+
responseType?: MongoDBResponseConstructor
160162
): Promise<T> {
161163
const keyVaultNamespace = executor._keyVaultNamespace;
162164
const keyVaultClient = executor._keyVaultClient;
163165
const metaDataClient = executor._metaDataClient;
164166
const mongocryptdClient = executor._mongocryptdClient;
165167
const mongocryptdManager = executor._mongocryptdManager;
166-
let result: T | null = null;
168+
let result: any | null = null;
167169

168170
while (context.state !== MONGOCRYPT_CTX_DONE && context.state !== MONGOCRYPT_CTX_ERROR) {
169171
debug(`[context#${context.id}] ${stateToString.get(context.state) || context.state}`);
@@ -252,7 +254,12 @@ export class StateMachine {
252254
const message = context.status.message || 'Finalization error';
253255
throw new MongoCryptError(message);
254256
}
255-
result = deserialize(finalizedContext, this.options) as T;
257+
258+
result =
259+
responseType != null
260+
? new responseType(finalizedContext)
261+
: (result = deserialize(finalizedContext, this.options) as T);
262+
256263
break;
257264
}
258265

src/cmap/wire_protocol/responses.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ export type MongoDBResponseConstructor = {
1717

1818
/** @internal */
1919
export class MongoDBResponse extends OnDemandDocument {
20+
static is(value: unknown): value is MongoDBResponse {
21+
return value instanceof MongoDBResponse;
22+
}
23+
2024
// {ok:1}
2125
static empty = new MongoDBResponse(new Uint8Array([13, 0, 0, 0, 16, 111, 107, 0, 1, 0, 0, 0, 0]));
2226

@@ -154,18 +158,18 @@ export class CursorResponse extends MongoDBResponse {
154158
this.documents = Object.defineProperties(Object.create(null), {
155159
length: {
156160
get: () => {
157-
return this.batchSize - this.iterated;
161+
return Math.max(this.batchSize - this.iterated, 0);
158162
}
159163
},
160164
shift: {
161165
value: (options?: BSONSerializeOptions) => {
162166
this.iterated += 1;
163-
const r = this.values?.next();
164-
if (!r || r.done) return null;
167+
const result = this.values?.next();
168+
if (!result || result.done) return null;
165169
if (options?.raw) {
166-
return r.value.toBytes();
170+
return result.value.toBytes();
167171
} else {
168-
return r.value.toObject(options);
172+
return result.value.toObject(options);
169173
}
170174
}
171175
},

0 commit comments

Comments
 (0)