Skip to content

Commit

Permalink
fix(NODE-4232): stream() also returns generic AsyncIterable
Browse files Browse the repository at this point in the history
  • Loading branch information
qwelias committed May 10, 2022
1 parent 8c16374 commit ed4ba58
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/change_stream.ts
Expand Up @@ -385,7 +385,7 @@ export class ChangeStream<
/** @internal */
[kResumeQueue]: Denque<Callback<ChangeStreamCursor<TSchema, TChange>>>;
/** @internal */
[kCursorStream]?: Readable;
[kCursorStream]?: Readable & AsyncIterable<TChange>;
/** @internal */
[kClosed]: boolean;
/** @internal */
Expand Down Expand Up @@ -473,7 +473,7 @@ export class ChangeStream<
}

/** @internal */
get cursorStream(): Readable | undefined {
get cursorStream(): (Readable & AsyncIterable<TChange>) | undefined {
return this[kCursorStream];
}

Expand Down Expand Up @@ -542,7 +542,7 @@ export class ChangeStream<
* Return a modified Readable stream including a possible transform method.
* @throws MongoDriverError if this.cursor is undefined
*/
stream(options?: CursorStreamOptions): Readable {
stream(options?: CursorStreamOptions): Readable & AsyncIterable<TChange> {
this.streamOptions = options;
if (!this.cursor) throw new MongoChangeStreamError(NO_CURSOR_ERROR);
return this.cursor.stream(options);
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/abstract_cursor.ts
Expand Up @@ -258,7 +258,7 @@ export abstract class AbstractCursor<
};
}

stream(options?: CursorStreamOptions): Readable {
stream(options?: CursorStreamOptions): Readable & AsyncIterable<TSchema> {
if (options?.transform) {
const transform = options.transform;
const readable = makeCursorStream(this);
Expand Down
8 changes: 7 additions & 1 deletion test/types/community/cursor.test-d.ts
Expand Up @@ -30,9 +30,15 @@ const cursor = collection
.sort({})
.map(result => ({ foo: result.age }));

const cursorStream = cursor.stream();
expectType<FindCursor<{ foo: number }>>(cursor);
expectType<Readable>(cursor.stream());
expectType<Readable & AsyncIterable<{ foo: number }>>(cursorStream);
expectType<FindCursor<Document>>(cursor.project({}));
(async () => {
for await (const doc of cursorStream) {
expectType<{ foo: number }>(doc);
}
})();

collection.find().project({});
collection.find().project({ notExistingField: 1 });
Expand Down

0 comments on commit ed4ba58

Please sign in to comment.