File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -238,6 +238,27 @@ QueryCursor.prototype.close = async function close() {
238238 }
239239} ;
240240
241+ /**
242+ * Marks this cursor as destroyed. Will stop streaming and subsequent calls to
243+ * `next()` will error.
244+ *
245+ * @return {this }
246+ * @api private
247+ * @method _destroy
248+ */
249+
250+ QueryCursor . prototype . _destroy = function _destroy ( _err , callback ) {
251+ this . cursor . close ( )
252+ . then ( ( ) => {
253+ this . _closed = false ;
254+ callback ( ) ;
255+ } )
256+ . catch ( error => {
257+ callback ( error ) ;
258+ } ) ;
259+ return this ;
260+ } ;
261+
241262/**
242263 * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will
243264 * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even
Original file line number Diff line number Diff line change 44
55'use strict' ;
66
7+ const { once } = require ( 'events' ) ;
78const start = require ( './common' ) ;
89
910const assert = require ( 'assert' ) ;
@@ -920,6 +921,21 @@ describe('QueryCursor', function() {
920921 assert . ok ( cursor . cursor ) ;
921922 assert . equal ( driverCursor , cursor . cursor ) ;
922923 } ) ;
924+
925+ it ( 'handles destroy() (gh-14966)' , async function ( ) {
926+ db . deleteModel ( / T e s t / ) ;
927+ const TestModel = db . model ( 'Test' , mongoose . Schema ( { name : String } ) ) ;
928+
929+ const stream = await TestModel . find ( ) . cursor ( ) ;
930+ await once ( stream , 'cursor' ) ;
931+ assert . ok ( ! stream . cursor . closed ) ;
932+
933+ stream . destroy ( ) ;
934+
935+ await once ( stream . cursor , 'close' ) ;
936+ assert . ok ( stream . destroyed ) ;
937+ assert . ok ( stream . cursor . closed ) ;
938+ } ) ;
923939} ) ;
924940
925941async function delay ( ms ) {
Original file line number Diff line number Diff line change @@ -26,6 +26,12 @@ declare module 'mongoose' {
2626 */
2727 close ( ) : Promise < void > ;
2828
29+ /**
30+ * Destroy this cursor, closing the underlying cursor. Will stop streaming
31+ * and subsequent calls to `next()` will error.
32+ */
33+ destroy ( ) : this;
34+
2935 /**
3036 * Rewind this cursor to its uninitialized state. Any options that are present on the cursor will
3137 * remain in effect. Iterating this cursor will cause new queries to be sent to the server, even
You can’t perform that action at this time.
0 commit comments