Skip to content

Commit ed4ba58

Browse files
authored
fix(NODE-4232): stream() also returns generic AsyncIterable
1 parent 8c16374 commit ed4ba58

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

src/change_stream.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ export class ChangeStream<
385385
/** @internal */
386386
[kResumeQueue]: Denque<Callback<ChangeStreamCursor<TSchema, TChange>>>;
387387
/** @internal */
388-
[kCursorStream]?: Readable;
388+
[kCursorStream]?: Readable & AsyncIterable<TChange>;
389389
/** @internal */
390390
[kClosed]: boolean;
391391
/** @internal */
@@ -473,7 +473,7 @@ export class ChangeStream<
473473
}
474474

475475
/** @internal */
476-
get cursorStream(): Readable | undefined {
476+
get cursorStream(): (Readable & AsyncIterable<TChange>) | undefined {
477477
return this[kCursorStream];
478478
}
479479

@@ -542,7 +542,7 @@ export class ChangeStream<
542542
* Return a modified Readable stream including a possible transform method.
543543
* @throws MongoDriverError if this.cursor is undefined
544544
*/
545-
stream(options?: CursorStreamOptions): Readable {
545+
stream(options?: CursorStreamOptions): Readable & AsyncIterable<TChange> {
546546
this.streamOptions = options;
547547
if (!this.cursor) throw new MongoChangeStreamError(NO_CURSOR_ERROR);
548548
return this.cursor.stream(options);

src/cursor/abstract_cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export abstract class AbstractCursor<
258258
};
259259
}
260260

261-
stream(options?: CursorStreamOptions): Readable {
261+
stream(options?: CursorStreamOptions): Readable & AsyncIterable<TSchema> {
262262
if (options?.transform) {
263263
const transform = options.transform;
264264
const readable = makeCursorStream(this);

test/types/community/cursor.test-d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ const cursor = collection
3030
.sort({})
3131
.map(result => ({ foo: result.age }));
3232

33+
const cursorStream = cursor.stream();
3334
expectType<FindCursor<{ foo: number }>>(cursor);
34-
expectType<Readable>(cursor.stream());
35+
expectType<Readable & AsyncIterable<{ foo: number }>>(cursorStream);
3536
expectType<FindCursor<Document>>(cursor.project({}));
37+
(async () => {
38+
for await (const doc of cursorStream) {
39+
expectType<{ foo: number }>(doc);
40+
}
41+
})();
3642

3743
collection.find().project({});
3844
collection.find().project({ notExistingField: 1 });

0 commit comments

Comments
 (0)