@@ -13,15 +13,15 @@ import {
1313 MongoTailableCursorError
1414} from '../error' ;
1515import type { MongoClient } from '../mongo_client' ;
16- import { type TODO_NODE_3286 , TypedEventEmitter } from '../mongo_types' ;
16+ import { TypedEventEmitter } from '../mongo_types' ;
1717import { executeOperation , type ExecutionResult } from '../operations/execute_operation' ;
1818import { GetMoreOperation } from '../operations/get_more' ;
1919import { KillCursorsOperation } from '../operations/kill_cursors' ;
2020import { ReadConcern , type ReadConcernLike } from '../read_concern' ;
2121import { ReadPreference , type ReadPreferenceLike } from '../read_preference' ;
2222import type { Server } from '../sdam/server' ;
2323import { ClientSession , maybeClearPinnedConnection } from '../sessions' ;
24- import { List , type MongoDBNamespace , ns , squashError } from '../utils' ;
24+ import { type MongoDBNamespace , squashError } from '../utils' ;
2525
2626/** @internal */
2727const kId = Symbol ( 'id' ) ;
@@ -145,13 +145,7 @@ export abstract class AbstractCursor<
145145 /** @internal */
146146 [ kNamespace ] : MongoDBNamespace ;
147147 /** @internal */
148- [ kDocuments ] : {
149- length : number ;
150- shift ( bsonOptions ?: any ) : TSchema | null ;
151- clear ( ) : void ;
152- pushMany ( many : Iterable < TSchema > ) : void ;
153- push ( item : TSchema ) : void ;
154- } ;
148+ [ kDocuments ] : CursorResponse = { length : 0 } as unknown as CursorResponse ;
155149 /** @internal */
156150 [ kClient ] : MongoClient ;
157151 /** @internal */
@@ -182,7 +176,6 @@ export abstract class AbstractCursor<
182176 this [ kClient ] = client ;
183177 this [ kNamespace ] = namespace ;
184178 this [ kId ] = null ;
185- this [ kDocuments ] = new List ( ) ;
186179 this [ kInitialized ] = false ;
187180 this [ kClosed ] = false ;
188181 this [ kKilled ] = false ;
@@ -637,13 +630,12 @@ export abstract class AbstractCursor<
637630 protected abstract _initialize ( session : ClientSession | undefined ) : Promise < ExecutionResult > ;
638631
639632 /** @internal */
640- async getMore ( batchSize : number , useCursorResponse = false ) : Promise < Document | null > {
633+ async getMore ( batchSize : number ) : Promise < CursorResponse > {
641634 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
642635 const getMoreOperation = new GetMoreOperation ( this [ kNamespace ] , this [ kId ] ! , this [ kServer ] ! , {
643636 ...this [ kOptions ] ,
644637 session : this [ kSession ] ,
645- batchSize,
646- useCursorResponse
638+ batchSize
647639 } ) ;
648640
649641 return await executeOperation ( this [ kClient ] , getMoreOperation ) ;
@@ -661,37 +653,13 @@ export abstract class AbstractCursor<
661653 const state = await this . _initialize ( this [ kSession ] ) ;
662654 const response = state . response ;
663655 this [ kServer ] = state . server ;
664- if ( CursorResponse . is ( response ) ) {
665- this [ kId ] = response . id ;
666- if ( response . ns ) this [ kNamespace ] = response . ns ;
667- this [ kDocuments ] = response ;
668- } else if ( response . cursor ) {
669- // TODO(NODE-2674): Preserve int64 sent from MongoDB
670- this [ kId ] =
671- typeof response . cursor . id === 'number'
672- ? Long . fromNumber ( response . cursor . id )
673- : typeof response . cursor . id === 'bigint'
674- ? Long . fromBigInt ( response . cursor . id )
675- : response . cursor . id ;
676-
677- if ( response . cursor . ns ) {
678- this [ kNamespace ] = ns ( response . cursor . ns ) ;
679- }
680-
681- this [ kDocuments ] . pushMany ( response . cursor . firstBatch ) ;
682- }
683656
684- // When server responses return without a cursor document, we close this cursor
685- // and return the raw server response. This is often the case for explain commands
686- // for example
687- if ( this [ kId ] == null ) {
688- this [ kId ] = Long . ZERO ;
689- // TODO(NODE-3286): ExecutionResult needs to accept a generic parameter
690- this [ kDocuments ] . push ( state . response as TODO_NODE_3286 ) ;
691- }
657+ if ( ! CursorResponse . is ( response ) ) throw new Error ( 'ah' ) ;
692658
693- // the cursor is now initialized, even if it is dead
694- this [ kInitialized ] = true ;
659+ this [ kId ] = response . id ;
660+ this [ kNamespace ] = response . ns ?? this [ kNamespace ] ;
661+ this [ kDocuments ] = response ;
662+ this [ kInitialized ] = true ; // the cursor is now initialized, even if it is dead
695663 } catch ( error ) {
696664 // the cursor is now initialized, even if an error occurred
697665 this [ kInitialized ] = true ;
@@ -802,20 +770,8 @@ async function next<T>(
802770
803771 try {
804772 const response = await cursor . getMore ( batchSize ) ;
805- if ( CursorResponse . is ( response ) ) {
806- cursor [ kId ] = response . id ;
807- cursor [ kDocuments ] = response ;
808- } else if ( response ) {
809- const cursorId =
810- typeof response . cursor . id === 'number'
811- ? Long . fromNumber ( response . cursor . id )
812- : typeof response . cursor . id === 'bigint'
813- ? Long . fromBigInt ( response . cursor . id )
814- : response . cursor . id ;
815-
816- cursor [ kDocuments ] . pushMany ( response . cursor . nextBatch ) ;
817- cursor [ kId ] = cursorId ;
818- }
773+ cursor [ kId ] = response . id ;
774+ cursor [ kDocuments ] = response ;
819775 } catch ( error ) {
820776 try {
821777 await cleanupCursor ( cursor , { error, needsToEmitClosed : true } ) ;
0 commit comments