Skip to content

Commit

Permalink
fix regress
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Sep 4, 2024
1 parent 733ad19 commit b3c6494
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 85 deletions.
11 changes: 11 additions & 0 deletions src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,14 @@ export function resolveBSONOptions(
options?.enableUtf8Validation ?? parentOptions?.enableUtf8Validation ?? true
};
}

/** @internal */
export function parseUtf8ValidationOption(options?: { enableUtf8Validation?: boolean }): {
utf8: { writeErrors: false } | false;
} {
const enableUtf8Validation = options?.enableUtf8Validation;
if (enableUtf8Validation === false) {
return { utf8: false };
}
return { utf8: { writeErrors: false } };
}
3 changes: 2 additions & 1 deletion src/cmap/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
type MongoDBResponseConstructor
} from './wire_protocol/responses';
import { getReadPreference, isSharded } from './wire_protocol/shared';
import { DeserializeOptions } from 'bson';

/** @internal */
export interface CommandOptions extends BSONSerializeOptions {
Expand Down Expand Up @@ -487,7 +488,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {

// If `documentsReturnedIn` not set or raw is not enabled, use input bson options
// Otherwise, support raw flag. Raw only works for cursors that hardcode firstBatch/nextBatch fields
const bsonOptions =
const bsonOptions: DeserializeOptions =
options.documentsReturnedIn == null || !options.raw
? options
: {
Expand Down
8 changes: 5 additions & 3 deletions src/cmap/wire_protocol/on_demand/document.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { DeserializeOptions } from 'bson';
import {
Binary,
BSON,
type BSONElement,
BSONError,
type BSONSerializeOptions,
BSONType,
deserialize,
getBigInt64LE,
getFloat64LE,
getInt32LE,
ObjectId,
parseToElementsToArray,
pluckBSONSerializeOptions,
Timestamp,
toUTF8
} from '../../../bson';
Expand Down Expand Up @@ -329,8 +331,8 @@ export class OnDemandDocument {
* Deserialize this object, DOES NOT cache result so avoid multiple invocations
* @param options - BSON deserialization options
*/
public toObject(options?: BSONSerializeOptions): Record<string, any> {
return BSON.deserialize(this.bson, {
public toObject(options?: DeserializeOptions & { enableUtf8Validation?: never }): Record<string, any> {
return deserialize(this.bson, {
...options,
index: this.offset,
allowObjectSmallerThanBufferSize: true
Expand Down
24 changes: 3 additions & 21 deletions src/cmap/wire_protocol/responses.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DeserializeOptions } from 'bson';
import {
type BSONElement,
type BSONSerializeOptions,
BSONType,
type Document,
Long,
parseToElementsToArray,
pluckBSONSerializeOptions,
type Timestamp
} from '../../bson';
import { MongoUnexpectedServerResponseError } from '../../error';
Expand Down Expand Up @@ -166,24 +166,6 @@ export class MongoDBResponse extends OnDemandDocument {
}
return this.clusterTime ?? null;
}

public override toObject(options?: BSONSerializeOptions): Record<string, any> {
const exactBSONOptions = {
...pluckBSONSerializeOptions(options ?? {}),
validation: this.parseBsonSerializationOptions(options)
};
return super.toObject(exactBSONOptions);
}

private parseBsonSerializationOptions(options?: { enableUtf8Validation?: boolean }): {
utf8: { writeErrors: false } | false;
} {
const enableUtf8Validation = options?.enableUtf8Validation;
if (enableUtf8Validation === false) {
return { utf8: false };
}
return { utf8: { writeErrors: false } };
}
}

/** @internal */
Expand Down Expand Up @@ -272,7 +254,7 @@ export class CursorResponse extends MongoDBResponse {
);
}

public shift(options?: BSONSerializeOptions): any {
public shift(options?: DeserializeOptions & { __tag: 'shift options' }): any {
if (this.iterated >= this.batchSize) {
return null;
}
Expand Down Expand Up @@ -324,7 +306,7 @@ export class ExplainedCursorResponse extends CursorResponse {
return this._length;
}

override shift(options?: BSONSerializeOptions | undefined) {
override shift(options?: DeserializeOptions) {
if (this._length === 0) return null;
this._length -= 1;
return this.toObject(options);
Expand Down
19 changes: 14 additions & 5 deletions src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Readable, Transform } from 'stream';

import { type BSONSerializeOptions, type Document, Long, pluckBSONSerializeOptions } from '../bson';
import { type BSONSerializeOptions, type Document, Long, parseUtf8ValidationOption, pluckBSONSerializeOptions } from '../bson';
import { type CursorResponse } from '../cmap/wire_protocol/responses';
import {
MongoAPIError,
Expand All @@ -21,6 +21,7 @@ import { type AsyncDisposable, configureResourceManagement } from '../resource_m
import type { Server } from '../sdam/server';
import { ClientSession, maybeClearPinnedConnection } from '../sessions';
import { type MongoDBNamespace, squashError } from '../utils';
import { DeserializeOptions } from 'bson';

/**
* @internal
Expand Down Expand Up @@ -157,6 +158,8 @@ export abstract class AbstractCursor<
/** @event */
static readonly CLOSE = 'close' as const;

protected deserializationOptions: DeserializeOptions & { __tag: 'shift options' };

/** @internal */
protected constructor(
client: MongoClient,
Expand Down Expand Up @@ -211,6 +214,12 @@ export abstract class AbstractCursor<
} else {
this.cursorSession = this.cursorClient.startSession({ owner: this, explicit: false });
}

this.deserializationOptions = {
...this.cursorOptions,
validation: parseUtf8ValidationOption(this.cursorOptions),
__tag: 'shift options'
}
}

/**
Expand Down Expand Up @@ -304,7 +313,7 @@ export abstract class AbstractCursor<
);

for (let count = 0; count < documentsToRead; count++) {
const document = this.documents?.shift(this.cursorOptions);
const document = this.documents?.shift(this.deserializationOptions);
if (document != null) {
bufferedDocs.push(document);
}
Expand Down Expand Up @@ -406,7 +415,7 @@ export abstract class AbstractCursor<
}

do {
const doc = this.documents?.shift(this.cursorOptions);
const doc = this.documents?.shift(this.deserializationOptions);
if (doc != null) {
if (this.transform != null) return await this.transformDocument(doc);
return doc;
Expand All @@ -425,15 +434,15 @@ export abstract class AbstractCursor<
throw new MongoCursorExhaustedError();
}

let doc = this.documents?.shift(this.cursorOptions);
let doc = this.documents?.shift(this.deserializationOptions);
if (doc != null) {
if (this.transform != null) return await this.transformDocument(doc);
return doc;
}

await this.fetchBatch();

doc = this.documents?.shift(this.cursorOptions);
doc = this.documents?.shift(this.deserializationOptions);
if (doc != null) {
if (this.transform != null) return await this.transformDocument(doc);
return doc;
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/aggregation_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class AggregationCursor<TSchema = any> extends AbstractCursor<TSchema> {
explain: verbosity ?? true
})
)
).shift(this.aggregateOptions);
).shift(this.deserializationOptions);
}

/** Add a stage to the aggregation pipeline
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class FindCursor<TSchema = any> extends AbstractCursor<TSchema> {
explain: verbosity ?? true
})
)
).shift(this.findOptions);
).shift(this.deserializationOptions);
}

/** Set the cursor query */
Expand Down
Loading

0 comments on commit b3c6494

Please sign in to comment.