Added node-modules

This commit is contained in:
Dobie Wollert
2014-09-14 07:04:16 -04:00
parent 663941bf57
commit 6a92348cf5
4870 changed files with 670395 additions and 0 deletions

View File

@ -0,0 +1,8 @@
/*!
* Module dependencies.
*/
var Binary = require('mongodb').BSONPure.Binary;
module.exports = exports = Binary;

View File

@ -0,0 +1,211 @@
/*!
* Module dependencies.
*/
var MongooseCollection = require('../../collection')
, Collection = require('mongodb').Collection
, STATES = require('../../connectionstate')
, utils = require('../../utils')
/**
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) collection implementation.
*
* All methods methods from the [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) driver are copied and wrapped in queue management.
*
* @inherits Collection
* @api private
*/
function NativeCollection () {
this.collection = null;
MongooseCollection.apply(this, arguments);
}
/*!
* Inherit from abstract Collection.
*/
NativeCollection.prototype.__proto__ = MongooseCollection.prototype;
/**
* Called when the connection opens.
*
* @api private
*/
NativeCollection.prototype.onOpen = function () {
var self = this;
// always get a new collection in case the user changed host:port
// of parent db instance when re-opening the connection.
if (!self.opts.capped.size) {
// non-capped
return self.conn.db.collection(self.name, callback);
}
// capped
return self.conn.db.collection(self.name, function (err, c) {
if (err) return callback(err);
// discover if this collection exists and if it is capped
c.options(function (err, exists) {
if (err) return callback(err);
if (exists) {
if (exists.capped) {
callback(null, c);
} else {
var msg = 'A non-capped collection exists with the name: '+ self.name +'\n\n'
+ ' To use this collection as a capped collection, please '
+ 'first convert it.\n'
+ ' http://www.mongodb.org/display/DOCS/Capped+Collections#CappedCollections-Convertingacollectiontocapped'
err = new Error(msg);
callback(err);
}
} else {
// create
var opts = utils.clone(self.opts.capped);
opts.capped = true;
self.conn.db.createCollection(self.name, opts, callback);
}
});
});
function callback (err, collection) {
if (err) {
// likely a strict mode error
self.conn.emit('error', err);
} else {
self.collection = collection;
MongooseCollection.prototype.onOpen.call(self);
}
};
};
/**
* Called when the connection closes
*
* @api private
*/
NativeCollection.prototype.onClose = function () {
MongooseCollection.prototype.onClose.call(this);
};
/*!
* Copy the collection methods and make them subject to queues
*/
for (var i in Collection.prototype) {
(function(i){
NativeCollection.prototype[i] = function () {
if (this.buffer) {
this.addQueue(i, arguments);
return;
}
var collection = this.collection
, args = arguments
, self = this
, debug = self.conn.base.options.debug;
if (debug) {
if ('function' === typeof debug) {
debug.apply(debug
, [self.name, i].concat(utils.args(args, 0, args.length-1)));
} else {
console.error('\x1B[0;36mMongoose:\x1B[0m %s.%s(%s) %s %s %s'
, self.name
, i
, print(args[0])
, print(args[1])
, print(args[2])
, print(args[3]))
}
}
collection[i].apply(collection, args);
};
})(i);
}
/*!
* Debug print helper
*/
function print (arg) {
var type = typeof arg;
if ('function' === type || 'undefined' === type) return '';
return format(arg);
}
/*!
* Debug print helper
*/
function format (obj, sub) {
var x = utils.clone(obj);
if (x) {
if ('Binary' === x.constructor.name) {
x = '[object Buffer]';
} else if ('ObjectID' === x.constructor.name) {
var representation = 'ObjectId("' + x.toHexString() + '")';
x = { inspect: function() { return representation; } };
} else if ('Date' === x.constructor.name) {
var representation = 'new Date("' + x.toUTCString() + '")';
x = { inspect: function() { return representation; } };
} else if ('Object' === x.constructor.name) {
var keys = Object.keys(x)
, i = keys.length
, key
while (i--) {
key = keys[i];
if (x[key]) {
if ('Binary' === x[key].constructor.name) {
x[key] = '[object Buffer]';
} else if ('Object' === x[key].constructor.name) {
x[key] = format(x[key], true);
} else if ('ObjectID' === x[key].constructor.name) {
;(function(x){
var representation = 'ObjectId("' + x[key].toHexString() + '")';
x[key] = { inspect: function() { return representation; } };
})(x)
} else if ('Date' === x[key].constructor.name) {
;(function(x){
var representation = 'new Date("' + x[key].toUTCString() + '")';
x[key] = { inspect: function() { return representation; } };
})(x)
} else if (Array.isArray(x[key])) {
x[key] = x[key].map(function (o) {
return format(o, true)
});
}
}
}
}
if (sub) return x;
}
return require('util')
.inspect(x, false, 10, true)
.replace(/\n/g, '')
.replace(/\s{2,}/g, ' ')
}
/**
* Retreives information about this collections indexes.
*
* @param {Function} callback
* @method getIndexes
* @api public
*/
NativeCollection.prototype.getIndexes = NativeCollection.prototype.indexInformation;
/*!
* Module exports.
*/
module.exports = NativeCollection;

