summaryrefslogtreecommitdiff
path: root/node_modules/mongodb/lib/mongodb/commands
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/mongodb/lib/mongodb/commands')
-rw-r--r--node_modules/mongodb/lib/mongodb/commands/base_command.js27
-rw-r--r--node_modules/mongodb/lib/mongodb/commands/db_command.js205
-rw-r--r--node_modules/mongodb/lib/mongodb/commands/delete_command.js111
-rw-r--r--node_modules/mongodb/lib/mongodb/commands/get_more_command.js83
-rw-r--r--node_modules/mongodb/lib/mongodb/commands/insert_command.js141
-rw-r--r--node_modules/mongodb/lib/mongodb/commands/kill_cursor_command.js98
-rw-r--r--node_modules/mongodb/lib/mongodb/commands/query_command.js209
-rw-r--r--node_modules/mongodb/lib/mongodb/commands/update_command.js174
8 files changed, 1048 insertions, 0 deletions
diff --git a/node_modules/mongodb/lib/mongodb/commands/base_command.js b/node_modules/mongodb/lib/mongodb/commands/base_command.js
new file mode 100644
index 0000000..6e531d3
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/base_command.js
@@ -0,0 +1,27 @@
+/**
+ Base object used for common functionality
+**/
+var BaseCommand = exports.BaseCommand = function() {
+};
+
+var id = 1;
+BaseCommand.prototype.getRequestId = function() {
+ if (!this.requestId) this.requestId = id++;
+ return this.requestId;
+};
+
+BaseCommand.prototype.updateRequestId = function() {
+ this.requestId = id++;
+ return this.requestId;
+};
+
+// OpCodes
+BaseCommand.OP_REPLY = 1;
+BaseCommand.OP_MSG = 1000;
+BaseCommand.OP_UPDATE = 2001;
+BaseCommand.OP_INSERT = 2002;
+BaseCommand.OP_GET_BY_OID = 2003;
+BaseCommand.OP_QUERY = 2004;
+BaseCommand.OP_GET_MORE = 2005;
+BaseCommand.OP_DELETE = 2006;
+BaseCommand.OP_KILL_CURSORS = 2007; \ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/commands/db_command.js b/node_modules/mongodb/lib/mongodb/commands/db_command.js
new file mode 100644
index 0000000..7586886
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/db_command.js
@@ -0,0 +1,205 @@
+var QueryCommand = require('./query_command').QueryCommand,
+ InsertCommand = require('./insert_command').InsertCommand,
+ inherits = require('util').inherits,
+ crypto = require('crypto');
+
+/**
+ Db Command
+**/
+var DbCommand = exports.DbCommand = function(dbInstance, collectionName, queryOptions, numberToSkip, numberToReturn, query, returnFieldSelector, options) {
+ QueryCommand.call(this);
+ this.collectionName = collectionName;
+ this.queryOptions = queryOptions;
+ this.numberToSkip = numberToSkip;
+ this.numberToReturn = numberToReturn;
+ this.query = query;
+ this.returnFieldSelector = returnFieldSelector;
+ this.db = dbInstance;
+
+ // Make sure we don't get a null exception
+ options = options == null ? {} : options;
+ // Let us defined on a command basis if we want functions to be serialized or not
+ if(options['serializeFunctions'] != null && options['serializeFunctions']) {
+ this.serializeFunctions = true;
+ }
+};
+
+inherits(DbCommand, QueryCommand);
+
+// Constants
+DbCommand.SYSTEM_NAMESPACE_COLLECTION = "system.namespaces";
+DbCommand.SYSTEM_INDEX_COLLECTION = "system.indexes";
+DbCommand.SYSTEM_PROFILE_COLLECTION = "system.profile";
+DbCommand.SYSTEM_USER_COLLECTION = "system.users";
+DbCommand.SYSTEM_COMMAND_COLLECTION = "$cmd";
+
+// New commands
+DbCommand.NcreateIsMasterCommand = function(db, databaseName) {
+ return new DbCommand(db, databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'ismaster':1}, null);
+};
+
+// Provide constructors for different db commands
+DbCommand.createIsMasterCommand = function(db) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'ismaster':1}, null);
+};
+
+DbCommand.createCollectionInfoCommand = function(db, selector) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_NAMESPACE_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, 0, selector, null);
+};
+
+DbCommand.createGetNonceCommand = function(db) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'getnonce':1}, null);
+};
+
+DbCommand.createAuthenticationCommand = function(db, username, password, nonce) {
+ // Use node md5 generator
+ var md5 = crypto.createHash('md5');
+ // Generate keys used for authentication
+ md5.update(username + ":mongo:" + password);
+ var hash_password = md5.digest('hex');
+ // Final key
+ md5 = crypto.createHash('md5');
+ md5.update(nonce + username + hash_password);
+ var key = md5.digest('hex');
+ // Creat selector
+ var selector = {'authenticate':1, 'user':username, 'nonce':nonce, 'key':key};
+ // Create db command
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NONE, 0, -1, selector, null);
+};
+
+DbCommand.createLogoutCommand = function(db) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'logout':1}, null);
+};
+
+DbCommand.createCreateCollectionCommand = function(db, collectionName, options) {
+ var selector = {'create':collectionName};
+ // Modify the options to ensure correct behaviour
+ for(var name in options) {
+ if(options[name] != null && options[name].constructor != Function) selector[name] = options[name];
+ }
+ // Execute the command
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, selector, null);
+};
+
+DbCommand.createDropCollectionCommand = function(db, collectionName) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'drop':collectionName}, null);
+};
+
+DbCommand.createRenameCollectionCommand = function(db, fromCollectionName, toCollectionName) {
+ var renameCollection = db.databaseName + "." + fromCollectionName;
+ var toCollection = db.databaseName + "." + toCollectionName;
+ return new DbCommand(db, "admin." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'renameCollection':renameCollection, 'to':toCollection}, null);
+};
+
+DbCommand.createGetLastErrorCommand = function(options, db) {
+ var args = Array.prototype.slice.call(arguments, 0);
+ db = args.pop();
+ options = args.length ? args.shift() : {};
+ // Final command
+ var command = {'getlasterror':1};
+ // If we have an options Object let's merge in the fields (fsync/wtimeout/w)
+ if('object' === typeof options) {
+ for(var name in options) {
+ command[name] = options[name]
+ }
+ }
+
+ // Execute command
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command, null);
+};
+
+DbCommand.createGetLastStatusCommand = DbCommand.createGetLastErrorCommand;
+
+DbCommand.createGetPreviousErrorsCommand = function(db) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'getpreverror':1}, null);
+};
+
+DbCommand.createResetErrorHistoryCommand = function(db) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'reseterror':1}, null);
+};
+
+DbCommand.createCreateIndexCommand = function(db, collectionName, fieldOrSpec, options) {
+ var fieldHash = {};
+ var indexes = [];
+ var keys;
+
+ // Get all the fields accordingly
+ if (fieldOrSpec.constructor === String) { // 'type'
+ indexes.push(fieldOrSpec + '_' + 1);
+ fieldHash[fieldOrSpec] = 1;
+ } else if (fieldOrSpec.constructor === Array) { // [{location:'2d'}, ...]
+ fieldOrSpec.forEach(function(f) {
+ if (f.constructor === String) { // [{location:'2d'}, 'type']
+ indexes.push(f + '_' + 1);
+ fieldHash[f] = 1;
+ } else if (f.constructor === Array) { // [['location', '2d'],['type', 1]]
+ indexes.push(f[0] + '_' + (f[1] || 1));
+ fieldHash[f[0]] = f[1] || 1;
+ } else if (f.constructor === Object) { // [{location:'2d'}, {type:1}]
+ keys = Object.keys(f);
+ keys.forEach(function(k) {
+ indexes.push(k + '_' + f[k]);
+ fieldHash[k] = f[k];
+ });
+ } else {
+ // undefined
+ }
+ });
+ } else if (fieldOrSpec.constructor === Object) { // {location:'2d', type:1}
+ keys = Object.keys(fieldOrSpec);
+ keys.forEach(function(key) {
+ indexes.push(key + '_' + fieldOrSpec[key]);
+ fieldHash[key] = fieldOrSpec[key];
+ });
+ }
+
+ // Generate the index name
+ var indexName = indexes.join("_");
+ // Build the selector
+ var selector = {'ns':(db.databaseName + "." + collectionName), 'key':fieldHash, 'name':indexName};
+
+ // Ensure we have a correct finalUnique
+ var finalUnique = options == null || 'object' === typeof options ? false : options;
+ // Set up options
+ options = options == null || typeof options == 'boolean' ? {} : options;
+
+ // Add all the options
+ var keys = Object.keys(options);
+ // Add all the fields to the selector
+ for(var i = 0; i < keys.length; i++) {
+ selector[keys[i]] = options[keys[i]];
+ }
+
+ // If we don't have the unique property set on the selector
+ if(selector['unique'] == null) selector['unique'] = finalUnique;
+ // Create the insert command for the index and return the document
+ return new InsertCommand(db, db.databaseName + "." + DbCommand.SYSTEM_INDEX_COLLECTION, false).add(selector);
+};
+
+DbCommand.logoutCommand = function(db, command_hash) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command_hash, null);
+}
+
+DbCommand.createDropIndexCommand = function(db, collectionName, indexName) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'deleteIndexes':collectionName, 'index':indexName}, null);
+};
+
+DbCommand.createReIndexCommand = function(db, collectionName) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'reIndex':collectionName}, null);
+};
+
+DbCommand.createDropDatabaseCommand = function(db) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, {'dropDatabase':1}, null);
+};
+
+DbCommand.createDbCommand = function(db, command_hash, options) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command_hash, null, options);
+};
+
+DbCommand.createAdminDbCommand = function(db, command_hash) {
+ return new DbCommand(db, "admin." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT, 0, -1, command_hash, null);
+};
+
+DbCommand.createDbSlaveOkCommand = function(db, command_hash, options) {
+ return new DbCommand(db, db.databaseName + "." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT | QueryCommand.OPTS_SLAVE, 0, -1, command_hash, null, options);
+}; \ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/commands/delete_command.js b/node_modules/mongodb/lib/mongodb/commands/delete_command.js
new file mode 100644
index 0000000..1adad07
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/delete_command.js
@@ -0,0 +1,111 @@
+var BaseCommand = require('./base_command').BaseCommand,
+ inherits = require('util').inherits;
+
+/**
+ Insert Document Command
+**/
+var DeleteCommand = exports.DeleteCommand = function(db, collectionName, selector) {
+ BaseCommand.call(this);
+
+ // Validate correctness off the selector
+ var object = selector;
+ if(Buffer.isBuffer(object)) {
+ var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
+ if(object_size != object.length) {
+ var error = new Error("delete raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
+ error.name = 'MongoError';
+ throw error;
+ }
+ }
+
+ this.collectionName = collectionName;
+ this.selector = selector;
+ this.db = db;
+};
+
+inherits(DeleteCommand, BaseCommand);
+
+DeleteCommand.OP_DELETE = 2006;
+
+/*
+struct {
+ MsgHeader header; // standard message header
+ int32 ZERO; // 0 - reserved for future use
+ cstring fullCollectionName; // "dbname.collectionname"
+ int32 ZERO; // 0 - reserved for future use
+ mongo.BSON selector; // query object. See below for details.
+}
+*/
+DeleteCommand.prototype.toBinary = function() {
+ // Calculate total length of the document
+ var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + this.db.bson.calculateObjectSize(this.selector, false, true) + (4 * 4);
+ // Let's build the single pass buffer command
+ var _index = 0;
+ var _command = new Buffer(totalLengthOfCommand);
+ // Write the header information to the buffer
+ _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
+ _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
+ _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
+ _command[_index] = totalLengthOfCommand & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write the request ID
+ _command[_index + 3] = (this.requestId >> 24) & 0xff;
+ _command[_index + 2] = (this.requestId >> 16) & 0xff;
+ _command[_index + 1] = (this.requestId >> 8) & 0xff;
+ _command[_index] = this.requestId & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ // Write the op_code for the command
+ _command[_index + 3] = (DeleteCommand.OP_DELETE >> 24) & 0xff;
+ _command[_index + 2] = (DeleteCommand.OP_DELETE >> 16) & 0xff;
+ _command[_index + 1] = (DeleteCommand.OP_DELETE >> 8) & 0xff;
+ _command[_index] = DeleteCommand.OP_DELETE & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+
+ // Write the collection name to the command
+ _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
+ _command[_index - 1] = 0;
+
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+
+ // Document binary length
+ var documentLength = 0
+
+ // Serialize the selector
+ // If we are passing a raw buffer, do minimal validation
+ if(Buffer.isBuffer(this.selector)) {
+ documentLength = this.selector.length;
+ // Copy the data into the current buffer
+ this.selector.copy(_command, _index);
+ } else {
+ documentLength = this.db.bson.serializeWithBufferAndIndex(this.selector, this.checkKeys, _command, _index) - _index + 1;
+ }
+
+ // Write the length to the document
+ _command[_index + 3] = (documentLength >> 24) & 0xff;
+ _command[_index + 2] = (documentLength >> 16) & 0xff;
+ _command[_index + 1] = (documentLength >> 8) & 0xff;
+ _command[_index] = documentLength & 0xff;
+ // Update index in buffer
+ _index = _index + documentLength;
+ // Add terminating 0 for the object
+ _command[_index - 1] = 0;
+ return _command;
+}; \ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/commands/get_more_command.js b/node_modules/mongodb/lib/mongodb/commands/get_more_command.js
new file mode 100644
index 0000000..d3aac02
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/get_more_command.js
@@ -0,0 +1,83 @@
+var BaseCommand = require('./base_command').BaseCommand,
+ inherits = require('util').inherits,
+ binaryutils = require('../utils');
+
+/**
+ Get More Document Command
+**/
+var GetMoreCommand = exports.GetMoreCommand = function(db, collectionName, numberToReturn, cursorId) {
+ BaseCommand.call(this);
+
+ this.collectionName = collectionName;
+ this.numberToReturn = numberToReturn;
+ this.cursorId = cursorId;
+ this.db = db;
+};
+
+inherits(GetMoreCommand, BaseCommand);
+
+GetMoreCommand.OP_GET_MORE = 2005;
+
+GetMoreCommand.prototype.toBinary = function() {
+ // Calculate total length of the document
+ var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + 8 + (4 * 4);
+ // Let's build the single pass buffer command
+ var _index = 0;
+ var _command = new Buffer(totalLengthOfCommand);
+ // Write the header information to the buffer
+ _command[_index++] = totalLengthOfCommand & 0xff;
+ _command[_index++] = (totalLengthOfCommand >> 8) & 0xff;
+ _command[_index++] = (totalLengthOfCommand >> 16) & 0xff;
+ _command[_index++] = (totalLengthOfCommand >> 24) & 0xff;
+
+ // Write the request ID
+ _command[_index++] = this.requestId & 0xff;
+ _command[_index++] = (this.requestId >> 8) & 0xff;
+ _command[_index++] = (this.requestId >> 16) & 0xff;
+ _command[_index++] = (this.requestId >> 24) & 0xff;
+
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+
+ // Write the op_code for the command
+ _command[_index++] = GetMoreCommand.OP_GET_MORE & 0xff;
+ _command[_index++] = (GetMoreCommand.OP_GET_MORE >> 8) & 0xff;
+ _command[_index++] = (GetMoreCommand.OP_GET_MORE >> 16) & 0xff;
+ _command[_index++] = (GetMoreCommand.OP_GET_MORE >> 24) & 0xff;
+
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+
+ // Write the collection name to the command
+ _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
+ _command[_index - 1] = 0;
+
+ // Number of documents to return
+ _command[_index++] = this.numberToReturn & 0xff;
+ _command[_index++] = (this.numberToReturn >> 8) & 0xff;
+ _command[_index++] = (this.numberToReturn >> 16) & 0xff;
+ _command[_index++] = (this.numberToReturn >> 24) & 0xff;
+
+ // Encode the cursor id
+ var low_bits = this.cursorId.getLowBits();
+ // Encode low bits
+ _command[_index++] = low_bits & 0xff;
+ _command[_index++] = (low_bits >> 8) & 0xff;
+ _command[_index++] = (low_bits >> 16) & 0xff;
+ _command[_index++] = (low_bits >> 24) & 0xff;
+
+ var high_bits = this.cursorId.getHighBits();
+ // Encode high bits
+ _command[_index++] = high_bits & 0xff;
+ _command[_index++] = (high_bits >> 8) & 0xff;
+ _command[_index++] = (high_bits >> 16) & 0xff;
+ _command[_index++] = (high_bits >> 24) & 0xff;
+ // Return command
+ return _command;
+}; \ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/commands/insert_command.js b/node_modules/mongodb/lib/mongodb/commands/insert_command.js
new file mode 100644
index 0000000..3036a02
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/insert_command.js
@@ -0,0 +1,141 @@
+var BaseCommand = require('./base_command').BaseCommand,
+ inherits = require('util').inherits;
+
+/**
+ Insert Document Command
+**/
+var InsertCommand = exports.InsertCommand = function(db, collectionName, checkKeys, options) {
+ BaseCommand.call(this);
+
+ this.collectionName = collectionName;
+ this.documents = [];
+ this.checkKeys = checkKeys == null ? true : checkKeys;
+ this.db = db;
+ this.flags = 0;
+ this.serializeFunctions = false;
+
+ // Ensure valid options hash
+ options = options == null ? {} : options;
+
+ // Check if we have keepGoing set -> set flag if it's the case
+ if(options['keepGoing'] != null && options['keepGoing']) {
+ // This will finish inserting all non-index violating documents even if it returns an error
+ this.flags = 1;
+ }
+
+ // Let us defined on a command basis if we want functions to be serialized or not
+ if(options['serializeFunctions'] != null && options['serializeFunctions']) {
+ this.serializeFunctions = true;
+ }
+};
+
+inherits(InsertCommand, BaseCommand);
+
+// OpCodes
+InsertCommand.OP_INSERT = 2002;
+
+InsertCommand.prototype.add = function(document) {
+ if(Buffer.isBuffer(document)) {
+ var object_size = document[0] | document[1] << 8 | document[2] << 16 | document[3] << 24;
+ if(object_size != document.length) {
+ var error = new Error("insert raw message size does not match message header size [" + document.length + "] != [" + object_size + "]");
+ error.name = 'MongoError';
+ throw error;
+ }
+ }
+
+ this.documents.push(document);
+ return this;
+};
+
+/*
+struct {
+ MsgHeader header; // standard message header
+ int32 ZERO; // 0 - reserved for future use
+ cstring fullCollectionName; // "dbname.collectionname"
+ BSON[] documents; // one or more documents to insert into the collection
+}
+*/
+InsertCommand.prototype.toBinary = function() {
+ // Calculate total length of the document
+ var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + (4 * 4);
+ // var docLength = 0
+ for(var i = 0; i < this.documents.length; i++) {
+ if(Buffer.isBuffer(this.documents[i])) {
+ totalLengthOfCommand += this.documents[i].length;
+ } else {
+ // Calculate size of document
+ totalLengthOfCommand += this.db.bson.calculateObjectSize(this.documents[i], this.serializeFunctions, true);
+ }
+ }
+
+ // Let's build the single pass buffer command
+ var _index = 0;
+ var _command = new Buffer(totalLengthOfCommand);
+ // Write the header information to the buffer
+ _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
+ _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
+ _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
+ _command[_index] = totalLengthOfCommand & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write the request ID
+ _command[_index + 3] = (this.requestId >> 24) & 0xff;
+ _command[_index + 2] = (this.requestId >> 16) & 0xff;
+ _command[_index + 1] = (this.requestId >> 8) & 0xff;
+ _command[_index] = this.requestId & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ // Write the op_code for the command
+ _command[_index + 3] = (InsertCommand.OP_INSERT >> 24) & 0xff;
+ _command[_index + 2] = (InsertCommand.OP_INSERT >> 16) & 0xff;
+ _command[_index + 1] = (InsertCommand.OP_INSERT >> 8) & 0xff;
+ _command[_index] = InsertCommand.OP_INSERT & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write flags if any
+ _command[_index + 3] = (this.flags >> 24) & 0xff;
+ _command[_index + 2] = (this.flags >> 16) & 0xff;
+ _command[_index + 1] = (this.flags >> 8) & 0xff;
+ _command[_index] = this.flags & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write the collection name to the command
+ _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
+ _command[_index - 1] = 0;
+
+ // Write all the bson documents to the buffer at the index offset
+ for(var i = 0; i < this.documents.length; i++) {
+ // Document binary length
+ var documentLength = 0
+ var object = this.documents[i];
+
+ // Serialize the selector
+ // If we are passing a raw buffer, do minimal validation
+ if(Buffer.isBuffer(object)) {
+ documentLength = object.length;
+ // Copy the data into the current buffer
+ object.copy(_command, _index);
+ } else {
+ // Serialize the document straight to the buffer
+ documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1;
+ }
+
+ // Write the length to the document
+ _command[_index + 3] = (documentLength >> 24) & 0xff;
+ _command[_index + 2] = (documentLength >> 16) & 0xff;
+ _command[_index + 1] = (documentLength >> 8) & 0xff;
+ _command[_index] = documentLength & 0xff;
+ // Update index in buffer
+ _index = _index + documentLength;
+ // Add terminating 0 for the object
+ _command[_index - 1] = 0;
+ }
+
+ return _command;
+};
diff --git a/node_modules/mongodb/lib/mongodb/commands/kill_cursor_command.js b/node_modules/mongodb/lib/mongodb/commands/kill_cursor_command.js
new file mode 100644
index 0000000..d8ccb0c
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/kill_cursor_command.js
@@ -0,0 +1,98 @@
+var BaseCommand = require('./base_command').BaseCommand,
+ inherits = require('util').inherits,
+ binaryutils = require('../utils');
+
+/**
+ Insert Document Command
+**/
+var KillCursorCommand = exports.KillCursorCommand = function(db, cursorIds) {
+ BaseCommand.call(this);
+
+ this.cursorIds = cursorIds;
+ this.db = db;
+};
+
+inherits(KillCursorCommand, BaseCommand);
+
+KillCursorCommand.OP_KILL_CURSORS = 2007;
+
+/*
+struct {
+ MsgHeader header; // standard message header
+ int32 ZERO; // 0 - reserved for future use
+ int32 numberOfCursorIDs; // number of cursorIDs in message
+ int64[] cursorIDs; // array of cursorIDs to close
+}
+*/
+KillCursorCommand.prototype.toBinary = function() {
+ // Calculate total length of the document
+ var totalLengthOfCommand = 4 + 4 + (4 * 4) + (this.cursorIds.length * 8);
+ // Let's build the single pass buffer command
+ var _index = 0;
+ var _command = new Buffer(totalLengthOfCommand);
+ // Write the header information to the buffer
+ _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
+ _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
+ _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
+ _command[_index] = totalLengthOfCommand & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write the request ID
+ _command[_index + 3] = (this.requestId >> 24) & 0xff;
+ _command[_index + 2] = (this.requestId >> 16) & 0xff;
+ _command[_index + 1] = (this.requestId >> 8) & 0xff;
+ _command[_index] = this.requestId & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ // Write the op_code for the command
+ _command[_index + 3] = (KillCursorCommand.OP_KILL_CURSORS >> 24) & 0xff;
+ _command[_index + 2] = (KillCursorCommand.OP_KILL_CURSORS >> 16) & 0xff;
+ _command[_index + 1] = (KillCursorCommand.OP_KILL_CURSORS >> 8) & 0xff;
+ _command[_index] = KillCursorCommand.OP_KILL_CURSORS & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+
+ // Number of cursors to kill
+ var numberOfCursors = this.cursorIds.length;
+ _command[_index + 3] = (numberOfCursors >> 24) & 0xff;
+ _command[_index + 2] = (numberOfCursors >> 16) & 0xff;
+ _command[_index + 1] = (numberOfCursors >> 8) & 0xff;
+ _command[_index] = numberOfCursors & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Encode all the cursors
+ for(var i = 0; i < this.cursorIds.length; i++) {
+ // Encode the cursor id
+ var low_bits = this.cursorIds[i].getLowBits();
+ // Encode low bits
+ _command[_index + 3] = (low_bits >> 24) & 0xff;
+ _command[_index + 2] = (low_bits >> 16) & 0xff;
+ _command[_index + 1] = (low_bits >> 8) & 0xff;
+ _command[_index] = low_bits & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ var high_bits = this.cursorIds[i].getHighBits();
+ // Encode high bits
+ _command[_index + 3] = (high_bits >> 24) & 0xff;
+ _command[_index + 2] = (high_bits >> 16) & 0xff;
+ _command[_index + 1] = (high_bits >> 8) & 0xff;
+ _command[_index] = high_bits & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ }
+
+ return _command;
+}; \ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/commands/query_command.js b/node_modules/mongodb/lib/mongodb/commands/query_command.js
new file mode 100644
index 0000000..e07a8aa
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/query_command.js
@@ -0,0 +1,209 @@
+var BaseCommand = require('./base_command').BaseCommand,
+ inherits = require('util').inherits;
+
+/**
+ Insert Document Command
+**/
+var QueryCommand = exports.QueryCommand = function(db, collectionName, queryOptions, numberToSkip, numberToReturn, query, returnFieldSelector, options) {
+ BaseCommand.call(this);
+
+ // Validate correctness off the selector
+ var object = query;
+ if(Buffer.isBuffer(object)) {
+ var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
+ if(object_size != object.length) {
+ var error = new Error("query selector raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
+ error.name = 'MongoError';
+ throw error;
+ }
+ }
+
+ var object = returnFieldSelector;
+ if(Buffer.isBuffer(object)) {
+ var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
+ if(object_size != object.length) {
+ var error = new Error("query fields raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
+ error.name = 'MongoError';
+ throw error;
+ }
+ }
+
+ // Make sure we don't get a null exception
+ options = options == null ? {} : options;
+ // Set up options
+ this.collectionName = collectionName;
+ this.queryOptions = queryOptions;
+ this.numberToSkip = numberToSkip;
+ this.numberToReturn = numberToReturn;
+ this.query = query;
+ this.returnFieldSelector = returnFieldSelector;
+ this.db = db;
+
+ // Let us defined on a command basis if we want functions to be serialized or not
+ if(options['serializeFunctions'] != null && options['serializeFunctions']) {
+ this.serializeFunctions = true;
+ }
+};
+
+inherits(QueryCommand, BaseCommand);
+
+QueryCommand.OP_QUERY = 2004;
+
+/*
+struct {
+ MsgHeader header; // standard message header
+ int32 opts; // query options. See below for details.
+ cstring fullCollectionName; // "dbname.collectionname"
+ int32 numberToSkip; // number of documents to skip when returning results
+ int32 numberToReturn; // number of documents to return in the first OP_REPLY
+ BSON query ; // query object. See below for details.
+ [ BSON returnFieldSelector; ] // OPTIONAL : selector indicating the fields to return. See below for details.
+}
+*/
+QueryCommand.prototype.toBinary = function() {
+ var totalLengthOfCommand = 0;
+ // Calculate total length of the document
+ if(Buffer.isBuffer(this.query)) {
+ totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + 4 + this.query.length + (4 * 4);
+ } else {
+ totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + 4 + this.db.bson.calculateObjectSize(this.query, this.serializeFunctions, true) + (4 * 4);
+ }
+
+ // Calculate extra fields size
+ if(this.returnFieldSelector != null && !(Buffer.isBuffer(this.returnFieldSelector))) {
+ if(Object.keys(this.returnFieldSelector).length > 0) {
+ totalLengthOfCommand += this.db.bson.calculateObjectSize(this.returnFieldSelector, this.serializeFunctions, true);
+ }
+ } else if(Buffer.isBuffer(this.returnFieldSelector)) {
+ totalLengthOfCommand += this.returnFieldSelector.length;
+ }
+
+ // Let's build the single pass buffer command
+ var _index = 0;
+ var _command = new Buffer(totalLengthOfCommand);
+ // Write the header information to the buffer
+ _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
+ _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
+ _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
+ _command[_index] = totalLengthOfCommand & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write the request ID
+ _command[_index + 3] = (this.requestId >> 24) & 0xff;
+ _command[_index + 2] = (this.requestId >> 16) & 0xff;
+ _command[_index + 1] = (this.requestId >> 8) & 0xff;
+ _command[_index] = this.requestId & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ // Write the op_code for the command
+ _command[_index + 3] = (QueryCommand.OP_QUERY >> 24) & 0xff;
+ _command[_index + 2] = (QueryCommand.OP_QUERY >> 16) & 0xff;
+ _command[_index + 1] = (QueryCommand.OP_QUERY >> 8) & 0xff;
+ _command[_index] = QueryCommand.OP_QUERY & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Write the query options
+ _command[_index + 3] = (this.queryOptions >> 24) & 0xff;
+ _command[_index + 2] = (this.queryOptions >> 16) & 0xff;
+ _command[_index + 1] = (this.queryOptions >> 8) & 0xff;
+ _command[_index] = this.queryOptions & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Write the collection name to the command
+ _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
+ _command[_index - 1] = 0;
+
+ // Write the number of documents to skip
+ _command[_index + 3] = (this.numberToSkip >> 24) & 0xff;
+ _command[_index + 2] = (this.numberToSkip >> 16) & 0xff;
+ _command[_index + 1] = (this.numberToSkip >> 8) & 0xff;
+ _command[_index] = this.numberToSkip & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Write the number of documents to return
+ _command[_index + 3] = (this.numberToReturn >> 24) & 0xff;
+ _command[_index + 2] = (this.numberToReturn >> 16) & 0xff;
+ _command[_index + 1] = (this.numberToReturn >> 8) & 0xff;
+ _command[_index] = this.numberToReturn & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Document binary length
+ var documentLength = 0
+ var object = this.query;
+
+ // Serialize the selector
+ if(Buffer.isBuffer(object)) {
+ documentLength = object.length;
+ // Copy the data into the current buffer
+ object.copy(_command, _index);
+ } else {
+ // Serialize the document straight to the buffer
+ documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1;
+ }
+
+ // Write the length to the document
+ _command[_index + 3] = (documentLength >> 24) & 0xff;
+ _command[_index + 2] = (documentLength >> 16) & 0xff;
+ _command[_index + 1] = (documentLength >> 8) & 0xff;
+ _command[_index] = documentLength & 0xff;
+ // Update index in buffer
+ _index = _index + documentLength;
+ // Add terminating 0 for the object
+ _command[_index - 1] = 0;
+
+ // Push field selector if available
+ if(this.returnFieldSelector != null && !(Buffer.isBuffer(this.returnFieldSelector))) {
+ if(Object.keys(this.returnFieldSelector).length > 0) {
+ var documentLength = this.db.bson.serializeWithBufferAndIndex(this.returnFieldSelector, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1;
+ // Write the length to the document
+ _command[_index + 3] = (documentLength >> 24) & 0xff;
+ _command[_index + 2] = (documentLength >> 16) & 0xff;
+ _command[_index + 1] = (documentLength >> 8) & 0xff;
+ _command[_index] = documentLength & 0xff;
+ // Update index in buffer
+ _index = _index + documentLength;
+ // Add terminating 0 for the object
+ _command[_index - 1] = 0;
+ }
+ } if(this.returnFieldSelector != null && Buffer.isBuffer(this.returnFieldSelector)) {
+ // Document binary length
+ var documentLength = 0
+ var object = this.returnFieldSelector;
+
+ // Serialize the selector
+ documentLength = object.length;
+ // Copy the data into the current buffer
+ object.copy(_command, _index);
+
+ // Write the length to the document
+ _command[_index + 3] = (documentLength >> 24) & 0xff;
+ _command[_index + 2] = (documentLength >> 16) & 0xff;
+ _command[_index + 1] = (documentLength >> 8) & 0xff;
+ _command[_index] = documentLength & 0xff;
+ // Update index in buffer
+ _index = _index + documentLength;
+ // Add terminating 0 for the object
+ _command[_index - 1] = 0;
+ }
+
+ // Return finished command
+ return _command;
+};
+
+// Constants
+QueryCommand.OPTS_NONE = 0;
+QueryCommand.OPTS_TAILABLE_CURSOR = 2;
+QueryCommand.OPTS_SLAVE = 4;
+QueryCommand.OPTS_OPLOG_REPLY = 8;
+QueryCommand.OPTS_NO_CURSOR_TIMEOUT = 16;
+QueryCommand.OPTS_AWAIT_DATA = 32;
+QueryCommand.OPTS_EXHAUST = 64; \ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/commands/update_command.js b/node_modules/mongodb/lib/mongodb/commands/update_command.js
new file mode 100644
index 0000000..9829dea
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/update_command.js
@@ -0,0 +1,174 @@
+var BaseCommand = require('./base_command').BaseCommand,
+ inherits = require('util').inherits;
+
+/**
+ Update Document Command
+**/
+var UpdateCommand = exports.UpdateCommand = function(db, collectionName, spec, document, options) {
+ BaseCommand.call(this);
+
+ var object = spec;
+ if(Buffer.isBuffer(object)) {
+ var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
+ if(object_size != object.length) {
+ var error = new Error("update spec raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
+ error.name = 'MongoError';
+ throw error;
+ }
+ }
+
+ var object = document;
+ if(Buffer.isBuffer(object)) {
+ var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
+ if(object_size != object.length) {
+ var error = new Error("update document raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
+ error.name = 'MongoError';
+ throw error;
+ }
+ }
+
+ this.collectionName = collectionName;
+ this.spec = spec;
+ this.document = document;
+ this.db = db;
+ this.serializeFunctions = false;
+
+ // Generate correct flags
+ var db_upsert = 0;
+ var db_multi_update = 0;
+ db_upsert = options != null && options['upsert'] != null ? (options['upsert'] == true ? 1 : 0) : db_upsert;
+ db_multi_update = options != null && options['multi'] != null ? (options['multi'] == true ? 1 : 0) : db_multi_update;
+
+ // Flags
+ this.flags = parseInt(db_multi_update.toString() + db_upsert.toString(), 2);
+ // Let us defined on a command basis if we want functions to be serialized or not
+ if(options['serializeFunctions'] != null && options['serializeFunctions']) {
+ this.serializeFunctions = true;
+ }
+};
+
+inherits(UpdateCommand, BaseCommand);
+
+UpdateCommand.OP_UPDATE = 2001;
+
+/*
+struct {
+ MsgHeader header; // standard message header
+ int32 ZERO; // 0 - reserved for future use
+ cstring fullCollectionName; // "dbname.collectionname"
+ int32 flags; // bit vector. see below
+ BSON spec; // the query to select the document
+ BSON document; // the document data to update with or insert
+}
+*/
+UpdateCommand.prototype.toBinary = function() {
+ // Calculate total length of the document
+ var totalLengthOfCommand = 4 + Buffer.byteLength(this.collectionName) + 1 + 4 + this.db.bson.calculateObjectSize(this.spec, false, true) +
+ this.db.bson.calculateObjectSize(this.document, this.serializeFunctions, true) + (4 * 4);
+
+ // Let's build the single pass buffer command
+ var _index = 0;
+ var _command = new Buffer(totalLengthOfCommand);
+ // Write the header information to the buffer
+ _command[_index + 3] = (totalLengthOfCommand >> 24) & 0xff;
+ _command[_index + 2] = (totalLengthOfCommand >> 16) & 0xff;
+ _command[_index + 1] = (totalLengthOfCommand >> 8) & 0xff;
+ _command[_index] = totalLengthOfCommand & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write the request ID
+ _command[_index + 3] = (this.requestId >> 24) & 0xff;
+ _command[_index + 2] = (this.requestId >> 16) & 0xff;
+ _command[_index + 1] = (this.requestId >> 8) & 0xff;
+ _command[_index] = this.requestId & 0xff;
+ // Adjust index
+ _index = _index + 4;
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ // Write the op_code for the command
+ _command[_index + 3] = (UpdateCommand.OP_UPDATE >> 24) & 0xff;
+ _command[_index + 2] = (UpdateCommand.OP_UPDATE >> 16) & 0xff;
+ _command[_index + 1] = (UpdateCommand.OP_UPDATE >> 8) & 0xff;
+ _command[_index] = UpdateCommand.OP_UPDATE & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Write zero
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+ _command[_index++] = 0;
+
+ // Write the collection name to the command
+ _index = _index + _command.write(this.collectionName, _index, 'utf8') + 1;
+ _command[_index - 1] = 0;
+
+ // Write the update flags
+ _command[_index + 3] = (this.flags >> 24) & 0xff;
+ _command[_index + 2] = (this.flags >> 16) & 0xff;
+ _command[_index + 1] = (this.flags >> 8) & 0xff;
+ _command[_index] = this.flags & 0xff;
+ // Adjust index
+ _index = _index + 4;
+
+ // Document binary length
+ var documentLength = 0
+ var object = this.spec;
+
+ // Serialize the selector
+ // If we are passing a raw buffer, do minimal validation
+ if(Buffer.isBuffer(object)) {
+ var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
+ if(object_size != object.length) throw new Error("raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
+ documentLength = object.length;
+ // Copy the data into the current buffer
+ object.copy(_command, _index);
+ } else {
+ documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, false) - _index + 1;
+ }
+
+ // Write the length to the document
+ _command[_index + 3] = (documentLength >> 24) & 0xff;
+ _command[_index + 2] = (documentLength >> 16) & 0xff;
+ _command[_index + 1] = (documentLength >> 8) & 0xff;
+ _command[_index] = documentLength & 0xff;
+ // Update index in buffer
+ _index = _index + documentLength;
+ // Add terminating 0 for the object
+ _command[_index - 1] = 0;
+
+ // Document binary length
+ var documentLength = 0
+ var object = this.document;
+
+ // Serialize the document
+ // If we are passing a raw buffer, do minimal validation
+ if(Buffer.isBuffer(object)) {
+ var object_size = object[0] | object[1] << 8 | object[2] << 16 | object[3] << 24;
+ if(object_size != object.length) throw new Error("raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
+ documentLength = object.length;
+ // Copy the data into the current buffer
+ object.copy(_command, _index);
+ } else {
+ documentLength = this.db.bson.serializeWithBufferAndIndex(object, this.checkKeys, _command, _index, this.serializeFunctions) - _index + 1;
+ }
+
+ // Write the length to the document
+ _command[_index + 3] = (documentLength >> 24) & 0xff;
+ _command[_index + 2] = (documentLength >> 16) & 0xff;
+ _command[_index + 1] = (documentLength >> 8) & 0xff;
+ _command[_index] = documentLength & 0xff;
+ // Update index in buffer
+ _index = _index + documentLength;
+ // Add terminating 0 for the object
+ _command[_index - 1] = 0;
+
+ return _command;
+};
+
+// Constants
+UpdateCommand.DB_UPSERT = 0;
+UpdateCommand.DB_MULTI_UPDATE = 1; \ No newline at end of file