diff --git a/app.js b/app.js
new file mode 100644
index 0000000..deeca0b
--- /dev/null
+++ b/app.js
@@ -0,0 +1,223 @@
+
+/**
+ * Module dependencies.
+ */
+var fs = require('fs');
+var path = require('path');
+var mongo = require('mongodb');
+var BSON = mongo.BSONPure;
+var db = require('mongoskin').db('localhost:27017/test');
+var testcollection = db.collection('testcollection');
+var exercisecollection = db.collection('exercisecollection');
+var expressocollection = db.collection('expressocollection');
+var hrdatacollection = db.collection('hrdatacollection');
+var util = require('util');
+var formidable = require('formidable');
+var xml2js = require('xml2js');
+var parser = new xml2js.Parser();
+var dateFormat = require('dateformat');
+
+var app = require('http').createServer(function handler(request, response) {
+
+ console.log('request starting...;' + request.url);
+
+ // switch(request.url) {
+ // case '/upload':
+ // var form = new formidable.IncomingForm(),
+ // files = [],
+ // fields = [];
+
+ // var tempdirectory = "/tmp/";
+
+ // // //tempdirectory changes if the operating system is windows
+ // if(process.platform == "windows")
+ // {
+ // tempdirectory = "c:\\temp\\";
+ // }
+ // form.uploaddir = tempdirectory;
+
+ // //tempDirectory = "c:\\Temp\\";
+ // //form.uploadDir = tempDirectory;
+
+ // form.on('error', function(err) {
+ // response.writeHead(200, {'content-type': 'text/plain'});
+ // response.end('error:\n\n'+util.inspect(err));
+ // });
+ // form.on('field', function(field, value) {
+ // console.log(field, value);
+ // fields.push([field, value]);
+ // });
+ // form.on('file', function(field, file) {
+ // console.log(field, file);
+ // files.push([field, file]);
+ // });
+ // form.on('end', function() {
+ // console.log('-> upload done');
+ // response.writeHead(200, {'content-type': 'text/plain'});
+ // response.write('received fields:\n\n '+util.inspect(fields));
+ // response.write('\n\n');
+ // response.write('received files:\n\n '+util.inspect(files));
+ // });
+
+ // form.parse(request, function(err, fields, files) {
+ // console.log('-> uploaded -' + files.upload.path);
+ // fs.readFile(files.upload.path, function(err, data) {
+ // parser.parseString(data, function (err, result) {
+ // response.write('received file contents:\n\n ');
+ // response.end(JSON.stringify(result));
+ // console.log('Done');
+
+ // //hrdatacollectionJSON.stringify(result)
+ // var data = JSON.stringify(result);
+ // var buf1 = new Buffer(12);
+ // var dataid = JSON.parse(data).Activities.Activity.Id;
+ // var datadate = Date.parse(dataid);
+ // //console.log('TCX ID' + JSON.parse(data).Activities.Activity.Id);
+ // console.log('TCX ID ' + datadate);
+ // var document_id = new BSON.ObjectID(datadate);
+ // console.log('inserted BSONID ' + document_id);
+ // hrdatacollection.update({_id:document_id}, data,{upsert:true} , function(err, result) {
+ // if (err) throw err;
+ // });
+
+ // });
+ // });
+ // });
+ // break;
+ // default:
+ var filePath = '.' + request.url;
+ if (filePath == './')
+ filePath = './index.html';
+
+ var extname = path.extname(filePath);
+ var contentType = 'text/html';
+ switch (extname) {
+ case '.js':
+ contentType = 'text/javascript';
+ break;
+ case '.css':
+ contentType = 'text/css';
+ break;
+ }
+ path.exists(filePath, function(exists) {
+
+ if (exists) {
+ fs.readFile(filePath, function(error, content) {
+ if (error) {
+ response.writeHead(500);
+ response.end();
+ }
+ else {
+ response.writeHead(200, { 'Content-Type': contentType });
+ response.end(content, 'utf-8');
+ }
+ });
+ }
+ else {
+ response.writeHead(404);
+ response.end();
+ }
+ });
+ // }
+}).listen(3000);
+
+
+var io = require('socket.io');
+io = io.listen(app);
+io.configure('development', function(){
+io.set("transports", ["websocket"]);
+});
+
+io.sockets.on('connection', function(socket) {
+ console.log('Client connected');
+
+ socket.on('getactivites', function(data) {
+ console.log('getactivites');
+ testcollection.find().toArray(function(err, result) {
+ if (err) throw err;
+ socket.emit('populateactivities', result);
+ });
+ });
+///////////////////////////////////////
+ socket.on('getactivitybyid', function(id) {
+ console.log('getactivitybyid');
+ testcollection.findById(id, function(err, result) {
+ if (err) throw err;
+ socket.emit('populateactivitybyid', result);
+ });
+ });
+
+
+////////////////////////
+ socket.on('addactivity', function(data, docid) {
+ console.log('addactivity' + docid);
+ if (docid === null) {
+ var document_id = new BSON.ObjectID();
+ }
+ else {
+ var document_id = new BSON.ObjectID(docid);
+ }
+ //var document_id = new BSON.ObjectID(docid);
+ console.log('inserted BSONID' + document_id);
+ testcollection.update({_id:document_id}, data,{upsert:true} , function(err, result) {
+ if (err) throw err;
+ exercisecollection.find().toArray(function(err, result) {
+ if (err) throw err;
+ socket.emit('populateexercises', result);
+ });
+ });
+
+
+ });
+
+/////////////////////
+ socket.on('delactivity', function(id) {
+ testcollection.removeById(id,function(err, reply){
+ if (err) throw err;
+ testcollection.find().toArray(function(err, result) {
+ if (err) throw err;
+ socket.emit('populateactivities', result);
+ });
+ });
+ });
+ ///////////////////
+ socket.on('getexercises', function(data) {
+ console.log('emit exercises');
+ exercisecollection.find().toArray(function(err, result) {
+ if (err) throw err;
+ socket.emit('populateexercises', result);
+ });
+ });
+ socket.on('updateexercises', function(data, docid) {
+ console.log('updateexecises' + JSON.stringify(data));
+ if (docid == 'undefined') {
+ console.log('edited updateexecises' + JSON.stringify(data));
+ exercisecollection.insert(data, function(err, result) {
+ if (err) throw err;
+ exercisecollection.find().toArray(function(err, result) {
+ if (err) throw err;
+ socket.emit('populateexercises', result);
+ });
+ });
+ }
+ else {
+ var document_id = new BSON.ObjectID(docid);
+ exercisecollection.update({_id:document_id}, data,{upsert:true} , function(err, result) {
+ if (err) throw err;
+ exercisecollection.find().toArray(function(err, result) {
+ if (err) throw err;
+ console.log('populateexercises');
+ socket.emit('populateexercises', result);
+ });
+ });
+
+ }
+ });
+
+
+
+////////////////
+});
+
+
+
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..1e307c5
--- /dev/null
+++ b/index.html
@@ -0,0 +1,117 @@
+
+
+
+
+ BodyREP Demo
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
BodyREP Demo
+
+
+-
+
+
+ delete
+
+
+
+
+
+
+
+
+
+
+
+
+ Cancel
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/node_modules/dateformat/Readme.md b/node_modules/dateformat/Readme.md
new file mode 100644
index 0000000..76ccf1d
--- /dev/null
+++ b/node_modules/dateformat/Readme.md
@@ -0,0 +1,67 @@
+# dateformat
+
+A node.js package for Steven Levithan's excellent [dateFormat()][dateformat] function.
+
+## Modifications
+
+* Removed the `Date.prototype.format` method. Sorry folks, but extending native prototypes is for suckers.
+* Added a `module.exports = dateFormat;` statement at the bottom
+
+## Usage
+
+As taken from Steven's post, modified to match the Modifications listed above:
+
+ var dateFormat = require('dateformat');
+ var now = new Date();
+
+ // Basic usage
+ dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT");
+ // Saturday, June 9th, 2007, 5:46:21 PM
+
+ // You can use one of several named masks
+ dateFormat(now, "isoDateTime");
+ // 2007-06-09T17:46:21
+
+ // ...Or add your own
+ dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
+ dateFormat(now, "hammerTime");
+ // 17:46! Can't touch this!
+
+ // When using the standalone dateFormat function,
+ // you can also provide the date as a string
+ dateFormat("Jun 9 2007", "fullDate");
+ // Saturday, June 9, 2007
+
+ // Note that if you don't include the mask argument,
+ // dateFormat.masks.default is used
+ dateFormat(now);
+ // Sat Jun 09 2007 17:46:21
+
+ // And if you don't include the date argument,
+ // the current date and time is used
+ dateFormat();
+ // Sat Jun 09 2007 17:46:22
+
+ // You can also skip the date argument (as long as your mask doesn't
+ // contain any numbers), in which case the current date/time is used
+ dateFormat("longTime");
+ // 5:46:22 PM EST
+
+ // And finally, you can convert local time to UTC time. Simply pass in
+ // true as an additional argument (no argument skipping allowed in this case):
+ dateFormat(now, "longTime", true);
+ // 10:46:21 PM UTC
+
+ // ...Or add the prefix "UTC:" to your mask.
+ dateFormat(now, "UTC:h:MM:ss TT Z");
+ // 10:46:21 PM UTC
+
+ // You can also get the ISO 8601 week of the year:
+ dateFormat(now, "W");
+ // 42
+## License
+
+(c) 2007-2009 Steven Levithan [stevenlevithan.com][stevenlevithan], MIT license.
+
+[dateformat]: http://blog.stevenlevithan.com/archives/date-time-format
+[stevenlevithan]: http://stevenlevithan.com/
diff --git a/node_modules/dateformat/lib/dateformat.js b/node_modules/dateformat/lib/dateformat.js
new file mode 100644
index 0000000..c7f4fb2
--- /dev/null
+++ b/node_modules/dateformat/lib/dateformat.js
@@ -0,0 +1,165 @@
+/*
+ * Date Format 1.2.3
+ * (c) 2007-2009 Steven Levithan
+ * MIT license
+ *
+ * Includes enhancements by Scott Trenda
+ * and Kris Kowal
+ *
+ * Accepts a date, a mask, or a date and a mask.
+ * Returns a formatted version of the given date.
+ * The date defaults to the current date/time.
+ * The mask defaults to dateFormat.masks.default.
+ */
+
+var dateFormat = function () {
+ var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZW]|"[^"]*"|'[^']*'/g,
+ timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
+ timezoneClip = /[^-+\dA-Z]/g,
+ pad = function (val, len) {
+ val = String(val);
+ len = len || 2;
+ while (val.length < len) val = "0" + val;
+ return val;
+ },
+ /**
+ * Get the ISO 8601 week number
+ * Based on comments from
+ * http://techblog.procurios.nl/k/n618/news/view/33796/14863/Calculate-ISO-8601-week-and-year-in-javascript.html
+ */
+ getWeek = function (date) {
+ // Remove time components of date
+ var targetThursday = new Date(date.getFullYear(), date.getMonth(), date.getDate());
+
+ // Change date to Thursday same week
+ targetThursday.setDate(targetThursday.getDate() - ((targetThursday.getDay() + 6) % 7) + 3);
+
+ // Take January 4th as it is always in week 1 (see ISO 8601)
+ var firstThursday = new Date(targetThursday.getFullYear(), 0, 4);
+
+ // Change date to Thursday same week
+ firstThursday.setDate(firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3);
+
+ // Check if daylight-saving-time-switch occured and correct for it
+ var ds = targetThursday.getTimezoneOffset() - firstThursday.getTimezoneOffset();
+ targetThursday.setHours(targetThursday.getHours() - ds);
+
+ // Number of weeks between target Thursday and first Thursday
+ var weekDiff = (targetThursday - firstThursday) / (86400000*7);
+ return 1 + weekDiff;
+ };
+
+ // Regexes and supporting functions are cached through closure
+ return function (date, mask, utc) {
+ var dF = dateFormat;
+
+ // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
+ if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
+ mask = date;
+ date = undefined;
+ }
+
+ date = date || new Date;
+
+ if(!(date instanceof Date)) {
+ date = new Date(date);
+ }
+
+ if (isNaN(date)) {
+ throw TypeError("Invalid date");
+ }
+
+ mask = String(dF.masks[mask] || mask || dF.masks["default"]);
+
+ // Allow setting the utc argument via the mask
+ if (mask.slice(0, 4) == "UTC:") {
+ mask = mask.slice(4);
+ utc = true;
+ }
+
+ var _ = utc ? "getUTC" : "get",
+ d = date[_ + "Date"](),
+ D = date[_ + "Day"](),
+ m = date[_ + "Month"](),
+ y = date[_ + "FullYear"](),
+ H = date[_ + "Hours"](),
+ M = date[_ + "Minutes"](),
+ s = date[_ + "Seconds"](),
+ L = date[_ + "Milliseconds"](),
+ o = utc ? 0 : date.getTimezoneOffset(),
+ W = getWeek(date),
+ flags = {
+ d: d,
+ dd: pad(d),
+ ddd: dF.i18n.dayNames[D],
+ dddd: dF.i18n.dayNames[D + 7],
+ m: m + 1,
+ mm: pad(m + 1),
+ mmm: dF.i18n.monthNames[m],
+ mmmm: dF.i18n.monthNames[m + 12],
+ yy: String(y).slice(2),
+ yyyy: y,
+ h: H % 12 || 12,
+ hh: pad(H % 12 || 12),
+ H: H,
+ HH: pad(H),
+ M: M,
+ MM: pad(M),
+ s: s,
+ ss: pad(s),
+ l: pad(L, 3),
+ L: pad(L > 99 ? Math.round(L / 10) : L),
+ t: H < 12 ? "a" : "p",
+ tt: H < 12 ? "am" : "pm",
+ T: H < 12 ? "A" : "P",
+ TT: H < 12 ? "AM" : "PM",
+ Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
+ o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
+ S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10],
+ W: W
+ };
+
+ return mask.replace(token, function ($0) {
+ return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
+ });
+ };
+}();
+
+// Some common format strings
+dateFormat.masks = {
+ "default": "ddd mmm dd yyyy HH:MM:ss",
+ shortDate: "m/d/yy",
+ mediumDate: "mmm d, yyyy",
+ longDate: "mmmm d, yyyy",
+ fullDate: "dddd, mmmm d, yyyy",
+ shortTime: "h:MM TT",
+ mediumTime: "h:MM:ss TT",
+ longTime: "h:MM:ss TT Z",
+ isoDate: "yyyy-mm-dd",
+ isoTime: "HH:MM:ss",
+ isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
+ isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
+};
+
+// Internationalization strings
+dateFormat.i18n = {
+ dayNames: [
+ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
+ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
+ ],
+ monthNames: [
+ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
+ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
+ ]
+};
+
+/*
+// For convenience...
+Date.prototype.format = function (mask, utc) {
+ return dateFormat(this, mask, utc);
+};
+*/
+
+if (typeof exports !== "undefined") {
+ module.exports = dateFormat;
+}
diff --git a/node_modules/dateformat/package.json b/node_modules/dateformat/package.json
new file mode 100644
index 0000000..6bfb7b6
--- /dev/null
+++ b/node_modules/dateformat/package.json
@@ -0,0 +1,33 @@
+{
+ "name": "dateformat",
+ "description": "A node.js package for Steven Levithan's excellent dateFormat() function.",
+ "maintainers": "Felix Geisendörfer ",
+ "homepage": "https://github.com/felixge/node-dateformat",
+ "author": {
+ "name": "Steven Levithan"
+ },
+ "contributors": [
+ {
+ "name": "Steven Levithan"
+ },
+ {
+ "name": "Felix Geisendörfer",
+ "email": "felix@debuggable.com"
+ },
+ {
+ "name": "Christoph Tavan",
+ "email": "dev@tavan.de"
+ }
+ ],
+ "version": "1.0.4-1.2.3",
+ "main": "./lib/dateformat",
+ "dependencies": {},
+ "devDependencies": {},
+ "engines": {
+ "node": "*"
+ },
+ "readme": "# dateformat\n\nA node.js package for Steven Levithan's excellent [dateFormat()][dateformat] function.\n\n## Modifications\n\n* Removed the `Date.prototype.format` method. Sorry folks, but extending native prototypes is for suckers.\n* Added a `module.exports = dateFormat;` statement at the bottom\n\n## Usage\n\nAs taken from Steven's post, modified to match the Modifications listed above:\n\n var dateFormat = require('dateformat');\n var now = new Date();\n\n // Basic usage\n dateFormat(now, \"dddd, mmmm dS, yyyy, h:MM:ss TT\");\n // Saturday, June 9th, 2007, 5:46:21 PM\n\n // You can use one of several named masks\n dateFormat(now, \"isoDateTime\");\n // 2007-06-09T17:46:21\n\n // ...Or add your own\n dateFormat.masks.hammerTime = 'HH:MM! \"Can\\'t touch this!\"';\n dateFormat(now, \"hammerTime\");\n // 17:46! Can't touch this!\n\n // When using the standalone dateFormat function,\n // you can also provide the date as a string\n dateFormat(\"Jun 9 2007\", \"fullDate\");\n // Saturday, June 9, 2007\n\n // Note that if you don't include the mask argument,\n // dateFormat.masks.default is used\n dateFormat(now);\n // Sat Jun 09 2007 17:46:21\n\n // And if you don't include the date argument,\n // the current date and time is used\n dateFormat();\n // Sat Jun 09 2007 17:46:22\n\n // You can also skip the date argument (as long as your mask doesn't\n // contain any numbers), in which case the current date/time is used\n dateFormat(\"longTime\");\n // 5:46:22 PM EST\n\n // And finally, you can convert local time to UTC time. Simply pass in\n // true as an additional argument (no argument skipping allowed in this case):\n dateFormat(now, \"longTime\", true);\n // 10:46:21 PM UTC\n\n // ...Or add the prefix \"UTC:\" to your mask.\n dateFormat(now, \"UTC:h:MM:ss TT Z\");\n // 10:46:21 PM UTC\n\n // You can also get the ISO 8601 week of the year:\n dateFormat(now, \"W\");\n // 42\n## License\n\n(c) 2007-2009 Steven Levithan [stevenlevithan.com][stevenlevithan], MIT license.\n\n[dateformat]: http://blog.stevenlevithan.com/archives/date-time-format\n[stevenlevithan]: http://stevenlevithan.com/\n",
+ "readmeFilename": "Readme.md",
+ "_id": "dateformat@1.0.4-1.2.3",
+ "_from": "dateformat@>= 0.0.1"
+}
diff --git a/node_modules/dateformat/test/basic.js b/node_modules/dateformat/test/basic.js
new file mode 100644
index 0000000..9602a72
--- /dev/null
+++ b/node_modules/dateformat/test/basic.js
@@ -0,0 +1,45 @@
+var dateFormat = require('./lib/dateformat.js');
+var now = new Date();
+
+// Basic usage
+console.log(dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT"));
+// Saturday, June 9th, 2007, 5:46:21 PM
+
+// You can use one of several named masks
+console.log(dateFormat(now, "isoDateTime"));
+// 2007-06-09T17:46:21
+
+// ...Or add your own
+dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"';
+console.log(dateFormat(now, "hammerTime"));
+// 17:46! Can't touch this!
+
+// When using the standalone dateFormat function,
+// you can also provide the date as a string
+console.log(dateFormat("Jun 9 2007", "fullDate"));
+// Saturday, June 9, 2007
+
+// Note that if you don't include the mask argument,
+// dateFormat.masks.default is used
+console.log(dateFormat(now));
+// Sat Jun 09 2007 17:46:21
+
+// And if you don't include the date argument,
+// the current date and time is used
+console.log(dateFormat());
+// Sat Jun 09 2007 17:46:22
+
+// You can also skip the date argument (as long as your mask doesn't
+// contain any numbers), in which case the current date/time is used
+console.log(dateFormat("longTime"));
+// 5:46:22 PM EST
+
+// And finally, you can convert local time to UTC time. Simply pass in
+// true as an additional argument (no argument skipping allowed in this case):
+console.log(dateFormat(now, "longTime", true));
+// 10:46:21 PM UTC
+
+// ...Or add the prefix "UTC:" to your mask.
+console.log(dateFormat(now, "UTC:h:MM:ss TT Z"));
+// 10:46:21 PM UTC
+
diff --git a/node_modules/dateformat/test/test_weekofyear.js b/node_modules/dateformat/test/test_weekofyear.js
new file mode 100644
index 0000000..d1ddbe8
--- /dev/null
+++ b/node_modules/dateformat/test/test_weekofyear.js
@@ -0,0 +1,4 @@
+var dateFormat = require('../lib/dateformat.js');
+
+var val = process.argv[2] || new Date();
+console.log(dateFormat(val, 'W'));
diff --git a/node_modules/dateformat/test/test_weekofyear.sh b/node_modules/dateformat/test/test_weekofyear.sh
new file mode 100644
index 0000000..3c3e69b
--- /dev/null
+++ b/node_modules/dateformat/test/test_weekofyear.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+# this just takes php's date() function as a reference to check if week of year
+# is calculated correctly in the range from 1970 .. 2038 by brute force...
+
+SEQ="seq"
+SYSTEM=`uname`
+if [ "$SYSTEM" = "Darwin" ]; then
+ SEQ="jot"
+fi
+
+for YEAR in {1970..2038}; do
+ for MONTH in {1..12}; do
+ DAYS=$(cal $MONTH $YEAR | egrep "28|29|30|31" |tail -1 |awk '{print $NF}')
+ for DAY in $( $SEQ $DAYS ); do
+ DATE=$YEAR-$MONTH-$DAY
+ echo -n $DATE ...
+ NODEVAL=$(node test_weekofyear.js $DATE)
+ PHPVAL=$(php -r "echo intval(date('W', strtotime('$DATE')));")
+ if [ "$NODEVAL" -ne "$PHPVAL" ]; then
+ echo "MISMATCH: node: $NODEVAL vs php: $PHPVAL for date $DATE"
+ else
+ echo " OK"
+ fi
+ done
+ done
+done
diff --git a/node_modules/formidable/.npmignore b/node_modules/formidable/.npmignore
new file mode 100644
index 0000000..4fbabb3
--- /dev/null
+++ b/node_modules/formidable/.npmignore
@@ -0,0 +1,4 @@
+/test/tmp/
+*.upload
+*.un~
+*.http
diff --git a/node_modules/formidable/.travis.yml b/node_modules/formidable/.travis.yml
new file mode 100644
index 0000000..f1d0f13
--- /dev/null
+++ b/node_modules/formidable/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.4
+ - 0.6
diff --git a/node_modules/formidable/Makefile b/node_modules/formidable/Makefile
new file mode 100644
index 0000000..8945872
--- /dev/null
+++ b/node_modules/formidable/Makefile
@@ -0,0 +1,14 @@
+SHELL := /bin/bash
+
+test:
+ @./test/run.js
+
+build: npm test
+
+npm:
+ npm install .
+
+clean:
+ rm test/tmp/*
+
+.PHONY: test clean build
diff --git a/node_modules/formidable/Readme.md b/node_modules/formidable/Readme.md
new file mode 100644
index 0000000..a5ca104
--- /dev/null
+++ b/node_modules/formidable/Readme.md
@@ -0,0 +1,311 @@
+# Formidable
+
+[](http://travis-ci.org/felixge/node-formidable)
+
+## Purpose
+
+A node.js module for parsing form data, especially file uploads.
+
+## Current status
+
+This module was developed for [Transloadit](http://transloadit.com/), a service focused on uploading
+and encoding images and videos. It has been battle-tested against hundreds of GB of file uploads from
+a large variety of clients and is considered production-ready.
+
+## Features
+
+* Fast (~500mb/sec), non-buffering multipart parser
+* Automatically writing file uploads to disk
+* Low memory footprint
+* Graceful error handling
+* Very high test coverage
+
+## Changelog
+
+### v1.0.9
+
+* Emit progress when content length header parsed (Tim Koschützki)
+* Fix Readme syntax due to GitHub changes (goob)
+* Replace references to old 'sys' module in Readme with 'util' (Peter Sugihara)
+
+### v1.0.8
+
+* Strip potentially unsafe characters when using `keepExtensions: true`.
+* Switch to utest / urun for testing
+* Add travis build
+
+### v1.0.7
+
+* Remove file from package that was causing problems when installing on windows. (#102)
+* Fix typos in Readme (Jason Davies).
+
+### v1.0.6
+
+* Do not default to the default to the field name for file uploads where
+ filename="".
+
+### v1.0.5
+
+* Support filename="" in multipart parts
+* Explain unexpected end() errors in parser better
+
+**Note:** Starting with this version, formidable emits 'file' events for empty
+file input fields. Previously those were incorrectly emitted as regular file
+input fields with value = "".
+
+### v1.0.4
+
+* Detect a good default tmp directory regardless of platform. (#88)
+
+### v1.0.3
+
+* Fix problems with utf8 characters (#84) / semicolons in filenames (#58)
+* Small performance improvements
+* New test suite and fixture system
+
+### v1.0.2
+
+* Exclude node\_modules folder from git
+* Implement new `'aborted'` event
+* Fix files in example folder to work with recent node versions
+* Make gently a devDependency
+
+[See Commits](https://github.com/felixge/node-formidable/compare/v1.0.1...v1.0.2)
+
+### v1.0.1
+
+* Fix package.json to refer to proper main directory. (#68, Dean Landolt)
+
+[See Commits](https://github.com/felixge/node-formidable/compare/v1.0.0...v1.0.1)
+
+### v1.0.0
+
+* Add support for multipart boundaries that are quoted strings. (Jeff Craig)
+
+This marks the beginning of development on version 2.0 which will include
+several architectural improvements.
+
+[See Commits](https://github.com/felixge/node-formidable/compare/v0.9.11...v1.0.0)
+
+### v0.9.11
+
+* Emit `'progress'` event when receiving data, regardless of parsing it. (Tim Koschützki)
+* Use [W3C FileAPI Draft](http://dev.w3.org/2006/webapi/FileAPI/) properties for File class
+
+**Important:** The old property names of the File class will be removed in a
+future release.
+
+[See Commits](https://github.com/felixge/node-formidable/compare/v0.9.10...v0.9.11)
+
+### Older releases
+
+These releases were done before starting to maintain the above Changelog:
+
+* [v0.9.10](https://github.com/felixge/node-formidable/compare/v0.9.9...v0.9.10)
+* [v0.9.9](https://github.com/felixge/node-formidable/compare/v0.9.8...v0.9.9)
+* [v0.9.8](https://github.com/felixge/node-formidable/compare/v0.9.7...v0.9.8)
+* [v0.9.7](https://github.com/felixge/node-formidable/compare/v0.9.6...v0.9.7)
+* [v0.9.6](https://github.com/felixge/node-formidable/compare/v0.9.5...v0.9.6)
+* [v0.9.5](https://github.com/felixge/node-formidable/compare/v0.9.4...v0.9.5)
+* [v0.9.4](https://github.com/felixge/node-formidable/compare/v0.9.3...v0.9.4)
+* [v0.9.3](https://github.com/felixge/node-formidable/compare/v0.9.2...v0.9.3)
+* [v0.9.2](https://github.com/felixge/node-formidable/compare/v0.9.1...v0.9.2)
+* [v0.9.1](https://github.com/felixge/node-formidable/compare/v0.9.0...v0.9.1)
+* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
+* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
+* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
+* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
+* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
+* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
+* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
+* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)
+* [v0.1.0](https://github.com/felixge/node-formidable/commits/v0.1.0)
+
+## Installation
+
+Via [npm](http://github.com/isaacs/npm):
+
+ npm install formidable@latest
+
+Manually:
+
+ git clone git://github.com/felixge/node-formidable.git formidable
+ vim my.js
+ # var formidable = require('./formidable');
+
+Note: Formidable requires [gently](http://github.com/felixge/node-gently) to run the unit tests, but you won't need it for just using the library.
+
+## Example
+
+Parse an incoming file upload.
+
+ var formidable = require('formidable'),
+ http = require('http'),
+
+ util = require('util');
+
+ http.createServer(function(req, res) {
+ if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
+ // parse a file upload
+ var form = new formidable.IncomingForm();
+ form.parse(req, function(err, fields, files) {
+ res.writeHead(200, {'content-type': 'text/plain'});
+ res.write('received upload:\n\n');
+ res.end(util.inspect({fields: fields, files: files}));
+ });
+ return;
+ }
+
+ // show a file upload form
+ res.writeHead(200, {'content-type': 'text/html'});
+ res.end(
+ ''
+ );
+ }).listen(80);
+
+## API
+
+### formidable.IncomingForm
+
+__new formidable.IncomingForm()__
+
+Creates a new incoming form.
+
+__incomingForm.encoding = 'utf-8'__
+
+The encoding to use for incoming form fields.
+
+__incomingForm.uploadDir = process.env.TMP || '/tmp' || process.cwd()__
+
+The directory for placing file uploads in. You can move them later on using
+`fs.rename()`. The default directory is picked at module load time depending on
+the first existing directory from those listed above.
+
+__incomingForm.keepExtensions = false__
+
+If you want the files written to `incomingForm.uploadDir` to include the extensions of the original files, set this property to `true`.
+
+__incomingForm.type__
+
+Either 'multipart' or 'urlencoded' depending on the incoming request.
+
+__incomingForm.maxFieldsSize = 2 * 1024 * 1024__
+
+Limits the amount of memory a field (not file) can allocate in bytes.
+If this value is exceeded, an `'error'` event is emitted. The default
+size is 2MB.
+
+__incomingForm.hash = false__
+
+If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`.
+
+__incomingForm.bytesReceived__
+
+The amount of bytes received for this form so far.
+
+__incomingForm.bytesExpected__
+
+The expected number of bytes in this form.
+
+__incomingForm.parse(request, [cb])__
+
+Parses an incoming node.js `request` containing form data. If `cb` is provided, all fields an files are collected and passed to the callback:
+
+ incomingForm.parse(req, function(err, fields, files) {
+ // ...
+ });
+
+__incomingForm.onPart(part)__
+
+You may overwrite this method if you are interested in directly accessing the multipart stream. Doing so will disable any `'field'` / `'file'` events processing which would occur otherwise, making you fully responsible for handling the processing.
+
+ incomingForm.onPart = function(part) {
+ part.addListener('data', function() {
+ // ...
+ });
+ }
+
+If you want to use formidable to only handle certain parts for you, you can do so:
+
+ incomingForm.onPart = function(part) {
+ if (!part.filename) {
+ // let formidable handle all non-file parts
+ incomingForm.handlePart(part);
+ }
+ }
+
+Check the code in this method for further inspiration.
+
+__Event: 'progress' (bytesReceived, bytesExpected)__
+
+Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.
+
+__Event: 'field' (name, value)__
+
+Emitted whenever a field / value pair has been received.
+
+__Event: 'fileBegin' (name, file)__
+
+Emitted whenever a new file is detected in the upload stream. Use this even if
+you want to stream the file to somewhere else while buffering the upload on
+the file system.
+
+__Event: 'file' (name, file)__
+
+Emitted whenever a field / file pair has been received. `file` is an instance of `File`.
+
+__Event: 'error' (err)__
+
+Emitted when there is an error processing the incoming form. A request that experiences an error is automatically paused, you will have to manually call `request.resume()` if you want the request to continue firing `'data'` events.
+
+__Event: 'aborted'__
+
+Emitted when the request was aborted by the user. Right now this can be due to a 'timeout' or 'close' event on the socket. In the future there will be a separate 'timeout' event (needs a change in the node core).
+
+__Event: 'end' ()__
+
+Emitted when the entire request has been received, and all contained files have finished flushing to disk. This is a great place for you to send your response.
+
+### formidable.File
+
+__file.size = 0__
+
+The size of the uploaded file in bytes. If the file is still being uploaded (see `'fileBegin'` event), this property says how many bytes of the file have been written to disk yet.
+
+__file.path = null__
+
+The path this file is being written to. You can modify this in the `'fileBegin'` event in
+case you are unhappy with the way formidable generates a temporary path for your files.
+
+__file.name = null__
+
+The name this file had according to the uploading client.
+
+__file.type = null__
+
+The mime type of this file, according to the uploading client.
+
+__file.lastModifiedDate = null__
+
+A date object (or `null`) containing the time this file was last written to. Mostly
+here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/).
+
+__file.hash = null__
+
+If hash calculation was set, you can read the hex digest out of this var.
+
+## License
+
+Formidable is licensed under the MIT license.
+
+## Ports
+
+* [multipart-parser](http://github.com/FooBarWidget/multipart-parser): a C++ parser based on formidable
+
+## Credits
+
+* [Ryan Dahl](http://twitter.com/ryah) for his work on [http-parser](http://github.com/ry/http-parser) which heavily inspired multipart_parser.js
diff --git a/node_modules/formidable/TODO b/node_modules/formidable/TODO
new file mode 100644
index 0000000..e1107f2
--- /dev/null
+++ b/node_modules/formidable/TODO
@@ -0,0 +1,3 @@
+- Better bufferMaxSize handling approach
+- Add tests for JSON parser pull request and merge it
+- Implement QuerystringParser the same way as MultipartParser
diff --git a/node_modules/formidable/benchmark/bench-multipart-parser.js b/node_modules/formidable/benchmark/bench-multipart-parser.js
new file mode 100644
index 0000000..bff41f1
--- /dev/null
+++ b/node_modules/formidable/benchmark/bench-multipart-parser.js
@@ -0,0 +1,70 @@
+require('../test/common');
+var multipartParser = require('../lib/multipart_parser'),
+ MultipartParser = multipartParser.MultipartParser,
+ parser = new MultipartParser(),
+ Buffer = require('buffer').Buffer,
+ boundary = '-----------------------------168072824752491622650073',
+ mb = 100,
+ buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
+ callbacks =
+ { partBegin: -1,
+ partEnd: -1,
+ headerField: -1,
+ headerValue: -1,
+ partData: -1,
+ end: -1,
+ };
+
+
+parser.initWithBoundary(boundary);
+parser.onHeaderField = function() {
+ callbacks.headerField++;
+};
+
+parser.onHeaderValue = function() {
+ callbacks.headerValue++;
+};
+
+parser.onPartBegin = function() {
+ callbacks.partBegin++;
+};
+
+parser.onPartData = function() {
+ callbacks.partData++;
+};
+
+parser.onPartEnd = function() {
+ callbacks.partEnd++;
+};
+
+parser.onEnd = function() {
+ callbacks.end++;
+};
+
+var start = +new Date(),
+ nparsed = parser.write(buffer),
+ duration = +new Date - start,
+ mbPerSec = (mb / (duration / 1000)).toFixed(2);
+
+console.log(mbPerSec+' mb/sec');
+
+assert.equal(nparsed, buffer.length);
+
+function createMultipartBuffer(boundary, size) {
+ var head =
+ '--'+boundary+'\r\n'
+ + 'content-disposition: form-data; name="field1"\r\n'
+ + '\r\n'
+ , tail = '\r\n--'+boundary+'--\r\n'
+ , buffer = new Buffer(size);
+
+ buffer.write(head, 'ascii', 0);
+ buffer.write(tail, 'ascii', buffer.length - tail.length);
+ return buffer;
+}
+
+process.on('exit', function() {
+ for (var k in callbacks) {
+ assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
+ }
+});
diff --git a/node_modules/formidable/example/post.js b/node_modules/formidable/example/post.js
new file mode 100644
index 0000000..f6c15a6
--- /dev/null
+++ b/node_modules/formidable/example/post.js
@@ -0,0 +1,43 @@
+require('../test/common');
+var http = require('http'),
+ util = require('util'),
+ formidable = require('formidable'),
+ server;
+
+server = http.createServer(function(req, res) {
+ if (req.url == '/') {
+ res.writeHead(200, {'content-type': 'text/html'});
+ res.end(
+ ''
+ );
+ } else if (req.url == '/post') {
+ var form = new formidable.IncomingForm(),
+ fields = [];
+
+ form
+ .on('error', function(err) {
+ res.writeHead(200, {'content-type': 'text/plain'});
+ res.end('error:\n\n'+util.inspect(err));
+ })
+ .on('field', function(field, value) {
+ console.log(field, value);
+ fields.push([field, value]);
+ })
+ .on('end', function() {
+ console.log('-> post done');
+ res.writeHead(200, {'content-type': 'text/plain'});
+ res.end('received fields:\n\n '+util.inspect(fields));
+ });
+ form.parse(req);
+ } else {
+ res.writeHead(404, {'content-type': 'text/plain'});
+ res.end('404');
+ }
+});
+server.listen(TEST_PORT);
+
+console.log('listening on http://localhost:'+TEST_PORT+'/');
diff --git a/node_modules/formidable/example/upload.js b/node_modules/formidable/example/upload.js
new file mode 100644
index 0000000..050cdd9
--- /dev/null
+++ b/node_modules/formidable/example/upload.js
@@ -0,0 +1,48 @@
+require('../test/common');
+var http = require('http'),
+ util = require('util'),
+ formidable = require('formidable'),
+ server;
+
+server = http.createServer(function(req, res) {
+ if (req.url == '/') {
+ res.writeHead(200, {'content-type': 'text/html'});
+ res.end(
+ ''
+ );
+ } else if (req.url == '/upload') {
+ var form = new formidable.IncomingForm(),
+ files = [],
+ fields = [];
+
+ form.uploadDir = TEST_TMP;
+
+ form
+ .on('field', function(field, value) {
+ console.log(field, value);
+ fields.push([field, value]);
+ })
+ .on('file', function(field, file) {
+ console.log(field, file);
+ files.push([field, file]);
+ })
+ .on('end', function() {
+ console.log('-> upload done');
+ res.writeHead(200, {'content-type': 'text/plain'});
+ res.write('received fields:\n\n '+util.inspect(fields));
+ res.write('\n\n');
+ res.end('received files:\n\n '+util.inspect(files));
+ });
+ form.parse(req);
+ } else {
+ res.writeHead(404, {'content-type': 'text/plain'});
+ res.end('404');
+ }
+});
+server.listen(TEST_PORT);
+
+console.log('listening on http://localhost:'+TEST_PORT+'/');
diff --git a/node_modules/formidable/index.js b/node_modules/formidable/index.js
new file mode 100644
index 0000000..be41032
--- /dev/null
+++ b/node_modules/formidable/index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/formidable');
\ No newline at end of file
diff --git a/node_modules/formidable/lib/file.js b/node_modules/formidable/lib/file.js
new file mode 100644
index 0000000..dad8d5f
--- /dev/null
+++ b/node_modules/formidable/lib/file.js
@@ -0,0 +1,73 @@
+if (global.GENTLY) require = GENTLY.hijack(require);
+
+var util = require('./util'),
+ WriteStream = require('fs').WriteStream,
+ EventEmitter = require('events').EventEmitter,
+ crypto = require('crypto');
+
+function File(properties) {
+ EventEmitter.call(this);
+
+ this.size = 0;
+ this.path = null;
+ this.name = null;
+ this.type = null;
+ this.hash = null;
+ this.lastModifiedDate = null;
+
+ this._writeStream = null;
+
+ for (var key in properties) {
+ this[key] = properties[key];
+ }
+
+ if(typeof this.hash === 'string') {
+ this.hash = crypto.createHash(properties.hash);
+ }
+
+ this._backwardsCompatibility();
+}
+module.exports = File;
+util.inherits(File, EventEmitter);
+
+// @todo Next release: Show error messages when accessing these
+File.prototype._backwardsCompatibility = function() {
+ var self = this;
+ this.__defineGetter__('length', function() {
+ return self.size;
+ });
+ this.__defineGetter__('filename', function() {
+ return self.name;
+ });
+ this.__defineGetter__('mime', function() {
+ return self.type;
+ });
+};
+
+File.prototype.open = function() {
+ this._writeStream = new WriteStream(this.path);
+};
+
+File.prototype.write = function(buffer, cb) {
+ var self = this;
+ this._writeStream.write(buffer, function() {
+ if(self.hash) {
+ self.hash.update(buffer);
+ }
+ self.lastModifiedDate = new Date();
+ self.size += buffer.length;
+ self.emit('progress', self.size);
+ cb();
+ });
+};
+
+File.prototype.end = function(cb) {
+ var self = this;
+ this._writeStream.end(function() {
+ if(self.hash) {
+ self.hash = self.hash.digest('hex');
+ }
+ self.emit('end');
+ cb();
+ });
+};
diff --git a/node_modules/formidable/lib/incoming_form.js b/node_modules/formidable/lib/incoming_form.js
new file mode 100644
index 0000000..060eac2
--- /dev/null
+++ b/node_modules/formidable/lib/incoming_form.js
@@ -0,0 +1,384 @@
+if (global.GENTLY) require = GENTLY.hijack(require);
+
+var fs = require('fs');
+var util = require('./util'),
+ path = require('path'),
+ File = require('./file'),
+ MultipartParser = require('./multipart_parser').MultipartParser,
+ QuerystringParser = require('./querystring_parser').QuerystringParser,
+ StringDecoder = require('string_decoder').StringDecoder,
+ EventEmitter = require('events').EventEmitter,
+ Stream = require('stream').Stream;
+
+function IncomingForm(opts) {
+ if (!(this instanceof IncomingForm)) return new IncomingForm;
+ EventEmitter.call(this);
+
+ opts=opts||{};
+
+ this.error = null;
+ this.ended = false;
+
+ this.maxFieldsSize = opts.maxFieldsSize || 2 * 1024 * 1024;
+ this.keepExtensions = opts.keepExtensions || false;
+ this.uploadDir = opts.uploadDir || IncomingForm.UPLOAD_DIR;
+ this.encoding = opts.encoding || 'utf-8';
+ this.headers = null;
+ this.type = null;
+ this.hash = false;
+
+ this.bytesReceived = null;
+ this.bytesExpected = null;
+
+ this._parser = null;
+ this._flushing = 0;
+ this._fieldsSize = 0;
+};
+util.inherits(IncomingForm, EventEmitter);
+exports.IncomingForm = IncomingForm;
+
+IncomingForm.UPLOAD_DIR = (function() {
+ var dirs = [process.env.TMP, '/tmp', process.cwd()];
+ for (var i = 0; i < dirs.length; i++) {
+ var dir = dirs[i];
+ var isDirectory = false;
+
+ try {
+ isDirectory = fs.statSync(dir).isDirectory();
+ } catch (e) {}
+
+ if (isDirectory) return dir;
+ }
+})();
+
+IncomingForm.prototype.parse = function(req, cb) {
+ this.pause = function() {
+ try {
+ req.pause();
+ } catch (err) {
+ // the stream was destroyed
+ if (!this.ended) {
+ // before it was completed, crash & burn
+ this._error(err);
+ }
+ return false;
+ }
+ return true;
+ };
+
+ this.resume = function() {
+ try {
+ req.resume();
+ } catch (err) {
+ // the stream was destroyed
+ if (!this.ended) {
+ // before it was completed, crash & burn
+ this._error(err);
+ }
+ return false;
+ }
+
+ return true;
+ };
+
+ this.writeHeaders(req.headers);
+
+ var self = this;
+ req
+ .on('error', function(err) {
+ self._error(err);
+ })
+ .on('aborted', function() {
+ self.emit('aborted');
+ })
+ .on('data', function(buffer) {
+ self.write(buffer);
+ })
+ .on('end', function() {
+ if (self.error) {
+ return;
+ }
+
+ var err = self._parser.end();
+ if (err) {
+ self._error(err);
+ }
+ });
+
+ if (cb) {
+ var fields = {}, files = {};
+ this
+ .on('field', function(name, value) {
+ fields[name] = value;
+ })
+ .on('file', function(name, file) {
+ files[name] = file;
+ })
+ .on('error', function(err) {
+ cb(err, fields, files);
+ })
+ .on('end', function() {
+ cb(null, fields, files);
+ });
+ }
+
+ return this;
+};
+
+IncomingForm.prototype.writeHeaders = function(headers) {
+ this.headers = headers;
+ this._parseContentLength();
+ this._parseContentType();
+};
+
+IncomingForm.prototype.write = function(buffer) {
+ if (!this._parser) {
+ this._error(new Error('unintialized parser'));
+ return;
+ }
+
+ this.bytesReceived += buffer.length;
+ this.emit('progress', this.bytesReceived, this.bytesExpected);
+
+ var bytesParsed = this._parser.write(buffer);
+ if (bytesParsed !== buffer.length) {
+ this._error(new Error('parser error, '+bytesParsed+' of '+buffer.length+' bytes parsed'));
+ }
+
+ return bytesParsed;
+};
+
+IncomingForm.prototype.pause = function() {
+ // this does nothing, unless overwritten in IncomingForm.parse
+ return false;
+};
+
+IncomingForm.prototype.resume = function() {
+ // this does nothing, unless overwritten in IncomingForm.parse
+ return false;
+};
+
+IncomingForm.prototype.onPart = function(part) {
+ // this method can be overwritten by the user
+ this.handlePart(part);
+};
+
+IncomingForm.prototype.handlePart = function(part) {
+ var self = this;
+
+ if (part.filename === undefined) {
+ var value = ''
+ , decoder = new StringDecoder(this.encoding);
+
+ part.on('data', function(buffer) {
+ self._fieldsSize += buffer.length;
+ if (self._fieldsSize > self.maxFieldsSize) {
+ self._error(new Error('maxFieldsSize exceeded, received '+self._fieldsSize+' bytes of field data'));
+ return;
+ }
+ value += decoder.write(buffer);
+ });
+
+ part.on('end', function() {
+ self.emit('field', part.name, value);
+ });
+ return;
+ }
+
+ this._flushing++;
+
+ var file = new File({
+ path: this._uploadPath(part.filename),
+ name: part.filename,
+ type: part.mime,
+ hash: self.hash
+ });
+
+ this.emit('fileBegin', part.name, file);
+
+ file.open();
+
+ part.on('data', function(buffer) {
+ self.pause();
+ file.write(buffer, function() {
+ self.resume();
+ });
+ });
+
+ part.on('end', function() {
+ file.end(function() {
+ self._flushing--;
+ self.emit('file', part.name, file);
+ self._maybeEnd();
+ });
+ });
+};
+
+IncomingForm.prototype._parseContentType = function() {
+ if (!this.headers['content-type']) {
+ this._error(new Error('bad content-type header, no content-type'));
+ return;
+ }
+
+ if (this.headers['content-type'].match(/urlencoded/i)) {
+ this._initUrlencoded();
+ return;
+ }
+
+ if (this.headers['content-type'].match(/multipart/i)) {
+ var m;
+ if (m = this.headers['content-type'].match(/boundary=(?:"([^"]+)"|([^;]+))/i)) {
+ this._initMultipart(m[1] || m[2]);
+ } else {
+ this._error(new Error('bad content-type header, no multipart boundary'));
+ }
+ return;
+ }
+
+ this._error(new Error('bad content-type header, unknown content-type: '+this.headers['content-type']));
+};
+
+IncomingForm.prototype._error = function(err) {
+ if (this.error) {
+ return;
+ }
+
+ this.error = err;
+ this.pause();
+ this.emit('error', err);
+};
+
+IncomingForm.prototype._parseContentLength = function() {
+ if (this.headers['content-length']) {
+ this.bytesReceived = 0;
+ this.bytesExpected = parseInt(this.headers['content-length'], 10);
+ this.emit('progress', this.bytesReceived, this.bytesExpected);
+ }
+};
+
+IncomingForm.prototype._newParser = function() {
+ return new MultipartParser();
+};
+
+IncomingForm.prototype._initMultipart = function(boundary) {
+ this.type = 'multipart';
+
+ var parser = new MultipartParser(),
+ self = this,
+ headerField,
+ headerValue,
+ part;
+
+ parser.initWithBoundary(boundary);
+
+ parser.onPartBegin = function() {
+ part = new Stream();
+ part.readable = true;
+ part.headers = {};
+ part.name = null;
+ part.filename = null;
+ part.mime = null;
+ headerField = '';
+ headerValue = '';
+ };
+
+ parser.onHeaderField = function(b, start, end) {
+ headerField += b.toString(self.encoding, start, end);
+ };
+
+ parser.onHeaderValue = function(b, start, end) {
+ headerValue += b.toString(self.encoding, start, end);
+ };
+
+ parser.onHeaderEnd = function() {
+ headerField = headerField.toLowerCase();
+ part.headers[headerField] = headerValue;
+
+ var m;
+ if (headerField == 'content-disposition') {
+ if (m = headerValue.match(/name="([^"]+)"/i)) {
+ part.name = m[1];
+ }
+
+ part.filename = self._fileName(headerValue);
+ } else if (headerField == 'content-type') {
+ part.mime = headerValue;
+ }
+
+ headerField = '';
+ headerValue = '';
+ };
+
+ parser.onHeadersEnd = function() {
+ self.onPart(part);
+ };
+
+ parser.onPartData = function(b, start, end) {
+ part.emit('data', b.slice(start, end));
+ };
+
+ parser.onPartEnd = function() {
+ part.emit('end');
+ };
+
+ parser.onEnd = function() {
+ self.ended = true;
+ self._maybeEnd();
+ };
+
+ this._parser = parser;
+};
+
+IncomingForm.prototype._fileName = function(headerValue) {
+ var m = headerValue.match(/filename="(.*?)"($|; )/i)
+ if (!m) return;
+
+ var filename = m[1].substr(m[1].lastIndexOf('\\') + 1);
+ filename = filename.replace(/%22/g, '"');
+ filename = filename.replace(/([\d]{4});/g, function(m, code) {
+ return String.fromCharCode(code);
+ });
+ return filename;
+};
+
+IncomingForm.prototype._initUrlencoded = function() {
+ this.type = 'urlencoded';
+
+ var parser = new QuerystringParser()
+ , self = this;
+
+ parser.onField = function(key, val) {
+ self.emit('field', key, val);
+ };
+
+ parser.onEnd = function() {
+ self.ended = true;
+ self._maybeEnd();
+ };
+
+ this._parser = parser;
+};
+
+IncomingForm.prototype._uploadPath = function(filename) {
+ var name = '';
+ for (var i = 0; i < 32; i++) {
+ name += Math.floor(Math.random() * 16).toString(16);
+ }
+
+ if (this.keepExtensions) {
+ var ext = path.extname(filename);
+ ext = ext.replace(/(\.[a-z0-9]+).*/, '$1')
+
+ name += ext;
+ }
+
+ return path.join(this.uploadDir, name);
+};
+
+IncomingForm.prototype._maybeEnd = function() {
+ if (!this.ended || this._flushing) {
+ return;
+ }
+
+ this.emit('end');
+};
diff --git a/node_modules/formidable/lib/index.js b/node_modules/formidable/lib/index.js
new file mode 100644
index 0000000..7a6e3e1
--- /dev/null
+++ b/node_modules/formidable/lib/index.js
@@ -0,0 +1,3 @@
+var IncomingForm = require('./incoming_form').IncomingForm;
+IncomingForm.IncomingForm = IncomingForm;
+module.exports = IncomingForm;
diff --git a/node_modules/formidable/lib/multipart_parser.js b/node_modules/formidable/lib/multipart_parser.js
new file mode 100644
index 0000000..9ca567c
--- /dev/null
+++ b/node_modules/formidable/lib/multipart_parser.js
@@ -0,0 +1,312 @@
+var Buffer = require('buffer').Buffer,
+ s = 0,
+ S =
+ { PARSER_UNINITIALIZED: s++,
+ START: s++,
+ START_BOUNDARY: s++,
+ HEADER_FIELD_START: s++,
+ HEADER_FIELD: s++,
+ HEADER_VALUE_START: s++,
+ HEADER_VALUE: s++,
+ HEADER_VALUE_ALMOST_DONE: s++,
+ HEADERS_ALMOST_DONE: s++,
+ PART_DATA_START: s++,
+ PART_DATA: s++,
+ PART_END: s++,
+ END: s++,
+ },
+
+ f = 1,
+ F =
+ { PART_BOUNDARY: f,
+ LAST_BOUNDARY: f *= 2,
+ },
+
+ LF = 10,
+ CR = 13,
+ SPACE = 32,
+ HYPHEN = 45,
+ COLON = 58,
+ A = 97,
+ Z = 122,
+
+ lower = function(c) {
+ return c | 0x20;
+ };
+
+for (var s in S) {
+ exports[s] = S[s];
+}
+
+function MultipartParser() {
+ this.boundary = null;
+ this.boundaryChars = null;
+ this.lookbehind = null;
+ this.state = S.PARSER_UNINITIALIZED;
+
+ this.index = null;
+ this.flags = 0;
+};
+exports.MultipartParser = MultipartParser;
+
+MultipartParser.stateToString = function(stateNumber) {
+ for (var state in S) {
+ var number = S[state];
+ if (number === stateNumber) return state;
+ }
+};
+
+MultipartParser.prototype.initWithBoundary = function(str) {
+ this.boundary = new Buffer(str.length+4);
+ this.boundary.write('\r\n--', 'ascii', 0);
+ this.boundary.write(str, 'ascii', 4);
+ this.lookbehind = new Buffer(this.boundary.length+8);
+ this.state = S.START;
+
+ this.boundaryChars = {};
+ for (var i = 0; i < this.boundary.length; i++) {
+ this.boundaryChars[this.boundary[i]] = true;
+ }
+};
+
+MultipartParser.prototype.write = function(buffer) {
+ var self = this,
+ i = 0,
+ len = buffer.length,
+ prevIndex = this.index,
+ index = this.index,
+ state = this.state,
+ flags = this.flags,
+ lookbehind = this.lookbehind,
+ boundary = this.boundary,
+ boundaryChars = this.boundaryChars,
+ boundaryLength = this.boundary.length,
+ boundaryEnd = boundaryLength - 1,
+ bufferLength = buffer.length,
+ c,
+ cl,
+
+ mark = function(name) {
+ self[name+'Mark'] = i;
+ },
+ clear = function(name) {
+ delete self[name+'Mark'];
+ },
+ callback = function(name, buffer, start, end) {
+ if (start !== undefined && start === end) {
+ return;
+ }
+
+ var callbackSymbol = 'on'+name.substr(0, 1).toUpperCase()+name.substr(1);
+ if (callbackSymbol in self) {
+ self[callbackSymbol](buffer, start, end);
+ }
+ },
+ dataCallback = function(name, clear) {
+ var markSymbol = name+'Mark';
+ if (!(markSymbol in self)) {
+ return;
+ }
+
+ if (!clear) {
+ callback(name, buffer, self[markSymbol], buffer.length);
+ self[markSymbol] = 0;
+ } else {
+ callback(name, buffer, self[markSymbol], i);
+ delete self[markSymbol];
+ }
+ };
+
+ for (i = 0; i < len; i++) {
+ c = buffer[i];
+ switch (state) {
+ case S.PARSER_UNINITIALIZED:
+ return i;
+ case S.START:
+ index = 0;
+ state = S.START_BOUNDARY;
+ case S.START_BOUNDARY:
+ if (index == boundary.length - 2) {
+ if (c != CR) {
+ return i;
+ }
+ index++;
+ break;
+ } else if (index - 1 == boundary.length - 2) {
+ if (c != LF) {
+ return i;
+ }
+ index = 0;
+ callback('partBegin');
+ state = S.HEADER_FIELD_START;
+ break;
+ }
+
+ if (c != boundary[index+2]) {
+ return i;
+ }
+ index++;
+ break;
+ case S.HEADER_FIELD_START:
+ state = S.HEADER_FIELD;
+ mark('headerField');
+ index = 0;
+ case S.HEADER_FIELD:
+ if (c == CR) {
+ clear('headerField');
+ state = S.HEADERS_ALMOST_DONE;
+ break;
+ }
+
+ index++;
+ if (c == HYPHEN) {
+ break;
+ }
+
+ if (c == COLON) {
+ if (index == 1) {
+ // empty header field
+ return i;
+ }
+ dataCallback('headerField', true);
+ state = S.HEADER_VALUE_START;
+ break;
+ }
+
+ cl = lower(c);
+ if (cl < A || cl > Z) {
+ return i;
+ }
+ break;
+ case S.HEADER_VALUE_START:
+ if (c == SPACE) {
+ break;
+ }
+
+ mark('headerValue');
+ state = S.HEADER_VALUE;
+ case S.HEADER_VALUE:
+ if (c == CR) {
+ dataCallback('headerValue', true);
+ callback('headerEnd');
+ state = S.HEADER_VALUE_ALMOST_DONE;
+ }
+ break;
+ case S.HEADER_VALUE_ALMOST_DONE:
+ if (c != LF) {
+ return i;
+ }
+ state = S.HEADER_FIELD_START;
+ break;
+ case S.HEADERS_ALMOST_DONE:
+ if (c != LF) {
+ return i;
+ }
+
+ callback('headersEnd');
+ state = S.PART_DATA_START;
+ break;
+ case S.PART_DATA_START:
+ state = S.PART_DATA
+ mark('partData');
+ case S.PART_DATA:
+ prevIndex = index;
+
+ if (index == 0) {
+ // boyer-moore derrived algorithm to safely skip non-boundary data
+ i += boundaryEnd;
+ while (i < bufferLength && !(buffer[i] in boundaryChars)) {
+ i += boundaryLength;
+ }
+ i -= boundaryEnd;
+ c = buffer[i];
+ }
+
+ if (index < boundary.length) {
+ if (boundary[index] == c) {
+ if (index == 0) {
+ dataCallback('partData', true);
+ }
+ index++;
+ } else {
+ index = 0;
+ }
+ } else if (index == boundary.length) {
+ index++;
+ if (c == CR) {
+ // CR = part boundary
+ flags |= F.PART_BOUNDARY;
+ } else if (c == HYPHEN) {
+ // HYPHEN = end boundary
+ flags |= F.LAST_BOUNDARY;
+ } else {
+ index = 0;
+ }
+ } else if (index - 1 == boundary.length) {
+ if (flags & F.PART_BOUNDARY) {
+ index = 0;
+ if (c == LF) {
+ // unset the PART_BOUNDARY flag
+ flags &= ~F.PART_BOUNDARY;
+ callback('partEnd');
+ callback('partBegin');
+ state = S.HEADER_FIELD_START;
+ break;
+ }
+ } else if (flags & F.LAST_BOUNDARY) {
+ if (c == HYPHEN) {
+ callback('partEnd');
+ callback('end');
+ state = S.END;
+ } else {
+ index = 0;
+ }
+ } else {
+ index = 0;
+ }
+ }
+
+ if (index > 0) {
+ // when matching a possible boundary, keep a lookbehind reference
+ // in case it turns out to be a false lead
+ lookbehind[index-1] = c;
+ } else if (prevIndex > 0) {
+ // if our boundary turned out to be rubbish, the captured lookbehind
+ // belongs to partData
+ callback('partData', lookbehind, 0, prevIndex);
+ prevIndex = 0;
+ mark('partData');
+
+ // reconsider the current character even so it interrupted the sequence
+ // it could be the beginning of a new sequence
+ i--;
+ }
+
+ break;
+ case S.END:
+ break;
+ default:
+ return i;
+ }
+ }
+
+ dataCallback('headerField');
+ dataCallback('headerValue');
+ dataCallback('partData');
+
+ this.index = index;
+ this.state = state;
+ this.flags = flags;
+
+ return len;
+};
+
+MultipartParser.prototype.end = function() {
+ if (this.state != S.END) {
+ return new Error('MultipartParser.end(): stream ended unexpectedly: ' + this.explain());
+ }
+};
+
+MultipartParser.prototype.explain = function() {
+ return 'state = ' + MultipartParser.stateToString(this.state);
+};
diff --git a/node_modules/formidable/lib/querystring_parser.js b/node_modules/formidable/lib/querystring_parser.js
new file mode 100644
index 0000000..63f109e
--- /dev/null
+++ b/node_modules/formidable/lib/querystring_parser.js
@@ -0,0 +1,25 @@
+if (global.GENTLY) require = GENTLY.hijack(require);
+
+// This is a buffering parser, not quite as nice as the multipart one.
+// If I find time I'll rewrite this to be fully streaming as well
+var querystring = require('querystring');
+
+function QuerystringParser() {
+ this.buffer = '';
+};
+exports.QuerystringParser = QuerystringParser;
+
+QuerystringParser.prototype.write = function(buffer) {
+ this.buffer += buffer.toString('ascii');
+ return buffer.length;
+};
+
+QuerystringParser.prototype.end = function() {
+ var fields = querystring.parse(this.buffer);
+ for (var field in fields) {
+ this.onField(field, fields[field]);
+ }
+ this.buffer = '';
+
+ this.onEnd();
+};
\ No newline at end of file
diff --git a/node_modules/formidable/lib/util.js b/node_modules/formidable/lib/util.js
new file mode 100644
index 0000000..e9493e9
--- /dev/null
+++ b/node_modules/formidable/lib/util.js
@@ -0,0 +1,6 @@
+// Backwards compatibility ...
+try {
+ module.exports = require('util');
+} catch (e) {
+ module.exports = require('sys');
+}
diff --git a/node_modules/formidable/node-gently/Makefile b/node_modules/formidable/node-gently/Makefile
new file mode 100644
index 0000000..01f7140
--- /dev/null
+++ b/node_modules/formidable/node-gently/Makefile
@@ -0,0 +1,4 @@
+test:
+ @find test/simple/test-*.js | xargs -n 1 -t node
+
+.PHONY: test
\ No newline at end of file
diff --git a/node_modules/formidable/node-gently/Readme.md b/node_modules/formidable/node-gently/Readme.md
new file mode 100644
index 0000000..f8f0c66
--- /dev/null
+++ b/node_modules/formidable/node-gently/Readme.md
@@ -0,0 +1,167 @@
+# Gently
+
+## Purpose
+
+A node.js module that helps with stubbing and behavior verification. It allows you to test the most remote and nested corners of your code while keeping being fully unobtrusive.
+
+## Features
+
+* Overwrite and stub individual object functions
+* Verify that all expected calls have been made in the expected order
+* Restore stubbed functions to their original behavior
+* Detect object / class names from obj.constructor.name and obj.toString()
+* Hijack any required module function or class constructor
+
+## Installation
+
+Via [npm](http://github.com/isaacs/npm):
+
+ npm install gently@latest
+
+## Example
+
+Make sure your dog is working properly:
+
+ function Dog() {}
+
+ Dog.prototype.seeCat = function() {
+ this.bark('whuf, whuf');
+ this.run();
+ }
+
+ Dog.prototype.bark = function(bark) {
+ require('sys').puts(bark);
+ }
+
+ var gently = new (require('gently'))
+ , assert = require('assert')
+ , dog = new Dog();
+
+ gently.expect(dog, 'bark', function(bark) {
+ assert.equal(bark, 'whuf, whuf');
+ });
+ gently.expect(dog, 'run');
+
+ dog.seeCat();
+
+You can also easily test event emitters with this, for example a simple sequence of 2 events emitted by `fs.WriteStream`:
+
+ var gently = new (require('gently'))
+ , stream = new (require('fs').WriteStream)('my_file.txt');
+
+ gently.expect(stream, 'emit', function(event) {
+ assert.equal(event, 'open');
+ });
+
+ gently.expect(stream, 'emit', function(event) {
+ assert.equal(event, 'drain');
+ });
+
+For a full read world example, check out this test case: [test-incoming-form.js](http://github.com/felixge/node-formidable/blob/master/test/simple/test-incoming-form.js) (in [node-formdiable](http://github.com/felixge/node-formidable)).
+
+## API
+
+### Gently
+
+#### new Gently()
+
+Creates a new gently instance. It listens to the process `'exit'` event to make sure all expectations have been verified.
+
+#### gently.expect(obj, method, [[count], stubFn])
+
+Creates an expectation for an objects method to be called. You can optionally specify the call `count` you are expecting, as well as `stubFn` function that will run instead of the original function.
+
+Returns a reference to the function that is getting overwritten.
+
+#### gently.expect([count], stubFn)
+
+Returns a function that is supposed to be executed `count` times, delegating any calls to the provided `stubFn` function. Naming your stubFn closure will help to properly diagnose errors that are being thrown:
+
+ childProcess.exec('ls', gently.expect(function lsCallback(code) {
+ assert.equal(0, code);
+ }));
+
+#### gently.restore(obj, method)
+
+Restores an object method that has been previously overwritten using `gently.expect()`.
+
+#### gently.hijack(realRequire)
+
+Returns a new require functions that catches a reference to all required modules into `gently.hijacked`.
+
+To use this function, include a line like this in your `'my-module.js'`.
+
+ if (global.GENTLY) require = GENTLY.hijack(require);
+
+ var sys = require('sys');
+ exports.hello = function() {
+ sys.log('world');
+ };
+
+Now you can write a test for the module above:
+
+ var gently = global.GENTLY = new (require('gently'))
+ , myModule = require('./my-module');
+
+ gently.expect(gently.hijacked.sys, 'log', function(str) {
+ assert.equal(str, 'world');
+ });
+
+ myModule.hello();
+
+#### gently.stub(location, [exportsName])
+
+Returns a stub class that will be used instead of the real class from the module at `location` with the given `exportsName`.
+
+This allows to test an OOP version of the previous example, where `'my-module.js'`.
+
+ if (global.GENTLY) require = GENTLY.hijack(require);
+
+ var World = require('./world');
+
+ exports.hello = function() {
+ var world = new World();
+ world.hello();
+ }
+
+And `world.js` looks like this:
+
+ var sys = require('sys');
+
+ function World() {
+
+ }
+ module.exports = World;
+
+ World.prototype.hello = function() {
+ sys.log('world');
+ };
+
+Testing `'my-module.js'` can now easily be accomplished:
+
+ var gently = global.GENTLY = new (require('gently'))
+ , WorldStub = gently.stub('./world')
+ , myModule = require('./my-module')
+ , WORLD;
+
+ gently.expect(WorldStub, 'new', function() {
+ WORLD = this;
+ });
+
+ gently.expect(WORLD, 'hello');
+
+ myModule.hello();
+
+#### gently.hijacked
+
+An object that holds the references to all hijacked modules.
+
+#### gently.verify([msg])
+
+Verifies that all expectations of this gently instance have been satisfied. If not called manually, this method is called when the process `'exit'` event is fired.
+
+If `msg` is given, it will appear in any error that might be thrown.
+
+## License
+
+Gently is licensed under the MIT license.
\ No newline at end of file
diff --git a/node_modules/formidable/node-gently/example/dog.js b/node_modules/formidable/node-gently/example/dog.js
new file mode 100644
index 0000000..022fae0
--- /dev/null
+++ b/node_modules/formidable/node-gently/example/dog.js
@@ -0,0 +1,22 @@
+require('../test/common');
+function Dog() {}
+
+Dog.prototype.seeCat = function() {
+ this.bark('whuf, whuf');
+ this.run();
+}
+
+Dog.prototype.bark = function(bark) {
+ require('sys').puts(bark);
+}
+
+var gently = new (require('gently'))
+ , assert = require('assert')
+ , dog = new Dog();
+
+gently.expect(dog, 'bark', function(bark) {
+ assert.equal(bark, 'whuf, whuf');
+});
+gently.expect(dog, 'run');
+
+dog.seeCat();
\ No newline at end of file
diff --git a/node_modules/formidable/node-gently/example/event_emitter.js b/node_modules/formidable/node-gently/example/event_emitter.js
new file mode 100644
index 0000000..7def134
--- /dev/null
+++ b/node_modules/formidable/node-gently/example/event_emitter.js
@@ -0,0 +1,11 @@
+require('../test/common');
+var gently = new (require('gently'))
+ , stream = new (require('fs').WriteStream)('my_file.txt');
+
+gently.expect(stream, 'emit', function(event) {
+ assert.equal(event, 'open');
+});
+
+gently.expect(stream, 'emit', function(event) {
+ assert.equal(event, 'drain');
+});
\ No newline at end of file
diff --git a/node_modules/formidable/node-gently/index.js b/node_modules/formidable/node-gently/index.js
new file mode 100644
index 0000000..69122bd
--- /dev/null
+++ b/node_modules/formidable/node-gently/index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/gently');
\ No newline at end of file
diff --git a/node_modules/formidable/node-gently/lib/gently/gently.js b/node_modules/formidable/node-gently/lib/gently/gently.js
new file mode 100644
index 0000000..8af0e1e
--- /dev/null
+++ b/node_modules/formidable/node-gently/lib/gently/gently.js
@@ -0,0 +1,184 @@
+var path = require('path');
+
+function Gently() {
+ this.expectations = [];
+ this.hijacked = {};
+
+ var self = this;
+ process.addListener('exit', function() {
+ self.verify('process exit');
+ });
+};
+module.exports = Gently;
+
+Gently.prototype.stub = function(location, exportsName) {
+ function Stub() {
+ return Stub['new'].apply(this, arguments);
+ };
+
+ Stub['new'] = function () {};
+
+ var stubName = 'require('+JSON.stringify(location)+')';
+ if (exportsName) {
+ stubName += '.'+exportsName;
+ }
+
+ Stub.prototype.toString = Stub.toString = function() {
+ return stubName;
+ };
+
+ var exports = this.hijacked[location] || {};
+ if (exportsName) {
+ exports[exportsName] = Stub;
+ } else {
+ exports = Stub;
+ }
+
+ this.hijacked[location] = exports;
+ return Stub;
+};
+
+Gently.prototype.hijack = function(realRequire) {
+ var self = this;
+ return function(location) {
+ return self.hijacked[location] = (self.hijacked[location])
+ ? self.hijacked[location]
+ : realRequire(location);
+ };
+};
+
+Gently.prototype.expect = function(obj, method, count, stubFn) {
+ if (typeof obj != 'function' && typeof obj != 'object' && typeof obj != 'number') {
+ throw new Error
+ ( 'Bad 1st argument for gently.expect(), '
+ + 'object, function, or number expected, got: '+(typeof obj)
+ );
+ } else if (typeof obj == 'function' && (typeof method != 'string')) {
+ // expect(stubFn) interface
+ stubFn = obj;
+ obj = null;
+ method = null;
+ count = 1;
+ } else if (typeof method == 'function') {
+ // expect(count, stubFn) interface
+ count = obj;
+ stubFn = method;
+ obj = null;
+ method = null;
+ } else if (typeof count == 'function') {
+ // expect(obj, method, stubFn) interface
+ stubFn = count;
+ count = 1;
+ } else if (count === undefined) {
+ // expect(obj, method) interface
+ count = 1;
+ }
+
+ var name = this._name(obj, method, stubFn);
+ this.expectations.push({obj: obj, method: method, stubFn: stubFn, name: name, count: count});
+
+ var self = this;
+ function delegate() {
+ return self._stubFn(this, obj, method, name, Array.prototype.slice.call(arguments));
+ }
+
+ if (!obj) {
+ return delegate;
+ }
+
+ var original = (obj[method])
+ ? obj[method]._original || obj[method]
+ : undefined;
+
+ obj[method] = delegate;
+ return obj[method]._original = original;
+};
+
+Gently.prototype.restore = function(obj, method) {
+ if (!obj[method] || !obj[method]._original) {
+ throw new Error(this._name(obj, method)+' is not gently stubbed');
+ }
+ obj[method] = obj[method]._original;
+};
+
+Gently.prototype.verify = function(msg) {
+ if (!this.expectations.length) {
+ return;
+ }
+
+ var validExpectations = [];
+ for (var i = 0, l = this.expectations.length; i < l; i++) {
+ var expectation = this.expectations[i];
+
+ if (expectation.count > 0) {
+ validExpectations.push(expectation);
+ }
+ }
+
+ this.expectations = []; // reset so that no duplicate verification attempts are made
+
+ if (!validExpectations.length) {
+ return;
+ }
+
+ var expectation = validExpectations[0];
+
+ throw new Error
+ ( 'Expected call to '+expectation.name+' did not happen'
+ + ( (msg)
+ ? ' ('+msg+')'
+ : ''
+ )
+ );
+};
+
+Gently.prototype._stubFn = function(self, obj, method, name, args) {
+ var expectation = this.expectations[0], obj, method;
+
+ if (!expectation) {
+ throw new Error('Unexpected call to '+name+', no call was expected');
+ }
+
+ if (expectation.obj !== obj || expectation.method !== method) {
+ throw new Error('Unexpected call to '+name+', expected call to '+ expectation.name);
+ }
+
+ expectation.count -= 1;
+ if (expectation.count === 0) {
+ this.expectations.shift();
+
+ // autorestore original if its not a closure
+ // and no more expectations on that object
+ var has_more_expectations = this.expectations.reduce(function (memo, expectation) {
+ return memo || (expectation.obj === obj && expectation.method === method);
+ }, false);
+ if (obj !== null && method !== null && !has_more_expectations) {
+ if (typeof obj[method]._original !== 'undefined') {
+ obj[method] = obj[method]._original;
+ delete obj[method]._original;
+ } else {
+ delete obj[method];
+ }
+ }
+ }
+
+ if (expectation.stubFn) {
+ return expectation.stubFn.apply(self, args);
+ }
+};
+
+Gently.prototype._name = function(obj, method, stubFn) {
+ if (obj) {
+ var objectName = obj.toString();
+ if (objectName == '[object Object]' && obj.constructor.name) {
+ objectName = '['+obj.constructor.name+']';
+ }
+ return (objectName)+'.'+method+'()';
+ }
+
+ if (stubFn.name) {
+ return stubFn.name+'()';
+ }
+
+ return '>> '+stubFn.toString()+' <<';
+};
diff --git a/node_modules/formidable/node-gently/lib/gently/index.js b/node_modules/formidable/node-gently/lib/gently/index.js
new file mode 100644
index 0000000..64c1977
--- /dev/null
+++ b/node_modules/formidable/node-gently/lib/gently/index.js
@@ -0,0 +1 @@
+module.exports = require('./gently');
\ No newline at end of file
diff --git a/node_modules/formidable/node-gently/package.json b/node_modules/formidable/node-gently/package.json
new file mode 100644
index 0000000..9c1b7a0
--- /dev/null
+++ b/node_modules/formidable/node-gently/package.json
@@ -0,0 +1,14 @@
+{
+ "name": "gently",
+ "version": "0.9.2",
+ "directories": {
+ "lib": "./lib/gently"
+ },
+ "main": "./lib/gently/index",
+ "dependencies": {},
+ "devDependencies": {},
+ "engines": {
+ "node": "*"
+ },
+ "optionalDependencies": {}
+}
diff --git a/node_modules/formidable/node-gently/test/common.js b/node_modules/formidable/node-gently/test/common.js
new file mode 100644
index 0000000..978b5c5
--- /dev/null
+++ b/node_modules/formidable/node-gently/test/common.js
@@ -0,0 +1,8 @@
+var path = require('path')
+ , sys = require('sys');
+
+require.paths.unshift(path.dirname(__dirname)+'/lib');
+
+global.puts = sys.puts;
+global.p = function() {sys.error(sys.inspect.apply(null, arguments))};;
+global.assert = require('assert');
\ No newline at end of file
diff --git a/node_modules/formidable/node-gently/test/simple/test-gently.js b/node_modules/formidable/node-gently/test/simple/test-gently.js
new file mode 100644
index 0000000..4f8fe2d
--- /dev/null
+++ b/node_modules/formidable/node-gently/test/simple/test-gently.js
@@ -0,0 +1,348 @@
+require('../common');
+var Gently = require('gently')
+ , gently;
+
+function test(test) {
+ process.removeAllListeners('exit');
+ gently = new Gently();
+ test();
+}
+
+test(function constructor() {
+ assert.deepEqual(gently.expectations, []);
+ assert.deepEqual(gently.hijacked, {});
+ assert.equal(gently.constructor.name, 'Gently');
+});
+
+test(function expectBadArgs() {
+ var BAD_ARG = 'oh no';
+ try {
+ gently.expect(BAD_ARG);
+ assert.ok(false, 'throw needs to happen');
+ } catch (e) {
+ assert.equal(e.message, 'Bad 1st argument for gently.expect(), object, function, or number expected, got: '+(typeof BAD_ARG));
+ }
+});
+
+test(function expectObjMethod() {
+ var OBJ = {}, NAME = 'foobar';
+ OBJ.foo = function(x) {
+ return x;
+ };
+
+ gently._name = function() {
+ return NAME;
+ };
+
+ var original = OBJ.foo
+ , stubFn = function() {};
+
+ (function testAddOne() {
+ assert.strictEqual(gently.expect(OBJ, 'foo', stubFn), original);
+
+ assert.equal(gently.expectations.length, 1);
+ var expectation = gently.expectations[0];
+ assert.strictEqual(expectation.obj, OBJ);
+ assert.strictEqual(expectation.method, 'foo');
+ assert.strictEqual(expectation.stubFn, stubFn);
+ assert.strictEqual(expectation.name, NAME);
+ assert.strictEqual(OBJ.foo._original, original);
+ })();
+
+ (function testAddTwo() {
+ gently.expect(OBJ, 'foo', 2, stubFn);
+ assert.equal(gently.expectations.length, 2);
+ assert.strictEqual(OBJ.foo._original, original);
+ })();
+
+ (function testAddOneWithoutMock() {
+ gently.expect(OBJ, 'foo');
+ assert.equal(gently.expectations.length, 3);
+ })();
+
+ var stubFnCalled = 0, SELF = {};
+ gently._stubFn = function(self, obj, method, name, args) {
+ stubFnCalled++;
+ assert.strictEqual(self, SELF);
+ assert.strictEqual(obj, OBJ);
+ assert.strictEqual(method, 'foo');
+ assert.strictEqual(name, NAME);
+ assert.deepEqual(args, [1, 2]);
+ return 23;
+ };
+ assert.equal(OBJ.foo.apply(SELF, [1, 2]), 23);
+ assert.equal(stubFnCalled, 1);
+});
+
+test(function expectClosure() {
+ var NAME = 'MY CLOSURE';
+ function closureFn() {};
+
+ gently._name = function() {
+ return NAME;
+ };
+
+ var fn = gently.expect(closureFn);
+ assert.equal(gently.expectations.length, 1);
+ var expectation = gently.expectations[0];
+ assert.strictEqual(expectation.obj, null);
+ assert.strictEqual(expectation.method, null);
+ assert.strictEqual(expectation.stubFn, closureFn);
+ assert.strictEqual(expectation.name, NAME);
+
+ var stubFnCalled = 0, SELF = {};
+ gently._stubFn = function(self, obj, method, name, args) {
+ stubFnCalled++;
+ assert.strictEqual(self, SELF);
+ assert.strictEqual(obj, null);
+ assert.strictEqual(method, null);
+ assert.strictEqual(name, NAME);
+ assert.deepEqual(args, [1, 2]);
+ return 23;
+ };
+ assert.equal(fn.apply(SELF, [1, 2]), 23);
+ assert.equal(stubFnCalled, 1);
+});
+
+test(function expectClosureCount() {
+ var stubFnCalled = 0;
+ function closureFn() {stubFnCalled++};
+
+ var fn = gently.expect(2, closureFn);
+ assert.equal(gently.expectations.length, 1);
+ fn();
+ assert.equal(gently.expectations.length, 1);
+ fn();
+ assert.equal(stubFnCalled, 2);
+});
+
+test(function restore() {
+ var OBJ = {}, NAME = '[my object].myFn()';
+ OBJ.foo = function(x) {
+ return x;
+ };
+
+ gently._name = function() {
+ return NAME;
+ };
+
+ var original = OBJ.foo;
+ gently.expect(OBJ, 'foo');
+ gently.restore(OBJ, 'foo');
+ assert.strictEqual(OBJ.foo, original);
+
+ (function testError() {
+ try {
+ gently.restore(OBJ, 'foo');
+ assert.ok(false, 'throw needs to happen');
+ } catch (e) {
+ assert.equal(e.message, NAME+' is not gently stubbed');
+ }
+ })();
+});
+
+test(function _stubFn() {
+ var OBJ1 = {toString: function() {return '[OBJ 1]'}}
+ , OBJ2 = {toString: function() {return '[OBJ 2]'}, foo: function () {return 'bar';}}
+ , SELF = {};
+
+ gently.expect(OBJ1, 'foo', function(x) {
+ assert.strictEqual(this, SELF);
+ return x * 2;
+ });
+
+ assert.equal(gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]), 10);
+
+ (function testAutorestore() {
+ assert.equal(OBJ2.foo(), 'bar');
+
+ gently.expect(OBJ2, 'foo', function() {
+ return 'stubbed foo';
+ });
+
+ gently.expect(OBJ2, 'foo', function() {
+ return "didn't restore yet";
+ });
+
+ assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), 'stubbed foo');
+ assert.equal(gently._stubFn(SELF, OBJ2, 'foo', 'dummy_name', []), "didn't restore yet");
+ assert.equal(OBJ2.foo(), 'bar');
+ assert.deepEqual(gently.expectations, []);
+ })();
+
+ (function testNoMoreCallExpected() {
+ try {
+ gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
+ assert.ok(false, 'throw needs to happen');
+ } catch (e) {
+ assert.equal(e.message, 'Unexpected call to dummy_name, no call was expected');
+ }
+ })();
+
+ (function testDifferentCallExpected() {
+ gently.expect(OBJ2, 'bar');
+ try {
+ gently._stubFn(SELF, OBJ1, 'foo', 'dummy_name', [5]);
+ assert.ok(false, 'throw needs to happen');
+ } catch (e) {
+ assert.equal(e.message, 'Unexpected call to dummy_name, expected call to '+gently._name(OBJ2, 'bar'));
+ }
+
+ assert.equal(gently.expectations.length, 1);
+ })();
+
+ (function testNoMockCallback() {
+ OBJ2.bar();
+ assert.equal(gently.expectations.length, 0);
+ })();
+});
+
+test(function stub() {
+ var LOCATION = './my_class';
+
+ (function testRegular() {
+ var Stub = gently.stub(LOCATION);
+ assert.ok(Stub instanceof Function);
+ assert.strictEqual(gently.hijacked[LOCATION], Stub);
+ assert.ok(Stub['new'] instanceof Function);
+ assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
+
+ (function testConstructor() {
+ var newCalled = 0
+ , STUB
+ , ARGS = ['foo', 'bar'];
+
+ Stub['new'] = function(a, b) {
+ assert.equal(a, ARGS[0]);
+ assert.equal(b, ARGS[1]);
+ newCalled++;
+ STUB = this;
+ };
+
+ var stub = new Stub(ARGS[0], ARGS[1]);
+ assert.strictEqual(stub, STUB);
+ assert.equal(newCalled, 1);
+ assert.equal(stub.toString(), 'require('+JSON.stringify(LOCATION)+')');
+ })();
+
+ (function testUseReturnValueAsInstance() {
+ var R = {};
+
+ Stub['new'] = function() {
+ return R;
+ };
+
+ var stub = new Stub();
+ assert.strictEqual(stub, R);
+
+ })();
+ })();
+
+ var EXPORTS_NAME = 'MyClass';
+ test(function testExportsName() {
+ var Stub = gently.stub(LOCATION, EXPORTS_NAME);
+ assert.strictEqual(gently.hijacked[LOCATION][EXPORTS_NAME], Stub);
+ assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
+
+ (function testConstructor() {
+ var stub = new Stub();
+ assert.equal(Stub.toString(), 'require('+JSON.stringify(LOCATION)+').'+EXPORTS_NAME);
+ })();
+ });
+});
+
+test(function hijack() {
+ var LOCATION = './foo'
+ , REQUIRE_CALLS = 0
+ , EXPORTS = {}
+ , REQUIRE = function() {
+ REQUIRE_CALLS++;
+ return EXPORTS;
+ };
+
+ var hijackedRequire = gently.hijack(REQUIRE);
+ hijackedRequire(LOCATION);
+ assert.strictEqual(gently.hijacked[LOCATION], EXPORTS);
+
+ assert.equal(REQUIRE_CALLS, 1);
+
+ // make sure we are caching the hijacked module
+ hijackedRequire(LOCATION);
+ assert.equal(REQUIRE_CALLS, 1);
+});
+
+test(function verify() {
+ var OBJ = {toString: function() {return '[OBJ]'}};
+ gently.verify();
+
+ gently.expect(OBJ, 'foo');
+ try {
+ gently.verify();
+ assert.ok(false, 'throw needs to happen');
+ } catch (e) {
+ assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen');
+ }
+
+ try {
+ gently.verify('foo');
+ assert.ok(false, 'throw needs to happen');
+ } catch (e) {
+ assert.equal(e.message, 'Expected call to [OBJ].foo() did not happen (foo)');
+ }
+});
+
+test(function processExit() {
+ var verifyCalled = 0;
+ gently.verify = function(msg) {
+ verifyCalled++;
+ assert.equal(msg, 'process exit');
+ };
+
+ process.emit('exit');
+ assert.equal(verifyCalled, 1);
+});
+
+test(function _name() {
+ (function testNamedClass() {
+ function Foo() {};
+ var foo = new Foo();
+ assert.equal(gently._name(foo, 'bar'), '[Foo].bar()');
+ })();
+
+ (function testToStringPreference() {
+ function Foo() {};
+ Foo.prototype.toString = function() {
+ return '[Superman 123]';
+ };
+ var foo = new Foo();
+ assert.equal(gently._name(foo, 'bar'), '[Superman 123].bar()');
+ })();
+
+ (function testUnamedClass() {
+ var Foo = function() {};
+ var foo = new Foo();
+ assert.equal(gently._name(foo, 'bar'), foo.toString()+'.bar()');
+ })();
+
+ (function testNamedClosure() {
+ function myClosure() {};
+ assert.equal(gently._name(null, null, myClosure), myClosure.name+'()');
+ })();
+
+ (function testUnamedClosure() {
+ var myClosure = function() {2+2 == 5};
+ assert.equal(gently._name(null, null, myClosure), '>> '+myClosure.toString()+' <<');
+ })();
+});
+
+test(function verifyExpectNone() {
+ var OBJ = {toString: function() {return '[OBJ]'}};
+ gently.verify();
+
+ gently.expect(OBJ, 'foo', 0);
+ try {
+ gently.verify();
+ } catch (e) {
+ assert.fail('Exception should not have been thrown');
+ }
+});
\ No newline at end of file
diff --git a/node_modules/formidable/package.json b/node_modules/formidable/package.json
new file mode 100644
index 0000000..7aa4342
--- /dev/null
+++ b/node_modules/formidable/package.json
@@ -0,0 +1,28 @@
+{
+ "name": "formidable",
+ "version": "1.0.11",
+ "dependencies": {},
+ "devDependencies": {
+ "gently": "0.8.0",
+ "findit": "0.1.1",
+ "hashish": "0.0.4",
+ "urun": "0.0.4",
+ "utest": "0.0.3"
+ },
+ "directories": {
+ "lib": "./lib"
+ },
+ "main": "./lib/index",
+ "scripts": {
+ "test": "make test"
+ },
+ "engines": {
+ "node": "*"
+ },
+ "optionalDependencies": {},
+ "readme": "# Formidable\n\n[](http://travis-ci.org/felixge/node-formidable)\n\n## Purpose\n\nA node.js module for parsing form data, especially file uploads.\n\n## Current status\n\nThis module was developed for [Transloadit](http://transloadit.com/), a service focused on uploading\nand encoding images and videos. It has been battle-tested against hundreds of GB of file uploads from\na large variety of clients and is considered production-ready.\n\n## Features\n\n* Fast (~500mb/sec), non-buffering multipart parser\n* Automatically writing file uploads to disk\n* Low memory footprint\n* Graceful error handling\n* Very high test coverage\n\n## Changelog\n\n### v1.0.9\n\n* Emit progress when content length header parsed (Tim Koschützki)\n* Fix Readme syntax due to GitHub changes (goob)\n* Replace references to old 'sys' module in Readme with 'util' (Peter Sugihara)\n\n### v1.0.8\n\n* Strip potentially unsafe characters when using `keepExtensions: true`.\n* Switch to utest / urun for testing\n* Add travis build\n\n### v1.0.7\n\n* Remove file from package that was causing problems when installing on windows. (#102)\n* Fix typos in Readme (Jason Davies).\n\n### v1.0.6\n\n* Do not default to the default to the field name for file uploads where\n filename=\"\".\n\n### v1.0.5\n\n* Support filename=\"\" in multipart parts\n* Explain unexpected end() errors in parser better\n\n**Note:** Starting with this version, formidable emits 'file' events for empty\nfile input fields. Previously those were incorrectly emitted as regular file\ninput fields with value = \"\".\n\n### v1.0.4\n\n* Detect a good default tmp directory regardless of platform. (#88)\n\n### v1.0.3\n\n* Fix problems with utf8 characters (#84) / semicolons in filenames (#58)\n* Small performance improvements\n* New test suite and fixture system\n\n### v1.0.2\n\n* Exclude node\\_modules folder from git\n* Implement new `'aborted'` event\n* Fix files in example folder to work with recent node versions\n* Make gently a devDependency\n\n[See Commits](https://github.com/felixge/node-formidable/compare/v1.0.1...v1.0.2)\n\n### v1.0.1\n\n* Fix package.json to refer to proper main directory. (#68, Dean Landolt)\n\n[See Commits](https://github.com/felixge/node-formidable/compare/v1.0.0...v1.0.1)\n\n### v1.0.0\n\n* Add support for multipart boundaries that are quoted strings. (Jeff Craig)\n\nThis marks the beginning of development on version 2.0 which will include\nseveral architectural improvements.\n\n[See Commits](https://github.com/felixge/node-formidable/compare/v0.9.11...v1.0.0)\n\n### v0.9.11\n\n* Emit `'progress'` event when receiving data, regardless of parsing it. (Tim Koschützki)\n* Use [W3C FileAPI Draft](http://dev.w3.org/2006/webapi/FileAPI/) properties for File class\n\n**Important:** The old property names of the File class will be removed in a\nfuture release.\n\n[See Commits](https://github.com/felixge/node-formidable/compare/v0.9.10...v0.9.11)\n\n### Older releases\n\nThese releases were done before starting to maintain the above Changelog:\n\n* [v0.9.10](https://github.com/felixge/node-formidable/compare/v0.9.9...v0.9.10)\n* [v0.9.9](https://github.com/felixge/node-formidable/compare/v0.9.8...v0.9.9)\n* [v0.9.8](https://github.com/felixge/node-formidable/compare/v0.9.7...v0.9.8)\n* [v0.9.7](https://github.com/felixge/node-formidable/compare/v0.9.6...v0.9.7)\n* [v0.9.6](https://github.com/felixge/node-formidable/compare/v0.9.5...v0.9.6)\n* [v0.9.5](https://github.com/felixge/node-formidable/compare/v0.9.4...v0.9.5)\n* [v0.9.4](https://github.com/felixge/node-formidable/compare/v0.9.3...v0.9.4)\n* [v0.9.3](https://github.com/felixge/node-formidable/compare/v0.9.2...v0.9.3)\n* [v0.9.2](https://github.com/felixge/node-formidable/compare/v0.9.1...v0.9.2)\n* [v0.9.1](https://github.com/felixge/node-formidable/compare/v0.9.0...v0.9.1)\n* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)\n* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)\n* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)\n* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)\n* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)\n* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)\n* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)\n* [v0.9.0](https://github.com/felixge/node-formidable/compare/v0.8.0...v0.9.0)\n* [v0.1.0](https://github.com/felixge/node-formidable/commits/v0.1.0)\n\n## Installation\n\nVia [npm](http://github.com/isaacs/npm):\n\n npm install formidable@latest\n\nManually:\n\n git clone git://github.com/felixge/node-formidable.git formidable\n vim my.js\n # var formidable = require('./formidable');\n\nNote: Formidable requires [gently](http://github.com/felixge/node-gently) to run the unit tests, but you won't need it for just using the library.\n\n## Example\n\nParse an incoming file upload.\n\n var formidable = require('formidable'),\n http = require('http'),\n\n util = require('util');\n\n http.createServer(function(req, res) {\n if (req.url == '/upload' && req.method.toLowerCase() == 'post') {\n // parse a file upload\n var form = new formidable.IncomingForm();\n form.parse(req, function(err, fields, files) {\n res.writeHead(200, {'content-type': 'text/plain'});\n res.write('received upload:\\n\\n');\n res.end(util.inspect({fields: fields, files: files}));\n });\n return;\n }\n\n // show a file upload form\n res.writeHead(200, {'content-type': 'text/html'});\n res.end(\n ''\n );\n }).listen(80);\n\n## API\n\n### formidable.IncomingForm\n\n__new formidable.IncomingForm()__\n\nCreates a new incoming form.\n\n__incomingForm.encoding = 'utf-8'__\n\nThe encoding to use for incoming form fields.\n\n__incomingForm.uploadDir = process.env.TMP || '/tmp' || process.cwd()__\n\nThe directory for placing file uploads in. You can move them later on using\n`fs.rename()`. The default directory is picked at module load time depending on\nthe first existing directory from those listed above.\n\n__incomingForm.keepExtensions = false__\n\nIf you want the files written to `incomingForm.uploadDir` to include the extensions of the original files, set this property to `true`.\n\n__incomingForm.type__\n\nEither 'multipart' or 'urlencoded' depending on the incoming request.\n\n__incomingForm.maxFieldsSize = 2 * 1024 * 1024__\n\nLimits the amount of memory a field (not file) can allocate in bytes.\nIf this value is exceeded, an `'error'` event is emitted. The default\nsize is 2MB.\n\n__incomingForm.hash = false__\n\nIf you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`.\n\n__incomingForm.bytesReceived__\n\nThe amount of bytes received for this form so far.\n\n__incomingForm.bytesExpected__\n\nThe expected number of bytes in this form.\n\n__incomingForm.parse(request, [cb])__\n\nParses an incoming node.js `request` containing form data. If `cb` is provided, all fields an files are collected and passed to the callback:\n\n incomingForm.parse(req, function(err, fields, files) {\n // ...\n });\n\n__incomingForm.onPart(part)__\n\nYou may overwrite this method if you are interested in directly accessing the multipart stream. Doing so will disable any `'field'` / `'file'` events processing which would occur otherwise, making you fully responsible for handling the processing.\n\n incomingForm.onPart = function(part) {\n part.addListener('data', function() {\n // ...\n });\n }\n\nIf you want to use formidable to only handle certain parts for you, you can do so:\n\n incomingForm.onPart = function(part) {\n if (!part.filename) {\n // let formidable handle all non-file parts\n incomingForm.handlePart(part);\n }\n }\n\nCheck the code in this method for further inspiration.\n\n__Event: 'progress' (bytesReceived, bytesExpected)__\n\nEmitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.\n\n__Event: 'field' (name, value)__\n\nEmitted whenever a field / value pair has been received.\n\n__Event: 'fileBegin' (name, file)__\n\nEmitted whenever a new file is detected in the upload stream. Use this even if\nyou want to stream the file to somewhere else while buffering the upload on\nthe file system.\n\n__Event: 'file' (name, file)__\n\nEmitted whenever a field / file pair has been received. `file` is an instance of `File`.\n\n__Event: 'error' (err)__\n\nEmitted when there is an error processing the incoming form. A request that experiences an error is automatically paused, you will have to manually call `request.resume()` if you want the request to continue firing `'data'` events.\n\n__Event: 'aborted'__\n\nEmitted when the request was aborted by the user. Right now this can be due to a 'timeout' or 'close' event on the socket. In the future there will be a separate 'timeout' event (needs a change in the node core).\n\n__Event: 'end' ()__\n\nEmitted when the entire request has been received, and all contained files have finished flushing to disk. This is a great place for you to send your response.\n\n### formidable.File\n\n__file.size = 0__\n\nThe size of the uploaded file in bytes. If the file is still being uploaded (see `'fileBegin'` event), this property says how many bytes of the file have been written to disk yet.\n\n__file.path = null__\n\nThe path this file is being written to. You can modify this in the `'fileBegin'` event in\ncase you are unhappy with the way formidable generates a temporary path for your files.\n\n__file.name = null__\n\nThe name this file had according to the uploading client.\n\n__file.type = null__\n\nThe mime type of this file, according to the uploading client.\n\n__file.lastModifiedDate = null__\n\nA date object (or `null`) containing the time this file was last written to. Mostly\nhere for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/).\n\n__file.hash = null__\n\nIf hash calculation was set, you can read the hex digest out of this var.\n\n## License\n\nFormidable is licensed under the MIT license.\n\n## Ports\n\n* [multipart-parser](http://github.com/FooBarWidget/multipart-parser): a C++ parser based on formidable\n\n## Credits\n\n* [Ryan Dahl](http://twitter.com/ryah) for his work on [http-parser](http://github.com/ry/http-parser) which heavily inspired multipart_parser.js\n",
+ "readmeFilename": "Readme.md",
+ "_id": "formidable@1.0.11",
+ "description": "[](http://travis-ci.org/felixge/node-formidable)",
+ "_from": "formidable@>= 0.0.1"
+}
diff --git a/node_modules/formidable/test/common.js b/node_modules/formidable/test/common.js
new file mode 100644
index 0000000..eb432ad
--- /dev/null
+++ b/node_modules/formidable/test/common.js
@@ -0,0 +1,19 @@
+var mysql = require('..');
+var path = require('path');
+
+var root = path.join(__dirname, '../');
+exports.dir = {
+ root : root,
+ lib : root + '/lib',
+ fixture : root + '/test/fixture',
+ tmp : root + '/test/tmp',
+};
+
+exports.port = 13532;
+
+exports.formidable = require('..');
+exports.assert = require('assert');
+
+exports.require = function(lib) {
+ return require(exports.dir.lib + '/' + lib);
+};
diff --git a/node_modules/formidable/test/fixture/file/funkyfilename.txt b/node_modules/formidable/test/fixture/file/funkyfilename.txt
new file mode 100644
index 0000000..e7a4785
--- /dev/null
+++ b/node_modules/formidable/test/fixture/file/funkyfilename.txt
@@ -0,0 +1 @@
+I am a text file with a funky name!
diff --git a/node_modules/formidable/test/fixture/file/plain.txt b/node_modules/formidable/test/fixture/file/plain.txt
new file mode 100644
index 0000000..9b6903e
--- /dev/null
+++ b/node_modules/formidable/test/fixture/file/plain.txt
@@ -0,0 +1 @@
+I am a plain text file
diff --git a/node_modules/formidable/test/fixture/http/special-chars-in-filename/info.md b/node_modules/formidable/test/fixture/http/special-chars-in-filename/info.md
new file mode 100644
index 0000000..3c9dbe3
--- /dev/null
+++ b/node_modules/formidable/test/fixture/http/special-chars-in-filename/info.md
@@ -0,0 +1,3 @@
+* Opera does not allow submitting this file, it shows a warning to the
+ user that the file could not be found instead. Tested in 9.8, 11.51 on OSX.
+ Reported to Opera on 08.09.2011 (tracking email DSK-346009@bugs.opera.com).
diff --git a/node_modules/formidable/test/fixture/js/no-filename.js b/node_modules/formidable/test/fixture/js/no-filename.js
new file mode 100644
index 0000000..0bae449
--- /dev/null
+++ b/node_modules/formidable/test/fixture/js/no-filename.js
@@ -0,0 +1,3 @@
+module.exports['generic.http'] = [
+ {type: 'file', name: 'upload', filename: '', fixture: 'plain.txt'},
+];
diff --git a/node_modules/formidable/test/fixture/js/special-chars-in-filename.js b/node_modules/formidable/test/fixture/js/special-chars-in-filename.js
new file mode 100644
index 0000000..eb76fdc
--- /dev/null
+++ b/node_modules/formidable/test/fixture/js/special-chars-in-filename.js
@@ -0,0 +1,21 @@
+var properFilename = 'funkyfilename.txt';
+
+function expect(filename) {
+ return [
+ {type: 'field', name: 'title', value: 'Weird filename'},
+ {type: 'file', name: 'upload', filename: filename, fixture: properFilename},
+ ];
+};
+
+var webkit = " ? % * | \" < > . ? ; ' @ # $ ^ & ( ) - _ = + { } [ ] ` ~.txt";
+var ffOrIe = " ? % * | \" < > . ☃ ; ' @ # $ ^ & ( ) - _ = + { } [ ] ` ~.txt";
+
+module.exports = {
+ 'osx-chrome-13.http' : expect(webkit),
+ 'osx-firefox-3.6.http' : expect(ffOrIe),
+ 'osx-safari-5.http' : expect(webkit),
+ 'xp-chrome-12.http' : expect(webkit),
+ 'xp-ie-7.http' : expect(ffOrIe),
+ 'xp-ie-8.http' : expect(ffOrIe),
+ 'xp-safari-5.http' : expect(webkit),
+};
diff --git a/node_modules/formidable/test/fixture/multipart.js b/node_modules/formidable/test/fixture/multipart.js
new file mode 100644
index 0000000..a476169
--- /dev/null
+++ b/node_modules/formidable/test/fixture/multipart.js
@@ -0,0 +1,72 @@
+exports['rfc1867'] =
+ { boundary: 'AaB03x',
+ raw:
+ '--AaB03x\r\n'+
+ 'content-disposition: form-data; name="field1"\r\n'+
+ '\r\n'+
+ 'Joe Blow\r\nalmost tricked you!\r\n'+
+ '--AaB03x\r\n'+
+ 'content-disposition: form-data; name="pics"; filename="file1.txt"\r\n'+
+ 'Content-Type: text/plain\r\n'+
+ '\r\n'+
+ '... contents of file1.txt ...\r\r\n'+
+ '--AaB03x--\r\n',
+ parts:
+ [ { headers: {
+ 'content-disposition': 'form-data; name="field1"',
+ },
+ data: 'Joe Blow\r\nalmost tricked you!',
+ },
+ { headers: {
+ 'content-disposition': 'form-data; name="pics"; filename="file1.txt"',
+ 'Content-Type': 'text/plain',
+ },
+ data: '... contents of file1.txt ...\r',
+ }
+ ]
+ };
+
+exports['noTrailing\r\n'] =
+ { boundary: 'AaB03x',
+ raw:
+ '--AaB03x\r\n'+
+ 'content-disposition: form-data; name="field1"\r\n'+
+ '\r\n'+
+ 'Joe Blow\r\nalmost tricked you!\r\n'+
+ '--AaB03x\r\n'+
+ 'content-disposition: form-data; name="pics"; filename="file1.txt"\r\n'+
+ 'Content-Type: text/plain\r\n'+
+ '\r\n'+
+ '... contents of file1.txt ...\r\r\n'+
+ '--AaB03x--',
+ parts:
+ [ { headers: {
+ 'content-disposition': 'form-data; name="field1"',
+ },
+ data: 'Joe Blow\r\nalmost tricked you!',
+ },
+ { headers: {
+ 'content-disposition': 'form-data; name="pics"; filename="file1.txt"',
+ 'Content-Type': 'text/plain',
+ },
+ data: '... contents of file1.txt ...\r',
+ }
+ ]
+ };
+
+exports['emptyHeader'] =
+ { boundary: 'AaB03x',
+ raw:
+ '--AaB03x\r\n'+
+ 'content-disposition: form-data; name="field1"\r\n'+
+ ': foo\r\n'+
+ '\r\n'+
+ 'Joe Blow\r\nalmost tricked you!\r\n'+
+ '--AaB03x\r\n'+
+ 'content-disposition: form-data; name="pics"; filename="file1.txt"\r\n'+
+ 'Content-Type: text/plain\r\n'+
+ '\r\n'+
+ '... contents of file1.txt ...\r\r\n'+
+ '--AaB03x--\r\n',
+ expectError: true,
+ };
diff --git a/node_modules/formidable/test/integration/test-fixtures.js b/node_modules/formidable/test/integration/test-fixtures.js
new file mode 100644
index 0000000..66ad259
--- /dev/null
+++ b/node_modules/formidable/test/integration/test-fixtures.js
@@ -0,0 +1,89 @@
+var hashish = require('hashish');
+var fs = require('fs');
+var findit = require('findit');
+var path = require('path');
+var http = require('http');
+var net = require('net');
+var assert = require('assert');
+
+var common = require('../common');
+var formidable = common.formidable;
+
+var server = http.createServer();
+server.listen(common.port, findFixtures);
+
+function findFixtures() {
+ var fixtures = [];
+ findit
+ .sync(common.dir.fixture + '/js')
+ .forEach(function(jsPath) {
+ if (!/\.js$/.test(jsPath)) return;
+
+ var group = path.basename(jsPath, '.js');
+ hashish.forEach(require(jsPath), function(fixture, name) {
+ fixtures.push({
+ name : group + '/' + name,
+ fixture : fixture,
+ });
+ });
+ });
+
+ testNext(fixtures);
+}
+
+function testNext(fixtures) {
+ var fixture = fixtures.shift();
+ if (!fixture) return server.close();
+
+ var name = fixture.name;
+ var fixture = fixture.fixture;
+
+ uploadFixture(name, function(err, parts) {
+ if (err) throw err;
+
+ fixture.forEach(function(expectedPart, i) {
+ var parsedPart = parts[i];
+ assert.equal(parsedPart.type, expectedPart.type);
+ assert.equal(parsedPart.name, expectedPart.name);
+
+ if (parsedPart.type === 'file') {
+ var filename = parsedPart.value.name;
+ assert.equal(filename, expectedPart.filename);
+ }
+ });
+
+ testNext(fixtures);
+ });
+};
+
+function uploadFixture(name, cb) {
+ server.once('request', function(req, res) {
+ var form = new formidable.IncomingForm();
+ form.uploadDir = common.dir.tmp;
+ form.parse(req);
+
+ function callback() {
+ var realCallback = cb;
+ cb = function() {};
+ realCallback.apply(null, arguments);
+ }
+
+ var parts = [];
+ form
+ .on('error', callback)
+ .on('fileBegin', function(name, value) {
+ parts.push({type: 'file', name: name, value: value});
+ })
+ .on('field', function(name, value) {
+ parts.push({type: 'field', name: name, value: value});
+ })
+ .on('end', function() {
+ callback(null, parts);
+ });
+ });
+
+ var socket = net.createConnection(common.port);
+ var file = fs.createReadStream(common.dir.fixture + '/http/' + name);
+
+ file.pipe(socket);
+}
diff --git a/node_modules/formidable/test/legacy/common.js b/node_modules/formidable/test/legacy/common.js
new file mode 100644
index 0000000..2b98598
--- /dev/null
+++ b/node_modules/formidable/test/legacy/common.js
@@ -0,0 +1,24 @@
+var path = require('path'),
+ fs = require('fs');
+
+try {
+ global.Gently = require('gently');
+} catch (e) {
+ throw new Error('this test suite requires node-gently');
+}
+
+exports.lib = path.join(__dirname, '../../lib');
+
+global.GENTLY = new Gently();
+
+global.assert = require('assert');
+global.TEST_PORT = 13532;
+global.TEST_FIXTURES = path.join(__dirname, '../fixture');
+global.TEST_TMP = path.join(__dirname, '../tmp');
+
+// Stupid new feature in node that complains about gently attaching too many
+// listeners to process 'exit'. This is a workaround until I can think of a
+// better way to deal with this.
+if (process.setMaxListeners) {
+ process.setMaxListeners(10000);
+}
diff --git a/node_modules/formidable/test/legacy/integration/test-multipart-parser.js b/node_modules/formidable/test/legacy/integration/test-multipart-parser.js
new file mode 100644
index 0000000..75232aa
--- /dev/null
+++ b/node_modules/formidable/test/legacy/integration/test-multipart-parser.js
@@ -0,0 +1,80 @@
+var common = require('../common');
+var CHUNK_LENGTH = 10,
+ multipartParser = require(common.lib + '/multipart_parser'),
+ MultipartParser = multipartParser.MultipartParser,
+ parser = new MultipartParser(),
+ fixtures = require(TEST_FIXTURES + '/multipart'),
+ Buffer = require('buffer').Buffer;
+
+Object.keys(fixtures).forEach(function(name) {
+ var fixture = fixtures[name],
+ buffer = new Buffer(Buffer.byteLength(fixture.raw, 'binary')),
+ offset = 0,
+ chunk,
+ nparsed,
+
+ parts = [],
+ part = null,
+ headerField,
+ headerValue,
+ endCalled = '';
+
+ parser.initWithBoundary(fixture.boundary);
+ parser.onPartBegin = function() {
+ part = {headers: {}, data: ''};
+ parts.push(part);
+ headerField = '';
+ headerValue = '';
+ };
+
+ parser.onHeaderField = function(b, start, end) {
+ headerField += b.toString('ascii', start, end);
+ };
+
+ parser.onHeaderValue = function(b, start, end) {
+ headerValue += b.toString('ascii', start, end);
+ }
+
+ parser.onHeaderEnd = function() {
+ part.headers[headerField] = headerValue;
+ headerField = '';
+ headerValue = '';
+ };
+
+ parser.onPartData = function(b, start, end) {
+ var str = b.toString('ascii', start, end);
+ part.data += b.slice(start, end);
+ }
+
+ parser.onEnd = function() {
+ endCalled = true;
+ }
+
+ buffer.write(fixture.raw, 'binary', 0);
+
+ while (offset < buffer.length) {
+ if (offset + CHUNK_LENGTH < buffer.length) {
+ chunk = buffer.slice(offset, offset+CHUNK_LENGTH);
+ } else {
+ chunk = buffer.slice(offset, buffer.length);
+ }
+ offset = offset + CHUNK_LENGTH;
+
+ nparsed = parser.write(chunk);
+ if (nparsed != chunk.length) {
+ if (fixture.expectError) {
+ return;
+ }
+ puts('-- ERROR --');
+ p(chunk.toString('ascii'));
+ throw new Error(chunk.length+' bytes written, but only '+nparsed+' bytes parsed!');
+ }
+ }
+
+ if (fixture.expectError) {
+ throw new Error('expected parse error did not happen');
+ }
+
+ assert.ok(endCalled);
+ assert.deepEqual(parts, fixture.parts);
+});
diff --git a/node_modules/formidable/test/legacy/simple/test-file.js b/node_modules/formidable/test/legacy/simple/test-file.js
new file mode 100644
index 0000000..52ceedb
--- /dev/null
+++ b/node_modules/formidable/test/legacy/simple/test-file.js
@@ -0,0 +1,104 @@
+var common = require('../common');
+var WriteStreamStub = GENTLY.stub('fs', 'WriteStream');
+
+var File = require(common.lib + '/file'),
+ EventEmitter = require('events').EventEmitter,
+ file,
+ gently;
+
+function test(test) {
+ gently = new Gently();
+ file = new File();
+ test();
+ gently.verify(test.name);
+}
+
+test(function constructor() {
+ assert.ok(file instanceof EventEmitter);
+ assert.strictEqual(file.size, 0);
+ assert.strictEqual(file.path, null);
+ assert.strictEqual(file.name, null);
+ assert.strictEqual(file.type, null);
+ assert.strictEqual(file.lastModifiedDate, null);
+
+ assert.strictEqual(file._writeStream, null);
+
+ (function testSetProperties() {
+ var file2 = new File({foo: 'bar'});
+ assert.equal(file2.foo, 'bar');
+ })();
+});
+
+test(function open() {
+ var WRITE_STREAM;
+ file.path = '/foo';
+
+ gently.expect(WriteStreamStub, 'new', function (path) {
+ WRITE_STREAM = this;
+ assert.strictEqual(path, file.path);
+ });
+
+ file.open();
+ assert.strictEqual(file._writeStream, WRITE_STREAM);
+});
+
+test(function write() {
+ var BUFFER = {length: 10},
+ CB_STUB,
+ CB = function() {
+ CB_STUB.apply(this, arguments);
+ };
+
+ file._writeStream = {};
+
+ gently.expect(file._writeStream, 'write', function (buffer, cb) {
+ assert.strictEqual(buffer, BUFFER);
+
+ gently.expect(file, 'emit', function (event, bytesWritten) {
+ assert.ok(file.lastModifiedDate instanceof Date);
+ assert.equal(event, 'progress');
+ assert.equal(bytesWritten, file.size);
+ });
+
+ CB_STUB = gently.expect(function writeCb() {
+ assert.equal(file.size, 10);
+ });
+
+ cb();
+
+ gently.expect(file, 'emit', function (event, bytesWritten) {
+ assert.equal(event, 'progress');
+ assert.equal(bytesWritten, file.size);
+ });
+
+ CB_STUB = gently.expect(function writeCb() {
+ assert.equal(file.size, 20);
+ });
+
+ cb();
+ });
+
+ file.write(BUFFER, CB);
+});
+
+test(function end() {
+ var CB_STUB,
+ CB = function() {
+ CB_STUB.apply(this, arguments);
+ };
+
+ file._writeStream = {};
+
+ gently.expect(file._writeStream, 'end', function (cb) {
+ gently.expect(file, 'emit', function (event) {
+ assert.equal(event, 'end');
+ });
+
+ CB_STUB = gently.expect(function endCb() {
+ });
+
+ cb();
+ });
+
+ file.end(CB);
+});
diff --git a/node_modules/formidable/test/legacy/simple/test-incoming-form.js b/node_modules/formidable/test/legacy/simple/test-incoming-form.js
new file mode 100644
index 0000000..84de439
--- /dev/null
+++ b/node_modules/formidable/test/legacy/simple/test-incoming-form.js
@@ -0,0 +1,727 @@
+var common = require('../common');
+var MultipartParserStub = GENTLY.stub('./multipart_parser', 'MultipartParser'),
+ QuerystringParserStub = GENTLY.stub('./querystring_parser', 'QuerystringParser'),
+ EventEmitterStub = GENTLY.stub('events', 'EventEmitter'),
+ StreamStub = GENTLY.stub('stream', 'Stream'),
+ FileStub = GENTLY.stub('./file');
+
+var formidable = require(common.lib + '/index'),
+ IncomingForm = formidable.IncomingForm,
+ events = require('events'),
+ fs = require('fs'),
+ path = require('path'),
+ Buffer = require('buffer').Buffer,
+ fixtures = require(TEST_FIXTURES + '/multipart'),
+ form,
+ gently;
+
+function test(test) {
+ gently = new Gently();
+ gently.expect(EventEmitterStub, 'call');
+ form = new IncomingForm();
+ test();
+ gently.verify(test.name);
+}
+
+test(function constructor() {
+ assert.strictEqual(form.error, null);
+ assert.strictEqual(form.ended, false);
+ assert.strictEqual(form.type, null);
+ assert.strictEqual(form.headers, null);
+ assert.strictEqual(form.keepExtensions, false);
+ assert.strictEqual(form.uploadDir, '/tmp');
+ assert.strictEqual(form.encoding, 'utf-8');
+ assert.strictEqual(form.bytesReceived, null);
+ assert.strictEqual(form.bytesExpected, null);
+ assert.strictEqual(form.maxFieldsSize, 2 * 1024 * 1024);
+ assert.strictEqual(form._parser, null);
+ assert.strictEqual(form._flushing, 0);
+ assert.strictEqual(form._fieldsSize, 0);
+ assert.ok(form instanceof EventEmitterStub);
+ assert.equal(form.constructor.name, 'IncomingForm');
+
+ (function testSimpleConstructor() {
+ gently.expect(EventEmitterStub, 'call');
+ var form = IncomingForm();
+ assert.ok(form instanceof IncomingForm);
+ })();
+
+ (function testSimpleConstructorShortcut() {
+ gently.expect(EventEmitterStub, 'call');
+ var form = formidable();
+ assert.ok(form instanceof IncomingForm);
+ })();
+});
+
+test(function parse() {
+ var REQ = {headers: {}}
+ , emit = {};
+
+ gently.expect(form, 'writeHeaders', function(headers) {
+ assert.strictEqual(headers, REQ.headers);
+ });
+
+ var events = ['error', 'aborted', 'data', 'end'];
+ gently.expect(REQ, 'on', events.length, function(event, fn) {
+ assert.equal(event, events.shift());
+ emit[event] = fn;
+ return this;
+ });
+
+ form.parse(REQ);
+
+ (function testPause() {
+ gently.expect(REQ, 'pause');
+ assert.strictEqual(form.pause(), true);
+ })();
+
+ (function testPauseCriticalException() {
+ form.ended = false;
+
+ var ERR = new Error('dasdsa');
+ gently.expect(REQ, 'pause', function() {
+ throw ERR;
+ });
+
+ gently.expect(form, '_error', function(err) {
+ assert.strictEqual(err, ERR);
+ });
+
+ assert.strictEqual(form.pause(), false);
+ })();
+
+ (function testPauseHarmlessException() {
+ form.ended = true;
+
+ var ERR = new Error('dasdsa');
+ gently.expect(REQ, 'pause', function() {
+ throw ERR;
+ });
+
+ assert.strictEqual(form.pause(), false);
+ })();
+
+ (function testResume() {
+ gently.expect(REQ, 'resume');
+ assert.strictEqual(form.resume(), true);
+ })();
+
+ (function testResumeCriticalException() {
+ form.ended = false;
+
+ var ERR = new Error('dasdsa');
+ gently.expect(REQ, 'resume', function() {
+ throw ERR;
+ });
+
+ gently.expect(form, '_error', function(err) {
+ assert.strictEqual(err, ERR);
+ });
+
+ assert.strictEqual(form.resume(), false);
+ })();
+
+ (function testResumeHarmlessException() {
+ form.ended = true;
+
+ var ERR = new Error('dasdsa');
+ gently.expect(REQ, 'resume', function() {
+ throw ERR;
+ });
+
+ assert.strictEqual(form.resume(), false);
+ })();
+
+ (function testEmitError() {
+ var ERR = new Error('something bad happened');
+ gently.expect(form, '_error',function(err) {
+ assert.strictEqual(err, ERR);
+ });
+ emit.error(ERR);
+ })();
+
+ (function testEmitAborted() {
+ gently.expect(form, 'emit',function(event) {
+ assert.equal(event, 'aborted');
+ });
+
+ emit.aborted();
+ })();
+
+
+ (function testEmitData() {
+ var BUFFER = [1, 2, 3];
+ gently.expect(form, 'write', function(buffer) {
+ assert.strictEqual(buffer, BUFFER);
+ });
+ emit.data(BUFFER);
+ })();
+
+ (function testEmitEnd() {
+ form._parser = {};
+
+ (function testWithError() {
+ var ERR = new Error('haha');
+ gently.expect(form._parser, 'end', function() {
+ return ERR;
+ });
+
+ gently.expect(form, '_error', function(err) {
+ assert.strictEqual(err, ERR);
+ });
+
+ emit.end();
+ })();
+
+ (function testWithoutError() {
+ gently.expect(form._parser, 'end');
+ emit.end();
+ })();
+
+ (function testAfterError() {
+ form.error = true;
+ emit.end();
+ })();
+ })();
+
+ (function testWithCallback() {
+ gently.expect(EventEmitterStub, 'call');
+ var form = new IncomingForm(),
+ REQ = {headers: {}},
+ parseCalled = 0;
+
+ gently.expect(form, 'writeHeaders');
+ gently.expect(REQ, 'on', 4, function() {
+ return this;
+ });
+
+ gently.expect(form, 'on', 4, function(event, fn) {
+ if (event == 'field') {
+ fn('field1', 'foo');
+ fn('field1', 'bar');
+ fn('field2', 'nice');
+ }
+
+ if (event == 'file') {
+ fn('file1', '1');
+ fn('file1', '2');
+ fn('file2', '3');
+ }
+
+ if (event == 'end') {
+ fn();
+ }
+ return this;
+ });
+
+ form.parse(REQ, gently.expect(function parseCbOk(err, fields, files) {
+ assert.deepEqual(fields, {field1: 'bar', field2: 'nice'});
+ assert.deepEqual(files, {file1: '2', file2: '3'});
+ }));
+
+ gently.expect(form, 'writeHeaders');
+ gently.expect(REQ, 'on', 4, function() {
+ return this;
+ });
+
+ var ERR = new Error('test');
+ gently.expect(form, 'on', 3, function(event, fn) {
+ if (event == 'field') {
+ fn('foo', 'bar');
+ }
+
+ if (event == 'error') {
+ fn(ERR);
+ gently.expect(form, 'on');
+ }
+ return this;
+ });
+
+ form.parse(REQ, gently.expect(function parseCbErr(err, fields, files) {
+ assert.strictEqual(err, ERR);
+ assert.deepEqual(fields, {foo: 'bar'});
+ }));
+ })();
+});
+
+test(function pause() {
+ assert.strictEqual(form.pause(), false);
+});
+
+test(function resume() {
+ assert.strictEqual(form.resume(), false);
+});
+
+
+test(function writeHeaders() {
+ var HEADERS = {};
+ gently.expect(form, '_parseContentLength');
+ gently.expect(form, '_parseContentType');
+
+ form.writeHeaders(HEADERS);
+ assert.strictEqual(form.headers, HEADERS);
+});
+
+test(function write() {
+ var parser = {},
+ BUFFER = [1, 2, 3];
+
+ form._parser = parser;
+ form.bytesExpected = 523423;
+
+ (function testBasic() {
+ gently.expect(form, 'emit', function(event, bytesReceived, bytesExpected) {
+ assert.equal(event, 'progress');
+ assert.equal(bytesReceived, BUFFER.length);
+ assert.equal(bytesExpected, form.bytesExpected);
+ });
+
+ gently.expect(parser, 'write', function(buffer) {
+ assert.strictEqual(buffer, BUFFER);
+ return buffer.length;
+ });
+
+ assert.equal(form.write(BUFFER), BUFFER.length);
+ assert.equal(form.bytesReceived, BUFFER.length);
+ })();
+
+ (function testParserError() {
+ gently.expect(form, 'emit');
+
+ gently.expect(parser, 'write', function(buffer) {
+ assert.strictEqual(buffer, BUFFER);
+ return buffer.length - 1;
+ });
+
+ gently.expect(form, '_error', function(err) {
+ assert.ok(err.message.match(/parser error/i));
+ });
+
+ assert.equal(form.write(BUFFER), BUFFER.length - 1);
+ assert.equal(form.bytesReceived, BUFFER.length + BUFFER.length);
+ })();
+
+ (function testUninitialized() {
+ delete form._parser;
+
+ gently.expect(form, '_error', function(err) {
+ assert.ok(err.message.match(/unintialized parser/i));
+ });
+ form.write(BUFFER);
+ })();
+});
+
+test(function parseContentType() {
+ var HEADERS = {};
+
+ form.headers = {'content-type': 'application/x-www-form-urlencoded'};
+ gently.expect(form, '_initUrlencoded');
+ form._parseContentType();
+
+ // accept anything that has 'urlencoded' in it
+ form.headers = {'content-type': 'broken-client/urlencoded-stupid'};
+ gently.expect(form, '_initUrlencoded');
+ form._parseContentType();
+
+ var BOUNDARY = '---------------------------57814261102167618332366269';
+ form.headers = {'content-type': 'multipart/form-data; boundary='+BOUNDARY};
+
+ gently.expect(form, '_initMultipart', function(boundary) {
+ assert.equal(boundary, BOUNDARY);
+ });
+ form._parseContentType();
+
+ (function testQuotedBoundary() {
+ form.headers = {'content-type': 'multipart/form-data; boundary="' + BOUNDARY + '"'};
+
+ gently.expect(form, '_initMultipart', function(boundary) {
+ assert.equal(boundary, BOUNDARY);
+ });
+ form._parseContentType();
+ })();
+
+ (function testNoBoundary() {
+ form.headers = {'content-type': 'multipart/form-data'};
+
+ gently.expect(form, '_error', function(err) {
+ assert.ok(err.message.match(/no multipart boundary/i));
+ });
+ form._parseContentType();
+ })();
+
+ (function testNoContentType() {
+ form.headers = {};
+
+ gently.expect(form, '_error', function(err) {
+ assert.ok(err.message.match(/no content-type/i));
+ });
+ form._parseContentType();
+ })();
+
+ (function testUnknownContentType() {
+ form.headers = {'content-type': 'invalid'};
+
+ gently.expect(form, '_error', function(err) {
+ assert.ok(err.message.match(/unknown content-type/i));
+ });
+ form._parseContentType();
+ })();
+});
+
+test(function parseContentLength() {
+ var HEADERS = {};
+
+ form.headers = {};
+ form._parseContentLength();
+ assert.strictEqual(form.bytesReceived, null);
+ assert.strictEqual(form.bytesExpected, null);
+
+ form.headers['content-length'] = '8';
+ gently.expect(form, 'emit', function(event, bytesReceived, bytesExpected) {
+ assert.equal(event, 'progress');
+ assert.equal(bytesReceived, 0);
+ assert.equal(bytesExpected, 8);
+ });
+ form._parseContentLength();
+ assert.strictEqual(form.bytesReceived, 0);
+ assert.strictEqual(form.bytesExpected, 8);
+
+ // JS can be evil, lets make sure we are not
+ form.headers['content-length'] = '08';
+ gently.expect(form, 'emit', function(event, bytesReceived, bytesExpected) {
+ assert.equal(event, 'progress');
+ assert.equal(bytesReceived, 0);
+ assert.equal(bytesExpected, 8);
+ });
+ form._parseContentLength();
+ assert.strictEqual(form.bytesExpected, 8);
+});
+
+test(function _initMultipart() {
+ var BOUNDARY = '123',
+ PARSER;
+
+ gently.expect(MultipartParserStub, 'new', function() {
+ PARSER = this;
+ });
+
+ gently.expect(MultipartParserStub.prototype, 'initWithBoundary', function(boundary) {
+ assert.equal(boundary, BOUNDARY);
+ });
+
+ form._initMultipart(BOUNDARY);
+ assert.equal(form.type, 'multipart');
+ assert.strictEqual(form._parser, PARSER);
+
+ (function testRegularField() {
+ var PART;
+ gently.expect(StreamStub, 'new', function() {
+ PART = this;
+ });
+
+ gently.expect(form, 'onPart', function(part) {
+ assert.strictEqual(part, PART);
+ assert.deepEqual
+ ( part.headers
+ , { 'content-disposition': 'form-data; name="field1"'
+ , 'foo': 'bar'
+ }
+ );
+ assert.equal(part.name, 'field1');
+
+ var strings = ['hello', ' world'];
+ gently.expect(part, 'emit', 2, function(event, b) {
+ assert.equal(event, 'data');
+ assert.equal(b.toString(), strings.shift());
+ });
+
+ gently.expect(part, 'emit', function(event, b) {
+ assert.equal(event, 'end');
+ });
+ });
+
+ PARSER.onPartBegin();
+ PARSER.onHeaderField(new Buffer('content-disposition'), 0, 10);
+ PARSER.onHeaderField(new Buffer('content-disposition'), 10, 19);
+ PARSER.onHeaderValue(new Buffer('form-data; name="field1"'), 0, 14);
+ PARSER.onHeaderValue(new Buffer('form-data; name="field1"'), 14, 24);
+ PARSER.onHeaderEnd();
+ PARSER.onHeaderField(new Buffer('foo'), 0, 3);
+ PARSER.onHeaderValue(new Buffer('bar'), 0, 3);
+ PARSER.onHeaderEnd();
+ PARSER.onHeadersEnd();
+ PARSER.onPartData(new Buffer('hello world'), 0, 5);
+ PARSER.onPartData(new Buffer('hello world'), 5, 11);
+ PARSER.onPartEnd();
+ })();
+
+ (function testFileField() {
+ var PART;
+ gently.expect(StreamStub, 'new', function() {
+ PART = this;
+ });
+
+ gently.expect(form, 'onPart', function(part) {
+ assert.deepEqual
+ ( part.headers
+ , { 'content-disposition': 'form-data; name="field2"; filename="C:\\Documents and Settings\\IE\\Must\\Die\\Sun"et.jpg"'
+ , 'content-type': 'text/plain'
+ }
+ );
+ assert.equal(part.name, 'field2');
+ assert.equal(part.filename, 'Sun"et.jpg');
+ assert.equal(part.mime, 'text/plain');
+
+ gently.expect(part, 'emit', function(event, b) {
+ assert.equal(event, 'data');
+ assert.equal(b.toString(), '... contents of file1.txt ...');
+ });
+
+ gently.expect(part, 'emit', function(event, b) {
+ assert.equal(event, 'end');
+ });
+ });
+
+ PARSER.onPartBegin();
+ PARSER.onHeaderField(new Buffer('content-disposition'), 0, 19);
+ PARSER.onHeaderValue(new Buffer('form-data; name="field2"; filename="C:\\Documents and Settings\\IE\\Must\\Die\\Sun"et.jpg"'), 0, 85);
+ PARSER.onHeaderEnd();
+ PARSER.onHeaderField(new Buffer('Content-Type'), 0, 12);
+ PARSER.onHeaderValue(new Buffer('text/plain'), 0, 10);
+ PARSER.onHeaderEnd();
+ PARSER.onHeadersEnd();
+ PARSER.onPartData(new Buffer('... contents of file1.txt ...'), 0, 29);
+ PARSER.onPartEnd();
+ })();
+
+ (function testEnd() {
+ gently.expect(form, '_maybeEnd');
+ PARSER.onEnd();
+ assert.ok(form.ended);
+ })();
+});
+
+test(function _fileName() {
+ // TODO
+ return;
+});
+
+test(function _initUrlencoded() {
+ var PARSER;
+
+ gently.expect(QuerystringParserStub, 'new', function() {
+ PARSER = this;
+ });
+
+ form._initUrlencoded();
+ assert.equal(form.type, 'urlencoded');
+ assert.strictEqual(form._parser, PARSER);
+
+ (function testOnField() {
+ var KEY = 'KEY', VAL = 'VAL';
+ gently.expect(form, 'emit', function(field, key, val) {
+ assert.equal(field, 'field');
+ assert.equal(key, KEY);
+ assert.equal(val, VAL);
+ });
+
+ PARSER.onField(KEY, VAL);
+ })();
+
+ (function testOnEnd() {
+ gently.expect(form, '_maybeEnd');
+
+ PARSER.onEnd();
+ assert.equal(form.ended, true);
+ })();
+});
+
+test(function _error() {
+ var ERR = new Error('bla');
+
+ gently.expect(form, 'pause');
+ gently.expect(form, 'emit', function(event, err) {
+ assert.equal(event, 'error');
+ assert.strictEqual(err, ERR);
+ });
+
+ form._error(ERR);
+ assert.strictEqual(form.error, ERR);
+
+ // make sure _error only does its thing once
+ form._error(ERR);
+});
+
+test(function onPart() {
+ var PART = {};
+ gently.expect(form, 'handlePart', function(part) {
+ assert.strictEqual(part, PART);
+ });
+
+ form.onPart(PART);
+});
+
+test(function handlePart() {
+ (function testUtf8Field() {
+ var PART = new events.EventEmitter();
+ PART.name = 'my_field';
+
+ gently.expect(form, 'emit', function(event, field, value) {
+ assert.equal(event, 'field');
+ assert.equal(field, 'my_field');
+ assert.equal(value, 'hello world: €');
+ });
+
+ form.handlePart(PART);
+ PART.emit('data', new Buffer('hello'));
+ PART.emit('data', new Buffer(' world: '));
+ PART.emit('data', new Buffer([0xE2]));
+ PART.emit('data', new Buffer([0x82, 0xAC]));
+ PART.emit('end');
+ })();
+
+ (function testBinaryField() {
+ var PART = new events.EventEmitter();
+ PART.name = 'my_field2';
+
+ gently.expect(form, 'emit', function(event, field, value) {
+ assert.equal(event, 'field');
+ assert.equal(field, 'my_field2');
+ assert.equal(value, 'hello world: '+new Buffer([0xE2, 0x82, 0xAC]).toString('binary'));
+ });
+
+ form.encoding = 'binary';
+ form.handlePart(PART);
+ PART.emit('data', new Buffer('hello'));
+ PART.emit('data', new Buffer(' world: '));
+ PART.emit('data', new Buffer([0xE2]));
+ PART.emit('data', new Buffer([0x82, 0xAC]));
+ PART.emit('end');
+ })();
+
+ (function testFieldSize() {
+ form.maxFieldsSize = 8;
+ var PART = new events.EventEmitter();
+ PART.name = 'my_field';
+
+ gently.expect(form, '_error', function(err) {
+ assert.equal(err.message, 'maxFieldsSize exceeded, received 9 bytes of field data');
+ });
+
+ form.handlePart(PART);
+ form._fieldsSize = 1;
+ PART.emit('data', new Buffer(7));
+ PART.emit('data', new Buffer(1));
+ })();
+
+ (function testFilePart() {
+ var PART = new events.EventEmitter(),
+ FILE = new events.EventEmitter(),
+ PATH = '/foo/bar';
+
+ PART.name = 'my_file';
+ PART.filename = 'sweet.txt';
+ PART.mime = 'sweet.txt';
+
+ gently.expect(form, '_uploadPath', function(filename) {
+ assert.equal(filename, PART.filename);
+ return PATH;
+ });
+
+ gently.expect(FileStub, 'new', function(properties) {
+ assert.equal(properties.path, PATH);
+ assert.equal(properties.name, PART.filename);
+ assert.equal(properties.type, PART.mime);
+ FILE = this;
+
+ gently.expect(form, 'emit', function (event, field, file) {
+ assert.equal(event, 'fileBegin');
+ assert.strictEqual(field, PART.name);
+ assert.strictEqual(file, FILE);
+ });
+
+ gently.expect(FILE, 'open');
+ });
+
+ form.handlePart(PART);
+ assert.equal(form._flushing, 1);
+
+ var BUFFER;
+ gently.expect(form, 'pause');
+ gently.expect(FILE, 'write', function(buffer, cb) {
+ assert.strictEqual(buffer, BUFFER);
+ gently.expect(form, 'resume');
+ // @todo handle cb(new Err)
+ cb();
+ });
+
+ PART.emit('data', BUFFER = new Buffer('test'));
+
+ gently.expect(FILE, 'end', function(cb) {
+ gently.expect(form, 'emit', function(event, field, file) {
+ assert.equal(event, 'file');
+ assert.strictEqual(file, FILE);
+ });
+
+ gently.expect(form, '_maybeEnd');
+
+ cb();
+ assert.equal(form._flushing, 0);
+ });
+
+ PART.emit('end');
+ })();
+});
+
+test(function _uploadPath() {
+ (function testUniqueId() {
+ var UUID_A, UUID_B;
+ gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, uuid) {
+ assert.equal(uploadDir, form.uploadDir);
+ UUID_A = uuid;
+ });
+ form._uploadPath();
+
+ gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, uuid) {
+ UUID_B = uuid;
+ });
+ form._uploadPath();
+
+ assert.notEqual(UUID_A, UUID_B);
+ })();
+
+ (function testFileExtension() {
+ form.keepExtensions = true;
+ var FILENAME = 'foo.jpg',
+ EXT = '.bar';
+
+ gently.expect(GENTLY.hijacked.path, 'extname', function(filename) {
+ assert.equal(filename, FILENAME);
+ gently.restore(path, 'extname');
+
+ return EXT;
+ });
+
+ gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, name) {
+ assert.equal(path.extname(name), EXT);
+ });
+ form._uploadPath(FILENAME);
+ })();
+});
+
+test(function _maybeEnd() {
+ gently.expect(form, 'emit', 0);
+ form._maybeEnd();
+
+ form.ended = true;
+ form._flushing = 1;
+ form._maybeEnd();
+
+ gently.expect(form, 'emit', function(event) {
+ assert.equal(event, 'end');
+ });
+
+ form.ended = true;
+ form._flushing = 0;
+ form._maybeEnd();
+});
diff --git a/node_modules/formidable/test/legacy/simple/test-multipart-parser.js b/node_modules/formidable/test/legacy/simple/test-multipart-parser.js
new file mode 100644
index 0000000..d8dc968
--- /dev/null
+++ b/node_modules/formidable/test/legacy/simple/test-multipart-parser.js
@@ -0,0 +1,50 @@
+var common = require('../common');
+var multipartParser = require(common.lib + '/multipart_parser'),
+ MultipartParser = multipartParser.MultipartParser,
+ events = require('events'),
+ Buffer = require('buffer').Buffer,
+ parser;
+
+function test(test) {
+ parser = new MultipartParser();
+ test();
+}
+
+test(function constructor() {
+ assert.equal(parser.boundary, null);
+ assert.equal(parser.state, 0);
+ assert.equal(parser.flags, 0);
+ assert.equal(parser.boundaryChars, null);
+ assert.equal(parser.index, null);
+ assert.equal(parser.lookbehind, null);
+ assert.equal(parser.constructor.name, 'MultipartParser');
+});
+
+test(function initWithBoundary() {
+ var boundary = 'abc';
+ parser.initWithBoundary(boundary);
+ assert.deepEqual(Array.prototype.slice.call(parser.boundary), [13, 10, 45, 45, 97, 98, 99]);
+ assert.equal(parser.state, multipartParser.START);
+
+ assert.deepEqual(parser.boundaryChars, {10: true, 13: true, 45: true, 97: true, 98: true, 99: true});
+});
+
+test(function parserError() {
+ var boundary = 'abc',
+ buffer = new Buffer(5);
+
+ parser.initWithBoundary(boundary);
+ buffer.write('--ad', 'ascii', 0);
+ assert.equal(parser.write(buffer), 3);
+});
+
+test(function end() {
+ (function testError() {
+ assert.equal(parser.end().message, 'MultipartParser.end(): stream ended unexpectedly: ' + parser.explain());
+ })();
+
+ (function testRegular() {
+ parser.state = multipartParser.END;
+ assert.strictEqual(parser.end(), undefined);
+ })();
+});
diff --git a/node_modules/formidable/test/legacy/simple/test-querystring-parser.js b/node_modules/formidable/test/legacy/simple/test-querystring-parser.js
new file mode 100644
index 0000000..54d3e2d
--- /dev/null
+++ b/node_modules/formidable/test/legacy/simple/test-querystring-parser.js
@@ -0,0 +1,45 @@
+var common = require('../common');
+var QuerystringParser = require(common.lib + '/querystring_parser').QuerystringParser,
+ Buffer = require('buffer').Buffer,
+ gently,
+ parser;
+
+function test(test) {
+ gently = new Gently();
+ parser = new QuerystringParser();
+ test();
+ gently.verify(test.name);
+}
+
+test(function constructor() {
+ assert.equal(parser.buffer, '');
+ assert.equal(parser.constructor.name, 'QuerystringParser');
+});
+
+test(function write() {
+ var a = new Buffer('a=1');
+ assert.equal(parser.write(a), a.length);
+
+ var b = new Buffer('&b=2');
+ parser.write(b);
+ assert.equal(parser.buffer, a + b);
+});
+
+test(function end() {
+ var FIELDS = {a: ['b', {c: 'd'}], e: 'f'};
+
+ gently.expect(GENTLY.hijacked.querystring, 'parse', function(str) {
+ assert.equal(str, parser.buffer);
+ return FIELDS;
+ });
+
+ gently.expect(parser, 'onField', Object.keys(FIELDS).length, function(key, val) {
+ assert.deepEqual(FIELDS[key], val);
+ });
+
+ gently.expect(parser, 'onEnd');
+
+ parser.buffer = 'my buffer';
+ parser.end();
+ assert.equal(parser.buffer, '');
+});
diff --git a/node_modules/formidable/test/legacy/system/test-multi-video-upload.js b/node_modules/formidable/test/legacy/system/test-multi-video-upload.js
new file mode 100644
index 0000000..479e46d
--- /dev/null
+++ b/node_modules/formidable/test/legacy/system/test-multi-video-upload.js
@@ -0,0 +1,75 @@
+var common = require('../common');
+var BOUNDARY = '---------------------------10102754414578508781458777923',
+ FIXTURE = TEST_FIXTURES+'/multi_video.upload',
+ fs = require('fs'),
+ util = require(common.lib + '/util'),
+ http = require('http'),
+ formidable = require(common.lib + '/index'),
+ server = http.createServer();
+
+server.on('request', function(req, res) {
+ var form = new formidable.IncomingForm(),
+ uploads = {};
+
+ form.uploadDir = TEST_TMP;
+ form.hash = 'sha1';
+ form.parse(req);
+
+ form
+ .on('fileBegin', function(field, file) {
+ assert.equal(field, 'upload');
+
+ var tracker = {file: file, progress: [], ended: false};
+ uploads[file.filename] = tracker;
+ file
+ .on('progress', function(bytesReceived) {
+ tracker.progress.push(bytesReceived);
+ assert.equal(bytesReceived, file.length);
+ })
+ .on('end', function() {
+ tracker.ended = true;
+ });
+ })
+ .on('field', function(field, value) {
+ assert.equal(field, 'title');
+ assert.equal(value, '');
+ })
+ .on('file', function(field, file) {
+ assert.equal(field, 'upload');
+ assert.strictEqual(uploads[file.filename].file, file);
+ })
+ .on('end', function() {
+ assert.ok(uploads['shortest_video.flv']);
+ assert.ok(uploads['shortest_video.flv'].ended);
+ assert.ok(uploads['shortest_video.flv'].progress.length > 3);
+ assert.equal(uploads['shortest_video.flv'].file.hash, 'd6a17616c7143d1b1438ceeef6836d1a09186b3a');
+ assert.equal(uploads['shortest_video.flv'].progress.slice(-1), uploads['shortest_video.flv'].file.length);
+ assert.ok(uploads['shortest_video.mp4']);
+ assert.ok(uploads['shortest_video.mp4'].ended);
+ assert.ok(uploads['shortest_video.mp4'].progress.length > 3);
+ assert.equal(uploads['shortest_video.mp4'].file.hash, '937dfd4db263f4887ceae19341dcc8d63bcd557f');
+
+ server.close();
+ res.writeHead(200);
+ res.end('good');
+ });
+});
+
+server.listen(TEST_PORT, function() {
+ var client = http.createClient(TEST_PORT),
+ stat = fs.statSync(FIXTURE),
+ headers = {
+ 'content-type': 'multipart/form-data; boundary='+BOUNDARY,
+ 'content-length': stat.size,
+ }
+ request = client.request('POST', '/', headers),
+ fixture = new fs.ReadStream(FIXTURE);
+
+ fixture
+ .on('data', function(b) {
+ request.write(b);
+ })
+ .on('end', function() {
+ request.end();
+ });
+});
diff --git a/node_modules/formidable/test/run.js b/node_modules/formidable/test/run.js
new file mode 100644
index 0000000..50b2361
--- /dev/null
+++ b/node_modules/formidable/test/run.js
@@ -0,0 +1,2 @@
+#!/usr/bin/env node
+require('urun')(__dirname)
diff --git a/node_modules/formidable/test/unit/test-incoming-form.js b/node_modules/formidable/test/unit/test-incoming-form.js
new file mode 100644
index 0000000..fe2ac1c
--- /dev/null
+++ b/node_modules/formidable/test/unit/test-incoming-form.js
@@ -0,0 +1,63 @@
+var common = require('../common');
+var test = require('utest');
+var assert = common.assert;
+var IncomingForm = common.require('incoming_form').IncomingForm;
+var path = require('path');
+
+var form;
+test('IncomingForm', {
+ before: function() {
+ form = new IncomingForm();
+ },
+
+ '#_fileName with regular characters': function() {
+ var filename = 'foo.txt';
+ assert.equal(form._fileName(makeHeader(filename)), 'foo.txt');
+ },
+
+ '#_fileName with unescaped quote': function() {
+ var filename = 'my".txt';
+ assert.equal(form._fileName(makeHeader(filename)), 'my".txt');
+ },
+
+ '#_fileName with escaped quote': function() {
+ var filename = 'my%22.txt';
+ assert.equal(form._fileName(makeHeader(filename)), 'my".txt');
+ },
+
+ '#_fileName with bad quote and additional sub-header': function() {
+ var filename = 'my".txt';
+ var header = makeHeader(filename) + '; foo="bar"';
+ assert.equal(form._fileName(header), filename);
+ },
+
+ '#_fileName with semicolon': function() {
+ var filename = 'my;.txt';
+ assert.equal(form._fileName(makeHeader(filename)), 'my;.txt');
+ },
+
+ '#_fileName with utf8 character': function() {
+ var filename = 'my☃.txt';
+ assert.equal(form._fileName(makeHeader(filename)), 'my☃.txt');
+ },
+
+ '#_uploadPath strips harmful characters from extension when keepExtensions': function() {
+ form.keepExtensions = true;
+
+ var ext = path.extname(form._uploadPath('fine.jpg?foo=bar'));
+ assert.equal(ext, '.jpg');
+
+ var ext = path.extname(form._uploadPath('fine?foo=bar'));
+ assert.equal(ext, '');
+
+ var ext = path.extname(form._uploadPath('super.cr2+dsad'));
+ assert.equal(ext, '.cr2');
+
+ var ext = path.extname(form._uploadPath('super.bar'));
+ assert.equal(ext, '.bar');
+ },
+});
+
+function makeHeader(filename) {
+ return 'Content-Disposition: form-data; name="upload"; filename="' + filename + '"';
+}
diff --git a/node_modules/formidable/tool/record.js b/node_modules/formidable/tool/record.js
new file mode 100644
index 0000000..9f1cef8
--- /dev/null
+++ b/node_modules/formidable/tool/record.js
@@ -0,0 +1,47 @@
+var http = require('http');
+var fs = require('fs');
+var connections = 0;
+
+var server = http.createServer(function(req, res) {
+ var socket = req.socket;
+ console.log('Request: %s %s -> %s', req.method, req.url, socket.filename);
+
+ req.on('end', function() {
+ if (req.url !== '/') {
+ res.end(JSON.stringify({
+ method: req.method,
+ url: req.url,
+ filename: socket.filename,
+ }));
+ return;
+ }
+
+ res.writeHead(200, {'content-type': 'text/html'});
+ res.end(
+ ''
+ );
+ });
+});
+
+server.on('connection', function(socket) {
+ connections++;
+
+ socket.id = connections;
+ socket.filename = 'connection-' + socket.id + '.http';
+ socket.file = fs.createWriteStream(socket.filename);
+ socket.pipe(socket.file);
+
+ console.log('--> %s', socket.filename);
+ socket.on('close', function() {
+ console.log('<-- %s', socket.filename);
+ });
+});
+
+var port = process.env.PORT || 8080;
+server.listen(port, function() {
+ console.log('Recording connections on port %s', port);
+});
diff --git a/node_modules/mongodb/.travis.yml b/node_modules/mongodb/.travis.yml
new file mode 100644
index 0000000..94740d0
--- /dev/null
+++ b/node_modules/mongodb/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
+ - 0.9 # development version of 0.8, may be unstable
\ No newline at end of file
diff --git a/node_modules/mongodb/CONTRIBUTING.md b/node_modules/mongodb/CONTRIBUTING.md
new file mode 100644
index 0000000..2a1c52e
--- /dev/null
+++ b/node_modules/mongodb/CONTRIBUTING.md
@@ -0,0 +1,23 @@
+## Contributing to the driver
+
+### Bugfixes
+
+- Before starting to write code, look for existing [tickets](https://github.com/mongodb/node-mongodb-native/issues) or [create one](https://github.com/mongodb/node-mongodb-native/issues/new) for your specific issue. That way you avoid working on something that might not be of interest or that has been addressed already in a different branch.
+- Fork the [repo](https://github.com/mongodb/node-mongodb-native) _or_ for small documentation changes, navigate to the source on github and click the [Edit](https://github.com/blog/844-forking-with-the-edit-button) button.
+- Follow the general coding style of the rest of the project:
+ - 2 space tabs
+ - no trailing whitespace
+ - comma last
+ - inline documentation for new methods, class members, etc
+ - 0 space between conditionals/functions, and their parenthesis and curly braces
+ - `if(..) {`
+ - `for(..) {`
+ - `while(..) {`
+ - `function(err) {`
+- Write tests and make sure they pass (execute `make test` from the cmd line to run the test suite).
+
+### Documentation
+
+To contribute to the [API documentation](http://mongodb.github.com/node-mongodb-native/) just make your changes to the inline documentation of the appropriate [source code](https://github.com/mongodb/node-mongodb-native/tree/master/docs) in the master branch and submit a [pull request](https://help.github.com/articles/using-pull-requests/). You might also use the github [Edit](https://github.com/blog/844-forking-with-the-edit-button) button.
+
+If you'd like to preview your documentation changes, first commit your changes to your local master branch, then execute `make generate_docs`. Make sure you have the python documentation framework sphinx installed `easy_install sphinx`. The docs are generated under `docs/build'. If all looks good, submit a [pull request](https://help.github.com/articles/using-pull-requests/) to the master branch with your changes.
\ No newline at end of file
diff --git a/node_modules/mongodb/Makefile b/node_modules/mongodb/Makefile
new file mode 100644
index 0000000..ac55626
--- /dev/null
+++ b/node_modules/mongodb/Makefile
@@ -0,0 +1,64 @@
+NODE = node
+NPM = npm
+NODEUNIT = node_modules/nodeunit/bin/nodeunit
+DOX = node_modules/dox/bin/dox
+name = all
+
+total: build_native
+
+test-coverage:
+ rm -rf lib-cov/
+ jscoverage lib/ lib-cov/
+ @TEST_COVERAGE=true nodeunit test/ test/gridstore test/connection
+
+build_native:
+
+test: build_native
+ @echo "\n == Run All tests minus replicaset tests=="
+ $(NODE) dev/tools/test_all.js --noreplicaset --boot
+
+test_pure: build_native
+ @echo "\n == Run All tests minus replicaset tests=="
+ $(NODE) dev/tools/test_all.js --noreplicaset --boot --nonative
+
+test_junit: build_native
+ @echo "\n == Run All tests minus replicaset tests=="
+ $(NODE) dev/tools/test_all.js --junit --noreplicaset --nokill
+
+jenkins: build_native
+ @echo "\n == Run All tests minus replicaset tests=="
+ $(NODE) dev/tools/test_all.js --junit --noreplicaset --nokill
+
+test_nodeunit_pure:
+ @echo "\n == Execute Test Suite using Pure JS BSON Parser == "
+ @$(NODEUNIT) test/ test/gridstore test/bson
+
+test_nodeunit_replicaset_pure:
+ @echo "\n == Execute Test Suite using Pure JS BSON Parser == "
+ @$(NODEUNIT) test/replicaset
+
+test_nodeunit_native:
+ @echo "\n == Execute Test Suite using Native BSON Parser == "
+ @TEST_NATIVE=TRUE $(NODEUNIT) test/ test/gridstore test/bson
+
+test_nodeunit_replicaset_native:
+ @echo "\n == Execute Test Suite using Native BSON Parser == "
+ @TEST_NATIVE=TRUE $(NODEUNIT) test/replicaset
+
+test_all: build_native
+ @echo "\n == Run All tests =="
+ $(NODE) dev/tools/test_all.js --boot
+
+test_all_junit: build_native
+ @echo "\n == Run All tests =="
+ $(NODE) dev/tools/test_all.js --junit --boot
+
+clean:
+ rm ./external-libs/bson/bson.node
+ rm -r ./external-libs/bson/build
+
+generate_docs:
+ $(NODE) dev/tools/build-docs.js
+ make --directory=./docs/sphinx-docs --file=Makefile html
+
+.PHONY: total
diff --git a/node_modules/mongodb/Readme.md b/node_modules/mongodb/Readme.md
new file mode 100644
index 0000000..b81e719
--- /dev/null
+++ b/node_modules/mongodb/Readme.md
@@ -0,0 +1,442 @@
+Up to date documentation
+========================
+
+[Documentation](http://mongodb.github.com/node-mongodb-native/)
+
+Install
+=======
+
+To install the most recent release from npm, run:
+
+ npm install mongodb
+
+That may give you a warning telling you that bugs['web'] should be bugs['url'], it would be safe to ignore it (this has been fixed in the development version)
+
+To install the latest from the repository, run::
+
+ npm install path/to/node-mongodb-native
+
+Community
+=========
+Check out the google group [node-mongodb-native](http://groups.google.com/group/node-mongodb-native) for questions/answers from users of the driver.
+
+Try it live
+============
+
+
+Introduction
+============
+
+This is a node.js driver for MongoDB. It's a port (or close to a port) of the library for ruby at http://github.com/mongodb/mongo-ruby-driver/.
+
+A simple example of inserting a document.
+
+```javascript
+ var client = new Db('test', new Server("127.0.0.1", 27017, {}), {w: 1}),
+ test = function (err, collection) {
+ collection.insert({a:2}, function(err, docs) {
+
+ collection.count(function(err, count) {
+ test.assertEquals(1, count);
+ });
+
+ // Locate all the entries using find
+ collection.find().toArray(function(err, results) {
+ test.assertEquals(1, results.length);
+ test.assertTrue(results[0].a === 2);
+
+ // Let's close the db
+ client.close();
+ });
+ });
+ };
+
+ client.open(function(err, p_client) {
+ client.collection('test_insert', test);
+ });
+```
+
+Data types
+==========
+
+To store and retrieve the non-JSON MongoDb primitives ([ObjectID](http://www.mongodb.org/display/DOCS/Object+IDs), Long, Binary, [Timestamp](http://www.mongodb.org/display/DOCS/Timestamp+data+type), [DBRef](http://www.mongodb.org/display/DOCS/Database+References#DatabaseReferences-DBRef), Code).
+
+In particular, every document has a unique `_id` which can be almost any type, and by default a 12-byte ObjectID is created. ObjectIDs can be represented as 24-digit hexadecimal strings, but you must convert the string back into an ObjectID before you can use it in the database. For example:
+
+```javascript
+ // Get the objectID type
+ var ObjectID = require('mongodb').ObjectID;
+
+ var idString = '4e4e1638c85e808431000003';
+ collection.findOne({_id: new ObjectID(idString)}, console.log) // ok
+ collection.findOne({_id: idString}, console.log) // wrong! callback gets undefined
+```
+
+Here are the constructors the non-Javascript BSON primitive types:
+
+```javascript
+ // Fetch the library
+ var mongo = require('mongodb');
+ // Create new instances of BSON types
+ new mongo.Long(numberString)
+ new mongo.ObjectID(hexString)
+ new mongo.Timestamp() // the actual unique number is generated on insert.
+ new mongo.DBRef(collectionName, id, dbName)
+ new mongo.Binary(buffer) // takes a string or Buffer
+ new mongo.Code(code, [context])
+ new mongo.Symbol(string)
+ new mongo.MinKey()
+ new mongo.MaxKey()
+ new mongo.Double(number) // Force double storage
+```
+
+The C/C++ bson parser/serializer
+--------------------------------
+
+If you are running a version of this library has the C/C++ parser compiled, to enable the driver to use the C/C++ bson parser pass it the option native_parser:true like below
+
+```javascript
+ // using native_parser:
+ var client = new Db('integration_tests_20',
+ new Server("127.0.0.1", 27017),
+ {native_parser:true});
+```
+
+The C++ parser uses the js objects both for serialization and deserialization.
+
+GitHub information
+==================
+
+The source code is available at http://github.com/mongodb/node-mongodb-native.
+You can either clone the repository or download a tarball of the latest release.
+
+Once you have the source you can test the driver by running
+
+ $ make test
+
+in the main directory. You will need to have a mongo instance running on localhost for the integration tests to pass.
+
+Examples
+========
+
+For examples look in the examples/ directory. You can execute the examples using node.
+
+ $ cd examples
+ $ node queries.js
+
+GridStore
+=========
+
+The GridStore class allows for storage of binary files in mongoDB using the mongoDB defined files and chunks collection definition.
+
+For more information have a look at [Gridstore](https://github.com/mongodb/node-mongodb-native/blob/master/docs/gridfs.md)
+
+Replicasets
+===========
+For more information about how to connect to a replicaset have a look at [Replicasets](https://github.com/mongodb/node-mongodb-native/blob/master/docs/replicaset.md)
+
+Primary Key Factories
+---------------------
+
+Defining your own primary key factory allows you to generate your own series of id's
+(this could f.ex be to use something like ISBN numbers). The generated the id needs to be a 12 byte long "string".
+
+Simple example below
+
+```javascript
+ // Custom factory (need to provide a 12 byte array);
+ CustomPKFactory = function() {}
+ CustomPKFactory.prototype = new Object();
+ CustomPKFactory.createPk = function() {
+ return new ObjectID("aaaaaaaaaaaa");
+ }
+
+ var p_client = new Db('integration_tests_20', new Server("127.0.0.1", 27017, {}), {'pk':CustomPKFactory});
+ p_client.open(function(err, p_client) {
+ p_client.dropDatabase(function(err, done) {
+ p_client.createCollection('test_custom_key', function(err, collection) {
+ collection.insert({'a':1}, function(err, docs) {
+ collection.find({'_id':new ObjectID("aaaaaaaaaaaa")}, function(err, cursor) {
+ cursor.toArray(function(err, items) {
+ test.assertEquals(1, items.length);
+
+ // Let's close the db
+ p_client.close();
+ });
+ });
+ });
+ });
+ });
+ });
+```
+
+Strict mode
+-----------
+
+Each database has an optional strict mode. If it is set then asking for a collection
+that does not exist will return an Error object in the callback. Similarly if you
+attempt to create a collection that already exists. Strict is provided for convenience.
+
+```javascript
+ var error_client = new Db('integration_tests_', new Server("127.0.0.1", 27017, {auto_reconnect: false}), {strict:true});
+ test.assertEquals(true, error_client.strict);
+
+ error_client.open(function(err, error_client) {
+ error_client.collection('does-not-exist', function(err, collection) {
+ test.assertTrue(err instanceof Error);
+ test.assertEquals("Collection does-not-exist does not exist. Currently in strict mode.", err.message);
+ });
+
+ error_client.createCollection('test_strict_access_collection', function(err, collection) {
+ error_client.collection('test_strict_access_collection', function(err, collection) {
+ test.assertTrue(collection instanceof Collection);
+ // Let's close the db
+ error_client.close();
+ });
+ });
+ });
+```
+
+Documentation
+=============
+
+If this document doesn't answer your questions, see the source of
+[Collection](https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/collection.js)
+or [Cursor](https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/cursor.js),
+or the documentation at MongoDB for query and update formats.
+
+Find
+----
+
+The find method is actually a factory method to create
+Cursor objects. A Cursor lazily uses the connection the first time
+you call `nextObject`, `each`, or `toArray`.
+
+The basic operation on a cursor is the `nextObject` method
+that fetches the next matching document from the database. The convenience
+methods `each` and `toArray` call `nextObject` until the cursor is exhausted.
+
+Signatures:
+
+```javascript
+ var cursor = collection.find(query, [fields], options);
+ cursor.sort(fields).limit(n).skip(m).
+
+ cursor.nextObject(function(err, doc) {});
+ cursor.each(function(err, doc) {});
+ cursor.toArray(function(err, docs) {});
+
+ cursor.rewind() // reset the cursor to its initial state.
+```
+
+Useful chainable methods of cursor. These can optionally be options of `find` instead of method calls:
+
+* `.limit(n).skip(m)` to control paging.
+* `.sort(fields)` Order by the given fields. There are several equivalent syntaxes:
+ * `.sort({field1: -1, field2: 1})` descending by field1, then ascending by field2.
+ * `.sort([['field1', 'desc'], ['field2', 'asc']])` same as above
+ * `.sort([['field1', 'desc'], 'field2'])` same as above
+ * `.sort('field1')` ascending by field1
+
+Other options of `find`:
+
+* `fields` the fields to fetch (to avoid transferring the entire document)
+* `tailable` if true, makes the cursor [tailable](http://www.mongodb.org/display/DOCS/Tailable+Cursors).
+* `batchSize` The number of the subset of results to request the database
+to return for every request. This should initially be greater than 1 otherwise
+the database will automatically close the cursor. The batch size can be set to 1
+with `batchSize(n, function(err){})` after performing the initial query to the database.
+* `hint` See [Optimization: hint](http://www.mongodb.org/display/DOCS/Optimization#Optimization-Hint).
+* `explain` turns this into an explain query. You can also call
+`explain()` on any cursor to fetch the explanation.
+* `snapshot` prevents documents that are updated while the query is active
+from being returned multiple times. See more
+[details about query snapshots](http://www.mongodb.org/display/DOCS/How+to+do+Snapshotted+Queries+in+the+Mongo+Database).
+* `timeout` if false, asks MongoDb not to time out this cursor after an
+inactivity period.
+
+
+For information on how to create queries, see the
+[MongoDB section on querying](http://www.mongodb.org/display/DOCS/Querying).
+
+```javascript
+ var mongodb = require('mongodb');
+ var server = new mongodb.Server("127.0.0.1", 27017, {});
+ new mongodb.Db('test', server, {}).open(function (error, client) {
+ if (error) throw error;
+ var collection = new mongodb.Collection(client, 'test_collection');
+ collection.find({}, {limit:10}).toArray(function(err, docs) {
+ console.dir(docs);
+ });
+ });
+```
+
+Insert
+------
+
+Signature:
+
+```javascript
+ collection.insert(docs, options, [callback]);
+```
+
+where `docs` can be a single document or an array of documents.
+
+Useful options:
+
+* `safe:true` Should always set if you have a callback.
+
+See also: [MongoDB docs for insert](http://www.mongodb.org/display/DOCS/Inserting).
+
+```javascript
+ var mongodb = require('mongodb');
+ var server = new mongodb.Server("127.0.0.1", 27017, {});
+ new mongodb.Db('test', server, {w: 1}).open(function (error, client) {
+ if (error) throw error;
+ var collection = new mongodb.Collection(client, 'test_collection');
+ collection.insert({hello: 'world'}, {safe:true},
+ function(err, objects) {
+ if (err) console.warn(err.message);
+ if (err && err.message.indexOf('E11000 ') !== -1) {
+ // this _id was already inserted in the database
+ }
+ });
+ });
+```
+
+Note that there's no reason to pass a callback to the insert or update commands
+unless you use the `safe:true` option. If you don't specify `safe:true`, then
+your callback will be called immediately.
+
+Update; update and insert (upsert)
+----------------------------------
+
+The update operation will update the first document that matches your query
+(or all documents that match if you use `multi:true`).
+If `safe:true`, `upsert` is not set, and no documents match, your callback will return 0 documents updated.
+
+See the [MongoDB docs](http://www.mongodb.org/display/DOCS/Updating) for
+the modifier (`$inc`, `$set`, `$push`, etc.) formats.
+
+Signature:
+
+```javascript
+ collection.update(criteria, objNew, options, [callback]);
+```
+
+Useful options:
+
+* `safe:true` Should always set if you have a callback.
+* `multi:true` If set, all matching documents are updated, not just the first.
+* `upsert:true` Atomically inserts the document if no documents matched.
+
+Example for `update`:
+
+```javascript
+ var mongodb = require('mongodb');
+ var server = new mongodb.Server("127.0.0.1", 27017, {});
+ new mongodb.Db('test', server, {w: 1}).open(function (error, client) {
+ if (error) throw error;
+ var collection = new mongodb.Collection(client, 'test_collection');
+ collection.update({hi: 'here'}, {$set: {hi: 'there'}}, {safe:true},
+ function(err) {
+ if (err) console.warn(err.message);
+ else console.log('successfully updated');
+ });
+ });
+```
+
+Find and modify
+---------------
+
+`findAndModify` is like `update`, but it also gives the updated document to
+your callback. But there are a few key differences between findAndModify and
+update:
+
+ 1. The signatures differ.
+ 2. You can only findAndModify a single item, not multiple items.
+
+Signature:
+
+```javascript
+ collection.findAndModify(query, sort, update, options, callback)
+```
+
+The sort parameter is used to specify which object to operate on, if more than
+one document matches. It takes the same format as the cursor sort (see
+Connection.find above).
+
+See the
+[MongoDB docs for findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command)
+for more details.
+
+Useful options:
+
+* `remove:true` set to a true to remove the object before returning
+* `new:true` set to true if you want to return the modified object rather than the original. Ignored for remove.
+* `upsert:true` Atomically inserts the document if no documents matched.
+
+Example for `findAndModify`:
+
+```javascript
+ var mongodb = require('mongodb');
+ var server = new mongodb.Server("127.0.0.1", 27017, {});
+ new mongodb.Db('test', server, {w: 1}).open(function (error, client) {
+ if (error) throw error;
+ var collection = new mongodb.Collection(client, 'test_collection');
+ collection.findAndModify({hello: 'world'}, [['_id','asc']], {$set: {hi: 'there'}}, {},
+ function(err, object) {
+ if (err) console.warn(err.message);
+ else console.dir(object); // undefined if no matching object exists.
+ });
+ });
+```
+
+Save
+----
+
+The `save` method is a shorthand for upsert if the document contains an
+`_id`, or an insert if there is no `_id`.
+
+Sponsors
+========
+Just as Felix Geisendörfer I'm also working on the driver for my own startup and this driver is a big project that also benefits other companies who are using MongoDB.
+
+If your company could benefit from a even better-engineered node.js mongodb driver I would appreciate any type of sponsorship you may be able to provide. All the sponsors will get a lifetime display in this readme, priority support and help on problems and votes on the roadmap decisions for the driver. If you are interested contact me on [christkv AT g m a i l.com](mailto:christkv@gmail.com) for details.
+
+And I'm very thankful for code contributions. If you are interested in working on features please contact me so we can discuss API design and testing.
+
+Release Notes
+=============
+
+See HISTORY
+
+Credits
+=======
+
+1. [10gen](http://github.com/mongodb/mongo-ruby-driver/)
+2. [Google Closure Library](http://code.google.com/closure/library/)
+3. [Jonas Raoni Soares Silva](http://jsfromhell.com/classes/binary-parser)
+
+Contributors
+============
+
+Aaron Heckmann, Christoph Pojer, Pau Ramon Revilla, Nathan White, Emmerman, Seth LaForge, Boris Filipov, Stefan Schärmeli, Tedde Lundgren, renctan, Sergey Ukustov, Ciaran Jessup, kuno, srimonti, Erik Abele, Pratik Daga, Slobodan Utvic, Kristina Chodorow, Yonathan Randolph, Brian Noguchi, Sam Epstein, James Harrison Fisher, Vladimir Dronnikov, Ben Hockey, Henrik Johansson, Simon Weare, Alex Gorbatchev, Shimon Doodkin, Kyle Mueller, Eran Hammer-Lahav, Marcin Ciszak, François de Metz, Vinay Pulim, nstielau, Adam Wiggins, entrinzikyl, Jeremy Selier, Ian Millington, Public Keating, andrewjstone, Christopher Stott, Corey Jewett, brettkiefer, Rob Holland, Senmiao Liu, heroic, gitfy
+
+License
+=======
+
+ Copyright 2009 - 2012 Christian Amor Kvalheim.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
diff --git a/node_modules/mongodb/index.js b/node_modules/mongodb/index.js
new file mode 100644
index 0000000..4f59e9d
--- /dev/null
+++ b/node_modules/mongodb/index.js
@@ -0,0 +1 @@
+module.exports = require('./lib/mongodb');
diff --git a/node_modules/mongodb/install.js b/node_modules/mongodb/install.js
new file mode 100644
index 0000000..f9f2a57
--- /dev/null
+++ b/node_modules/mongodb/install.js
@@ -0,0 +1,40 @@
+var spawn = require('child_process').spawn,
+ exec = require('child_process').exec;
+
+process.stdout.write("================================================================================\n");
+process.stdout.write("= =\n");
+process.stdout.write("= To install with C++ bson parser do =\n");
+process.stdout.write("= =\n");
+process.stdout.write("================================================================================\n");
+
+// Check if we want to build the native code
+var build_native = process.env['npm_package_config_native'] != null ? process.env['npm_package_config_native'] : 'false';
+build_native = build_native == 'true' ? true : false;
+// If we are building the native bson extension ensure we use gmake if available
+if(build_native) {
+ // Check if we need to use gmake
+ exec('which gmake', function(err, stdout, stderr) {
+ // Set up spawn command
+ var make = null;
+ // No gmake build using make
+ if(err != null) {
+ make = spawn('make', ['total']);
+ } else {
+ make = spawn('gmake', ['total']);
+ }
+
+ // Execute spawn
+ make.stdout.on('data', function(data) {
+ process.stdout.write(data);
+ })
+
+ make.stderr.on('data', function(data) {
+ process.stdout.write(data);
+ })
+
+ make.on('exit', function(code) {
+ process.stdout.write('child process exited with code ' + code + "\n");
+ })
+ });
+}
+
diff --git a/node_modules/mongodb/lib/mongodb/admin.js b/node_modules/mongodb/lib/mongodb/admin.js
new file mode 100644
index 0000000..2183cf9
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/admin.js
@@ -0,0 +1,338 @@
+/*!
+ * Module dependencies.
+ */
+var Collection = require('./collection').Collection,
+ Cursor = require('./cursor').Cursor,
+ DbCommand = require('./commands/db_command').DbCommand;
+
+/**
+ * Allows the user to access the admin functionality of MongoDB
+ *
+ * @class Represents the Admin methods of MongoDB.
+ * @param {Object} db Current db instance we wish to perform Admin operations on.
+ * @return {Function} Constructor for Admin type.
+ */
+function Admin(db) {
+ if(!(this instanceof Admin)) return new Admin(db);
+ this.db = db;
+};
+
+/**
+ * Retrieve the server information for the current
+ * instance of the db client
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from buildInfo or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.buildInfo = function(callback) {
+ this.serverInfo(callback);
+}
+
+/**
+ * Retrieve the server information for the current
+ * instance of the db client
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from serverInfo or null if an error occured.
+ * @return {null} Returns no result
+ * @api private
+ */
+Admin.prototype.serverInfo = function(callback) {
+ this.db.executeDbAdminCommand({buildinfo:1}, function(err, doc) {
+ if(err != null) return callback(err, null);
+ return callback(null, doc.documents[0]);
+ });
+}
+
+/**
+ * Retrieve this db's server status.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from serverStatus or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Admin.prototype.serverStatus = function(callback) {
+ var self = this;
+
+ this.db.executeDbAdminCommand({serverStatus: 1}, function(err, doc) {
+ if(err == null && doc.documents[0].ok === 1) {
+ callback(null, doc.documents[0]);
+ } else {
+ if(err) return callback(err, false);
+ return callback(self.db.wrap(doc.documents[0]), false);
+ }
+ });
+};
+
+/**
+ * Retrieve the current profiling Level for MongoDB
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from profilingLevel or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.profilingLevel = function(callback) {
+ var self = this;
+
+ this.db.executeDbAdminCommand({profile:-1}, function(err, doc) {
+ doc = doc.documents[0];
+
+ if(err == null && doc.ok === 1) {
+ var was = doc.was;
+ if(was == 0) return callback(null, "off");
+ if(was == 1) return callback(null, "slow_only");
+ if(was == 2) return callback(null, "all");
+ return callback(new Error("Error: illegal profiling level value " + was), null);
+ } else {
+ err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
+ }
+ });
+};
+
+/**
+ * Ping the MongoDB server and retrieve results
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from ping or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.ping = function(options, callback) {
+ // Unpack calls
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+
+ this.db.executeDbAdminCommand({ping: 1}, callback);
+}
+
+/**
+ * Authenticate against MongoDB
+ *
+ * @param {String} username The user name for the authentication.
+ * @param {String} password The password for the authentication.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from authenticate or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.authenticate = function(username, password, callback) {
+ this.db.authenticate(username, password, {authdb: 'admin'}, function(err, doc) {
+ return callback(err, doc);
+ })
+}
+
+/**
+ * Logout current authenticated user
+ *
+ * @param {Object} [options] Optional parameters to the command.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from logout or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.logout = function(callback) {
+ this.db.logout({authdb: 'admin'}, function(err, doc) {
+ return callback(err, doc);
+ })
+}
+
+/**
+ * Add a user to the MongoDB server, if the user exists it will
+ * overwrite the current password
+ *
+ * Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {String} username The user name for the authentication.
+ * @param {String} password The password for the authentication.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from addUser or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.addUser = function(username, password, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 2);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ options.dbName = 'admin';
+ // Add user
+ this.db.addUser(username, password, options, function(err, doc) {
+ return callback(err, doc);
+ })
+}
+
+/**
+ * Remove a user from the MongoDB server
+ *
+ * Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {String} username The user name for the authentication.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from removeUser or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.removeUser = function(username, options, callback) {
+ var self = this;
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+ options.dbName = 'admin';
+
+ this.db.removeUser(username, options, function(err, doc) {
+ return callback(err, doc);
+ })
+}
+
+/**
+ * Set the current profiling level of MongoDB
+ *
+ * @param {String} level The new profiling level (off, slow_only, all)
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from setProfilingLevel or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.setProfilingLevel = function(level, callback) {
+ var self = this;
+ var command = {};
+ var profile = 0;
+
+ if(level == "off") {
+ profile = 0;
+ } else if(level == "slow_only") {
+ profile = 1;
+ } else if(level == "all") {
+ profile = 2;
+ } else {
+ return callback(new Error("Error: illegal profiling level value " + level));
+ }
+
+ // Set up the profile number
+ command['profile'] = profile;
+
+ this.db.executeDbAdminCommand(command, function(err, doc) {
+ doc = doc.documents[0];
+
+ if(err == null && doc.ok === 1)
+ return callback(null, level);
+ return err != null ? callback(err, null) : callback(new Error("Error with profile command"), null);
+ });
+};
+
+/**
+ * Retrive the current profiling information for MongoDB
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from profilingInfo or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.profilingInfo = function(callback) {
+ try {
+ new Cursor(this.db, new Collection(this.db, DbCommand.SYSTEM_PROFILE_COLLECTION), {}, {}, {dbName: 'admin'}).toArray(function(err, items) {
+ return callback(err, items);
+ });
+ } catch (err) {
+ return callback(err, null);
+ }
+};
+
+/**
+ * Execute a db command against the Admin database
+ *
+ * @param {Object} command A command object `{ping:1}`.
+ * @param {Object} [options] Optional parameters to the command.
+ * @param {Function} callback this will be called after executing this method. The command always return the whole result of the command as the second parameter.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.command = function(command, options, callback) {
+ var self = this;
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ // Execute a command
+ this.db.executeDbAdminCommand(command, options, function(err, doc) {
+ // Ensure change before event loop executes
+ return callback != null ? callback(err, doc) : null;
+ });
+}
+
+/**
+ * Validate an existing collection
+ *
+ * @param {String} collectionName The name of the collection to validate.
+ * @param {Object} [options] Optional parameters to the command.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from validateCollection or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.validateCollection = function(collectionName, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ var self = this;
+ var command = {validate: collectionName};
+ var keys = Object.keys(options);
+
+ // Decorate command with extra options
+ for(var i = 0; i < keys.length; i++) {
+ if(options.hasOwnProperty(keys[i])) {
+ command[keys[i]] = options[keys[i]];
+ }
+ }
+
+ this.db.executeDbCommand(command, function(err, doc) {
+ if(err != null) return callback(err, null);
+ doc = doc.documents[0];
+
+ if(doc.ok === 0)
+ return callback(new Error("Error with validate command"), null);
+ if(doc.result != null && doc.result.constructor != String)
+ return callback(new Error("Error with validation data"), null);
+ if(doc.result != null && doc.result.match(/exception|corrupt/) != null)
+ return callback(new Error("Error: invalid collection " + collectionName), null);
+ if(doc.valid != null && !doc.valid)
+ return callback(new Error("Error: invalid collection " + collectionName), null);
+
+ return callback(null, doc);
+ });
+};
+
+/**
+ * List the available databases
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from listDatabases or null if an error occured.
+ * @return {null} Returns no result
+ * @api public
+ */
+Admin.prototype.listDatabases = function(callback) {
+ // Execute the listAllDatabases command
+ this.db.executeDbAdminCommand({listDatabases:1}, {}, function(err, doc) {
+ if(err != null) return callback(err, null);
+ return callback(null, doc.documents[0]);
+ });
+}
+
+/**
+ * Get ReplicaSet status
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from replSetGetStatus or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Admin.prototype.replSetGetStatus = function(callback) {
+ var self = this;
+
+ this.db.executeDbAdminCommand({replSetGetStatus:1}, function(err, doc) {
+ if(err == null && doc.documents[0].ok === 1)
+ return callback(null, doc.documents[0]);
+ if(err) return callback(err, false);
+ return callback(self.db.wrap(doc.documents[0]), false);
+ });
+};
+
+/**
+ * @ignore
+ */
+exports.Admin = Admin;
diff --git a/node_modules/mongodb/lib/mongodb/collection.js b/node_modules/mongodb/lib/mongodb/collection.js
new file mode 100644
index 0000000..758793d
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/collection.js
@@ -0,0 +1,1699 @@
+/**
+ * Module dependencies.
+ * @ignore
+ */
+var InsertCommand = require('./commands/insert_command').InsertCommand
+ , QueryCommand = require('./commands/query_command').QueryCommand
+ , DeleteCommand = require('./commands/delete_command').DeleteCommand
+ , UpdateCommand = require('./commands/update_command').UpdateCommand
+ , DbCommand = require('./commands/db_command').DbCommand
+ , ObjectID = require('bson').ObjectID
+ , Code = require('bson').Code
+ , Cursor = require('./cursor').Cursor
+ , utils = require('./utils');
+
+/**
+ * Precompiled regexes
+ * @ignore
+**/
+const eErrorMessages = /No matching object found/;
+
+/**
+ * toString helper.
+ * @ignore
+ */
+var toString = Object.prototype.toString;
+
+/**
+ * Create a new Collection instance
+ *
+ * Options
+ * - **slaveOk** {Boolean, default:false}, Allow reads from secondaries.
+ * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document.
+ * - **raw** {Boolean, default:false}, perform all operations using raw bson objects.
+ * - **pkFactory** {Object}, object overriding the basic ObjectID primary key generation.
+ *
+ * @class Represents a Collection
+ * @param {Object} db db instance.
+ * @param {String} collectionName collection name.
+ * @param {Object} [pkFactory] alternative primary key factory.
+ * @param {Object} [options] additional options for the collection.
+ * @return {Object} a collection instance.
+ */
+function Collection (db, collectionName, pkFactory, options) {
+ if(!(this instanceof Collection)) return new Collection(db, collectionName, pkFactory, options);
+
+ checkCollectionName(collectionName);
+
+ this.db = db;
+ this.collectionName = collectionName;
+ this.internalHint = null;
+ this.opts = options != null && ('object' === typeof options) ? options : {};
+ this.slaveOk = options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk;
+ this.serializeFunctions = options == null || options.serializeFunctions == null ? db.serializeFunctions : options.serializeFunctions;
+ this.raw = options == null || options.raw == null ? db.raw : options.raw;
+
+ this.readPreference = options == null || options.readPreference == null ? db.serverConfig.readPreference : options.readPreference;
+ this.readPreference = this.readPreference == null ? 'primary' : this.readPreference;
+
+ this.pkFactory = pkFactory == null
+ ? ObjectID
+ : pkFactory;
+
+ var self = this;
+}
+
+/**
+ * Inserts a single document or a an array of documents into MongoDB.
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **continueOnError/keepGoing** {Boolean, default:false}, keep inserting documents even if one document has an error, *mongodb 1.9.1 >*.
+ * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document.
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {Array|Object} docs
+ * @param {Object} [options] optional options for insert command
+ * @param {Function} [callback] optional callback for the function, must be provided when using a writeconcern
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.insert = function insert (docs, options, callback) {
+ if ('function' === typeof options) callback = options, options = {};
+ if(options == null) options = {};
+ if(!('function' === typeof callback)) callback = null;
+ var self = this;
+ insertAll(self, Array.isArray(docs) ? docs : [docs], options, callback);
+ return this;
+};
+
+/**
+ * @ignore
+ */
+var checkCollectionName = function checkCollectionName (collectionName) {
+ if ('string' !== typeof collectionName) {
+ throw Error("collection name must be a String");
+ }
+
+ if (!collectionName || collectionName.indexOf('..') != -1) {
+ throw Error("collection names cannot be empty");
+ }
+
+ if (collectionName.indexOf('$') != -1 &&
+ collectionName.match(/((^\$cmd)|(oplog\.\$main))/) == null) {
+ throw Error("collection names must not contain '$'");
+ }
+
+ if (collectionName.match(/^\.|\.$/) != null) {
+ throw Error("collection names must not start or end with '.'");
+ }
+};
+
+/**
+ * Removes documents specified by `selector` from the db.
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **single** {Boolean, default:false}, removes the first document found.
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {Object} [selector] optional select, no selector is equivalent to removing all documents.
+ * @param {Object} [options] additional options during remove.
+ * @param {Function} [callback] must be provided if you performing a remove with a writeconcern
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.remove = function remove(selector, options, callback) {
+ if ('function' === typeof selector) {
+ callback = selector;
+ selector = options = {};
+ } else if ('function' === typeof options) {
+ callback = options;
+ options = {};
+ }
+
+ // Ensure options
+ if(options == null) options = {};
+ if(!('function' === typeof callback)) callback = null;
+ // Ensure we have at least an empty selector
+ selector = selector == null ? {} : selector;
+ // Set up flags for the command, if we have a single document remove
+ var flags = 0 | (options.single ? 1 : 0);
+
+ // DbName
+ var dbName = options['dbName'];
+ // If no dbname defined use the db one
+ if(dbName == null) {
+ dbName = this.db.databaseName;
+ }
+
+ // Create a delete command
+ var deleteCommand = new DeleteCommand(
+ this.db
+ , dbName + "." + this.collectionName
+ , selector
+ , flags);
+
+ var self = this;
+ var errorOptions = _getWriteConcern(self, options, callback);
+ // Execute the command, do not add a callback as it's async
+ if(_hasWriteConcern(errorOptions) && typeof callback == 'function') {
+ // Insert options
+ var commandOptions = {read:false};
+ // If we have safe set set async to false
+ if(errorOptions == null) commandOptions['async'] = true;
+ // Set safe option
+ commandOptions['safe'] = true;
+ // If we have an error option
+ if(typeof errorOptions == 'object') {
+ var keys = Object.keys(errorOptions);
+ for(var i = 0; i < keys.length; i++) {
+ commandOptions[keys[i]] = errorOptions[keys[i]];
+ }
+ }
+
+ // Execute command with safe options (rolls up both command and safe command into one and executes them on the same connection)
+ this.db._executeRemoveCommand(deleteCommand, commandOptions, function (err, error) {
+ error = error && error.documents;
+ if(!callback) return;
+
+ if(err) {
+ callback(err);
+ } else if(error[0].err || error[0].errmsg) {
+ callback(self.db.wrap(error[0]));
+ } else {
+ callback(null, error[0].n);
+ }
+ });
+ } else if(_hasWriteConcern(errorOptions) && callback == null) {
+ throw new Error("Cannot use a writeConcern without a provided callback");
+ } else {
+ var result = this.db._executeRemoveCommand(deleteCommand);
+ // If no callback just return
+ if (!callback) return;
+ // If error return error
+ if (result instanceof Error) {
+ return callback(result);
+ }
+ // Otherwise just return
+ return callback();
+ }
+};
+
+/**
+ * Renames the collection.
+ *
+ * @param {String} newName the new name of the collection.
+ * @param {Function} callback the callback accepting the result
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.rename = function rename (newName, callback) {
+ var self = this;
+ // Ensure the new name is valid
+ checkCollectionName(newName);
+ // Execute the command, return the new renamed collection if successful
+ self.db._executeQueryCommand(DbCommand.createRenameCollectionCommand(self.db, self.collectionName, newName), function(err, result) {
+ if(err == null && result.documents[0].ok == 1) {
+ if(callback != null) {
+ // Set current object to point to the new name
+ self.collectionName = newName;
+ // Return the current collection
+ callback(null, self);
+ }
+ } else if(result.documents[0].errmsg != null) {
+ if(callback != null) {
+ err != null ? callback(err, null) : callback(self.db.wrap(result.documents[0]), null);
+ }
+ }
+ });
+};
+
+/**
+ * @ignore
+ */
+var insertAll = function insertAll (self, docs, options, callback) {
+ if('function' === typeof options) callback = options, options = {};
+ if(options == null) options = {};
+ if(!('function' === typeof callback)) callback = null;
+
+ // Insert options (flags for insert)
+ var insertFlags = {};
+ // If we have a mongodb version >= 1.9.1 support keepGoing attribute
+ if(options['keepGoing'] != null) {
+ insertFlags['keepGoing'] = options['keepGoing'];
+ }
+
+ // If we have a mongodb version >= 1.9.1 support keepGoing attribute
+ if(options['continueOnError'] != null) {
+ insertFlags['continueOnError'] = options['continueOnError'];
+ }
+
+ // DbName
+ var dbName = options['dbName'];
+ // If no dbname defined use the db one
+ if(dbName == null) {
+ dbName = self.db.databaseName;
+ }
+
+ // Either use override on the function, or go back to default on either the collection
+ // level or db
+ if(options['serializeFunctions'] != null) {
+ insertFlags['serializeFunctions'] = options['serializeFunctions'];
+ } else {
+ insertFlags['serializeFunctions'] = self.serializeFunctions;
+ }
+
+ // Pass in options
+ var insertCommand = new InsertCommand(
+ self.db
+ , dbName + "." + self.collectionName, true, insertFlags);
+
+ // Add the documents and decorate them with id's if they have none
+ for(var index = 0, len = docs.length; index < len; ++index) {
+ var doc = docs[index];
+
+ // Add id to each document if it's not already defined
+ if (!(Buffer.isBuffer(doc)) && doc['_id'] == null && self.db.forceServerObjectId != true) {
+ doc['_id'] = self.pkFactory.createPk();
+ }
+
+ insertCommand.add(doc);
+ }
+
+ // Collect errorOptions
+ var errorOptions = _getWriteConcern(self, options, callback);
+ // Default command options
+ var commandOptions = {};
+ // If safe is defined check for error message
+ if(_hasWriteConcern(errorOptions) && typeof callback == 'function') {
+ // Insert options
+ commandOptions['read'] = false;
+ // If we have safe set set async to false
+ if(errorOptions == null) commandOptions['async'] = true;
+
+ // Set safe option
+ commandOptions['safe'] = errorOptions;
+ // If we have an error option
+ if(typeof errorOptions == 'object') {
+ var keys = Object.keys(errorOptions);
+ for(var i = 0; i < keys.length; i++) {
+ commandOptions[keys[i]] = errorOptions[keys[i]];
+ }
+ }
+
+ // Execute command with safe options (rolls up both command and safe command into one and executes them on the same connection)
+ self.db._executeInsertCommand(insertCommand, commandOptions, function (err, error) {
+ error = error && error.documents;
+ if(!callback) return;
+
+ if (err) {
+ callback(err);
+ } else if(error[0].err || error[0].errmsg) {
+ callback(self.db.wrap(error[0]));
+ } else {
+ callback(null, docs);
+ }
+ });
+ } else if(_hasWriteConcern(errorOptions) && callback == null) {
+ throw new Error("Cannot use a writeConcern without a provided callback");
+ } else {
+ // Execute the call without a write concern
+ var result = self.db._executeInsertCommand(insertCommand, commandOptions);
+ // If no callback just return
+ if(!callback) return;
+ // If error return error
+ if(result instanceof Error) {
+ return callback(result);
+ }
+ // Otherwise just return
+ return callback(null, docs);
+ }
+};
+
+/**
+ * Save a document. Simple full document replacement function. Not recommended for efficiency, use atomic
+ * operators and update instead for more efficient operations.
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {Object} [doc] the document to save
+ * @param {Object} [options] additional options during remove.
+ * @param {Function} [callback] must be provided if you performing a safe save
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.save = function save(doc, options, callback) {
+ if('function' === typeof options) callback = options, options = null;
+ if(options == null) options = {};
+ if(!('function' === typeof callback)) callback = null;
+ // Extract the id, if we have one we need to do a update command
+ var id = doc['_id'];
+ var commandOptions = _getWriteConcern(this, options, callback);
+
+ if(id) {
+ commandOptions.upsert = true;
+ this.update({ _id: id }, doc, commandOptions, callback);
+ } else {
+ this.insert(doc, commandOptions, callback && function (err, docs) {
+ if (err) return callback(err, null);
+
+ if (Array.isArray(docs)) {
+ callback(err, docs[0]);
+ } else {
+ callback(err, docs);
+ }
+ });
+ }
+};
+
+/**
+ * Updates documents.
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **upsert** {Boolean, default:false}, perform an upsert operation.
+ * - **multi** {Boolean, default:false}, update all documents matching the selector.
+ * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document.
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {Object} selector the query to select the document/documents to be updated
+ * @param {Object} document the fields/vals to be updated, or in the case of an upsert operation, inserted.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} [callback] must be provided if you performing an update with a writeconcern
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.update = function update(selector, document, options, callback) {
+ if('function' === typeof options) callback = options, options = null;
+ if(options == null) options = {};
+ if(!('function' === typeof callback)) callback = null;
+
+ // DbName
+ var dbName = options['dbName'];
+ // If no dbname defined use the db one
+ if(dbName == null) {
+ dbName = this.db.databaseName;
+ }
+
+ // Either use override on the function, or go back to default on either the collection
+ // level or db
+ if(options['serializeFunctions'] != null) {
+ options['serializeFunctions'] = options['serializeFunctions'];
+ } else {
+ options['serializeFunctions'] = this.serializeFunctions;
+ }
+
+ var updateCommand = new UpdateCommand(
+ this.db
+ , dbName + "." + this.collectionName
+ , selector
+ , document
+ , options);
+
+ var self = this;
+ // Unpack the error options if any
+ var errorOptions = _getWriteConcern(this, options, callback);
+ // If safe is defined check for error message
+ if(_hasWriteConcern(errorOptions) && typeof callback == 'function') {
+ // Insert options
+ var commandOptions = {read:false};
+ // If we have safe set set async to false
+ if(errorOptions == null) commandOptions['async'] = true;
+ // Set safe option
+ commandOptions['safe'] = errorOptions;
+ // If we have an error option
+ if(typeof errorOptions == 'object') {
+ var keys = Object.keys(errorOptions);
+ for(var i = 0; i < keys.length; i++) {
+ commandOptions[keys[i]] = errorOptions[keys[i]];
+ }
+ }
+
+ // Execute command with safe options (rolls up both command and safe command into one and executes them on the same connection)
+ this.db._executeUpdateCommand(updateCommand, commandOptions, function (err, error) {
+ error = error && error.documents;
+ if(!callback) return;
+
+ if(err) {
+ callback(err);
+ } else if(error[0].err || error[0].errmsg) {
+ callback(self.db.wrap(error[0]));
+ } else {
+ // Perform the callback
+ callback(null, error[0].n, error[0]);
+ }
+ });
+ } else if(_hasWriteConcern(errorOptions) && callback == null) {
+ throw new Error("Cannot use a writeConcern without a provided callback");
+ } else {
+ // Execute update
+ var result = this.db._executeUpdateCommand(updateCommand);
+ // If no callback just return
+ if (!callback) return;
+ // If error return error
+ if (result instanceof Error) {
+ return callback(result);
+ }
+ // Otherwise just return
+ return callback();
+ }
+};
+
+/**
+ * The distinct command returns returns a list of distinct values for the given key across a collection.
+ *
+ * Options
+ * - **readPreference** {String}, the preferred read preference (Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {String} key key to run distinct against.
+ * @param {Object} [query] option query to narrow the returned objects.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from distinct or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.distinct = function distinct(key, query, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ query = args.length ? args.shift() : {};
+ options = args.length ? args.shift() : {};
+
+ var mapCommandHash = {
+ 'distinct': this.collectionName
+ , 'query': query
+ , 'key': key
+ };
+
+ // Set read preference if we set one
+ var readPreference = options['readPreference'] ? options['readPreference'] : false;
+ // Create the command
+ var cmd = DbCommand.createDbSlaveOkCommand(this.db, mapCommandHash);
+
+ this.db._executeQueryCommand(cmd, {read:readPreference}, function (err, result) {
+ if(err)
+ return callback(err);
+ if(result.documents[0].ok != 1)
+ return callback(new Error(result.documents[0].errmsg));
+ callback(null, result.documents[0].values);
+ });
+};
+
+/**
+ * Count number of matching documents in the db to a query.
+ *
+ * Options
+ * - **readPreference** {String}, the preferred read preference (Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {Object} [query] query to filter by before performing count.
+ * @param {Object} [options] additional options during count.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the count method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.count = function count (query, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ query = args.length ? args.shift() : {};
+ options = args.length ? args.shift() : {};
+
+ // Final query
+ var final_query = {
+ 'count': this.collectionName
+ , 'query': query
+ , 'fields': null
+ };
+
+ // Set read preference if we set one
+ var readPreference = options['readPreference'] ? options['readPreference'] : false;
+
+ // Set up query options
+ var queryOptions = QueryCommand.OPTS_NO_CURSOR_TIMEOUT;
+ if (this.slaveOk || this.db.slaveOk) {
+ queryOptions |= QueryCommand.OPTS_SLAVE;
+ }
+
+ var queryCommand = new QueryCommand(
+ this.db
+ , this.db.databaseName + ".$cmd"
+ , queryOptions
+ , 0
+ , -1
+ , final_query
+ , null
+ );
+
+ var self = this;
+ this.db._executeQueryCommand(queryCommand, {read:readPreference}, function (err, result) {
+ result = result && result.documents;
+ if(!callback) return;
+
+ if(err) return callback(err);
+ if (result[0].ok != 1 || result[0].errmsg) return callback(self.db.wrap(result[0]));
+ callback(null, result[0].n);
+ });
+};
+
+
+/**
+ * Drop the collection
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the drop method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.drop = function drop(callback) {
+ this.db.dropCollection(this.collectionName, callback);
+};
+
+/**
+ * Find and update a document.
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **remove** {Boolean, default:false}, set to true to remove the object before returning.
+ * - **upsert** {Boolean, default:false}, perform an upsert operation.
+ * - **new** {Boolean, default:false}, set to true if you want to return the modified object rather than the original. Ignored for remove.
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {Object} query query object to locate the object to modify
+ * @param {Array} sort - if multiple docs match, choose the first one in the specified sort order as the object to manipulate
+ * @param {Object} doc - the fields/vals to be updated
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the findAndModify method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.findAndModify = function findAndModify (query, sort, doc, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ sort = args.length ? args.shift() : [];
+ doc = args.length ? args.shift() : null;
+ options = args.length ? args.shift() : {};
+ var self = this;
+
+ var queryObject = {
+ 'findandmodify': this.collectionName
+ , 'query': query
+ , 'sort': utils.formattedOrderClause(sort)
+ };
+
+ queryObject.new = options.new ? 1 : 0;
+ queryObject.remove = options.remove ? 1 : 0;
+ queryObject.upsert = options.upsert ? 1 : 0;
+
+ if (options.fields) {
+ queryObject.fields = options.fields;
+ }
+
+ if (doc && !options.remove) {
+ queryObject.update = doc;
+ }
+
+ // Either use override on the function, or go back to default on either the collection
+ // level or db
+ if(options['serializeFunctions'] != null) {
+ options['serializeFunctions'] = options['serializeFunctions'];
+ } else {
+ options['serializeFunctions'] = this.serializeFunctions;
+ }
+
+ // Unpack the error options if any
+ var errorOptions = _getWriteConcern(this, options, callback);
+
+ // If we have j, w or something else do the getLast Error path
+ if(errorOptions != null && typeof errorOptions == 'object') {
+ // Commands to send
+ var commands = [];
+ // Add the find and modify command
+ commands.push(DbCommand.createDbCommand(this.db, queryObject, options));
+ // If we have safe defined we need to return both call results
+ var chainedCommands = errorOptions != null ? true : false;
+ // Add error command if we have one
+ if(chainedCommands) {
+ commands.push(DbCommand.createGetLastErrorCommand(errorOptions, this.db));
+ }
+
+ // Fire commands and
+ this.db._executeQueryCommand(commands, {read:false}, function(err, result) {
+ if(err != null) return callback(err);
+ result = result && result.documents;
+
+ if(result[0].err != null) return callback(self.db.wrap(result[0]), null);
+ // Workaround due to 1.8.X returning an error on no matching object
+ // while 2.0.X does not not, making 2.0.X behaviour standard
+ if(result[0].errmsg != null && !result[0].errmsg.match(eErrorMessages))
+ return callback(self.db.wrap(result[0]), null, result[0]);
+ return callback(null, result[0].value, result[0]);
+ });
+ } else {
+ // Only run command and rely on getLastError command
+ var command = DbCommand.createDbCommand(this.db, queryObject, options)
+ // Execute command
+ this.db._executeQueryCommand(command, {read:false}, function(err, result) {
+ if(err != null) return callback(err);
+ result = result && result.documents;
+ if(result[0].errmsg != null && !result[0].errmsg.match(eErrorMessages))
+ return callback(self.db.wrap(result[0]), null, result[0]);
+ // If we have an error return it
+ if(result[0].lastErrorObject && result[0].lastErrorObject.err != null) return callback(self.db.wrap(result[0].lastErrorObject), null);
+ return callback(null, result[0].value, result[0]);
+ });
+ }
+}
+
+/**
+ * Find and remove a document
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {Object} query query object to locate the object to modify
+ * @param {Array} sort - if multiple docs match, choose the first one in the specified sort order as the object to manipulate
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the findAndRemove method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.findAndRemove = function(query, sort, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ sort = args.length ? args.shift() : [];
+ options = args.length ? args.shift() : {};
+ // Add the remove option
+ options['remove'] = true;
+ // Execute the callback
+ this.findAndModify(query, sort, null, options, callback);
+}
+
+var testForFields = {
+ limit: 1, sort: 1, fields:1, skip: 1, hint: 1, explain: 1, snapshot: 1, timeout: 1, tailable: 1, tailableRetryInterval: 1
+ , numberOfRetries: 1, awaitdata: 1, exhaust: 1, batchSize: 1, returnKey: 1, maxScan: 1, min: 1, max: 1, showDiskLoc: 1
+ , comment: 1, raw: 1, readPreference: 1, numberOfRetries: 1, partial: 1, read: 1, dbName: 1
+};
+
+/**
+ * Creates a cursor for a query that can be used to iterate over results from MongoDB
+ *
+ * Various argument possibilities
+ * - callback?
+ * - selector, callback?,
+ * - selector, fields, callback?
+ * - selector, options, callback?
+ * - selector, fields, options, callback?
+ * - selector, fields, skip, limit, callback?
+ * - selector, fields, skip, limit, timeout, callback?
+ *
+ * Options
+ * - **limit** {Number, default:0}, sets the limit of documents returned in the query.
+ * - **sort** {Array | Object}, set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc.
+ * - **fields** {Object}, the fields to return in the query. Object of fields to include or exclude (not both), {'a':1}
+ * - **skip** {Number, default:0}, set to skip N documents ahead in your query (useful for pagination).
+ * - **hint** {Object}, tell the query to use specific indexes in the query. Object of indexes to use, {'_id':1}
+ * - **explain** {Boolean, default:false}, explain the query instead of returning the data.
+ * - **snapshot** {Boolean, default:false}, snapshot query.
+ * - **timeout** {Boolean, default:false}, specify if the cursor can timeout.
+ * - **tailable** {Boolean, default:false}, specify if the cursor is tailable.
+ * - **tailableRetryInterval** {Number, default:100}, specify the miliseconds between getMores on tailable cursor.
+ * - **numberOfRetries** {Number, default:5}, specify the number of times to retry the tailable cursor.
+ * - **awaitdata** {Boolean, default:false} allow the cursor to wait for data, only applicable for tailable cursor.
+ * - **exhaust** {Boolean, default:false} have the server send all the documents at once as getMore packets, not recommended.
+ * - **batchSize** {Number, default:0}, set the batchSize for the getMoreCommand when iterating over the query results.
+ * - **returnKey** {Boolean, default:false}, only return the index key.
+ * - **maxScan** {Number}, Limit the number of items to scan.
+ * - **min** {Number}, Set index bounds.
+ * - **max** {Number}, Set index bounds.
+ * - **showDiskLoc** {Boolean, default:false}, Show disk location of results.
+ * - **comment** {String}, You can put a $comment field on a query to make looking in the profiler logs simpler.
+ * - **raw** {Boolean, default:false}, Return all BSON documents as Raw Buffer documents.
+ * - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ * - **numberOfRetries** {Number, default:5}, if using awaidata specifies the number of times to retry on timeout.
+ * - **partial** {Boolean, default:false}, specify if the cursor should return partial results when querying against a sharded system
+ *
+ * @param {Object} query query object to locate the object to modify
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the find method or null if an error occured.
+ * @return {Cursor} returns a cursor to the query
+ * @api public
+ */
+Collection.prototype.find = function find () {
+ var options
+ , args = Array.prototype.slice.call(arguments, 0)
+ , has_callback = typeof args[args.length - 1] === 'function'
+ , has_weird_callback = typeof args[0] === 'function'
+ , callback = has_callback ? args.pop() : (has_weird_callback ? args.shift() : null)
+ , len = args.length
+ , selector = len >= 1 ? args[0] : {}
+ , fields = len >= 2 ? args[1] : undefined;
+
+ if(len === 1 && has_weird_callback) {
+ // backwards compat for callback?, options case
+ selector = {};
+ options = args[0];
+ }
+
+ if(len === 2 && !Array.isArray(fields)) {
+ var fieldKeys = Object.getOwnPropertyNames(fields);
+ var is_option = false;
+
+ for(var i = 0; i < fieldKeys.length; i++) {
+ if(testForFields[fieldKeys[i]] != null) {
+ is_option = true;
+ break;
+ }
+ }
+
+ if(is_option) {
+ options = fields;
+ fields = undefined;
+ } else {
+ options = {};
+ }
+ } else if(len === 2 && Array.isArray(fields) && !Array.isArray(fields[0])) {
+ var newFields = {};
+ // Rewrite the array
+ for(var i = 0; i < fields.length; i++) {
+ newFields[fields[i]] = 1;
+ }
+ // Set the fields
+ fields = newFields;
+ }
+
+ if(3 === len) {
+ options = args[2];
+ }
+
+ // Ensure selector is not null
+ selector = selector == null ? {} : selector;
+ // 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("query selector raw message size does not match message header size [" + object.length + "] != [" + object_size + "]");
+ error.name = 'MongoError';
+ throw error;
+ }
+ }
+
+ // Validate correctness of the field selector
+ var object = fields;
+ 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;
+ }
+ }
+
+ // Check special case where we are using an objectId
+ if(selector instanceof ObjectID) {
+ selector = {_id:selector};
+ }
+
+ // If it's a serialized fields field we need to just let it through
+ // user be warned it better be good
+ if(options && options.fields && !(Buffer.isBuffer(options.fields))) {
+ fields = {};
+
+ if(Array.isArray(options.fields)) {
+ if(!options.fields.length) {
+ fields['_id'] = 1;
+ } else {
+ for (var i = 0, l = options.fields.length; i < l; i++) {
+ fields[options.fields[i]] = 1;
+ }
+ }
+ } else {
+ fields = options.fields;
+ }
+ }
+
+ if (!options) options = {};
+ options.skip = len > 3 ? args[2] : options.skip ? options.skip : 0;
+ options.limit = len > 3 ? args[3] : options.limit ? options.limit : 0;
+ options.raw = options.raw != null && typeof options.raw === 'boolean' ? options.raw : this.raw;
+ options.hint = options.hint != null ? normalizeHintField(options.hint) : this.internalHint;
+ options.timeout = len == 5 ? args[4] : typeof options.timeout === 'undefined' ? undefined : options.timeout;
+ // If we have overridden slaveOk otherwise use the default db setting
+ options.slaveOk = options.slaveOk != null ? options.slaveOk : this.db.slaveOk;
+
+ // Set option
+ var o = options;
+ // Support read/readPreference
+ if(o["read"] != null) o["readPreference"] = o["read"];
+ // Set the read preference
+ o.read = o["readPreference"] ? o.readPreference : this.readPreference;
+ // Adjust slave ok if read preference is secondary or secondary only
+ if(o.read == "secondary" || o.read == "secondaryOnly") options.slaveOk = true;
+
+ // callback for backward compatibility
+ if(callback) {
+ // TODO refactor Cursor args
+ callback(null, new Cursor(this.db, this, selector, fields, o));
+ } else {
+ return new Cursor(this.db, this, selector, fields, o);
+ }
+};
+
+/**
+ * Normalizes a `hint` argument.
+ *
+ * @param {String|Object|Array} hint
+ * @return {Object}
+ * @api private
+ */
+var normalizeHintField = function normalizeHintField(hint) {
+ var finalHint = null;
+
+ if (null != hint) {
+ switch (hint.constructor) {
+ case String:
+ finalHint = {};
+ finalHint[hint] = 1;
+ break;
+ case Object:
+ finalHint = {};
+ for (var name in hint) {
+ finalHint[name] = hint[name];
+ }
+ break;
+ case Array:
+ finalHint = {};
+ hint.forEach(function(param) {
+ finalHint[param] = 1;
+ });
+ break;
+ }
+ }
+
+ return finalHint;
+};
+
+/**
+ * Finds a single document based on the query
+ *
+ * Various argument possibilities
+ * - callback?
+ * - selector, callback?,
+ * - selector, fields, callback?
+ * - selector, options, callback?
+ * - selector, fields, options, callback?
+ * - selector, fields, skip, limit, callback?
+ * - selector, fields, skip, limit, timeout, callback?
+ *
+ * Options
+ * - **limit** {Number, default:0}, sets the limit of documents returned in the query.
+ * - **sort** {Array | Object}, set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc.
+ * - **fields** {Object}, the fields to return in the query. Object of fields to include or exclude (not both), {'a':1}
+ * - **skip** {Number, default:0}, set to skip N documents ahead in your query (useful for pagination).
+ * - **hint** {Object}, tell the query to use specific indexes in the query. Object of indexes to use, {'_id':1}
+ * - **explain** {Boolean, default:false}, explain the query instead of returning the data.
+ * - **snapshot** {Boolean, default:false}, snapshot query.
+ * - **timeout** {Boolean, default:false}, specify if the cursor can timeout.
+ * - **tailable** {Boolean, default:false}, specify if the cursor is tailable.
+ * - **batchSize** {Number, default:0}, set the batchSize for the getMoreCommand when iterating over the query results.
+ * - **returnKey** {Boolean, default:false}, only return the index key.
+ * - **maxScan** {Number}, Limit the number of items to scan.
+ * - **min** {Number}, Set index bounds.
+ * - **max** {Number}, Set index bounds.
+ * - **showDiskLoc** {Boolean, default:false}, Show disk location of results.
+ * - **comment** {String}, You can put a $comment field on a query to make looking in the profiler logs simpler.
+ * - **raw** {Boolean, default:false}, Return all BSON documents as Raw Buffer documents.
+ * - **readPreference** {String}, the preferred read preference (Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ * - **partial** {Boolean, default:false}, specify if the cursor should return partial results when querying against a sharded system
+ *
+ * @param {Object} query query object to locate the object to modify
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the findOne method or null if an error occured.
+ * @return {Cursor} returns a cursor to the query
+ * @api public
+ */
+Collection.prototype.findOne = function findOne () {
+ var self = this;
+ var args = Array.prototype.slice.call(arguments, 0);
+ var callback = args.pop();
+ var cursor = this.find.apply(this, args).limit(-1).batchSize(1);
+ // Return the item
+ cursor.toArray(function(err, items) {
+ if(err != null) return callback(err instanceof Error ? err : self.db.wrap(new Error(err)), null);
+ if(items.length == 1) return callback(null, items[0]);
+ callback(null, null);
+ });
+};
+
+/**
+ * Creates an index on the collection.
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **unique** {Boolean, default:false}, creates an unique index.
+ * - **sparse** {Boolean, default:false}, creates a sparse index.
+ * - **background** {Boolean, default:false}, creates the index in the background, yielding whenever possible.
+ * - **dropDups** {Boolean, default:false}, a unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
+ * - **min** {Number}, for geospatial indexes set the lower bound for the co-ordinates.
+ * - **max** {Number}, for geospatial indexes set the high bound for the co-ordinates.
+ * - **v** {Number}, specify the format version of the indexes.
+ * - **expireAfterSeconds** {Number}, allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
+ * - **name** {String}, override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {Object} fieldOrSpec fieldOrSpec that defines the index.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the createIndex method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.createIndex = function createIndex (fieldOrSpec, options, callback) {
+ // Clean up call
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+ options = typeof callback === 'function' ? options : callback;
+ options = options == null ? {} : options;
+
+ // Collect errorOptions
+ var errorOptions = _getWriteConcern(this, options, callback);
+ // Execute create index
+ this.db.createIndex(this.collectionName, fieldOrSpec, options, callback);
+};
+
+/**
+ * Ensures that an index exists, if it does not it creates it
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **unique** {Boolean, default:false}, creates an unique index.
+ * - **sparse** {Boolean, default:false}, creates a sparse index.
+ * - **background** {Boolean, default:false}, creates the index in the background, yielding whenever possible.
+ * - **dropDups** {Boolean, default:false}, a unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
+ * - **min** {Number}, for geospatial indexes set the lower bound for the co-ordinates.
+ * - **max** {Number}, for geospatial indexes set the high bound for the co-ordinates.
+ * - **v** {Number}, specify the format version of the indexes.
+ * - **expireAfterSeconds** {Number}, allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
+ * - **name** {String}, override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {Object} fieldOrSpec fieldOrSpec that defines the index.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the ensureIndex method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.ensureIndex = function ensureIndex (fieldOrSpec, options, callback) {
+ // Clean up call
+ if (typeof callback === 'undefined' && typeof options === 'function') {
+ callback = options;
+ options = {};
+ }
+
+ if (options == null) {
+ options = {};
+ }
+
+ // Execute create index
+ this.db.ensureIndex(this.collectionName, fieldOrSpec, options, callback);
+};
+
+/**
+ * Retrieves this collections index info.
+ *
+ * Options
+ * - **full** {Boolean, default:false}, returns the full raw index information.
+ *
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the indexInformation method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.indexInformation = function indexInformation (options, callback) {
+ // Unpack calls
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+ // Call the index information
+ this.db.indexInformation(this.collectionName, options, callback);
+};
+
+/**
+ * Drops an index from this collection.
+ *
+ * @param {String} name
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the dropIndex method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.dropIndex = function dropIndex (name, callback) {
+ this.db.dropIndex(this.collectionName, name, callback);
+};
+
+/**
+ * Drops all indexes from this collection.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the dropAllIndexes method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.dropAllIndexes = function dropIndexes (callback) {
+ this.db.dropIndex(this.collectionName, '*', function (err, result) {
+ if(err != null) {
+ callback(err, false);
+ } else if(result.documents[0].errmsg == null) {
+ callback(null, true);
+ } else {
+ callback(new Error(result.documents[0].errmsg), false);
+ }
+ });
+};
+
+/**
+ * Drops all indexes from this collection.
+ *
+ * @deprecated
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the dropIndexes method or null if an error occured.
+ * @return {null}
+ * @api private
+ */
+Collection.prototype.dropIndexes = Collection.prototype.dropAllIndexes;
+
+/**
+ * Reindex all indexes on the collection
+ * Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the reIndex method or null if an error occured.
+ * @return {null}
+ * @api public
+**/
+Collection.prototype.reIndex = function(callback) {
+ this.db.reIndex(this.collectionName, callback);
+}
+
+/**
+ * Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
+ *
+ * Options
+ * - **out** {Object, default:*{inline:1}*}, sets the output target for the map reduce job. *{inline:1} | {replace:'collectionName'} | {merge:'collectionName'} | {reduce:'collectionName'}*
+ * - **query** {Object}, query filter object.
+ * - **sort** {Object}, sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces.
+ * - **limit** {Number}, number of objects to return from collection.
+ * - **keeptemp** {Boolean, default:false}, keep temporary data.
+ * - **finalize** {Function | String}, finalize function.
+ * - **scope** {Object}, can pass in variables that can be access from map/reduce/finalize.
+ * - **jsMode** {Boolean, default:false}, it is possible to make the execution stay in JS. Provided in MongoDB > 2.0.X.
+ * - **verbose** {Boolean, default:false}, provide statistics on job execution time.
+ * - **readPreference** {String, only for inline results}, the preferred read preference (Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {Function|String} map the mapping function.
+ * @param {Function|String} reduce the reduce function.
+ * @param {Objects} [options] options for the map reduce job.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the mapReduce method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.mapReduce = function mapReduce (map, reduce, options, callback) {
+ if ('function' === typeof options) callback = options, options = {};
+ // Out must allways be defined (make sure we don't break weirdly on pre 1.8+ servers)
+ if(null == options.out) {
+ throw new Error("the out option parameter must be defined, see mongodb docs for possible values");
+ }
+
+ if ('function' === typeof map) {
+ map = map.toString();
+ }
+
+ if ('function' === typeof reduce) {
+ reduce = reduce.toString();
+ }
+
+ if ('function' === typeof options.finalize) {
+ options.finalize = options.finalize.toString();
+ }
+
+ var mapCommandHash = {
+ mapreduce: this.collectionName
+ , map: map
+ , reduce: reduce
+ };
+
+ // Add any other options passed in
+ for (var name in options) {
+ mapCommandHash[name] = options[name];
+ }
+
+ // Set read preference if we set one
+ var readPreference = options['readPreference'] ? options['readPreference'] : false;
+ // If we have a read preference and inline is not set as output fail hard
+ if(readPreference != false && options['out'] != 'inline') {
+ throw new Error("a readPreference can only be provided when performing an inline mapReduce");
+ }
+
+ // self
+ var self = this;
+ var cmd = DbCommand.createDbCommand(this.db, mapCommandHash);
+
+ this.db._executeQueryCommand(cmd, {read:readPreference}, function (err, result) {
+ if (err) {
+ return callback(err);
+ }
+
+ //
+ if (1 != result.documents[0].ok || result.documents[0].err || result.documents[0].errmsg) {
+ return callback(self.db.wrap(result.documents[0]));
+ }
+
+ // Create statistics value
+ var stats = {};
+ if(result.documents[0].timeMillis) stats['processtime'] = result.documents[0].timeMillis;
+ if(result.documents[0].counts) stats['counts'] = result.documents[0].counts;
+ if(result.documents[0].timing) stats['timing'] = result.documents[0].timing;
+
+ // invoked with inline?
+ if(result.documents[0].results) {
+ return callback(null, result.documents[0].results, stats);
+ }
+
+ // The returned collection
+ var collection = null;
+
+ // If we have an object it's a different db
+ if(result.documents[0].result != null && typeof result.documents[0].result == 'object') {
+ var doc = result.documents[0].result;
+ collection = self.db.db(doc.db).collection(doc.collection);
+ } else {
+ // Create a collection object that wraps the result collection
+ collection = self.db.collection(result.documents[0].result)
+ }
+
+ // If we wish for no verbosity
+ if(options['verbose'] == null || !options['verbose']) {
+ return callback(err, collection);
+ }
+
+ // Return stats as third set of values
+ callback(err, collection, stats);
+ });
+};
+
+/**
+ * Group function helper
+ * @ignore
+ */
+var groupFunction = function () {
+ var c = db[ns].find(condition);
+ var map = new Map();
+ var reduce_function = reduce;
+
+ while (c.hasNext()) {
+ var obj = c.next();
+ var key = {};
+
+ for (var i = 0, len = keys.length; i < len; ++i) {
+ var k = keys[i];
+ key[k] = obj[k];
+ }
+
+ var aggObj = map.get(key);
+
+ if (aggObj == null) {
+ var newObj = Object.extend({}, key);
+ aggObj = Object.extend(newObj, initial);
+ map.put(key, aggObj);
+ }
+
+ reduce_function(obj, aggObj);
+ }
+
+ return { "result": map.values() };
+}.toString();
+
+/**
+ * Run a group command across a collection
+ *
+ * Options
+ * - **readPreference** {String}, the preferred read preference (Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {Object|Array|Function|Code} keys an object, array or function expressing the keys to group by.
+ * @param {Object} condition an optional condition that must be true for a row to be considered.
+ * @param {Object} initial initial value of the aggregation counter object.
+ * @param {Function|Code} reduce the reduce function aggregates (reduces) the objects iterated
+ * @param {Function|Code} finalize an optional function to be run on each item in the result set just before the item is returned.
+ * @param {Boolean} command specify if you wish to run using the internal group command or using eval, default is true.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the group method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.group = function group(keys, condition, initial, reduce, finalize, command, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 3);
+ callback = args.pop();
+ // Fetch all commands
+ reduce = args.length ? args.shift() : null;
+ finalize = args.length ? args.shift() : null;
+ command = args.length ? args.shift() : null;
+ options = args.length ? args.shift() : {};
+
+ // Make sure we are backward compatible
+ if(!(typeof finalize == 'function')) {
+ command = finalize;
+ finalize = null;
+ }
+
+ if (!Array.isArray(keys) && keys instanceof Object && typeof(keys) !== 'function' && !(keys instanceof Code)) {
+ keys = Object.keys(keys);
+ }
+
+ if(typeof reduce === 'function') {
+ reduce = reduce.toString();
+ }
+
+ if(typeof finalize === 'function') {
+ finalize = finalize.toString();
+ }
+
+ // Set up the command as default
+ command = command == null ? true : command;
+
+ // Execute using the command
+ if(command) {
+ var reduceFunction = reduce instanceof Code
+ ? reduce
+ : new Code(reduce);
+
+ var selector = {
+ group: {
+ 'ns': this.collectionName
+ , '$reduce': reduceFunction
+ , 'cond': condition
+ , 'initial': initial
+ , 'out': "inline"
+ }
+ };
+
+ // if finalize is defined
+ if(finalize != null) selector.group['finalize'] = finalize;
+ // Set up group selector
+ if ('function' === typeof keys || keys instanceof Code) {
+ selector.group.$keyf = keys instanceof Code
+ ? keys
+ : new Code(keys);
+ } else {
+ var hash = {};
+ keys.forEach(function (key) {
+ hash[key] = 1;
+ });
+ selector.group.key = hash;
+ }
+
+ var cmd = DbCommand.createDbSlaveOkCommand(this.db, selector);
+ // Set read preference if we set one
+ var readPreference = options['readPreference'] ? options['readPreference'] : false;
+
+ this.db._executeQueryCommand(cmd, {read:readPreference}, function (err, result) {
+ if(err != null) return callback(err);
+
+ var document = result.documents[0];
+ if (null == document.retval) {
+ return callback(new Error("group command failed: " + document.errmsg));
+ }
+
+ callback(null, document.retval);
+ });
+
+ } else {
+ // Create execution scope
+ var scope = reduce != null && reduce instanceof Code
+ ? reduce.scope
+ : {};
+
+ scope.ns = this.collectionName;
+ scope.keys = keys;
+ scope.condition = condition;
+ scope.initial = initial;
+
+ // Pass in the function text to execute within mongodb.
+ var groupfn = groupFunction.replace(/ reduce;/, reduce.toString() + ';');
+
+ this.db.eval(new Code(groupfn, scope), function (err, results) {
+ if (err) return callback(err, null);
+ callback(null, results.result || results);
+ });
+ }
+};
+
+/**
+ * Returns the options of the collection.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the options method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.options = function options(callback) {
+ this.db.collectionsInfo(this.collectionName, function (err, cursor) {
+ if (err) return callback(err);
+ cursor.nextObject(function (err, document) {
+ callback(err, document && document.options || null);
+ });
+ });
+};
+
+/**
+ * Returns if the collection is a capped collection
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the isCapped method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.isCapped = function isCapped(callback) {
+ this.options(function(err, document) {
+ if(err != null) {
+ callback(err);
+ } else {
+ callback(null, document && document.capped);
+ }
+ });
+};
+
+/**
+ * Checks if one or more indexes exist on the collection
+ *
+ * @param {String|Array} indexNames check if one or more indexes exist on the collection.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the indexExists method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.indexExists = function indexExists(indexes, callback) {
+ this.indexInformation(function(err, indexInformation) {
+ // If we have an error return
+ if(err != null) return callback(err, null);
+ // Let's check for the index names
+ if(Array.isArray(indexes)) {
+ for(var i = 0; i < indexes.length; i++) {
+ if(indexInformation[indexes[i]] == null) {
+ return callback(null, false);
+ }
+ }
+
+ // All keys found return true
+ return callback(null, true);
+ } else {
+ return callback(null, indexInformation[indexes] != null);
+ }
+ });
+}
+
+/**
+ * Execute the geoNear command to search for items in the collection
+ *
+ * Options
+ * - **num** {Number}, max number of results to return.
+ * - **maxDistance** {Number}, include results up to maxDistance from the point.
+ * - **distanceMultiplier** {Number}, include a value to multiply the distances with allowing for range conversions.
+ * - **query** {Object}, filter the results by a query.
+ * - **spherical** {Boolean, default:false}, perform query using a spherical model.
+ * - **uniqueDocs** {Boolean, default:false}, the closest location in a document to the center of the search region will always be returned MongoDB > 2.X.
+ * - **includeLocs** {Boolean, default:false}, include the location data fields in the top level of the results MongoDB > 2.X.
+ * - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {Number} x point to search on the x axis, ensure the indexes are ordered in the same order.
+ * @param {Number} y point to search on the y axis, ensure the indexes are ordered in the same order.
+ * @param {Objects} [options] options for the map reduce job.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the geoNear method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.geoNear = function geoNear(x, y, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 2);
+ callback = args.pop();
+ // Fetch all commands
+ options = args.length ? args.shift() : {};
+
+ // Build command object
+ var commandObject = {
+ geoNear:this.collectionName,
+ near: [x, y]
+ }
+
+ // Decorate object if any with known properties
+ if(options['num'] != null) commandObject['num'] = options['num'];
+ if(options['maxDistance'] != null) commandObject['maxDistance'] = options['maxDistance'];
+ if(options['distanceMultiplier'] != null) commandObject['distanceMultiplier'] = options['distanceMultiplier'];
+ if(options['query'] != null) commandObject['query'] = options['query'];
+ if(options['spherical'] != null) commandObject['spherical'] = options['spherical'];
+ if(options['uniqueDocs'] != null) commandObject['uniqueDocs'] = options['uniqueDocs'];
+ if(options['includeLocs'] != null) commandObject['includeLocs'] = options['includeLocs'];
+
+ // Execute the command
+ this.db.command(commandObject, options, callback);
+}
+
+/**
+ * Execute a geo search using a geo haystack index on a collection.
+ *
+ * Options
+ * - **maxDistance** {Number}, include results up to maxDistance from the point.
+ * - **search** {Object}, filter the results by a query.
+ * - **limit** {Number}, max number of results to return.
+ * - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {Number} x point to search on the x axis, ensure the indexes are ordered in the same order.
+ * @param {Number} y point to search on the y axis, ensure the indexes are ordered in the same order.
+ * @param {Objects} [options] options for the map reduce job.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the geoHaystackSearch method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.geoHaystackSearch = function geoHaystackSearch(x, y, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 2);
+ callback = args.pop();
+ // Fetch all commands
+ options = args.length ? args.shift() : {};
+
+ // Build command object
+ var commandObject = {
+ geoSearch:this.collectionName,
+ near: [x, y]
+ }
+
+ // Decorate object if any with known properties
+ if(options['maxDistance'] != null) commandObject['maxDistance'] = options['maxDistance'];
+ if(options['query'] != null) commandObject['search'] = options['query'];
+ if(options['search'] != null) commandObject['search'] = options['search'];
+ if(options['limit'] != null) commandObject['limit'] = options['limit'];
+
+ // Execute the command
+ this.db.command(commandObject, options, callback);
+}
+
+/**
+ * Retrieve all the indexes on the collection.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the indexes method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.indexes = function indexes(callback) {
+ // Return all the index information
+ this.db.indexInformation(this.collectionName, {full:true}, callback);
+}
+
+/**
+ * Execute an aggregation framework pipeline against the collection, needs MongoDB >= 2.1
+ *
+ * Options
+ * - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {Array} array containing all the aggregation framework commands for the execution.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the aggregate method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.aggregate = function(pipeline, options, callback) {
+ // * - **explain** {Boolean}, return the query plan for the aggregation pipeline instead of the results. 2.3, 2.4
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ var self = this;
+
+ // If we have any of the supported options in the options object
+ var opts = args[args.length - 1];
+ options = opts.readPreference || opts.explain ? args.pop() : {}
+
+ // Convert operations to an array
+ if(!Array.isArray(args[0])) {
+ pipeline = [];
+ // Push all the operations to the pipeline
+ for(var i = 0; i < args.length; i++) pipeline.push(args[i]);
+ }
+
+ // Build the command
+ var command = { aggregate : this.collectionName, pipeline : pipeline};
+ // Add all options
+ var keys = Object.keys(options);
+ // Add all options
+ for(var i = 0; i < keys.length; i++) {
+ command[keys[i]] = options[keys[i]];
+ }
+
+ // Execute the command
+ this.db.command(command, options, function(err, result) {
+ if(err) {
+ callback(err);
+ } else if(result['err'] || result['errmsg']) {
+ callback(self.db.wrap(result));
+ } else if(typeof result == 'object' && result['serverPipeline']) {
+ callback(null, result);
+ } else {
+ callback(null, result.result);
+ }
+ });
+}
+
+/**
+ * Get all the collection statistics.
+ *
+ * Options
+ * - **scale** {Number}, divide the returned sizes by scale value.
+ * - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {Objects} [options] options for the stats command.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the stats method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Collection.prototype.stats = function stats(options, callback) {
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ // Fetch all commands
+ options = args.length ? args.shift() : {};
+
+ // Build command object
+ var commandObject = {
+ collStats:this.collectionName,
+ }
+
+ // Check if we have the scale value
+ if(options['scale'] != null) commandObject['scale'] = options['scale'];
+
+ // Execute the command
+ this.db.command(commandObject, options, callback);
+}
+
+/**
+ * @ignore
+ */
+Object.defineProperty(Collection.prototype, "hint", {
+ enumerable: true
+ , get: function () {
+ return this.internalHint;
+ }
+ , set: function (v) {
+ this.internalHint = normalizeHintField(v);
+ }
+});
+
+/**
+ * @ignore
+ */
+var _hasWriteConcern = function(errorOptions) {
+ return errorOptions == true
+ || errorOptions.w > 0
+ || errorOptions.w == 'majority'
+ || errorOptions.j == true
+ || errorOptions.journal == true
+ || errorOptions.fsync == true
+}
+
+/**
+ * @ignore
+ */
+var _setWriteConcernHash = function(options) {
+ var finalOptions = {};
+ if(options.w != null) finalOptions.w = options.w;
+ if(options.journal == true) finalOptions.j = options.journal;
+ if(options.j == true) finalOptions.j = options.j;
+ if(options.fsync == true) finalOptions.fsync = options.fsync;
+ if(options.wtimeout != null) finalOptions.wtimeout = options.wtimeout;
+ return finalOptions;
+}
+
+/**
+ * @ignore
+ */
+var _getWriteConcern = function(self, options, callback) {
+ // Final options
+ var finalOptions = {w:1};
+ // Local options verification
+ if(options.w != null || typeof options.j == 'boolean' || typeof options.journal == 'boolean' || typeof options.fsync == 'boolean') {
+ finalOptions = _setWriteConcernHash(options);
+ } else if(typeof options.safe == "boolean") {
+ finalOptions = {w: (options.safe ? 1 : 0)};
+ } else if(options.safe != null && typeof options.safe == 'object') {
+ finalOptions = _setWriteConcernHash(options.safe);
+ } else if(self.opts.w != null || typeof self.opts.j == 'boolean' || typeof self.opts.journal == 'boolean' || typeof self.opts.fsync == 'boolean') {
+ finalOptions = _setWriteConcernHash(self.opts);
+ } else if(typeof self.opts.safe == "boolean") {
+ finalOptions = {w: (self.opts.safe ? 1 : 0)};
+ } else if(self.db.safe.w != null || typeof self.db.safe.j == 'boolean' || typeof self.db.safe.journal == 'boolean' || typeof self.db.safe.fsync == 'boolean') {
+ finalOptions = _setWriteConcernHash(self.db.safe);
+ } else if(self.db.options.w != null || typeof self.db.options.j == 'boolean' || typeof self.db.options.journal == 'boolean' || typeof self.db.options.fsync == 'boolean') {
+ finalOptions = _setWriteConcernHash(self.db.options);
+ } else if(typeof self.db.safe == "boolean") {
+ finalOptions = {w: (self.db.safe ? 1 : 0)};
+ }
+
+ // Ensure we don't have an invalid combination of write concerns
+ if(finalOptions.w < 1
+ && (finalOptions.journal == true || finalOptions.j == true || finalOptions.fsync == true)) throw new Error("No acknowlegement using w < 1 cannot be combined with journal:true or fsync:true");
+
+ // Return the options
+ return finalOptions;
+}
+
+/**
+ * Expose.
+ */
+exports.Collection = Collection;
\ No newline at end of file
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..9558582
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/base_command.js
@@ -0,0 +1,29 @@
+/**
+ Base object used for common functionality
+**/
+var BaseCommand = exports.BaseCommand = function BaseCommand() {
+};
+
+var id = 1;
+BaseCommand.prototype.getRequestId = function getRequestId() {
+ if (!this.requestId) this.requestId = id++;
+ return this.requestId;
+};
+
+BaseCommand.prototype.setMongosReadPreference = function setMongosReadPreference(readPreference, tags) {}
+
+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..b1a798d
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/db_command.js
@@ -0,0 +1,240 @@
+var QueryCommand = require('./query_command').QueryCommand,
+ InsertCommand = require('./insert_command').InsertCommand,
+ inherits = require('util').inherits,
+ utils = require('../utils'),
+ 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";
+DbCommand.SYSTEM_JS_COLLECTION = "system.js";
+
+// 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, options) {
+ 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, authdb) {
+ // 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, authdb + "." + 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) {
+
+ if (typeof db === 'undefined') {
+ db = options;
+ options = {};
+ }
+ // 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]
+ }
+ }
+
+ // Special case for w == 1, remove the w
+ if(1 == command.w) {
+ delete command.w;
+ }
+
+ // 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('string' == typeof fieldOrSpec) {
+ // 'type'
+ indexes.push(fieldOrSpec + '_' + 1);
+ fieldHash[fieldOrSpec] = 1;
+
+ } else if(utils.isArray(fieldOrSpec)) {
+
+ fieldOrSpec.forEach(function(f) {
+ if('string' == typeof f) {
+ // [{location:'2d'}, 'type']
+ indexes.push(f + '_' + 1);
+ fieldHash[f] = 1;
+ } else if(utils.isArray(f)) {
+ // [['location', '2d'],['type', 1]]
+ indexes.push(f[0] + '_' + (f[1] || 1));
+ fieldHash[f[0]] = f[1] || 1;
+ } else if(utils.isObject(f)) {
+ // [{location:'2d'}, {type:1}]
+ keys = Object.keys(f);
+ keys.forEach(function(k) {
+ indexes.push(k + '_' + f[k]);
+ fieldHash[k] = f[k];
+ });
+ } else {
+ // undefined (ignore)
+ }
+ });
+
+ } else if(utils.isObject(fieldOrSpec)) {
+ // {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 = typeof options.name == 'string'
+ ? options.name
+ : indexes.join("_");
+
+ 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);
+ for(var i = 0; i < keys.length; i++) {
+ selector[keys[i]] = options[keys[i]];
+ }
+
+ if(selector['unique'] == null)
+ selector['unique'] = finalUnique;
+
+ var name = db.databaseName + "." + DbCommand.SYSTEM_INDEX_COLLECTION;
+ var cmd = new InsertCommand(db, name, false);
+ return cmd.add(selector);
+};
+
+DbCommand.logoutCommand = function(db, command_hash, options) {
+ var dbName = options != null && options['authdb'] != null ? options['authdb'] : db.databaseName;
+ // Create logout command
+ return new DbCommand(db, dbName + "." + 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.createAdminDbCommandSlaveOk = function(db, command_hash) {
+ return new DbCommand(db, "admin." + DbCommand.SYSTEM_COMMAND_COLLECTION, QueryCommand.OPTS_NO_CURSOR_TIMEOUT | QueryCommand.OPTS_SLAVE, 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);
+};
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..e6ae20a
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/delete_command.js
@@ -0,0 +1,114 @@
+var BaseCommand = require('./base_command').BaseCommand,
+ inherits = require('util').inherits;
+
+/**
+ Insert Document Command
+**/
+var DeleteCommand = exports.DeleteCommand = function(db, collectionName, selector, flags) {
+ 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.flags = flags;
+ 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 the 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
+
+ // 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..d6a2100
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/insert_command.js
@@ -0,0 +1,147 @@
+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;
+ }
+
+ // Check if we have keepGoing set -> set flag if it's the case
+ if(options['continueOnError'] != null && options['continueOnError']) {
+ // 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..76829d9
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/commands/query_command.js
@@ -0,0 +1,261 @@
+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,
+ object_size;
+ if(Buffer.isBuffer(object)) {
+ 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;
+ }
+ }
+
+ object = returnFieldSelector;
+ if(Buffer.isBuffer(object)) {
+ 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;
+
+ // Ensure we have no null query
+ query = query == null ? {} : query;
+ // Wrap query in the $query parameter so we can add read preferences for mongos
+ 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;
+
+/*
+ * Adds the read prefrence to the current command
+ */
+QueryCommand.prototype.setMongosReadPreference = function(readPreference, tags) {
+ // If we have readPreference set to true set to secondary prefered
+ if(readPreference == true) {
+ readPreference = 'secondaryPreferred';
+ } else if(readPreference == 'false') {
+ readPreference = 'primary';
+ }
+
+ // Force the slave ok flag to be set if we are not using primary read preference
+ if(readPreference != false && readPreference != 'primary') {
+ this.queryOptions |= QueryCommand.OPTS_SLAVE;
+ }
+
+ // Backward compatibility, ensure $query only set on read preference so 1.8.X works
+ if((readPreference != null || tags != null) && this.query['$query'] == null) {
+ this.query = {'$query': this.query};
+ }
+
+ // If we have no readPreference set and no tags, check if the slaveOk bit is set
+ if(readPreference == null && tags == null) {
+ // If we have a slaveOk bit set the read preference for MongoS
+ if(this.queryOptions & QueryCommand.OPTS_SLAVE) {
+ this.query['$readPreference'] = {mode: 'secondary'}
+ } else {
+ this.query['$readPreference'] = {mode: 'primary'}
+ }
+ }
+
+ // Build read preference object
+ if(typeof readPreference == 'object' && readPreference['_type'] == 'ReadPreference') {
+ this.query['$readPreference'] = readPreference.toObject();
+ } else if(readPreference != null) {
+ // Add the read preference
+ this.query['$readPreference'] = {mode: readPreference};
+
+ // If we have tags let's add them
+ if(tags != null) {
+ this.query['$readPreference']['tags'] = tags;
+ }
+ }
+}
+
+/*
+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() {
+ // Total length of the command
+ 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;
+QueryCommand.OPTS_PARTIAL = 128;
\ 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
diff --git a/node_modules/mongodb/lib/mongodb/connection/base.js b/node_modules/mongodb/lib/mongodb/connection/base.js
new file mode 100644
index 0000000..f0ff06f
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/base.js
@@ -0,0 +1,50 @@
+var EventEmitter = require('events').EventEmitter
+ , inherits = require('util').inherits;
+
+var Base = function Base() {
+ EventEmitter.call(this);
+}
+
+/**
+ * @ignore
+ */
+inherits(Base, EventEmitter);
+
+/**
+ * Fire all the errors
+ * @ignore
+ */
+Base.prototype.__executeAllCallbacksWithError = function(err) {
+ // Locate all the possible callbacks that need to return
+ for(var i = 0; i < this.dbInstances.length; i++) {
+ // Fetch the db Instance
+ var dbInstance = this.dbInstances[i];
+ // Check all callbacks
+ var keys = Object.keys(dbInstance._callBackStore._notReplied);
+ // For each key check if it's a callback that needs to be returned
+ for(var j = 0; j < keys.length; j++) {
+ var info = dbInstance._callBackStore._notReplied[keys[j]];
+ // Check if we have a chained command (findAndModify)
+ if(info && info['chained'] && Array.isArray(info['chained']) && info['chained'].length > 0) {
+ var chained = info['chained'];
+ // Only callback once and the last one is the right one
+ var finalCallback = chained.pop();
+ // Emit only the last event
+ dbInstance._callBackStore.emit(finalCallback, err, null);
+
+ // Put back the final callback to ensure we don't call all commands in the chain
+ chained.push(finalCallback);
+
+ // Remove all chained callbacks
+ for(var i = 0; i < chained.length; i++) {
+ delete dbInstance._callBackStore._notReplied[chained[i]];
+ }
+ } else {
+ dbInstance._callBackStore.emit(keys[j], err, null);
+ }
+ }
+ }
+}
+
+
+exports.Base = Base;
\ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/connection/connection.js b/node_modules/mongodb/lib/mongodb/connection/connection.js
new file mode 100644
index 0000000..f1c333a
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/connection.js
@@ -0,0 +1,440 @@
+var utils = require('./connection_utils'),
+ inherits = require('util').inherits,
+ net = require('net'),
+ EventEmitter = require('events').EventEmitter,
+ inherits = require('util').inherits,
+ binaryutils = require('../utils'),
+ tls = require('tls');
+
+var Connection = exports.Connection = function(id, socketOptions) {
+ // Set up event emitter
+ EventEmitter.call(this);
+ // Store all socket options
+ this.socketOptions = socketOptions ? socketOptions : {host:'localhost', port:27017, domainSocket:false};
+ // Set keep alive default if not overriden
+ if(this.socketOptions.keepAlive == null && (process.platform !== "sunos" || process.platform !== "win32")) this.socketOptions.keepAlive = 100;
+ // Id for the connection
+ this.id = id;
+ // State of the connection
+ this.connected = false;
+ // Set if this is a domain socket
+ this.domainSocket = this.socketOptions.domainSocket;
+
+ //
+ // Connection parsing state
+ //
+ this.maxBsonSize = socketOptions.maxBsonSize ? socketOptions.maxBsonSize : Connection.DEFAULT_MAX_BSON_SIZE;
+ // Contains the current message bytes
+ this.buffer = null;
+ // Contains the current message size
+ this.sizeOfMessage = 0;
+ // Contains the readIndex for the messaage
+ this.bytesRead = 0;
+ // Contains spill over bytes from additional messages
+ this.stubBuffer = 0;
+
+ // Just keeps list of events we allow
+ this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[], timeout:[], end:[]};
+
+ // Just keeps list of events we allow
+ resetHandlers(this, false);
+}
+
+// Set max bson size
+Connection.DEFAULT_MAX_BSON_SIZE = 1024 * 1024 * 4;
+
+// Inherit event emitter so we can emit stuff wohoo
+inherits(Connection, EventEmitter);
+
+Connection.prototype.start = function() {
+ // If we have a normal connection
+ if(this.socketOptions.ssl) {
+ // Create a new stream
+ this.connection = new net.Socket();
+ // Set timeout allowing backward compatibility to timeout if no connectTimeoutMS is set
+ this.connection.setTimeout(this.socketOptions.connectTimeoutMS != null ? this.socketOptions.connectTimeoutMS : this.socketOptions.timeout);
+ // Work around for 0.4.X
+ if(process.version.indexOf("v0.4") == -1) this.connection.setNoDelay(this.socketOptions.noDelay);
+ // Set keep alive if defined
+ if(process.version.indexOf("v0.4") == -1) {
+ if(this.socketOptions.keepAlive > 0) {
+ this.connection.setKeepAlive(true, this.socketOptions.keepAlive);
+ } else {
+ this.connection.setKeepAlive(false);
+ }
+ }
+
+ // Set up pair for tls with server, accept self-signed certificates as well
+ var pair = this.pair = tls.createSecurePair(false);
+ // Set up encrypted streams
+ this.pair.encrypted.pipe(this.connection);
+ this.connection.pipe(this.pair.encrypted);
+
+ // Setup clearText stream
+ this.writeSteam = this.pair.cleartext;
+ // Add all handlers to the socket to manage it
+ this.pair.on("secure", connectHandler(this));
+ this.pair.cleartext.on("data", createDataHandler(this));
+ // Add handlers
+ this.connection.on("error", errorHandler(this));
+ // this.connection.on("connect", connectHandler(this));
+ this.connection.on("end", endHandler(this));
+ this.connection.on("timeout", timeoutHandler(this));
+ this.connection.on("drain", drainHandler(this));
+ this.writeSteam.on("close", closeHandler(this));
+ // Start socket
+ this.connection.connect(this.socketOptions.port, this.socketOptions.host);
+ if(this.logger != null && this.logger.doDebug){
+ this.logger.debug("opened connection", this.socketOptions);
+ }
+ } else {
+ // Create new connection instance
+ if(this.domainSocket) {
+ this.connection = net.createConnection(this.socketOptions.host);
+ } else {
+ this.connection = net.createConnection(this.socketOptions.port, this.socketOptions.host);
+ }
+ if(this.logger != null && this.logger.doDebug){
+ this.logger.debug("opened connection", this.socketOptions);
+ }
+ // Set options on the socket
+ this.connection.setTimeout(this.socketOptions.connectTimeoutMS != null ? this.socketOptions.connectTimeoutMS : this.socketOptions.timeout);
+ // Work around for 0.4.X
+ if(process.version.indexOf("v0.4") == -1) this.connection.setNoDelay(this.socketOptions.noDelay);
+ // Set keep alive if defined
+ if(process.version.indexOf("v0.4") == -1) {
+ if(this.socketOptions.keepAlive > 0) {
+ this.connection.setKeepAlive(true, this.socketOptions.keepAlive);
+ } else {
+ this.connection.setKeepAlive(false);
+ }
+ }
+
+ // Set up write stream
+ this.writeSteam = this.connection;
+ // Add handlers
+ this.connection.on("error", errorHandler(this));
+ // Add all handlers to the socket to manage it
+ this.connection.on("connect", connectHandler(this));
+ // this.connection.on("end", endHandler(this));
+ this.connection.on("data", createDataHandler(this));
+ this.connection.on("timeout", timeoutHandler(this));
+ this.connection.on("drain", drainHandler(this));
+ this.connection.on("close", closeHandler(this));
+ }
+}
+
+// Check if the sockets are live
+Connection.prototype.isConnected = function() {
+ return this.connected && !this.connection.destroyed && this.connection.writable && this.connection.readable;
+}
+
+// Write the data out to the socket
+Connection.prototype.write = function(command, callback) {
+ try {
+ // If we have a list off commands to be executed on the same socket
+ if(Array.isArray(command)) {
+ for(var i = 0; i < command.length; i++) {
+ var binaryCommand = command[i].toBinary()
+ if(!this.socketOptions['disableDriverBSONSizeCheck'] && binaryCommand.length > this.maxBsonSize)
+ return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
+ if(this.logger != null && this.logger.doDebug)
+ this.logger.debug("writing command to mongodb", {binary: binaryCommand, json: command[i]});
+
+ var r = this.writeSteam.write(binaryCommand);
+ }
+ } else {
+ var binaryCommand = command.toBinary()
+ if(!this.socketOptions['disableDriverBSONSizeCheck'] && binaryCommand.length > this.maxBsonSize)
+ return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
+
+ if(this.logger != null && this.logger.doDebug)
+ this.logger.debug("writing command to mongodb", {binary: binaryCommand, json: command});
+
+ var r = this.writeSteam.write(binaryCommand);
+ }
+ } catch (err) {
+ if(typeof callback === 'function') callback(err);
+ }
+}
+
+// Force the closure of the connection
+Connection.prototype.close = function() {
+ // clear out all the listeners
+ resetHandlers(this, true);
+ // Add a dummy error listener to catch any weird last moment errors (and ignore them)
+ this.connection.on("error", function() {})
+ // destroy connection
+ this.connection.destroy();
+ if(this.logger != null && this.logger.doDebug){
+ this.logger.debug("closed connection", this.connection);
+ }
+}
+
+// Reset all handlers
+var resetHandlers = function(self, clearListeners) {
+ self.eventHandlers = {error:[], connect:[], close:[], end:[], timeout:[], parseError:[], message:[]};
+
+ // If we want to clear all the listeners
+ if(clearListeners && self.connection != null) {
+ var keys = Object.keys(self.eventHandlers);
+ // Remove all listeners
+ for(var i = 0; i < keys.length; i++) {
+ self.connection.removeAllListeners(keys[i]);
+ }
+ }
+}
+
+//
+// Handlers
+//
+
+// Connect handler
+var connectHandler = function(self) {
+ return function() {
+ // Set connected
+ self.connected = true;
+ // Now that we are connected set the socket timeout
+ self.connection.setTimeout(self.socketOptions.socketTimeoutMS != null ? self.socketOptions.socketTimeoutMS : self.socketOptions.timeout);
+ // Emit the connect event with no error
+ self.emit("connect", null, self);
+ }
+}
+
+var createDataHandler = exports.Connection.createDataHandler = function(self) {
+ // We need to handle the parsing of the data
+ // and emit the messages when there is a complete one
+ return function(data) {
+ // Parse until we are done with the data
+ while(data.length > 0) {
+ // If we still have bytes to read on the current message
+ if(self.bytesRead > 0 && self.sizeOfMessage > 0) {
+ // Calculate the amount of remaining bytes
+ var remainingBytesToRead = self.sizeOfMessage - self.bytesRead;
+ // Check if the current chunk contains the rest of the message
+ if(remainingBytesToRead > data.length) {
+ // Copy the new data into the exiting buffer (should have been allocated when we know the message size)
+ data.copy(self.buffer, self.bytesRead);
+ // Adjust the number of bytes read so it point to the correct index in the buffer
+ self.bytesRead = self.bytesRead + data.length;
+
+ // Reset state of buffer
+ data = new Buffer(0);
+ } else {
+ // Copy the missing part of the data into our current buffer
+ data.copy(self.buffer, self.bytesRead, 0, remainingBytesToRead);
+ // Slice the overflow into a new buffer that we will then re-parse
+ data = data.slice(remainingBytesToRead);
+
+ // Emit current complete message
+ try {
+ var emitBuffer = self.buffer;
+ // Reset state of buffer
+ self.buffer = null;
+ self.sizeOfMessage = 0;
+ self.bytesRead = 0;
+ self.stubBuffer = null;
+ // Emit the buffer
+ self.emit("message", emitBuffer, self);
+ } catch(err) {
+ var errorObject = {err:"socketHandler", trace:err, bin:buffer, parseState:{
+ sizeOfMessage:self.sizeOfMessage,
+ bytesRead:self.bytesRead,
+ stubBuffer:self.stubBuffer}};
+ if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
+ // We got a parse Error fire it off then keep going
+ self.emit("parseError", errorObject, self);
+ }
+ }
+ } else {
+ // Stub buffer is kept in case we don't get enough bytes to determine the
+ // size of the message (< 4 bytes)
+ if(self.stubBuffer != null && self.stubBuffer.length > 0) {
+
+ // If we have enough bytes to determine the message size let's do it
+ if(self.stubBuffer.length + data.length > 4) {
+ // Prepad the data
+ var newData = new Buffer(self.stubBuffer.length + data.length);
+ self.stubBuffer.copy(newData, 0);
+ data.copy(newData, self.stubBuffer.length);
+ // Reassign for parsing
+ data = newData;
+
+ // Reset state of buffer
+ self.buffer = null;
+ self.sizeOfMessage = 0;
+ self.bytesRead = 0;
+ self.stubBuffer = null;
+
+ } else {
+
+ // Add the the bytes to the stub buffer
+ var newStubBuffer = new Buffer(self.stubBuffer.length + data.length);
+ // Copy existing stub buffer
+ self.stubBuffer.copy(newStubBuffer, 0);
+ // Copy missing part of the data
+ data.copy(newStubBuffer, self.stubBuffer.length);
+ // Exit parsing loop
+ data = new Buffer(0);
+ }
+ } else {
+ if(data.length > 4) {
+ // Retrieve the message size
+ var sizeOfMessage = binaryutils.decodeUInt32(data, 0);
+ // If we have a negative sizeOfMessage emit error and return
+ if(sizeOfMessage < 0 || sizeOfMessage > self.maxBsonSize) {
+ var errorObject = {err:"socketHandler", trace:'', bin:self.buffer, parseState:{
+ sizeOfMessage: sizeOfMessage,
+ bytesRead: self.bytesRead,
+ stubBuffer: self.stubBuffer}};
+ if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
+ // We got a parse Error fire it off then keep going
+ self.emit("parseError", errorObject, self);
+ return;
+ }
+
+ // Ensure that the size of message is larger than 0 and less than the max allowed
+ if(sizeOfMessage > 4 && sizeOfMessage < self.maxBsonSize && sizeOfMessage > data.length) {
+ self.buffer = new Buffer(sizeOfMessage);
+ // Copy all the data into the buffer
+ data.copy(self.buffer, 0);
+ // Update bytes read
+ self.bytesRead = data.length;
+ // Update sizeOfMessage
+ self.sizeOfMessage = sizeOfMessage;
+ // Ensure stub buffer is null
+ self.stubBuffer = null;
+ // Exit parsing loop
+ data = new Buffer(0);
+
+ } else if(sizeOfMessage > 4 && sizeOfMessage < self.maxBsonSize && sizeOfMessage == data.length) {
+ try {
+ var emitBuffer = data;
+ // Reset state of buffer
+ self.buffer = null;
+ self.sizeOfMessage = 0;
+ self.bytesRead = 0;
+ self.stubBuffer = null;
+ // Exit parsing loop
+ data = new Buffer(0);
+ // Emit the message
+ self.emit("message", emitBuffer, self);
+ } catch (err) {
+ var errorObject = {err:"socketHandler", trace:err, bin:self.buffer, parseState:{
+ sizeOfMessage:self.sizeOfMessage,
+ bytesRead:self.bytesRead,
+ stubBuffer:self.stubBuffer}};
+ if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
+ // We got a parse Error fire it off then keep going
+ self.emit("parseError", errorObject, self);
+ }
+ } else if(sizeOfMessage <= 4 || sizeOfMessage > self.maxBsonSize) {
+ var errorObject = {err:"socketHandler", trace:null, bin:data, parseState:{
+ sizeOfMessage:sizeOfMessage,
+ bytesRead:0,
+ buffer:null,
+ stubBuffer:null}};
+ if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
+ // We got a parse Error fire it off then keep going
+ self.emit("parseError", errorObject, self);
+
+ // Clear out the state of the parser
+ self.buffer = null;
+ self.sizeOfMessage = 0;
+ self.bytesRead = 0;
+ self.stubBuffer = null;
+ // Exit parsing loop
+ data = new Buffer(0);
+
+ } else {
+ try {
+ var emitBuffer = data.slice(0, sizeOfMessage);
+ // Reset state of buffer
+ self.buffer = null;
+ self.sizeOfMessage = 0;
+ self.bytesRead = 0;
+ self.stubBuffer = null;
+ // Copy rest of message
+ data = data.slice(sizeOfMessage);
+ // Emit the message
+ self.emit("message", emitBuffer, self);
+ } catch (err) {
+ var errorObject = {err:"socketHandler", trace:err, bin:self.buffer, parseState:{
+ sizeOfMessage:sizeOfMessage,
+ bytesRead:self.bytesRead,
+ stubBuffer:self.stubBuffer}};
+ if(self.logger != null && self.logger.doError) self.logger.error("parseError", errorObject);
+ // We got a parse Error fire it off then keep going
+ self.emit("parseError", errorObject, self);
+ }
+
+ }
+ } else {
+ // Create a buffer that contains the space for the non-complete message
+ self.stubBuffer = new Buffer(data.length)
+ // Copy the data to the stub buffer
+ data.copy(self.stubBuffer, 0);
+ // Exit parsing loop
+ data = new Buffer(0);
+ }
+ }
+ }
+ }
+ }
+}
+
+var endHandler = function(self) {
+ return function() {
+ // Set connected to false
+ self.connected = false;
+ // Emit end event
+ self.emit("end", {err: 'connection received Fin packet from [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self);
+ }
+}
+
+var timeoutHandler = function(self) {
+ return function() {
+ self.emit("timeout", {err: 'connection to [' + self.socketOptions.host + ':' + self.socketOptions.port + '] timed out'}, self);
+ }
+}
+
+var drainHandler = function(self) {
+ return function() {
+ }
+}
+
+var errorHandler = function(self) {
+ return function(err) {
+ // Set connected to false
+ self.connected = false;
+ // Emit error
+ self.emit("error", {err: 'failed to connect to [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self);
+ }
+}
+
+var closeHandler = function(self) {
+ return function(hadError) {
+ // If we have an error during the connection phase
+ if(hadError && !self.connected) {
+ // Set disconnected
+ self.connected = false;
+ // Emit error
+ self.emit("error", {err: 'failed to connect to [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self);
+ } else {
+ // Set disconnected
+ self.connected = false;
+ // Emit close
+ self.emit("close", {err: 'connection closed to [' + self.socketOptions.host + ':' + self.socketOptions.port + ']'}, self);
+ }
+ }
+}
+
+// Some basic defaults
+Connection.DEFAULT_PORT = 27017;
+
+
+
+
+
+
+
diff --git a/node_modules/mongodb/lib/mongodb/connection/connection_pool.js b/node_modules/mongodb/lib/mongodb/connection/connection_pool.js
new file mode 100644
index 0000000..518158b
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/connection_pool.js
@@ -0,0 +1,251 @@
+var utils = require('./connection_utils'),
+ inherits = require('util').inherits,
+ net = require('net'),
+ EventEmitter = require('events').EventEmitter,
+ inherits = require('util').inherits,
+ MongoReply = require("../responses/mongo_reply").MongoReply,
+ Connection = require("./connection").Connection;
+
+var ConnectionPool = exports.ConnectionPool = function(host, port, poolSize, bson, socketOptions) {
+ if(typeof host !== 'string') {
+ throw new Error("host must be specified [" + host + "]");
+ }
+
+ // Set up event emitter
+ EventEmitter.call(this);
+
+ // Keep all options for the socket in a specific collection allowing the user to specify the
+ // Wished upon socket connection parameters
+ this.socketOptions = typeof socketOptions === 'object' ? socketOptions : {};
+ this.socketOptions.host = host;
+ this.socketOptions.port = port;
+ this.socketOptions.domainSocket = false;
+ this.bson = bson;
+ // PoolSize is always + 1 for special reserved "measurment" socket (like ping, stats etc)
+ this.poolSize = poolSize;
+ this.minPoolSize = Math.floor(this.poolSize / 2) + 1;
+
+ // Check if the host is a socket
+ if(host.match(/^\//)) {
+ this.socketOptions.domainSocket = true;
+ } else if(typeof port !== 'number') {
+ throw new Error("port must be specified [" + port + "]");
+ }
+
+ // Set default settings for the socket options
+ utils.setIntegerParameter(this.socketOptions, 'timeout', 0);
+ // Delay before writing out the data to the server
+ utils.setBooleanParameter(this.socketOptions, 'noDelay', true);
+ // Delay before writing out the data to the server
+ utils.setIntegerParameter(this.socketOptions, 'keepAlive', 0);
+ // Set the encoding of the data read, default is binary == null
+ utils.setStringParameter(this.socketOptions, 'encoding', null);
+ // Allows you to set a throttling bufferSize if you need to stop overflows
+ utils.setIntegerParameter(this.socketOptions, 'bufferSize', 0);
+
+ // Internal structures
+ this.openConnections = [];
+ // Assign connection id's
+ this.connectionId = 0;
+
+ // Current index for selection of pool connection
+ this.currentConnectionIndex = 0;
+ // The pool state
+ this._poolState = 'disconnected';
+ // timeout control
+ this._timeout = false;
+}
+
+inherits(ConnectionPool, EventEmitter);
+
+ConnectionPool.prototype.setMaxBsonSize = function(maxBsonSize) {
+ if(maxBsonSize == null){
+ maxBsonSize = Connection.DEFAULT_MAX_BSON_SIZE;
+ }
+
+ for(var i = 0; i < this.openConnections.length; i++) {
+ this.openConnections[i].maxBsonSize = maxBsonSize;
+ }
+}
+
+// Start a function
+var _connect = function(_self) {
+ return new function() {
+ // Create a new connection instance
+ var connection = new Connection(_self.connectionId++, _self.socketOptions);
+ // Set logger on pool
+ connection.logger = _self.logger;
+ // Connect handler
+ connection.on("connect", function(err, connection) {
+ // Add connection to list of open connections
+ _self.openConnections.push(connection);
+ // If the number of open connections is equal to the poolSize signal ready pool
+ if(_self.openConnections.length === _self.poolSize && _self._poolState !== 'disconnected') {
+ // Set connected
+ _self._poolState = 'connected';
+ // Emit pool ready
+ _self.emit("poolReady");
+ } else if(_self.openConnections.length < _self.poolSize) {
+ // We need to open another connection, make sure it's in the next
+ // tick so we don't get a cascade of errors
+ process.nextTick(function() {
+ _connect(_self);
+ });
+ }
+ });
+
+ var numberOfErrors = 0
+
+ // Error handler
+ connection.on("error", function(err, connection) {
+ numberOfErrors++;
+ // If we are already disconnected ignore the event
+ if(_self._poolState != 'disconnected' && _self.listeners("error").length > 0) {
+ _self.emit("error", err);
+ }
+
+ // Set disconnected
+ _self._poolState = 'disconnected';
+ // Stop
+ _self.stop();
+ });
+
+ // Close handler
+ connection.on("close", function() {
+ // If we are already disconnected ignore the event
+ if(_self._poolState !== 'disconnected' && _self.listeners("close").length > 0) {
+ _self.emit("close");
+ }
+ // Set disconnected
+ _self._poolState = 'disconnected';
+ // Stop
+ _self.stop();
+ });
+
+ // Timeout handler
+ connection.on("timeout", function(err, connection) {
+ // If we are already disconnected ignore the event
+ if(_self._poolState !== 'disconnected' && _self.listeners("timeout").length > 0) {
+ _self.emit("timeout", err);
+ }
+ // Set disconnected
+ _self._poolState = 'disconnected';
+ // Stop
+ _self.stop();
+ });
+
+ // Parse error, needs a complete shutdown of the pool
+ connection.on("parseError", function() {
+ // If we are already disconnected ignore the event
+ if(_self._poolState !== 'disconnected' && _self.listeners("parseError").length > 0) {
+ _self.emit("parseError", new Error("parseError occured"));
+ }
+
+ _self.stop();
+ });
+
+ connection.on("message", function(message) {
+ _self.emit("message", message);
+ });
+
+ // Start connection in the next tick
+ connection.start();
+ }();
+}
+
+
+// Start method, will throw error if no listeners are available
+// Pass in an instance of the listener that contains the api for
+// finding callbacks for a given message etc.
+ConnectionPool.prototype.start = function() {
+ var markerDate = new Date().getTime();
+ var self = this;
+
+ if(this.listeners("poolReady").length == 0) {
+ throw "pool must have at least one listener ready that responds to the [poolReady] event";
+ }
+
+ // Set pool state to connecting
+ this._poolState = 'connecting';
+ this._timeout = false;
+
+ _connect(self);
+}
+
+// Restart a connection pool (on a close the pool might be in a wrong state)
+ConnectionPool.prototype.restart = function() {
+ // Close all connections
+ this.stop(false);
+ // Now restart the pool
+ this.start();
+}
+
+// Stop the connections in the pool
+ConnectionPool.prototype.stop = function(removeListeners) {
+ removeListeners = removeListeners == null ? true : removeListeners;
+ // Set disconnected
+ this._poolState = 'disconnected';
+
+ // Clear all listeners if specified
+ if(removeListeners) {
+ this.removeAllEventListeners();
+ }
+
+ // Close all connections
+ for(var i = 0; i < this.openConnections.length; i++) {
+ this.openConnections[i].close();
+ }
+
+ // Clean up
+ this.openConnections = [];
+}
+
+// Check the status of the connection
+ConnectionPool.prototype.isConnected = function() {
+ return this._poolState === 'connected';
+}
+
+// Checkout a connection from the pool for usage, or grab a specific pool instance
+ConnectionPool.prototype.checkoutConnection = function(id) {
+ var index = (this.currentConnectionIndex++ % (this.openConnections.length));
+ var connection = this.openConnections[index];
+ return connection;
+}
+
+ConnectionPool.prototype.getAllConnections = function() {
+ return this.openConnections;
+}
+
+// Remove all non-needed event listeners
+ConnectionPool.prototype.removeAllEventListeners = function() {
+ this.removeAllListeners("close");
+ this.removeAllListeners("error");
+ this.removeAllListeners("timeout");
+ this.removeAllListeners("connect");
+ this.removeAllListeners("end");
+ this.removeAllListeners("parseError");
+ this.removeAllListeners("message");
+ this.removeAllListeners("poolReady");
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/node_modules/mongodb/lib/mongodb/connection/connection_utils.js b/node_modules/mongodb/lib/mongodb/connection/connection_utils.js
new file mode 100644
index 0000000..5910924
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/connection_utils.js
@@ -0,0 +1,23 @@
+exports.setIntegerParameter = function(object, field, defaultValue) {
+ if(object[field] == null) {
+ object[field] = defaultValue;
+ } else if(typeof object[field] !== "number" && object[field] !== parseInt(object[field], 10)) {
+ throw "object field [" + field + "] must be a numeric integer value, attempted to set to [" + object[field] + "] type of [" + typeof object[field] + "]";
+ }
+}
+
+exports.setBooleanParameter = function(object, field, defaultValue) {
+ if(object[field] == null) {
+ object[field] = defaultValue;
+ } else if(typeof object[field] !== "boolean") {
+ throw "object field [" + field + "] must be a boolean value, attempted to set to [" + object[field] + "] type of [" + typeof object[field] + "]";
+ }
+}
+
+exports.setStringParameter = function(object, field, defaultValue) {
+ if(object[field] == null) {
+ object[field] = defaultValue;
+ } else if(typeof object[field] !== "string") {
+ throw "object field [" + field + "] must be a string value, attempted to set to [" + object[field] + "] type of [" + typeof object[field] + "]";
+ }
+}
\ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/connection/mongos.js b/node_modules/mongodb/lib/mongodb/connection/mongos.js
new file mode 100644
index 0000000..3ba7c87
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/mongos.js
@@ -0,0 +1,333 @@
+var ReadPreference = require('./read_preference').ReadPreference
+ , Base = require('./base').Base
+ , inherits = require('util').inherits;
+
+/**
+ * Mongos constructor provides a connection to a mongos proxy including failover to additional servers
+ *
+ * Options
+ * - **socketOptions** {Object, default:null}, an object containing socket options to use (noDelay:(boolean), keepAlive:(number), connectTimeoutMS:(number), socketTimeoutMS:(number))
+ * - **ha** {Boolean, default:true}, turn on high availability, attempts to reconnect to down proxies
+ * - **haInterval** {Number, default:2000}, time between each replicaset status check.
+ *
+ * @class Represents a Mongos connection with failover to backup proxies
+ * @param {Array} list of mongos server objects
+ * @param {Object} [options] additional options for the mongos connection
+ */
+var Mongos = function Mongos(servers, options) {
+ // Set up basic
+ if(!(this instanceof Mongos))
+ return new Mongos(servers, options);
+
+ // Set up event emitter
+ Base.call(this);
+
+ // Throw error on wrong setup
+ if(servers == null || !Array.isArray(servers) || servers.length == 0)
+ throw new Error("At least one mongos proxy must be in the array");
+
+ // Ensure we have at least an empty options object
+ this.options = options == null ? {} : options;
+ // Set default connection pool options
+ this.socketOptions = this.options.socketOptions != null ? this.options.socketOptions : {};
+ // Enabled ha
+ this.haEnabled = this.options['ha'] == null ? true : this.options['ha'];
+ // How often are we checking for new servers in the replicaset
+ this.mongosStatusCheckInterval = this.options['haInterval'] == null ? 2000 : this.options['haInterval'];
+ // Save all the server connections
+ this.servers = servers;
+ // Servers we need to attempt reconnect with
+ this.downServers = [];
+ // Just contains the current lowest ping time and server
+ this.lowestPingTimeServer = null;
+ this.lowestPingTime = 0;
+
+ // Add options to servers
+ for(var i = 0; i < this.servers.length; i++) {
+ var server = this.servers[i];
+ // Default empty socket options object
+ var socketOptions = {host: server.host, port: server.port};
+ // If a socket option object exists clone it
+ if(this.socketOptions != null) {
+ var keys = Object.keys(this.socketOptions);
+ for(var k = 0; k < keys.length;k++) socketOptions[keys[i]] = this.socketOptions[keys[i]];
+ }
+ // Set socket options
+ server.socketOptions = socketOptions;
+ }
+}
+
+/**
+ * @ignore
+ */
+inherits(Mongos, Base);
+
+/**
+ * @ignore
+ */
+Mongos.prototype.isMongos = function() {
+ return true;
+}
+
+/**
+ * @ignore
+ */
+Mongos.prototype.connect = function(db, options, callback) {
+ if('function' === typeof options) callback = options, options = {};
+ if(options == null) options = {};
+ if(!('function' === typeof callback)) callback = null;
+ var self = this;
+
+ // Keep reference to parent
+ this.db = db;
+ // Set server state to connecting
+ this._serverState = 'connecting';
+ // Number of total servers that need to initialized (known servers)
+ this._numberOfServersLeftToInitialize = this.servers.length;
+ // Default to the first proxy server as the first one to use
+ this._currentMongos = this.servers[0];
+
+ // Connect handler
+ var connectHandler = function(_server) {
+ return function(err, result) {
+ self._numberOfServersLeftToInitialize = self._numberOfServersLeftToInitialize - 1;
+
+ if(self._numberOfServersLeftToInitialize == 0) {
+ // Start ha function if it exists
+ if(self.haEnabled) {
+ // Setup the ha process
+ self._replicasetTimeoutId = setTimeout(self.mongosCheckFunction, self.mongosStatusCheckInterval);
+ }
+
+ // Set the mongos to connected
+ self._serverState = "connected";
+ // Emit the open event
+ self.db.emit("open", null, self.db);
+ // Callback
+ callback(null, self.db);
+ }
+ }
+ };
+
+ // Error handler
+ var errorOrCloseHandler = function(_server) {
+ return function(err, result) {
+ // Create current mongos comparision
+ var currentUrl = self._currentMongos.host + ":" + self._currentMongos.port;
+ var serverUrl = this.host + ":" + this.port;
+ // We need to check if the server that closed is the actual current proxy we are using, otherwise
+ // just ignore
+ if(currentUrl == serverUrl) {
+ // Remove the server from the list
+ if(self.servers.indexOf(_server) != -1) {
+ self.servers.splice(self.servers.indexOf(_server), 1);
+ }
+
+ // Pick the next one on the list if there is one
+ for(var i = 0; i < self.servers.length; i++) {
+ // Grab the server out of the array (making sure there is no servers in the list if none available)
+ var server = self.servers[i];
+ // Generate url for comparision
+ var serverUrl = server.host + ":" + server.port;
+ // It's not the current one and connected set it as the current db
+ if(currentUrl != serverUrl && server.isConnected()) {
+ self._currentMongos = server;
+ break;
+ }
+ }
+ }
+
+ // Ensure we don't store the _server twice
+ if(self.downServers.indexOf(_server) == -1) {
+ // Add the server instances
+ self.downServers.push(_server);
+ }
+ }
+ }
+
+ // Mongo function
+ this.mongosCheckFunction = function() {
+ // If we have down servers let's attempt a reconnect
+ if(self.downServers.length > 0) {
+ var numberOfServersLeft = self.downServers.length;
+ // Attempt to reconnect
+ for(var i = 0; i < self.downServers.length; i++) {
+ var downServer = self.downServers.pop();
+ // Attemp to reconnect
+ downServer.connect(self.db, {returnIsMasterResults: true}, function(_server) {
+ // Return a function to check for the values
+ return function(err, result) {
+ // Adjust the number of servers left
+ numberOfServersLeft = numberOfServersLeft - 1;
+
+ if(err != null) {
+ self.downServers.push(_server);
+ } else {
+ // Add server event handlers
+ _server.on("close", errorOrCloseHandler(_server));
+ _server.on("error", errorOrCloseHandler(_server));
+ // Add to list of servers
+ self.servers.push(_server);
+ }
+
+ if(numberOfServersLeft <= 0) {
+ // Perfom another ha
+ self._replicasetTimeoutId = setTimeout(self.mongosCheckFunction, self.mongosStatusCheckInterval);
+ }
+ }
+ }(downServer));
+ }
+ } else if(self.servers.length > 0) {
+ var numberOfServersLeft = self.servers.length;
+ var _s = new Date().getTime()
+
+ // Else let's perform a ping command
+ for(var i = 0; i < self.servers.length; i++) {
+ var executePing = function(_server) {
+ // Get a read connection
+ var _connection = _server.checkoutReader();
+ // Execute ping command
+ self.db.command({ping:1}, {connection:_connection}, function(err, result) {
+ var pingTime = new Date().getTime() - _s;
+ // If no server set set the first one, otherwise check
+ // the lowest ping time and assign the server if it's got a lower ping time
+ if(self.lowestPingTimeServer == null) {
+ self.lowestPingTimeServer = _server;
+ self.lowestPingTime = pingTime;
+ self._currentMongos = _server;
+ } else if(self.lowestPingTime > pingTime
+ && (_server.host != self.lowestPingTimeServer.host || _server.port != self.lowestPingTimeServer.port)) {
+ self.lowestPingTimeServer = _server;
+ self.lowestPingTime = pingTime;
+ self._currentMongos = _server;
+ }
+
+ // Number of servers left
+ numberOfServersLeft = numberOfServersLeft - 1;
+ // All active mongos's pinged
+ if(numberOfServersLeft == 0) {
+ // Perfom another ha
+ self._replicasetTimeoutId = setTimeout(self.mongosCheckFunction, self.mongosStatusCheckInterval);
+ }
+ })
+ }
+ // Execute the function
+ executePing(self.servers[i]);
+ }
+ } else {
+ self._replicasetTimeoutId = setTimeout(self.mongosCheckFunction, self.mongosStatusCheckInterval);
+ }
+ }
+
+ // Connect all the server instances
+ for(var i = 0; i < this.servers.length; i++) {
+ // Get the connection
+ var server = this.servers[i];
+ server.mongosInstance = this;
+ // Add server event handlers
+ server.on("close", errorOrCloseHandler(server));
+ server.on("timeout", errorOrCloseHandler(server));
+ server.on("error", errorOrCloseHandler(server));
+ // Connect the instance
+ server.connect(self.db, {returnIsMasterResults: true}, connectHandler(server));
+ }
+}
+
+/**
+ * @ignore
+ * Just return the currently picked active connection
+ */
+Mongos.prototype.allServerInstances = function() {
+ return this.servers;
+}
+
+/**
+ * Always ourselves
+ * @ignore
+ */
+Mongos.prototype.setReadPreference = function() {}
+
+/**
+ * @ignore
+ */
+Mongos.prototype.allRawConnections = function() {
+ // Neeed to build a complete list of all raw connections, start with master server
+ var allConnections = [];
+ // Add all connections
+ for(var i = 0; i < this.servers.length; i++) {
+ allConnections = allConnections.concat(this.servers[i].allRawConnections());
+ }
+
+ // Return all the conections
+ return allConnections;
+}
+
+/**
+ * @ignore
+ */
+Mongos.prototype.isConnected = function() {
+ return this._serverState == "connected";
+}
+
+/**
+ * @ignore
+ */
+Mongos.prototype.checkoutWriter = function() {
+ // No current mongo, just pick first server
+ if(this._currentMongos == null && this.servers.length > 0) {
+ return this.servers[0].checkoutWriter();
+ }
+ return this._currentMongos.checkoutWriter();
+}
+
+/**
+ * @ignore
+ */
+Mongos.prototype.checkoutReader = function(read) {
+ // If we have a read preference object unpack it
+ if(typeof read == 'object' && read['_type'] == 'ReadPreference') {
+ // Validate if the object is using a valid mode
+ if(!read.isValid()) throw new Error("Illegal readPreference mode specified, " + read.mode);
+ } else if(!ReadPreference.isValid(read)) {
+ throw new Error("Illegal readPreference mode specified, " + read);
+ }
+
+ // No current mongo, just pick first server
+ if(this._currentMongos == null && this.servers.length > 0) {
+ return this.servers[0].checkoutReader();
+ }
+ return this._currentMongos.checkoutReader();
+}
+
+/**
+ * @ignore
+ */
+Mongos.prototype.close = function(callback) {
+ var self = this;
+ // Set server status as disconnected
+ this._serverState = 'disconnected';
+ // Number of connections to close
+ var numberOfConnectionsToClose = self.servers.length;
+ // If we have a ha process running kill it
+ if(self._replicasetTimeoutId != null) clearTimeout(self._replicasetTimeoutId);
+ // Close all proxy connections
+ for(var i = 0; i < self.servers.length; i++) {
+ self.servers[i].close(function(err, result) {
+ numberOfConnectionsToClose = numberOfConnectionsToClose - 1;
+ // Callback if we have one defined
+ if(numberOfConnectionsToClose == 0 && typeof callback == 'function') {
+ callback(null);
+ }
+ });
+ }
+}
+
+/**
+ * @ignore
+ * Return the used state
+ */
+Mongos.prototype._isUsed = function() {
+ return this._used;
+}
+
+exports.Mongos = Mongos;
\ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/connection/read_preference.js b/node_modules/mongodb/lib/mongodb/connection/read_preference.js
new file mode 100644
index 0000000..4cba587
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/read_preference.js
@@ -0,0 +1,66 @@
+/**
+ * A class representation of the Read Preference.
+ *
+ * Read Preferences
+ * - **ReadPreference.PRIMARY**, Read from primary only. All operations produce an error (throw an exception where applicable) if primary is unavailable. Cannot be combined with tags (This is the default.).
+ * - **ReadPreference.PRIMARY_PREFERRED**, Read from primary if available, otherwise a secondary.
+ * - **ReadPreference.SECONDARY**, Read from secondary if available, otherwise error.
+ * - **ReadPreference.SECONDARY_PREFERRED**, Read from a secondary if available, otherwise read from the primary.
+ * - **ReadPreference.NEAREST**, All modes read from among the nearest candidates, but unlike other modes, NEAREST will include both the primary and all secondaries in the random selection.
+ *
+ * @class Represents a Read Preference.
+ * @param {String} the read preference type
+ * @param {Object} tags
+ * @return {ReadPreference}
+ */
+var ReadPreference = function(mode, tags) {
+ if(!(this instanceof ReadPreference))
+ return new ReadPreference(mode, tags);
+ this._type = 'ReadPreference';
+ this.mode = mode;
+ this.tags = tags;
+}
+
+/**
+ * @ignore
+ */
+ReadPreference.isValid = function(_mode) {
+ return (_mode == ReadPreference.PRIMARY || _mode == ReadPreference.PRIMARY_PREFERRED
+ || _mode == ReadPreference.SECONDARY || _mode == ReadPreference.SECONDARY_PREFERRED
+ || _mode == ReadPreference.NEAREST);
+}
+
+/**
+ * @ignore
+ */
+ReadPreference.prototype.isValid = function(mode) {
+ var _mode = typeof mode == 'string' ? mode : this.mode;
+ return ReadPreference.isValid(_mode);
+}
+
+/**
+ * @ignore
+ */
+ReadPreference.prototype.toObject = function() {
+ var object = {mode:this.mode};
+
+ if(this.tags != null) {
+ object['tags'] = this.tags;
+ }
+
+ return object;
+}
+
+/**
+ * @ignore
+ */
+ReadPreference.PRIMARY = 'primary';
+ReadPreference.PRIMARY_PREFERRED = 'primaryPreferred';
+ReadPreference.SECONDARY = 'secondary';
+ReadPreference.SECONDARY_PREFERRED = 'secondaryPreferred';
+ReadPreference.NEAREST = 'nearest'
+
+/**
+ * @ignore
+ */
+exports.ReadPreference = ReadPreference;
\ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/connection/repl_set.js b/node_modules/mongodb/lib/mongodb/connection/repl_set.js
new file mode 100644
index 0000000..9a97775
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/repl_set.js
@@ -0,0 +1,1311 @@
+var Connection = require('./connection').Connection,
+ ReadPreference = require('./read_preference').ReadPreference,
+ DbCommand = require('../commands/db_command').DbCommand,
+ MongoReply = require('../responses/mongo_reply').MongoReply,
+ debug = require('util').debug,
+ inherits = require('util').inherits,
+ inspect = require('util').inspect,
+ Server = require('./server').Server,
+ PingStrategy = require('./strategies/ping_strategy').PingStrategy,
+ StatisticsStrategy = require('./strategies/statistics_strategy').StatisticsStrategy,
+ Base = require('./base').Base;
+
+const STATE_STARTING_PHASE_1 = 0;
+const STATE_PRIMARY = 1;
+const STATE_SECONDARY = 2;
+const STATE_RECOVERING = 3;
+const STATE_FATAL_ERROR = 4;
+const STATE_STARTING_PHASE_2 = 5;
+const STATE_UNKNOWN = 6;
+const STATE_ARBITER = 7;
+const STATE_DOWN = 8;
+const STATE_ROLLBACK = 9;
+
+/**
+ * ReplSet constructor provides replicaset functionality
+ *
+ * Options
+ * - **ha** {Boolean, default:true}, turn on high availability.
+ * - **haInterval** {Number, default:2000}, time between each replicaset status check.
+ * - **reconnectWait** {Number, default:1000}, time to wait in miliseconds before attempting reconnect.
+ * - **retries** {Number, default:30}, number of times to attempt a replicaset reconnect.
+ * - **rs_name** {String}, the name of the replicaset to connect to.
+ * - **socketOptions** {Object, default:null}, an object containing socket options to use (noDelay:(boolean), keepAlive:(number), connectTimeoutMS:(number), socketTimeoutMS:(number))
+ * - **readPreference** {String}, the prefered read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
+ * - **strategy** {String, default:null}, selection strategy for reads choose between (ping and statistical, default is round-robin)
+ * - **secondaryAcceptableLatencyMS** {Number, default:15}, sets the range of servers to pick when using NEAREST (lowest ping ms + the latency fence, ex: range of 1 to (1 + 15) ms)
+ * - **connectArbiter** {Boolean, default:false}, sets if the driver should connect to arbiters or not.
+ * - **logger** {Object, default:null}, an object representing a logger that you want to use, needs to support functions debug, log, error **({error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}})**.
+ *
+ * @class Represents a Replicaset Configuration
+ * @param {Array} list of server objects participating in the replicaset.
+ * @param {Object} [options] additional options for the replicaset connection.
+ */
+var ReplSet = exports.ReplSet = function(servers, options) {
+ this.count = 0;
+
+ // Set up basic
+ if(!(this instanceof ReplSet))
+ return new ReplSet(servers, options);
+
+ // Set up event emitter
+ Base.call(this);
+
+ // Ensure no Mongos's
+ for(var i = 0; i < servers.length; i++) {
+ if(!(servers[i] instanceof Server)) throw new Error("list of servers must be of type Server");
+ }
+
+ // Just reference for simplicity
+ var self = this;
+ // Contains the master server entry
+ this.options = options == null ? {} : options;
+ this.reconnectWait = this.options["reconnectWait"] != null ? this.options["reconnectWait"] : 1000;
+ this.retries = this.options["retries"] != null ? this.options["retries"] : 30;
+ this.replicaSet = this.options["rs_name"];
+
+ // Are we allowing reads from secondaries ?
+ this.readSecondary = this.options["read_secondary"];
+ this.slaveOk = true;
+ this.closedConnectionCount = 0;
+ this._used = false;
+
+ // Connect arbiters ?
+ this.connectArbiter = this.options.connectArbiter == null ? false : this.options.connectArbiter;
+
+ // Default poolSize for new server instances
+ this.poolSize = this.options.poolSize == null ? 5 : this.options.poolSize;
+ this._currentServerChoice = 0;
+
+ // Set up ssl connections
+ this.ssl = this.options.ssl == null ? false : this.options.ssl;
+
+ // Just keeps list of events we allow
+ this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[], timeout:[]};
+ // Internal state of server connection
+ this._serverState = 'disconnected';
+ // Read preference
+ this._readPreference = null;
+ // Number of initalized severs
+ this._numberOfServersLeftToInitialize = 0;
+ // Do we record server stats or not
+ this.recordQueryStats = false;
+ // Update health try server
+ this.updateHealthServerTry = 0;
+
+ // Get the readPreference
+ var readPreference = this.options['readPreference'];
+
+ // Validate correctness of Read preferences
+ if(readPreference != null) {
+ if(readPreference != ReadPreference.PRIMARY && readPreference != ReadPreference.PRIMARY_PREFERRED
+ && readPreference != ReadPreference.SECONDARY && readPreference != ReadPreference.SECONDARY_PREFERRED
+ && readPreference != ReadPreference.NEAREST && typeof readPreference != 'object' && readPreference['_type'] != 'ReadPreference') {
+ throw new Error("Illegal readPreference mode specified, " + readPreference);
+ }
+
+ this._readPreference = readPreference;
+ } else {
+ this._readPreference = null;
+ }
+
+ // Ensure read_secondary is set correctly
+ if(!this.readSecondary)
+ this.readSecondary = this._readPreference == ReadPreference.PRIMARY
+ || this._readPreference == false
+ || this._readPreference == null ? false : true;
+
+ // Strategy for picking a secondary
+ this.secondaryAcceptableLatencyMS = this.options['secondaryAcceptableLatencyMS'] == null ? 15 : this.options['secondaryAcceptableLatencyMS'];
+ this.strategy = this.options['strategy'];
+ // Make sure strategy is one of the two allowed
+ if(this.strategy != null && (this.strategy != 'ping' && this.strategy != 'statistical')) throw new Error("Only ping or statistical strategies allowed");
+ // Let's set up our strategy object for picking secodaries
+ if(this.strategy == 'ping') {
+ // Create a new instance
+ this.strategyInstance = new PingStrategy(this, this.secondaryAcceptableLatencyMS);
+ } else if(this.strategy == 'statistical') {
+ // Set strategy as statistical
+ this.strategyInstance = new StatisticsStrategy(this);
+ // Add enable query information
+ this.enableRecordQueryStats(true);
+ }
+
+ // Set default connection pool options
+ this.socketOptions = this.options.socketOptions != null ? this.options.socketOptions : {};
+
+ // Set up logger if any set
+ this.logger = this.options.logger != null
+ && (typeof this.options.logger.debug == 'function')
+ && (typeof this.options.logger.error == 'function')
+ && (typeof this.options.logger.debug == 'function')
+ ? this.options.logger : {error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}};
+
+ // Ensure all the instances are of type server and auto_reconnect is false
+ if(!Array.isArray(servers) || servers.length == 0) {
+ throw Error("The parameter must be an array of servers and contain at least one server");
+ } else if(Array.isArray(servers) || servers.length > 0) {
+ var count = 0;
+ servers.forEach(function(server) {
+ if(server instanceof Server) count = count + 1;
+ // Ensure no server has reconnect on
+ server.options.auto_reconnect = false;
+ });
+
+ if(count < servers.length) {
+ throw Error("All server entries must be of type Server");
+ } else {
+ this.servers = servers;
+ }
+ }
+
+ // var deduplicate list
+ var uniqueServers = {};
+ // De-duplicate any servers in the seed list
+ for(var i = 0; i < this.servers.length; i++) {
+ var server = this.servers[i];
+ // If server does not exist set it
+ if(uniqueServers[server.host + ":" + server.port] == null) {
+ uniqueServers[server.host + ":" + server.port] = server;
+ }
+ }
+
+ // Let's set the deduplicated list of servers
+ this.servers = [];
+ // Add the servers
+ for(var key in uniqueServers) {
+ this.servers.push(uniqueServers[key]);
+ }
+
+ // Enabled ha
+ this.haEnabled = this.options['ha'] == null ? true : this.options['ha'];
+ // How often are we checking for new servers in the replicaset
+ this.replicasetStatusCheckInterval = this.options['haInterval'] == null ? 1000 : this.options['haInterval'];
+ this._replicasetTimeoutId = null;
+
+ // Connection timeout
+ this._connectTimeoutMS = this.socketOptions.connectTimeoutMS
+ ? this.socketOptions.connectTimeoutMS
+ : 1000;
+
+ // Current list of servers to test
+ this.pingCandidateServers = [];
+
+ // Last replicaset check time
+ this.lastReplicaSetTime = new Date().getTime();
+};
+
+/**
+ * @ignore
+ */
+inherits(ReplSet, Base);
+
+/**
+ * @ignore
+ */
+// Allow setting the read preference at the replicaset level
+ReplSet.prototype.setReadPreference = function(preference) {
+ // Set read preference
+ this._readPreference = preference;
+ // Ensure slaveOk is correct for secodnaries read preference and tags
+ if((this._readPreference == ReadPreference.SECONDARY_PREFERRED || this._readPreference == ReadPreference.SECONDARY)
+ || (this._readPreference != null && typeof this._readPreference == 'object')) {
+ this.slaveOk = true;
+ }
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype._isUsed = function() {
+ return this._used;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.isMongos = function() {
+ return false;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.isConnected = function(read) {
+ // console.log("========================================= isConnected :: " + read)
+ if(read == null || read == ReadPreference.PRIMARY || read == false)
+ return this.primary != null && this._state.master != null && this._state.master.isConnected();
+
+ if((read == ReadPreference.PRIMARY_PREFERRED || read == ReadPreference.SECONDARY_PREFERRED || read == ReadPreference.NEAREST)
+ && ((this.primary != null && this._state.master != null && this._state.master.isConnected())
+ || (this._state && this._state.secondaries && Object.keys(this._state.secondaries).length > 0))) {
+ return true;
+ } else if(read == ReadPreference.SECONDARY) {
+ return this._state && this._state.secondaries && Object.keys(this._state.secondaries).length > 0;
+ }
+
+ // No valid connection return false
+ return false;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.isSetMember = function() {
+ return false;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.isPrimary = function(config) {
+ return this.readSecondary && Object.keys(this._state.secondaries).length > 0 ? false : true;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.isReadPrimary = ReplSet.prototype.isPrimary;
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.allServerInstances = function() {
+ var self = this;
+ // If no state yet return empty
+ if(!self._state) return [];
+ // Close all the servers (concatenate entire list of servers first for ease)
+ var allServers = self._state.master != null ? [self._state.master] : [];
+
+ // Secondary keys
+ var keys = Object.keys(self._state.secondaries);
+ // Add all secondaries
+ for(var i = 0; i < keys.length; i++) {
+ allServers.push(self._state.secondaries[keys[i]]);
+ }
+
+ // Arbiter keys
+ var keys = Object.keys(self._state.arbiters);
+ // Add all arbiters
+ for(var i = 0; i < keys.length; i++) {
+ allServers.push(self._state.arbiters[keys[i]]);
+ }
+
+ // Passive keys
+ var keys = Object.keys(self._state.passives);
+ // Add all arbiters
+ for(var i = 0; i < keys.length; i++) {
+ allServers.push(self._state.passives[keys[i]]);
+ }
+
+ // Return complete list of all servers
+ return allServers;
+}
+
+/**
+ * Enables high availability pings.
+ *
+ * @ignore
+ */
+ReplSet.prototype._enableHA = function () {
+ var self = this;
+ return check();
+
+ function ping () {
+ if("disconnected" == self._serverState) return;
+
+ if(Object.keys(self._state.addresses).length == 0) return;
+ var selectedServer = self._state.addresses[Object.keys(self._state.addresses)[self.updateHealthServerTry++]];
+ if(self.updateHealthServerTry >= Object.keys(self._state.addresses).length) self.updateHealthServerTry = 0;
+ if(selectedServer == null) return check();
+
+ // If we have an active db instance
+ if(self.dbInstances.length > 0) {
+ var db = self.dbInstances[0];
+
+ // Create a new master connection
+ var _server = new Server(selectedServer.host, selectedServer.port, {
+ auto_reconnect: false,
+ returnIsMasterResults: true,
+ slaveOk: true,
+ socketOptions: { connectTimeoutMS: 1000}
+ });
+
+ // Connect using the new _server connection to not impact the driver
+ // behavior on any errors we could possibly run into
+ _server.connect(db, function(err, result, _server) {
+ if(err) {
+ if(_server.close) _server.close();
+ return check();
+ }
+
+ // Create is master command
+ var cmd = DbCommand.createIsMasterCommand(db);
+ // Execute is master command
+ db._executeQueryCommand(cmd, {failFast:true, connection: _server.checkoutWriter()}, function(err, res) {
+ // Close the connection used
+ _server.close();
+ // If error let's set perform another check
+ if(err) return check();
+ // Validate the replicaset
+ self._validateReplicaset(res, db.auths, function() {
+ check();
+ });
+ });
+ });
+ }
+ }
+
+ function check () {
+ self._haTimer = setTimeout(ping, self.replicasetStatusCheckInterval);
+ }
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype._validateReplicaset = function(result, auths, cb) {
+ var self = this;
+ var res = result.documents[0];
+
+ // manage master node changes
+ if(res.primary && self._state.master && self._state.master.name != res.primary) {
+ // Delete master record so we can rediscover it
+ delete self._state.addresses[self._state.master.name];
+
+ // TODO existing issue? this seems to only work if
+ // we already have a connection to the new primary.
+
+ // Update information on new primary
+ // add as master, remove from secondary
+ var newMaster = self._state.addresses[res.primary];
+ newMaster.isMasterDoc.ismaster = true;
+ newMaster.isMasterDoc.secondary = false;
+ self._state.master = newMaster;
+ delete self._state.secondaries[res.primary];
+ }
+
+ // discover new hosts
+ var hosts = [];
+
+ for(var i = 0; i < res.hosts.length; ++i) {
+ var host = res.hosts[i];
+ if (host == res.me) continue;
+ if (!(self._state.addresses[host] || ~hosts.indexOf(host))) {
+ // we dont already have a connection to this host and aren't
+ // already planning on connecting.
+ hosts.push(host);
+ }
+ }
+
+ connectTo(hosts, auths, self, cb);
+}
+
+/**
+ * Create connections to all `hosts` firing `cb` after
+ * connections are attempted for all `hosts`.
+ *
+ * @param {Array} hosts
+ * @param {Array} [auths]
+ * @param {ReplSet} replset
+ * @param {Function} cb
+ * @ignore
+ */
+function connectTo (hosts, auths, replset, cb) {
+ var pending = hosts.length;
+ if (!pending) return cb();
+
+ for(var i = 0; i < hosts.length; ++i) {
+ connectToHost(hosts[i], auths, replset, handle);
+ }
+
+ function handle () {
+ --pending;
+ if (0 === pending) cb();
+ }
+}
+
+/**
+ * Attempts connection to `host` and authenticates with optional `auth`
+ * for the given `replset` firing `cb` when finished.
+ *
+ * @param {String} host
+ * @param {Array} auths
+ * @param {ReplSet} replset
+ * @param {Function} cb
+ * @ignore
+ */
+function connectToHost (host, auths, replset, cb) {
+ var server = createServer(host, replset);
+
+ var options = {
+ returnIsMasterResults: true,
+ eventReceiver: server
+ }
+
+ server.connect(replset.db, options, function(err, result) {
+ var doc = result && result.documents && result.documents[0];
+
+ if (err || !doc) {
+ server.close();
+ return cb(err, result, server);
+ }
+
+ if(!(doc.ismaster || doc.secondary || doc.arbiterOnly)) {
+ server.close();
+ return cb(null, result, server);
+ }
+
+ // if host is an arbiter, disconnect if not configured for it
+ if(doc.arbiterOnly && !replset.connectArbiter) {
+ server.close();
+ return cb(null, result, server);
+ }
+
+ // create handler for successful connections
+ var handleConnect = _connectHandler(replset, null, server);
+ function complete () {
+ handleConnect(err, result);
+ cb();
+ }
+
+ // authenticate if necessary
+ if(!(Array.isArray(auths) && auths.length > 0)) {
+ return complete();
+ }
+
+ var pending = auths.length;
+
+ var connections = server.allRawConnections();
+ var pendingAuthConn = connections.length;
+ for(var x = 0; x 0) {
+ self.db.emit(event, err);
+ }
+
+ // If it's the primary close all connections
+ if(self._state.master
+ && self._state.master.host == server.host
+ && self._state.master.port == server.port) {
+ // return self.close();
+ self._state.master = null;
+ }
+ }
+}
+
+var _connectHandler = function(self, candidateServers, instanceServer) {
+ return function(err, result) {
+ // We are disconnected stop attempting reconnect or connect
+ if(self._serverState == 'disconnected') return instanceServer.close();
+
+ // If no error handle isMaster
+ if(err == null && result.documents[0].hosts != null) {
+ // Fetch the isMaster command result
+ var document = result.documents[0];
+ // Break out the results
+ var setName = document.setName;
+ var isMaster = document.ismaster;
+ var secondary = document.secondary;
+ var passive = document.passive;
+ var arbiterOnly = document.arbiterOnly;
+ var hosts = Array.isArray(document.hosts) ? document.hosts : [];
+ var arbiters = Array.isArray(document.arbiters) ? document.arbiters : [];
+ var passives = Array.isArray(document.passives) ? document.passives : [];
+ var tags = document.tags ? document.tags : {};
+ var primary = document.primary;
+ // Find the current server name and fallback if none
+ var userProvidedServerString = instanceServer.host + ":" + instanceServer.port;
+ var me = document.me || userProvidedServerString;
+
+ // Verify if the set name is the same otherwise shut down and return an error
+ if(self.replicaSet == null) {
+ self.replicaSet = setName;
+ } else if(self.replicaSet != setName) {
+ // Stop the set
+ self.close();
+ // Emit a connection error
+ return self.emit("connectionError",
+ new Error("configured mongodb replicaset does not match provided replicaset [" + setName + "] != [" + self.replicaSet + "]"))
+ }
+
+ // Make sure we have the right reference
+ var oldServer = self._state.addresses[userProvidedServerString]
+ if (oldServer && oldServer !== instanceServer) oldServer.close();
+ delete self._state.addresses[userProvidedServerString];
+
+ if (self._state.addresses[me] && self._state.addresses[me] !== instanceServer) {
+ self._state.addresses[me].close();
+ }
+
+ self._state.addresses[me] = instanceServer;
+
+ // Let's add the server to our list of server types
+ if(secondary == true && (passive == false || passive == null)) {
+ self._state.secondaries[me] = instanceServer;
+ } else if(arbiterOnly == true) {
+ self._state.arbiters[me] = instanceServer;
+ } else if(secondary == true && passive == true) {
+ self._state.passives[me] = instanceServer;
+ } else if(isMaster == true) {
+ self._state.master = instanceServer;
+ } else if(isMaster == false && primary != null && self._state.addresses[primary]) {
+ self._state.master = self._state.addresses[primary];
+ }
+
+ // Set the name
+ instanceServer.name = me;
+ // Add tag info
+ instanceServer.tags = tags;
+
+ // Add the handlers to the instance
+ instanceServer.on("close", _handler("close", self));
+ instanceServer.on("error", _handler("error", self));
+ instanceServer.on("timeout", _handler("timeout", self));
+
+ // Possible hosts
+ var possibleHosts = Array.isArray(hosts) ? hosts.slice() : [];
+ possibleHosts = Array.isArray(passives) ? possibleHosts.concat(passives) : possibleHosts;
+
+ if(self.connectArbiter == true) {
+ possibleHosts = Array.isArray(arbiters) ? possibleHosts.concat(arbiters) : possibleHosts;
+ }
+
+ if(Array.isArray(candidateServers)) {
+ // Add any new candidate servers for connection
+ for(var j = 0; j < possibleHosts.length; j++) {
+ if(self._state.addresses[possibleHosts[j]] == null && possibleHosts[j] != null) {
+ var parts = possibleHosts[j].split(/:/);
+ if(parts.length == 1) {
+ parts = [parts[0], Connection.DEFAULT_PORT];
+ }
+
+ // New candidate server
+ var candidateServer = new Server(parts[0], parseInt(parts[1]));
+ candidateServer.name = possibleHosts[j];
+ self._state.addresses[possibleHosts[j]] = candidateServer;
+ // Add the new server to the list of candidate servers
+ candidateServers.push(candidateServer);
+ }
+ }
+ }
+ } else if(err != null || self._serverState == 'disconnected'){
+ delete self._state.addresses[instanceServer.host + ":" + instanceServer.port];
+ // Remove it from the set
+ instanceServer.close();
+ }
+
+ // Attempt to connect to the next server
+ if(Array.isArray(candidateServers) && candidateServers.length > 0) {
+ var server = candidateServers.pop();
+
+ // Get server addresses
+ var addresses = self._state.addresses;
+
+ // Default empty socket options object
+ var socketOptions = {};
+
+ // Set fast connect timeout
+ socketOptions['connectTimeoutMS'] = self._connectTimeoutMS;
+
+ // If a socket option object exists clone it
+ if(self.socketOptions != null && typeof self.socketOptions === 'object') {
+ var keys = Object.keys(self.socketOptions);
+ for(var j = 0; j < keys.length;j++) socketOptions[keys[j]] = self.socketOptions[keys[j]];
+ }
+
+ // If ssl is specified
+ if(self.ssl) server.ssl = true;
+
+ // Add host information to socket options
+ socketOptions['host'] = server.host;
+ socketOptions['port'] = server.port;
+ server.socketOptions = socketOptions;
+ server.replicasetInstance = self;
+ server.enableRecordQueryStats(self.recordQueryStats);
+
+ // Set the server
+ if (addresses[server.host + ":" + server.port] != server) {
+ if (addresses[server.host + ":" + server.port]) {
+ // Close the connection before deleting
+ addresses[server.host + ":" + server.port].close();
+ }
+ delete addresses[server.host + ":" + server.port];
+ }
+ addresses[server.host + ":" + server.port] = server;
+ // Connect
+ server.connect(self.db, {returnIsMasterResults: true, eventReceiver:server}, _connectHandler(self, candidateServers, server));
+ } else if(Array.isArray(candidateServers)) {
+ // If we have no primary emit error
+ if(self._state.master == null) {
+ // Stop the set
+ self.close();
+ // Emit a connection error
+ return self.emit("connectionError",
+ new Error("no primary server found in set"))
+ } else{
+ if (self.strategyInstance) {
+ self.strategyInstance.start();
+ }
+
+ self.emit("fullsetup", null, self.db, self);
+ self.emit("open", null, self.db, self);
+ }
+ }
+ }
+}
+
+/**
+ * Interval state object constructor
+ *
+ * @ignore
+ */
+ReplSet.State = function ReplSetState () {
+ this.errorMessages = [];
+ this.secondaries = {};
+ this.addresses = {};
+ this.arbiters = {};
+ this.passives = {};
+ this.members = [];
+ this.errors = {};
+ this.setName = null;
+ this.master = null;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.connect = function(parent, options, callback) {
+ var self = this;
+ if('function' === typeof options) callback = options, options = {};
+ if(options == null) options = {};
+ if(!('function' === typeof callback)) callback = null;
+
+ // Ensure it's all closed
+ self.close();
+
+ // Set connecting status
+ this.db = parent;
+ this._serverState = 'connecting';
+ this._callbackList = [];
+
+ this._state = new ReplSet.State();
+
+ // Ensure parent can do a slave query if it's set
+ parent.slaveOk = this.slaveOk
+ ? this.slaveOk
+ : parent.slaveOk;
+
+ // Remove any listeners
+ this.removeAllListeners("fullsetup");
+ this.removeAllListeners("connectionError");
+
+ // Add primary found event handler
+ this.once("fullsetup", function() {
+ self._handleOnFullSetup(parent);
+
+ // Callback
+ if(typeof callback == 'function') {
+ var internalCallback = callback;
+ callback = null;
+ internalCallback(null, parent, self);
+ }
+ });
+
+ this.once("connectionError", function(err) {
+ self._serverState = 'disconnected';
+ // Ensure it's all closed
+ self.close();
+ // Perform the callback
+ if(typeof callback == 'function') {
+ var internalCallback = callback;
+ callback = null;
+ internalCallback(err, parent, self);
+ }
+ });
+
+ // Get server addresses
+ var addresses = this._state.addresses;
+
+ // De-duplicate any servers
+ var server, key;
+ for(var i = 0; i < this.servers.length; i++) {
+ server = this.servers[i];
+ key = server.host + ":" + server.port;
+ if(null == addresses[key]) {
+ addresses[key] = server;
+ }
+ }
+
+ // Get the list of servers that is deduplicated and start connecting
+ var candidateServers = [];
+ var keys = Object.keys(addresses);
+ for(var i = 0; i < keys.length; i++) {
+ server = addresses[keys[i]];
+ server.assignReplicaSet(this);
+ candidateServers.push(server);
+ }
+
+ // Let's connect to the first one on the list
+ server = candidateServers.pop();
+ var opts = {
+ returnIsMasterResults: true,
+ eventReceiver: server
+ }
+ server.connect(parent, opts, _connectHandler(this, candidateServers, server));
+}
+
+/**
+ * Handles the first `fullsetup` event of this ReplSet.
+ *
+ * @param {Db} parent
+ * @ignore
+ */
+ReplSet.prototype._handleOnFullSetup = function (parent) {
+ this._serverState = 'connected';
+
+ // Emit the fullsetup and open event
+ parent.emit("open", null, this.db, this);
+ parent.emit("fullsetup", null, this.db, this);
+
+ if(!this.haEnabled) return;
+ this._enableHA();
+}
+
+/**
+ * Disables high availability pings.
+ *
+ * @ignore
+ */
+ReplSet.prototype._disableHA = function () {
+ clearTimeout(this._haTimer);
+ this._haTimer = undefined;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.checkoutWriter = function() {
+ // Establish connection
+ var connection = this._state.master != null ? this._state.master.checkoutWriter() : null;
+ // Return the connection
+ return connection;
+}
+
+/**
+ * @ignore
+ */
+var pickFirstConnectedSecondary = function pickFirstConnectedSecondary(self, tags) {
+ var keys = Object.keys(self._state.secondaries);
+ var connection;
+
+ // Find first available reader if any
+ for(var i = 0; i < keys.length; i++) {
+ connection = self._state.secondaries[keys[i]].checkoutReader();
+ if(connection) return connection;
+ }
+
+ // If we still have a null, read from primary if it's not secondary only
+ if(self._readPreference == ReadPreference.SECONDARY_PREFERRED) {
+ connection = self._state.master.checkoutReader();
+ if(connection) return connection;
+ }
+
+ var preferenceName = self._readPreference == ReadPreference.SECONDARY_PREFERRED
+ ? 'secondary'
+ : self._readPreference;
+
+ // console.log("================================================================ pickFirstConnectedSecondary :::: ")
+
+ return new Error("No replica set member available for query with ReadPreference "
+ + preferenceName + " and tags " + JSON.stringify(tags));
+}
+
+/**
+ * @ignore
+ */
+var _pickFromTags = function(self, tags) {
+ // If we have an array or single tag selection
+ var tagObjects = Array.isArray(tags) ? tags : [tags];
+ // Iterate over all tags until we find a candidate server
+ for(var _i = 0; _i < tagObjects.length; _i++) {
+ // Grab a tag object
+ var tagObject = tagObjects[_i];
+ // Matching keys
+ var matchingKeys = Object.keys(tagObject);
+ // Match all the servers that match the provdided tags
+ var keys = Object.keys(self._state.secondaries);
+ var candidateServers = [];
+
+ for(var i = 0; i < keys.length; i++) {
+ var server = self._state.secondaries[keys[i]];
+ // If we have tags match
+ if(server.tags != null) {
+ var matching = true;
+ // Ensure we have all the values
+ for(var j = 0; j < matchingKeys.length; j++) {
+ if(server.tags[matchingKeys[j]] != tagObject[matchingKeys[j]]) {
+ matching = false;
+ break;
+ }
+ }
+
+ // If we have a match add it to the list of matching servers
+ if(matching) {
+ candidateServers.push(server);
+ }
+ }
+ }
+
+ // If we have a candidate server return
+ if(candidateServers.length > 0) {
+ if(this.strategyInstance) return this.strategyInstance.checkoutSecondary(tags, candidateServers);
+ // Set instance to return
+ return candidateServers[Math.floor(Math.random() * candidateServers.length)].checkoutReader();
+ }
+ }
+
+ // No connection found
+ return null;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.checkoutReader = function(readPreference, tags) {
+ var connection = null;
+ // console.log("============================ checkoutReader")
+ // console.dir(readPreference)
+ // console.log(arguments.callee.caller.toString())
+
+ // If we have a read preference object unpack it
+ if(typeof readPreference == 'object' && readPreference['_type'] == 'ReadPreference') {
+ // Validate if the object is using a valid mode
+ if(!readPreference.isValid()) throw new Error("Illegal readPreference mode specified, " + readPreference.mode);
+ // Set the tag
+ tags = readPreference.tags;
+ readPreference = readPreference.mode;
+ } else if(typeof readPreference == 'object' && readPreference['_type'] != 'ReadPreference') {
+ throw new Error("read preferences must be either a string or an instance of ReadPreference");
+ }
+
+ // Set up our read Preference, allowing us to override the readPreference
+ var finalReadPreference = readPreference != null ? readPreference : this._readPreference;
+ finalReadPreference = finalReadPreference == true ? ReadPreference.SECONDARY_PREFERRED : finalReadPreference;
+ finalReadPreference = finalReadPreference == null ? ReadPreference.PRIMARY : finalReadPreference;
+ // finalReadPreference = 'primary';
+
+ // console.log("============================ finalReadPreference: " + finalReadPreference)
+ // console.dir(finalReadPreference)
+
+ // If we are reading from a primary
+ if(finalReadPreference == 'primary') {
+ // If we provide a tags set send an error
+ if(typeof tags == 'object' && tags != null) {
+ return new Error("PRIMARY cannot be combined with tags");
+ }
+
+ // If we provide a tags set send an error
+ if(this._state.master == null) {
+ return new Error("No replica set primary available for query with ReadPreference PRIMARY");
+ }
+
+ // Checkout a writer
+ return this.checkoutWriter();
+ }
+
+ // If we have specified to read from a secondary server grab a random one and read
+ // from it, otherwise just pass the primary connection
+ if((this.readSecondary || finalReadPreference == ReadPreference.SECONDARY_PREFERRED || finalReadPreference == ReadPreference.SECONDARY) && Object.keys(this._state.secondaries).length > 0) {
+ // If we have tags, look for servers matching the specific tag
+ if(tags != null && typeof tags == 'object') {
+ // Get connection
+ connection = _pickFromTags(this, tags);// = function(self, readPreference, tags) {
+ // No candidate servers that match the tags, error
+ if(connection == null) {
+ return new Error("No replica set members available for query");
+ }
+ } else {
+ connection = _roundRobin(this, tags);
+ }
+ } else if(finalReadPreference == ReadPreference.PRIMARY_PREFERRED) {
+ // Check if there is a primary available and return that if possible
+ connection = this.checkoutWriter();
+ // If no connection available checkout a secondary
+ if(connection == null) {
+ // If we have tags, look for servers matching the specific tag
+ if(tags != null && typeof tags == 'object') {
+ // Get connection
+ connection = _pickFromTags(this, tags);// = function(self, readPreference, tags) {
+ // No candidate servers that match the tags, error
+ if(connection == null) {
+ return new Error("No replica set members available for query");
+ }
+ } else {
+ connection = _roundRobin(this, tags);
+ }
+ }
+ } else if(finalReadPreference == ReadPreference.SECONDARY_PREFERRED && tags == null && Object.keys(this._state.secondaries).length == 0) {
+ // console.log("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ SECONDARY_PREFERRED")
+ connection = this.checkoutWriter();
+ // If no connection return an error
+ if(connection == null) {
+ var preferenceName = finalReadPreference == ReadPreference.SECONDARY ? 'secondary' : finalReadPreference;
+ connection = new Error("No replica set member available for query with ReadPreference " + preferenceName + " and tags " + JSON.stringify(tags));
+ }
+ } else if(finalReadPreference == ReadPreference.SECONDARY_PREFERRED) {
+ // If we have tags, look for servers matching the specific tag
+ if(tags != null && typeof tags == 'object') {
+ // Get connection
+ connection = _pickFromTags(this, tags);// = function(self, readPreference, tags) {
+ // No candidate servers that match the tags, error
+ if(connection == null) {
+ // No secondary server avilable, attemp to checkout a primary server
+ connection = this.checkoutWriter();
+ // If no connection return an error
+ if(connection == null) {
+ return new Error("No replica set members available for query");
+ }
+ }
+ } else if(this.strategyInstance != null) {
+ connection = this.strategyInstance.checkoutReader(tags);
+ }
+ } else if(finalReadPreference == ReadPreference.NEAREST && this.strategyInstance != null) {
+ connection = this.strategyInstance.checkoutSecondary(tags);
+ } else if(finalReadPreference == ReadPreference.NEAREST && this.strategyInstance == null) {
+ return new Error("A strategy for calculating nearness must be enabled such as ping or statistical");
+ } else if(finalReadPreference == ReadPreference.SECONDARY && Object.keys(this._state.secondaries).length == 0) {
+ // console.log("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ SECONDARY")
+ if(tags != null && typeof tags == 'object') {
+ var preferenceName = finalReadPreference == ReadPreference.SECONDARY ? 'secondary' : finalReadPreference;
+ connection = new Error("No replica set member available for query with ReadPreference " + preferenceName + " and tags " + JSON.stringify(tags));
+ } else {
+ connection = new Error("No replica set secondary available for query with ReadPreference SECONDARY");
+ }
+ } else {
+ connection = this.checkoutWriter();
+ }
+
+ // Return the connection
+ return connection;
+}
+
+/**
+ * Pick a secondary using round robin
+ *
+ * @ignore
+ */
+function _roundRobin (replset, tags) {
+ var keys = Object.keys(replset._state.secondaries);
+ var key = keys[replset._currentServerChoice++ % keys.length];
+
+ var conn = null != replset._state.secondaries[key]
+ ? replset._state.secondaries[key].checkoutReader()
+ : null;
+
+ // If connection is null fallback to first available secondary
+ if (null == conn) {
+ conn = pickFirstConnectedSecondary(replset, tags);
+ }
+
+ return conn;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.allRawConnections = function() {
+ // Neeed to build a complete list of all raw connections, start with master server
+ var allConnections = [];
+ if(this._state.master == null) return [];
+ // Get connection object
+ var allMasterConnections = this._state.master.connectionPool.getAllConnections();
+ // Add all connections to list
+ allConnections = allConnections.concat(allMasterConnections);
+ // If we have read secondary let's add all secondary servers
+ if(Object.keys(this._state.secondaries).length > 0) {
+ // Get all the keys
+ var keys = Object.keys(this._state.secondaries);
+ // For each of the secondaries grab the connections
+ for(var i = 0; i < keys.length; i++) {
+ // Get connection object
+ var secondaryPoolConnections = this._state.secondaries[keys[i]].connectionPool.getAllConnections();
+ // Add all connections to list
+ allConnections = allConnections.concat(secondaryPoolConnections);
+ }
+ }
+
+ // Return all the conections
+ return allConnections;
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.enableRecordQueryStats = function(enable) {
+ // Set the global enable record query stats
+ this.recordQueryStats = enable;
+ // Ensure all existing servers already have the flag set, even if the
+ // connections are up already or we have not connected yet
+ if(this._state != null && this._state.addresses != null) {
+ var keys = Object.keys(this._state.addresses);
+ // Iterate over all server instances and set the enableRecordQueryStats flag
+ for(var i = 0; i < keys.length; i++) {
+ this._state.addresses[keys[i]].enableRecordQueryStats(enable);
+ }
+ } else if(Array.isArray(this.servers)) {
+ for(var i = 0; i < this.servers.length; i++) {
+ this.servers[i].enableRecordQueryStats(enable);
+ }
+ }
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.disconnect = function(callback) {
+ this.close(callback);
+}
+
+/**
+ * @ignore
+ */
+ReplSet.prototype.close = function(callback) {
+ var self = this;
+ // Disconnect
+ this._serverState = 'disconnected';
+ // Close all servers
+ if(this._state && this._state.addresses) {
+ var keys = Object.keys(this._state.addresses);
+ // Iterate over all server instances
+ for(var i = 0; i < keys.length; i++) {
+ this._state.addresses[keys[i]].close();
+ }
+ }
+
+ // If we have a strategy stop it
+ if(this.strategyInstance) this.strategyInstance.stop();
+
+ // If it's a callback
+ if(typeof callback == 'function') callback(null, null);
+}
+
+/**
+ * Auto Reconnect property
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "autoReconnect", { enumerable: true
+ , get: function () {
+ return true;
+ }
+});
+
+/**
+ * Get Read Preference method
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "readPreference", { enumerable: true
+ , get: function () {
+ if(this._readPreference == null && this.readSecondary) {
+ return ReadPreference.SECONDARY_PREFERRED;
+ } else if(this._readPreference == null && !this.readSecondary) {
+ return ReadPreference.PRIMARY;
+ } else {
+ return this._readPreference;
+ }
+ }
+});
+
+/**
+ * Db Instances
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "dbInstances", {enumerable:true
+ , get: function() {
+ var servers = this.allServerInstances();
+ return servers.length > 0 ? servers[0].dbInstances : [];
+ }
+})
+
+/**
+ * Just make compatible with server.js
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "host", { enumerable: true
+ , get: function () {
+ if (this.primary != null) return this.primary.host;
+ }
+});
+
+/**
+ * Just make compatible with server.js
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "port", { enumerable: true
+ , get: function () {
+ if (this.primary != null) return this.primary.port;
+ }
+});
+
+/**
+ * Get status of read
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "read", { enumerable: true
+ , get: function () {
+ return this.secondaries.length > 0 ? this.secondaries[0] : null;
+ }
+});
+
+/**
+ * Get list of secondaries
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "secondaries", {enumerable: true
+ , get: function() {
+ var keys = Object.keys(this._state.secondaries);
+ var array = new Array(keys.length);
+ // Convert secondaries to array
+ for(var i = 0; i < keys.length; i++) {
+ array[i] = this._state.secondaries[keys[i]];
+ }
+ return array;
+ }
+});
+
+/**
+ * Get list of all secondaries including passives
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "allSecondaries", {enumerable: true
+ , get: function() {
+ return this.secondaries.concat(this.passives);
+ }
+});
+
+/**
+ * Get list of arbiters
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "arbiters", {enumerable: true
+ , get: function() {
+ var keys = Object.keys(this._state.arbiters);
+ var array = new Array(keys.length);
+ // Convert arbiters to array
+ for(var i = 0; i < keys.length; i++) {
+ array[i] = this._state.arbiters[keys[i]];
+ }
+ return array;
+ }
+});
+
+/**
+ * Get list of passives
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "passives", {enumerable: true
+ , get: function() {
+ var keys = Object.keys(this._state.passives);
+ var array = new Array(keys.length);
+ // Convert arbiters to array
+ for(var i = 0; i < keys.length; i++) {
+ array[i] = this._state.passives[keys[i]];
+ }
+ return array;
+ }
+});
+
+/**
+ * Master connection property
+ * @ignore
+ */
+Object.defineProperty(ReplSet.prototype, "primary", { enumerable: true
+ , get: function () {
+ return this._state != null ? this._state.master : null;
+ }
+});
+
+/**
+ * @ignore
+ */
+// Backward compatibility
+exports.ReplSetServers = ReplSet;
diff --git a/node_modules/mongodb/lib/mongodb/connection/server.js b/node_modules/mongodb/lib/mongodb/connection/server.js
new file mode 100644
index 0000000..34301f6
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/server.js
@@ -0,0 +1,860 @@
+var Connection = require('./connection').Connection,
+ ReadPreference = require('./read_preference').ReadPreference,
+ DbCommand = require('../commands/db_command').DbCommand,
+ MongoReply = require('../responses/mongo_reply').MongoReply,
+ ConnectionPool = require('./connection_pool').ConnectionPool,
+ EventEmitter = require('events').EventEmitter,
+ Base = require('./base').Base,
+ utils = require('../utils'),
+ inherits = require('util').inherits;
+
+/**
+ * Class representing a single MongoDB Server connection
+ *
+ * Options
+ * - **readPreference** {String, default:null}, set's the read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST)
+ * - **ssl** {Boolean, default:false}, use ssl connection (needs to have a mongod server with ssl support)
+ * - **slaveOk** {Boolean, default:false}, legacy option allowing reads from secondary, use **readPrefrence** instead.
+ * - **poolSize** {Number, default:5}, number of connections in the connection pool, set to 5 as default for legacy reasons.
+ * - **socketOptions** {Object, default:null}, an object containing socket options to use (noDelay:(boolean), keepAlive:(number), connectTimeoutMS:(number), socketTimeoutMS:(number))
+ * - **logger** {Object, default:null}, an object representing a logger that you want to use, needs to support functions debug, log, error **({error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}})**.
+ * - **auto_reconnect** {Boolean, default:false}, reconnect on error.
+ * - **disableDriverBSONSizeCheck** {Boolean, default:false}, force the server to error if the BSON message is to big
+ *
+ * @class Represents a Server connection.
+ * @param {String} host the server host
+ * @param {Number} port the server port
+ * @param {Object} [options] optional options for insert command
+ */
+function Server(host, port, options) {
+ // Set up Server instance
+ if(!(this instanceof Server)) return new Server(host, port, options);
+
+ // Set up event emitter
+ Base.call(this);
+
+ // Ensure correct values
+ if(port != null && typeof port == 'object') {
+ options = port;
+ port = Connection.DEFAULT_PORT;
+ }
+
+ var self = this;
+ this.host = host;
+ this.port = port;
+ this.options = options == null ? {} : options;
+ this.internalConnection;
+ this.internalMaster = false;
+ this.connected = false;
+ this.poolSize = this.options.poolSize == null ? 5 : this.options.poolSize;
+ this.disableDriverBSONSizeCheck = this.options.disableDriverBSONSizeCheck != null ? this.options.disableDriverBSONSizeCheck : false;
+ this.ssl = this.options.ssl == null ? false : this.options.ssl;
+ this.slaveOk = this.options["slave_ok"] ? this.options["slave_ok"] : this.options["slaveOk"];
+ this._used = false;
+ this.replicasetInstance = null;
+
+ // Get the readPreference
+ var readPreference = this.options['readPreference'];
+ // If readPreference is an object get the mode string
+ var validateReadPreference = readPreference != null && typeof readPreference == 'object' ? readPreference.mode : readPreference;
+ // Read preference setting
+ if(validateReadPreference != null) {
+ if(validateReadPreference != ReadPreference.PRIMARY && validateReadPreference != ReadPreference.SECONDARY && validateReadPreference != ReadPreference.NEAREST
+ && validateReadPreference != ReadPreference.SECONDARY_PREFERRED && validateReadPreference != ReadPreference.PRIMARY_PREFERRED) {
+ throw new Error("Illegal readPreference mode specified, " + validateReadPreference);
+ }
+
+ // Set read Preference
+ this._readPreference = readPreference;
+ } else {
+ this._readPreference = null;
+ }
+
+ // Contains the isMaster information returned from the server
+ this.isMasterDoc;
+
+ // Set default connection pool options
+ this.socketOptions = this.options.socketOptions != null ? this.options.socketOptions : {};
+ if(this.disableDriverBSONSizeCheck) this.socketOptions.disableDriverBSONSizeCheck = this.disableDriverBSONSizeCheck;
+ // Set ssl up if it's defined
+ if(this.ssl) {
+ this.socketOptions.ssl = true;
+ }
+
+ // Set up logger if any set
+ this.logger = this.options.logger != null
+ && (typeof this.options.logger.debug == 'function')
+ && (typeof this.options.logger.error == 'function')
+ && (typeof this.options.logger.log == 'function')
+ ? this.options.logger : {error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}};
+
+ // Just keeps list of events we allow
+ this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[], timeout:[]};
+ // Internal state of server connection
+ this._serverState = 'disconnected';
+ // this._timeout = false;
+ // Contains state information about server connection
+ this._state = {'runtimeStats': {'queryStats':new RunningStats()}};
+ // Do we record server stats or not
+ this.recordQueryStats = false;
+};
+
+/**
+ * @ignore
+ */
+inherits(Server, Base);
+
+//
+// Deprecated, USE ReadPreferences class
+//
+Server.READ_PRIMARY = ReadPreference.PRIMARY;
+Server.READ_SECONDARY = ReadPreference.SECONDARY_PREFERRED;
+Server.READ_SECONDARY_ONLY = ReadPreference.SECONDARY;
+
+/**
+ * Always ourselves
+ * @ignore
+ */
+Server.prototype.setReadPreference = function() {}
+
+/**
+ * @ignore
+ */
+Server.prototype.isMongos = function() {
+ return this.isMasterDoc != null && this.isMasterDoc['msg'] == "isdbgrid" ? true : false;
+}
+
+/**
+ * @ignore
+ */
+Server.prototype._isUsed = function() {
+ return this._used;
+}
+
+/**
+ * @ignore
+ */
+Server.prototype.close = function(callback) {
+ // Remove all local listeners
+ this.removeAllListeners();
+
+ if(this.connectionPool != null) {
+ // Remove all the listeners on the pool so it does not fire messages all over the place
+ this.connectionPool.removeAllEventListeners();
+ // Close the connection if it's open
+ this.connectionPool.stop(true);
+ }
+
+ // Set server status as disconnected
+ this._serverState = 'disconnected';
+ // Peform callback if present
+ if(typeof callback === 'function') callback();
+};
+
+/**
+ * @ignore
+ */
+Server.prototype.isConnected = function() {
+ return this._serverState == 'connected';
+}
+
+/**
+ * @ignore
+ */
+Server.prototype.allServerInstances = function() {
+ return [this];
+}
+
+/**
+ * @ignore
+ */
+Server.prototype.isSetMember = function() {
+ return this.replicasetInstance != null || this.mongosInstance != null;
+}
+
+/**
+ * Assigns a replica set to this `server`.
+ *
+ * @param {ReplSet} replset
+ * @ignore
+ */
+Server.prototype.assignReplicaSet = function (replset) {
+ this.replicasetInstance = replset;
+ this.inheritReplSetOptionsFrom(replset);
+ this.enableRecordQueryStats(replset.recordQueryStats);
+}
+
+/**
+ * Takes needed options from `replset` and overwrites
+ * our own options.
+ *
+ * @param {ReplSet} replset
+ * @ignore
+ */
+Server.prototype.inheritReplSetOptionsFrom = function (replset) {
+ this.socketOptions = {};
+ this.socketOptions.connectTimeoutMS = replset._connectTimeoutMS;
+
+ if(replset.ssl)
+ this.socketOptions.ssl = true;
+
+ // If a socket option object exists clone it
+ if(utils.isObject(replset.socketOptions)) {
+ var keys = Object.keys(replset.socketOptions);
+ for(var i = 0; i < keys.length; i++)
+ this.socketOptions[keys[i]] = replset.socketOptions[keys[i]];
+ }
+}
+
+/**
+ * Opens this server connection.
+ *
+ * @ignore
+ */
+Server.prototype.connect = function(dbInstance, options, callback) {
+ if('function' === typeof options) callback = options, options = {};
+ if(options == null) options = {};
+ if(!('function' === typeof callback)) callback = null;
+
+ // Currently needed to work around problems with multiple connections in a pool with ssl
+ // TODO fix if possible
+ if(this.ssl == true) {
+ // Set up socket options for ssl
+ this.socketOptions.ssl = true;
+ }
+
+ // Let's connect
+ var server = this;
+ // Let's us override the main receiver of events
+ var eventReceiver = options.eventReceiver != null ? options.eventReceiver : this;
+ // Creating dbInstance
+ this.dbInstance = dbInstance;
+ // Save reference to dbInstance
+ this.dbInstances = [dbInstance];
+
+ // Force connection pool if there is one
+ if(server.connectionPool) server.connectionPool.stop();
+
+ // Set server state to connecting
+ this._serverState = 'connecting';
+ // Ensure dbInstance can do a slave query if it's set
+ dbInstance.slaveOk = this.slaveOk ? this.slaveOk : dbInstance.slaveOk;
+ // Create connection Pool instance with the current BSON serializer
+ var connectionPool = new ConnectionPool(this.host, this.port, this.poolSize, dbInstance.bson, this.socketOptions);
+ // Set logger on pool
+ connectionPool.logger = this.logger;
+
+ // Set up a new pool using default settings
+ server.connectionPool = connectionPool;
+
+ // Set basic parameters passed in
+ var returnIsMasterResults = options.returnIsMasterResults == null ? false : options.returnIsMasterResults;
+
+ // Create a default connect handler, overriden when using replicasets
+ var connectCallback = function(err, reply) {
+ // ensure no callbacks get called twice
+ var internalCallback = callback;
+ callback = null;
+ // If something close down the connection and removed the callback before
+ // proxy killed connection etc, ignore the erorr as close event was isssued
+ if(err != null && internalCallback == null) return;
+ // Internal callback
+ if(err != null) return internalCallback(err, null);
+ server.master = reply.documents[0].ismaster == 1 ? true : false;
+ server.connectionPool.setMaxBsonSize(reply.documents[0].maxBsonObjectSize);
+ // Set server as connected
+ server.connected = true;
+ // Save document returned so we can query it
+ server.isMasterDoc = reply.documents[0];
+
+ // Emit open event
+ _emitAcrossAllDbInstances(server, eventReceiver, "open", null, returnIsMasterResults ? reply : dbInstance, null);
+
+ // If we have it set to returnIsMasterResults
+ if(returnIsMasterResults) {
+ internalCallback(null, reply, server);
+ } else {
+ internalCallback(null, dbInstance, server);
+ }
+ };
+
+ // Let's us override the main connect callback
+ var connectHandler = options.connectHandler == null ? connectCallback : options.connectHandler;
+
+ // Set up on connect method
+ connectionPool.on("poolReady", function() {
+ // Create db command and Add the callback to the list of callbacks by the request id (mapping outgoing messages to correct callbacks)
+ var db_command = DbCommand.NcreateIsMasterCommand(dbInstance, dbInstance.databaseName);
+ // Check out a reader from the pool
+ var connection = connectionPool.checkoutConnection();
+ // Set server state to connEcted
+ server._serverState = 'connected';
+
+ // Register handler for messages
+ dbInstance._registerHandler(db_command, false, connection, connectHandler);
+
+ // Write the command out
+ connection.write(db_command);
+ })
+
+ // Set up item connection
+ connectionPool.on("message", function(message) {
+ // Attempt to parse the message
+ try {
+ // Create a new mongo reply
+ var mongoReply = new MongoReply()
+ // Parse the header
+ mongoReply.parseHeader(message, connectionPool.bson)
+ // If message size is not the same as the buffer size
+ // something went terribly wrong somewhere
+ if(mongoReply.messageLength != message.length) {
+ // Emit the error
+ if(eventReceiver.listeners("error") && eventReceiver.listeners("error").length > 0) eventReceiver.emit("error", new Error("bson length is different from message length"), server);
+ // Remove all listeners
+ server.removeAllListeners();
+ } else {
+ var startDate = new Date().getTime();
+
+ // Callback instance
+ var callbackInfo = null;
+ var dbInstanceObject = null;
+
+ // Locate a callback instance and remove any additional ones
+ for(var i = 0; i < server.dbInstances.length; i++) {
+ var dbInstanceObjectTemp = server.dbInstances[i];
+ var hasHandler = dbInstanceObjectTemp._hasHandler(mongoReply.responseTo.toString());
+ // Assign the first one we find and remove any duplicate ones
+ if(hasHandler && callbackInfo == null) {
+ callbackInfo = dbInstanceObjectTemp._findHandler(mongoReply.responseTo.toString());
+ dbInstanceObject = dbInstanceObjectTemp;
+ } else if(hasHandler) {
+ dbInstanceObjectTemp._removeHandler(mongoReply.responseTo.toString());
+ }
+ }
+
+ // The command executed another request, log the handler again under that request id
+ if(mongoReply.requestId > 0 && mongoReply.cursorId.toString() != "0"
+ && callbackInfo && callbackInfo.info && callbackInfo.info.exhaust) {
+ dbInstance._reRegisterHandler(mongoReply.requestId, callbackInfo);
+ }
+
+ // Only execute callback if we have a caller
+ // chained is for findAndModify as it does not respect write concerns
+ if(callbackInfo && callbackInfo.callback && callbackInfo.info && Array.isArray(callbackInfo.info.chained)) {
+ // Check if callback has already been fired (missing chain command)
+ var chained = callbackInfo.info.chained;
+ var numberOfFoundCallbacks = 0;
+ for(var i = 0; i < chained.length; i++) {
+ if(dbInstanceObject._hasHandler(chained[i])) numberOfFoundCallbacks++;
+ }
+
+ // If we have already fired then clean up rest of chain and move on
+ if(numberOfFoundCallbacks != chained.length) {
+ for(var i = 0; i < chained.length; i++) {
+ dbInstanceObject._removeHandler(chained[i]);
+ }
+
+ // Just return from function
+ return;
+ }
+
+ // Parse the body
+ mongoReply.parseBody(message, connectionPool.bson, callbackInfo.info.raw, function(err) {
+ // console.log("+++++++++++++++++++++++++++++++++++++++ recieved message")
+ // console.dir(message)
+ if(err != null) {
+ // If pool connection is already closed
+ if(server._serverState === 'disconnected') return;
+ // Set server state to disconnected
+ server._serverState = 'disconnected';
+ // Remove all listeners and close the connection pool
+ server.removeAllListeners();
+ connectionPool.stop(true);
+
+ // If we have a callback return the error
+ if(typeof callback === 'function') {
+ // ensure no callbacks get called twice
+ var internalCallback = callback;
+ callback = null;
+ // Perform callback
+ internalCallback(new Error("connection closed due to parseError"), null, server);
+ } else if(server.isSetMember()) {
+ if(server.listeners("parseError") && server.listeners("parseError").length > 0) server.emit("parseError", new Error("connection closed due to parseError"), server);
+ } else {
+ if(eventReceiver.listeners("parseError") && eventReceiver.listeners("parseError").length > 0) eventReceiver.emit("parseError", new Error("connection closed due to parseError"), server);
+ }
+
+ // If we are a single server connection fire errors correctly
+ if(!server.isSetMember()) {
+ // Fire all callback errors
+ server.__executeAllCallbacksWithError(new Error("connection closed due to parseError"));
+ // Emit error
+ _emitAcrossAllDbInstances(server, eventReceiver, "parseError", server, null, true);
+ }
+ // Short cut
+ return;
+ }
+
+ // Fetch the callback
+ var callbackInfo = dbInstanceObject._findHandler(mongoReply.responseTo.toString());
+ // If we have an error let's execute the callback and clean up all other
+ // chained commands
+ var firstResult = mongoReply && mongoReply.documents;
+
+ // Check for an error, if we have one let's trigger the callback and clean up
+ // The chained callbacks
+ if(firstResult[0].err != null || firstResult[0].errmsg != null) {
+ // Trigger the callback for the error
+ dbInstanceObject._callHandler(mongoReply.responseTo, mongoReply, null);
+ } else {
+ var chainedIds = callbackInfo.info.chained;
+
+ if(chainedIds.length > 0 && chainedIds[chainedIds.length - 1] == mongoReply.responseTo) {
+ // Cleanup all other chained calls
+ chainedIds.pop();
+ // Remove listeners
+ for(var i = 0; i < chainedIds.length; i++) dbInstanceObject._removeHandler(chainedIds[i]);
+ // Call the handler
+ dbInstanceObject._callHandler(mongoReply.responseTo, callbackInfo.info.results.shift(), null);
+ } else{
+ // Add the results to all the results
+ for(var i = 0; i < chainedIds.length; i++) {
+ var handler = dbInstanceObject._findHandler(chainedIds[i]);
+ // Check if we have an object, if it's the case take the current object commands and
+ // and add this one
+ if(handler.info != null) {
+ handler.info.results = Array.isArray(callbackInfo.info.results) ? callbackInfo.info.results : [];
+ handler.info.results.push(mongoReply);
+ }
+ }
+ }
+ }
+ });
+ } else if(callbackInfo && callbackInfo.callback && callbackInfo.info) {
+ // Parse the body
+ mongoReply.parseBody(message, connectionPool.bson, callbackInfo.info.raw, function(err) {
+ // console.log("+++++++++++++++++++++++++++++++++++++++ recieved message")
+ // console.dir(message)
+ if(err != null) {
+ // If pool connection is already closed
+ if(server._serverState === 'disconnected') return;
+ // Set server state to disconnected
+ server._serverState = 'disconnected';
+ // Remove all listeners and close the connection pool
+ server.removeAllListeners();
+ connectionPool.stop(true);
+
+ // If we have a callback return the error
+ if(typeof callback === 'function') {
+ // ensure no callbacks get called twice
+ var internalCallback = callback;
+ callback = null;
+ // Perform callback
+ internalCallback(new Error("connection closed due to parseError"), null, server);
+ } else if(server.isSetMember()) {
+ if(server.listeners("parseError") && server.listeners("parseError").length > 0) server.emit("parseError", new Error("connection closed due to parseError"), server);
+ } else {
+ if(eventReceiver.listeners("parseError") && eventReceiver.listeners("parseError").length > 0) eventReceiver.emit("parseError", new Error("connection closed due to parseError"), server);
+ }
+
+ // If we are a single server connection fire errors correctly
+ if(!server.isSetMember()) {
+ // Fire all callback errors
+ server.__executeAllCallbacksWithError(new Error("connection closed due to parseError"));
+ // Emit error
+ _emitAcrossAllDbInstances(server, eventReceiver, "parseError", server, null, true);
+ }
+ // Short cut
+ return;
+ }
+
+ // Let's record the stats info if it's enabled
+ if(server.recordQueryStats == true && server._state['runtimeStats'] != null
+ && server._state.runtimeStats['queryStats'] instanceof RunningStats) {
+ // Add data point to the running statistics object
+ server._state.runtimeStats.queryStats.push(new Date().getTime() - callbackInfo.info.start);
+ }
+
+ dbInstanceObject._callHandler(mongoReply.responseTo, mongoReply, null);
+ });
+ }
+ }
+ } catch (err) {
+ // Throw error in next tick
+ process.nextTick(function() {
+ throw err;
+ })
+ }
+ });
+
+ // Handle timeout
+ connectionPool.on("timeout", function(err) {
+ // If pool connection is already closed
+ if(server._serverState === 'disconnected') return;
+ // Set server state to disconnected
+ server._serverState = 'disconnected';
+ // If we have a callback return the error
+ if(typeof callback === 'function') {
+ // ensure no callbacks get called twice
+ var internalCallback = callback;
+ callback = null;
+ // Perform callback
+ internalCallback(err, null, server);
+ } else if(server.isSetMember()) {
+ if(server.listeners("timeout") && server.listeners("timeout").length > 0) server.emit("timeout", err, server);
+ } else {
+ if(eventReceiver.listeners("timeout") && eventReceiver.listeners("timeout").length > 0) eventReceiver.emit("timeout", err, server);
+ }
+
+ // If we are a single server connection fire errors correctly
+ if(!server.isSetMember()) {
+ // Fire all callback errors
+ server.__executeAllCallbacksWithError(err);
+ // Emit error
+ _emitAcrossAllDbInstances(server, eventReceiver, "timeout", err, server, true);
+ }
+ });
+
+ // Handle errors
+ connectionPool.on("error", function(message) {
+ // If pool connection is already closed
+ if(server._serverState === 'disconnected') return;
+ // Set server state to disconnected
+ server._serverState = 'disconnected';
+ // If we have a callback return the error
+ if(typeof callback === 'function') {
+ // ensure no callbacks get called twice
+ var internalCallback = callback;
+ callback = null;
+ // Perform callback
+ internalCallback(new Error(message && message.err ? message.err : message), null, server);
+ } else if(server.isSetMember()) {
+ if(server.listeners("error") && server.listeners("error").length > 0) server.emit("error", new Error(message && message.err ? message.err : message), server);
+ } else {
+ if(eventReceiver.listeners("error") && eventReceiver.listeners("error").length > 0) eventReceiver.emit("error", new Error(message && message.err ? message.err : message), server);
+ }
+
+ // If we are a single server connection fire errors correctly
+ if(!server.isSetMember()) {
+ // Fire all callback errors
+ server.__executeAllCallbacksWithError(new Error(message && message.err ? message.err : message));
+ // Emit error
+ _emitAcrossAllDbInstances(server, eventReceiver, "error", new Error(message && message.err ? message.err : message), server, true);
+ }
+ });
+
+ // Handle close events
+ connectionPool.on("close", function() {
+ // If pool connection is already closed
+ if(server._serverState === 'disconnected') return;
+ // Set server state to disconnected
+ server._serverState = 'disconnected';
+ // If we have a callback return the error
+ if(typeof callback == 'function') {
+ // ensure no callbacks get called twice
+ var internalCallback = callback;
+ callback = null;
+ // Perform callback
+ internalCallback(new Error("connection closed"), null, server);
+ } else if(server.isSetMember()) {
+ if(server.listeners("close") && server.listeners("close").length > 0) server.emit("close", new Error("connection closed"), server);
+ } else {
+ if(eventReceiver.listeners("close") && eventReceiver.listeners("close").length > 0) eventReceiver.emit("close", new Error("connection closed"), server);
+ }
+
+ // If we are a single server connection fire errors correctly
+ if(!server.isSetMember()) {
+ // Fire all callback errors
+ server.__executeAllCallbacksWithError(new Error("connection closed"));
+ // Emit error
+ _emitAcrossAllDbInstances(server, eventReceiver, "close", server, null, true);
+ }
+ });
+
+ // If we have a parser error we are in an unknown state, close everything and emit
+ // error
+ connectionPool.on("parseError", function(message) {
+ // If pool connection is already closed
+ if(server._serverState === 'disconnected') return;
+ // Set server state to disconnected
+ server._serverState = 'disconnected';
+ // If we have a callback return the error
+ if(typeof callback === 'function') {
+ // ensure no callbacks get called twice
+ var internalCallback = callback;
+ callback = null;
+ // Perform callback
+ internalCallback(new Error("connection closed due to parseError"), null, server);
+ } else if(server.isSetMember()) {
+ if(server.listeners("parseError") && server.listeners("parseError").length > 0) server.emit("parseError", new Error("connection closed due to parseError"), server);
+ } else {
+ if(eventReceiver.listeners("parseError") && eventReceiver.listeners("parseError").length > 0) eventReceiver.emit("parseError", new Error("connection closed due to parseError"), server);
+ }
+
+ // If we are a single server connection fire errors correctly
+ if(!server.isSetMember()) {
+ // Fire all callback errors
+ server.__executeAllCallbacksWithError(new Error("connection closed due to parseError"));
+ // Emit error
+ _emitAcrossAllDbInstances(server, eventReceiver, "parseError", server, null, true);
+ }
+ });
+
+ // Boot up connection poole, pass in a locator of callbacks
+ connectionPool.start();
+}
+
+/**
+ * @ignore
+ */
+var _emitAcrossAllDbInstances = function(server, filterDb, event, message, object, resetConnection) {
+ // Emit close event across all db instances sharing the sockets
+ var allServerInstances = server.allServerInstances();
+ // Fetch the first server instance
+ var serverInstance = allServerInstances[0];
+ // For all db instances signal all db instances
+ if(Array.isArray(serverInstance.dbInstances) && serverInstance.dbInstances.length >= 1) {
+ for(var i = 0; i < serverInstance.dbInstances.length; i++) {
+ var dbInstance = serverInstance.dbInstances[i];
+ // Set the parent
+ if(resetConnection && typeof dbInstance.openCalled != 'undefined')
+ dbInstance.openCalled = false;
+ // Check if it's our current db instance and skip if it is
+ if(filterDb == null || filterDb.databaseName !== dbInstance.databaseName || filterDb.tag !== dbInstance.tag) {
+ // Only emit if there is a listener
+ if(dbInstance.listeners(event).length > 0)
+ dbInstance.emit(event, message, object);
+ }
+ }
+ }
+}
+
+/**
+ * @ignore
+ */
+Server.prototype.allRawConnections = function() {
+ return this.connectionPool.getAllConnections();
+}
+
+/**
+ * Check if a writer can be provided
+ * @ignore
+ */
+var canCheckoutWriter = function(self, read) {
+ // We cannot write to an arbiter or secondary server
+ if(self.isMasterDoc['arbiterOnly'] == true) {
+ return new Error("Cannot write to an arbiter");
+ } if(self.isMasterDoc['secondary'] == true) {
+ return new Error("Cannot write to a secondary");
+ } else if(read == true && self._readPreference == ReadPreference.SECONDARY && self.isMasterDoc['ismaster'] == true) {
+ return new Error("Cannot read from primary when secondary only specified");
+ }
+
+ // Return no error
+ return null;
+}
+
+/**
+ * @ignore
+ */
+Server.prototype.checkoutWriter = function(read) {
+ // console.log("===================== checkoutWriter :: " + read)
+ // console.dir(this.isMasterDoc)
+ if(read == true) return this.connectionPool.checkoutConnection();
+ // Check if are allowed to do a checkout (if we try to use an arbiter f.ex)
+ var result = canCheckoutWriter(this, read);
+ // If the result is null check out a writer
+ if(result == null && this.connectionPool != null) {
+ return this.connectionPool.checkoutConnection();
+ } else if(result == null) {
+ return null;
+ } else {
+ return result;
+ }
+}
+
+/**
+ * Check if a reader can be provided
+ * @ignore
+ */
+var canCheckoutReader = function(self) {
+ // We cannot write to an arbiter or secondary server
+ if(self.isMasterDoc && self.isMasterDoc['arbiterOnly'] == true) {
+ return new Error("Cannot write to an arbiter");
+ } else if(self._readPreference != null) {
+ // If the read preference is Primary and the instance is not a master return an error
+ if((self._readPreference == ReadPreference.PRIMARY) && self.isMasterDoc['ismaster'] != true) {
+ return new Error("Read preference is Server.PRIMARY and server is not master");
+ } else if(self._readPreference == ReadPreference.SECONDARY && self.isMasterDoc['ismaster'] == true) {
+ return new Error("Cannot read from primary when secondary only specified");
+ }
+ }
+
+ // Return no error
+ return null;
+}
+
+/**
+ * @ignore
+ */
+Server.prototype.checkoutReader = function() {
+ // console.log("===================== checkoutReader")
+ // Check if are allowed to do a checkout (if we try to use an arbiter f.ex)
+ var result = canCheckoutReader(this);
+ // If the result is null check out a writer
+ if(result == null && this.connectionPool != null) {
+ return this.connectionPool.checkoutConnection();
+ } else if(result == null) {
+ return null;
+ } else {
+ return result;
+ }
+}
+
+/**
+ * @ignore
+ */
+Server.prototype.enableRecordQueryStats = function(enable) {
+ this.recordQueryStats = enable;
+}
+
+/**
+ * Internal statistics object used for calculating average and standard devitation on
+ * running queries
+ * @ignore
+ */
+var RunningStats = function() {
+ var self = this;
+ this.m_n = 0;
+ this.m_oldM = 0.0;
+ this.m_oldS = 0.0;
+ this.m_newM = 0.0;
+ this.m_newS = 0.0;
+
+ // Define getters
+ Object.defineProperty(this, "numDataValues", { enumerable: true
+ , get: function () { return this.m_n; }
+ });
+
+ Object.defineProperty(this, "mean", { enumerable: true
+ , get: function () { return (this.m_n > 0) ? this.m_newM : 0.0; }
+ });
+
+ Object.defineProperty(this, "variance", { enumerable: true
+ , get: function () { return ((this.m_n > 1) ? this.m_newS/(this.m_n - 1) : 0.0); }
+ });
+
+ Object.defineProperty(this, "standardDeviation", { enumerable: true
+ , get: function () { return Math.sqrt(this.variance); }
+ });
+
+ Object.defineProperty(this, "sScore", { enumerable: true
+ , get: function () {
+ var bottom = this.mean + this.standardDeviation;
+ if(bottom == 0) return 0;
+ return ((2 * this.mean * this.standardDeviation)/(bottom));
+ }
+ });
+}
+
+/**
+ * @ignore
+ */
+RunningStats.prototype.push = function(x) {
+ // Update the number of samples
+ this.m_n = this.m_n + 1;
+ // See Knuth TAOCP vol 2, 3rd edition, page 232
+ if(this.m_n == 1) {
+ this.m_oldM = this.m_newM = x;
+ this.m_oldS = 0.0;
+ } else {
+ this.m_newM = this.m_oldM + (x - this.m_oldM) / this.m_n;
+ this.m_newS = this.m_oldS + (x - this.m_oldM) * (x - this.m_newM);
+
+ // set up for next iteration
+ this.m_oldM = this.m_newM;
+ this.m_oldS = this.m_newS;
+ }
+}
+
+/**
+ * @ignore
+ */
+Object.defineProperty(Server.prototype, "autoReconnect", { enumerable: true
+ , get: function () {
+ return this.options['auto_reconnect'] == null ? false : this.options['auto_reconnect'];
+ }
+});
+
+/**
+ * @ignore
+ */
+Object.defineProperty(Server.prototype, "connection", { enumerable: true
+ , get: function () {
+ return this.internalConnection;
+ }
+ , set: function(connection) {
+ this.internalConnection = connection;
+ }
+});
+
+/**
+ * @ignore
+ */
+Object.defineProperty(Server.prototype, "master", { enumerable: true
+ , get: function () {
+ return this.internalMaster;
+ }
+ , set: function(value) {
+ this.internalMaster = value;
+ }
+});
+
+/**
+ * @ignore
+ */
+Object.defineProperty(Server.prototype, "primary", { enumerable: true
+ , get: function () {
+ return this;
+ }
+});
+
+/**
+ * Getter for query Stats
+ * @ignore
+ */
+Object.defineProperty(Server.prototype, "queryStats", { enumerable: true
+ , get: function () {
+ return this._state.runtimeStats.queryStats;
+ }
+});
+
+/**
+ * @ignore
+ */
+Object.defineProperty(Server.prototype, "runtimeStats", { enumerable: true
+ , get: function () {
+ return this._state.runtimeStats;
+ }
+});
+
+/**
+ * Get Read Preference method
+ * @ignore
+ */
+Object.defineProperty(Server.prototype, "readPreference", { enumerable: true
+ , get: function () {
+ if(this._readPreference == null && this.readSecondary) {
+ return Server.READ_SECONDARY;
+ } else if(this._readPreference == null && !this.readSecondary) {
+ return Server.READ_PRIMARY;
+ } else {
+ return this._readPreference;
+ }
+ }
+});
+
+/**
+ * @ignore
+ */
+exports.Server = Server;
diff --git a/node_modules/mongodb/lib/mongodb/connection/strategies/ping_strategy.js b/node_modules/mongodb/lib/mongodb/connection/strategies/ping_strategy.js
new file mode 100644
index 0000000..4fefd78
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/strategies/ping_strategy.js
@@ -0,0 +1,188 @@
+var Server = require("../server").Server;
+
+// The ping strategy uses pings each server and records the
+// elapsed time for the server so it can pick a server based on lowest
+// return time for the db command {ping:true}
+var PingStrategy = exports.PingStrategy = function(replicaset, secondaryAcceptableLatencyMS) {
+ this.replicaset = replicaset;
+ this.secondaryAcceptableLatencyMS = secondaryAcceptableLatencyMS;
+ this.state = 'disconnected';
+ this.pingInterval = 5000;
+ // Class instance
+ this.Db = require("../../db").Db;
+}
+
+// Starts any needed code
+PingStrategy.prototype.start = function(callback) {
+ // already running?
+ if ('connected' == this.state) return;
+
+ this.state = 'connected';
+
+ // Start ping server
+ this._pingServer(callback);
+}
+
+// Stops and kills any processes running
+PingStrategy.prototype.stop = function(callback) {
+ // Stop the ping process
+ this.state = 'disconnected';
+
+ // optional callback
+ callback && callback(null, null);
+}
+
+PingStrategy.prototype.checkoutSecondary = function(tags, secondaryCandidates) {
+ // Servers are picked based on the lowest ping time and then servers that lower than that + secondaryAcceptableLatencyMS
+ // Create a list of candidat servers, containing the primary if available
+ var candidateServers = [];
+
+ // If we have not provided a list of candidate servers use the default setup
+ if(!Array.isArray(secondaryCandidates)) {
+ candidateServers = this.replicaset._state.master != null ? [this.replicaset._state.master] : [];
+ // Add all the secondaries
+ var keys = Object.keys(this.replicaset._state.secondaries);
+ for(var i = 0; i < keys.length; i++) {
+ candidateServers.push(this.replicaset._state.secondaries[keys[i]])
+ }
+ } else {
+ candidateServers = secondaryCandidates;
+ }
+
+ // Final list of eligable server
+ var finalCandidates = [];
+
+ // If we have tags filter by tags
+ if(tags != null && typeof tags == 'object') {
+ // If we have an array or single tag selection
+ var tagObjects = Array.isArray(tags) ? tags : [tags];
+ // Iterate over all tags until we find a candidate server
+ for(var _i = 0; _i < tagObjects.length; _i++) {
+ // Grab a tag object
+ var tagObject = tagObjects[_i];
+ // Matching keys
+ var matchingKeys = Object.keys(tagObject);
+ // Remove any that are not tagged correctly
+ for(var i = 0; i < candidateServers.length; i++) {
+ var server = candidateServers[i];
+ // If we have tags match
+ if(server.tags != null) {
+ var matching = true;
+
+ // Ensure we have all the values
+ for(var j = 0; j < matchingKeys.length; j++) {
+ if(server.tags[matchingKeys[j]] != tagObject[matchingKeys[j]]) {
+ matching = false;
+ break;
+ }
+ }
+
+ // If we have a match add it to the list of matching servers
+ if(matching) {
+ finalCandidates.push(server);
+ }
+ }
+ }
+ }
+ } else {
+ // Final array candidates
+ var finalCandidates = candidateServers;
+ }
+
+ // Sort by ping time
+ finalCandidates.sort(function(a, b) {
+ return a.runtimeStats['pingMs'] > b.runtimeStats['pingMs'];
+ });
+
+ if(0 === finalCandidates.length)
+ return new Error("No replica set members available for query");
+
+ // handle undefined pingMs
+ var lowestPing = finalCandidates[0].runtimeStats['pingMs'] | 0;
+
+ // determine acceptable latency
+ var acceptable = lowestPing + this.secondaryAcceptableLatencyMS;
+
+ // remove any server responding slower than acceptable
+ var len = finalCandidates.length;
+ while(len--) {
+ if(finalCandidates[len].runtimeStats['pingMs'] > acceptable) {
+ finalCandidates.splice(len, 1);
+ }
+ }
+
+ // If no candidates available return an error
+ if(finalCandidates.length == 0)
+ return new Error("No replica set members available for query");
+
+ // Pick a random acceptable server
+ return finalCandidates[Math.round(Math.random(1000000) * (finalCandidates.length - 1))].checkoutReader();
+}
+
+PingStrategy.prototype._pingServer = function(callback) {
+ var self = this;
+
+ // Ping server function
+ var pingFunction = function() {
+ if(self.state == 'disconnected') return;
+ var addresses = self.replicaset._state.addresses;
+
+ // Grab all servers
+ var serverKeys = Object.keys(addresses);
+
+ // Number of server entries
+ var numberOfEntries = serverKeys.length;
+
+ // We got keys
+ for(var i = 0; i < serverKeys.length; i++) {
+
+ // We got a server instance
+ var server = addresses[serverKeys[i]];
+
+ // Create a new server object, avoid using internal connections as they might
+ // be in an illegal state
+ new function(serverInstance) {
+ var options = { poolSize: 1, timeout: 500, auto_reconnect: false };
+ var server = new Server(serverInstance.host, serverInstance.port, options);
+ var db = new self.Db(self.replicaset.db.databaseName, server, { safe: true });
+
+ db.on("error", done);
+
+ // Open the db instance
+ db.open(function(err, _db) {
+ if(err) return done(err, _db);
+
+ // Startup time of the command
+ var startTime = Date.now();
+
+ // Execute ping on this connection
+ db.executeDbCommand({ping:1}, {failFast:true}, function() {
+ if(null != serverInstance.runtimeStats && serverInstance.isConnected()) {
+ serverInstance.runtimeStats['pingMs'] = Date.now() - startTime;
+ }
+
+ done(null, _db);
+ })
+ })
+
+ function done (err, _db) {
+ // Close connection
+ if (_db) _db.close(true);
+
+ // Adjust the number of checks
+ numberOfEntries--;
+
+ // If we are done with all results coming back trigger ping again
+ if(0 === numberOfEntries && 'connected' == self.state) {
+ setTimeout(pingFunction, self.pingInterval);
+ }
+ }
+ }(server);
+ }
+ }
+
+ // Start pingFunction
+ setTimeout(pingFunction, 1000);
+
+ callback && callback(null);
+}
diff --git a/node_modules/mongodb/lib/mongodb/connection/strategies/statistics_strategy.js b/node_modules/mongodb/lib/mongodb/connection/strategies/statistics_strategy.js
new file mode 100644
index 0000000..2e87dbd
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/strategies/statistics_strategy.js
@@ -0,0 +1,78 @@
+// The Statistics strategy uses the measure of each end-start time for each
+// query executed against the db to calculate the mean, variance and standard deviation
+// and pick the server which the lowest mean and deviation
+var StatisticsStrategy = exports.StatisticsStrategy = function(replicaset) {
+ this.replicaset = replicaset;
+}
+
+// Starts any needed code
+StatisticsStrategy.prototype.start = function(callback) {
+ callback && callback(null, null);
+}
+
+StatisticsStrategy.prototype.stop = function(callback) {
+ callback && callback(null, null);
+}
+
+StatisticsStrategy.prototype.checkoutSecondary = function(tags, secondaryCandidates) {
+ // Servers are picked based on the lowest ping time and then servers that lower than that + secondaryAcceptableLatencyMS
+ // Create a list of candidat servers, containing the primary if available
+ var candidateServers = [];
+
+ // If we have not provided a list of candidate servers use the default setup
+ if(!Array.isArray(secondaryCandidates)) {
+ candidateServers = this.replicaset._state.master != null ? [this.replicaset._state.master] : [];
+ // Add all the secondaries
+ var keys = Object.keys(this.replicaset._state.secondaries);
+ for(var i = 0; i < keys.length; i++) {
+ candidateServers.push(this.replicaset._state.secondaries[keys[i]])
+ }
+ } else {
+ candidateServers = secondaryCandidates;
+ }
+
+ // Final list of eligable server
+ var finalCandidates = [];
+
+ // If we have tags filter by tags
+ if(tags != null && typeof tags == 'object') {
+ // If we have an array or single tag selection
+ var tagObjects = Array.isArray(tags) ? tags : [tags];
+ // Iterate over all tags until we find a candidate server
+ for(var _i = 0; _i < tagObjects.length; _i++) {
+ // Grab a tag object
+ var tagObject = tagObjects[_i];
+ // Matching keys
+ var matchingKeys = Object.keys(tagObject);
+ // Remove any that are not tagged correctly
+ for(var i = 0; i < candidateServers.length; i++) {
+ var server = candidateServers[i];
+ // If we have tags match
+ if(server.tags != null) {
+ var matching = true;
+
+ // Ensure we have all the values
+ for(var j = 0; j < matchingKeys.length; j++) {
+ if(server.tags[matchingKeys[j]] != tagObject[matchingKeys[j]]) {
+ matching = false;
+ break;
+ }
+ }
+
+ // If we have a match add it to the list of matching servers
+ if(matching) {
+ finalCandidates.push(server);
+ }
+ }
+ }
+ }
+ } else {
+ // Final array candidates
+ var finalCandidates = candidateServers;
+ }
+
+ // If no candidates available return an error
+ if(finalCandidates.length == 0) return new Error("No replica set members available for query");
+ // Pick a random server
+ return finalCandidates[Math.round(Math.random(1000000) * (finalCandidates.length - 1))].checkoutReader();
+}
diff --git a/node_modules/mongodb/lib/mongodb/connection/url_parser.js b/node_modules/mongodb/lib/mongodb/connection/url_parser.js
new file mode 100644
index 0000000..9c280b7
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/connection/url_parser.js
@@ -0,0 +1,223 @@
+var fs = require('fs'),
+ ReadPreference = require('./read_preference').ReadPreference;
+
+exports.parse = function(url, options) {
+ // Ensure we have a default options object if none set
+ options = options || {};
+ // Variables
+ var connection_part = '';
+ var auth_part = '';
+ var query_string_part = '';
+ var dbName = 'default';
+
+ // Must start with mongodb
+ if(url.indexOf("mongodb://") != 0)
+ throw Error("URL must be in the format mongodb://user:pass@host:port/dbname");
+ // If we have a ? mark cut the query elements off
+ if(url.indexOf("?") != -1) {
+ query_string_part = url.substr(url.indexOf("?") + 1);
+ connection_part = url.substring("mongodb://".length, url.indexOf("?"))
+ } else {
+ connection_part = url.substring("mongodb://".length);
+ }
+
+ // Check if we have auth params
+ if(connection_part.indexOf("@") != -1) {
+ auth_part = connection_part.split("@")[0];
+ connection_part = connection_part.split("@")[1];
+ }
+
+ // Check if the connection string has a db
+ if(connection_part.indexOf(".sock") != -1) {
+ if(connection_part.indexOf(".sock/") != -1) {
+ dbName = connection_part.split(".sock/")[1];
+ connection_part = connection_part.split("/", connection_part.indexOf(".sock") + ".sock".length);
+ }
+ } else if(connection_part.indexOf("/") != -1) {
+ dbName = connection_part.split("/")[1];
+ connection_part = connection_part.split("/")[0];
+ }
+
+ // Result object
+ var object = {};
+
+ // Pick apart the authentication part of the string
+ var authPart = auth_part || '';
+ var auth = authPart.split(':', 2);
+ if(options['uri_decode_auth']){
+ auth[0] = decodeURIComponent(auth[0]);
+ if(auth[1]){
+ auth[1] = decodeURIComponent(auth[1]);
+ }
+ }
+
+ // Add auth to final object if we have 2 elements
+ if(auth.length == 2) object.auth = {user: auth[0], password: auth[1]};
+
+ // Variables used for temporary storage
+ var hostPart;
+ var urlOptions;
+ var servers;
+ var serverOptions = {socketOptions: {}};
+ var dbOptions = {read_preference_tags: []};
+ var replSetServersOptions = {socketOptions: {}};
+ // Add server options to final object
+ object.server_options = serverOptions;
+ object.db_options = dbOptions;
+ object.rs_options = replSetServersOptions;
+ object.mongos_options = {};
+
+ // Let's check if we are using a domain socket
+ if(url.match(/\.sock/)) {
+ // Split out the socket part
+ var domainSocket = url.substring(
+ url.indexOf("mongodb://") + "mongodb://".length
+ , url.lastIndexOf(".sock") + ".sock".length);
+ // Clean out any auth stuff if any
+ if(domainSocket.indexOf("@") != -1) domainSocket = domainSocket.split("@")[1];
+ servers = [{domain_socket: domainSocket}];
+ } else {
+ // Split up the db
+ hostPart = connection_part;
+ // Parse all server results
+ servers = hostPart.split(',').map(function(h) {
+ var hostPort = h.split(':', 2);
+ var _host = hostPort[0] || 'localhost';
+ var _port = hostPort[1] != null ? parseInt(hostPort[1], 10) : 27017;
+ // Check for localhost?safe=true style case
+ if(_host.indexOf("?") != -1) _host = _host.split(/\?/)[0];
+
+ // Return the mapped object
+ return {host: _host, port: _port};
+ });
+ }
+
+ // Get the db name
+ object.dbName = dbName || 'default';
+ // Split up all the options
+ urlOptions = (query_string_part || '').split(/[&;]/);
+ // Ugh, we have to figure out which options go to which constructor manually.
+ urlOptions.forEach(function(opt) {
+ if(!opt) return;
+ var splitOpt = opt.split('='), name = splitOpt[0], value = splitOpt[1];
+ // Options implementations
+ switch(name) {
+ case 'slaveOk':
+ case 'slave_ok':
+ serverOptions.slave_ok = (value == 'true');
+ break;
+ case 'maxPoolSize':
+ case 'poolSize':
+ serverOptions.poolSize = parseInt(value, 10);
+ replSetServersOptions.poolSize = parseInt(value, 10);
+ break;
+ case 'autoReconnect':
+ case 'auto_reconnect':
+ serverOptions.auto_reconnect = (value == 'true');
+ break;
+ case 'minPoolSize':
+ throw new Error("minPoolSize not supported");
+ case 'maxIdleTimeMS':
+ throw new Error("maxIdleTimeMS not supported");
+ case 'waitQueueMultiple':
+ throw new Error("waitQueueMultiple not supported");
+ case 'waitQueueTimeoutMS':
+ throw new Error("waitQueueTimeoutMS not supported");
+ case 'uuidRepresentation':
+ throw new Error("uuidRepresentation not supported");
+ case 'ssl':
+ if(value == 'prefer') {
+ serverOptions.socketOptions.ssl = value;
+ replSetServersOptions.socketOptions.ssl = value;
+ break;
+ }
+ serverOptions.socketOptions.ssl = (value == 'true');
+ replSetServersOptions.socketOptions.ssl = (value == 'true');
+ break;
+ case 'replicaSet':
+ case 'rs_name':
+ replSetServersOptions.rs_name = value;
+ break;
+ case 'reconnectWait':
+ replSetServersOptions.reconnectWait = parseInt(value, 10);
+ break;
+ case 'retries':
+ replSetServersOptions.retries = parseInt(value, 10);
+ break;
+ case 'readSecondary':
+ case 'read_secondary':
+ replSetServersOptions.read_secondary = (value == 'true');
+ break;
+ case 'fsync':
+ dbOptions.fsync = (value == 'true');
+ break;
+ case 'journal':
+ dbOptions.journal = (value == 'true');
+ break;
+ case 'safe':
+ dbOptions.safe = (value == 'true');
+ break;
+ case 'nativeParser':
+ case 'native_parser':
+ dbOptions.native_parser = (value == 'true');
+ break;
+ case 'connectTimeoutMS':
+ serverOptions.socketOptions.connectTimeoutMS = parseInt(value, 10);
+ replSetServersOptions.socketOptions.connectTimeoutMS = parseInt(value, 10);
+ break;
+ case 'socketTimeoutMS':
+ serverOptions.socketOptions.socketTimeoutMS = parseInt(value, 10);
+ replSetServersOptions.socketOptions.socketTimeoutMS = parseInt(value, 10);
+ break;
+ case 'w':
+ dbOptions.w = parseInt(value, 10);
+ break;
+ case 'wtimeoutMS':
+ dbOptions.wtimeoutMS = parseInt(value, 10);
+ break;
+ case 'readPreference':
+ if(!ReadPreference.isValid(value)) throw new Error("readPreference must be either primary/primaryPreferred/secondary/secondaryPreferred/nearest");
+ dbOptions.read_preference = value;
+ break;
+ case 'readPreferenceTags':
+ // Contains the tag object
+ var tagObject = {};
+ if(value == null || value == '') {
+ dbOptions.read_preference_tags.push(tagObject);
+ break;
+ }
+
+ // Split up the tags
+ var tags = value.split(/\,/);
+ for(var i = 0; i < tags.length; i++) {
+ var parts = tags[i].trim().split(/\:/);
+ tagObject[parts[0]] = parts[1];
+ }
+
+ // Set the preferences tags
+ dbOptions.read_preference_tags.push(tagObject);
+ break;
+ default:
+ break;
+ }
+ });
+
+ // No tags: should be null (not [])
+ if(dbOptions.read_preference_tags.length === 0) {
+ dbOptions.read_preference_tags = null;
+ }
+
+ // Validate if there are an invalid write concern combinations
+ if((dbOptions.w == -1 || dbOptions.w == 0) && (
+ dbOptions.journal == true
+ || dbOptions.fsync == true
+ || dbOptions.safe == true)) throw new Error("w set to -1 or 0 cannot be combined with safe/w/journal/fsync")
+
+ // If no read preference set it to primary
+ if(!dbOptions.read_preference) dbOptions.read_preference = 'primary';
+
+ // Add servers to result
+ object.servers = servers;
+ // Returned parsed object
+ return object;
+}
diff --git a/node_modules/mongodb/lib/mongodb/cursor.js b/node_modules/mongodb/lib/mongodb/cursor.js
new file mode 100644
index 0000000..34a1ddc
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/cursor.js
@@ -0,0 +1,881 @@
+var QueryCommand = require('./commands/query_command').QueryCommand,
+ GetMoreCommand = require('./commands/get_more_command').GetMoreCommand,
+ KillCursorCommand = require('./commands/kill_cursor_command').KillCursorCommand,
+ Long = require('bson').Long,
+ ReadPreference = require('./connection/read_preference').ReadPreference,
+ CursorStream = require('./cursorstream'),
+ utils = require('./utils');
+
+/**
+ * Constructor for a cursor object that handles all the operations on query result
+ * using find. This cursor object is unidirectional and cannot traverse backwards. Clients should not be creating a cursor directly,
+ * but use find to acquire a cursor.
+ *
+ * Options
+ * - **skip** {Number} skip number of documents to skip.
+ * - **limit** {Number}, limit the number of results to return. -1 has a special meaning and is used by Db.eval. A value of 1 will also be treated as if it were -1.
+ * - **sort** {Array | Object}, set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc.
+ * - **hint** {Object}, hint force the query to use a specific index.
+ * - **explain** {Boolean}, explain return the explaination of the query.
+ * - **snapshot** {Boolean}, snapshot Snapshot mode assures no duplicates are returned.
+ * - **timeout** {Boolean}, timeout allow the query to timeout.
+ * - **tailable** {Boolean}, tailable allow the cursor to be tailable.
+ * - **awaitdata** {Boolean}, awaitdata allow the cursor to wait for data, only applicable for tailable cursor.
+ * - **batchSize** {Number}, batchSize the number of the subset of results to request the database to return for every request. This should initially be greater than 1 otherwise the database will automatically close the cursor. The batch size can be set to 1 with cursorInstance.batchSize after performing the initial query to the database.
+ * - **raw** {Boolean}, raw return all query documents as raw buffers (default false).
+ * - **read** {Boolean}, read specify override of read from source (primary/secondary).
+ * - **slaveOk** {Boolean}, slaveOk, sets the slaveOk flag on the query wire protocol for secondaries.
+ * - **returnKey** {Boolean}, returnKey only return the index key.
+ * - **maxScan** {Number}, maxScan limit the number of items to scan.
+ * - **min** {Number}, min set index bounds.
+ * - **max** {Number}, max set index bounds.
+ * - **showDiskLoc** {Boolean}, showDiskLoc show disk location of results.
+ * - **comment** {String}, comment you can put a $comment field on a query to make looking in the profiler logs simpler.
+ * - **numberOfRetries** {Number}, numberOfRetries if using awaidata specifies the number of times to retry on timeout.
+ * - **dbName** {String}, dbName override the default dbName.
+ * - **tailableRetryInterval** {Number}, tailableRetryInterval specify the miliseconds between getMores on tailable cursor.
+ * - **exhaust** {Boolean}, exhaust have the server send all the documents at once as getMore packets.
+ * - **partial** {Boolean}, partial have the sharded system return a partial result from mongos.
+ *
+ * @class Represents a Cursor.
+ * @param {Db} db the database object to work with.
+ * @param {Collection} collection the collection to query.
+ * @param {Object} selector the query selector.
+ * @param {Object} fields an object containing what fields to include or exclude from objects returned.
+ * @param {Object} [options] additional options for the collection.
+*/
+function Cursor(db, collection, selector, fields, options) {
+ this.db = db;
+ this.collection = collection;
+ this.selector = selector;
+ this.fields = fields;
+ options = !options ? {} : options;
+
+ this.skipValue = options.skip == null ? 0 : options.skip;
+ this.limitValue = options.limit == null ? 0 : options.limit;
+ this.sortValue = options.sort;
+ this.hint = options.hint;
+ this.explainValue = options.explain;
+ this.snapshot = options.snapshot;
+ this.timeout = options.timeout == null ? true : options.timeout;
+ this.tailable = options.tailable;
+ this.awaitdata = options.awaitdata;
+ this.numberOfRetries = options.numberOfRetries == null ? 5 : options.numberOfRetries;
+ this.currentNumberOfRetries = this.numberOfRetries;
+ this.batchSizeValue = options.batchSize == null ? 0 : options.batchSize;
+ this.slaveOk = options.slaveOk == null ? collection.slaveOk : options.slaveOk;
+ this.raw = options.raw == null ? false : options.raw;
+ this.read = options.read == null ? ReadPreference.PRIMARY : options.read;
+ this.returnKey = options.returnKey;
+ this.maxScan = options.maxScan;
+ this.min = options.min;
+ this.max = options.max;
+ this.showDiskLoc = options.showDiskLoc;
+ this.comment = options.comment;
+ this.tailableRetryInterval = options.tailableRetryInterval || 100;
+ this.exhaust = options.exhaust || false;
+ this.partial = options.partial || false;
+
+ this.totalNumberOfRecords = 0;
+ this.items = [];
+ this.cursorId = Long.fromInt(0);
+
+ // This name
+ this.dbName = options.dbName;
+
+ // State variables for the cursor
+ this.state = Cursor.INIT;
+ // Keep track of the current query run
+ this.queryRun = false;
+ this.getMoreTimer = false;
+
+ // If we are using a specific db execute against it
+ if(this.dbName != null) {
+ this.collectionName = this.dbName + "." + this.collection.collectionName;
+ } else {
+ this.collectionName = (this.db.databaseName ? this.db.databaseName + "." : '') + this.collection.collectionName;
+ }
+};
+
+/**
+ * Resets this cursor to its initial state. All settings like the query string,
+ * tailable, batchSizeValue, skipValue and limits are preserved.
+ *
+ * @return {Cursor} returns itself with rewind applied.
+ * @api public
+ */
+Cursor.prototype.rewind = function() {
+ var self = this;
+
+ if (self.state != Cursor.INIT) {
+ if (self.state != Cursor.CLOSED) {
+ self.close(function() {});
+ }
+
+ self.numberOfReturned = 0;
+ self.totalNumberOfRecords = 0;
+ self.items = [];
+ self.cursorId = Long.fromInt(0);
+ self.state = Cursor.INIT;
+ self.queryRun = false;
+ }
+
+ return self;
+};
+
+
+/**
+ * Returns an array of documents. The caller is responsible for making sure that there
+ * is enough memory to store the results. Note that the array only contain partial
+ * results when this cursor had been previouly accessed. In that case,
+ * cursor.rewind() can be used to reset the cursor.
+ *
+ * @param {Function} callback This will be called after executing this method successfully. The first parameter will contain the Error object if an error occured, or null otherwise. The second parameter will contain an array of BSON deserialized objects as a result of the query.
+ * @return {null}
+ * @api public
+ */
+Cursor.prototype.toArray = function(callback) {
+ var self = this;
+
+ if(!callback) {
+ throw new Error('callback is mandatory');
+ }
+
+ if(this.tailable) {
+ callback(new Error("Tailable cursor cannot be converted to array"), null);
+ } else if(this.state != Cursor.CLOSED) {
+ var items = [];
+
+ this.each(function(err, item) {
+ if(err != null) return callback(err, null);
+
+ if(item != null && Array.isArray(items)) {
+ items.push(item);
+ } else {
+ var resultItems = items;
+ items = null;
+ self.items = [];
+ // Returns items
+ callback(err, resultItems);
+ }
+ });
+ } else {
+ callback(new Error("Cursor is closed"), null);
+ }
+};
+
+/**
+ * Iterates over all the documents for this cursor. As with **{cursor.toArray}**,
+ * not all of the elements will be iterated if this cursor had been previouly accessed.
+ * In that case, **{cursor.rewind}** can be used to reset the cursor. However, unlike
+ * **{cursor.toArray}**, the cursor will only hold a maximum of batch size elements
+ * at any given time if batch size is specified. Otherwise, the caller is responsible
+ * for making sure that the entire result can fit the memory.
+ *
+ * @param {Function} callback this will be called for while iterating every document of the query result. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the document.
+ * @return {null}
+ * @api public
+ */
+Cursor.prototype.each = function(callback) {
+ var self = this;
+
+ if (!callback) {
+ throw new Error('callback is mandatory');
+ }
+
+ if(this.state != Cursor.CLOSED) {
+ //FIX: stack overflow (on deep callback) (cred: https://github.com/limp/node-mongodb-native/commit/27da7e4b2af02035847f262b29837a94bbbf6ce2)
+ process.nextTick(function(){
+ var s = new Date()
+ // Fetch the next object until there is no more objects
+ self.nextObject(function(err, item) {
+ if(err != null) return callback(err, null);
+ if(item != null) {
+ callback(null, item);
+ self.each(callback);
+ } else {
+ // Close the cursor if done
+ self.state = Cursor.CLOSED;
+ callback(err, null);
+ }
+ });
+ });
+ } else {
+ callback(new Error("Cursor is closed"), null);
+ }
+};
+
+/**
+ * Determines how many result the query for this cursor will return
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the number of results or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Cursor.prototype.count = function(callback) {
+ this.collection.count(this.selector, callback);
+};
+
+/**
+ * Sets the sort parameter of this cursor to the given value.
+ *
+ * This method has the following method signatures:
+ * (keyOrList, callback)
+ * (keyOrList, direction, callback)
+ *
+ * @param {String|Array|Object} keyOrList This can be a string or an array. If passed as a string, the string will be the field to sort. If passed an array, each element will represent a field to be sorted and should be an array that contains the format [string, direction].
+ * @param {String|Number} direction this determines how the results are sorted. "asc", "ascending" or 1 for asceding order while "desc", "desceding or -1 for descending order. Note that the strings are case insensitive.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain an error object when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
+ * @return {Cursor} an instance of this object.
+ * @api public
+ */
+Cursor.prototype.sort = function(keyOrList, direction, callback) {
+ callback = callback || function(){};
+ if(typeof direction === "function") { callback = direction; direction = null; }
+
+ if(this.tailable) {
+ callback(new Error("Tailable cursor doesn't support sorting"), null);
+ } else if(this.queryRun == true || this.state == Cursor.CLOSED) {
+ callback(new Error("Cursor is closed"), null);
+ } else {
+ var order = keyOrList;
+
+ if(direction != null) {
+ order = [[keyOrList, direction]];
+ }
+
+ this.sortValue = order;
+ callback(null, this);
+ }
+ return this;
+};
+
+/**
+ * Sets the limit parameter of this cursor to the given value.
+ *
+ * @param {Number} limit the new limit.
+ * @param {Function} [callback] this optional callback will be called after executing this method. The first parameter will contain an error object when the limit given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
+ * @return {Cursor} an instance of this object.
+ * @api public
+ */
+Cursor.prototype.limit = function(limit, callback) {
+ if(this.tailable) {
+ if(callback) {
+ callback(new Error("Tailable cursor doesn't support limit"), null);
+ } else {
+ throw new Error("Tailable cursor doesn't support limit");
+ }
+ } else if(this.queryRun == true || this.state == Cursor.CLOSED) {
+ if(callback) {
+ callback(new Error("Cursor is closed"), null);
+ } else {
+ throw new Error("Cursor is closed");
+ }
+ } else {
+ if(limit != null && limit.constructor != Number) {
+ if(callback) {
+ callback(new Error("limit requires an integer"), null);
+ } else {
+ throw new Error("limit requires an integer");
+ }
+ } else {
+ this.limitValue = limit;
+ if(callback) return callback(null, this);
+ }
+ }
+
+ return this;
+};
+
+/**
+ * Sets the read preference for the cursor
+ *
+ * @param {String} the read preference for the cursor, one of Server.READ_PRIMARY, Server.READ_SECONDARY, Server.READ_SECONDARY_ONLY
+ * @param {Function} [callback] this optional callback will be called after executing this method. The first parameter will contain an error object when the read preference given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
+ * @return {Cursor} an instance of this object.
+ * @api public
+ */
+Cursor.prototype.setReadPreference = function(readPreference, tags, callback) {
+ if(typeof tags == 'function') callback = tags;
+
+ var _mode = readPreference != null && typeof readPreference == 'object' ? readPreference.mode : readPreference;
+
+ if(this.queryRun == true || this.state == Cursor.CLOSED) {
+ if(callback == null) throw new Error("Cannot change read preference on executed query or closed cursor");
+ callback(new Error("Cannot change read preference on executed query or closed cursor"));
+ } else if(_mode != null && _mode != 'primary'
+ && _mode != 'secondaryOnly' && _mode != 'secondary'
+ && _mode != 'nearest' && _mode != 'primaryPreferred' && _mode != 'secondaryPreferred') {
+ if(callback == null) throw new Error("only readPreference of primary, secondary, secondaryPreferred, primaryPreferred or nearest supported");
+ callback(new Error("only readPreference of primary, secondary, secondaryPreferred, primaryPreferred or nearest supported"));
+ } else {
+ this.read = readPreference;
+ if(callback != null) callback(null, this);
+ }
+
+ return this;
+}
+
+/**
+ * Sets the skip parameter of this cursor to the given value.
+ *
+ * @param {Number} skip the new skip value.
+ * @param {Function} [callback] this optional callback will be called after executing this method. The first parameter will contain an error object when the skip value given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
+ * @return {Cursor} an instance of this object.
+ * @api public
+ */
+Cursor.prototype.skip = function(skip, callback) {
+ callback = callback || function(){};
+
+ if(this.tailable) {
+ callback(new Error("Tailable cursor doesn't support skip"), null);
+ } else if(this.queryRun == true || this.state == Cursor.CLOSED) {
+ callback(new Error("Cursor is closed"), null);
+ } else {
+ if(skip != null && skip.constructor != Number) {
+ callback(new Error("skip requires an integer"), null);
+ } else {
+ this.skipValue = skip;
+ callback(null, this);
+ }
+ }
+
+ return this;
+};
+
+/**
+ * Sets the batch size parameter of this cursor to the given value.
+ *
+ * @param {Number} batchSize the new batch size.
+ * @param {Function} [callback] this optional callback will be called after executing this method. The first parameter will contain an error object when the batchSize given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
+ * @return {Cursor} an instance of this object.
+ * @api public
+ */
+Cursor.prototype.batchSize = function(batchSize, callback) {
+ if(this.state == Cursor.CLOSED) {
+ if(callback != null) {
+ return callback(new Error("Cursor is closed"), null);
+ } else {
+ throw new Error("Cursor is closed");
+ }
+ } else if(batchSize != null && batchSize.constructor != Number) {
+ if(callback != null) {
+ return callback(new Error("batchSize requires an integer"), null);
+ } else {
+ throw new Error("batchSize requires an integer");
+ }
+ } else {
+ this.batchSizeValue = batchSize;
+ if(callback != null) return callback(null, this);
+ }
+
+ return this;
+};
+
+/**
+ * The limit used for the getMore command
+ *
+ * @return {Number} The number of records to request per batch.
+ * @ignore
+ * @api private
+ */
+var limitRequest = function(self) {
+ var requestedLimit = self.limitValue;
+ var absLimitValue = Math.abs(self.limitValue);
+ var absBatchValue = Math.abs(self.batchSizeValue);
+
+ if(absLimitValue > 0) {
+ if (absBatchValue > 0) {
+ requestedLimit = Math.min(absLimitValue, absBatchValue);
+ }
+ } else {
+ requestedLimit = self.batchSizeValue;
+ }
+
+ return requestedLimit;
+};
+
+
+/**
+ * Generates a QueryCommand object using the parameters of this cursor.
+ *
+ * @return {QueryCommand} The command object
+ * @ignore
+ * @api private
+ */
+var generateQueryCommand = function(self) {
+ // Unpack the options
+ var queryOptions = QueryCommand.OPTS_NONE;
+ if(!self.timeout) {
+ queryOptions |= QueryCommand.OPTS_NO_CURSOR_TIMEOUT;
+ }
+
+ if(self.tailable != null) {
+ queryOptions |= QueryCommand.OPTS_TAILABLE_CURSOR;
+ self.skipValue = self.limitValue = 0;
+
+ // if awaitdata is set
+ if(self.awaitdata != null) {
+ queryOptions |= QueryCommand.OPTS_AWAIT_DATA;
+ }
+ }
+
+ if(self.exhaust) {
+ queryOptions |= QueryCommand.OPTS_EXHAUST;
+ }
+
+ if(self.slaveOk) {
+ queryOptions |= QueryCommand.OPTS_SLAVE;
+ }
+
+ if(self.partial) {
+ queryOptions |= QueryCommand.OPTS_PARTIAL;
+ }
+
+ // limitValue of -1 is a special case used by Db#eval
+ var numberToReturn = self.limitValue == -1 ? -1 : limitRequest(self);
+
+ // Check if we need a special selector
+ if(self.sortValue != null || self.explainValue != null || self.hint != null || self.snapshot != null
+ || self.returnKey != null || self.maxScan != null || self.min != null || self.max != null
+ || self.showDiskLoc != null || self.comment != null) {
+
+ // Build special selector
+ var specialSelector = {'$query':self.selector};
+ if(self.sortValue != null) specialSelector['orderby'] = utils.formattedOrderClause(self.sortValue);
+ if(self.hint != null && self.hint.constructor == Object) specialSelector['$hint'] = self.hint;
+ if(self.snapshot != null) specialSelector['$snapshot'] = true;
+ if(self.returnKey != null) specialSelector['$returnKey'] = self.returnKey;
+ if(self.maxScan != null) specialSelector['$maxScan'] = self.maxScan;
+ if(self.min != null) specialSelector['$min'] = self.min;
+ if(self.max != null) specialSelector['$max'] = self.max;
+ if(self.showDiskLoc != null) specialSelector['$showDiskLoc'] = self.showDiskLoc;
+ if(self.comment != null) specialSelector['$comment'] = self.comment;
+ // If we have explain set only return a single document with automatic cursor close
+ if(self.explainValue != null) {
+ numberToReturn = (-1)*Math.abs(numberToReturn);
+ specialSelector['$explain'] = true;
+ }
+
+ // Return the query
+ return new QueryCommand(self.db, self.collectionName, queryOptions, self.skipValue, numberToReturn, specialSelector, self.fields);
+ } else {
+ return new QueryCommand(self.db, self.collectionName, queryOptions, self.skipValue, numberToReturn, self.selector, self.fields);
+ }
+};
+
+/**
+ * @return {Object} Returns an object containing the sort value of this cursor with
+ * the proper formatting that can be used internally in this cursor.
+ * @ignore
+ * @api private
+ */
+Cursor.prototype.formattedOrderClause = function() {
+ return utils.formattedOrderClause(this.sortValue);
+};
+
+/**
+ * Converts the value of the sort direction into its equivalent numerical value.
+ *
+ * @param sortDirection {String|number} Range of acceptable values:
+ * 'ascending', 'descending', 'asc', 'desc', 1, -1
+ *
+ * @return {number} The equivalent numerical value
+ * @throws Error if the given sortDirection is invalid
+ * @ignore
+ * @api private
+ */
+Cursor.prototype.formatSortValue = function(sortDirection) {
+ return utils.formatSortValue(sortDirection);
+};
+
+/**
+ * Gets the next document from the cursor.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain an error object on error while the second parameter will contain a document from the returned result or null if there are no more results.
+ * @api public
+ */
+Cursor.prototype.nextObject = function(callback) {
+ var self = this;
+
+ if(self.state == Cursor.INIT) {
+ var cmd;
+ try {
+ cmd = generateQueryCommand(self);
+ } catch (err) {
+ return callback(err, null);
+ }
+
+ var commandHandler = function(err, result) {
+ // console.log("=========================================== QUERY NEXT OBJECT")
+ // console.dir(err)
+ if(err != null && result == null) return callback(err, null);
+
+ if(!err && result.documents[0] && result.documents[0]['$err']) {
+ return self.close(function() {callback(result.documents[0]['$err'], null);});
+ }
+
+ self.queryRun = true;
+ self.state = Cursor.OPEN; // Adjust the state of the cursor
+ self.cursorId = result.cursorId;
+ self.totalNumberOfRecords = result.numberReturned;
+
+ // Add the new documents to the list of items, using forloop to avoid
+ // new array allocations and copying
+ for(var i = 0; i < result.documents.length; i++) {
+ self.items.push(result.documents[i]);
+ }
+
+ // Ignore callbacks until the cursor is dead for exhausted
+ if(self.exhaust && result.cursorId.toString() == "0") {
+ self.nextObject(callback);
+ } else if(self.exhaust == false || self.exhaust == null) {
+ self.nextObject(callback);
+ }
+ };
+
+ // If we have no connection set on this cursor check one out
+ if(self.connection == null) {
+ try {
+ self.connection = this.read == null ? self.db.serverConfig.checkoutWriter() : self.db.serverConfig.checkoutReader(this.read);
+ } catch(err) {
+ return callback(err, null);
+ }
+ }
+
+ // Execute the command
+ self.db._executeQueryCommand(cmd, {exhaust: self.exhaust, raw:self.raw, read:this.read, connection:self.connection}, commandHandler);
+ // Set the command handler to null
+ commandHandler = null;
+ } else if(self.items.length) {
+ callback(null, self.items.shift());
+ } else if(self.cursorId.greaterThan(Long.fromInt(0))) {
+ getMore(self, callback);
+ } else {
+ // Force cursor to stay open
+ return self.close(function() {callback(null, null);});
+ }
+}
+
+/**
+ * Gets more results from the database if any.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain an error object on error while the second parameter will contain a document from the returned result or null if there are no more results.
+ * @ignore
+ * @api private
+ */
+var getMore = function(self, callback) {
+ var limit = 0;
+
+ if (!self.tailable && self.limitValue > 0) {
+ limit = self.limitValue - self.totalNumberOfRecords;
+ if (limit < 1) {
+ self.close(function() {callback(null, null);});
+ return;
+ }
+ }
+ try {
+ var getMoreCommand = new GetMoreCommand(
+ self.db
+ , self.collectionName
+ , limitRequest(self)
+ , self.cursorId
+ );
+
+ // Set up options
+ var options = {read: self.read, raw: self.raw, connection:self.connection };
+
+ // Execute the command
+ self.db._executeQueryCommand(getMoreCommand, options, function(err, result) {
+ try {
+ if(err != null) {
+ return callback(err, null);
+ }
+
+ var isDead = 1 === result.responseFlag && result.cursorId.isZero();
+
+ self.cursorId = result.cursorId;
+ self.totalNumberOfRecords += result.numberReturned;
+
+ // Determine if there's more documents to fetch
+ if(result.numberReturned > 0) {
+ if (self.limitValue > 0) {
+ var excessResult = self.totalNumberOfRecords - self.limitValue;
+
+ if (excessResult > 0) {
+ result.documents.splice(-1 * excessResult, excessResult);
+ }
+ }
+
+ // Reset the tries for awaitdata if we are using it
+ self.currentNumberOfRetries = self.numberOfRetries;
+ // Get the documents
+ for(var i = 0; i < result.documents.length; i++) {
+ self.items.push(result.documents[i]);
+ }
+
+ // result = null;
+ callback(null, self.items.shift());
+ } else if(self.tailable && !isDead && self.awaitdata) {
+ // Excute the tailable cursor once more, will timeout after ~4 sec if awaitdata used
+ self.currentNumberOfRetries = self.currentNumberOfRetries - 1;
+ if(self.currentNumberOfRetries == 0) {
+ self.close(function() {
+ callback(new Error("tailable cursor timed out"), null);
+ });
+ } else {
+ process.nextTick(function() { getMore(self, callback); });
+ }
+ } else if(self.tailable && !isDead) {
+ self.getMoreTimer = setTimeout(function() { getMore(self, callback); }, self.tailableRetryInterval);
+ } else {
+ self.close(function() {callback(null, null); });
+ }
+
+ result = null;
+ } catch(err) {
+ callback(err, null);
+ }
+ });
+
+ getMoreCommand = null;
+ } catch(err) {
+ var handleClose = function() {
+ callback(err, null);
+ };
+
+ self.close(handleClose);
+ handleClose = null;
+ }
+}
+
+/**
+ * Gets a detailed information about how the query is performed on this cursor and how
+ * long it took the database to process it.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will always be null while the second parameter will be an object containing the details.
+ * @api public
+ */
+Cursor.prototype.explain = function(callback) {
+ var limit = (-1)*Math.abs(this.limitValue);
+
+ // * - **skip** {Number} skip number of documents to skip.
+ // * - **limit** {Number}, limit the number of results to return. -1 has a special meaning and is used by Db.eval. A value of 1 will also be treated as if it were -1.
+ // * - **hint** {Object}, hint force the query to use a specific index.
+ // * - **explain** {Boolean}, explain return the explaination of the query.
+ // * - **slaveOk** {Boolean}, slaveOk, sets the slaveOk flag on the query wire protocol for secondaries.
+ // * - **snapshot** {Boolean}, snapshot Snapshot mode assures no duplicates are returned.
+ // * - **timeout** {Boolean}, timeout allow the query to timeout.
+ // * - **tailable** {Boolean}, tailable allow the cursor to be tailable.
+ // * - **awaitdata** {Boolean}, awaitdata allow the cursor to wait for data, only applicable for tailable cursor.
+ // * - **batchSize** {Number}, batchSize the number of the subset of results to request the database to return for every request. This should initially be greater than 1 otherwise the database will automatically close the cursor. The batch size can be set to 1 with cursorInstance.batchSize after performing the initial query to the database.
+ // * - **raw** {Boolean}, raw return all query documents as raw buffers (default false).
+ // * - **read** {Boolean}, read specify override of read from source (primary/secondary).
+ // * - **returnKey** {Boolean}, returnKey only return the index key.
+ // * - **maxScan** {Number}, maxScan limit the number of items to scan.
+ // * - **min** {Number}, min set index bounds.
+ // * - **max** {Number}, max set index bounds.
+ // * - **showDiskLoc** {Boolean}, showDiskLoc show disk location of results.
+ // * - **comment** {String}, comment you can put a $comment field on a query to make looking in the profiler logs simpler.
+ // * - **numberOfRetries** {Number}, numberOfRetries if using awaidata specifies the number of times to retry on timeout.
+ // * - **dbName** {String}, dbName override the default dbName.
+ // * - **tailableRetryInterval** {Number}, tailableRetryInterval specify the miliseconds between getMores on tailable cursor.
+ // * - **exhaust** {Boolean}, exhaust have the server send all the documents at once as getMore packets.
+ // * - **partial** {Boolean}, partial have the sharded system return a partial result from mongos.
+
+ // * - **sort** {Array | Object}, set to sort the documents coming back from the query. Array of indexes, [['a', 1]] etc.
+
+// function Cursor(db, collection, selector, fields, skip, limit
+// - , sort, hint, explain, snapshot, timeout, tailable, batchSize, slaveOk, raw, read
+// - , returnKey, maxScan, min, max, showDiskLoc, comment, awaitdata, numberOfRetries, dbName, tailableRetry
+
+ // Create a new cursor and fetch the plan
+ var cursor = new Cursor(this.db, this.collection, this.selector, this.fields, {
+ skip: this.skipValue
+ , limit:limit
+ , sort: this.sortValue
+ , hint: this.hint
+ , explain: true
+ , snapshot: this.snapshot
+ , timeout: this.timeout
+ , tailable: this.tailable
+ , batchSize: this.batchSizeValue
+ , slaveOk: this.slaveOk
+ , raw: this.raw
+ , read: this.read
+ , returnKey: this.returnKey
+ , maxScan: this.maxScan
+ , min: this.min
+ , max: this.max
+ , showDiskLoc: this.showDiskLoc
+ , comment: this.comment
+ , awaitdata: this.awaitdata
+ , numberOfRetries: this.numberOfRetries
+ , dbName: this.dbName
+ });
+
+ // Fetch the explaination document
+ cursor.nextObject(function(err, item) {
+ if(err != null) return callback(err, null);
+ // close the cursor
+ cursor.close(function(err, result) {
+ if(err != null) return callback(err, null);
+ callback(null, item);
+ });
+ });
+};
+
+/**
+ * @ignore
+ */
+Cursor.prototype.streamRecords = function(options) {
+ console.log("[WARNING] streamRecords method is deprecated, please use stream method which is much faster");
+ var args = Array.prototype.slice.call(arguments, 0);
+ options = args.length ? args.shift() : {};
+
+ var
+ self = this,
+ stream = new process.EventEmitter(),
+ recordLimitValue = this.limitValue || 0,
+ emittedRecordCount = 0,
+ queryCommand = generateQueryCommand(self);
+
+ // see http://www.mongodb.org/display/DOCS/Mongo+Wire+Protocol
+ queryCommand.numberToReturn = options.fetchSize ? options.fetchSize : 500;
+ // Execute the query
+ execute(queryCommand);
+
+ function execute(command) {
+ self.db._executeQueryCommand(command, {exhaust: self.exhaust, read:self.read, raw:self.raw, connection:self.connection}, function(err,result) {
+ if(err) {
+ stream.emit('error', err);
+ self.close(function(){});
+ return;
+ }
+
+ if (!self.queryRun && result) {
+ self.queryRun = true;
+ self.cursorId = result.cursorId;
+ self.state = Cursor.OPEN;
+ self.getMoreCommand = new GetMoreCommand(self.db, self.collectionName, queryCommand.numberToReturn, result.cursorId);
+ }
+
+ var resflagsMap = {
+ CursorNotFound:1<<0,
+ QueryFailure:1<<1,
+ ShardConfigStale:1<<2,
+ AwaitCapable:1<<3
+ };
+
+ if(result.documents && result.documents.length && !(result.responseFlag & resflagsMap.QueryFailure)) {
+ try {
+ result.documents.forEach(function(doc){
+ if(recordLimitValue && emittedRecordCount>=recordLimitValue) {
+ throw("done");
+ }
+ emittedRecordCount++;
+ stream.emit('data', doc);
+ });
+ } catch(err) {
+ if (err != "done") { throw err; }
+ else {
+ self.close(function(){
+ stream.emit('end', recordLimitValue);
+ });
+ self.close(function(){});
+ return;
+ }
+ }
+ // rinse & repeat
+ execute(self.getMoreCommand);
+ } else {
+ self.close(function(){
+ stream.emit('end', recordLimitValue);
+ });
+ }
+ });
+ }
+
+ return stream;
+};
+
+/**
+ * Returns a Node ReadStream interface for this cursor.
+ *
+ * @return {CursorStream} returns a stream object.
+ * @api public
+ */
+Cursor.prototype.stream = function stream () {
+ return new CursorStream(this);
+}
+
+/**
+ * Close the cursor.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will always contain null while the second parameter will contain a reference to this cursor.
+ * @return {null}
+ * @api public
+ */
+Cursor.prototype.close = function(callback) {
+ var self = this
+ this.getMoreTimer && clearTimeout(this.getMoreTimer);
+ // Close the cursor if not needed
+ if(this.cursorId instanceof Long && this.cursorId.greaterThan(Long.fromInt(0))) {
+ try {
+ var command = new KillCursorCommand(this.db, [this.cursorId]);
+ // Added an empty callback to ensure we don't throw any null exceptions
+ this.db._executeQueryCommand(command, {read:self.read, raw:self.raw, connection:self.connection}, function() {});
+ } catch(err) {}
+ }
+
+ // Null out the connection
+ self.connection = null;
+ // Reset cursor id
+ this.cursorId = Long.fromInt(0);
+ // Set to closed status
+ this.state = Cursor.CLOSED;
+
+ if(callback) {
+ callback(null, self);
+ self.items = [];
+ }
+
+ return this;
+};
+
+/**
+ * Check if the cursor is closed or open.
+ *
+ * @return {Boolean} returns the state of the cursor.
+ * @api public
+ */
+Cursor.prototype.isClosed = function() {
+ return this.state == Cursor.CLOSED ? true : false;
+};
+
+/**
+ * Init state
+ *
+ * @classconstant INIT
+ **/
+Cursor.INIT = 0;
+
+/**
+ * Cursor open
+ *
+ * @classconstant OPEN
+ **/
+Cursor.OPEN = 1;
+
+/**
+ * Cursor closed
+ *
+ * @classconstant CLOSED
+ **/
+Cursor.CLOSED = 2;
+
+/**
+ * @ignore
+ * @api private
+ */
+exports.Cursor = Cursor;
\ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/cursorstream.js b/node_modules/mongodb/lib/mongodb/cursorstream.js
new file mode 100644
index 0000000..27f2f41
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/cursorstream.js
@@ -0,0 +1,151 @@
+/**
+ * Module dependecies.
+ */
+var Stream = require('stream').Stream;
+
+/**
+ * CursorStream
+ *
+ * Returns a stream interface for the **cursor**.
+ *
+ * Events
+ * - **data** {function(item) {}} the data event triggers when a document is ready.
+ * - **error** {function(err) {}} the error event triggers if an error happens.
+ * - **close** {function() {}} the end event triggers when there is no more documents available.
+ *
+ * @class Represents a CursorStream.
+ * @param {Cursor} cursor a cursor object that the stream wraps.
+ * @return {Stream}
+ */
+function CursorStream(cursor) {
+ if(!(this instanceof CursorStream)) return new CursorStream(cursor);
+
+ Stream.call(this);
+
+ this.readable = true;
+ this.paused = false;
+ this._cursor = cursor;
+ this._destroyed = null;
+
+ // give time to hook up events
+ var self = this;
+ process.nextTick(function () {
+ self._init();
+ });
+}
+
+/**
+ * Inherit from Stream
+ * @ignore
+ * @api private
+ */
+CursorStream.prototype.__proto__ = Stream.prototype;
+
+/**
+ * Flag stating whether or not this stream is readable.
+ */
+CursorStream.prototype.readable;
+
+/**
+ * Flag stating whether or not this stream is paused.
+ */
+CursorStream.prototype.paused;
+
+/**
+ * Initialize the cursor.
+ * @ignore
+ * @api private
+ */
+CursorStream.prototype._init = function () {
+ if (this._destroyed) return;
+ this._next();
+}
+
+/**
+ * Pull the next document from the cursor.
+ * @ignore
+ * @api private
+ */
+CursorStream.prototype._next = function () {
+ if (this.paused || this._destroyed) return;
+
+ var self = this;
+
+ // nextTick is necessary to avoid stack overflows when
+ // dealing with large result sets.
+ process.nextTick(function () {
+ self._cursor.nextObject(function (err, doc) {
+ self._onNextObject(err, doc);
+ });
+ });
+}
+
+/**
+ * Handle each document as its returned from the cursor.
+ * @ignore
+ * @api private
+ */
+CursorStream.prototype._onNextObject = function (err, doc) {
+ if (err) return this.destroy(err);
+
+ // when doc is null we hit the end of the cursor
+ if (!doc) {
+ this.emit('end')
+ return this.destroy();
+ }
+
+ this.emit('data', doc);
+ this._next();
+}
+
+/**
+ * Pauses the stream.
+ *
+ * @api public
+ */
+CursorStream.prototype.pause = function () {
+ this.paused = true;
+}
+
+/**
+ * Resumes the stream.
+ *
+ * @api public
+ */
+CursorStream.prototype.resume = function () {
+ var self = this;
+
+ // Don't do anything if we are not paused
+ if(!this.paused) return;
+
+ process.nextTick(function() {
+ self.paused = false;
+ self._next();
+ })
+}
+
+/**
+ * Destroys the stream, closing the underlying
+ * cursor. No more events will be emitted.
+ *
+ * @api public
+ */
+CursorStream.prototype.destroy = function (err) {
+ if (this._destroyed) return;
+ this._destroyed = true;
+ this.readable = false;
+
+ this._cursor.close();
+
+ if (err) {
+ this.emit('error', err);
+ }
+
+ this.emit('close');
+}
+
+// TODO - maybe implement the raw option to pass binary?
+//CursorStream.prototype.setEncoding = function () {
+//}
+
+module.exports = exports = CursorStream;
diff --git a/node_modules/mongodb/lib/mongodb/db.js b/node_modules/mongodb/lib/mongodb/db.js
new file mode 100644
index 0000000..25c1ad9
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/db.js
@@ -0,0 +1,2205 @@
+/**
+ * Module dependencies.
+ * @ignore
+ */
+var QueryCommand = require('./commands/query_command').QueryCommand,
+ DbCommand = require('./commands/db_command').DbCommand,
+ MongoReply = require('./responses/mongo_reply').MongoReply,
+ Admin = require('./admin').Admin,
+ Collection = require('./collection').Collection,
+ Server = require('./connection/server').Server,
+ ReplSet = require('./connection/repl_set').ReplSet,
+ ReadPreference = require('./connection/read_preference').ReadPreference,
+ Mongos = require('./connection/mongos').Mongos,
+ Cursor = require('./cursor').Cursor,
+ EventEmitter = require('events').EventEmitter,
+ inherits = require('util').inherits,
+ crypto = require('crypto'),
+ parse = require('./connection/url_parser').parse;
+
+/**
+ * Internal class for callback storage
+ * @ignore
+ */
+var CallbackStore = function() {
+ // Make class an event emitter
+ EventEmitter.call(this);
+ // Add a info about call variable
+ this._notReplied = {};
+}
+
+/**
+ * @ignore
+ */
+inherits(CallbackStore, EventEmitter);
+
+/**
+ * Create a new Db instance.
+ *
+ * Options
+ * - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **readPreference** {String}, the prefered read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
+ * - **native_parser** {Boolean, default:false}, use c++ bson parser.
+ * - **forceServerObjectId** {Boolean, default:false}, force server to create _id fields instead of client.
+ * - **pkFactory** {Object}, object overriding the basic ObjectID primary key generation.
+ * - **serializeFunctions** {Boolean, default:false}, serialize functions.
+ * - **raw** {Boolean, default:false}, peform operations using raw bson buffers.
+ * - **recordQueryStats** {Boolean, default:false}, record query statistics during execution.
+ * - **retryMiliSeconds** {Number, default:5000}, number of miliseconds between retries.
+ * - **numberOfRetries** {Number, default:5}, number of retries off connection.
+ * - **logger** {Object, default:null}, an object representing a logger that you want to use, needs to support functions debug, log, error **({error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}})**.
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @class Represents a Db
+ * @param {String} databaseName name of the database.
+ * @param {Object} serverConfig server config object.
+ * @param {Object} [options] additional options for the collection.
+ */
+function Db(databaseName, serverConfig, options) {
+ if(!(this instanceof Db)) return new Db(databaseName, serverConfig, options);
+
+ EventEmitter.call(this);
+ this.databaseName = databaseName;
+ this.serverConfig = serverConfig;
+ this.options = options == null ? {} : options;
+ // State to check against if the user force closed db
+ this._applicationClosed = false;
+ // Fetch the override flag if any
+ var overrideUsedFlag = this.options['override_used_flag'] == null ? false : this.options['override_used_flag'];
+
+ // Verify that nobody is using this config
+ if(!overrideUsedFlag && this.serverConfig != null && typeof this.serverConfig == 'object' && this.serverConfig._isUsed && this.serverConfig._isUsed()) {
+ throw new Error("A Server or ReplSet instance cannot be shared across multiple Db instances");
+ } else if(!overrideUsedFlag && typeof this.serverConfig == 'object'){
+ // Set being used
+ this.serverConfig._used = true;
+ }
+
+ // Ensure we have a valid db name
+ validateDatabaseName(databaseName);
+
+ // Contains all the connections for the db
+ try {
+ this.native_parser = this.options.native_parser;
+ // The bson lib
+ var bsonLib = this.bsonLib = this.options.native_parser ? require('bson').BSONNative : require('bson').BSONPure;
+ // Fetch the serializer object
+ var BSON = bsonLib.BSON;
+ // Create a new instance
+ this.bson = new BSON([bsonLib.Long, bsonLib.ObjectID, bsonLib.Binary, bsonLib.Code, bsonLib.DBRef, bsonLib.Symbol, bsonLib.Double, bsonLib.Timestamp, bsonLib.MaxKey, bsonLib.MinKey]);
+ // Backward compatibility to access types
+ this.bson_deserializer = bsonLib;
+ this.bson_serializer = bsonLib;
+ } catch (err) {
+ // If we tried to instantiate the native driver
+ var msg = "Native bson parser not compiled, please compile "
+ + "or avoid using native_parser=true";
+ throw Error(msg);
+ }
+
+ // Internal state of the server
+ this._state = 'disconnected';
+
+ this.pkFactory = this.options.pk == null ? bsonLib.ObjectID : this.options.pk;
+ this.forceServerObjectId = this.options.forceServerObjectId != null ? this.options.forceServerObjectId : false;
+
+ // Added safe
+ this.safe = this.options.safe == null ? false : this.options.safe;
+
+ // If we have not specified a "safe mode" we just print a warning to the console
+ if(this.options.safe == null && this.options.w == null && this.options.journal == null && this.options.fsync == null) {
+ console.log("========================================================================================");
+ console.log("= Please ensure that you set the default write concern for the database by setting =");
+ console.log("= one of the options =");
+ console.log("= =");
+ console.log("= w: (value of > -1 or the string 'majority'), where < 1 means =");
+ console.log("= no write acknowlegement =");
+ console.log("= journal: true/false, wait for flush to journal before acknowlegement =");
+ console.log("= fsync: true/false, wait for flush to file system before acknowlegement =");
+ console.log("= =");
+ console.log("= For backward compatibility safe is still supported and =");
+ console.log("= allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}] =");
+ console.log("= the default value is false which means the driver receives does not =");
+ console.log("= return the information of the success/error of the insert/update/remove =");
+ console.log("= =");
+ console.log("= ex: new Db(new Server('localhost', 27017), {safe:false}) =");
+ console.log("= =");
+ console.log("= http://www.mongodb.org/display/DOCS/getLastError+Command =");
+ console.log("= =");
+ console.log("= The default of no acknowlegement will change in the very near future =");
+ console.log("= =");
+ console.log("= This message will disappear when the default safe is set on the driver Db =");
+ console.log("========================================================================================");
+ }
+
+ // Internal states variables
+ this.notReplied ={};
+ this.isInitializing = true;
+ this.auths = [];
+ this.openCalled = false;
+
+ // Command queue, keeps a list of incoming commands that need to be executed once the connection is up
+ this.commands = [];
+
+ // Contains all the callbacks
+ this._callBackStore = new CallbackStore();
+
+ // Set up logger
+ this.logger = this.options.logger != null
+ && (typeof this.options.logger.debug == 'function')
+ && (typeof this.options.logger.error == 'function')
+ && (typeof this.options.logger.log == 'function')
+ ? this.options.logger : {error:function(message, object) {}, log:function(message, object) {}, debug:function(message, object) {}};
+ // Allow slaveOk
+ this.slaveOk = this.options["slave_ok"] == null ? false : this.options["slave_ok"];
+
+ var self = this;
+ // Associate the logger with the server config
+ this.serverConfig.logger = this.logger;
+ this.tag = new Date().getTime();
+ // Just keeps list of events we allow
+ this.eventHandlers = {error:[], parseError:[], poolReady:[], message:[], close:[]};
+
+ // Controls serialization options
+ this.serializeFunctions = this.options.serializeFunctions != null ? this.options.serializeFunctions : false;
+
+ // Raw mode
+ this.raw = this.options.raw != null ? this.options.raw : false;
+
+ // Record query stats
+ this.recordQueryStats = this.options.recordQueryStats != null ? this.options.recordQueryStats : false;
+
+ // If we have server stats let's make sure the driver objects have it enabled
+ if(this.recordQueryStats == true) {
+ this.serverConfig.enableRecordQueryStats(true);
+ }
+
+ // Retry information
+ this.retryMiliSeconds = this.options.retryMiliSeconds != null ? this.options.retryMiliSeconds : 1000;
+ this.numberOfRetries = this.options.numberOfRetries != null ? this.options.numberOfRetries : 60;
+
+ // Set default read preference if any
+ this.readPreference = this.options.readPreference;
+};
+
+/**
+ * @ignore
+ */
+function validateDatabaseName(databaseName) {
+ if(typeof databaseName !== 'string') throw new Error("database name must be a string");
+ if(databaseName.length === 0) throw new Error("database name cannot be the empty string");
+
+ var invalidChars = [" ", ".", "$", "/", "\\"];
+ for(var i = 0; i < invalidChars.length; i++) {
+ if(databaseName.indexOf(invalidChars[i]) != -1) throw new Error("database names cannot contain the character '" + invalidChars[i] + "'");
+ }
+}
+
+/**
+ * @ignore
+ */
+inherits(Db, EventEmitter);
+
+/**
+ * Initialize the database connection.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the index information or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.open = function(callback) {
+ var self = this;
+
+ // Check that the user has not called this twice
+ if(this.openCalled) {
+ // Close db
+ this.close();
+ // Throw error
+ throw new Error("db object already connecting, open cannot be called multiple times");
+ }
+
+ // If we have a specified read preference
+ if(this.readPreference != null) this.serverConfig.setReadPreference(this.readPreference);
+
+ // Set that db has been opened
+ this.openCalled = true;
+ // Set the status of the server
+ self._state = 'connecting';
+ // Set up connections
+ if(self.serverConfig instanceof Server || self.serverConfig instanceof ReplSet || self.serverConfig instanceof Mongos) {
+ self.serverConfig.connect(self, {firstCall: true}, function(err, result) {
+ if(err != null) {
+ // Set that db has been closed
+ self.openCalled = false;
+ // Return error from connection
+ return callback(err, null);
+ }
+ // Set the status of the server
+ self._state = 'connected';
+ // Callback
+ return callback(null, self);
+ });
+ } else {
+ return callback(Error("Server parameter must be of type Server, ReplSet or Mongos"), null);
+ }
+};
+
+/**
+ * Create a new Db instance sharing the current socket connections.
+ *
+ * @param {String} dbName the name of the database we want to use.
+ * @return {Db} a db instance using the new database.
+ * @api public
+ */
+Db.prototype.db = function(dbName) {
+ // Copy the options and add out internal override of the not shared flag
+ var options = {};
+ for(var key in this.options) {
+ options[key] = this.options[key];
+ }
+ // Add override flag
+ options['override_used_flag'] = true;
+ // Create a new db instance
+ var newDbInstance = new Db(dbName, this.serverConfig, options);
+ //copy over any auths, we may need them for reconnecting
+ if (this.serverConfig.db) {
+ newDbInstance.auths = this.serverConfig.db.auths;
+ }
+ // Add the instance to the list of approved db instances
+ var allServerInstances = this.serverConfig.allServerInstances();
+ // Add ourselves to all server callback instances
+ for(var i = 0; i < allServerInstances.length; i++) {
+ var server = allServerInstances[i];
+ server.dbInstances.push(newDbInstance);
+ }
+ // Return new db object
+ return newDbInstance;
+}
+
+/**
+ * Close the current db connection, including all the child db instances. Emits close event if no callback is provided.
+ *
+ * @param {Boolean} [forceClose] connection can never be reused.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.close = function(forceClose, callback) {
+ var self = this;
+ // Ensure we force close all connections
+ this._applicationClosed = false;
+
+ if(typeof forceClose == 'function') {
+ callback = forceClose;
+ } else if(typeof forceClose == 'boolean') {
+ this._applicationClosed = forceClose;
+ }
+
+ // Remove all listeners and close the connection
+ this.serverConfig.close(function(err, result) {
+ // Emit the close event
+ if(typeof callback !== 'function') self.emit("close");
+
+ // Emit close event across all db instances sharing the sockets
+ var allServerInstances = self.serverConfig.allServerInstances();
+ // Fetch the first server instance
+ if(Array.isArray(allServerInstances) && allServerInstances.length > 0) {
+ var server = allServerInstances[0];
+ // For all db instances signal all db instances
+ if(Array.isArray(server.dbInstances) && server.dbInstances.length > 1) {
+ for(var i = 0; i < server.dbInstances.length; i++) {
+ var dbInstance = server.dbInstances[i];
+ // Check if it's our current db instance and skip if it is
+ if(dbInstance.databaseName !== self.databaseName && dbInstance.tag !== self.tag) {
+ server.dbInstances[i].emit("close");
+ }
+ }
+ }
+ }
+
+ // Remove all listeners
+ self.removeAllEventListeners();
+ // You can reuse the db as everything is shut down
+ self.openCalled = false;
+ // If we have a callback call it
+ if(callback) callback(err, result);
+ });
+};
+
+/**
+ * Access the Admin database
+ *
+ * @param {Function} [callback] returns the results.
+ * @return {Admin} the admin db object.
+ * @api public
+ */
+Db.prototype.admin = function(callback) {
+ if(callback == null) return new Admin(this);
+ callback(null, new Admin(this));
+};
+
+/**
+ * Returns a cursor to all the collection information.
+ *
+ * @param {String} [collectionName] the collection name we wish to retrieve the information from.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the options or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.collectionsInfo = function(collectionName, callback) {
+ if(callback == null && typeof collectionName == 'function') { callback = collectionName; collectionName = null; }
+ // Create selector
+ var selector = {};
+ // If we are limiting the access to a specific collection name
+ if(collectionName != null) selector.name = this.databaseName + "." + collectionName;
+
+ // Return Cursor
+ // callback for backward compatibility
+ if(callback) {
+ callback(null, new Cursor(this, new Collection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector));
+ } else {
+ return new Cursor(this, new Collection(this, DbCommand.SYSTEM_NAMESPACE_COLLECTION), selector);
+ }
+};
+
+/**
+ * Get the list of all collection names for the specified db
+ *
+ * Options
+ * - **namesOnly** {String, default:false}, Return only the full collection namespace.
+ *
+ * @param {String} [collectionName] the collection name we wish to filter by.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the collection names or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.collectionNames = function(collectionName, options, callback) {
+ var self = this;
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ collectionName = args.length ? args.shift() : null;
+ options = args.length ? args.shift() : {};
+
+ // Ensure no breaking behavior
+ if(collectionName != null && typeof collectionName == 'object') {
+ options = collectionName;
+ collectionName = null;
+ }
+
+ // Let's make our own callback to reuse the existing collections info method
+ self.collectionsInfo(collectionName, function(err, cursor) {
+ if(err != null) return callback(err, null);
+
+ cursor.toArray(function(err, documents) {
+ if(err != null) return callback(err, null);
+
+ // List of result documents that have been filtered
+ var filtered_documents = documents.filter(function(document) {
+ return !(document.name.indexOf(self.databaseName) == -1 || document.name.indexOf('$') != -1);
+ });
+
+ // If we are returning only the names
+ if(options.namesOnly) {
+ filtered_documents = filtered_documents.map(function(document) { return document.name });
+ }
+
+ // Return filtered items
+ callback(null, filtered_documents);
+ });
+ });
+};
+
+/**
+ * Fetch a specific collection (containing the actual collection information)
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document.
+ * - **raw** {Boolean, default:false}, perform all operations using raw bson objects.
+ * - **pkFactory** {Object}, object overriding the basic ObjectID primary key generation.
+ * - **readPreference** {String}, the prefered read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
+ * - **strict**, (Boolean, default:false) throws and error if collection already exists
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ *
+ * @param {String} collectionName the collection name we wish to access.
+ * @param {Object} [options] returns option results.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the collection or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.collection = function(collectionName, options, callback) {
+ var self = this;
+ if(typeof options === "function") { callback = options; options = {}; }
+ // Execute safe
+
+ if(options && (options.strict)) {
+ self.collectionNames(collectionName, function(err, collections) {
+ if(err != null) return callback(err, null);
+
+ if(collections.length == 0) {
+ return callback(new Error("Collection " + collectionName + " does not exist. Currently in safe mode."), null);
+ } else {
+ try {
+ var collection = new Collection(self, collectionName, self.pkFactory, options);
+ } catch(err) {
+ return callback(err, null);
+ }
+ return callback(null, collection);
+ }
+ });
+ } else {
+ try {
+ var collection = new Collection(self, collectionName, self.pkFactory, options);
+ } catch(err) {
+ if(callback == null) {
+ throw err;
+ } else {
+ return callback(err, null);
+ }
+ }
+
+ // If we have no callback return collection object
+ return callback == null ? collection : callback(null, collection);
+ }
+};
+
+/**
+ * Fetch all collections for the current db.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the collections or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.collections = function(callback) {
+ var self = this;
+ // Let's get the collection names
+ self.collectionNames(function(err, documents) {
+ if(err != null) return callback(err, null);
+ var collections = [];
+ documents.forEach(function(document) {
+ collections.push(new Collection(self, document.name.replace(self.databaseName + ".", ''), self.pkFactory));
+ });
+ // Return the collection objects
+ callback(null, collections);
+ });
+};
+
+/**
+ * Evaluate javascript on the server
+ *
+ * Options
+ * - **nolock** {Boolean, default:false}, Tell MongoDB not to block on the evaulation of the javascript.
+ *
+ * @param {Code} code javascript to execute on server.
+ * @param {Object|Array} [parameters] the parameters for the call.
+ * @param {Object} [options] the options
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from eval or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.eval = function(code, parameters, options, callback) {
+ // Unpack calls
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ parameters = args.length ? args.shift() : parameters;
+ options = args.length ? args.shift() : {};
+
+ var finalCode = code;
+ var finalParameters = [];
+ // If not a code object translate to one
+ if(!(finalCode instanceof this.bsonLib.Code)) {
+ finalCode = new this.bsonLib.Code(finalCode);
+ }
+
+ // Ensure the parameters are correct
+ if(parameters != null && parameters.constructor != Array && typeof parameters !== 'function') {
+ finalParameters = [parameters];
+ } else if(parameters != null && parameters.constructor == Array && typeof parameters !== 'function') {
+ finalParameters = parameters;
+ }
+
+ // Create execution selector
+ var selector = {'$eval':finalCode, 'args':finalParameters};
+ // Check if the nolock parameter is passed in
+ if(options['nolock']) {
+ selector['nolock'] = options['nolock'];
+ }
+
+ // Set primary read preference
+ options.readPreference = ReadPreference.PRIMARY;
+
+ // Execute the eval
+ this.collection(DbCommand.SYSTEM_COMMAND_COLLECTION).findOne(selector, options, function(err, result) {
+ if(err) return callback(err);
+
+ if(result && result.ok == 1) {
+ callback(null, result.retval);
+ } else if(result) {
+ callback(new Error("eval failed: " + result.errmsg), null); return;
+ } else {
+ callback(err, result);
+ }
+ });
+};
+
+/**
+ * Dereference a dbref, against a db
+ *
+ * @param {DBRef} dbRef db reference object we wish to resolve.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from dereference or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.dereference = function(dbRef, callback) {
+ var db = this;
+ // If we have a db reference then let's get the db first
+ if(dbRef.db != null) db = this.db(dbRef.db);
+ // Fetch the collection and find the reference
+ var collection = db.collection(dbRef.namespace);
+ collection.findOne({'_id':dbRef.oid}, function(err, result) {
+ callback(err, result);
+ });
+};
+
+/**
+ * Logout user from server, fire off on all connections and remove all auth info
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from logout or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.logout = function(options, callback) {
+ var self = this;
+ // Unpack calls
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ // Number of connections we need to logout from
+ var numberOfConnections = this.serverConfig.allRawConnections().length;
+
+ // Let's generate the logout command object
+ var logoutCommand = DbCommand.logoutCommand(self, {logout:1}, options);
+ self._executeQueryCommand(logoutCommand, {onAll:true}, function(err, result) {
+ // Count down
+ numberOfConnections = numberOfConnections - 1;
+ // Work around the case where the number of connections are 0
+ if(numberOfConnections <= 0 && typeof callback == 'function') {
+ var internalCallback = callback;
+ callback = null;
+ // Reset auth
+ self.auths = [];
+ // Handle any errors
+ if(err == null && result.documents[0].ok == 1) {
+ internalCallback(null, true);
+ } else {
+ err != null ? internalCallback(err, false) : internalCallback(new Error(result.documents[0].errmsg), false);
+ }
+ }
+ });
+}
+
+/**
+ * Authenticate a user against the server.
+ *
+ * Options
+ * - **authdb** {String}, The database that the credentials are for,
+ * different from the name of the current DB, for example admin
+ * @param {String} username username.
+ * @param {String} password password.
+ * @param {Object} [options] the options
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from authentication or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.authenticate = function(username, password, options, callback) {
+ var self = this;
+
+ if (typeof callback === 'undefined') {
+ callback = options;
+ options = {};
+ }
+ // the default db to authenticate against is 'this'
+ // if authententicate is called from a retry context, it may be another one, like admin
+ var authdb = options.authdb ? options.authdb : self.databaseName;
+ // Push the new auth if we have no previous record
+
+ var numberOfConnections = 0;
+ var errorObject = null;
+
+ if(options['connection' != null]) {
+ //if a connection was explicitly passed on options, then we have only one...
+ numberOfConnections = 1;
+ } else {
+ // Get the amount of connections in the pool to ensure we have authenticated all comments
+ numberOfConnections = this.serverConfig.allRawConnections().length;
+ options['onAll'] = true;
+ }
+
+ // Execute all four
+ this._executeQueryCommand(DbCommand.createGetNonceCommand(self), options, function(err, result, connection) {
+ // Execute on all the connections
+ if(err == null) {
+ // Nonce used to make authentication request with md5 hash
+ var nonce = result.documents[0].nonce;
+ // Execute command
+ self._executeQueryCommand(DbCommand.createAuthenticationCommand(self, username, password, nonce, authdb), {connection:connection}, function(err, result) {
+ // Count down
+ numberOfConnections = numberOfConnections - 1;
+ // Ensure we save any error
+ if(err) {
+ errorObject = err;
+ } else if(result.documents[0].err != null || result.documents[0].errmsg != null){
+ errorObject = self.wrap(result.documents[0]);
+ }
+
+ // Work around the case where the number of connections are 0
+ if(numberOfConnections <= 0 && typeof callback == 'function') {
+ var internalCallback = callback;
+ callback = null;
+
+ if(errorObject == null && result.documents[0].ok == 1) {
+ // We authenticated correctly save the credentials
+ self.auths = [{'username':username, 'password':password, 'authdb': authdb}];
+ // Return callback
+ internalCallback(errorObject, true);
+ } else {
+ internalCallback(errorObject, false);
+ }
+ }
+ });
+ }
+ });
+};
+
+/**
+ * Add a user to the database.
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {String} username username.
+ * @param {String} password password.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from addUser or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.addUser = function(username, password, options, callback) {
+ var self = this;
+ var args = Array.prototype.slice.call(arguments, 2);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ // Figure out the safe mode settings
+ var safe = self.safe != null && self.safe == false ? {w: 1} : self.safe;
+ // Override with options passed in if applicable
+ safe = options != null && options['safe'] != null ? options['safe'] : safe;
+ // Ensure it's at least set to safe
+ safe = safe == null ? {w: 1} : safe;
+ // Use node md5 generator
+ var md5 = crypto.createHash('md5');
+ // Generate keys used for authentication
+ md5.update(username + ":mongo:" + password);
+ var userPassword = md5.digest('hex');
+ // Fetch a user collection
+ var collection = this.collection(DbCommand.SYSTEM_USER_COLLECTION);
+ // Check if we are inserting the first user
+ collection.count({}, function(err, count) {
+ // We got an error (f.ex not authorized)
+ if(err != null) return callback(err, null);
+ // Check if the user exists and update i
+ collection.find({user: username}, {dbName: options['dbName']}).toArray(function(err, documents) {
+ // We got an error (f.ex not authorized)
+ if(err != null) return callback(err, null);
+ // Add command keys
+ var commandOptions = safe;
+ commandOptions.dbName = options['dbName'];
+ commandOptions.upsert = true;
+ // We have a user, let's update the password or upsert if not
+ collection.update({user: username},{$set: {user: username, pwd: userPassword}}, commandOptions, function(err, results) {
+ if(count == 0 && err) {
+ callback(null, [{user:username, pwd:userPassword}]);
+ } else if(err) {
+ callback(err, null)
+ } else {
+ callback(null, [{user:username, pwd:userPassword}]);
+ }
+ });
+ });
+ });
+};
+
+/**
+ * Remove a user from a database
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {String} username username.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from removeUser or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.removeUser = function(username, options, callback) {
+ var self = this;
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ // Figure out the safe mode settings
+ var safe = self.safe != null && self.safe == false ? {w: 1} : self.safe;
+ // Override with options passed in if applicable
+ safe = options != null && options['safe'] != null ? options['safe'] : safe;
+ // Ensure it's at least set to safe
+ safe = safe == null ? {w: 1} : safe;
+
+ // Fetch a user collection
+ var collection = this.collection(DbCommand.SYSTEM_USER_COLLECTION);
+ collection.findOne({user: username}, {dbName: options['dbName']}, function(err, user) {
+ if(user != null) {
+ // Add command keys
+ var commandOptions = safe;
+ commandOptions.dbName = options['dbName'];
+
+ collection.remove({user: username}, commandOptions, function(err, result) {
+ callback(err, true);
+ });
+ } else {
+ callback(err, false);
+ }
+ });
+};
+
+/**
+ * Creates a collection on a server pre-allocating space, need to create f.ex capped collections.
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **serializeFunctions** {Boolean, default:false}, serialize functions on the document.
+ * - **raw** {Boolean, default:false}, perform all operations using raw bson objects.
+ * - **pkFactory** {Object}, object overriding the basic ObjectID primary key generation.
+ * - **capped** {Boolean, default:false}, create a capped collection.
+ * - **size** {Number}, the size of the capped collection in bytes.
+ * - **max** {Number}, the maximum number of documents in the capped collection.
+ * - **autoIndexId** {Boolean, default:true}, create an index on the _id field of the document, True by default on MongoDB 2.2 or higher off for version < 2.2.
+ * - **readPreference** {String}, the prefered read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
+ * - **strict**, (Boolean, default:false) throws and error if collection already exists
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {String} collectionName the collection name we wish to access.
+ * @param {Object} [options] returns option results.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from createCollection or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.createCollection = function(collectionName, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ options = args.length ? args.shift() : null;
+ var self = this;
+
+ // Figure out the safe mode settings
+ var safe = self.safe != null && self.safe == false ? {w: 1} : self.safe;
+ // Override with options passed in if applicable
+ safe = options != null && options['safe'] != null ? options['safe'] : safe;
+ // Ensure it's at least set to safe
+ safe = safe == null ? {w: 1} : safe;
+
+ // Check if we have the name
+ this.collectionNames(collectionName, function(err, collections) {
+ if(err != null) return callback(err, null);
+
+ var found = false;
+ collections.forEach(function(collection) {
+ if(collection.name == self.databaseName + "." + collectionName) found = true;
+ });
+
+ // If the collection exists either throw an exception (if db in safe mode) or return the existing collection
+ if(found && options && options.strict) {
+ return callback(new Error("Collection " + collectionName + " already exists. Currently in safe mode."), null);
+ } else if(found){
+ try {
+ var collection = new Collection(self, collectionName, self.pkFactory, options);
+ } catch(err) {
+ return callback(err, null);
+ }
+ return callback(null, collection);
+ }
+
+ // Create a new collection and return it
+ self._executeQueryCommand(DbCommand.createCreateCollectionCommand(self, collectionName, options), {read:false, safe:safe}, function(err, result) {
+ var document = result.documents[0];
+ // If we have no error let's return the collection
+ if(err == null && document.ok == 1) {
+ try {
+ var collection = new Collection(self, collectionName, self.pkFactory, options);
+ } catch(err) {
+ return callback(err, null);
+ }
+ return callback(null, collection);
+ } else {
+ err != null ? callback(err, null) : callback(self.wrap(document), null);
+ }
+ });
+ });
+};
+
+/**
+ * Execute a command hash against MongoDB. This lets you acess any commands not available through the api on the server.
+ *
+ * @param {Object} selector the command hash to send to the server, ex: {ping:1}.
+ * @param {Function} callback this will be called after executing this method. The command always return the whole result of the command as the second parameter.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.command = function(selector, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ // Set up the options
+ var cursor = new Cursor(this
+ , new Collection(this, DbCommand.SYSTEM_COMMAND_COLLECTION), selector, {}, {
+ limit: -1, timeout: QueryCommand.OPTS_NO_CURSOR_TIMEOUT, dbName: options['dbName']
+ });
+
+ // Set read preference if we set one
+ var readPreference = options['readPreference'] ? options['readPreference'] : false;
+
+ // Ensure only commands who support read Prefrences are exeuted otherwise override and use Primary
+ if(readPreference != false) {
+ if(selector['group'] || selector['aggregate'] || selector['collStats'] || selector['dbStats']
+ || selector['count'] || selector['distinct'] || selector['geoNear'] || selector['geoSearch'] || selector['geoWalk']
+ || (selector['mapreduce'] && selector.out == 'inline')) {
+ // Set the read preference
+ cursor.setReadPreference(readPreference);
+ } else {
+ cursor.setReadPreference(ReadPreference.PRIMARY);
+ }
+ }
+
+ // Return the next object
+ cursor.nextObject(callback);
+};
+
+/**
+ * Drop a collection from the database, removing it permanently. New accesses will create a new collection.
+ *
+ * @param {String} collectionName the name of the collection we wish to drop.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from dropCollection or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.dropCollection = function(collectionName, callback) {
+ var self = this;
+
+ // Drop the collection
+ this._executeQueryCommand(DbCommand.createDropCollectionCommand(this, collectionName), function(err, result) {
+ if(err == null && result.documents[0].ok == 1) {
+ if(callback != null) return callback(null, true);
+ } else {
+ if(callback != null) err != null ? callback(err, null) : callback(self.wrap(result.documents[0]), null);
+ }
+ });
+};
+
+/**
+ * Rename a collection.
+ *
+ * @param {String} fromCollection the name of the current collection we wish to rename.
+ * @param {String} toCollection the new name of the collection.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from renameCollection or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.renameCollection = function(fromCollection, toCollection, callback) {
+ var self = this;
+
+ // Execute the command, return the new renamed collection if successful
+ this._executeQueryCommand(DbCommand.createRenameCollectionCommand(this, fromCollection, toCollection), function(err, result) {
+ if(err == null && result.documents[0].ok == 1) {
+ if(callback != null) return callback(null, new Collection(self, toCollection, self.pkFactory));
+ } else {
+ if(callback != null) err != null ? callback(err, null) : callback(self.wrap(result.documents[0]), null);
+ }
+ });
+};
+
+/**
+ * Return last error message for the given connection, note options can be combined.
+ *
+ * Options
+ * - **fsync** {Boolean, default:false}, option forces the database to fsync all files before returning.
+ * - **j** {Boolean, default:false}, awaits the journal commit before returning, > MongoDB 2.0.
+ * - **w** {Number}, until a write operation has been replicated to N servers.
+ * - **wtimeout** {Number}, number of miliseconds to wait before timing out.
+ *
+ * Connection Options
+ * - **connection** {Connection}, fire the getLastError down a specific connection.
+ *
+ * @param {Object} [options] returns option results.
+ * @param {Object} [connectionOptions] returns option results.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from lastError or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.lastError = function(options, connectionOptions, callback) {
+ // Unpack calls
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+ connectionOptions = args.length ? args.shift() : {};
+
+ this._executeQueryCommand(DbCommand.createGetLastErrorCommand(options, this), connectionOptions, function(err, error) {
+ callback(err, error && error.documents);
+ });
+};
+
+/**
+ * Legacy method calls.
+ *
+ * @ignore
+ * @api private
+ */
+Db.prototype.error = Db.prototype.lastError;
+Db.prototype.lastStatus = Db.prototype.lastError;
+
+/**
+ * Return all errors up to the last time db reset_error_history was called.
+ *
+ * Options
+ * - **connection** {Connection}, fire the getLastError down a specific connection.
+ *
+ * @param {Object} [options] returns option results.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from previousErrors or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.previousErrors = function(options, callback) {
+ // Unpack calls
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ this._executeQueryCommand(DbCommand.createGetPreviousErrorsCommand(this), options, function(err, error) {
+ callback(err, error.documents);
+ });
+};
+
+/**
+ * Runs a command on the database.
+ * @ignore
+ * @api private
+ */
+Db.prototype.executeDbCommand = function(command_hash, options, callback) {
+ if(callback == null) { callback = options; options = {}; }
+ this._executeQueryCommand(DbCommand.createDbSlaveOkCommand(this, command_hash, options), options, callback);
+};
+
+/**
+ * Runs a command on the database as admin.
+ * @ignore
+ * @api private
+ */
+Db.prototype.executeDbAdminCommand = function(command_hash, options, callback) {
+ if(callback == null) { callback = options; options = {}; }
+ this._executeQueryCommand(DbCommand.createAdminDbCommand(this, command_hash), options, callback);
+};
+
+/**
+ * Resets the error history of the mongo instance.
+ *
+ * Options
+ * - **connection** {Connection}, fire the getLastError down a specific connection.
+ *
+ * @param {Object} [options] returns option results.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from resetErrorHistory or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.resetErrorHistory = function(options, callback) {
+ // Unpack calls
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ this._executeQueryCommand(DbCommand.createResetErrorHistoryCommand(this), options, function(err, error) {
+ callback(err, error.documents);
+ });
+};
+
+/**
+ * Creates an index on the collection.
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **unique** {Boolean, default:false}, creates an unique index.
+ * - **sparse** {Boolean, default:false}, creates a sparse index.
+ * - **background** {Boolean, default:false}, creates the index in the background, yielding whenever possible.
+ * - **dropDups** {Boolean, default:false}, a unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
+ * - **min** {Number}, for geospatial indexes set the lower bound for the co-ordinates.
+ * - **max** {Number}, for geospatial indexes set the high bound for the co-ordinates.
+ * - **v** {Number}, specify the format version of the indexes.
+ * - **expireAfterSeconds** {Number}, allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
+ * - **name** {String}, override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ *
+ * @param {String} collectionName name of the collection to create the index on.
+ * @param {Object} fieldOrSpec fieldOrSpec that defines the index.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from createIndex or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.createIndex = function(collectionName, fieldOrSpec, options, callback) {
+ var self = this;
+ var args = Array.prototype.slice.call(arguments, 2);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+ options = typeof callback === 'function' ? options : callback;
+ options = options == null ? {} : options;
+
+ // Get the error options
+ var errorOptions = _getWriteConcern(this, options, callback);
+ // Create command
+ var command = DbCommand.createCreateIndexCommand(this, collectionName, fieldOrSpec, options);
+ // Default command options
+ var commandOptions = {};
+
+ // If we have error conditions set handle them
+ if(_hasWriteConcern(errorOptions) && typeof callback == 'function') {
+ // Insert options
+ commandOptions['read'] = false;
+ // If we have safe set set async to false
+ if(errorOptions == null) commandOptions['async'] = true;
+
+ // Set safe option
+ commandOptions['safe'] = errorOptions;
+ // If we have an error option
+ if(typeof errorOptions == 'object') {
+ var keys = Object.keys(errorOptions);
+ for(var i = 0; i < keys.length; i++) {
+ commandOptions[keys[i]] = errorOptions[keys[i]];
+ }
+ }
+
+ // Execute insert command
+ this._executeInsertCommand(command, commandOptions, function(err, result) {
+ if(err != null) return callback(err, null);
+
+ result = result && result.documents;
+ if (result[0].err) {
+ callback(self.wrap(result[0]));
+ } else {
+ callback(null, command.documents[0].name);
+ }
+ });
+ } else if(_hasWriteConcern(errorOptions) && callback == null) {
+ throw new Error("Cannot use a writeConcern without a provided callback");
+ } else {
+ // Execute insert command
+ var result = this._executeInsertCommand(command, commandOptions);
+ // If no callback just return
+ if(!callback) return;
+ // If error return error
+ if(result instanceof Error) {
+ return callback(result);
+ }
+ // Otherwise just return
+ return callback(null, null);
+ }
+};
+
+/**
+ * Ensures that an index exists, if it does not it creates it
+ *
+ * Options
+* - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **unique** {Boolean, default:false}, creates an unique index.
+ * - **sparse** {Boolean, default:false}, creates a sparse index.
+ * - **background** {Boolean, default:false}, creates the index in the background, yielding whenever possible.
+ * - **dropDups** {Boolean, default:false}, a unique index cannot be created on a key that has pre-existing duplicate values. If you would like to create the index anyway, keeping the first document the database indexes and deleting all subsequent documents that have duplicate value
+ * - **min** {Number}, for geospatial indexes set the lower bound for the co-ordinates.
+ * - **max** {Number}, for geospatial indexes set the high bound for the co-ordinates.
+ * - **v** {Number}, specify the format version of the indexes.
+ * - **expireAfterSeconds** {Number}, allows you to expire data on indexes applied to a data (MongoDB 2.2 or higher)
+ * - **name** {String}, override the autogenerated index name (useful if the resulting name is larger than 128 bytes)
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @param {String} collectionName name of the collection to create the index on.
+ * @param {Object} fieldOrSpec fieldOrSpec that defines the index.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from ensureIndex or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.ensureIndex = function(collectionName, fieldOrSpec, options, callback) {
+ var self = this;
+
+ if (typeof callback === 'undefined' && typeof options === 'function') {
+ callback = options;
+ options = {};
+ }
+
+ if (options == null) {
+ options = {};
+ }
+
+ // Get the error options
+ var errorOptions = _getWriteConcern(this, options, callback);
+ // Make sure we don't try to do a write concern without a callback
+ if(_hasWriteConcern(errorOptions) && callback == null)
+ throw new Error("Cannot use a writeConcern without a provided callback");
+ // Create command
+ var command = DbCommand.createCreateIndexCommand(this, collectionName, fieldOrSpec, options);
+ var index_name = command.documents[0].name;
+
+ // Default command options
+ var commandOptions = {};
+ // Check if the index allready exists
+ this.indexInformation(collectionName, function(err, collectionInfo) {
+ if(err != null) return callback(err, null);
+
+ if(!collectionInfo[index_name]) {
+ // If we have error conditions set handle them
+ if(_hasWriteConcern(errorOptions) && typeof callback == 'function') {
+ // Insert options
+ commandOptions['read'] = false;
+ // If we have safe set set async to false
+ if(errorOptions == null) commandOptions['async'] = true;
+
+ // If we have an error option
+ if(typeof errorOptions == 'object') {
+ var keys = Object.keys(errorOptions);
+ for(var i = 0; i < keys.length; i++) {
+ commandOptions[keys[i]] = errorOptions[keys[i]];
+ }
+ }
+
+ if(typeof callback === 'function'
+ && commandOptions.w < 1 && !commandOptions.fsync && !commandOptions.journal) {
+ commandOptions.w = 1;
+ }
+
+ self._executeInsertCommand(command, commandOptions, function(err, result) {
+ // Only callback if we have one specified
+ if(typeof callback === 'function') {
+ if(err != null) return callback(err, null);
+
+ result = result && result.documents;
+ if (result[0].err) {
+ callback(self.wrap(result[0]));
+ } else {
+ callback(null, command.documents[0].name);
+ }
+ }
+ });
+ } else {
+ // Execute insert command
+ var result = self._executeInsertCommand(command, commandOptions);
+ // If no callback just return
+ if(!callback) return;
+ // If error return error
+ if(result instanceof Error) {
+ return callback(result);
+ }
+ // Otherwise just return
+ return callback(null, index_name);
+ }
+ } else {
+ if(typeof callback === 'function') return callback(null, index_name);
+ }
+ });
+};
+
+/**
+ * Returns the information available on allocated cursors.
+ *
+ * Options
+ * - **readPreference** {String}, the prefered read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
+ *
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from cursorInfo or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.cursorInfo = function(options, callback) {
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+
+ this._executeQueryCommand(DbCommand.createDbSlaveOkCommand(this, {'cursorInfo':1}), options, function(err, result) {
+ callback(err, result.documents[0]);
+ });
+};
+
+/**
+ * Drop an index on a collection.
+ *
+ * @param {String} collectionName the name of the collection where the command will drop an index.
+ * @param {String} indexName name of the index to drop.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from dropIndex or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.dropIndex = function(collectionName, indexName, callback) {
+ this._executeQueryCommand(DbCommand.createDropIndexCommand(this, collectionName, indexName), callback);
+};
+
+/**
+ * Reindex all indexes on the collection
+ * Warning: reIndex is a blocking operation (indexes are rebuilt in the foreground) and will be slow for large collections.
+ *
+ * @param {String} collectionName the name of the collection.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from reIndex or null if an error occured.
+ * @api public
+**/
+Db.prototype.reIndex = function(collectionName, callback) {
+ this._executeQueryCommand(DbCommand.createReIndexCommand(this, collectionName), function(err, result) {
+ if(err != null) {
+ callback(err, false);
+ } else if(result.documents[0].errmsg == null) {
+ callback(null, true);
+ } else {
+ callback(new Error(result.documents[0].errmsg), false);
+ }
+ });
+};
+
+/**
+ * Retrieves this collections index info.
+ *
+ * Options
+ * - **full** {Boolean, default:false}, returns the full raw index information.
+ * - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {String} collectionName the name of the collection.
+ * @param {Object} [options] additional options during update.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from indexInformation or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.indexInformation = function(collectionName, options, callback) {
+ if(typeof callback === 'undefined') {
+ if(typeof options === 'undefined') {
+ callback = collectionName;
+ collectionName = null;
+ } else {
+ callback = options;
+ }
+ options = {};
+ }
+
+ // If we specified full information
+ var full = options['full'] == null ? false : options['full'];
+ // Build selector for the indexes
+ var selector = collectionName != null ? {ns: (this.databaseName + "." + collectionName)} : {};
+
+ // Set read preference if we set one
+ var readPreference = options['readPreference'] ? options['readPreference'] : ReadPreference.PRIMARY;
+
+ // Iterate through all the fields of the index
+ this.collection(DbCommand.SYSTEM_INDEX_COLLECTION, function(err, collection) {
+ // Perform the find for the collection
+ collection.find(selector).setReadPreference(readPreference).toArray(function(err, indexes) {
+ if(err != null) return callback(err, null);
+ // Contains all the information
+ var info = {};
+
+ // if full defined just return all the indexes directly
+ if(full) return callback(null, indexes);
+
+ // Process all the indexes
+ for(var i = 0; i < indexes.length; i++) {
+ var index = indexes[i];
+ // Let's unpack the object
+ info[index.name] = [];
+ for(var name in index.key) {
+ info[index.name].push([name, index.key[name]]);
+ }
+ }
+
+ // Return all the indexes
+ callback(null, info);
+ });
+ });
+};
+
+/**
+ * Drop a database.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from dropDatabase or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.dropDatabase = function(callback) {
+ var self = this;
+
+ this._executeQueryCommand(DbCommand.createDropDatabaseCommand(this), function(err, result) {
+ if (err == null && result.documents[0].ok == 1) {
+ callback(null, true);
+ } else {
+ if (err) {
+ callback(err, false);
+ } else {
+ callback(self.wrap(result.documents[0]), false);
+ }
+ }
+ });
+};
+
+/**
+ * Get all the db statistics.
+ *
+ * Options
+ * - **scale** {Number}, divide the returned sizes by scale value.
+ * - **readPreference** {String}, the preferred read preference ((Server.PRIMARY, Server.PRIMARY_PREFERRED, Server.SECONDARY, Server.SECONDARY_PREFERRED, Server.NEAREST).
+ *
+ * @param {Objects} [options] options for the stats command
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from stats or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.prototype.stats = function stats(options, callback) {
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ // Fetch all commands
+ options = args.length ? args.shift() : {};
+
+ // Build command object
+ var commandObject = {
+ dbStats:this.collectionName,
+ }
+
+ // Check if we have the scale value
+ if(options['scale'] != null) commandObject['scale'] = options['scale'];
+
+ // Execute the command
+ this.command(commandObject, options, callback);
+}
+
+/**
+ * Register a handler
+ * @ignore
+ * @api private
+ */
+Db.prototype._registerHandler = function(db_command, raw, connection, exhaust, callback) {
+ // If we have an array of commands, chain them
+ var chained = Array.isArray(db_command);
+
+ // Check if we have exhausted
+ if(typeof exhaust == 'function') {
+ callback = exhaust;
+ exhaust = false;
+ }
+
+ // If they are chained we need to add a special handler situation
+ if(chained) {
+ // List off chained id's
+ var chainedIds = [];
+ // Add all id's
+ for(var i = 0; i < db_command.length; i++) chainedIds.push(db_command[i].getRequestId().toString());
+ // Register all the commands together
+ for(var i = 0; i < db_command.length; i++) {
+ var command = db_command[i];
+ // Add the callback to the store
+ this._callBackStore.once(command.getRequestId(), callback);
+ // Add the information about the reply
+ this._callBackStore._notReplied[command.getRequestId().toString()] = {start: new Date().getTime(), 'raw': raw, chained:chainedIds, connection:connection, exhaust:false};
+ }
+ } else {
+ // Add the callback to the list of handlers
+ this._callBackStore.once(db_command.getRequestId(), callback);
+ // Add the information about the reply
+ this._callBackStore._notReplied[db_command.getRequestId().toString()] = {start: new Date().getTime(), 'raw': raw, connection:connection, exhaust:exhaust};
+ }
+}
+
+/**
+ * Re-Register a handler, on the cursor id f.ex
+ * @ignore
+ * @api private
+ */
+Db.prototype._reRegisterHandler = function(newId, object, callback) {
+ // Add the callback to the list of handlers
+ this._callBackStore.once(newId, object.callback.listener);
+ // Add the information about the reply
+ this._callBackStore._notReplied[newId] = object.info;
+}
+
+/**
+ *
+ * @ignore
+ * @api private
+ */
+Db.prototype._callHandler = function(id, document, err) {
+ // If there is a callback peform it
+ if(this._callBackStore.listeners(id).length >= 1) {
+ // Get info object
+ var info = this._callBackStore._notReplied[id];
+ // Delete the current object
+ delete this._callBackStore._notReplied[id];
+ // Emit to the callback of the object
+ this._callBackStore.emit(id, err, document, info.connection);
+ }
+}
+
+/**
+ *
+ * @ignore
+ * @api private
+ */
+Db.prototype._hasHandler = function(id) {
+ // If there is a callback peform it
+ return this._callBackStore.listeners(id).length >= 1;
+}
+
+/**
+ *
+ * @ignore
+ * @api private
+ */
+Db.prototype._removeHandler = function(id) {
+ // Remove the information
+ if(this._callBackStore._notReplied[id] != null) delete this._callBackStore._notReplied[id];
+ // Remove the callback if it's registered
+ this._callBackStore.removeAllListeners(id);
+ // Force cleanup _events, node.js seems to set it as a null value
+ if(this._callBackStore._events != null) delete this._callBackStore._events[id];
+}
+
+/**
+ *
+ * @ignore
+ * @api private
+ */
+Db.prototype._findHandler = function(id) {
+ var info = this._callBackStore._notReplied[id];
+ // Return the callback
+ return {info:info, callback:(this._callBackStore.listeners(id).length >= 1) ? this._callBackStore.listeners(id)[0] : null}
+}
+
+/**
+ * @ignore
+ */
+var __executeQueryCommand = function(self, db_command, options, callback) {
+ // Options unpacking
+ var read = options['read'] != null ? options['read'] : false;
+ var raw = options['raw'] != null ? options['raw'] : self.raw;
+ var onAll = options['onAll'] != null ? options['onAll'] : false;
+ var specifiedConnection = options['connection'] != null ? options['connection'] : null;
+
+ // Correct read preference to default primary if set to false, null or primary
+ if(!(typeof read == 'object') && read._type == 'ReadPreference') {
+ read = (read == null || read == 'primary' || read == false) ? ReadPreference.PRIMARY : read;
+ if(!ReadPreference.isValid(read)) return callback(new Error("Illegal readPreference mode specified, " + read));
+ } else if(typeof read == 'object' && read._type == 'ReadPreference') {
+ if(!read.isValid()) return callback(new Error("Illegal readPreference mode specified, " + read.mode));
+ }
+
+ // If we have a read preference set and we are a mongos pass the read preference on to the mongos instance,
+ if(self.serverConfig.isMongos() && read != null && read != false) {
+ db_command.setMongosReadPreference(read);
+ }
+
+ // If we got a callback object
+ if(typeof callback === 'function' && !onAll) {
+ // Override connection if we passed in a specific connection
+ var connection = specifiedConnection != null ? specifiedConnection : null;
+ // connection = connection != null && connection.connected != null ? connection : null;
+
+ if(connection instanceof Error) return callback(connection, null);
+
+ // Fetch either a reader or writer dependent on the specified read option if no connection
+ // was passed in
+ if(connection == null) {
+ connection = read == null || read == 'primary' || read == false ? self.serverConfig.checkoutWriter(true) : self.serverConfig.checkoutReader(read);
+ }
+
+ // Ensure we have a valid connection
+ if(connection == null) {
+ return callback(new Error("no open connections"));
+ } else if(connection instanceof Error || connection['message'] != null) {
+ return callback(connection);
+ }
+
+ // Exhaust Option
+ var exhaust = options.exhaust || false;
+
+ // Register the handler in the data structure
+ self._registerHandler(db_command, raw, connection, exhaust, callback);
+
+ // Write the message out and handle any errors if there are any
+ connection.write(db_command, function(err) {
+ if(err != null) {
+ // Call the handler with an error
+ self._callHandler(db_command.getRequestId(), null, err);
+ }
+ });
+ } else if(typeof callback === 'function' && onAll) {
+ var connections = self.serverConfig.allRawConnections();
+ var numberOfEntries = connections.length;
+ // Go through all the connections
+ for(var i = 0; i < connections.length; i++) {
+ // Fetch a connection
+ var connection = connections[i];
+ // Override connection if needed
+ connection = specifiedConnection != null ? specifiedConnection : connection;
+ // Ensure we have a valid connection
+ if(connection == null) {
+ return callback(new Error("no open connections"));
+ } else if(connection instanceof Error) {
+ return callback(connection);
+ }
+
+ // Register the handler in the data structure
+ self._registerHandler(db_command, raw, connection, callback);
+
+ // Write the message out
+ connection.write(db_command, function(err) {
+ // Adjust the number of entries we need to process
+ numberOfEntries = numberOfEntries - 1;
+ // Remove listener
+ if(err != null) {
+ // Clean up listener and return error
+ self._removeHandler(db_command.getRequestId());
+ }
+
+ // No more entries to process callback with the error
+ if(numberOfEntries <= 0) {
+ callback(err);
+ }
+ });
+
+ // Update the db_command request id
+ db_command.updateRequestId();
+ }
+ } else {
+ // Fetch either a reader or writer dependent on the specified read option
+ var connection = read == null || read == 'primary' || read == false ? self.serverConfig.checkoutWriter(true) : self.serverConfig.checkoutReader(read);
+ // Override connection if needed
+ connection = specifiedConnection != null ? specifiedConnection : connection;
+ // Ensure we have a valid connection
+ if(connection == null || connection instanceof Error || connection['message'] != null) return null;
+ // Write the message out
+ connection.write(db_command, function(err) {
+ if(err != null) {
+ // Emit the error
+ self.emit("error", err);
+ }
+ });
+ }
+}
+
+/**
+ * @ignore
+ */
+var __retryCommandOnFailure = function(self, retryInMilliseconds, numberOfTimes, command, db_command, options, callback) {
+ if(this._state == 'connected' || this._state == 'disconnected') this._state = 'connecting';
+ // Number of retries done
+ var numberOfRetriesDone = numberOfTimes;
+ // Retry function, execute once
+ var retryFunction = function(_self, _numberOfRetriesDone, _retryInMilliseconds, _numberOfTimes, _command, _db_command, _options, _callback) {
+ _self.serverConfig.connect(_self, {}, function(err, result, _serverConfig) {
+ // Adjust the number of retries left
+ _numberOfRetriesDone = _numberOfRetriesDone - 1;
+ // Definitively restart
+ if(err != null && _numberOfRetriesDone > 0) {
+ _self._state = 'connecting';
+ // Close the server config
+ _serverConfig.close(function(err) {
+ // Retry the connect
+ setTimeout(function() {
+ retryFunction(_self, _numberOfRetriesDone, _retryInMilliseconds, _numberOfTimes, _command, _db_command, _options, _callback);
+ }, _retryInMilliseconds);
+ });
+ } else if(err != null && _numberOfRetriesDone <= 0) {
+ _self._state = 'disconnected';
+ // Force close the current connections
+ _serverConfig.close(function(_err) {
+ // Force close the current connections
+ if(typeof _callback == 'function') _callback(err, null);
+ });
+ } else if(err == null && _self.serverConfig.isConnected() == true && Array.isArray(_self.auths) && _self.auths.length > 0) {
+ _self._state = 'connected';
+ // Get number of auths we need to execute
+ var numberOfAuths = _self.auths.length;
+ // Apply all auths
+ for(var i = 0; i < _self.auths.length; i++) {
+ _self.authenticate(_self.auths[i].username, _self.auths[i].password, {'authdb':_self.auths[i].authdb}, function(err, authenticated) {
+ numberOfAuths = numberOfAuths - 1;
+
+ // If we have no more authentications to replay
+ if(numberOfAuths == 0) {
+ if(err != null || !authenticated) {
+ if(typeof _callback == 'function') _callback(err, null);
+ return;
+ } else {
+ // Execute command
+ command(_self, _db_command, _options, _callback);
+
+ // Execute any backed up commands
+ process.nextTick(function() {
+ // Execute any backed up commands
+ while(_self.commands.length > 0) {
+ // Fetch the command
+ var command = _self.commands.shift();
+ // Execute based on type
+ if(command['type'] == 'query') {
+ __executeQueryCommand(_self, command['db_command'], command['options'], command['callback']);
+ } else if(command['type'] == 'insert') {
+ __executeInsertCommand(_self, command['db_command'], command['options'], command['callback']);
+ }
+ }
+ });
+ }
+ }
+ });
+ }
+ } else if(err == null && _self.serverConfig.isConnected() == true) {
+ _self._state = 'connected';
+ // Execute command
+ command(_self, _db_command, _options, _callback);
+
+ process.nextTick(function() {
+ // Execute any backed up commands
+ while(_self.commands.length > 0) {
+ // Fetch the command
+ var command = _self.commands.shift();
+ // Execute based on type
+ if(command['type'] == 'query') {
+ __executeQueryCommand(_self, command['db_command'], command['options'], command['callback']);
+ } else if(command['type'] == 'insert') {
+ __executeInsertCommand(_self, command['db_command'], command['options'], command['callback']);
+ }
+ }
+ });
+ } else {
+ _self._state = 'connecting';
+ // Force close the current connections
+ _serverConfig.close(function(err) {
+ // _self.serverConfig.close(function(err) {
+ // Retry the connect
+ setTimeout(function() {
+ retryFunction(_self, _numberOfRetriesDone, _retryInMilliseconds, _numberOfTimes, _command, _db_command, _options, _callback);
+ }, _retryInMilliseconds);
+ });
+ }
+ });
+ };
+
+ // Execute function first time
+ retryFunction(self, numberOfRetriesDone, retryInMilliseconds, numberOfTimes, command, db_command, options, callback);
+}
+
+/**
+ * Execute db query command (not safe)
+ * @ignore
+ * @api private
+ */
+Db.prototype._executeQueryCommand = function(db_command, options, callback) {
+ var self = this;
+
+ // Unpack the parameters
+ if (typeof callback === 'undefined') {
+ callback = options;
+ options = {};
+ }
+
+ // fast fail option used for HA, no retry
+ var failFast = options['failFast'] != null
+ ? options['failFast']
+ : false;
+
+ // Check if the user force closed the command
+ if(this._applicationClosed) {
+ var err = new Error("db closed by application");
+ if('function' == typeof callback) {
+ return callback(err, null);
+ } else {
+ throw err;
+ }
+ }
+
+ var config = this.serverConfig;
+ // If the pool is not connected, attemp to reconnect to send the message
+ if(this._state == 'connecting' && config.autoReconnect && !failFast) {
+ return process.nextTick(function() {
+ self.commands.push({
+ type: 'query',
+ db_command: db_command,
+ options: options,
+ callback: callback
+ });
+ })
+ }
+
+ if(!failFast && !config.isConnected(options.read) && config.autoReconnect
+ && (options.read == null
+ || options.read == false
+ || options.read == ReadPreference.PRIMARY
+ || config.checkoutReader() == null)) {
+ this._state = 'connecting';
+ return __retryCommandOnFailure(this,
+ this.retryMiliSeconds,
+ this.numberOfRetries,
+ __executeQueryCommand,
+ db_command,
+ options,
+ callback);
+ }
+
+ if(!config.isConnected(options.read) && !config.autoReconnect && callback) {
+ // Fire an error to the callback if we are not connected
+ // and don't reconnect.
+ return callback(new Error("no open connections"), null);
+ }
+
+ __executeQueryCommand(self, db_command, options, function (err, result, conn) {
+ callback(err, result, conn);
+ });
+
+};
+
+/**
+ * @ignore
+ */
+var __executeInsertCommand = function(self, db_command, options, callback) {
+ // Always checkout a writer for this kind of operations
+ var connection = self.serverConfig.checkoutWriter();
+ // Get safe mode
+ var safe = options['safe'] != null ? options['safe'] : false;
+ var raw = options['raw'] != null ? options['raw'] : self.raw;
+ var specifiedConnection = options['connection'] != null ? options['connection'] : null;
+ // Override connection if needed
+ connection = specifiedConnection != null ? specifiedConnection : connection;
+
+ // Ensure we have a valid connection
+ if(typeof callback === 'function') {
+ // Ensure we have a valid connection
+ if(connection == null) {
+ return callback(new Error("no open connections"));
+ } else if(connection instanceof Error) {
+ return callback(connection);
+ }
+
+ var errorOptions = _getWriteConcern(self, options, callback);
+ if(errorOptions.w > 0 || errorOptions.w == 'majority' || errorOptions.j || errorOptions.journal || errorOptions.fsync) {
+ // db command is now an array of commands (original command + lastError)
+ db_command = [db_command, DbCommand.createGetLastErrorCommand(safe, self)];
+ // Register the handler in the data structure
+ self._registerHandler(db_command[1], raw, connection, callback);
+ }
+ }
+
+ // If we have no callback and there is no connection
+ if(connection == null) return null;
+ if(connection instanceof Error && typeof callback == 'function') return callback(connection, null);
+ if(connection instanceof Error) return null;
+ if(connection == null && typeof callback == 'function') return callback(new Error("no primary server found"), null);
+
+ // Write the message out
+ connection.write(db_command, function(err) {
+ // Return the callback if it's not a safe operation and the callback is defined
+ if(typeof callback === 'function' && (safe == null || safe == false)) {
+ // Perform the callback
+ callback(err, null);
+ } else if(typeof callback === 'function') {
+ // Call the handler with an error
+ self._callHandler(db_command[1].getRequestId(), null, err);
+ } else if(typeof callback == 'function' && safe && safe.w == -1) {
+ // Call the handler with no error
+ self._callHandler(db_command[1].getRequestId(), null, null);
+ } else if(!safe && safe.w == -1) {
+ self.emit("error", err);
+ }
+ });
+}
+
+/**
+ * Execute an insert Command
+ * @ignore
+ * @api private
+ */
+Db.prototype._executeInsertCommand = function(db_command, options, callback) {
+ var self = this;
+
+ // Unpack the parameters
+ if(callback == null && typeof options === 'function') {
+ callback = options;
+ options = {};
+ }
+
+ // Ensure options are not null
+ options = options == null ? {} : options;
+
+ // Check if the user force closed the command
+ if(this._applicationClosed) {
+ if(typeof callback == 'function') {
+ return callback(new Error("db closed by application"), null);
+ } else {
+ throw new Error("db closed by application");
+ }
+ }
+
+ // If the pool is not connected, attemp to reconnect to send the message
+ if(self._state == 'connecting' && this.serverConfig.autoReconnect) {
+ process.nextTick(function() {
+ self.commands.push({type:'insert', 'db_command':db_command, 'options':options, 'callback':callback});
+ })
+ } else if(!this.serverConfig.isConnected() && this.serverConfig.autoReconnect) {
+ this._state = 'connecting';
+ // Retry command
+ __retryCommandOnFailure(this, this.retryMiliSeconds, this.numberOfRetries, __executeInsertCommand, db_command, options, callback);
+ } else if(!this.serverConfig.isConnected() && !this.serverConfig.autoReconnect && callback) {
+ // Fire an error to the callback if we are not connected and don't do reconnect
+ if(callback) callback(new Error("no open connections"), null);
+ } else {
+ __executeInsertCommand(self, db_command, options, callback);
+ }
+}
+
+/**
+ * Update command is the same
+ * @ignore
+ * @api private
+ */
+Db.prototype._executeUpdateCommand = Db.prototype._executeInsertCommand;
+/**
+ * Remove command is the same
+ * @ignore
+ * @api private
+ */
+Db.prototype._executeRemoveCommand = Db.prototype._executeInsertCommand;
+
+/**
+ * Wrap a Mongo error document into an Error instance
+ * @ignore
+ * @api private
+ */
+Db.prototype.wrap = function(error) {
+ var msg = error.err || error.errmsg || error;
+ var e = new Error(msg);
+ e.name = 'MongoError';
+
+ // Get all object keys
+ var keys = Object.keys(error);
+ // Populate error object with properties
+ for(var i = 0; i < keys.length; i++) {
+ e[keys[i]] = error[keys[i]];
+ }
+
+ return e;
+}
+
+/**
+ * Default URL
+ *
+ * @classconstant DEFAULT_URL
+ **/
+Db.DEFAULT_URL = 'mongodb://localhost:27017/default';
+
+/**
+ * Connect to MongoDB using a url as documented at
+ *
+ * www.mongodb.org/display/DOCS/Connections
+ *
+ * Options
+ * - **uri_decode_auth** {Boolean, default:false} uri decode the user name and password for authentication
+ * - **db** {Object, default: null} a hash off options to set on the db object, see **Db constructor**
+ * - **server** {Object, default: null} a hash off options to set on the server objects, see **Server** constructor**
+ * - **replSet** {Object, default: null} a hash off options to set on the replSet object, see **ReplSet** constructor**
+ * - **mongos** {Object, default: null} a hash off options to set on the mongos object, see **Mongos** constructor**
+ *
+ * @param {String} url connection url for MongoDB.
+ * @param {Object} [options] optional options for insert command
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the db instance or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+Db.connect = function(url, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = typeof args[args.length - 1] == 'function' ? args.pop() : null;
+ options = args.length ? args.shift() : null;
+ options = options || {};
+ var serverOptions = options.server || {};
+ var mongosOptions = options.mongos || {};
+ var replSetServersOptions = options.replSet || options.replSetServers || {};
+ var dbOptions = options.db || {};
+
+ // If callback is null throw an exception
+ if(callback == null) throw new Error("no callback function provided");
+
+ // Parse the string
+ var object = parse(url);
+ // Merge in any options for db in options object
+ if(dbOptions) {
+ for(var name in dbOptions) object.db_options[name] = dbOptions[name];
+ }
+
+ // Merge in any options for server in options object
+ if(serverOptions) {
+ for(var name in serverOptions) object.server_options[name] = serverOptions[name];
+ }
+
+ // Merge in any replicaset server options
+ if(replSetServersOptions) {
+ for(var name in replSetServersOptions) object.rs_options[name] = replSetServersOptions[name];
+ }
+
+ // Merge in any replicaset server options
+ if(mongosOptions) {
+ for(var name in mongosOptions) object.mongos_options[name] = mongosOptions[name];
+ }
+
+ // We need to ensure that the list of servers are only either direct members or mongos
+ // they cannot be a mix of monogs and mongod's
+ var totalNumberOfServers = object.servers.length;
+ var totalNumberOfMongosServers = 0;
+ var totalNumberOfMongodServers = 0;
+ var serverConfig = null;
+
+ // Failure modes
+ if(object.servers.length == 0) throw new Error("connection string must contain at least one seed host");
+
+ // If we have no db setting for the native parser try to set the c++ one first
+ object.db_options.native_parser = _setNativeParser(object.db_options);
+ // If no auto_reconnect is set, set it to true as default for single servers
+ if(typeof object.server_options.auto_reconnect != 'boolean') {
+ object.server_options.auto_reconnect = true;
+ }
+
+ // If we have more than a server, it could be replicaset or mongos list
+ // need to verify that it's one or the other and fail if it's a mix
+ // Connect to all servers and run ismaster
+ for(var i = 0; i < object.servers.length; i++) {
+ // Set up the Server object
+ var _server = object.servers[i].domain_socket
+ ? new Server(object.servers[i].domain_socket, {socketOptions:{connectTimeoutMS:1000}, auto_reconnect:false})
+ : new Server(object.servers[i].host, object.servers[i].port, {socketOptions:{connectTimeoutMS:1000}, auto_reconnect:false});
+
+ // Attempt connect
+ new Db(object.dbName, _server, {safe:false, native_parser:false}).open(function(err, db) {
+ // Update number of servers
+ totalNumberOfServers = totalNumberOfServers - 1;
+ // If no error do the correct checks
+ if(!err) {
+ // Close the connection
+ db.close(true);
+ var isMasterDoc = db.serverConfig.isMasterDoc;
+ // Check what type of server we have
+ if(isMasterDoc.setName) totalNumberOfMongodServers++;
+ if(isMasterDoc.msg && isMasterDoc.msg == "isdbgrid") totalNumberOfMongosServers++;
+ }
+
+ if(totalNumberOfServers == 0) {
+ // If we have a mix of mongod and mongos, throw an error
+ if(totalNumberOfMongosServers > 0 && totalNumberOfMongodServers > 0)
+ return callback(new Error("cannot combine a list of replicaset seeds and mongos seeds"));
+
+ if(totalNumberOfMongodServers == 0 && object.servers.length == 1) {
+ var obj = object.servers[0];
+ serverConfig = obj.domain_socket ?
+ new Server(obj.domain_socket, object.server_options)
+ : new Server(obj.host, obj.port, object.server_options);
+ } else if(totalNumberOfMongodServers > 0) {
+ serverConfig = new ReplSet(object.servers.map(function(serverObj) {
+ return new Server(serverObj.host, serverObj.port, object.server_options);
+ }), object.rs_options);
+ } else if(totalNumberOfMongosServers > 0) {
+ serverConfig = new Mongos(object.servers.map(function(serverObj) {
+ return new Server(serverObj.host, serverObj.port, object.server_options);
+ }), object.mongos_options);
+ }
+
+ if(serverConfig == null) return callback(new Error("Could not locate any valid servers in initial seed list"));
+ // Set up all options etc and connect to the database
+ _finishConnecting(serverConfig, object, options, callback)
+ }
+ });
+ }
+}
+
+var _setNativeParser = function(db_options) {
+ if(typeof db_options.native_parser == 'boolean') return db_options.native_parser;
+
+ try {
+ require('bson').BSONNative.BSON;
+ return true;
+ } catch(err) {
+ return false;
+ }
+}
+
+var _finishConnecting = function(serverConfig, object, options, callback) {
+ // Safe settings
+ var safe = {};
+ // Build the safe parameter if needed
+ if(object.db_options.journal) safe.j = object.db_options.journal;
+ if(object.db_options.w) safe.w = object.db_options.w;
+ if(object.db_options.fsync) safe.fsync = object.db_options.fsync;
+ if(object.db_options.wtimeoutMS) safe.wtimeout = object.db_options.wtimeoutMS;
+
+ // If we have a read Preference set
+ if(object.db_options.read_preference) {
+ var readPreference = new ReadPreference(object.db_options.read_preference);
+ // If we have the tags set up
+ if(object.db_options.read_preference_tags)
+ readPreference = new ReadPreference(object.db_options.read_preference, object.db_options.read_preference_tags);
+ // Add the read preference
+ object.db_options.readPreference = readPreference;
+ }
+
+ // No safe mode if no keys
+ if(Object.keys(safe).length == 0) safe = false;
+ // Add the safe object
+ object.db_options.safe = safe;
+ // Set up the db options
+ var db = new Db(object.dbName, serverConfig, object.db_options);
+ // Don't open the connection
+ if(options.noOpen) return db;
+
+ // Open the db
+ db.open(function(err, db){
+ if(err == null && object.auth){
+ db.authenticate(object.auth.user, object.auth.password, function(err, success){
+ if(success){
+ callback(null, db);
+ } else {
+ callback(err ? err : new Error('Could not authenticate user ' + auth[0]), db);
+ }
+ });
+ } else {
+ callback(err, db);
+ }
+ });
+}
+
+/**
+ * State of the db connection
+ * @ignore
+ */
+Object.defineProperty(Db.prototype, "state", { enumerable: true
+ , get: function () {
+ return this.serverConfig._serverState;
+ }
+});
+
+/**
+ * @ignore
+ */
+var _hasWriteConcern = function(errorOptions) {
+ return errorOptions == true
+ || errorOptions.w > 0
+ || errorOptions.w == 'majority'
+ || errorOptions.j == true
+ || errorOptions.journal == true
+ || errorOptions.fsync == true
+}
+
+/**
+ * @ignore
+ */
+var _setWriteConcernHash = function(options) {
+ var finalOptions = {};
+ if(options.w != null) finalOptions.w = options.w;
+ if(options.journal == true) finalOptions.j = options.journal;
+ if(options.j == true) finalOptions.j = options.j;
+ if(options.fsync == true) finalOptions.fsync = options.fsync;
+ if(options.wtimeout != null) finalOptions.wtimeout = options.wtimeout;
+ return finalOptions;
+}
+
+/**
+ * @ignore
+ */
+var _getWriteConcern = function(self, options, callback) {
+ // Final options
+ var finalOptions = {w:1};
+ // Local options verification
+ if(options.w != null || typeof options.j == 'boolean' || typeof options.journal == 'boolean' || typeof options.fsync == 'boolean') {
+ finalOptions = _setWriteConcernHash(options);
+ } else if(options.safe != null && typeof options.safe == 'object') {
+ finalOptions = _setWriteConcernHash(options.safe);
+ } else if(typeof options.safe == "boolean") {
+ finalOptions = {w: (options.safe ? 1 : 0)};
+ } else if(self.options.w != null || typeof self.options.j == 'boolean' || typeof self.options.journal == 'boolean' || typeof self.options.fsync == 'boolean') {
+ finalOptions = _setWriteConcernHash(self.options);
+ } else if(self.safe.w != null || typeof self.safe.j == 'boolean' || typeof self.safe.journal == 'boolean' || typeof self.safe.fsync == 'boolean') {
+ finalOptions = _setWriteConcernHash(self.safe);
+ } else if(typeof self.safe == "boolean") {
+ finalOptions = {w: (self.safe ? 1 : 0)};
+ }
+
+ // Ensure we don't have an invalid combination of write concerns
+ if(finalOptions.w < 1
+ && (finalOptions.journal == true || finalOptions.j == true || finalOptions.fsync == true)) throw new Error("No acknowlegement using w < 1 cannot be combined with journal:true or fsync:true");
+
+ // Return the options
+ return finalOptions;
+}
+
+/**
+ * Legacy support
+ *
+ * @ignore
+ * @api private
+ */
+exports.connect = Db.connect;
+exports.Db = Db;
+
+/**
+ * Remove all listeners to the db instance.
+ * @ignore
+ * @api private
+ */
+Db.prototype.removeAllEventListeners = function() {
+ this.removeAllListeners("close");
+ this.removeAllListeners("error");
+ this.removeAllListeners("timeout");
+ this.removeAllListeners("parseError");
+ this.removeAllListeners("poolReady");
+ this.removeAllListeners("message");
+}
diff --git a/node_modules/mongodb/lib/mongodb/gridfs/chunk.js b/node_modules/mongodb/lib/mongodb/gridfs/chunk.js
new file mode 100644
index 0000000..572d144
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/gridfs/chunk.js
@@ -0,0 +1,213 @@
+var Binary = require('bson').Binary,
+ ObjectID = require('bson').ObjectID;
+
+/**
+ * Class for representing a single chunk in GridFS.
+ *
+ * @class
+ *
+ * @param file {GridStore} The {@link GridStore} object holding this chunk.
+ * @param mongoObject {object} The mongo object representation of this chunk.
+ *
+ * @throws Error when the type of data field for {@link mongoObject} is not
+ * supported. Currently supported types for data field are instances of
+ * {@link String}, {@link Array}, {@link Binary} and {@link Binary}
+ * from the bson module
+ *
+ * @see Chunk#buildMongoObject
+ */
+var Chunk = exports.Chunk = function(file, mongoObject) {
+ if(!(this instanceof Chunk)) return new Chunk(file, mongoObject);
+
+ this.file = file;
+ var self = this;
+ var mongoObjectFinal = mongoObject == null ? {} : mongoObject;
+
+ this.objectId = mongoObjectFinal._id == null ? new ObjectID() : mongoObjectFinal._id;
+ this.chunkNumber = mongoObjectFinal.n == null ? 0 : mongoObjectFinal.n;
+ this.data = new Binary();
+
+ if(mongoObjectFinal.data == null) {
+ } else if(typeof mongoObjectFinal.data == "string") {
+ var buffer = new Buffer(mongoObjectFinal.data.length);
+ buffer.write(mongoObjectFinal.data, 'binary', 0);
+ this.data = new Binary(buffer);
+ } else if(Array.isArray(mongoObjectFinal.data)) {
+ var buffer = new Buffer(mongoObjectFinal.data.length);
+ buffer.write(mongoObjectFinal.data.join(''), 'binary', 0);
+ this.data = new Binary(buffer);
+ } else if(mongoObjectFinal.data instanceof Binary || Object.prototype.toString.call(mongoObjectFinal.data) == "[object Binary]") {
+ this.data = mongoObjectFinal.data;
+ } else if(Buffer.isBuffer(mongoObjectFinal.data)) {
+ } else {
+ throw Error("Illegal chunk format");
+ }
+ // Update position
+ this.internalPosition = 0;
+};
+
+/**
+ * Writes a data to this object and advance the read/write head.
+ *
+ * @param data {string} the data to write
+ * @param callback {function(*, GridStore)} This will be called after executing
+ * this method. The first parameter will contain null and the second one
+ * will contain a reference to this object.
+ */
+Chunk.prototype.write = function(data, callback) {
+ this.data.write(data, this.internalPosition);
+ this.internalPosition = this.data.length();
+ if(callback != null) return callback(null, this);
+ return this;
+};
+
+/**
+ * Reads data and advances the read/write head.
+ *
+ * @param length {number} The length of data to read.
+ *
+ * @return {string} The data read if the given length will not exceed the end of
+ * the chunk. Returns an empty String otherwise.
+ */
+Chunk.prototype.read = function(length) {
+ // Default to full read if no index defined
+ length = length == null || length == 0 ? this.length() : length;
+
+ if(this.length() - this.internalPosition + 1 >= length) {
+ var data = this.data.read(this.internalPosition, length);
+ this.internalPosition = this.internalPosition + length;
+ return data;
+ } else {
+ return '';
+ }
+};
+
+Chunk.prototype.readSlice = function(length) {
+ if ((this.length() - this.internalPosition) >= length) {
+ var data = null;
+ if (this.data.buffer != null) { //Pure BSON
+ data = this.data.buffer.slice(this.internalPosition, this.internalPosition + length);
+ } else { //Native BSON
+ data = new Buffer(length);
+ length = this.data.readInto(data, this.internalPosition);
+ }
+ this.internalPosition = this.internalPosition + length;
+ return data;
+ } else {
+ return null;
+ }
+};
+
+/**
+ * Checks if the read/write head is at the end.
+ *
+ * @return {boolean} Whether the read/write head has reached the end of this
+ * chunk.
+ */
+Chunk.prototype.eof = function() {
+ return this.internalPosition == this.length() ? true : false;
+};
+
+/**
+ * Reads one character from the data of this chunk and advances the read/write
+ * head.
+ *
+ * @return {string} a single character data read if the the read/write head is
+ * not at the end of the chunk. Returns an empty String otherwise.
+ */
+Chunk.prototype.getc = function() {
+ return this.read(1);
+};
+
+/**
+ * Clears the contents of the data in this chunk and resets the read/write head
+ * to the initial position.
+ */
+Chunk.prototype.rewind = function() {
+ this.internalPosition = 0;
+ this.data = new Binary();
+};
+
+/**
+ * Saves this chunk to the database. Also overwrites existing entries having the
+ * same id as this chunk.
+ *
+ * @param callback {function(*, GridStore)} This will be called after executing
+ * this method. The first parameter will contain null and the second one
+ * will contain a reference to this object.
+ */
+Chunk.prototype.save = function(callback) {
+ var self = this;
+
+ self.file.chunkCollection(function(err, collection) {
+ if(err) return callback(err);
+
+ collection.remove({'_id':self.objectId}, {safe:true}, function(err, result) {
+ if(err) return callback(err);
+
+ if(self.data.length() > 0) {
+ self.buildMongoObject(function(mongoObject) {
+ collection.insert(mongoObject, {safe:true}, function(err, collection) {
+ callback(err, self);
+ });
+ });
+ } else {
+ callback(null, self);
+ }
+ });
+ });
+};
+
+/**
+ * Creates a mongoDB object representation of this chunk.
+ *
+ * @param callback {function(Object)} This will be called after executing this
+ * method. The object will be passed to the first parameter and will have
+ * the structure:
+ *
+ *
+ * {
+ * '_id' : , // {number} id for this chunk
+ * 'files_id' : , // {number} foreign key to the file collection
+ * 'n' : , // {number} chunk number
+ * 'data' : , // {bson#Binary} the chunk data itself
+ * }
+ *
+ *
+ * @see MongoDB GridFS Chunk Object Structure
+ */
+Chunk.prototype.buildMongoObject = function(callback) {
+ var mongoObject = {'_id': this.objectId,
+ 'files_id': this.file.fileId,
+ 'n': this.chunkNumber,
+ 'data': this.data};
+ callback(mongoObject);
+};
+
+/**
+ * @return {number} the length of the data
+ */
+Chunk.prototype.length = function() {
+ return this.data.length();
+};
+
+/**
+ * The position of the read/write head
+ * @name position
+ * @lends Chunk#
+ * @field
+ */
+Object.defineProperty(Chunk.prototype, "position", { enumerable: true
+ , get: function () {
+ return this.internalPosition;
+ }
+ , set: function(value) {
+ this.internalPosition = value;
+ }
+});
+
+/**
+ * The default chunk size
+ * @constant
+ */
+Chunk.DEFAULT_CHUNK_SIZE = 1024 * 256;
diff --git a/node_modules/mongodb/lib/mongodb/gridfs/grid.js b/node_modules/mongodb/lib/mongodb/gridfs/grid.js
new file mode 100644
index 0000000..858d1a3
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/gridfs/grid.js
@@ -0,0 +1,98 @@
+var GridStore = require('./gridstore').GridStore,
+ ObjectID = require('bson').ObjectID;
+
+/**
+ * A class representation of a simple Grid interface.
+ *
+ * @class Represents the Grid.
+ * @param {Db} db A database instance to interact with.
+ * @param {String} [fsName] optional different root collection for GridFS.
+ * @return {Grid}
+ */
+function Grid(db, fsName) {
+
+ if(!(this instanceof Grid)) return new Grid(db, fsName);
+
+ this.db = db;
+ this.fsName = fsName == null ? GridStore.DEFAULT_ROOT_COLLECTION : fsName;
+}
+
+/**
+ * Puts binary data to the grid
+ *
+ * @param {Buffer} data buffer with Binary Data.
+ * @param {Object} [options] the options for the files.
+ * @param {Function} callback this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
+ * @return {null}
+ * @api public
+ */
+Grid.prototype.put = function(data, options, callback) {
+ var self = this;
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ options = args.length ? args.shift() : {};
+ // If root is not defined add our default one
+ options['root'] = options['root'] == null ? this.fsName : options['root'];
+
+ // Return if we don't have a buffer object as data
+ if(!(Buffer.isBuffer(data))) return callback(new Error("Data object must be a buffer object"), null);
+ // Get filename if we are using it
+ var filename = options['filename'];
+ // Create gridstore
+ var gridStore = new GridStore(this.db, filename, "w", options);
+ gridStore.open(function(err, gridStore) {
+ if(err) return callback(err, null);
+
+ gridStore.write(data, function(err, result) {
+ if(err) return callback(err, null);
+
+ gridStore.close(function(err, result) {
+ if(err) return callback(err, null);
+ callback(null, result);
+ })
+ })
+ })
+}
+
+/**
+ * Get binary data to the grid
+ *
+ * @param {ObjectID} id ObjectID for file.
+ * @param {Function} callback this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
+ * @return {null}
+ * @api public
+ */
+Grid.prototype.get = function(id, callback) {
+ // Validate that we have a valid ObjectId
+ if(!(id instanceof ObjectID)) return callback(new Error("Not a valid ObjectID", null));
+ // Create gridstore
+ var gridStore = new GridStore(this.db, id, "r", {root:this.fsName});
+ gridStore.open(function(err, gridStore) {
+ if(err) return callback(err, null);
+
+ // Return the data
+ gridStore.read(function(err, data) {
+ return callback(err, data)
+ });
+ })
+}
+
+/**
+ * Delete file from grid
+ *
+ * @param {ObjectID} id ObjectID for file.
+ * @param {Function} callback this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
+ * @return {null}
+ * @api public
+ */
+Grid.prototype.delete = function(id, callback) {
+ // Validate that we have a valid ObjectId
+ if(!(id instanceof ObjectID)) return callback(new Error("Not a valid ObjectID", null));
+ // Create gridstore
+ GridStore.unlink(this.db, id, {root:this.fsName}, function(err, result) {
+ if(err) return callback(err, false);
+ return callback(null, true);
+ });
+}
+
+exports.Grid = Grid;
diff --git a/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js b/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js
new file mode 100644
index 0000000..5fb773f
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js
@@ -0,0 +1,1467 @@
+/**
+ * @fileOverview GridFS is a tool for MongoDB to store files to the database.
+ * Because of the restrictions of the object size the database can hold, a
+ * facility to split a file into several chunks is needed. The {@link GridStore}
+ * class offers a simplified api to interact with files while managing the
+ * chunks of split files behind the scenes. More information about GridFS can be
+ * found here.
+ */
+var Chunk = require('./chunk').Chunk,
+ DbCommand = require('../commands/db_command').DbCommand,
+ ObjectID = require('bson').ObjectID,
+ Buffer = require('buffer').Buffer,
+ fs = require('fs'),
+ util = require('util'),
+ inherits = util.inherits,
+ ReadStream = require('./readstream').ReadStream,
+ Stream = require('stream');
+
+var REFERENCE_BY_FILENAME = 0,
+ REFERENCE_BY_ID = 1;
+
+/**
+ * A class representation of a file stored in GridFS.
+ *
+ * Modes
+ * - **"r"** - read only. This is the default mode.
+ * - **"w"** - write in truncate mode. Existing data will be overwriten.
+ * - **w+"** - write in edit mode.
+ *
+ * Options
+ * - **root** {String}, root collection to use. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**.
+ * - **content_type** {String}, mime type of the file. Defaults to **{GridStore.DEFAULT_CONTENT_TYPE}**.
+ * - **chunk_size** {Number}, size for the chunk. Defaults to **{Chunk.DEFAULT_CHUNK_SIZE}**.
+ * - **metadata** {Object}, arbitrary data the user wants to store.
+ *
+ * @class Represents the GridStore.
+ * @param {Db} db A database instance to interact with.
+ * @param {ObjectID} id an unique ObjectID for this file
+ * @param {String} [filename] optional a filename for this file, no unique constrain on the field
+ * @param {String} mode set the mode for this file.
+ * @param {Object} options optional properties to specify.
+ * @return {GridStore}
+ */
+var GridStore = function GridStore(db, id, filename, mode, options) {
+ if(!(this instanceof GridStore)) return new GridStore(db, id, filename, mode, options);
+
+ var self = this;
+ this.db = db;
+
+ // Call stream constructor
+ if(typeof Stream == 'function') {
+ Stream.call(this);
+ } else {
+ // 0.4.X backward compatibility fix
+ Stream.Stream.call(this);
+ }
+
+ // Handle options
+ if(options == null) options = {};
+ // Handle mode
+ if(mode == null) {
+ mode = filename;
+ filename = null;
+ } else if(typeof mode == 'object') {
+ options = mode;
+ mode = filename;
+ filename = null;
+ }
+
+ // Handle id
+ if(id instanceof ObjectID && (typeof filename == 'string' || filename == null)) {
+ this.referenceBy = 1;
+ this.fileId = id;
+ this.filename = filename;
+ } else if(!(id instanceof ObjectID) && typeof id == 'string' && mode.indexOf("w") != null) {
+ this.referenceBy = 0;
+ this.fileId = new ObjectID();
+ this.filename = id;
+ } else if(!(id instanceof ObjectID) && typeof id == 'string' && mode.indexOf("r") != null) {
+ this.referenceBy = 0;
+ this.filename = filename;
+ } else {
+ this.referenceBy = 1;
+ this.fileId = id;
+ this.filename = filename;
+ }
+
+ // Set up the rest
+ this.mode = mode == null ? "r" : mode;
+ this.options = options == null ? {} : options;
+ this.root = this.options['root'] == null ? exports.GridStore.DEFAULT_ROOT_COLLECTION : this.options['root'];
+ this.position = 0;
+ // Set default chunk size
+ this.internalChunkSize = this.options['chunkSize'] == null ? Chunk.DEFAULT_CHUNK_SIZE : this.options['chunkSize'];
+}
+
+/**
+ * Code for the streaming capabilities of the gridstore object
+ * Most code from Aaron heckmanns project https://github.com/aheckmann/gridfs-stream
+ * Modified to work on the gridstore object itself
+ * @ignore
+ */
+if(typeof Stream == 'function') {
+ GridStore.prototype = { __proto__: Stream.prototype }
+} else {
+ // Node 0.4.X compatibility code
+ GridStore.prototype = { __proto__: Stream.Stream.prototype }
+}
+
+// Move pipe to _pipe
+GridStore.prototype._pipe = GridStore.prototype.pipe;
+
+/**
+ * Opens the file from the database and initialize this object. Also creates a
+ * new one if file does not exist.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain an **{Error}** object and the second parameter will be null if an error occured. Otherwise, the first parameter will be null and the second will contain the reference to this object.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.open = function(callback) {
+ if( this.mode != "w" && this.mode != "w+" && this.mode != "r"){
+ callback(new Error("Illegal mode " + this.mode), null);
+ return;
+ }
+
+ var self = this;
+
+ if((self.mode == "w" || self.mode == "w+") && self.db.serverConfig.primary != null) {
+ // Get files collection
+ self.collection(function(err, collection) {
+ if(err) return callback(err);
+
+ // Put index on filename
+ collection.ensureIndex([['filename', 1]], function(err, index) {
+ if(err) return callback(err);
+
+ // Get chunk collection
+ self.chunkCollection(function(err, chunkCollection) {
+ if(err) return callback(err);
+
+ // Ensure index on chunk collection
+ chunkCollection.ensureIndex([['files_id', 1], ['n', 1]], function(err, index) {
+ if(err) return callback(err);
+ _open(self, callback);
+ });
+ });
+ });
+ });
+ } else {
+ // Open the gridstore
+ _open(self, callback);
+ }
+};
+
+/**
+ * Hidding the _open function
+ * @ignore
+ * @api private
+ */
+var _open = function(self, callback) {
+ self.collection(function(err, collection) {
+ if(err!==null) {
+ callback(new Error("at collection: "+err), null);
+ return;
+ }
+
+ // Create the query
+ var query = self.referenceBy == REFERENCE_BY_ID ? {_id:self.fileId} : {filename:self.filename};
+ query = null == self.fileId && this.filename == null ? null : query;
+
+ // Fetch the chunks
+ if(query != null) {
+ collection.find(query, function(err, cursor) {
+ if(err) return error(err);
+
+ // Fetch the file
+ cursor.nextObject(function(err, doc) {
+ if(err) return error(err);
+
+ // Check if the collection for the files exists otherwise prepare the new one
+ if(doc != null) {
+ self.fileId = doc._id;
+ self.filename = doc.filename;
+ self.contentType = doc.contentType;
+ self.internalChunkSize = doc.chunkSize;
+ self.uploadDate = doc.uploadDate;
+ self.aliases = doc.aliases;
+ self.length = doc.length;
+ self.metadata = doc.metadata;
+ self.internalMd5 = doc.md5;
+ } else if (self.mode != 'r') {
+ self.fileId = self.fileId == null ? new ObjectID() : self.fileId;
+ self.contentType = exports.GridStore.DEFAULT_CONTENT_TYPE;
+ self.internalChunkSize = self.internalChunkSize == null ? Chunk.DEFAULT_CHUNK_SIZE : self.internalChunkSize;
+ self.length = 0;
+ } else {
+ self.length = 0;
+ return error(new Error((self.referenceBy == REFERENCE_BY_ID ? self.fileId.toHexString() : self.filename) + " does not exist", self));
+ }
+
+ // Process the mode of the object
+ if(self.mode == "r") {
+ nthChunk(self, 0, function(err, chunk) {
+ if(err) return error(err);
+ self.currentChunk = chunk;
+ self.position = 0;
+ callback(null, self);
+ });
+ } else if(self.mode == "w") {
+ // Delete any existing chunks
+ deleteChunks(self, function(err, result) {
+ if(err) return error(err);
+ self.currentChunk = new Chunk(self, {'n':0});
+ self.contentType = self.options['content_type'] == null ? self.contentType : self.options['content_type'];
+ self.internalChunkSize = self.options['chunk_size'] == null ? self.internalChunkSize : self.options['chunk_size'];
+ self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata'];
+ self.position = 0;
+ callback(null, self);
+ });
+ } else if(self.mode == "w+") {
+ nthChunk(self, lastChunkNumber(self), function(err, chunk) {
+ if(err) return error(err);
+ // Set the current chunk
+ self.currentChunk = chunk == null ? new Chunk(self, {'n':0}) : chunk;
+ self.currentChunk.position = self.currentChunk.data.length();
+ self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata'];
+ self.position = self.length;
+ callback(null, self);
+ });
+ }
+ });
+ });
+ } else {
+ // Write only mode
+ self.fileId = null == self.fileId ? new ObjectID() : self.fileId;
+ self.contentType = exports.GridStore.DEFAULT_CONTENT_TYPE;
+ self.internalChunkSize = self.internalChunkSize == null ? Chunk.DEFAULT_CHUNK_SIZE : self.internalChunkSize;
+ self.length = 0;
+
+ self.chunkCollection(function(err, collection2) {
+ if(err) return error(err);
+
+ // No file exists set up write mode
+ if(self.mode == "w") {
+ // Delete any existing chunks
+ deleteChunks(self, function(err, result) {
+ if(err) return error(err);
+ self.currentChunk = new Chunk(self, {'n':0});
+ self.contentType = self.options['content_type'] == null ? self.contentType : self.options['content_type'];
+ self.internalChunkSize = self.options['chunk_size'] == null ? self.internalChunkSize : self.options['chunk_size'];
+ self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata'];
+ self.position = 0;
+ callback(null, self);
+ });
+ } else if(self.mode == "w+") {
+ nthChunk(self, lastChunkNumber(self), function(err, chunk) {
+ if(err) return error(err);
+ // Set the current chunk
+ self.currentChunk = chunk == null ? new Chunk(self, {'n':0}) : chunk;
+ self.currentChunk.position = self.currentChunk.data.length();
+ self.metadata = self.options['metadata'] == null ? self.metadata : self.options['metadata'];
+ self.position = self.length;
+ callback(null, self);
+ });
+ }
+ });
+ }
+ });
+
+ // only pass error to callback once
+ function error (err) {
+ if(error.err) return;
+ callback(error.err = err);
+ }
+};
+
+/**
+ * Stores a file from the file system to the GridFS database.
+ *
+ * @param {String|Buffer|FileHandle} file the file to store.
+ * @param {Function} callback this will be called after this method is executed. The first parameter will be null and the the second will contain the reference to this object.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.writeFile = function (file, callback) {
+ var self = this;
+ if (typeof file === 'string') {
+ fs.open(file, 'r', 0666, function (err, fd) {
+ if(err) return callback(err);
+ self.writeFile(fd, callback);
+ });
+ return;
+ }
+
+ self.open(function (err, self) {
+ if(err) return callback(err);
+
+ fs.fstat(file, function (err, stats) {
+ if(err) return callback(err);
+
+ var offset = 0;
+ var index = 0;
+ var numberOfChunksLeft = Math.min(stats.size / self.chunkSize);
+
+ // Write a chunk
+ var writeChunk = function() {
+ fs.read(file, self.chunkSize, offset, 'binary', function(err, data, bytesRead) {
+ if(err) return callback(err);
+
+ offset = offset + bytesRead;
+
+ // Create a new chunk for the data
+ var chunk = new Chunk(self, {n:index++});
+ chunk.write(data, function(err, chunk) {
+ if(err) return callback(err);
+
+ chunk.save(function(err, result) {
+ if(err) return callback(err);
+
+ self.position = self.position + data.length;
+
+ // Point to current chunk
+ self.currentChunk = chunk;
+
+ if(offset >= stats.size) {
+ fs.close(file);
+ self.close(callback);
+ } else {
+ return process.nextTick(writeChunk);
+ }
+ });
+ });
+ });
+ }
+
+ // Process the first write
+ process.nextTick(writeChunk);
+ });
+ });
+};
+
+/**
+ * Writes some data. This method will work properly only if initialized with mode
+ * "w" or "w+".
+ *
+ * @param string {string} The data to write.
+ * @param close {boolean=false} opt_argument Closes this file after writing if
+ * true.
+ * @param callback {function(*, GridStore)} This will be called after executing
+ * this method. The first parameter will contain null and the second one
+ * will contain a reference to this object.
+ *
+ * @ignore
+ * @api private
+ */
+var writeBuffer = function(self, buffer, close, callback) {
+ if(typeof close === "function") { callback = close; close = null; }
+ var finalClose = (close == null) ? false : close;
+
+ if(self.mode[0] != "w") {
+ callback(new Error((self.referenceBy == REFERENCE_BY_ID ? self.toHexString() : self.filename) + " not opened for writing"), null);
+ } else {
+ if(self.currentChunk.position + buffer.length >= self.chunkSize) {
+ // Write out the current Chunk and then keep writing until we have less data left than a chunkSize left
+ // to a new chunk (recursively)
+ var previousChunkNumber = self.currentChunk.chunkNumber;
+ var leftOverDataSize = self.chunkSize - self.currentChunk.position;
+ var firstChunkData = buffer.slice(0, leftOverDataSize);
+ var leftOverData = buffer.slice(leftOverDataSize);
+ // A list of chunks to write out
+ var chunksToWrite = [self.currentChunk.write(firstChunkData)];
+ // If we have more data left than the chunk size let's keep writing new chunks
+ while(leftOverData.length >= self.chunkSize) {
+ // Create a new chunk and write to it
+ var newChunk = new Chunk(self, {'n': (previousChunkNumber + 1)});
+ var firstChunkData = leftOverData.slice(0, self.chunkSize);
+ leftOverData = leftOverData.slice(self.chunkSize);
+ // Update chunk number
+ previousChunkNumber = previousChunkNumber + 1;
+ // Write data
+ newChunk.write(firstChunkData);
+ // Push chunk to save list
+ chunksToWrite.push(newChunk);
+ }
+
+ // Set current chunk with remaining data
+ self.currentChunk = new Chunk(self, {'n': (previousChunkNumber + 1)});
+ // If we have left over data write it
+ if(leftOverData.length > 0) self.currentChunk.write(leftOverData);
+
+ // Update the position for the gridstore
+ self.position = self.position + buffer.length;
+ // Total number of chunks to write
+ var numberOfChunksToWrite = chunksToWrite.length;
+ // Write out all the chunks and then return
+ for(var i = 0; i < chunksToWrite.length; i++) {
+ var chunk = chunksToWrite[i];
+ chunk.save(function(err, result) {
+ if(err) return callback(err);
+
+ numberOfChunksToWrite = numberOfChunksToWrite - 1;
+
+ if(numberOfChunksToWrite <= 0) {
+ return callback(null, self);
+ }
+ })
+ }
+ } else {
+ // Update the position for the gridstore
+ self.position = self.position + buffer.length;
+ // We have less data than the chunk size just write it and callback
+ self.currentChunk.write(buffer);
+ callback(null, self);
+ }
+ }
+};
+
+/**
+ * Creates a mongoDB object representation of this object.
+ *
+ * @param callback {function(object)} This will be called after executing this
+ * method. The object will be passed to the first parameter and will have
+ * the structure:
+ *
+ *
+ * {
+ * '_id' : , // {number} id for this file
+ * 'filename' : , // {string} name for this file
+ * 'contentType' : , // {string} mime type for this file
+ * 'length' : , // {number} size of this file?
+ * 'chunksize' : , // {number} chunk size used by this file
+ * 'uploadDate' : , // {Date}
+ * 'aliases' : , // {array of string}
+ * 'metadata' : , // {string}
+ * }
+ *
+ *
+ * @ignore
+ * @api private
+ */
+var buildMongoObject = function(self, callback) {
+ // // Keeps the final chunk number
+ // var chunkNumber = 0;
+ // var previousChunkSize = 0;
+ // // Get the correct chunk Number, if we have an empty chunk return the previous chunk number
+ // if(null != self.currentChunk && self.currentChunk.chunkNumber > 0 && self.currentChunk.position == 0) {
+ // chunkNumber = self.currentChunk.chunkNumber - 1;
+ // } else {
+ // chunkNumber = self.currentChunk.chunkNumber;
+ // previousChunkSize = self.currentChunk.position;
+ // }
+
+ // // Calcuate the length
+ // var length = self.currentChunk != null ? (chunkNumber * self.chunkSize + previousChunkSize) : 0;
+ var mongoObject = {
+ '_id': self.fileId,
+ 'filename': self.filename,
+ 'contentType': self.contentType,
+ 'length': self.position ? self.position : 0,
+ 'chunkSize': self.chunkSize,
+ 'uploadDate': self.uploadDate,
+ 'aliases': self.aliases,
+ 'metadata': self.metadata
+ };
+
+ var md5Command = {filemd5:self.fileId, root:self.root};
+ self.db.command(md5Command, function(err, results) {
+ mongoObject.md5 = results.md5;
+ callback(mongoObject);
+ });
+};
+
+/**
+ * Saves this file to the database. This will overwrite the old entry if it
+ * already exists. This will work properly only if mode was initialized to
+ * "w" or "w+".
+ *
+ * @param {Function} callback this will be called after executing this method. Passes an **{Error}** object to the first parameter and null to the second if an error occured. Otherwise, passes null to the first and a reference to this object to the second.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.close = function(callback) {
+ var self = this;
+
+ if(self.mode[0] == "w") {
+ if(self.currentChunk != null && self.currentChunk.position > 0) {
+ self.currentChunk.save(function(err, chunk) {
+ if(err) return callback(err);
+
+ self.collection(function(err, files) {
+ if(err) return callback(err);
+
+ // Build the mongo object
+ if(self.uploadDate != null) {
+ files.remove({'_id':self.fileId}, {safe:true}, function(err, collection) {
+ if(err) return callback(err);
+
+ buildMongoObject(self, function(mongoObject) {
+ files.save(mongoObject, {safe:true}, function(err) {
+ callback(err, mongoObject);
+ });
+ });
+ });
+ } else {
+ self.uploadDate = new Date();
+ buildMongoObject(self, function(mongoObject) {
+ files.save(mongoObject, {safe:true}, function(err) {
+ callback(err, mongoObject);
+ });
+ });
+ }
+ });
+ });
+ } else {
+ self.collection(function(err, files) {
+ if(err) return callback(err);
+
+ self.uploadDate = new Date();
+ buildMongoObject(self, function(mongoObject) {
+ files.save(mongoObject, {safe:true}, function(err) {
+ callback(err, mongoObject);
+ });
+ });
+ });
+ }
+ } else if(self.mode[0] == "r") {
+ callback(null, null);
+ } else {
+ callback(new Error("Illegal mode " + self.mode), null);
+ }
+};
+
+/**
+ * Gets the nth chunk of this file.
+ *
+ * @param chunkNumber {number} The nth chunk to retrieve.
+ * @param callback {function(*, Chunk|object)} This will be called after
+ * executing this method. null will be passed to the first parameter while
+ * a new {@link Chunk} instance will be passed to the second parameter if
+ * the chunk was found or an empty object {} if not.
+ *
+ * @ignore
+ * @api private
+ */
+var nthChunk = function(self, chunkNumber, callback) {
+ self.chunkCollection(function(err, collection) {
+ if(err) return callback(err);
+
+ collection.find({'files_id':self.fileId, 'n':chunkNumber}, function(err, cursor) {
+ if(err) return callback(err);
+
+ cursor.nextObject(function(err, chunk) {
+ if(err) return callback(err);
+
+ var finalChunk = chunk == null ? {} : chunk;
+ callback(null, new Chunk(self, finalChunk));
+ });
+ });
+ });
+};
+
+/**
+ *
+ * @ignore
+ * @api private
+ */
+GridStore.prototype._nthChunk = function(chunkNumber, callback) {
+ nthChunk(this, chunkNumber, callback);
+}
+
+/**
+ * @return {Number} The last chunk number of this file.
+ *
+ * @ignore
+ * @api private
+ */
+var lastChunkNumber = function(self) {
+ return Math.floor(self.length/self.chunkSize);
+};
+
+/**
+ * Retrieve this file's chunks collection.
+ *
+ * @param {Function} callback this will be called after executing this method. An exception object will be passed to the first parameter when an error occured or null otherwise. A new **{Collection}** object will be passed to the second parameter if no error occured.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.chunkCollection = function(callback) {
+ this.db.collection((this.root + ".chunks"), callback);
+};
+
+/**
+ * Deletes all the chunks of this file in the database.
+ *
+ * @param callback {function(*, boolean)} This will be called after this method
+ * executes. Passes null to the first and true to the second argument.
+ *
+ * @ignore
+ * @api private
+ */
+var deleteChunks = function(self, callback) {
+ if(self.fileId != null) {
+ self.chunkCollection(function(err, collection) {
+ if(err) return callback(err, false);
+ collection.remove({'files_id':self.fileId}, {safe:true}, function(err, result) {
+ if(err) return callback(err, false);
+ callback(null, true);
+ });
+ });
+ } else {
+ callback(null, true);
+ }
+};
+
+/**
+ * Deletes all the chunks of this file in the database.
+ *
+ * @param {Function} callback this will be called after this method executes. Passes null to the first and true to the second argument.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.unlink = function(callback) {
+ var self = this;
+ deleteChunks(this, function(err) {
+ if(err!==null) {
+ err.message = "at deleteChunks: " + err.message;
+ return callback(err);
+ }
+
+ self.collection(function(err, collection) {
+ if(err!==null) {
+ err.message = "at collection: " + err.message;
+ return callback(err);
+ }
+
+ collection.remove({'_id':self.fileId}, {safe:true}, function(err) {
+ callback(err, self);
+ });
+ });
+ });
+};
+
+/**
+ * Retrieves the file collection associated with this object.
+ *
+ * @param {Function} callback this will be called after executing this method. An exception object will be passed to the first parameter when an error occured or null otherwise. A new **{Collection}** object will be passed to the second parameter if no error occured.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.collection = function(callback) {
+ this.db.collection(this.root + ".files", callback);
+};
+
+/**
+ * Reads the data of this file.
+ *
+ * @param {String} [separator] the character to be recognized as the newline separator.
+ * @param {Function} callback This will be called after this method is executed. The first parameter will be null and the second parameter will contain an array of strings representing the entire data, each element representing a line including the separator character.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.readlines = function(separator, callback) {
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ separator = args.length ? args.shift() : "\n";
+
+ this.read(function(err, data) {
+ if(err) return callback(err);
+
+ var items = data.toString().split(separator);
+ items = items.length > 0 ? items.splice(0, items.length - 1) : [];
+ for(var i = 0; i < items.length; i++) {
+ items[i] = items[i] + separator;
+ }
+
+ callback(null, items);
+ });
+};
+
+/**
+ * Deletes all the chunks of this file in the database if mode was set to "w" or
+ * "w+" and resets the read/write head to the initial position.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.rewind = function(callback) {
+ var self = this;
+
+ if(this.currentChunk.chunkNumber != 0) {
+ if(this.mode[0] == "w") {
+ deleteChunks(self, function(err, gridStore) {
+ if(err) return callback(err);
+ self.currentChunk = new Chunk(self, {'n': 0});
+ self.position = 0;
+ callback(null, self);
+ });
+ } else {
+ self.currentChunk(0, function(err, chunk) {
+ if(err) return callback(err);
+ self.currentChunk = chunk;
+ self.currentChunk.rewind();
+ self.position = 0;
+ callback(null, self);
+ });
+ }
+ } else {
+ self.currentChunk.rewind();
+ self.position = 0;
+ callback(null, self);
+ }
+};
+
+/**
+ * Retrieves the contents of this file and advances the read/write head. Works with Buffers only.
+ *
+ * There are 3 signatures for this method:
+ *
+ * (callback)
+ * (length, callback)
+ * (length, buffer, callback)
+ *
+ * @param {Number} [length] the number of characters to read. Reads all the characters from the read/write head to the EOF if not specified.
+ * @param {String|Buffer} [buffer] a string to hold temporary data. This is used for storing the string data read so far when recursively calling this method.
+ * @param {Function} callback this will be called after this method is executed. null will be passed to the first parameter and a string containing the contents of the buffer concatenated with the contents read from this file will be passed to the second.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.read = function(length, buffer, callback) {
+ var self = this;
+
+ var args = Array.prototype.slice.call(arguments, 0);
+ callback = args.pop();
+ length = args.length ? args.shift() : null;
+ buffer = args.length ? args.shift() : null;
+
+ // The data is a c-terminated string and thus the length - 1
+ var finalLength = length == null ? self.length - self.position : length;
+ var finalBuffer = buffer == null ? new Buffer(finalLength) : buffer;
+ // Add a index to buffer to keep track of writing position or apply current index
+ finalBuffer._index = buffer != null && buffer._index != null ? buffer._index : 0;
+
+ if((self.currentChunk.length() - self.currentChunk.position + finalBuffer._index) >= finalLength) {
+ var slice = self.currentChunk.readSlice(finalLength - finalBuffer._index);
+ // Copy content to final buffer
+ slice.copy(finalBuffer, finalBuffer._index);
+ // Update internal position
+ self.position = finalBuffer.length;
+ // Check if we don't have a file at all
+ if(finalLength == 0 && finalBuffer.length == 0) return callback(new Error("File does not exist"), null);
+ // Else return data
+ callback(null, finalBuffer);
+ } else {
+ var slice = self.currentChunk.readSlice(self.currentChunk.length() - self.currentChunk.position);
+ // Copy content to final buffer
+ slice.copy(finalBuffer, finalBuffer._index);
+ // Update index position
+ finalBuffer._index += slice.length;
+
+ // Load next chunk and read more
+ nthChunk(self, self.currentChunk.chunkNumber + 1, function(err, chunk) {
+ if(err) return callback(err);
+
+ if(chunk.length() > 0) {
+ self.currentChunk = chunk;
+ self.read(length, finalBuffer, callback);
+ } else {
+ if (finalBuffer._index > 0) {
+ callback(null, finalBuffer)
+ } else {
+ callback(new Error("no chunks found for file, possibly corrupt"), null);
+ }
+ }
+ });
+ }
+}
+
+/**
+ * Retrieves the position of the read/write head of this file.
+ *
+ * @param {Function} callback This gets called after this method terminates. null is passed to the first parameter and the position is passed to the second.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.tell = function(callback) {
+ callback(null, this.position);
+};
+
+/**
+ * Moves the read/write head to a new location.
+ *
+ * There are 3 signatures for this method
+ *
+ * Seek Location Modes
+ * - **GridStore.IO_SEEK_SET**, **(default)** set the position from the start of the file.
+ * - **GridStore.IO_SEEK_CUR**, set the position from the current position in the file.
+ * - **GridStore.IO_SEEK_END**, set the position from the end of the file.
+ *
+ * @param {Number} [position] the position to seek to
+ * @param {Number} [seekLocation] seek mode. Use one of the Seek Location modes.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.seek = function(position, seekLocation, callback) {
+ var self = this;
+
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ seekLocation = args.length ? args.shift() : null;
+
+ var seekLocationFinal = seekLocation == null ? exports.GridStore.IO_SEEK_SET : seekLocation;
+ var finalPosition = position;
+ var targetPosition = 0;
+ if(seekLocationFinal == exports.GridStore.IO_SEEK_CUR) {
+ targetPosition = self.position + finalPosition;
+ } else if(seekLocationFinal == exports.GridStore.IO_SEEK_END) {
+ targetPosition = self.length + finalPosition;
+ } else {
+ targetPosition = finalPosition;
+ }
+
+ var newChunkNumber = Math.floor(targetPosition/self.chunkSize);
+ if(newChunkNumber != self.currentChunk.chunkNumber) {
+ var seekChunk = function() {
+ nthChunk(self, newChunkNumber, function(err, chunk) {
+ self.currentChunk = chunk;
+ self.position = targetPosition;
+ self.currentChunk.position = (self.position % self.chunkSize);
+ callback(err, self);
+ });
+ };
+
+ if(self.mode[0] == 'w') {
+ self.currentChunk.save(function(err) {
+ if(err) return callback(err);
+ seekChunk();
+ });
+ } else {
+ seekChunk();
+ }
+ } else {
+ self.position = targetPosition;
+ self.currentChunk.position = (self.position % self.chunkSize);
+ callback(null, self);
+ }
+};
+
+/**
+ * Verify if the file is at EOF.
+ *
+ * @return {Boolean} true if the read/write head is at the end of this file.
+ * @api public
+ */
+GridStore.prototype.eof = function() {
+ return this.position == this.length ? true : false;
+};
+
+/**
+ * Retrieves a single character from this file.
+ *
+ * @param {Function} callback this gets called after this method is executed. Passes null to the first parameter and the character read to the second or null to the second if the read/write head is at the end of the file.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.getc = function(callback) {
+ var self = this;
+
+ if(self.eof()) {
+ callback(null, null);
+ } else if(self.currentChunk.eof()) {
+ nthChunk(self, self.currentChunk.chunkNumber + 1, function(err, chunk) {
+ self.currentChunk = chunk;
+ self.position = self.position + 1;
+ callback(err, self.currentChunk.getc());
+ });
+ } else {
+ self.position = self.position + 1;
+ callback(null, self.currentChunk.getc());
+ }
+};
+
+/**
+ * Writes a string to the file with a newline character appended at the end if
+ * the given string does not have one.
+ *
+ * @param {String} string the string to write.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.puts = function(string, callback) {
+ var finalString = string.match(/\n$/) == null ? string + "\n" : string;
+ this.write(finalString, callback);
+};
+
+/**
+ * Returns read stream based on this GridStore file
+ *
+ * Events
+ * - **data** {function(item) {}} the data event triggers when a document is ready.
+ * - **end** {function() {}} the end event triggers when there is no more documents available.
+ * - **close** {function() {}} the close event triggers when the stream is closed.
+ * - **error** {function(err) {}} the error event triggers if an error happens.
+ *
+ * @param {Boolean} autoclose if true current GridStore will be closed when EOF and 'close' event will be fired
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.stream = function(autoclose) {
+ return new ReadStream(autoclose, this);
+};
+
+/**
+* The collection to be used for holding the files and chunks collection.
+*
+* @classconstant DEFAULT_ROOT_COLLECTION
+**/
+GridStore.DEFAULT_ROOT_COLLECTION = 'fs';
+
+/**
+* Default file mime type
+*
+* @classconstant DEFAULT_CONTENT_TYPE
+**/
+GridStore.DEFAULT_CONTENT_TYPE = 'binary/octet-stream';
+
+/**
+* Seek mode where the given length is absolute.
+*
+* @classconstant IO_SEEK_SET
+**/
+GridStore.IO_SEEK_SET = 0;
+
+/**
+* Seek mode where the given length is an offset to the current read/write head.
+*
+* @classconstant IO_SEEK_CUR
+**/
+GridStore.IO_SEEK_CUR = 1;
+
+/**
+* Seek mode where the given length is an offset to the end of the file.
+*
+* @classconstant IO_SEEK_END
+**/
+GridStore.IO_SEEK_END = 2;
+
+/**
+ * Checks if a file exists in the database.
+ *
+ * @param {Db} db the database to query.
+ * @param {String} name the name of the file to look for.
+ * @param {String} [rootCollection] the root collection that holds the files and chunks collection. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**.
+ * @param {Function} callback this will be called after this method executes. Passes null to the first and passes true to the second if the file exists and false otherwise.
+ * @return {null}
+ * @api public
+ */
+GridStore.exist = function(db, fileIdObject, rootCollection, callback) {
+ var args = Array.prototype.slice.call(arguments, 2);
+ callback = args.pop();
+ rootCollection = args.length ? args.shift() : null;
+
+ // Fetch collection
+ var rootCollectionFinal = rootCollection != null ? rootCollection : GridStore.DEFAULT_ROOT_COLLECTION;
+ db.collection(rootCollectionFinal + ".files", function(err, collection) {
+ if(err) return callback(err);
+
+ // Build query
+ var query = (typeof fileIdObject == 'string' || Object.prototype.toString.call(fileIdObject) == '[object RegExp]' )
+ ? {'filename':fileIdObject}
+ : {'_id':fileIdObject}; // Attempt to locate file
+
+ collection.find(query, function(err, cursor) {
+ if(err) return callback(err);
+
+ cursor.nextObject(function(err, item) {
+ if(err) return callback(err);
+ callback(null, item == null ? false : true);
+ });
+ });
+ });
+};
+
+/**
+ * Gets the list of files stored in the GridFS.
+ *
+ * @param {Db} db the database to query.
+ * @param {String} [rootCollection] the root collection that holds the files and chunks collection. Defaults to **{GridStore.DEFAULT_ROOT_COLLECTION}**.
+ * @param {Function} callback this will be called after this method executes. Passes null to the first and passes an array of strings containing the names of the files.
+ * @return {null}
+ * @api public
+ */
+GridStore.list = function(db, rootCollection, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 1);
+ callback = args.pop();
+ rootCollection = args.length ? args.shift() : null;
+ options = args.length ? args.shift() : {};
+
+ // Ensure we have correct values
+ if(rootCollection != null && typeof rootCollection == 'object') {
+ options = rootCollection;
+ rootCollection = null;
+ }
+
+ // Check if we are returning by id not filename
+ var byId = options['id'] != null ? options['id'] : false;
+ // Fetch item
+ var rootCollectionFinal = rootCollection != null ? rootCollection : GridStore.DEFAULT_ROOT_COLLECTION;
+ var items = [];
+ db.collection((rootCollectionFinal + ".files"), function(err, collection) {
+ if(err) return callback(err);
+
+ collection.find(function(err, cursor) {
+ if(err) return callback(err);
+
+ cursor.each(function(err, item) {
+ if(item != null) {
+ items.push(byId ? item._id : item.filename);
+ } else {
+ callback(err, items);
+ }
+ });
+ });
+ });
+};
+
+/**
+ * Reads the contents of a file.
+ *
+ * This method has the following signatures
+ *
+ * (db, name, callback)
+ * (db, name, length, callback)
+ * (db, name, length, offset, callback)
+ * (db, name, length, offset, options, callback)
+ *
+ * @param {Db} db the database to query.
+ * @param {String} name the name of the file.
+ * @param {Number} [length] the size of data to read.
+ * @param {Number} [offset] the offset from the head of the file of which to start reading from.
+ * @param {Object} [options] the options for the file.
+ * @param {Function} callback this will be called after this method executes. A string with an error message will be passed to the first parameter when the length and offset combination exceeds the length of the file while an Error object will be passed if other forms of error occured, otherwise, a string is passed. The second parameter will contain the data read if successful or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+GridStore.read = function(db, name, length, offset, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 2);
+ callback = args.pop();
+ length = args.length ? args.shift() : null;
+ offset = args.length ? args.shift() : null;
+ options = args.length ? args.shift() : null;
+
+ new GridStore(db, name, "r", options).open(function(err, gridStore) {
+ if(err) return callback(err);
+ // Make sure we are not reading out of bounds
+ if(offset && offset >= gridStore.length) return callback("offset larger than size of file", null);
+ if(length && length > gridStore.length) return callback("length is larger than the size of the file", null);
+ if(offset && length && (offset + length) > gridStore.length) return callback("offset and length is larger than the size of the file", null);
+
+ if(offset != null) {
+ gridStore.seek(offset, function(err, gridStore) {
+ if(err) return callback(err);
+ gridStore.read(length, callback);
+ });
+ } else {
+ gridStore.read(length, callback);
+ }
+ });
+};
+
+/**
+ * Reads the data of this file.
+ *
+ * @param {Db} db the database to query.
+ * @param {String} name the name of the file.
+ * @param {String} [separator] the character to be recognized as the newline separator.
+ * @param {Object} [options] file options.
+ * @param {Function} callback this will be called after this method is executed. The first parameter will be null and the second parameter will contain an array of strings representing the entire data, each element representing a line including the separator character.
+ * @return {null}
+ * @api public
+ */
+GridStore.readlines = function(db, name, separator, options, callback) {
+ var args = Array.prototype.slice.call(arguments, 2);
+ callback = args.pop();
+ separator = args.length ? args.shift() : null;
+ options = args.length ? args.shift() : null;
+
+ var finalSeperator = separator == null ? "\n" : separator;
+ new GridStore(db, name, "r", options).open(function(err, gridStore) {
+ if(err) return callback(err);
+ gridStore.readlines(finalSeperator, callback);
+ });
+};
+
+/**
+ * Deletes the chunks and metadata information of a file from GridFS.
+ *
+ * @param {Db} db the database to interact with.
+ * @param {String|Array} names the name/names of the files to delete.
+ * @param {Object} [options] the options for the files.
+ * @callback {Function} this will be called after this method is executed. The first parameter will contain an Error object if an error occured or null otherwise. The second parameter will contain a reference to this object.
+ * @return {null}
+ * @api public
+ */
+GridStore.unlink = function(db, names, options, callback) {
+ var self = this;
+ var args = Array.prototype.slice.call(arguments, 2);
+ callback = args.pop();
+ options = args.length ? args.shift() : null;
+
+ if(names.constructor == Array) {
+ var tc = 0;
+ for(var i = 0; i < names.length; i++) {
+ ++tc;
+ self.unlink(db, names[i], function(result) {
+ if(--tc == 0) {
+ callback(null, self);
+ }
+ });
+ }
+ } else {
+ new GridStore(db, names, "w", options).open(function(err, gridStore) {
+ if(err) return callback(err);
+ deleteChunks(gridStore, function(err, result) {
+ if(err) return callback(err);
+ gridStore.collection(function(err, collection) {
+ if(err) return callback(err);
+ collection.remove({'_id':gridStore.fileId}, {safe:true}, function(err, collection) {
+ callback(err, self);
+ });
+ });
+ });
+ });
+ }
+};
+
+/**
+ * Returns the current chunksize of the file.
+ *
+ * @field chunkSize
+ * @type {Number}
+ * @getter
+ * @setter
+ * @property return number of bytes in the current chunkSize.
+ */
+Object.defineProperty(GridStore.prototype, "chunkSize", { enumerable: true
+ , get: function () {
+ return this.internalChunkSize;
+ }
+ , set: function(value) {
+ if(!(this.mode[0] == "w" && this.position == 0 && this.uploadDate == null)) {
+ this.internalChunkSize = this.internalChunkSize;
+ } else {
+ this.internalChunkSize = value;
+ }
+ }
+});
+
+/**
+ * The md5 checksum for this file.
+ *
+ * @field md5
+ * @type {Number}
+ * @getter
+ * @setter
+ * @property return this files md5 checksum.
+ */
+Object.defineProperty(GridStore.prototype, "md5", { enumerable: true
+ , get: function () {
+ return this.internalMd5;
+ }
+});
+
+/**
+ * GridStore Streaming methods
+ * Handles the correct return of the writeable stream status
+ * @ignore
+ */
+Object.defineProperty(GridStore.prototype, "writable", { enumerable: true
+ , get: function () {
+ if(this._writeable == null) {
+ this._writeable = this.mode != null && this.mode.indexOf("w") != -1;
+ }
+ // Return the _writeable
+ return this._writeable;
+ }
+ , set: function(value) {
+ this._writeable = value;
+ }
+});
+
+/**
+ * Handles the correct return of the readable stream status
+ * @ignore
+ */
+Object.defineProperty(GridStore.prototype, "readable", { enumerable: true
+ , get: function () {
+ if(this._readable == null) {
+ this._readable = this.mode != null && this.mode.indexOf("r") != -1;
+ }
+ return this._readable;
+ }
+ , set: function(value) {
+ this._readable = value;
+ }
+});
+
+GridStore.prototype.paused;
+
+/**
+ * Handles the correct setting of encoding for the stream
+ * @ignore
+ */
+GridStore.prototype.setEncoding = fs.ReadStream.prototype.setEncoding;
+
+/**
+ * Handles the end events
+ * @ignore
+ */
+GridStore.prototype.end = function end(data) {
+ var self = this;
+ // allow queued data to write before closing
+ if(!this.writable) return;
+ this.writable = false;
+
+ if(data) {
+ this._q.push(data);
+ }
+
+ this.on('drain', function () {
+ self.close(function (err) {
+ if (err) return _error(self, err);
+ self.emit('close');
+ });
+ });
+
+ _flush(self);
+}
+
+/**
+ * Handles the normal writes to gridstore
+ * @ignore
+ */
+var _writeNormal = function(self, data, close, callback) {
+ // If we have a buffer write it using the writeBuffer method
+ if(Buffer.isBuffer(data)) {
+ return writeBuffer(self, data, close, callback);
+ } else {
+ // Wrap the string in a buffer and write
+ return writeBuffer(self, new Buffer(data, 'binary'), close, callback);
+ }
+}
+
+/**
+ * Writes some data. This method will work properly only if initialized with mode "w" or "w+".
+ *
+ * @param {String|Buffer} data the data to write.
+ * @param {Boolean} [close] closes this file after writing if set to true.
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain null and the second one will contain a reference to this object.
+ * @return {null}
+ * @api public
+ */
+GridStore.prototype.write = function write(data, close, callback) {
+ // If it's a normal write delegate the call
+ if(typeof close == 'function' || typeof callback == 'function') {
+ return _writeNormal(this, data, close, callback);
+ }
+
+ // Otherwise it's a stream write
+ var self = this;
+ if (!this.writable) {
+ throw new Error('GridWriteStream is not writable');
+ }
+
+ // queue data until we open.
+ if (!this._opened) {
+ // Set up a queue to save data until gridstore object is ready
+ this._q = [];
+ _openStream(self);
+ this._q.push(data);
+ return false;
+ }
+
+ // Push data to queue
+ this._q.push(data);
+ _flush(this);
+ // Return write successful
+ return true;
+}
+
+/**
+ * Handles the destroy part of a stream
+ * @ignore
+ */
+GridStore.prototype.destroy = function destroy() {
+ // close and do not emit any more events. queued data is not sent.
+ if(!this.writable) return;
+ this.readable = false;
+ if(this.writable) {
+ this.writable = false;
+ this._q.length = 0;
+ this.emit('close');
+ }
+}
+
+/**
+ * Handles the destroySoon part of a stream
+ * @ignore
+ */
+GridStore.prototype.destroySoon = function destroySoon() {
+ // as soon as write queue is drained, destroy.
+ // may call destroy immediately if no data is queued.
+ if(!this._q.length) {
+ return this.destroy();
+ }
+ this._destroying = true;
+}
+
+/**
+ * Handles the pipe part of the stream
+ * @ignore
+ */
+GridStore.prototype.pipe = function(destination, options) {
+ var self = this;
+ // Open the gridstore
+ this.open(function(err, result) {
+ if(err) _errorRead(self, err);
+ if(!self.readable) return;
+ // Set up the pipe
+ self._pipe(destination, options);
+ // Emit the stream is open
+ self.emit('open');
+ // Read from the stream
+ _read(self);
+ })
+}
+
+/**
+ * Internal module methods
+ * @ignore
+ */
+var _read = function _read(self) {
+ if (!self.readable || self.paused || self.reading) {
+ return;
+ }
+
+ self.reading = true;
+ var stream = self._stream = self.stream();
+ stream.paused = self.paused;
+
+ stream.on('data', function (data) {
+ if (self._decoder) {
+ var str = self._decoder.write(data);
+ if (str.length) self.emit('data', str);
+ } else {
+ self.emit('data', data);
+ }
+ });
+
+ stream.on('end', function (data) {
+ self.emit('end', data);
+ });
+
+ stream.on('error', function (data) {
+ _errorRead(self, data);
+ });
+
+ stream.on('close', function (data) {
+ self.emit('close', data);
+ });
+
+ self.pause = function () {
+ // native doesn't always pause.
+ // bypass its pause() method to hack it
+ self.paused = stream.paused = true;
+ }
+
+ self.resume = function () {
+ if(!self.paused) return;
+
+ self.paused = false;
+ stream.resume();
+ self.readable = stream.readable;
+ }
+
+ self.destroy = function () {
+ self.readable = false;
+ stream.destroy();
+ }
+}
+
+/**
+ * pause
+ * @ignore
+ */
+GridStore.prototype.pause = function pause () {
+ // Overridden when the GridStore opens.
+ this.paused = true;
+}
+
+/**
+ * resume
+ * @ignore
+ */
+GridStore.prototype.resume = function resume () {
+ // Overridden when the GridStore opens.
+ this.paused = false;
+}
+
+/**
+ * Internal module methods
+ * @ignore
+ */
+var _flush = function _flush(self, _force) {
+ if (!self._opened) return;
+ if (!_force && self._flushing) return;
+ self._flushing = true;
+
+ // write the entire q to gridfs
+ if (!self._q.length) {
+ self._flushing = false;
+ self.emit('drain');
+
+ if(self._destroying) {
+ self.destroy();
+ }
+ return;
+ }
+
+ self.write(self._q.shift(), function (err, store) {
+ if (err) return _error(self, err);
+ self.emit('progress', store.position);
+ _flush(self, true);
+ });
+}
+
+var _openStream = function _openStream (self) {
+ if(self._opening == true) return;
+ self._opening = true;
+
+ // Open the store
+ self.open(function (err, gridstore) {
+ if (err) return _error(self, err);
+ self._opened = true;
+ self.emit('open');
+ _flush(self);
+ });
+}
+
+var _error = function _error(self, err) {
+ self.destroy();
+ self.emit('error', err);
+}
+
+var _errorRead = function _errorRead (self, err) {
+ self.readable = false;
+ self.emit('error', err);
+}
+
+/**
+ * @ignore
+ * @api private
+ */
+exports.GridStore = GridStore;
diff --git a/node_modules/mongodb/lib/mongodb/gridfs/readstream.js b/node_modules/mongodb/lib/mongodb/gridfs/readstream.js
new file mode 100644
index 0000000..ebb09bd
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/gridfs/readstream.js
@@ -0,0 +1,188 @@
+var Stream = require('stream').Stream,
+ util = require('util');
+
+/**
+ * ReadStream
+ *
+ * Returns a stream interface for the **file**.
+ *
+ * Events
+ * - **data** {function(item) {}} the data event triggers when a document is ready.
+ * - **end** {function() {}} the end event triggers when there is no more documents available.
+ * - **close** {function() {}} the close event triggers when the stream is closed.
+ * - **error** {function(err) {}} the error event triggers if an error happens.
+ *
+ * @class Represents a GridFS File Stream.
+ * @param {Boolean} autoclose automatically close file when the stream reaches the end.
+ * @param {GridStore} cursor a cursor object that the stream wraps.
+ * @return {ReadStream}
+ */
+function ReadStream(autoclose, gstore) {
+ if (!(this instanceof ReadStream)) return new ReadStream(autoclose, gstore);
+ Stream.call(this);
+
+ this.autoclose = !!autoclose;
+ this.gstore = gstore;
+
+ this.finalLength = gstore.length - gstore.position;
+ this.completedLength = 0;
+ this.currentChunkNumber = gstore.currentChunk.chunkNumber;
+
+ this.paused = false;
+ this.readable = true;
+ this.pendingChunk = null;
+ this.executing = false;
+
+ // Calculate the number of chunks
+ this.numberOfChunks = Math.ceil(gstore.length/gstore.chunkSize);
+
+ // This seek start position inside the current chunk
+ this.seekStartPosition = gstore.position - (this.currentChunkNumber * gstore.chunkSize);
+
+ var self = this;
+ process.nextTick(function() {
+ self._execute();
+ });
+};
+
+/**
+ * Inherit from Stream
+ * @ignore
+ * @api private
+ */
+ReadStream.prototype.__proto__ = Stream.prototype;
+
+/**
+ * Flag stating whether or not this stream is readable.
+ */
+ReadStream.prototype.readable;
+
+/**
+ * Flag stating whether or not this stream is paused.
+ */
+ReadStream.prototype.paused;
+
+/**
+ * @ignore
+ * @api private
+ */
+ReadStream.prototype._execute = function() {
+ if(this.paused === true || this.readable === false) {
+ return;
+ }
+
+ var gstore = this.gstore;
+ var self = this;
+ // Set that we are executing
+ this.executing = true;
+
+ var last = false;
+ var toRead = 0;
+
+ if(gstore.currentChunk.chunkNumber >= (this.numberOfChunks - 1)) {
+ self.executing = false;
+ last = true;
+ }
+
+ // Data setup
+ var data = null;
+
+ // Read a slice (with seek set if none)
+ if(this.seekStartPosition > 0 && (gstore.currentChunk.length() - this.seekStartPosition) > 0) {
+ data = gstore.currentChunk.readSlice(gstore.currentChunk.length() - this.seekStartPosition);
+ this.seekStartPosition = 0;
+ } else {
+ data = gstore.currentChunk.readSlice(gstore.currentChunk.length());
+ }
+
+ // Return the data
+ if(data != null && gstore.currentChunk.chunkNumber == self.currentChunkNumber) {
+ self.currentChunkNumber = self.currentChunkNumber + 1;
+ self.completedLength += data.length;
+ self.pendingChunk = null;
+ self.emit("data", data);
+ }
+
+ if(last === true) {
+ self.readable = false;
+ self.emit("end");
+
+ if(self.autoclose === true) {
+ if(gstore.mode[0] == "w") {
+ gstore.close(function(err, doc) {
+ if (err) {
+ self.emit("error", err);
+ return;
+ }
+ self.readable = false;
+ self.emit("close", doc);
+ });
+ } else {
+ self.readable = false;
+ self.emit("close");
+ }
+ }
+ } else {
+ gstore._nthChunk(gstore.currentChunk.chunkNumber + 1, function(err, chunk) {
+ if(err) {
+ self.readable = false;
+ self.emit("error", err);
+ self.executing = false;
+ return;
+ }
+
+ self.pendingChunk = chunk;
+ if(self.paused === true) {
+ self.executing = false;
+ return;
+ }
+
+ gstore.currentChunk = self.pendingChunk;
+ self._execute();
+ });
+ }
+};
+
+/**
+ * Pauses this stream, then no farther events will be fired.
+ *
+ * @ignore
+ * @api public
+ */
+ReadStream.prototype.pause = function() {
+ if(!this.executing) {
+ this.paused = true;
+ }
+};
+
+/**
+ * Destroys the stream, then no farther events will be fired.
+ *
+ * @ignore
+ * @api public
+ */
+ReadStream.prototype.destroy = function() {
+ this.readable = false;
+ // Emit close event
+ this.emit("close");
+};
+
+/**
+ * Resumes this stream.
+ *
+ * @ignore
+ * @api public
+ */
+ReadStream.prototype.resume = function() {
+ if(this.paused === false || !this.readable) {
+ return;
+ }
+
+ this.paused = false;
+ var self = this;
+ process.nextTick(function() {
+ self._execute();
+ });
+};
+
+exports.ReadStream = ReadStream;
diff --git a/node_modules/mongodb/lib/mongodb/index.js b/node_modules/mongodb/lib/mongodb/index.js
new file mode 100644
index 0000000..6a2b727
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/index.js
@@ -0,0 +1,69 @@
+try {
+ exports.BSONPure = require('bson').BSONPure;
+ exports.BSONNative = require('bson').BSONNative;
+} catch(err) {
+ // do nothing
+}
+
+[ 'commands/base_command'
+ , 'admin'
+ , 'collection'
+ , 'connection/read_preference'
+ , 'connection/connection'
+ , 'connection/server'
+ , 'connection/mongos'
+ , 'connection/repl_set'
+ , 'mongo_client'
+ , 'cursor'
+ , 'db'
+ , 'mongo_client'
+ , 'gridfs/grid'
+ , 'gridfs/chunk'
+ , 'gridfs/gridstore'].forEach(function (path) {
+ var module = require('./' + path);
+ for (var i in module) {
+ exports[i] = module[i];
+ }
+
+ // backwards compat
+ exports.ReplSetServers = exports.ReplSet;
+
+ // Add BSON Classes
+ exports.Binary = require('bson').Binary;
+ exports.Code = require('bson').Code;
+ exports.DBRef = require('bson').DBRef;
+ exports.Double = require('bson').Double;
+ exports.Long = require('bson').Long;
+ exports.MinKey = require('bson').MinKey;
+ exports.MaxKey = require('bson').MaxKey;
+ exports.ObjectID = require('bson').ObjectID;
+ exports.Symbol = require('bson').Symbol;
+ exports.Timestamp = require('bson').Timestamp;
+
+ // Add BSON Parser
+ exports.BSON = require('bson').BSONPure.BSON;
+
+});
+
+// Get the Db object
+var Db = require('./db').Db;
+// Set up the connect function
+var connect = Db.connect;
+var obj = connect;
+// Map all values to the exports value
+for(var name in exports) {
+ obj[name] = exports[name];
+}
+
+// Add the pure and native backward compatible functions
+exports.pure = exports.native = function() {
+ return obj;
+}
+
+// Map all values to the exports value
+for(var name in exports) {
+ connect[name] = exports[name];
+}
+
+// Set our exports to be the connect function
+module.exports = connect;
\ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/mongo_client.js b/node_modules/mongodb/lib/mongodb/mongo_client.js
new file mode 100644
index 0000000..cfc9e6f
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/mongo_client.js
@@ -0,0 +1,116 @@
+var Db = require('./db').Db;
+
+/**
+ * Create a new MongoClient instance.
+ *
+ * Options
+ * - **w**, {Number/String, > -1 || 'majority' || tag name} the write concern for the operation where < 1 is no acknowlegement of write and w >= 1, w = 'majority' or tag acknowledges the write
+ * - **wtimeout**, {Number, 0} set the timeout for waiting for write concern to finish (combines with w option)
+ * - **fsync**, (Boolean, default:false) write waits for fsync before returning
+ * - **journal**, (Boolean, default:false) write waits for journal sync before returning
+ * - **readPreference** {String}, the prefered read preference (ReadPreference.PRIMARY, ReadPreference.PRIMARY_PREFERRED, ReadPreference.SECONDARY, ReadPreference.SECONDARY_PREFERRED, ReadPreference.NEAREST).
+ * - **native_parser** {Boolean, default:false}, use c++ bson parser.
+ * - **forceServerObjectId** {Boolean, default:false}, force server to create _id fields instead of client.
+ * - **pkFactory** {Object}, object overriding the basic ObjectID primary key generation.
+ * - **serializeFunctions** {Boolean, default:false}, serialize functions.
+ * - **raw** {Boolean, default:false}, peform operations using raw bson buffers.
+ * - **recordQueryStats** {Boolean, default:false}, record query statistics during execution.
+ * - **retryMiliSeconds** {Number, default:5000}, number of miliseconds between retries.
+ * - **numberOfRetries** {Number, default:5}, number of retries off connection.
+ *
+ * Deprecated Options
+ * - **safe** {true | {w:n, wtimeout:n} | {fsync:true}, default:false}, executes with a getLastError command returning the results of the command on MongoDB.
+ *
+ * @class Represents a MongoClient
+ * @param {Object} serverConfig server config object.
+ * @param {Object} [options] additional options for the collection.
+ */
+function MongoClient(serverConfig, options) {
+ options = options == null ? {} : options;
+ // If no write concern is set set the default to w:1
+ if(options != null && !options.journal && !options.w && !options.fsync) {
+ options.w = 1;
+ }
+
+ // The internal db instance we are wrapping
+ this._db = new Db('test', serverConfig, options);
+}
+
+/**
+ * Initialize the database connection.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the connected mongoclient or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+MongoClient.prototype.open = function(callback) {
+ // Self reference
+ var self = this;
+
+ this._db.open(function(err, db) {
+ if(err) return callback(err, null);
+ callback(null, self);
+ })
+}
+
+/**
+ * Close the current db connection, including all the child db instances. Emits close event if no callback is provided.
+ *
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the results from the close method or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+MongoClient.prototype.close = function(callback) {
+ this._db.close(callback);
+}
+
+/**
+ * Create a new Db instance sharing the current socket connections.
+ *
+ * @param {String} dbName the name of the database we want to use.
+ * @return {Db} a db instance using the new database.
+ * @api public
+ */
+MongoClient.prototype.db = function(dbName) {
+ return this._db.db(dbName);
+}
+
+/**
+ * Connect to MongoDB using a url as documented at
+ *
+ * www.mongodb.org/display/DOCS/Connections
+ *
+ * Options
+ * - **uri_decode_auth** {Boolean, default:false} uri decode the user name and password for authentication
+ * - **db** {Object, default: null} a hash off options to set on the db object, see **Db constructor**
+ * - **server** {Object, default: null} a hash off options to set on the server objects, see **Server** constructor**
+ * - **replSet** {Object, default: null} a hash off options to set on the replSet object, see **ReplSet** constructor**
+ * - **mongos** {Object, default: null} a hash off options to set on the mongos object, see **Mongos** constructor**
+ *
+ * @param {String} url connection url for MongoDB.
+ * @param {Object} [options] optional options for insert command
+ * @param {Function} callback this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the initialized db object or null if an error occured.
+ * @return {null}
+ * @api public
+ */
+MongoClient.connect = function(url, options, callback) {
+ if(typeof options == 'function') {
+ callback = options;
+ options = {};
+ }
+
+ Db.connect(url, options, function(err, db) {
+ if(err) return callback(err, null);
+
+ if(db.options !== null && !db.options.safe && !db.options.journal
+ && !db.options.w && !db.options.fsync && typeof db.options.w != 'number'
+ && (db.options.safe == false && url.indexOf("safe=") == -1)) {
+ db.options.w = 1;
+ }
+
+ // Return the db
+ callback(null, db);
+ });
+}
+
+exports.MongoClient = MongoClient;
\ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js b/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js
new file mode 100644
index 0000000..b129dc6
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js
@@ -0,0 +1,140 @@
+var Long = require('bson').Long;
+
+/**
+ Reply message from mongo db
+**/
+var MongoReply = exports.MongoReply = function() {
+ this.documents = [];
+ this.index = 0;
+};
+
+MongoReply.prototype.parseHeader = function(binary_reply, bson) {
+ // Unpack the standard header first
+ this.messageLength = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
+ this.index = this.index + 4;
+ // Fetch the request id for this reply
+ this.requestId = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
+ this.index = this.index + 4;
+ // Fetch the id of the request that triggered the response
+ this.responseTo = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
+ // Skip op-code field
+ this.index = this.index + 4 + 4;
+ // Unpack the reply message
+ this.responseFlag = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
+ this.index = this.index + 4;
+ // Unpack the cursor id (a 64 bit long integer)
+ var low_bits = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
+ this.index = this.index + 4;
+ var high_bits = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
+ this.index = this.index + 4;
+ this.cursorId = new Long(low_bits, high_bits);
+ // Unpack the starting from
+ this.startingFrom = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
+ this.index = this.index + 4;
+ // Unpack the number of objects returned
+ this.numberReturned = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
+ this.index = this.index + 4;
+}
+
+MongoReply.prototype.parseBody = function(binary_reply, bson, raw, callback) {
+ raw = raw == null ? false : raw;
+ // Just set a doc limit for deserializing
+ var docLimitSize = 1024*20;
+
+ // If our message length is very long, let's switch to process.nextTick for messages
+ if(this.messageLength > docLimitSize) {
+ var batchSize = this.numberReturned;
+ this.documents = new Array(this.numberReturned);
+
+ // Just walk down until we get a positive number >= 1
+ for(var i = 50; i > 0; i--) {
+ if((this.numberReturned/i) >= 1) {
+ batchSize = i;
+ break;
+ }
+ }
+
+ // Actual main creator of the processFunction setting internal state to control the flow
+ var parseFunction = function(_self, _binary_reply, _batchSize, _numberReturned) {
+ var object_index = 0;
+ // Internal loop process that will use nextTick to ensure we yield some time
+ var processFunction = function() {
+ // Adjust batchSize if we have less results left than batchsize
+ if((_numberReturned - object_index) < _batchSize) {
+ _batchSize = _numberReturned - object_index;
+ }
+
+ // If raw just process the entries
+ if(raw) {
+ // Iterate over the batch
+ for(var i = 0; i < _batchSize; i++) {
+ // Are we done ?
+ if(object_index <= _numberReturned) {
+ // Read the size of the bson object
+ var bsonObjectSize = _binary_reply[_self.index] | _binary_reply[_self.index + 1] << 8 | _binary_reply[_self.index + 2] << 16 | _binary_reply[_self.index + 3] << 24;
+ // If we are storing the raw responses to pipe straight through
+ _self.documents[object_index] = binary_reply.slice(_self.index, _self.index + bsonObjectSize);
+ // Adjust binary index to point to next block of binary bson data
+ _self.index = _self.index + bsonObjectSize;
+ // Update number of docs parsed
+ object_index = object_index + 1;
+ }
+ }
+ } else {
+ try {
+ // Parse documents
+ _self.index = bson.deserializeStream(binary_reply, _self.index, _batchSize, _self.documents, object_index);
+ // Adjust index
+ object_index = object_index + _batchSize;
+ } catch (err) {
+ return callback(err);
+ }
+ }
+
+ // If we hav more documents process NextTick
+ if(object_index < _numberReturned) {
+ process.nextTick(processFunction);
+ } else {
+ callback(null);
+ }
+ }
+
+ // Return the process function
+ return processFunction;
+ }(this, binary_reply, batchSize, this.numberReturned)();
+ } else {
+ try {
+ // Let's unpack all the bson documents, deserialize them and store them
+ for(var object_index = 0; object_index < this.numberReturned; object_index++) {
+ // Read the size of the bson object
+ var bsonObjectSize = binary_reply[this.index] | binary_reply[this.index + 1] << 8 | binary_reply[this.index + 2] << 16 | binary_reply[this.index + 3] << 24;
+ // If we are storing the raw responses to pipe straight through
+ if(raw) {
+ // Deserialize the object and add to the documents array
+ this.documents.push(binary_reply.slice(this.index, this.index + bsonObjectSize));
+ } else {
+ // Deserialize the object and add to the documents array
+ this.documents.push(bson.deserialize(binary_reply.slice(this.index, this.index + bsonObjectSize)));
+ }
+ // Adjust binary index to point to next block of binary bson data
+ this.index = this.index + bsonObjectSize;
+ }
+ } catch(err) {
+ return callback(err);
+ }
+
+ // No error return
+ callback(null);
+ }
+}
+
+MongoReply.prototype.is_error = function(){
+ if(this.documents.length == 1) {
+ return this.documents[0].ok == 1 ? false : true;
+ }
+ return false;
+};
+
+MongoReply.prototype.error_message = function() {
+ return this.documents.length == 1 && this.documents[0].ok == 1 ? '' : this.documents[0].errmsg;
+};
\ No newline at end of file
diff --git a/node_modules/mongodb/lib/mongodb/utils.js b/node_modules/mongodb/lib/mongodb/utils.js
new file mode 100644
index 0000000..120a584
--- /dev/null
+++ b/node_modules/mongodb/lib/mongodb/utils.js
@@ -0,0 +1,97 @@
+/**
+ * Sort functions, Normalize and prepare sort parameters
+ */
+var formatSortValue = exports.formatSortValue = function(sortDirection) {
+ var value = ("" + sortDirection).toLowerCase();
+
+ switch (value) {
+ case 'ascending':
+ case 'asc':
+ case '1':
+ return 1;
+ case 'descending':
+ case 'desc':
+ case '-1':
+ return -1;
+ default:
+ throw new Error("Illegal sort clause, must be of the form "
+ + "[['field1', '(ascending|descending)'], "
+ + "['field2', '(ascending|descending)']]");
+ }
+};
+
+var formattedOrderClause = exports.formattedOrderClause = function(sortValue) {
+ var orderBy = {};
+
+ if (Array.isArray(sortValue)) {
+ for(var i = 0; i < sortValue.length; i++) {
+ if(sortValue[i].constructor == String) {
+ orderBy[sortValue[i]] = 1;
+ } else {
+ orderBy[sortValue[i][0]] = formatSortValue(sortValue[i][1]);
+ }
+ }
+ } else if(Object.prototype.toString.call(sortValue) === '[object Object]') {
+ orderBy = sortValue;
+ } else if (sortValue.constructor == String) {
+ orderBy[sortValue] = 1;
+ } else {
+ throw new Error("Illegal sort clause, must be of the form " +
+ "[['field1', '(ascending|descending)'], ['field2', '(ascending|descending)']]");
+ }
+
+ return orderBy;
+};
+
+exports.encodeInt = function(value) {
+ var buffer = new Buffer(4);
+ buffer[3] = (value >> 24) & 0xff;
+ buffer[2] = (value >> 16) & 0xff;
+ buffer[1] = (value >> 8) & 0xff;
+ buffer[0] = value & 0xff;
+ return buffer;
+}
+
+exports.encodeIntInPlace = function(value, buffer, index) {
+ buffer[index + 3] = (value >> 24) & 0xff;
+ buffer[index + 2] = (value >> 16) & 0xff;
+ buffer[index + 1] = (value >> 8) & 0xff;
+ buffer[index] = value & 0xff;
+}
+
+exports.encodeCString = function(string) {
+ var buf = new Buffer(string, 'utf8');
+ return [buf, new Buffer([0])];
+}
+
+exports.decodeUInt32 = function(array, index) {
+ return array[index] | array[index + 1] << 8 | array[index + 2] << 16 | array[index + 3] << 24;
+}
+
+// Decode the int
+exports.decodeUInt8 = function(array, index) {
+ return array[index];
+}
+
+/**
+ * Context insensitive type checks
+ */
+
+var toString = Object.prototype.toString;
+
+exports.isObject = function (arg) {
+ return '[object Object]' == toString.call(arg)
+}
+
+exports.isArray = function (arg) {
+ return Array.isArray(arg) ||
+ 'object' == typeof arg && '[object Array]' == toString.call(arg)
+}
+
+exports.isDate = function (arg) {
+ return 'object' == typeof arg && '[object Date]' == toString.call(arg)
+}
+
+exports.isRegExp = function (arg) {
+ return 'object' == typeof arg && '[object RegExp]' == toString.call(arg)
+}
diff --git a/node_modules/mongodb/node_modules/bson/.travis.yml b/node_modules/mongodb/node_modules/bson/.travis.yml
new file mode 100644
index 0000000..94740d0
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - 0.6
+ - 0.8
+ - 0.9 # development version of 0.8, may be unstable
\ No newline at end of file
diff --git a/node_modules/mongodb/node_modules/bson/Makefile b/node_modules/mongodb/node_modules/bson/Makefile
new file mode 100644
index 0000000..2ca5592
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/Makefile
@@ -0,0 +1,16 @@
+NODE = node
+NPM = npm
+NODEUNIT = node_modules/nodeunit/bin/nodeunit
+
+all: clean node_gyp
+
+test: clean node_gyp
+ npm test
+
+node_gyp: clean
+ node-gyp configure build
+
+clean:
+ node-gyp clean
+
+.PHONY: all
diff --git a/node_modules/mongodb/node_modules/bson/README.md b/node_modules/mongodb/node_modules/bson/README.md
new file mode 100644
index 0000000..73892e2
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/README.md
@@ -0,0 +1 @@
+A JS/C++ Bson parser for node, used in the MongoDB Native driver
\ No newline at end of file
diff --git a/node_modules/mongodb/node_modules/bson/benchmarks/benchmarks.js b/node_modules/mongodb/node_modules/bson/benchmarks/benchmarks.js
new file mode 100644
index 0000000..45a1111
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/benchmarks/benchmarks.js
@@ -0,0 +1,130 @@
+// var BSON = require('../../lib/mongodb').BSONNative.BSON,
+// ObjectID = require('../../lib/mongodb').BSONNative.ObjectID,
+// Code = require('../../lib/mongodb').BSONNative.Code,
+// Long = require('../../lib/mongodb').BSONNative.Long,
+// Binary = require('../../lib/mongodb').BSONNative.Binary,
+// debug = require('util').debug,
+// inspect = require('util').inspect,
+//
+// Long = require('../../lib/mongodb').Long,
+// ObjectID = require('../../lib/mongodb').ObjectID,
+// Binary = require('../../lib/mongodb').Binary,
+// Code = require('../../lib/mongodb').Code,
+// DBRef = require('../../lib/mongodb').DBRef,
+// Symbol = require('../../lib/mongodb').Symbol,
+// Double = require('../../lib/mongodb').Double,
+// MaxKey = require('../../lib/mongodb').MaxKey,
+// MinKey = require('../../lib/mongodb').MinKey,
+// Timestamp = require('../../lib/mongodb').Timestamp;
+
+
+// var BSON = require('../../lib/mongodb').BSONPure.BSON,
+// ObjectID = require('../../lib/mongodb').BSONPure.ObjectID,
+// Code = require('../../lib/mongodb').BSONPure.Code,
+// Long = require('../../lib/mongodb').BSONPure.Long,
+// Binary = require('../../lib/mongodb').BSONPure.Binary;
+
+var BSON = require('../lib/bson').BSONNative.BSON,
+ Long = require('../lib/bson').Long,
+ ObjectID = require('../lib/bson').ObjectID,
+ Binary = require('../lib/bson').Binary,
+ Code = require('../lib/bson').Code,
+ DBRef = require('../lib/bson').DBRef,
+ Symbol = require('../lib/bson').Symbol,
+ Double = require('../lib/bson').Double,
+ MaxKey = require('../lib/bson').MaxKey,
+ MinKey = require('../lib/bson').MinKey,
+ Timestamp = require('../lib/bson').Timestamp;
+
+ // console.dir(require('../lib/bson'))
+
+var COUNT = 1000;
+var COUNT = 100;
+
+var object = {
+ string: "Strings are great",
+ decimal: 3.14159265,
+ bool: true,
+ integer: 5,
+ date: new Date(),
+ double: new Double(1.4),
+ id: new ObjectID(),
+ min: new MinKey(),
+ max: new MaxKey(),
+ symbol: new Symbol('hello'),
+ long: Long.fromNumber(100),
+ bin: new Binary(new Buffer(100)),
+
+ subObject: {
+ moreText: "Bacon ipsum dolor sit amet cow pork belly rump ribeye pastrami andouille. Tail hamburger pork belly, drumstick flank salami t-bone sirloin pork chop ribeye ham chuck pork loin shankle. Ham fatback pork swine, sirloin shankle short loin andouille shank sausage meatloaf drumstick. Pig chicken cow bresaola, pork loin jerky meatball tenderloin brisket strip steak jowl spare ribs. Biltong sirloin pork belly boudin, bacon pastrami rump chicken. Jowl rump fatback, biltong bacon t-bone turkey. Turkey pork loin boudin, tenderloin jerky beef ribs pastrami spare ribs biltong pork chop beef.",
+ longKeylongKeylongKeylongKeylongKeylongKey: "Pork belly boudin shoulder ribeye pork chop brisket biltong short ribs. Salami beef pork belly, t-bone sirloin meatloaf tail jowl spare ribs. Sirloin biltong bresaola cow turkey. Biltong fatback meatball, bresaola tail shankle turkey pancetta ham ribeye flank bacon jerky pork chop. Boudin sirloin shoulder, salami swine flank jerky t-bone pork chop pork beef tongue. Bresaola ribeye jerky andouille. Ribeye ground round sausage biltong beef ribs chuck, shank hamburger chicken short ribs spare ribs tenderloin meatloaf pork loin."
+ },
+
+ subArray: [1,2,3,4,5,6,7,8,9,10],
+ anotherString: "another string",
+ code: new Code("function() {}", {i:1})
+}
+
+// Number of objects
+var numberOfObjects = 10000;
+var bson = new BSON([Long, ObjectID, Binary, Code, DBRef, Symbol, Double, Timestamp, MaxKey, MinKey]);
+console.log("---------------------- 1")
+var s = new Date()
+// Object serialized
+for(var i = 0; i < numberOfObjects; i++) {
+ objectBSON = bson.serialize(object, null, true)
+}
+console.log("====================== " + (new Date().getTime() - s.getTime()) + " :: " + ((new Date().getTime() - s.getTime()))/numberOfObjects)
+
+console.log("---------------------- 2")
+var s = new Date()
+// Object serialized
+for(var i = 0; i < numberOfObjects; i++) {
+ bson.deserialize(objectBSON);
+}
+console.log("====================== " + (new Date().getTime() - s.getTime()) + " :: " + ((new Date().getTime() - s.getTime()))/numberOfObjects)
+
+// // Buffer With copies of the objectBSON
+// var data = new Buffer(objectBSON.length * numberOfObjects);
+// var index = 0;
+//
+// // Copy the buffer 1000 times to create a strea m of objects
+// for(var i = 0; i < numberOfObjects; i++) {
+// // Copy data
+// objectBSON.copy(data, index);
+// // Adjust index
+// index = index + objectBSON.length;
+// }
+//
+// // console.log("-----------------------------------------------------------------------------------")
+// // console.dir(objectBSON)
+//
+// var x, start, end, j
+// var objectBSON, objectJSON
+//
+// // Allocate the return array (avoid concatinating everything)
+// var results = new Array(numberOfObjects);
+//
+// console.log(COUNT + "x (objectBSON = BSON.serialize(object))")
+// start = new Date
+//
+// // var objects = BSON.deserializeStream(data, 0, numberOfObjects);
+// // console.log("----------------------------------------------------------------------------------- 0")
+// // var objects = BSON.deserialize(data);
+// // console.log("----------------------------------------------------------------------------------- 1")
+// // console.dir(objects)
+//
+// for (j=COUNT; --j>=0; ) {
+// var nextIndex = BSON.deserializeStream(data, 0, numberOfObjects, results, 0);
+// }
+//
+// end = new Date
+// var opsprsecond = COUNT / ((end - start)/1000);
+// console.log("bson size (bytes): ", objectBSON.length);
+// console.log("time = ", end - start, "ms -", COUNT / ((end - start)/1000), " ops/sec");
+// console.log("MB/s = " + ((opsprsecond*objectBSON.length)/1024));
+//
+// // console.dir(nextIndex)
+// // console.dir(results)
+
+
diff --git a/node_modules/mongodb/node_modules/bson/binding.gyp b/node_modules/mongodb/node_modules/bson/binding.gyp
new file mode 100644
index 0000000..42445d3
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/binding.gyp
@@ -0,0 +1,17 @@
+{
+ 'targets': [
+ {
+ 'target_name': 'bson',
+ 'sources': [ 'ext/bson.cc' ],
+ 'cflags!': [ '-fno-exceptions' ],
+ 'cflags_cc!': [ '-fno-exceptions' ],
+ 'conditions': [
+ ['OS=="mac"', {
+ 'xcode_settings': {
+ 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
+ }
+ }]
+ ]
+ }
+ ]
+}
\ No newline at end of file
diff --git a/node_modules/mongodb/node_modules/bson/build/Makefile b/node_modules/mongodb/node_modules/bson/build/Makefile
new file mode 100644
index 0000000..5c7ea72
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/build/Makefile
@@ -0,0 +1,359 @@
+# We borrow heavily from the kernel build setup, though we are simpler since
+# we don't have Kconfig tweaking settings on us.
+
+# The implicit make rules have it looking for RCS files, among other things.
+# We instead explicitly write all the rules we care about.
+# It's even quicker (saves ~200ms) to pass -r on the command line.
+MAKEFLAGS=-r
+
+# The source directory tree.
+srcdir := ..
+abs_srcdir := $(abspath $(srcdir))
+
+# The name of the builddir.
+builddir_name ?= .
+
+# The V=1 flag on command line makes us verbosely print command lines.
+ifdef V
+ quiet=
+else
+ quiet=quiet_
+endif
+
+# Specify BUILDTYPE=Release on the command line for a release build.
+BUILDTYPE ?= Release
+
+# Directory all our build output goes into.
+# Note that this must be two directories beneath src/ for unit tests to pass,
+# as they reach into the src/ directory for data with relative paths.
+builddir ?= $(builddir_name)/$(BUILDTYPE)
+abs_builddir := $(abspath $(builddir))
+depsdir := $(builddir)/.deps
+
+# Object output directory.
+obj := $(builddir)/obj
+abs_obj := $(abspath $(obj))
+
+# We build up a list of every single one of the targets so we can slurp in the
+# generated dependency rule Makefiles in one pass.
+all_deps :=
+
+
+
+# C++ apps need to be linked with g++.
+#
+# Note: flock is used to seralize linking. Linking is a memory-intensive
+# process so running parallel links can often lead to thrashing. To disable
+# the serialization, override LINK via an envrionment variable as follows:
+#
+# export LINK=g++
+#
+# This will allow make to invoke N linker processes as specified in -jN.
+LINK ?= ./gyp-mac-tool flock $(builddir)/linker.lock $(CXX)
+
+CC.target ?= $(CC)
+CFLAGS.target ?= $(CFLAGS)
+CXX.target ?= $(CXX)
+CXXFLAGS.target ?= $(CXXFLAGS)
+LINK.target ?= $(LINK)
+LDFLAGS.target ?= $(LDFLAGS)
+AR.target ?= $(AR)
+ARFLAGS.target ?= crs
+
+# N.B.: the logic of which commands to run should match the computation done
+# in gyp's make.py where ARFLAGS.host etc. is computed.
+# TODO(evan): move all cross-compilation logic to gyp-time so we don't need
+# to replicate this environment fallback in make as well.
+CC.host ?= gcc
+CFLAGS.host ?=
+CXX.host ?= g++
+CXXFLAGS.host ?=
+LINK.host ?= g++
+LDFLAGS.host ?=
+AR.host ?= ar
+ARFLAGS.host := crs
+
+# Define a dir function that can handle spaces.
+# http://www.gnu.org/software/make/manual/make.html#Syntax-of-Functions
+# "leading spaces cannot appear in the text of the first argument as written.
+# These characters can be put into the argument value by variable substitution."
+empty :=
+space := $(empty) $(empty)
+
+# http://stackoverflow.com/questions/1189781/using-make-dir-or-notdir-on-a-path-with-spaces
+replace_spaces = $(subst $(space),?,$1)
+unreplace_spaces = $(subst ?,$(space),$1)
+dirx = $(call unreplace_spaces,$(dir $(call replace_spaces,$1)))
+
+# Flags to make gcc output dependency info. Note that you need to be
+# careful here to use the flags that ccache and distcc can understand.
+# We write to a dep file on the side first and then rename at the end
+# so we can't end up with a broken dep file.
+depfile = $(depsdir)/$(call replace_spaces,$@).d
+DEPFLAGS = -MMD -MF $(depfile).raw
+
+# We have to fixup the deps output in a few ways.
+# (1) the file output should mention the proper .o file.
+# ccache or distcc lose the path to the target, so we convert a rule of
+# the form:
+# foobar.o: DEP1 DEP2
+# into
+# path/to/foobar.o: DEP1 DEP2
+# (2) we want missing files not to cause us to fail to build.
+# We want to rewrite
+# foobar.o: DEP1 DEP2 \
+# DEP3
+# to
+# DEP1:
+# DEP2:
+# DEP3:
+# so if the files are missing, they're just considered phony rules.
+# We have to do some pretty insane escaping to get those backslashes
+# and dollar signs past make, the shell, and sed at the same time.
+# Doesn't work with spaces, but that's fine: .d files have spaces in
+# their names replaced with other characters.
+define fixup_dep
+# The depfile may not exist if the input file didn't have any #includes.
+touch $(depfile).raw
+# Fixup path as in (1).
+sed -e "s|^$(notdir $@)|$@|" $(depfile).raw >> $(depfile)
+# Add extra rules as in (2).
+# We remove slashes and replace spaces with new lines;
+# remove blank lines;
+# delete the first line and append a colon to the remaining lines.
+sed -e 's|\\||' -e 'y| |\n|' $(depfile).raw |\
+ grep -v '^$$' |\
+ sed -e 1d -e 's|$$|:|' \
+ >> $(depfile)
+rm $(depfile).raw
+endef
+
+# Command definitions:
+# - cmd_foo is the actual command to run;
+# - quiet_cmd_foo is the brief-output summary of the command.
+
+quiet_cmd_cc = CC($(TOOLSET)) $@
+cmd_cc = $(CC.$(TOOLSET)) $(GYP_CFLAGS) $(DEPFLAGS) $(CFLAGS.$(TOOLSET)) -c -o $@ $<
+
+quiet_cmd_cxx = CXX($(TOOLSET)) $@
+cmd_cxx = $(CXX.$(TOOLSET)) $(GYP_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
+
+quiet_cmd_objc = CXX($(TOOLSET)) $@
+cmd_objc = $(CC.$(TOOLSET)) $(GYP_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<
+
+quiet_cmd_objcxx = CXX($(TOOLSET)) $@
+cmd_objcxx = $(CXX.$(TOOLSET)) $(GYP_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<
+
+# Commands for precompiled header files.
+quiet_cmd_pch_c = CXX($(TOOLSET)) $@
+cmd_pch_c = $(CC.$(TOOLSET)) $(GYP_PCH_CFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
+quiet_cmd_pch_cc = CXX($(TOOLSET)) $@
+cmd_pch_cc = $(CC.$(TOOLSET)) $(GYP_PCH_CXXFLAGS) $(DEPFLAGS) $(CXXFLAGS.$(TOOLSET)) -c -o $@ $<
+quiet_cmd_pch_m = CXX($(TOOLSET)) $@
+cmd_pch_m = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCFLAGS) $(DEPFLAGS) -c -o $@ $<
+quiet_cmd_pch_mm = CXX($(TOOLSET)) $@
+cmd_pch_mm = $(CC.$(TOOLSET)) $(GYP_PCH_OBJCXXFLAGS) $(DEPFLAGS) -c -o $@ $<
+
+# gyp-mac-tool is written next to the root Makefile by gyp.
+# Use $(4) for the command, since $(2) and $(3) are used as flag by do_cmd
+# already.
+quiet_cmd_mac_tool = MACTOOL $(4) $<
+cmd_mac_tool = ./gyp-mac-tool $(4) $< "$@"
+
+quiet_cmd_mac_package_framework = PACKAGE FRAMEWORK $@
+cmd_mac_package_framework = ./gyp-mac-tool package-framework "$@" $(4)
+
+quiet_cmd_infoplist = INFOPLIST $@
+cmd_infoplist = $(CC.$(TOOLSET)) -E -P -Wno-trigraphs -x c $(INFOPLIST_DEFINES) "$<" -o "$@"
+
+quiet_cmd_touch = TOUCH $@
+cmd_touch = touch $@
+
+quiet_cmd_copy = COPY $@
+# send stderr to /dev/null to ignore messages when linking directories.
+cmd_copy = ln -f "$<" "$@" 2>/dev/null || (rm -rf "$@" && cp -af "$<" "$@")
+
+quiet_cmd_alink = LIBTOOL-STATIC $@
+cmd_alink = rm -f $@ && ./gyp-mac-tool filter-libtool libtool -static -o $@ $(filter %.o,$^)
+
+quiet_cmd_link = LINK($(TOOLSET)) $@
+cmd_link = $(LINK.$(TOOLSET)) $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)
+
+# TODO(thakis): Find out and document the difference between shared_library and
+# loadable_module on mac.
+quiet_cmd_solink = SOLINK($(TOOLSET)) $@
+cmd_solink = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o "$@" $(LD_INPUTS) $(LIBS)
+
+# TODO(thakis): The solink_module rule is likely wrong. Xcode seems to pass
+# -bundle -single_module here (for osmesa.so).
+quiet_cmd_solink_module = SOLINK_MODULE($(TOOLSET)) $@
+cmd_solink_module = $(LINK.$(TOOLSET)) -shared $(GYP_LDFLAGS) $(LDFLAGS.$(TOOLSET)) -o $@ $(filter-out FORCE_DO_CMD, $^) $(LIBS)
+
+
+# Define an escape_quotes function to escape single quotes.
+# This allows us to handle quotes properly as long as we always use
+# use single quotes and escape_quotes.
+escape_quotes = $(subst ','\'',$(1))
+# This comment is here just to include a ' to unconfuse syntax highlighting.
+# Define an escape_vars function to escape '$' variable syntax.
+# This allows us to read/write command lines with shell variables (e.g.
+# $LD_LIBRARY_PATH), without triggering make substitution.
+escape_vars = $(subst $$,$$$$,$(1))
+# Helper that expands to a shell command to echo a string exactly as it is in
+# make. This uses printf instead of echo because printf's behaviour with respect
+# to escape sequences is more portable than echo's across different shells
+# (e.g., dash, bash).
+exact_echo = printf '%s\n' '$(call escape_quotes,$(1))'
+
+# Helper to compare the command we're about to run against the command
+# we logged the last time we ran the command. Produces an empty
+# string (false) when the commands match.
+# Tricky point: Make has no string-equality test function.
+# The kernel uses the following, but it seems like it would have false
+# positives, where one string reordered its arguments.
+# arg_check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
+# $(filter-out $(cmd_$@), $(cmd_$(1))))
+# We instead substitute each for the empty string into the other, and
+# say they're equal if both substitutions produce the empty string.
+# .d files contain ? instead of spaces, take that into account.
+command_changed = $(or $(subst $(cmd_$(1)),,$(cmd_$(call replace_spaces,$@))),\
+ $(subst $(cmd_$(call replace_spaces,$@)),,$(cmd_$(1))))
+
+# Helper that is non-empty when a prerequisite changes.
+# Normally make does this implicitly, but we force rules to always run
+# so we can check their command lines.
+# $? -- new prerequisites
+# $| -- order-only dependencies
+prereq_changed = $(filter-out FORCE_DO_CMD,$(filter-out $|,$?))
+
+# Helper that executes all postbuilds, and deletes the output file when done
+# if any of the postbuilds failed.
+define do_postbuilds
+ @E=0;\
+ for p in $(POSTBUILDS); do\
+ eval $$p;\
+ F=$$?;\
+ if [ $$F -ne 0 ]; then\
+ E=$$F;\
+ fi;\
+ done;\
+ if [ $$E -ne 0 ]; then\
+ rm -rf "$@";\
+ exit $$E;\
+ fi
+endef
+
+# do_cmd: run a command via the above cmd_foo names, if necessary.
+# Should always run for a given target to handle command-line changes.
+# Second argument, if non-zero, makes it do asm/C/C++ dependency munging.
+# Third argument, if non-zero, makes it do POSTBUILDS processing.
+# Note: We intentionally do NOT call dirx for depfile, since it contains ? for
+# spaces already and dirx strips the ? characters.
+define do_cmd
+$(if $(or $(command_changed),$(prereq_changed)),
+ @$(call exact_echo, $($(quiet)cmd_$(1)))
+ @mkdir -p "$(call dirx,$@)" "$(dir $(depfile))"
+ $(if $(findstring flock,$(word 2,$(cmd_$1))),
+ @$(cmd_$(1))
+ @echo " $(quiet_cmd_$(1)): Finished",
+ @$(cmd_$(1))
+ )
+ @$(call exact_echo,$(call escape_vars,cmd_$(call replace_spaces,$@) := $(cmd_$(1)))) > $(depfile)
+ @$(if $(2),$(fixup_dep))
+ $(if $(and $(3), $(POSTBUILDS)),
+ $(call do_postbuilds)
+ )
+)
+endef
+
+# Declare the "all" target first so it is the default,
+# even though we don't have the deps yet.
+.PHONY: all
+all:
+
+# make looks for ways to re-generate included makefiles, but in our case, we
+# don't have a direct way. Explicitly telling make that it has nothing to do
+# for them makes it go faster.
+%.d: ;
+
+# Use FORCE_DO_CMD to force a target to run. Should be coupled with
+# do_cmd.
+.PHONY: FORCE_DO_CMD
+FORCE_DO_CMD:
+
+TOOLSET := target
+# Suffix rules, putting all outputs into $(obj).
+$(obj).$(TOOLSET)/%.o: $(srcdir)/%.c FORCE_DO_CMD
+ @$(call do_cmd,cc,1)
+$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cc FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cpp FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+$(obj).$(TOOLSET)/%.o: $(srcdir)/%.cxx FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+$(obj).$(TOOLSET)/%.o: $(srcdir)/%.m FORCE_DO_CMD
+ @$(call do_cmd,objc,1)
+$(obj).$(TOOLSET)/%.o: $(srcdir)/%.mm FORCE_DO_CMD
+ @$(call do_cmd,objcxx,1)
+$(obj).$(TOOLSET)/%.o: $(srcdir)/%.S FORCE_DO_CMD
+ @$(call do_cmd,cc,1)
+$(obj).$(TOOLSET)/%.o: $(srcdir)/%.s FORCE_DO_CMD
+ @$(call do_cmd,cc,1)
+
+# Try building from generated source, too.
+$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.c FORCE_DO_CMD
+ @$(call do_cmd,cc,1)
+$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cc FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cpp FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.cxx FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.m FORCE_DO_CMD
+ @$(call do_cmd,objc,1)
+$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.mm FORCE_DO_CMD
+ @$(call do_cmd,objcxx,1)
+$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.S FORCE_DO_CMD
+ @$(call do_cmd,cc,1)
+$(obj).$(TOOLSET)/%.o: $(obj).$(TOOLSET)/%.s FORCE_DO_CMD
+ @$(call do_cmd,cc,1)
+
+$(obj).$(TOOLSET)/%.o: $(obj)/%.c FORCE_DO_CMD
+ @$(call do_cmd,cc,1)
+$(obj).$(TOOLSET)/%.o: $(obj)/%.cc FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+$(obj).$(TOOLSET)/%.o: $(obj)/%.cpp FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+$(obj).$(TOOLSET)/%.o: $(obj)/%.cxx FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+$(obj).$(TOOLSET)/%.o: $(obj)/%.m FORCE_DO_CMD
+ @$(call do_cmd,objc,1)
+$(obj).$(TOOLSET)/%.o: $(obj)/%.mm FORCE_DO_CMD
+ @$(call do_cmd,objcxx,1)
+$(obj).$(TOOLSET)/%.o: $(obj)/%.S FORCE_DO_CMD
+ @$(call do_cmd,cc,1)
+$(obj).$(TOOLSET)/%.o: $(obj)/%.s FORCE_DO_CMD
+ @$(call do_cmd,cc,1)
+
+
+ifeq ($(strip $(foreach prefix,$(NO_LOAD),\
+ $(findstring $(join ^,$(prefix)),\
+ $(join ^,bson.target.mk)))),)
+ include bson.target.mk
+endif
+
+quiet_cmd_regen_makefile = ACTION Regenerating $@
+cmd_regen_makefile = /Users/ck/.node-gyp/0.8.11/tools/gyp/gyp -fmake --ignore-environment "--toplevel-dir=." -I/Users/ck/coding/projects/js-bson/build/config.gypi -I/usr/local/lib/node_modules/node-gyp/addon.gypi -I/Users/ck/.node-gyp/0.8.11/common.gypi "--depth=." "-Goutput_dir=." "--generator-output=build" "-Dlibrary=shared_library" "-Dvisibility=default" "-Dnode_root_dir=/Users/ck/.node-gyp/0.8.11" "-Dmodule_root_dir=/Users/ck/coding/projects/js-bson" binding.gyp
+Makefile: $(srcdir)/../../../.node-gyp/0.8.11/common.gypi $(srcdir)/build/config.gypi $(srcdir)/binding.gyp $(srcdir)/../../../../../usr/local/lib/node_modules/node-gyp/addon.gypi
+ $(call do_cmd,regen_makefile)
+
+# "all" is a concatenation of the "all" targets from all the included
+# sub-makefiles. This is just here to clarify.
+all:
+
+# Add in dependency-tracking rules. $(all_deps) is the list of every single
+# target in our tree. Only consider the ones with .d (dependency) info:
+d_files := $(wildcard $(foreach f,$(all_deps),$(depsdir)/$(f).d))
+ifneq ($(d_files),)
+ include $(d_files)
+endif
diff --git a/node_modules/mongodb/node_modules/bson/build/Release/.deps/Release/bson.node.d b/node_modules/mongodb/node_modules/bson/build/Release/.deps/Release/bson.node.d
new file mode 100644
index 0000000..20963f4
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/build/Release/.deps/Release/bson.node.d
@@ -0,0 +1 @@
+cmd_Release/bson.node := ./gyp-mac-tool flock ./Release/linker.lock c++ -shared -Wl,-search_paths_first -mmacosx-version-min=10.5 -arch x86_64 -L./Release -install_name /usr/local/lib/bson.node -o Release/bson.node Release/obj.target/bson/ext/bson.o -undefined dynamic_lookup
diff --git a/node_modules/mongodb/node_modules/bson/build/Release/.deps/Release/obj.target/bson/ext/bson.o.d b/node_modules/mongodb/node_modules/bson/build/Release/.deps/Release/obj.target/bson/ext/bson.o.d
new file mode 100644
index 0000000..f224e00
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/build/Release/.deps/Release/obj.target/bson/ext/bson.o.d
@@ -0,0 +1,34 @@
+cmd_Release/obj.target/bson/ext/bson.o := c++ '-D_LARGEFILE_SOURCE' '-D_FILE_OFFSET_BITS=64' '-D_DARWIN_USE_64_BIT_INODE=1' -I/Users/ck/.node-gyp/0.8.11/src -I/Users/ck/.node-gyp/0.8.11/deps/uv/include -I/Users/ck/.node-gyp/0.8.11/deps/v8/include -Os -gdwarf-2 -mmacosx-version-min=10.5 -arch x86_64 -Wall -Wendif-labels -W -Wno-unused-parameter -fno-rtti -fno-threadsafe-statics -fno-strict-aliasing -MMD -MF ./Release/.deps/Release/obj.target/bson/ext/bson.o.d.raw -c -o Release/obj.target/bson/ext/bson.o ../ext/bson.cc
+Release/obj.target/bson/ext/bson.o: ../ext/bson.cc \
+ /Users/ck/.node-gyp/0.8.11/deps/v8/include/v8.h \
+ /Users/ck/.node-gyp/0.8.11/deps/v8/include/v8stdint.h \
+ /Users/ck/.node-gyp/0.8.11/src/node.h \
+ /Users/ck/.node-gyp/0.8.11/deps/uv/include/uv.h \
+ /Users/ck/.node-gyp/0.8.11/deps/uv/include/ares.h \
+ /Users/ck/.node-gyp/0.8.11/deps/uv/include/ares_version.h \
+ /Users/ck/.node-gyp/0.8.11/deps/uv/include/uv-private/uv-unix.h \
+ /Users/ck/.node-gyp/0.8.11/deps/uv/include/uv-private/ngx-queue.h \
+ /Users/ck/.node-gyp/0.8.11/deps/uv/include/uv-private/ev.h \
+ /Users/ck/.node-gyp/0.8.11/deps/uv/include/uv-private/eio.h \
+ /Users/ck/.node-gyp/0.8.11/src/node_object_wrap.h \
+ /Users/ck/.node-gyp/0.8.11/src/ev-emul.h \
+ /Users/ck/.node-gyp/0.8.11/src/eio-emul.h \
+ /Users/ck/.node-gyp/0.8.11/src/node_version.h \
+ /Users/ck/.node-gyp/0.8.11/src/node_buffer.h ../ext/bson.h
+../ext/bson.cc:
+/Users/ck/.node-gyp/0.8.11/deps/v8/include/v8.h:
+/Users/ck/.node-gyp/0.8.11/deps/v8/include/v8stdint.h:
+/Users/ck/.node-gyp/0.8.11/src/node.h:
+/Users/ck/.node-gyp/0.8.11/deps/uv/include/uv.h:
+/Users/ck/.node-gyp/0.8.11/deps/uv/include/ares.h:
+/Users/ck/.node-gyp/0.8.11/deps/uv/include/ares_version.h:
+/Users/ck/.node-gyp/0.8.11/deps/uv/include/uv-private/uv-unix.h:
+/Users/ck/.node-gyp/0.8.11/deps/uv/include/uv-private/ngx-queue.h:
+/Users/ck/.node-gyp/0.8.11/deps/uv/include/uv-private/ev.h:
+/Users/ck/.node-gyp/0.8.11/deps/uv/include/uv-private/eio.h:
+/Users/ck/.node-gyp/0.8.11/src/node_object_wrap.h:
+/Users/ck/.node-gyp/0.8.11/src/ev-emul.h:
+/Users/ck/.node-gyp/0.8.11/src/eio-emul.h:
+/Users/ck/.node-gyp/0.8.11/src/node_version.h:
+/Users/ck/.node-gyp/0.8.11/src/node_buffer.h:
+../ext/bson.h:
diff --git a/node_modules/mongodb/node_modules/bson/build/Release/bson.node b/node_modules/mongodb/node_modules/bson/build/Release/bson.node
new file mode 100644
index 0000000..f8f0779
Binary files /dev/null and b/node_modules/mongodb/node_modules/bson/build/Release/bson.node differ
diff --git a/node_modules/mongodb/node_modules/bson/build/Release/linker.lock b/node_modules/mongodb/node_modules/bson/build/Release/linker.lock
new file mode 100644
index 0000000..e69de29
diff --git a/node_modules/mongodb/node_modules/bson/build/Release/obj.target/bson/ext/bson.o b/node_modules/mongodb/node_modules/bson/build/Release/obj.target/bson/ext/bson.o
new file mode 100644
index 0000000..af87b6b
Binary files /dev/null and b/node_modules/mongodb/node_modules/bson/build/Release/obj.target/bson/ext/bson.o differ
diff --git a/node_modules/mongodb/node_modules/bson/build/binding.Makefile b/node_modules/mongodb/node_modules/bson/build/binding.Makefile
new file mode 100644
index 0000000..90bf824
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/build/binding.Makefile
@@ -0,0 +1,6 @@
+# This file is generated by gyp; do not edit.
+
+export builddir_name ?= build/./.
+.PHONY: all
+all:
+ $(MAKE) bson
diff --git a/node_modules/mongodb/node_modules/bson/build/bson.target.mk b/node_modules/mongodb/node_modules/bson/build/bson.target.mk
new file mode 100644
index 0000000..f41c6bd
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/build/bson.target.mk
@@ -0,0 +1,145 @@
+# This file is generated by gyp; do not edit.
+
+TOOLSET := target
+TARGET := bson
+DEFS_Debug := \
+ '-D_LARGEFILE_SOURCE' \
+ '-D_FILE_OFFSET_BITS=64' \
+ '-D_DARWIN_USE_64_BIT_INODE=1' \
+ '-DDEBUG' \
+ '-D_DEBUG'
+
+# Flags passed to all source files.
+CFLAGS_Debug := \
+ -Os \
+ -gdwarf-2 \
+ -mmacosx-version-min=10.5 \
+ -arch x86_64 \
+ -Wall \
+ -Wendif-labels \
+ -W \
+ -Wno-unused-parameter
+
+# Flags passed to only C files.
+CFLAGS_C_Debug := \
+ -fno-strict-aliasing
+
+# Flags passed to only C++ files.
+CFLAGS_CC_Debug := \
+ -fno-rtti \
+ -fno-threadsafe-statics \
+ -fno-strict-aliasing
+
+# Flags passed to only ObjC files.
+CFLAGS_OBJC_Debug :=
+
+# Flags passed to only ObjC++ files.
+CFLAGS_OBJCC_Debug :=
+
+INCS_Debug := \
+ -I/Users/ck/.node-gyp/0.8.11/src \
+ -I/Users/ck/.node-gyp/0.8.11/deps/uv/include \
+ -I/Users/ck/.node-gyp/0.8.11/deps/v8/include
+
+DEFS_Release := \
+ '-D_LARGEFILE_SOURCE' \
+ '-D_FILE_OFFSET_BITS=64' \
+ '-D_DARWIN_USE_64_BIT_INODE=1'
+
+# Flags passed to all source files.
+CFLAGS_Release := \
+ -Os \
+ -gdwarf-2 \
+ -mmacosx-version-min=10.5 \
+ -arch x86_64 \
+ -Wall \
+ -Wendif-labels \
+ -W \
+ -Wno-unused-parameter
+
+# Flags passed to only C files.
+CFLAGS_C_Release := \
+ -fno-strict-aliasing
+
+# Flags passed to only C++ files.
+CFLAGS_CC_Release := \
+ -fno-rtti \
+ -fno-threadsafe-statics \
+ -fno-strict-aliasing
+
+# Flags passed to only ObjC files.
+CFLAGS_OBJC_Release :=
+
+# Flags passed to only ObjC++ files.
+CFLAGS_OBJCC_Release :=
+
+INCS_Release := \
+ -I/Users/ck/.node-gyp/0.8.11/src \
+ -I/Users/ck/.node-gyp/0.8.11/deps/uv/include \
+ -I/Users/ck/.node-gyp/0.8.11/deps/v8/include
+
+OBJS := \
+ $(obj).target/$(TARGET)/ext/bson.o
+
+# Add to the list of files we specially track dependencies for.
+all_deps += $(OBJS)
+
+# CFLAGS et al overrides must be target-local.
+# See "Target-specific Variable Values" in the GNU Make manual.
+$(OBJS): TOOLSET := $(TOOLSET)
+$(OBJS): GYP_CFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE))
+$(OBJS): GYP_CXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE))
+$(OBJS): GYP_OBJCFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_C_$(BUILDTYPE)) $(CFLAGS_OBJC_$(BUILDTYPE))
+$(OBJS): GYP_OBJCXXFLAGS := $(DEFS_$(BUILDTYPE)) $(INCS_$(BUILDTYPE)) $(CFLAGS_$(BUILDTYPE)) $(CFLAGS_CC_$(BUILDTYPE)) $(CFLAGS_OBJCC_$(BUILDTYPE))
+
+# Suffix rules, putting all outputs into $(obj).
+
+$(obj).$(TOOLSET)/$(TARGET)/%.o: $(srcdir)/%.cc FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+
+# Try building from generated source, too.
+
+$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj).$(TOOLSET)/%.cc FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+
+$(obj).$(TOOLSET)/$(TARGET)/%.o: $(obj)/%.cc FORCE_DO_CMD
+ @$(call do_cmd,cxx,1)
+
+# End of this set of suffix rules
+### Rules for final target.
+LDFLAGS_Debug := \
+ -Wl,-search_paths_first \
+ -mmacosx-version-min=10.5 \
+ -arch x86_64 \
+ -L$(builddir) \
+ -install_name /usr/local/lib/bson.node
+
+LDFLAGS_Release := \
+ -Wl,-search_paths_first \
+ -mmacosx-version-min=10.5 \
+ -arch x86_64 \
+ -L$(builddir) \
+ -install_name /usr/local/lib/bson.node
+
+LIBS := \
+ -undefined dynamic_lookup
+
+$(builddir)/bson.node: GYP_LDFLAGS := $(LDFLAGS_$(BUILDTYPE))
+$(builddir)/bson.node: LIBS := $(LIBS)
+$(builddir)/bson.node: TOOLSET := $(TOOLSET)
+$(builddir)/bson.node: $(OBJS) FORCE_DO_CMD
+ $(call do_cmd,solink_module)
+
+all_deps += $(builddir)/bson.node
+# Add target alias
+.PHONY: bson
+bson: $(builddir)/bson.node
+
+# Short alias for building this executable.
+.PHONY: bson.node
+bson.node: $(builddir)/bson.node
+
+# Add executable to "all" target.
+.PHONY: all
+all: $(builddir)/bson.node
+
diff --git a/node_modules/mongodb/node_modules/bson/build/config.gypi b/node_modules/mongodb/node_modules/bson/build/config.gypi
new file mode 100644
index 0000000..431cd3c
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/build/config.gypi
@@ -0,0 +1,28 @@
+# Do not edit. File was generated by node-gyp's "configure" step
+{
+ "target_defaults": {
+ "cflags": [],
+ "default_configuration": "Release",
+ "defines": [],
+ "include_dirs": [],
+ "libraries": []
+ },
+ "variables": {
+ "clang": 1,
+ "host_arch": "x64",
+ "node_install_npm": "true",
+ "node_install_waf": "true",
+ "node_prefix": "",
+ "node_shared_openssl": "false",
+ "node_shared_v8": "false",
+ "node_shared_zlib": "false",
+ "node_use_dtrace": "false",
+ "node_use_etw": "false",
+ "node_use_openssl": "true",
+ "target_arch": "x64",
+ "v8_no_strict_aliasing": 1,
+ "v8_use_snapshot": "true",
+ "nodedir": "/Users/ck/.node-gyp/0.8.11",
+ "copy_dev_lib": "true"
+ }
+}
diff --git a/node_modules/mongodb/node_modules/bson/build/gyp-mac-tool b/node_modules/mongodb/node_modules/bson/build/gyp-mac-tool
new file mode 100644
index 0000000..22f8331
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/build/gyp-mac-tool
@@ -0,0 +1,210 @@
+#!/usr/bin/env python
+# Generated by gyp. Do not edit.
+# Copyright (c) 2012 Google Inc. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Utility functions to perform Xcode-style build steps.
+
+These functions are executed via gyp-mac-tool when using the Makefile generator.
+"""
+
+import fcntl
+import os
+import plistlib
+import re
+import shutil
+import string
+import subprocess
+import sys
+
+
+def main(args):
+ executor = MacTool()
+ exit_code = executor.Dispatch(args)
+ if exit_code is not None:
+ sys.exit(exit_code)
+
+
+class MacTool(object):
+ """This class performs all the Mac tooling steps. The methods can either be
+ executed directly, or dispatched from an argument list."""
+
+ def Dispatch(self, args):
+ """Dispatches a string command to a method."""
+ if len(args) < 1:
+ raise Exception("Not enough arguments")
+
+ method = "Exec%s" % self._CommandifyName(args[0])
+ return getattr(self, method)(*args[1:])
+
+ def _CommandifyName(self, name_string):
+ """Transforms a tool name like copy-info-plist to CopyInfoPlist"""
+ return name_string.title().replace('-', '')
+
+ def ExecCopyBundleResource(self, source, dest):
+ """Copies a resource file to the bundle/Resources directory, performing any
+ necessary compilation on each resource."""
+ extension = os.path.splitext(source)[1].lower()
+ if os.path.isdir(source):
+ # Copy tree.
+ if os.path.exists(dest):
+ shutil.rmtree(dest)
+ shutil.copytree(source, dest)
+ elif extension == '.xib':
+ return self._CopyXIBFile(source, dest)
+ elif extension == '.strings':
+ self._CopyStringsFile(source, dest)
+ else:
+ shutil.copyfile(source, dest)
+
+ def _CopyXIBFile(self, source, dest):
+ """Compiles a XIB file with ibtool into a binary plist in the bundle."""
+ tools_dir = os.environ.get('DEVELOPER_BIN_DIR', '/usr/bin')
+ args = [os.path.join(tools_dir, 'ibtool'), '--errors', '--warnings',
+ '--notices', '--output-format', 'human-readable-text', '--compile',
+ dest, source]
+ ibtool_section_re = re.compile(r'/\*.*\*/')
+ ibtool_re = re.compile(r'.*note:.*is clipping its content')
+ ibtoolout = subprocess.Popen(args, stdout=subprocess.PIPE)
+ current_section_header = None
+ for line in ibtoolout.stdout:
+ if ibtool_section_re.match(line):
+ current_section_header = line
+ elif not ibtool_re.match(line):
+ if current_section_header:
+ sys.stdout.write(current_section_header)
+ current_section_header = None
+ sys.stdout.write(line)
+ return ibtoolout.returncode
+
+ def _CopyStringsFile(self, source, dest):
+ """Copies a .strings file using iconv to reconvert the input into UTF-16."""
+ input_code = self._DetectInputEncoding(source) or "UTF-8"
+ fp = open(dest, 'w')
+ args = ['/usr/bin/iconv', '--from-code', input_code, '--to-code',
+ 'UTF-16', source]
+ subprocess.call(args, stdout=fp)
+ fp.close()
+
+ def _DetectInputEncoding(self, file_name):
+ """Reads the first few bytes from file_name and tries to guess the text
+ encoding. Returns None as a guess if it can't detect it."""
+ fp = open(file_name, 'rb')
+ try:
+ header = fp.read(3)
+ except e:
+ fp.close()
+ return None
+ fp.close()
+ if header.startswith("\xFE\xFF"):
+ return "UTF-16BE"
+ elif header.startswith("\xFF\xFE"):
+ return "UTF-16LE"
+ elif header.startswith("\xEF\xBB\xBF"):
+ return "UTF-8"
+ else:
+ return None
+
+ def ExecCopyInfoPlist(self, source, dest):
+ """Copies the |source| Info.plist to the destination directory |dest|."""
+ # Read the source Info.plist into memory.
+ fd = open(source, 'r')
+ lines = fd.read()
+ fd.close()
+
+ # Go through all the environment variables and replace them as variables in
+ # the file.
+ for key in os.environ:
+ if key.startswith('_'):
+ continue
+ evar = '${%s}' % key
+ lines = string.replace(lines, evar, os.environ[key])
+
+ # Write out the file with variables replaced.
+ fd = open(dest, 'w')
+ fd.write(lines)
+ fd.close()
+
+ # Now write out PkgInfo file now that the Info.plist file has been
+ # "compiled".
+ self._WritePkgInfo(dest)
+
+ def _WritePkgInfo(self, info_plist):
+ """This writes the PkgInfo file from the data stored in Info.plist."""
+ plist = plistlib.readPlist(info_plist)
+ if not plist:
+ return
+
+ # Only create PkgInfo for executable types.
+ package_type = plist['CFBundlePackageType']
+ if package_type != 'APPL':
+ return
+
+ # The format of PkgInfo is eight characters, representing the bundle type
+ # and bundle signature, each four characters. If that is missing, four
+ # '?' characters are used instead.
+ signature_code = plist.get('CFBundleSignature', '????')
+ if len(signature_code) != 4: # Wrong length resets everything, too.
+ signature_code = '?' * 4
+
+ dest = os.path.join(os.path.dirname(info_plist), 'PkgInfo')
+ fp = open(dest, 'w')
+ fp.write('%s%s' % (package_type, signature_code))
+ fp.close()
+
+ def ExecFlock(self, lockfile, *cmd_list):
+ """Emulates the most basic behavior of Linux's flock(1)."""
+ # Rely on exception handling to report errors.
+ fd = os.open(lockfile, os.O_RDONLY|os.O_NOCTTY|os.O_CREAT, 0o666)
+ fcntl.flock(fd, fcntl.LOCK_EX)
+ return subprocess.call(cmd_list)
+
+ def ExecFilterLibtool(self, *cmd_list):
+ """Calls libtool and filters out 'libtool: file: foo.o has no symbols'."""
+ libtool_re = re.compile(r'^libtool: file: .* has no symbols$')
+ libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE)
+ for line in libtoolout.stderr:
+ if not libtool_re.match(line):
+ sys.stderr.write(line)
+ return libtoolout.returncode
+
+ def ExecPackageFramework(self, framework, version):
+ """Takes a path to Something.framework and the Current version of that and
+ sets up all the symlinks."""
+ # Find the name of the binary based on the part before the ".framework".
+ binary = os.path.basename(framework).split('.')[0]
+
+ CURRENT = 'Current'
+ RESOURCES = 'Resources'
+ VERSIONS = 'Versions'
+
+ if not os.path.exists(os.path.join(framework, VERSIONS, version, binary)):
+ # Binary-less frameworks don't seem to contain symlinks (see e.g.
+ # chromium's out/Debug/org.chromium.Chromium.manifest/ bundle).
+ return
+
+ # Move into the framework directory to set the symlinks correctly.
+ pwd = os.getcwd()
+ os.chdir(framework)
+
+ # Set up the Current version.
+ self._Relink(version, os.path.join(VERSIONS, CURRENT))
+
+ # Set up the root symlinks.
+ self._Relink(os.path.join(VERSIONS, CURRENT, binary), binary)
+ self._Relink(os.path.join(VERSIONS, CURRENT, RESOURCES), RESOURCES)
+
+ # Back to where we were before!
+ os.chdir(pwd)
+
+ def _Relink(self, dest, link):
+ """Creates a symlink to |dest| named |link|. If |link| already exists,
+ it is overwritten."""
+ if os.path.lexists(link):
+ os.remove(link)
+ os.symlink(dest, link)
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv[1:]))
diff --git a/node_modules/mongodb/node_modules/bson/ext/Makefile b/node_modules/mongodb/node_modules/bson/ext/Makefile
new file mode 100644
index 0000000..435999e
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/ext/Makefile
@@ -0,0 +1,28 @@
+NODE = node
+name = all
+JOBS = 1
+
+all:
+ rm -rf build .lock-wscript bson.node
+ node-waf configure build
+ cp -R ./build/Release/bson.node . || true
+
+all_debug:
+ rm -rf build .lock-wscript bson.node
+ node-waf --debug configure build
+ cp -R ./build/Release/bson.node . || true
+
+clang:
+ rm -rf build .lock-wscript bson.node
+ CXX=clang node-waf configure build
+ cp -R ./build/Release/bson.node . || true
+
+clang_debug:
+ rm -rf build .lock-wscript bson.node
+ CXX=clang node-waf --debug configure build
+ cp -R ./build/Release/bson.node . || true
+
+clean:
+ rm -rf build .lock-wscript bson.node
+
+.PHONY: all
\ No newline at end of file
diff --git a/node_modules/mongodb/node_modules/bson/ext/bson.cc b/node_modules/mongodb/node_modules/bson/ext/bson.cc
new file mode 100644
index 0000000..01e4c28
--- /dev/null
+++ b/node_modules/mongodb/node_modules/bson/ext/bson.cc
@@ -0,0 +1,1020 @@
+//===========================================================================
+
+#include
+#include
+#include
+#include
+#include
+
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wunused-parameter"
+#endif
+
+#include
+
+// this and the above block must be around the v8.h header otherwise
+// v8 is not happy
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif
+
+#include
+#include
+#include
+
+#include
+#include
+#include
+#include
+
+#ifdef __sun
+ #include
+#endif
+
+#include "bson.h"
+
+using namespace v8;
+using namespace node;
+
+//===========================================================================
+
+void DataStream::WriteObjectId(const Handle