Cleanup
This commit is contained in:
116
node_modules/mongodb/lib/operations/execute_operation.js
generated
vendored
116
node_modules/mongodb/lib/operations/execute_operation.js
generated
vendored
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const maybePromise = require('../utils').maybePromise;
|
||||
const MongoError = require('../core/error').MongoError;
|
||||
const Aspect = require('./operation').Aspect;
|
||||
const OperationBase = require('./operation').OperationBase;
|
||||
@@ -21,7 +22,7 @@ const isUnifiedTopology = require('../core/utils').isUnifiedTopology;
|
||||
* @param {Operation} operation The operation to execute
|
||||
* @param {function} callback The command result callback
|
||||
*/
|
||||
function executeOperation(topology, operation, callback) {
|
||||
function executeOperation(topology, operation, cb) {
|
||||
if (topology == null) {
|
||||
throw new TypeError('This method requires a valid topology instance');
|
||||
}
|
||||
@@ -30,64 +31,57 @@ function executeOperation(topology, operation, callback) {
|
||||
throw new TypeError('This method requires a valid operation instance');
|
||||
}
|
||||
|
||||
if (isUnifiedTopology(topology) && topology.shouldCheckForSessionSupport()) {
|
||||
return selectServerForSessionSupport(topology, operation, callback);
|
||||
}
|
||||
|
||||
const Promise = topology.s.promiseLibrary;
|
||||
|
||||
// The driver sessions spec mandates that we implicitly create sessions for operations
|
||||
// that are not explicitly provided with a session.
|
||||
let session, owner;
|
||||
if (topology.hasSessionSupport()) {
|
||||
if (operation.session == null) {
|
||||
owner = Symbol();
|
||||
session = topology.startSession({ owner });
|
||||
operation.session = session;
|
||||
} else if (operation.session.hasEnded) {
|
||||
throw new MongoError('Use of expired sessions is not permitted');
|
||||
return maybePromise(topology, cb, callback => {
|
||||
if (isUnifiedTopology(topology) && topology.shouldCheckForSessionSupport()) {
|
||||
// Recursive call to executeOperation after a server selection
|
||||
return selectServerForSessionSupport(topology, operation, callback);
|
||||
}
|
||||
}
|
||||
|
||||
let result;
|
||||
if (typeof callback !== 'function') {
|
||||
result = new Promise((resolve, reject) => {
|
||||
callback = (err, res) => {
|
||||
if (err) return reject(err);
|
||||
resolve(res);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function executeCallback(err, result) {
|
||||
if (session && session.owner === owner) {
|
||||
session.endSession();
|
||||
if (operation.session === session) {
|
||||
operation.clearSession();
|
||||
// The driver sessions spec mandates that we implicitly create sessions for operations
|
||||
// that are not explicitly provided with a session.
|
||||
let session, owner;
|
||||
if (topology.hasSessionSupport()) {
|
||||
if (operation.session == null) {
|
||||
owner = Symbol();
|
||||
session = topology.startSession({ owner });
|
||||
operation.session = session;
|
||||
} else if (operation.session.hasEnded) {
|
||||
return callback(new MongoError('Use of expired sessions is not permitted'));
|
||||
}
|
||||
} else if (operation.session) {
|
||||
// If the user passed an explicit session and we are still, after server selection,
|
||||
// trying to run against a topology that doesn't support sessions we error out.
|
||||
return callback(new MongoError('Current topology does not support sessions'));
|
||||
}
|
||||
|
||||
callback(err, result);
|
||||
}
|
||||
|
||||
try {
|
||||
if (operation.hasAspect(Aspect.EXECUTE_WITH_SELECTION)) {
|
||||
executeWithServerSelection(topology, operation, executeCallback);
|
||||
} else {
|
||||
operation.execute(executeCallback);
|
||||
}
|
||||
} catch (e) {
|
||||
if (session && session.owner === owner) {
|
||||
session.endSession();
|
||||
if (operation.session === session) {
|
||||
operation.clearSession();
|
||||
function executeCallback(err, result) {
|
||||
if (session && session.owner === owner) {
|
||||
session.endSession();
|
||||
if (operation.session === session) {
|
||||
operation.clearSession();
|
||||
}
|
||||
}
|
||||
|
||||
callback(err, result);
|
||||
}
|
||||
|
||||
throw e;
|
||||
}
|
||||
try {
|
||||
if (operation.hasAspect(Aspect.EXECUTE_WITH_SELECTION)) {
|
||||
executeWithServerSelection(topology, operation, executeCallback);
|
||||
} else {
|
||||
operation.execute(executeCallback);
|
||||
}
|
||||
} catch (error) {
|
||||
if (session && session.owner === owner) {
|
||||
session.endSession();
|
||||
if (operation.session === session) {
|
||||
operation.clearSession();
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
callback(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function supportsRetryableReads(server) {
|
||||
@@ -139,7 +133,6 @@ function executeWithServerSelection(topology, operation, callback) {
|
||||
callback(err, null);
|
||||
return;
|
||||
}
|
||||
|
||||
const shouldRetryReads =
|
||||
topology.s.options.retryReads !== false &&
|
||||
operation.session &&
|
||||
@@ -156,31 +149,16 @@ function executeWithServerSelection(topology, operation, callback) {
|
||||
});
|
||||
}
|
||||
|
||||
// TODO: This is only supported for unified topology, it should go away once
|
||||
// we remove support for legacy topology types.
|
||||
// The Unified Topology runs serverSelection before executing every operation
|
||||
// Session support is determined by the result of a monitoring check triggered by this selection
|
||||
function selectServerForSessionSupport(topology, operation, callback) {
|
||||
const Promise = topology.s.promiseLibrary;
|
||||
|
||||
let result;
|
||||
if (typeof callback !== 'function') {
|
||||
result = new Promise((resolve, reject) => {
|
||||
callback = (err, result) => {
|
||||
if (err) return reject(err);
|
||||
resolve(result);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
topology.selectServer(ReadPreference.primaryPreferred, err => {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
executeOperation(topology, operation, callback);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = executeOperation;
|
||||
|
||||
Reference in New Issue
Block a user