Refactoring day1
This commit is contained in:
78
node_modules/mongodb/lib/utils.js
generated
vendored
78
node_modules/mongodb/lib/utils.js
generated
vendored
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
const MongoError = require('./core/error').MongoError;
|
||||
const ReadPreference = require('./core/topologies/read_preference');
|
||||
const WriteConcern = require('./write_concern');
|
||||
|
||||
var shallowClone = function(obj) {
|
||||
@@ -8,6 +9,31 @@ var shallowClone = function(obj) {
|
||||
return copy;
|
||||
};
|
||||
|
||||
// Figure out the read preference
|
||||
var translateReadPreference = function(options) {
|
||||
var r = null;
|
||||
if (options.readPreference) {
|
||||
r = options.readPreference;
|
||||
} else {
|
||||
return options;
|
||||
}
|
||||
|
||||
if (typeof r === 'string') {
|
||||
options.readPreference = new ReadPreference(r);
|
||||
} else if (r && !(r instanceof ReadPreference) && typeof r === 'object') {
|
||||
const mode = r.mode || r.preference;
|
||||
if (mode && typeof mode === 'string') {
|
||||
options.readPreference = new ReadPreference(mode, r.tags, {
|
||||
maxStalenessSeconds: r.maxStalenessSeconds
|
||||
});
|
||||
}
|
||||
} else if (!(r instanceof ReadPreference)) {
|
||||
throw new TypeError('Invalid read preference: ' + r);
|
||||
}
|
||||
|
||||
return options;
|
||||
};
|
||||
|
||||
// Set simple property
|
||||
var getSingleProperty = function(obj, name, value) {
|
||||
Object.defineProperty(obj, name, {
|
||||
@@ -464,6 +490,37 @@ function applyWriteConcern(target, sources, options) {
|
||||
return target;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a read preference based on well-defined inheritance rules. This method will not only
|
||||
* determine the read preference (if there is one), but will also ensure the returned value is a
|
||||
* properly constructed instance of `ReadPreference`.
|
||||
*
|
||||
* @param {Collection|Db|MongoClient} parent The parent of the operation on which to determine the read
|
||||
* preference, used for determining the inherited read preference.
|
||||
* @param {Object} options The options passed into the method, potentially containing a read preference
|
||||
* @returns {(ReadPreference|null)} The resolved read preference
|
||||
*/
|
||||
function resolveReadPreference(parent, options) {
|
||||
options = options || {};
|
||||
const session = options.session;
|
||||
|
||||
const inheritedReadPreference = parent.readPreference;
|
||||
|
||||
let readPreference;
|
||||
if (options.readPreference) {
|
||||
readPreference = ReadPreference.fromOptions(options);
|
||||
} else if (session && session.inTransaction() && session.transaction.options.readPreference) {
|
||||
// The transaction’s read preference MUST override all other user configurable read preferences.
|
||||
readPreference = session.transaction.options.readPreference;
|
||||
} else if (inheritedReadPreference != null) {
|
||||
readPreference = inheritedReadPreference;
|
||||
} else {
|
||||
throw new Error('No readPreference was provided or inherited.');
|
||||
}
|
||||
|
||||
return typeof readPreference === 'string' ? new ReadPreference(readPreference) : readPreference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given value is a Promise
|
||||
*
|
||||
@@ -721,12 +778,6 @@ function makeInterruptableAsyncInterval(fn, options) {
|
||||
const timeUntilNextCall = Math.max(interval - timeSinceLastCall, 0);
|
||||
lastWakeTime = currentTime;
|
||||
|
||||
// For the streaming protocol: there is nothing obviously stopping this
|
||||
// interval from being woken up again while we are waiting "infinitely"
|
||||
// for `fn` to be called again`. Since the function effectively
|
||||
// never completes, the `timeUntilNextCall` will continue to grow
|
||||
// negatively unbounded, so it will never trigger a reschedule here.
|
||||
|
||||
// debounce multiple calls to wake within the `minInterval`
|
||||
if (timeSinceLastWake < minInterval) {
|
||||
return;
|
||||
@@ -759,7 +810,6 @@ function makeInterruptableAsyncInterval(fn, options) {
|
||||
function executeAndReschedule() {
|
||||
lastWakeTime = 0;
|
||||
lastCallTime = now();
|
||||
|
||||
fn(err => {
|
||||
if (err) throw err;
|
||||
reschedule(interval);
|
||||
@@ -776,15 +826,6 @@ function makeInterruptableAsyncInterval(fn, options) {
|
||||
return { wake, stop };
|
||||
}
|
||||
|
||||
function hasAtomicOperators(doc) {
|
||||
if (Array.isArray(doc)) {
|
||||
return doc.reduce((err, u) => err || hasAtomicOperators(u), null);
|
||||
}
|
||||
|
||||
const keys = Object.keys(doc);
|
||||
return keys.length > 0 && keys[0][0] === '$';
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
filterOptions,
|
||||
mergeOptions,
|
||||
@@ -802,6 +843,7 @@ module.exports = {
|
||||
debugOptions,
|
||||
MAX_JS_INT: Number.MAX_SAFE_INTEGER + 1,
|
||||
mergeOptionsAndWriteConcern,
|
||||
translateReadPreference,
|
||||
executeLegacyOperation,
|
||||
applyRetryableWrites,
|
||||
applyWriteConcern,
|
||||
@@ -811,11 +853,11 @@ module.exports = {
|
||||
deprecateOptions,
|
||||
SUPPORTS,
|
||||
MongoDBNamespace,
|
||||
resolveReadPreference,
|
||||
emitDeprecationWarning,
|
||||
makeCounter,
|
||||
maybePromise,
|
||||
now,
|
||||
calculateDurationInMs,
|
||||
makeInterruptableAsyncInterval,
|
||||
hasAtomicOperators
|
||||
makeInterruptableAsyncInterval
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user