Skip to content

Commit

Permalink
[gh-10902 v5] Add node major version to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
iovanom committed Oct 29, 2021
1 parent 5468642 commit 22e9b3b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/cursor/AggregationCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const promiseOrCallback = require('../helpers/promiseOrCallback');
const eachAsync = require('../helpers/cursor/eachAsync');
const immediate = require('../helpers/immediate');
const util = require('util');
const utils = require('../../lib/utils');

/**
* An AggregationCursor is a concurrency primitive for processing aggregation
Expand Down Expand Up @@ -38,7 +39,7 @@ const util = require('util');
function AggregationCursor(agg) {
const streamOpts = { objectMode: true };
// for node < 12 we will emit 'close' event after 'end'
if (parseInt(process.versions.node.split('.')[0]) >= 12) {
if (utils.nodeMajorVersion >= 12) {
// set autoDestroy=true because on node 12 it's by default false
// gh-10902 need autoDestroy to destroy correctly and emit 'close' event for node >= 12
streamOpts.autoDestroy = true;
Expand Down Expand Up @@ -94,7 +95,7 @@ AggregationCursor.prototype._read = function() {
return _this.emit('error', error);
}
// for node >= 12 the autoDestroy will emit the 'close' event
if (parseInt(process.versions.node.split('.')[0]) < 12) {
if (utils.nodeMajorVersion < 12) {
_this.on('end', () => _this.emit('close'));
}
});
Expand Down
5 changes: 3 additions & 2 deletions lib/cursor/QueryCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const eachAsync = require('../helpers/cursor/eachAsync');
const helpers = require('../queryhelpers');
const immediate = require('../helpers/immediate');
const util = require('util');
const utils = require('../../lib/utils');

/**
* A QueryCursor is a concurrency primitive for processing query results
Expand All @@ -36,7 +37,7 @@ const util = require('util');
function QueryCursor(query, options) {
const streamOpts = { objectMode: true };
// for node < 12 we will emit 'close' event after 'end'
if (parseInt(process.versions.node.split('.')[0]) >= 12) {
if (utils.nodeMajorVersion >= 12) {
// set autoDestroy=true because on node 12 it's by default false
// gh-10902 need autoDestroy to destroy correctly and emit 'close' event for node >= 12
streamOpts.autoDestroy = true;
Expand Down Expand Up @@ -103,7 +104,7 @@ QueryCursor.prototype._read = function() {
return _this.emit('error', error);
}
// for node >= 12 the autoDestroy will emit the 'close' event
if (parseInt(process.versions.node.split('.')[0]) < 12) {
if (utils.nodeMajorVersion < 12) {
_this.on('end', () => _this.emit('close'));
}
});
Expand Down
4 changes: 3 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,4 +934,6 @@ exports.errorToPOJO = function errorToPOJO(error) {
ret[properyName] = error[properyName];
}
return ret;
};
};

exports.nodeMajorVersion = parseInt(process.versions.node.split('.')[0]);

0 comments on commit 22e9b3b

Please sign in to comment.