This commit is contained in:
Jonasz Bigda
2023-03-25 21:51:42 +01:00
parent 0db1d5117e
commit b332e9ceb0
1044 changed files with 37502 additions and 63938 deletions

View File

@@ -13,8 +13,9 @@ const ReplSet = require('../topologies/replset');
const Server = require('../topologies/server');
const ServerSessionPool = require('../core').Sessions.ServerSessionPool;
const emitDeprecationWarning = require('../utils').emitDeprecationWarning;
const emitWarningOnce = require('../utils').emitWarningOnce;
const fs = require('fs');
const BSON = require('../core/connection/utils').retrieveBSON();
const WriteConcern = require('../write_concern');
const CMAP_EVENT_NAMES = require('../cmap/events').CMAP_EVENT_NAMES;
let client;
@@ -33,9 +34,11 @@ const legacyParse = deprecate(
const AUTH_MECHANISM_INTERNAL_MAP = {
DEFAULT: 'default',
'MONGODB-CR': 'mongocr',
PLAIN: 'plain',
GSSAPI: 'gssapi',
'MONGODB-CR': 'mongocr',
'MONGODB-X509': 'x509',
'MONGODB-AWS': 'mongodb-aws',
'SCRAM-SHA-1': 'scram-sha-1',
'SCRAM-SHA-256': 'scram-sha-256'
};
@@ -66,12 +69,13 @@ const monitoringEvents = [
const VALID_AUTH_MECHANISMS = new Set([
'DEFAULT',
'MONGODB-CR',
'PLAIN',
'GSSAPI',
'MONGODB-CR',
'MONGODB-X509',
'MONGODB-AWS',
'SCRAM-SHA-1',
'SCRAM-SHA-256',
'GSSAPI'
'SCRAM-SHA-256'
]);
const validOptionNames = [
@@ -102,6 +106,7 @@ const validOptionNames = [
'w',
'wtimeout',
'j',
'writeConcern',
'forceServerObjectId',
'serializeFunctions',
'ignoreUndefined',
@@ -117,6 +122,7 @@ const validOptionNames = [
'promoteValues',
'promoteBuffers',
'promoteLongs',
'bsonRegExp',
'domainsEnabled',
'checkServerIdentity',
'validateOptions',
@@ -132,6 +138,7 @@ const validOptionNames = [
'auto_reconnect',
'minSize',
'monitorCommands',
'serverApi',
'retryWrites',
'retryReads',
'useNewUrlParser',
@@ -151,6 +158,8 @@ const validOptionNames = [
'tlsCertificateKeyFilePassword',
'minHeartbeatFrequencyMS',
'heartbeatFrequencyMS',
'directConnection',
'appName',
// CMAP options
'maxPoolSize',
@@ -175,12 +184,12 @@ function validOptions(options) {
if (options.validateOptions) {
return new MongoError(`option ${name} is not supported`);
} else {
console.warn(`the options [${name}] is not supported`);
emitWarningOnce(`the options [${name}] is not supported`);
}
}
if (legacyOptionNames.indexOf(name) !== -1) {
console.warn(
emitWarningOnce(
`the server/replset/mongos/db options are deprecated, ` +
`all their options are supported at the top level of the options object [${validOptionNames}]`
);
@@ -250,9 +259,6 @@ function resolveTLSOptions(options) {
});
}
const emitDeprecationForNonUnifiedTopology = deprecate(() => {},
'current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. ' + 'To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.');
function connect(mongoClient, url, options, callback) {
options = Object.assign({}, options);
@@ -285,7 +291,7 @@ function connect(mongoClient, url, options, callback) {
const _finalOptions = createUnifiedOptions(object, options);
// Check if we have connection and socket timeout set
if (_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 360000;
if (_finalOptions.socketTimeoutMS == null) _finalOptions.socketTimeoutMS = 0;
if (_finalOptions.connectTimeoutMS == null) _finalOptions.connectTimeoutMS = 10000;
if (_finalOptions.retryWrites == null) _finalOptions.retryWrites = true;
if (_finalOptions.useRecoveryToken == null) _finalOptions.useRecoveryToken = true;
@@ -295,18 +301,16 @@ function connect(mongoClient, url, options, callback) {
delete _finalOptions.db_options.auth;
}
// `journal` should be translated to `j` for the driver
if (_finalOptions.journal != null) {
_finalOptions.j = _finalOptions.journal;
_finalOptions.journal = undefined;
}
// resolve tls options if needed
resolveTLSOptions(_finalOptions);
// Store the merged options object
mongoClient.s.options = _finalOptions;
// Apply read and write concern from parsed url
mongoClient.s.readPreference = ReadPreference.fromOptions(_finalOptions);
mongoClient.s.writeConcern = WriteConcern.fromOptions(_finalOptions);
// Failure modes
if (object.servers.length === 0) {
return callback(new Error('connection string must contain at least one seed host'));
@@ -330,7 +334,9 @@ function connect(mongoClient, url, options, callback) {
return createTopology(mongoClient, 'unified', _finalOptions, connectCallback);
}
emitDeprecationForNonUnifiedTopology();
emitWarningOnce(
'Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.'
);
// Do we have a replicaset then skip discovery and go straight to connectivity
if (_finalOptions.replicaSet || _finalOptions.rs_name) {
@@ -491,58 +497,9 @@ function createTopology(mongoClient, topologyType, options, callback) {
// determine CSFLE support
if (options.autoEncryption != null) {
let AutoEncrypter;
try {
require.resolve('mongodb-client-encryption');
} catch (err) {
callback(
new MongoError(
'Auto-encryption requested, but the module is not installed. Please add `mongodb-client-encryption` as a dependency of your project'
)
);
return;
}
try {
let mongodbClientEncryption = require('mongodb-client-encryption');
if (typeof mongodbClientEncryption.extension !== 'function') {
callback(
new MongoError(
'loaded version of `mongodb-client-encryption` does not have property `extension`. Please make sure you are loading the correct version of `mongodb-client-encryption`'
)
);
}
AutoEncrypter = mongodbClientEncryption.extension(require('../../index')).AutoEncrypter;
} catch (err) {
callback(err);
return;
}
const mongoCryptOptions = Object.assign(
{
bson:
options.bson ||
new BSON([
BSON.Binary,
BSON.Code,
BSON.DBRef,
BSON.Decimal128,
BSON.Double,
BSON.Int32,
BSON.Long,
BSON.Map,
BSON.MaxKey,
BSON.MinKey,
BSON.ObjectId,
BSON.BSONRegExp,
BSON.Symbol,
BSON.Timestamp
])
},
options.autoEncryption
);
options.autoEncrypter = new AutoEncrypter(mongoClient, mongoCryptOptions);
const Encrypter = require('../encrypter').Encrypter;
options.encrypter = new Encrypter(mongoClient, options);
options.autoEncrypter = options.encrypter.autoEncrypter;
}
// Create the topology
@@ -580,7 +537,10 @@ function createTopology(mongoClient, topologyType, options, callback) {
return;
}
callback(undefined, topology);
options.encrypter.connectInternalClient(error => {
if (error) return callback(error);
callback(undefined, topology);
});
});
});
@@ -611,9 +571,12 @@ function createUnifiedOptions(finalOptions, options) {
'mongos_options'
];
const noMerge = ['readconcern', 'compression', 'autoencryption'];
const skip = ['w', 'wtimeout', 'j', 'journal', 'fsync', 'writeconcern'];
for (const name in options) {
if (noMerge.indexOf(name.toLowerCase()) !== -1) {
if (skip.indexOf(name.toLowerCase()) !== -1) {
continue;
} else if (noMerge.indexOf(name.toLowerCase()) !== -1) {
finalOptions[name] = options[name];
} else if (childOptions.indexOf(name.toLowerCase()) !== -1) {
finalOptions = mergeOptions(finalOptions, options[name], false);
@@ -631,6 +594,14 @@ function createUnifiedOptions(finalOptions, options) {
}
}
// Handle write concern keys separately, since `options` may have the keys at the top level or
// under `options.writeConcern`. The final merged keys will be under `finalOptions.writeConcern`.
// This way, `fromOptions` will warn once if `options` is using deprecated write concern options
const optionsWriteConcern = WriteConcern.fromOptions(options);
if (optionsWriteConcern) {
finalOptions.writeConcern = Object.assign({}, finalOptions.writeConcern, optionsWriteConcern);
}
return finalOptions;
}
@@ -644,6 +615,7 @@ function generateCredentials(client, username, password, options) {
// authMechanism
const authMechanismRaw = options.authMechanism || 'DEFAULT';
const authMechanism = authMechanismRaw.toUpperCase();
const mechanismProperties = options.authMechanismProperties;
if (!VALID_AUTH_MECHANISMS.has(authMechanism)) {
throw MongoError.create({
@@ -652,18 +624,9 @@ function generateCredentials(client, username, password, options) {
});
}
if (authMechanism === 'GSSAPI') {
return new MongoCredentials({
mechanism: process.platform === 'win32' ? 'sspi' : 'gssapi',
mechanismProperties: options,
source,
username,
password
});
}
return new MongoCredentials({
mechanism: AUTH_MECHANISM_INTERNAL_MAP[authMechanism],
mechanismProperties,
source,
username,
password
@@ -763,12 +726,24 @@ function transformUrlOptions(_object) {
if (object.wTimeoutMS) {
object.wtimeout = object.wTimeoutMS;
object.wTimeoutMS = undefined;
}
if (_object.srvHost) {
object.srvHost = _object.srvHost;
}
// Any write concern options from the URL will be top-level, so we manually
// move them options under `object.writeConcern` to avoid warnings later
const wcKeys = ['w', 'wtimeout', 'j', 'journal', 'fsync'];
for (const key of wcKeys) {
if (object[key] !== undefined) {
if (object.writeConcern === undefined) object.writeConcern = {};
object.writeConcern[key] = object[key];
object[key] = undefined;
}
}
return object;
}
@@ -791,7 +766,7 @@ function translateOptions(options, translationOptions) {
}
// Set the socket and connection timeouts
if (options.socketTimeoutMS == null) options.socketTimeoutMS = 360000;
if (options.socketTimeoutMS == null) options.socketTimeoutMS = 0;
if (options.connectTimeoutMS == null) options.connectTimeoutMS = 10000;
if (!translationOptions.createServers) {