@@ -13,15 +13,15 @@ import {
13
13
MongoTailableCursorError
14
14
} from '../error' ;
15
15
import type { MongoClient } from '../mongo_client' ;
16
- import { type TODO_NODE_3286 , TypedEventEmitter } from '../mongo_types' ;
16
+ import { TypedEventEmitter } from '../mongo_types' ;
17
17
import { executeOperation , type ExecutionResult } from '../operations/execute_operation' ;
18
18
import { GetMoreOperation } from '../operations/get_more' ;
19
19
import { KillCursorsOperation } from '../operations/kill_cursors' ;
20
20
import { ReadConcern , type ReadConcernLike } from '../read_concern' ;
21
21
import { ReadPreference , type ReadPreferenceLike } from '../read_preference' ;
22
22
import type { Server } from '../sdam/server' ;
23
23
import { ClientSession , maybeClearPinnedConnection } from '../sessions' ;
24
- import { List , type MongoDBNamespace , ns , squashError } from '../utils' ;
24
+ import { type MongoDBNamespace , squashError } from '../utils' ;
25
25
26
26
/** @internal */
27
27
const kId = Symbol ( 'id' ) ;
@@ -145,13 +145,7 @@ export abstract class AbstractCursor<
145
145
/** @internal */
146
146
[ kNamespace ] : MongoDBNamespace ;
147
147
/** @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 ;
155
149
/** @internal */
156
150
[ kClient ] : MongoClient ;
157
151
/** @internal */
@@ -182,7 +176,6 @@ export abstract class AbstractCursor<
182
176
this [ kClient ] = client ;
183
177
this [ kNamespace ] = namespace ;
184
178
this [ kId ] = null ;
185
- this [ kDocuments ] = new List ( ) ;
186
179
this [ kInitialized ] = false ;
187
180
this [ kClosed ] = false ;
188
181
this [ kKilled ] = false ;
@@ -637,13 +630,12 @@ export abstract class AbstractCursor<
637
630
protected abstract _initialize ( session : ClientSession | undefined ) : Promise < ExecutionResult > ;
638
631
639
632
/** @internal */
640
- async getMore ( batchSize : number , useCursorResponse = false ) : Promise < Document | null > {
633
+ async getMore ( batchSize : number ) : Promise < CursorResponse > {
641
634
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
642
635
const getMoreOperation = new GetMoreOperation ( this [ kNamespace ] , this [ kId ] ! , this [ kServer ] ! , {
643
636
...this [ kOptions ] ,
644
637
session : this [ kSession ] ,
645
- batchSize,
646
- useCursorResponse
638
+ batchSize
647
639
} ) ;
648
640
649
641
return await executeOperation ( this [ kClient ] , getMoreOperation ) ;
@@ -661,37 +653,13 @@ export abstract class AbstractCursor<
661
653
const state = await this . _initialize ( this [ kSession ] ) ;
662
654
const response = state . response ;
663
655
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
- }
683
656
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' ) ;
692
658
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
695
663
} catch ( error ) {
696
664
// the cursor is now initialized, even if an error occurred
697
665
this [ kInitialized ] = true ;
@@ -802,20 +770,8 @@ async function next<T>(
802
770
803
771
try {
804
772
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 ;
819
775
} catch ( error ) {
820
776
try {
821
777
await cleanupCursor ( cursor , { error, needsToEmitClosed : true } ) ;
0 commit comments