mongoose 9.11.4 → 9.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@
7
7
  const MongooseError = require('../error/mongooseError');
8
8
  const Readable = require('stream').Readable;
9
9
  const eachAsync = require('../helpers/cursor/eachAsync');
10
+ const waitForInitialization = require('../helpers/cursor/waitForInitialization');
10
11
  const immediate = require('../helpers/immediate');
11
12
  const kareem = require('kareem');
12
13
  const util = require('util');
@@ -42,6 +43,7 @@ function AggregationCursor(agg) {
42
43
  Readable.call(this, { autoDestroy: true, objectMode: true });
43
44
 
44
45
  this.cursor = null;
46
+ this._initializationComplete = false;
45
47
  this.agg = agg;
46
48
  this._transforms = [];
47
49
  const connection = agg._connection;
@@ -58,6 +60,7 @@ function AggregationCursor(agg) {
58
60
  options = driverOptions;
59
61
  }
60
62
  this.cursor = connection.db.aggregate(agg._pipeline, options);
63
+ this._initializationComplete = true;
61
64
  setImmediate(() => this.emit('cursor', this.cursor));
62
65
  } else {
63
66
  _init(model, this, agg);
@@ -108,8 +111,10 @@ function _getRawCursor(model, aggregationCursor, agg) {
108
111
  }
109
112
  const cursor = model.collection.aggregate(agg._pipeline, options);
110
113
  aggregationCursor.cursor = cursor;
114
+ aggregationCursor._initializationComplete = true;
111
115
  aggregationCursor.emit('cursor', cursor);
112
116
  } catch (err) {
117
+ aggregationCursor._initializationComplete = true;
113
118
  aggregationCursor._markError(err);
114
119
  aggregationCursor.listeners('error').length > 0 && aggregationCursor.emit('error', aggregationCursor._error);
115
120
  }
@@ -126,6 +131,7 @@ function _getRawCursor(model, aggregationCursor, agg) {
126
131
  */
127
132
 
128
133
  function _handlePreHookError(queryCursor, err) {
134
+ queryCursor._initializationComplete = true;
129
135
  if (err instanceof kareem.skipWrappedFunction) {
130
136
  const resultValue = err.args[0];
131
137
  if (resultValue != null && (!Array.isArray(resultValue) || resultValue.length)) {
@@ -275,16 +281,14 @@ AggregationCursor.prototype.close = async function close() {
275
281
 
276
282
  AggregationCursor.prototype._destroy = function _destroy(_err, callback) {
277
283
  let waitForCursor = null;
278
- if (!this.cursor) {
279
- waitForCursor = new Promise((resolve) => {
280
- this.once('cursor', resolve);
281
- });
284
+ if (!this._initializationComplete) {
285
+ waitForCursor = waitForInitialization(this);
282
286
  } else {
283
287
  waitForCursor = Promise.resolve();
284
288
  }
285
289
 
286
290
  waitForCursor
287
- .then(() => this.cursor.close())
291
+ .then(() => this.cursor?.close())
288
292
  .then(() => {
289
293
  this._closed = true;
290
294
  callback();
@@ -7,6 +7,7 @@
7
7
  const MongooseError = require('../error/mongooseError');
8
8
  const Readable = require('stream').Readable;
9
9
  const eachAsync = require('../helpers/cursor/eachAsync');
10
+ const waitForInitialization = require('../helpers/cursor/waitForInitialization');
10
11
  const helpers = require('../queryHelpers');
11
12
  const kareem = require('kareem');
12
13
  const immediate = require('../helpers/immediate');
@@ -44,6 +45,7 @@ function QueryCursor(query) {
44
45
  Readable.call(this, { autoDestroy: true, objectMode: true });
45
46
 
46
47
  this.cursor = null;
48
+ this._initializationComplete = false;
47
49
  this.skipped = false;
48
50
  this.query = query;
49
51
  this._closed = false;
@@ -56,6 +58,7 @@ function QueryCursor(query) {
56
58
 
57
59
  const onPreComplete = (err) => {
58
60
  if (err != null) {
61
+ this._initializationComplete = true;
59
62
  if (err instanceof kareem.skipWrappedFunction) {
60
63
  const resultValue = err.args[0];
61
64
  if (resultValue != null && (!Array.isArray(resultValue) || resultValue.length)) {
@@ -123,8 +126,10 @@ function _getRawCursor(query, queryCursor) {
123
126
  try {
124
127
  const cursor = query.model.collection.find(query._conditions, queryCursor.options);
125
128
  queryCursor.cursor = cursor;
129
+ queryCursor._initializationComplete = true;
126
130
  queryCursor.emit('cursor', cursor);
127
131
  } catch (err) {
132
+ queryCursor._initializationComplete = true;
128
133
  queryCursor._markError(err);
129
134
  queryCursor.listeners('error').length > 0 && queryCursor.emit('error', queryCursor._error);
130
135
  }
@@ -271,17 +276,15 @@ QueryCursor.prototype.close = async function close() {
271
276
 
272
277
  QueryCursor.prototype._destroy = function _destroy(_err, callback) {
273
278
  let waitForCursor = null;
274
- if (!this.cursor) {
275
- waitForCursor = new Promise((resolve) => {
276
- this.once('cursor', resolve);
277
- });
279
+ if (!this._initializationComplete) {
280
+ waitForCursor = waitForInitialization(this);
278
281
  } else {
279
282
  waitForCursor = Promise.resolve();
280
283
  }
281
284
 
282
285
  waitForCursor
283
286
  .then(() => {
284
- return this.cursor.close();
287
+ return this.cursor?.close();
285
288
  })
286
289
  .then(() => {
287
290
  this._closed = true;