View File

@ -0,0 +1,303 @@
/*!
* Module dependencies.
*/
var MongooseConnection = require('../../connection')
, mongo = require('mongodb')
, Db = mongo.Db
, Server = mongo.Server
, Mongos = mongo.Mongos
, STATES = require('../../connectionstate')
, ReplSetServers = mongo.ReplSetServers;
/**
* A [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) connection implementation.
*
* @inherits Connection
* @api private
*/
function NativeConnection() {
MongooseConnection.apply(this, arguments);
this._listening = false;
};
/*!
* Inherits from Connection.
*/
NativeConnection.prototype.__proto__ = MongooseConnection.prototype;
/**
* Opens the connection to MongoDB.
*
* @param {Function} fn
* @return {Connection} this
* @api private
*/
NativeConnection.prototype.doOpen = function (fn) {
if (this.db) {
mute(this);
}
var server = new Server(this.host, this.port, this.options.server);
this.db = new Db(this.name, server, this.options.db);
var self = this;
this.db.open(function (err) {
if (err) return fn(err);
listen(self);
fn();
});
return this;
};
/*!
* Register listeners for important events and bubble appropriately.
*/
function listen (conn) {
if (conn._listening) return;
conn._listening = true;
conn.db.on('close', function(){
if (conn._closeCalled) return;
// the driver never emits an `open` event. auto_reconnect still
// emits a `close` event but since we never get another
// `open` we can't emit close
if (conn.db.serverConfig.autoReconnect) {
conn.readyState = STATES.disconnected;
conn.emit('close');
return;
}
conn.onClose();
});
conn.db.on('error', function(err){
conn.emit('error', err);
});
conn.db.on('timeout', function(err){
var error = new Error(err && err.err || 'connection timeout');
conn.emit('error', error);
});
conn.db.on('open', function (err, db) {
if (STATES.disconnected === conn.readyState && db && db.databaseName) {
conn.readyState = STATES.connected;
conn.emit('reconnected')
}
})
}
/*!
* Remove listeners registered in `listen`
*/
function mute (conn) {
if (!conn.db) throw new Error('missing db');
conn.db.removeAllListeners("close");
conn.db.removeAllListeners("error");
conn.db.removeAllListeners("timeout");
conn.db.removeAllListeners("open");
conn.db.removeAllListeners("fullsetup");
conn._listening = false;
}
/**
* Opens a connection to a MongoDB ReplicaSet.
*
* See description of [doOpen](#NativeConnection-doOpen) for server options. In this case `options.replset` is also passed to ReplSetServers.
*
* @param {Function} fn
* @api private
* @return {Connection} this
*/
NativeConnection.prototype.doOpenSet = function (fn) {
if (this.db) {
mute(this);
}
var servers = []
, self = this;
this.hosts.forEach(function (server) {
var host = server.host || server.ipc;
var port = server.port || 27017;
servers.push(new Server(host, port, self.options.server));
})
var server = this.options.mongos
? new Mongos(servers, this.options.mongos)
: new ReplSetServers(servers, this.options.replset);
this.db = new Db(this.name, server, this.options.db);
this.db.on('fullsetup', function () {
self.emit('fullsetup')
});
this.db.open(function (err) {
if (err) return fn(err);
fn();
listen(self);
});
return this;
};
/**
* Closes the connection
*
* @param {Function} fn
* @return {Connection} this
* @api private
*/
NativeConnection.prototype.doClose = function (fn) {
this.db.close();
if (fn) fn();
return this;
}
/**
* Prepares default connection options for the node-mongodb-native driver.
*
* _NOTE: `passed` options take precedence over connection string options._
*
* @param {Object} passed options that were passed directly during connection
* @param {Object} [connStrOptions] options that were passed in the connection string
* @api private
*/
NativeConnection.prototype.parseOptions = function (passed, connStrOpts) {
var o = passed || {};
o.db || (o.db = {});
o.auth || (o.auth = {});
o.server || (o.server = {});
o.replset || (o.replset = {});
o.server.socketOptions || (o.server.socketOptions = {});
o.replset.socketOptions || (o.replset.socketOptions = {});
var opts = connStrOpts || {};
Object.keys(opts).forEach(function (name) {
switch (name) {
case 'poolSize':
if ('undefined' == typeof o.server.poolSize) {
o.server.poolSize = o.replset.poolSize = opts[name];
}
break;
case 'slaveOk':
if ('undefined' == typeof o.server.slave_ok) {
o.server.slave_ok = opts[name];
}
break;
case 'autoReconnect':
if ('undefined' == typeof o.server.auto_reconnect) {
o.server.auto_reconnect = opts[name];
}
break;
case 'ssl':
case 'socketTimeoutMS':
case 'connectTimeoutMS':
if ('undefined' == typeof o.server.socketOptions[name]) {
o.server.socketOptions[name] = o.replset.socketOptions[name] = opts[name];
}
break;
case 'authdb':
if ('undefined' == typeof o.auth.authdb) {
o.auth.authdb = opts[name];
}
break;
case 'authSource':
if ('undefined' == typeof o.auth.authSource) {
o.auth.authSource = opts[name];
}
break;
case 'retries':
case 'reconnectWait':
case 'rs_name':
if ('undefined' == typeof o.replset[name]) {
o.replset[name] = opts[name];
}
break;
case 'replicaSet':
if ('undefined' == typeof o.replset.rs_name) {
o.replset.rs_name = opts[name];
}
break;
case 'readSecondary':
if ('undefined' == typeof o.replset.read_secondary) {
o.replset.read_secondary = opts[name];
}
break;
case 'nativeParser':
if ('undefined' == typeof o.db.native_parser) {
o.db.native_parser = opts[name];
}
break;
case 'w':
case 'safe':
case 'fsync':
case 'journal':
case 'wtimeoutMS':
if ('undefined' == typeof o.db[name]) {
o.db[name] = opts[name];
}
break;
case 'readPreference':
if ('undefined' == typeof o.db.read_preference) {
o.db.read_preference = opts[name];
}
break;
case 'readPreferenceTags':
if ('undefined' == typeof o.db.read_preference_tags) {
o.db.read_preference_tags = opts[name];
}
break;
}
})
if (!('auto_reconnect' in o.server)) {
o.server.auto_reconnect = true;
}
if (!o.db.read_preference) {
// read from primaries by default
o.db.read_preference = 'primary';
}
// mongoose creates its own ObjectIds
o.db.forceServerObjectId = false;
// default safe using new nomenclature
if (!('journal' in o.db || 'j' in o.db ||
'fsync' in o.db || 'safe' in o.db || 'w' in o.db)) {
o.db.w = 1;
}
validate(o);
return o;
}
/*!
* Validates the driver db options.
*
* @param {Object} o
*/
function validate (o) {
if (-1 === o.db.w || 0 === o.db.w) {
if (o.db.journal || o.db.fsync || o.db.safe) {
throw new Error(
'Invalid writeConcern: '
+ 'w set to -1 or 0 cannot be combined with safe|fsync|journal');
}
}
}
/*!
* Module exports.
*/
module.exports = NativeConnection;

View File

@ -0,0 +1,29 @@
/*!
* [node-mongodb-native](https://github.com/mongodb/node-mongodb-native) ObjectId
* @constructor NodeMongoDbObjectId
* @see ObjectId
*/
var ObjectId = require('mongodb').BSONPure.ObjectID;
/*!
* ignore
*/
var ObjectIdToString = ObjectId.toString.bind(ObjectId);
module.exports = exports = ObjectId;
ObjectId.fromString = function(str){
// patch native driver bug in V0.9.6.4
if (!('string' === typeof str && 24 === str.length)) {
throw new Error("Invalid ObjectId");
}
return ObjectId.createFromHexString(str);
};
ObjectId.toString = function(oid){
if (!arguments.length) return ObjectIdToString();
return oid.toHexString();
};