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

@@ -8,6 +8,9 @@ exports.Binary = require('./binary');
exports.Collection = function() {
throw new Error('Cannot create a collection from browser library');
};
exports.getConnection = () => function() {
throw new Error('Cannot create a connection from browser library');
};
exports.Decimal128 = require('./decimal128');
exports.ObjectId = require('./objectid');
exports.ReadPreference = require('./ReadPreference');

View File

@@ -7,7 +7,9 @@
const MongooseCollection = require('../../collection');
const MongooseError = require('../../error/mongooseError');
const Collection = require('mongodb').Collection;
const ObjectId = require('./objectid');
const get = require('../../helpers/get');
const getConstructorName = require('../../helpers/getConstructorName');
const sliced = require('sliced');
const stream = require('stream');
const util = require('util');
@@ -21,9 +23,11 @@ const util = require('util');
* @api private
*/
function NativeCollection(name, options) {
function NativeCollection(name, conn, options) {
this.collection = null;
this.Promise = options.Promise || Promise;
this.modelName = options.modelName;
delete options.modelName;
this._closed = false;
MongooseCollection.apply(this, arguments);
}
@@ -54,6 +58,7 @@ NativeCollection.prototype.onOpen = function() {
if (_this.opts.autoCreate === false) {
_this.collection = _this.conn.db.collection(_this.name);
MongooseCollection.prototype.onOpen.call(_this);
return _this.collection;
}
@@ -127,6 +132,7 @@ function iter(i) {
const _this = this;
const debug = get(_this, 'conn.base.options.debug');
const lastArg = arguments[arguments.length - 1];
const opId = new ObjectId();
// If user force closed, queueing will hang forever. See #5664
if (this.conn.$wasForceClosed) {
@@ -140,22 +146,77 @@ function iter(i) {
}
}
if (this.buffer) {
let _args = args;
let callback = null;
if (this._shouldBufferCommands() && this.buffer) {
if (syncCollectionMethods[i]) {
throw new Error('Collection method ' + i + ' is synchronous');
}
this.conn.emit('buffer', {
_id: opId,
modelName: _this.modelName,
collectionName: _this.name,
method: i,
args: args
});
let callback;
let _args;
let promise = null;
let timeout = null;
if (typeof lastArg === 'function') {
this.addQueue(i, args);
callback = function collectionOperationCallback() {
if (timeout != null) {
clearTimeout(timeout);
}
return lastArg.apply(this, arguments);
};
_args = args.slice(0, args.length - 1).concat([callback]);
} else {
promise = new this.Promise((resolve, reject) => {
callback = function collectionOperationCallback(err, res) {
if (timeout != null) {
clearTimeout(timeout);
}
if (err != null) {
return reject(err);
}
resolve(res);
};
_args = args.concat([callback]);
this.addQueue(i, _args);
});
}
const bufferTimeoutMS = this._getBufferTimeoutMS();
timeout = setTimeout(() => {
const removed = this.removeQueue(i, _args);
if (removed) {
const message = 'Operation `' + this.name + '.' + i + '()` buffering timed out after ' +
bufferTimeoutMS + 'ms';
const err = new MongooseError(message);
this.conn.emit('buffer-end', { _id: opId, modelName: _this.modelName, collectionName: _this.name, method: i, error: err });
callback(err);
}
}, bufferTimeoutMS);
if (typeof lastArg === 'function') {
this.addQueue(i, _args);
return;
}
return new this.Promise((resolve, reject) => {
this.addQueue(i, [].concat(args).concat([(err, res) => {
if (err != null) {
return reject(err);
}
resolve(res);
}]));
});
return promise;
} else if (!syncCollectionMethods[i] && typeof lastArg === 'function') {
callback = function collectionOperationCallback(err, res) {
if (err != null) {
_this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: _this.name, method: i, error: err });
} else {
_this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: _this.name, method: i, result: res });
}
return lastArg.apply(this, arguments);
};
_args = args.slice(0, args.length - 1).concat([callback]);
}
if (debug) {
@@ -165,18 +226,45 @@ function iter(i) {
} else if (debug instanceof stream.Writable) {
this.$printToStream(_this.name, i, args, debug);
} else {
this.$print(_this.name, i, args, typeof debug.color === 'undefined' ? true : debug.color);
const color = debug.color == null ? true : debug.color;
const shell = debug.shell == null ? false : debug.shell;
this.$print(_this.name, i, args, color, shell);
}
}
this.conn.emit('operation-start', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, params: _args });
try {
return collection[i].apply(collection, args);
if (collection == null) {
const message = 'Cannot call `' + this.name + '.' + i + '()` before initial connection ' +
'is complete if `bufferCommands = false`. Make sure you `await mongoose.connect()` if ' +
'you have `bufferCommands = false`.';
throw new MongooseError(message);
}
const ret = collection[i].apply(collection, _args);
if (ret != null && typeof ret.then === 'function') {
return ret.then(
res => {
this.conn.emit('operation-end', { _id: opId, modelName: this.modelName, collectionName: this.name, method: i, result: res });
return res;
},
err => {
this.conn.emit('operation-end', { _id: opId, modelName: this.modelName, collectionName: this.name, method: i, error: err });
throw err;
}
);
}
return ret;
} catch (error) {
// Collection operation may throw because of max bson size, catch it here
// See gh-3906
if (args.length > 0 &&
typeof args[args.length - 1] === 'function') {
args[args.length - 1](error);
if (typeof callback === 'function') {
callback(error);
} else {
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, error: error });
}
if (typeof lastArg === 'function') {
lastArg(error);
} else {
throw error;
}
@@ -206,13 +294,13 @@ for (const key of Object.keys(Collection.prototype)) {
* @method $print
*/
NativeCollection.prototype.$print = function(name, i, args, color) {
NativeCollection.prototype.$print = function(name, i, args, color, shell) {
const moduleName = color ? '\x1B[0;36mMongoose:\x1B[0m ' : 'Mongoose: ';
const functionCall = [name, i].join('.');
const _args = [];
for (let j = args.length - 1; j >= 0; --j) {
if (this.$format(args[j]) || _args.length) {
_args.unshift(this.$format(args[j], color));
_args.unshift(this.$format(args[j], color, shell));
}
}
const params = '(' + _args.join(', ') + ')';
@@ -247,10 +335,10 @@ NativeCollection.prototype.$printToStream = function(name, i, args, stream) {
* @method $format
*/
NativeCollection.prototype.$format = function(arg, color) {
NativeCollection.prototype.$format = function(arg, color, shell) {
const type = typeof arg;
if (type === 'function' || type === 'undefined') return '';
return format(arg, false, color);
return format(arg, false, color, shell);
};
/*!
@@ -272,10 +360,14 @@ function map(o) {
function formatObjectId(x, key) {
x[key] = inspectable('ObjectId("' + x[key].toHexString() + '")');
}
function formatDate(x, key) {
x[key] = inspectable('new Date("' + x[key].toUTCString() + '")');
function formatDate(x, key, shell) {
if (shell) {
x[key] = inspectable('ISODate("' + x[key].toUTCString() + '")');
} else {
x[key] = inspectable('new Date("' + x[key].toUTCString() + '")');
}
}
function format(obj, sub, color) {
function format(obj, sub, color, shell) {
if (obj && typeof obj.toBSON === 'function') {
obj = obj.toBSON();
}
@@ -285,14 +377,15 @@ function format(obj, sub, color) {
const clone = require('../../helpers/clone');
let x = clone(obj, { transform: false });
const constructorName = getConstructorName(x);
if (x.constructor.name === 'Binary') {
if (constructorName === 'Binary') {
x = 'BinData(' + x.sub_type + ', "' + x.toString('base64') + '")';
} else if (x.constructor.name === 'ObjectID') {
} else if (constructorName === 'ObjectID') {
x = inspectable('ObjectId("' + x.toHexString() + '")');
} else if (x.constructor.name === 'Date') {
} else if (constructorName === 'Date') {
x = inspectable('new Date("' + x.toUTCString() + '")');
} else if (x.constructor.name === 'Object') {
} else if (constructorName === 'Object') {
const keys = Object.keys(x);
const numKeys = keys.length;
let key;
@@ -311,16 +404,17 @@ function format(obj, sub, color) {
error = _error;
}
}
if (x[key].constructor.name === 'Binary') {
const _constructorName = getConstructorName(x[key]);
if (_constructorName === 'Binary') {
x[key] = 'BinData(' + x[key].sub_type + ', "' +
x[key].buffer.toString('base64') + '")';
} else if (x[key].constructor.name === 'Object') {
} else if (_constructorName === 'Object') {
x[key] = format(x[key], true);
} else if (x[key].constructor.name === 'ObjectID') {
} else if (_constructorName === 'ObjectID') {
formatObjectId(x, key);
} else if (x[key].constructor.name === 'Date') {
formatDate(x, key);
} else if (x[key].constructor.name === 'ClientSession') {
} else if (_constructorName === 'Date') {
formatDate(x, key, shell);
} else if (_constructorName === 'ClientSession') {
x[key] = inspectable('ClientSession("' +
get(x[key], 'id.id.buffer', '').toString('hex') + '")');
} else if (Array.isArray(x[key])) {

View File

@@ -6,6 +6,8 @@
const MongooseConnection = require('../../connection');
const STATES = require('../../connectionstate');
const immediate = require('../../helpers/immediate');
const setTimeout = require('../../helpers/timers').setTimeout;
/**
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation.
@@ -38,16 +40,20 @@ NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
* Returns a new connection object, with the new db. If you set the `useCache`
* option, `useDb()` will cache connections by `name`.
*
* **Note:** Calling `close()` on a `useDb()` connection will close the base connection as well.
*
* @param {String} name The database name
* @param {Object} [options]
* @param {Boolean} [options.useCache=false] If true, cache results so calling `useDb()` multiple times with the same name only creates 1 connection object.
* @param {Boolean} [options.noListener=false] If true, the new connection object won't listen to any events on the base connection. This is better for memory usage in cases where you're calling `useDb()` for every request.
* @return {Connection} New Connection Object
* @api public
*/
NativeConnection.prototype.useDb = function(name, options) {
// Return immediately if cached
if (options && options.useCache && this.relatedDbs[name]) {
options = options || {};
if (options.useCache && this.relatedDbs[name]) {
return this.relatedDbs[name];
}
@@ -58,7 +64,7 @@ NativeConnection.prototype.useDb = function(name, options) {
newConn.collections = {};
newConn.models = {};
newConn.replica = this.replica;
newConn.config = Object.assign({}, this.base.connection.config, newConn.config);
newConn.config = Object.assign({}, this.config, newConn.config);
newConn.name = this.name;
newConn.options = this.options;
newConn._readyState = this._readyState;
@@ -90,16 +96,24 @@ NativeConnection.prototype.useDb = function(name, options) {
function wireup() {
newConn.client = _this.client;
newConn.db = _this.client.db(name);
const _opts = {};
if (options.hasOwnProperty('noListener')) {
_opts.noListener = options.noListener;
}
newConn.db = _this.client.db(name, _opts);
newConn.onOpen();
// setup the events appropriately
listen(newConn);
if (options.noListener !== true) {
listen(newConn);
}
}
newConn.name = name;
// push onto the otherDbs stack, this is used when state changes
this.otherDbs.push(newConn);
if (options.noListener !== true) {
this.otherDbs.push(newConn);
}
newConn.otherDbs.push(this);
// push onto the relatedDbs cache, this is used when state changes
@@ -116,13 +130,16 @@ NativeConnection.prototype.useDb = function(name, options) {
*/
function listen(conn) {
if (conn.db._listening) {
if (conn._listening) {
return;
}
conn.db._listening = true;
conn._listening = true;
conn.db.on('close', function(force) {
if (conn._closeCalled) return;
conn.client.on('close', function(force) {
if (conn._closeCalled) {
return;
}
conn._closeCalled = conn.client._closeCalled;
// the driver never emits an `open` event. auto_reconnect still
// emits a `close` event but since we never get another
@@ -134,26 +151,30 @@ function listen(conn) {
}
conn.onClose(force);
});
conn.db.on('error', function(err) {
conn.client.on('error', function(err) {
conn.emit('error', err);
});
conn.db.on('reconnect', function() {
conn.readyState = STATES.connected;
conn.emit('reconnect');
conn.emit('reconnected');
conn.onOpen();
});
conn.db.on('timeout', function(err) {
conn.emit('timeout', err);
});
conn.db.on('open', function(err, db) {
if (STATES.disconnected === conn.readyState && db && db.databaseName) {
if (!conn.client.s.options.useUnifiedTopology) {
conn.db.on('reconnect', function() {
conn.readyState = STATES.connected;
conn.emit('reconnect');
conn.emit('reconnected');
}
conn.onOpen();
});
conn.db.on('open', function(err, db) {
if (STATES.disconnected === conn.readyState && db && db.databaseName) {
conn.readyState = STATES.connected;
conn.emit('reconnect');
conn.emit('reconnected');
}
});
}
conn.client.on('timeout', function(err) {
conn.emit('timeout', err);
});
conn.db.on('parseError', function(err) {
conn.client.on('parseError', function(err) {
conn.emit('parseError', err);
});
}
@@ -168,6 +189,11 @@ function listen(conn) {
*/
NativeConnection.prototype.doClose = function(force, fn) {
if (this.client == null) {
immediate(() => fn());
return this;
}
this.client.close(force, (err, res) => {
// Defer because the driver will wait at least 1ms before finishing closing
// the pool, see https://github.com/mongodb-js/mongodb-core/blob/a8f8e4ce41936babc3b9112bf42d609779f03b39/lib/connection/pool.js#L1026-L1030.

View File

@@ -8,4 +8,5 @@ exports.Binary = require('./binary');
exports.Collection = require('./collection');
exports.Decimal128 = require('./decimal128');
exports.ObjectId = require('./objectid');
exports.ReadPreference = require('./ReadPreference');
exports.ReadPreference = require('./ReadPreference');
exports.getConnection = () => require('./connection');