1+ import { type DeserializeOptions } from 'bson' ;
12import { Readable , Transform } from 'stream' ;
23
3- import { type BSONSerializeOptions , type Document , Long , pluckBSONSerializeOptions } from '../bson' ;
4+ import {
5+ type BSONSerializeOptions ,
6+ type Document ,
7+ Long ,
8+ parseUtf8ValidationOption ,
9+ pluckBSONSerializeOptions
10+ } from '../bson' ;
11+ import { type OnDemandDocumentDeserializeOptions } from '../cmap/wire_protocol/on_demand/document' ;
412import { type CursorResponse } from '../cmap/wire_protocol/responses' ;
513import {
614 MongoAPIError ,
@@ -153,6 +161,9 @@ export abstract class AbstractCursor<
153161 /** @event */
154162 static readonly CLOSE = 'close' as const ;
155163
164+ /** @internal */
165+ protected deserializationOptions : OnDemandDocumentDeserializeOptions ;
166+
156167 /** @internal */
157168 protected constructor (
158169 client : MongoClient ,
@@ -207,6 +218,13 @@ export abstract class AbstractCursor<
207218 } else {
208219 this . cursorSession = this . cursorClient . startSession ( { owner : this , explicit : false } ) ;
209220 }
221+
222+ this . deserializationOptions = {
223+ ...this . cursorOptions ,
224+ validation : {
225+ utf8 : options ?. enableUtf8Validation === false ? false : true
226+ }
227+ } ;
210228 }
211229
212230 /**
@@ -289,7 +307,7 @@ export abstract class AbstractCursor<
289307 ) ;
290308
291309 for ( let count = 0 ; count < documentsToRead ; count ++ ) {
292- const document = this . documents ?. shift ( this . cursorOptions ) ;
310+ const document = this . documents ?. shift ( this . deserializationOptions ) ;
293311 if ( document != null ) {
294312 bufferedDocs . push ( document ) ;
295313 }
@@ -390,7 +408,7 @@ export abstract class AbstractCursor<
390408 }
391409
392410 do {
393- const doc = this . documents ?. shift ( this . cursorOptions ) ;
411+ const doc = this . documents ?. shift ( this . deserializationOptions ) ;
394412 if ( doc != null ) {
395413 if ( this . transform != null ) return await this . transformDocument ( doc ) ;
396414 return doc ;
@@ -409,15 +427,15 @@ export abstract class AbstractCursor<
409427 throw new MongoCursorExhaustedError ( ) ;
410428 }
411429
412- let doc = this . documents ?. shift ( this . cursorOptions ) ;
430+ let doc = this . documents ?. shift ( this . deserializationOptions ) ;
413431 if ( doc != null ) {
414432 if ( this . transform != null ) return await this . transformDocument ( doc ) ;
415433 return doc ;
416434 }
417435
418436 await this . fetchBatch ( ) ;
419437
420- doc = this . documents ?. shift ( this . cursorOptions ) ;
438+ doc = this . documents ?. shift ( this . deserializationOptions ) ;
421439 if ( doc != null ) {
422440 if ( this . transform != null ) return await this . transformDocument ( doc ) ;
423441 return doc ;
0 commit comments