Skip to content

Commit b3c6494

Browse files
fix regress
1 parent 733ad19 commit b3c6494

File tree

9 files changed

+237
-85
lines changed

9 files changed

+237
-85
lines changed

src/bson.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,14 @@ export function resolveBSONOptions(
132132
options?.enableUtf8Validation ?? parentOptions?.enableUtf8Validation ?? true
133133
};
134134
}
135+
136+
/** @internal */
137+
export function parseUtf8ValidationOption(options?: { enableUtf8Validation?: boolean }): {
138+
utf8: { writeErrors: false } | false;
139+
} {
140+
const enableUtf8Validation = options?.enableUtf8Validation;
141+
if (enableUtf8Validation === false) {
142+
return { utf8: false };
143+
}
144+
return { utf8: { writeErrors: false } };
145+
}

src/cmap/connection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import {
6969
type MongoDBResponseConstructor
7070
} from './wire_protocol/responses';
7171
import { getReadPreference, isSharded } from './wire_protocol/shared';
72+
import { DeserializeOptions } from 'bson';
7273

7374
/** @internal */
7475
export interface CommandOptions extends BSONSerializeOptions {
@@ -487,7 +488,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
487488

488489
// If `documentsReturnedIn` not set or raw is not enabled, use input bson options
489490
// Otherwise, support raw flag. Raw only works for cursors that hardcode firstBatch/nextBatch fields
490-
const bsonOptions =
491+
const bsonOptions: DeserializeOptions =
491492
options.documentsReturnedIn == null || !options.raw
492493
? options
493494
: {

src/cmap/wire_protocol/on_demand/document.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import { DeserializeOptions } from 'bson';
12
import {
23
Binary,
3-
BSON,
44
type BSONElement,
55
BSONError,
66
type BSONSerializeOptions,
77
BSONType,
8+
deserialize,
89
getBigInt64LE,
910
getFloat64LE,
1011
getInt32LE,
1112
ObjectId,
1213
parseToElementsToArray,
14+
pluckBSONSerializeOptions,
1315
Timestamp,
1416
toUTF8
1517
} from '../../../bson';
@@ -329,8 +331,8 @@ export class OnDemandDocument {
329331
* Deserialize this object, DOES NOT cache result so avoid multiple invocations
330332
* @param options - BSON deserialization options
331333
*/
332-
public toObject(options?: BSONSerializeOptions): Record<string, any> {
333-
return BSON.deserialize(this.bson, {
334+
public toObject(options?: DeserializeOptions & { enableUtf8Validation?: never }): Record<string, any> {
335+
return deserialize(this.bson, {
334336
...options,
335337
index: this.offset,
336338
allowObjectSmallerThanBufferSize: true

src/cmap/wire_protocol/responses.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { DeserializeOptions } from 'bson';
12
import {
23
type BSONElement,
34
type BSONSerializeOptions,
45
BSONType,
56
type Document,
67
Long,
78
parseToElementsToArray,
8-
pluckBSONSerializeOptions,
99
type Timestamp
1010
} from '../../bson';
1111
import { MongoUnexpectedServerResponseError } from '../../error';
@@ -166,24 +166,6 @@ export class MongoDBResponse extends OnDemandDocument {
166166
}
167167
return this.clusterTime ?? null;
168168
}
169-
170-
public override toObject(options?: BSONSerializeOptions): Record<string, any> {
171-
const exactBSONOptions = {
172-
...pluckBSONSerializeOptions(options ?? {}),
173-
validation: this.parseBsonSerializationOptions(options)
174-
};
175-
return super.toObject(exactBSONOptions);
176-
}
177-
178-
private parseBsonSerializationOptions(options?: { enableUtf8Validation?: boolean }): {
179-
utf8: { writeErrors: false } | false;
180-
} {
181-
const enableUtf8Validation = options?.enableUtf8Validation;
182-
if (enableUtf8Validation === false) {
183-
return { utf8: false };
184-
}
185-
return { utf8: { writeErrors: false } };
186-
}
187169
}
188170

189171
/** @internal */
@@ -272,7 +254,7 @@ export class CursorResponse extends MongoDBResponse {
272254
);
273255
}
274256

275-
public shift(options?: BSONSerializeOptions): any {
257+
public shift(options?: DeserializeOptions & { __tag: 'shift options' }): any {
276258
if (this.iterated >= this.batchSize) {
277259
return null;
278260
}
@@ -324,7 +306,7 @@ export class ExplainedCursorResponse extends CursorResponse {
324306
return this._length;
325307
}
326308

327-
override shift(options?: BSONSerializeOptions | undefined) {
309+
override shift(options?: DeserializeOptions) {
328310
if (this._length === 0) return null;
329311
this._length -= 1;
330312
return this.toObject(options);

src/cursor/abstract_cursor.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Readable, Transform } from 'stream';
22

3-
import { type BSONSerializeOptions, type Document, Long, pluckBSONSerializeOptions } from '../bson';
3+
import { type BSONSerializeOptions, type Document, Long, parseUtf8ValidationOption, pluckBSONSerializeOptions } from '../bson';
44
import { type CursorResponse } from '../cmap/wire_protocol/responses';
55
import {
66
MongoAPIError,
@@ -21,6 +21,7 @@ import { type AsyncDisposable, configureResourceManagement } from '../resource_m
2121
import type { Server } from '../sdam/server';
2222
import { ClientSession, maybeClearPinnedConnection } from '../sessions';
2323
import { type MongoDBNamespace, squashError } from '../utils';
24+
import { DeserializeOptions } from 'bson';
2425

2526
/**
2627
* @internal
@@ -157,6 +158,8 @@ export abstract class AbstractCursor<
157158
/** @event */
158159
static readonly CLOSE = 'close' as const;
159160

161+
protected deserializationOptions: DeserializeOptions & { __tag: 'shift options' };
162+
160163
/** @internal */
161164
protected constructor(
162165
client: MongoClient,
@@ -211,6 +214,12 @@ export abstract class AbstractCursor<
211214
} else {
212215
this.cursorSession = this.cursorClient.startSession({ owner: this, explicit: false });
213216
}
217+
218+
this.deserializationOptions = {
219+
...this.cursorOptions,
220+
validation: parseUtf8ValidationOption(this.cursorOptions),
221+
__tag: 'shift options'
222+
}
214223
}
215224

216225
/**
@@ -304,7 +313,7 @@ export abstract class AbstractCursor<
304313
);
305314

306315
for (let count = 0; count < documentsToRead; count++) {
307-
const document = this.documents?.shift(this.cursorOptions);
316+
const document = this.documents?.shift(this.deserializationOptions);
308317
if (document != null) {
309318
bufferedDocs.push(document);
310319
}
@@ -406,7 +415,7 @@ export abstract class AbstractCursor<
406415
}
407416

408417
do {
409-
const doc = this.documents?.shift(this.cursorOptions);
418+
const doc = this.documents?.shift(this.deserializationOptions);
410419
if (doc != null) {
411420
if (this.transform != null) return await this.transformDocument(doc);
412421
return doc;
@@ -425,15 +434,15 @@ export abstract class AbstractCursor<
425434
throw new MongoCursorExhaustedError();
426435
}
427436

428-
let doc = this.documents?.shift(this.cursorOptions);
437+
let doc = this.documents?.shift(this.deserializationOptions);
429438
if (doc != null) {
430439
if (this.transform != null) return await this.transformDocument(doc);
431440
return doc;
432441
}
433442

434443
await this.fetchBatch();
435444

436-
doc = this.documents?.shift(this.cursorOptions);
445+
doc = this.documents?.shift(this.deserializationOptions);
437446
if (doc != null) {
438447
if (this.transform != null) return await this.transformDocument(doc);
439448
return doc;

src/cursor/aggregation_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
7676
explain: verbosity ?? true
7777
})
7878
)
79-
).shift(this.aggregateOptions);
79+
).shift(this.deserializationOptions);
8080
}
8181

8282
/** Add a stage to the aggregation pipeline

src/cursor/find_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
143143
explain: verbosity ?? true
144144
})
145145
)
146-
).shift(this.findOptions);
146+
).shift(this.deserializationOptions);
147147
}
148148

149149
/** Set the cursor query */

0 commit comments

Comments
 (0)