Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Apply write concern to command
command = writeConcern(command, self, options);
// Use node md5 generator
var md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ":mongo:" + password);
var userPassword = md5.digest('hex');
// No password
if(typeof password == 'string') {
command.pwd = userPassword;
}
// Force write using primary
commandOptions.readPreference = CoreReadPreference.primary;
// Execute the command
self.command(command, commandOptions, function(err, result) {
if(err && err.ok == 0 && err.code == undefined) return handleCallback(callback, {code: -5000}, null);
if(err) return handleCallback(callback, err, null);
handleCallback(callback, !result.ok ? toError(result) : null
, result.ok ? [{user: username, pwd: ''}] : null);
})
}
var executeCommand = function(self, command, options, callback) {
// Did the user destroy the topology
if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
// Get the db name we are executing against
var dbName = options.dbName || options.authdb || self.s.databaseName;
// If we have a readPreference set
if(options.readPreference == null && self.s.readPreference) {
options.readPreference = self.s.readPreference;
}
// Convert the readPreference if its not a write
if(options.readPreference) {
options.readPreference = convertReadPreference(options.readPreference);
} else {
options.readPreference = CoreReadPreference.primary;
}
// Debug information
if(self.s.logger.isDebug()) self.s.logger.debug(f('executing command %s against %s with options [%s]'
, JSON.stringify(command), f('%s.$cmd', dbName), JSON.stringify(debugOptions(debugFields, options))));
// Execute command
self.s.topology.command(f('%s.$cmd', dbName), command, options, function(err, result) {
if(err) return handleCallback(callback, err);
if(options.full) return handleCallback(callback, null, result);
handleCallback(callback, null, result.result);
});
}
Db.prototype.listCollections = function(filter, options) {
filter = filter || {};
options = options || {};
// Shallow clone the object
options = shallowClone(options);
// Set the promise library
options.promiseLibrary = this.s.promiseLibrary;
// Ensure valid readPreference
if (options.readPreference) {
options.readPreference = convertReadPreference(options.readPreference);
} else {
options.readPreference = this.s.readPreference || ReadPreference.primary;
}
// We have a list collections command
if (this.serverConfig.capabilities().hasListCollectionsCommand) {
// Cursor options
var cursor = options.batchSize ? { batchSize: options.batchSize } : {};
// Build the command
var command = { listCollections: true, filter: filter, cursor: cursor };
// Set the AggregationCursor constructor
options.cursorFactory = CommandCursor;
// Create the cursor
cursor = this.s.topology.cursor(f('%s.$cmd', this.s.databaseName), command, options);
// Do we have a readPreference, apply it
if (options.readPreference) {
cursor.setReadPreference(options.readPreference);
}
var executeCommand = function(self, command, options, callback) {
// Did the user destroy the topology
if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
// Get the db name we are executing against
var dbName = options.dbName || options.authdb || self.s.databaseName;
// If we have a readPreference set
if(options.readPreference == null && self.s.readPreference) {
options.readPreference = self.s.readPreference;
}
// Convert the readPreference if its not a write
if(options.readPreference) {
options.readPreference = convertReadPreference(options.readPreference);
} else {
options.readPreference = CoreReadPreference.primary;
}
// Debug information
if(self.s.logger.isDebug()) self.s.logger.debug(f('executing command %s against %s with options [%s]'
, JSON.stringify(command), f('%s.$cmd', dbName), JSON.stringify(debugOptions(debugFields, options))));
// Execute command
self.s.topology.command(f('%s.$cmd', dbName), command, options, function(err, result) {
if(err) return handleCallback(callback, err);
if(options.full) return handleCallback(callback, null, result);
handleCallback(callback, null, result.result);
});
}
// Get additional values
const maxTimeMS = typeof options.maxTimeMS === 'number' ? options.maxTimeMS : null;
// Add maxTimeMS to options if set
if (maxTimeMS != null) commandOptions.maxTimeMS = maxTimeMS;
// Build the command to execute
let command = {
dropUser: username
};
// Apply write concern to command
command = applyWriteConcern(command, { db }, options);
// Force write using primary
commandOptions.readPreference = ReadPreference.primary;
// Execute the command
executeCommand(db, command, commandOptions, (err, result) => {
if (err && !err.ok && err.code === undefined) return handleCallback(callback, { code: -5000 });
if (err) return handleCallback(callback, err, null);
handleCallback(callback, null, result.ok ? true : false);
});
}
var executeCommand = function(self, command, options, callback) {
// Did the user destroy the topology
if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
// Get the db name we are executing against
var dbName = options.dbName || options.authdb || self.s.databaseName;
// If we have a readPreference set
if(options.readPreference == null && self.s.readPreference) {
options.readPreference = self.s.readPreference;
}
// Convert the readPreference if its not a write
if(options.readPreference) {
options.readPreference = convertReadPreference(options.readPreference);
} else {
options.readPreference = CoreReadPreference.primary;
}
// Debug information
if(self.s.logger.isDebug()) self.s.logger.debug(f('executing command %s against %s with options [%s]'
, JSON.stringify(command), f('%s.$cmd', dbName), JSON.stringify(debugOptions(debugFields, options))));
// Execute command
self.s.topology.command(f('%s.$cmd', dbName), command, options, function(err, result) {
if(err) return handleCallback(callback, err);
if(options.full) return handleCallback(callback, null, result);
handleCallback(callback, null, result.result);
});
}
// Apply write concern to command
command = writeConcern(command, self, options);
// Use node md5 generator
var md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ":mongo:" + password);
var userPassword = md5.digest('hex');
// No password
if(typeof password == 'string') {
command.pwd = userPassword;
}
// Force write using primary
commandOptions.readPreference = CoreReadPreference.primary;
// Execute the command
self.command(command, commandOptions, function(err, result) {
if(err && err.ok == 0 && err.code == undefined) return handleCallback(callback, {code: -5000}, null);
if(err) return handleCallback(callback, err, null);
handleCallback(callback, !result.ok ? toError(result) : null
, result.ok ? [{user: username, pwd: ''}] : null);
})
}
// Apply write concern to command
command = writeConcern(command, self, options);
// Use node md5 generator
var md5 = crypto.createHash('md5');
// Generate keys used for authentication
md5.update(username + ":mongo:" + password);
var userPassword = md5.digest('hex');
// No password
if(typeof password == 'string') {
command.pwd = userPassword;
}
// Force write using primary
commandOptions.readPreference = CoreReadPreference.primary;
// Execute the command
self.command(command, commandOptions, function(err, result) {
if(err && err.ok == 0 && err.code == undefined) return handleCallback(callback, {code: -5000}, null);
if(err) return handleCallback(callback, err, null);
handleCallback(callback, !result.ok ? toError(result) : null
, result.ok ? [{user: username, pwd: ''}] : null);
})
}
var executeCommand = function(self, command, options, callback) {
// Did the user destroy the topology
if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
// Get the db name we are executing against
var dbName = options.dbName || options.authdb || self.s.databaseName;
// If we have a readPreference set
if(options.readPreference == null && self.s.readPreference) {
options.readPreference = self.s.readPreference;
}
// Convert the readPreference if its not a write
if(options.readPreference) {
options.readPreference = convertReadPreference(options.readPreference);
} else {
options.readPreference = CoreReadPreference.primary;
}
// Debug information
if(self.s.logger.isDebug()) self.s.logger.debug(f('executing command %s against %s with options [%s]'
, JSON.stringify(command), f('%s.$cmd', dbName), JSON.stringify(debugOptions(debugFields, options))));
// Execute command
self.s.topology.command(f('%s.$cmd', dbName), command, options, function(err, result) {
if(err) return handleCallback(callback, err);
if(options.full) return handleCallback(callback, null, result);
handleCallback(callback, null, result.result);
});
}