11import { 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' ;
44import { type CursorResponse } from '../cmap/wire_protocol/responses' ;
55import {
66 MongoAPIError ,
@@ -21,6 +21,7 @@ import { type AsyncDisposable, configureResourceManagement } from '../resource_m
2121import type { Server } from '../sdam/server' ;
2222import { ClientSession , maybeClearPinnedConnection } from '../sessions' ;
2323import { 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 ;
0 commit comments