Skip to content

Commit ca88168

Browse files
committed
fix: batch always exists and emptyGetMore made simpler
1 parent 2f10f0e commit ca88168

File tree

1 file changed

+5
-19
lines changed

1 file changed

+5
-19
lines changed

src/cmap/wire_protocol/responses.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,13 @@ export class MongoDBResponse extends OnDemandDocument {
170170
/** @internal */
171171
export class CursorResponse extends MongoDBResponse {
172172
/**
173-
* This is a BSON document containing the following:
174-
* ```
175-
* { ok: 1, cursor: { id: 0n, nextBatch: new Array(0) } }
176-
* ```
177-
* This is used when the client side findCursor is closed by tracking the number returned and limit
178-
* to avoid an extra round trip. It provides a cursor response that the server _would_ return _if_
179-
* that round trip were to be made.
173+
* This supports a feature of the FindCursor.
174+
* It is an optimization to avoid an extra getMore when the limit has been reached
180175
*/
181-
static emptyGetMore = new CursorResponse(
182-
Buffer.from(
183-
'NgAAABBvawABAAAAA2N1cnNvcgAhAAAAEmlkAAAAAAAAAAAABG5leHRCYXRjaAAFAAAAAAAA',
184-
'base64'
185-
)
186-
);
176+
static emptyGetMore = { id: new Long(0), length: 0, shift: () => null };
187177

188178
static override is(value: unknown): value is CursorResponse {
189-
return value instanceof CursorResponse;
179+
return value instanceof CursorResponse || value === CursorResponse.emptyGetMore;
190180
}
191181

192182
public id: Long;
@@ -223,13 +213,9 @@ export class CursorResponse extends MongoDBResponse {
223213
return null;
224214
}
225215

226-
const result = this.batch?.get(this.iterated, BSONType.object, true) ?? null;
216+
const result = this.batch.get(this.iterated, BSONType.object, true) ?? null;
227217
this.iterated += 1;
228218

229-
if (result == null) {
230-
throw new MongoUnexpectedServerResponseError('Cursor batch contains null values');
231-
}
232-
233219
if (options?.raw) {
234220
return result.toBytes();
235221
} else {

0 commit comments

Comments
 (0)