555aa79721339288b83f6896b59e1515b79ab7a4
[aai/esr-gui.git] /
1 var Long = require('../lib/bson/long').Long
2   , Double = require('../lib/bson/double').Double
3   , Timestamp = require('../lib/bson/timestamp').Timestamp
4   , ObjectID = require('../lib/bson/objectid').ObjectID
5   , Symbol = require('../lib/bson/symbol').Symbol
6   , Code = require('../lib/bson/code').Code
7   , MinKey = require('../lib/bson/min_key').MinKey
8   , MaxKey = require('../lib/bson/max_key').MaxKey
9   , DBRef = require('../lib/bson/db_ref').DBRef
10   , Binary = require('../lib/bson/binary').Binary
11   , BinaryParser = require('../lib/bson/binary_parser').BinaryParser
12   , writeIEEE754 = require('../lib/bson/float_parser').writeIEEE754
13   , readIEEE754 = require('../lib/bson/float_parser').readIEEE754
14
15 // To ensure that 0.4 of node works correctly
16 var isDate = function isDate(d) {
17   return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
18 }
19
20 /**
21  * Create a new BSON instance
22  *
23  * @class
24  * @return {BSON} instance of BSON Parser.
25  */
26 function BSON () {};
27
28 /**
29  * @ignore
30  * @api private
31  */
32 // BSON MAX VALUES
33 BSON.BSON_INT32_MAX = 0x7FFFFFFF;
34 BSON.BSON_INT32_MIN = -0x80000000;
35
36 BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
37 BSON.BSON_INT64_MIN = -Math.pow(2, 63);
38
39 // JS MAX PRECISE VALUES
40 BSON.JS_INT_MAX = 0x20000000000000;  // Any integer up to 2^53 can be precisely represented by a double.
41 BSON.JS_INT_MIN = -0x20000000000000;  // Any integer down to -2^53 can be precisely represented by a double.
42
43 // Internal long versions
44 var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000);  // Any integer up to 2^53 can be precisely represented by a double.
45 var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000);  // Any integer down to -2^53 can be precisely represented by a double.
46
47 /**
48  * Number BSON Type
49  *
50  * @classconstant BSON_DATA_NUMBER
51  **/
52 BSON.BSON_DATA_NUMBER = 1;
53 /**
54  * String BSON Type
55  *
56  * @classconstant BSON_DATA_STRING
57  **/
58 BSON.BSON_DATA_STRING = 2;
59 /**
60  * Object BSON Type
61  *
62  * @classconstant BSON_DATA_OBJECT
63  **/
64 BSON.BSON_DATA_OBJECT = 3;
65 /**
66  * Array BSON Type
67  *
68  * @classconstant BSON_DATA_ARRAY
69  **/
70 BSON.BSON_DATA_ARRAY = 4;
71 /**
72  * Binary BSON Type
73  *
74  * @classconstant BSON_DATA_BINARY
75  **/
76 BSON.BSON_DATA_BINARY = 5;
77 /**
78  * Binary BSON Type
79  *
80  * @classconstant BSON_DATA_UNDEFINED
81  **/
82 BSON.BSON_DATA_UNDEFINED = 6;
83 /**
84  * ObjectID BSON Type
85  *
86  * @classconstant BSON_DATA_OID
87  **/
88 BSON.BSON_DATA_OID = 7;
89 /**
90  * Boolean BSON Type
91  *
92  * @classconstant BSON_DATA_BOOLEAN
93  **/
94 BSON.BSON_DATA_BOOLEAN = 8;
95 /**
96  * Date BSON Type
97  *
98  * @classconstant BSON_DATA_DATE
99  **/
100 BSON.BSON_DATA_DATE = 9;
101 /**
102  * null BSON Type
103  *
104  * @classconstant BSON_DATA_NULL
105  **/
106 BSON.BSON_DATA_NULL = 10;
107 /**
108  * RegExp BSON Type
109  *
110  * @classconstant BSON_DATA_REGEXP
111  **/
112 BSON.BSON_DATA_REGEXP = 11;
113 /**
114  * Code BSON Type
115  *
116  * @classconstant BSON_DATA_CODE
117  **/
118 BSON.BSON_DATA_CODE = 13;
119 /**
120  * Symbol BSON Type
121  *
122  * @classconstant BSON_DATA_SYMBOL
123  **/
124 BSON.BSON_DATA_SYMBOL = 14;
125 /**
126  * Code with Scope BSON Type
127  *
128  * @classconstant BSON_DATA_CODE_W_SCOPE
129  **/
130 BSON.BSON_DATA_CODE_W_SCOPE = 15;
131 /**
132  * 32 bit Integer BSON Type
133  *
134  * @classconstant BSON_DATA_INT
135  **/
136 BSON.BSON_DATA_INT = 16;
137 /**
138  * Timestamp BSON Type
139  *
140  * @classconstant BSON_DATA_TIMESTAMP
141  **/
142 BSON.BSON_DATA_TIMESTAMP = 17;
143 /**
144  * Long BSON Type
145  *
146  * @classconstant BSON_DATA_LONG
147  **/
148 BSON.BSON_DATA_LONG = 18;
149 /**
150  * MinKey BSON Type
151  *
152  * @classconstant BSON_DATA_MIN_KEY
153  **/
154 BSON.BSON_DATA_MIN_KEY = 0xff;
155 /**
156  * MaxKey BSON Type
157  *
158  * @classconstant BSON_DATA_MAX_KEY
159  **/
160 BSON.BSON_DATA_MAX_KEY = 0x7f;
161
162 /**
163  * Binary Default Type
164  *
165  * @classconstant BSON_BINARY_SUBTYPE_DEFAULT
166  **/
167 BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
168 /**
169  * Binary Function Type
170  *
171  * @classconstant BSON_BINARY_SUBTYPE_FUNCTION
172  **/
173 BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
174 /**
175  * Binary Byte Array Type
176  *
177  * @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
178  **/
179 BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
180 /**
181  * Binary UUID Type
182  *
183  * @classconstant BSON_BINARY_SUBTYPE_UUID
184  **/
185 BSON.BSON_BINARY_SUBTYPE_UUID = 3;
186 /**
187  * Binary MD5 Type
188  *
189  * @classconstant BSON_BINARY_SUBTYPE_MD5
190  **/
191 BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
192 /**
193  * Binary User Defined Type
194  *
195  * @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
196  **/
197 BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
198
199 /**
200  * Calculate the bson size for a passed in Javascript object.
201  *
202  * @param {Object} object the Javascript object to calculate the BSON byte size for.
203  * @param {Boolean} [serializeFunctions] serialize all functions in the object **(default:false)**.
204  * @return {Number} returns the number of bytes the BSON object will take up.
205  * @api public
206  */
207 BSON.calculateObjectSize = function calculateObjectSize(object, serializeFunctions) {
208   var totalLength = (4 + 1);
209
210   if(Array.isArray(object)) {
211     for(var i = 0; i < object.length; i++) {
212       totalLength += calculateElement(i.toString(), object[i], serializeFunctions)
213     }
214   } else {
215                 // If we have toBSON defined, override the current object
216                 if(object.toBSON) {
217                         object = object.toBSON();
218                 }
219
220                 // Calculate size
221     for(var key in object) {
222       totalLength += calculateElement(key, object[key], serializeFunctions)
223     }
224   }
225
226   return totalLength;
227 }
228
229 /**
230  * @ignore
231  * @api private
232  */
233 function calculateElement(name, value, serializeFunctions) {
234   var isBuffer = typeof Buffer !== 'undefined';
235
236   // If we have toBSON defined, override the current object
237   if(value && value.toBSON){
238     value = value.toBSON();
239   }
240
241   switch(typeof value) {
242     case 'string':
243       return 1 + (!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1 + 4 + (!isBuffer ? numberOfBytes(value) : Buffer.byteLength(value, 'utf8')) + 1;
244     case 'number':
245       if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
246         if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) { // 32 bit
247           return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (4 + 1);
248         } else {
249           return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
250         }
251       } else {  // 64 bit
252         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
253       }
254     case 'undefined':
255       return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1);
256     case 'boolean':
257       return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1 + 1);
258     case 'object':
259       if(value == null || value instanceof MinKey || value instanceof MaxKey || value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
260         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1);
261       } else if(value instanceof ObjectID || value['_bsontype'] == 'ObjectID') {
262         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (12 + 1);
263       } else if(value instanceof Date || isDate(value)) {
264         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
265       } else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
266         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1 + 4 + 1) + value.length;
267       } else if(value instanceof Long || value instanceof Double || value instanceof Timestamp
268           || value['_bsontype'] == 'Long' || value['_bsontype'] == 'Double' || value['_bsontype'] == 'Timestamp') {
269         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
270       } else if(value instanceof Code || value['_bsontype'] == 'Code') {
271         // Calculate size depending on the availability of a scope
272         if(value.scope != null && Object.keys(value.scope).length > 0) {
273           return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + 4 + (!isBuffer ? numberOfBytes(value.code.toString()) : Buffer.byteLength(value.code.toString(), 'utf8')) + 1 + BSON.calculateObjectSize(value.scope, serializeFunctions);
274         } else {
275           return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + (!isBuffer ? numberOfBytes(value.code.toString()) : Buffer.byteLength(value.code.toString(), 'utf8')) + 1;
276         }
277       } else if(value instanceof Binary || value['_bsontype'] == 'Binary') {
278         // Check what kind of subtype we have
279         if(value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) {
280           return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (value.position + 1 + 4 + 1 + 4);
281         } else {
282           return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (value.position + 1 + 4 + 1);
283         }
284       } else if(value instanceof Symbol || value['_bsontype'] == 'Symbol') {
285         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + ((!isBuffer ? numberOfBytes(value.value) : Buffer.byteLength(value.value, 'utf8')) + 4 + 1 + 1);
286       } else if(value instanceof DBRef || value['_bsontype'] == 'DBRef') {
287         // Set up correct object for serialization
288         var ordered_values = {
289             '$ref': value.namespace
290           , '$id' : value.oid
291         };
292
293         // Add db reference if it exists
294         if(null != value.db) {
295           ordered_values['$db'] = value.db;
296         }
297
298         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + BSON.calculateObjectSize(ordered_values, serializeFunctions);
299       } else if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]') {
300           return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + (!isBuffer ? numberOfBytes(value.source) : Buffer.byteLength(value.source, 'utf8')) + 1
301             + (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1
302       } else {
303         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + BSON.calculateObjectSize(value, serializeFunctions) + 1;
304       }
305     case 'function':
306       // WTF for 0.4.X where typeof /someregexp/ === 'function'
307       if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]' || String.call(value) == '[object RegExp]') {
308         return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + (!isBuffer ? numberOfBytes(value.source) : Buffer.byteLength(value.source, 'utf8')) + 1
309           + (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1
310       } else {
311         if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
312           return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + 4 + (!isBuffer ? numberOfBytes(value.toString()) : Buffer.byteLength(value.toString(), 'utf8')) + 1 + BSON.calculateObjectSize(value.scope, serializeFunctions);
313         } else if(serializeFunctions) {
314           return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + 1 + 4 + (!isBuffer ? numberOfBytes(value.toString()) : Buffer.byteLength(value.toString(), 'utf8')) + 1;
315         }
316       }
317   }
318
319   return 0;
320 }
321
322 /**
323  * Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
324  *
325  * @param {Object} object the Javascript object to serialize.
326  * @param {Boolean} checkKeys the serializer will check if keys are valid.
327  * @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
328  * @param {Number} index the index in the buffer where we wish to start serializing into.
329  * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
330  * @return {Number} returns the new write index in the Buffer.
331  * @api public
332  */
333 BSON.serializeWithBufferAndIndex = function serializeWithBufferAndIndex(object, checkKeys, buffer, index, serializeFunctions) {
334   // Default setting false
335   serializeFunctions = serializeFunctions == null ? false : serializeFunctions;
336   // Write end information (length of the object)
337   var size = buffer.length;
338   // Write the size of the object
339   buffer[index++] = size & 0xff;
340   buffer[index++] = (size >> 8) & 0xff;
341   buffer[index++] = (size >> 16) & 0xff;
342   buffer[index++] = (size >> 24) & 0xff;
343   return serializeObject(object, checkKeys, buffer, index, serializeFunctions) - 1;
344 }
345
346 /**
347  * @ignore
348  * @api private
349  */
350 var serializeObject = function(object, checkKeys, buffer, index, serializeFunctions) {
351   if(object.toBSON) {
352     if(typeof object.toBSON != 'function') throw new Error("toBSON is not a function");
353     object = object.toBSON();
354     if(object != null && typeof object != 'object') throw new Error("toBSON function did not return an object");
355   }
356
357   // Process the object
358   if(Array.isArray(object)) {
359     for(var i = 0; i < object.length; i++) {
360       index = packElement(i.toString(), object[i], checkKeys, buffer, index, serializeFunctions);
361     }
362   } else {
363                 // If we have toBSON defined, override the current object
364                 if(object.toBSON) {
365                         object = object.toBSON();
366                 }
367
368                 // Serialize the object
369     for(var key in object) {
370       // Check the key and throw error if it's illegal
371       if (key != '$db' && key != '$ref' && key != '$id') {
372         // dollars and dots ok
373         BSON.checkKey(key, !checkKeys);
374       }
375
376       // Pack the element
377       index = packElement(key, object[key], checkKeys, buffer, index, serializeFunctions);
378     }
379   }
380
381   // Write zero
382   buffer[index++] = 0;
383   return index;
384 }
385
386 var stringToBytes = function(str) {
387   var ch, st, re = [];
388   for (var i = 0; i < str.length; i++ ) {
389     ch = str.charCodeAt(i);  // get char
390     st = [];                 // set up "stack"
391     do {
392       st.push( ch & 0xFF );  // push byte to stack
393       ch = ch >> 8;          // shift value down by 1 byte
394     }
395     while ( ch );
396     // add stack contents to result
397     // done because chars have "wrong" endianness
398     re = re.concat( st.reverse() );
399   }
400   // return an array of bytes
401   return re;
402 }
403
404 var numberOfBytes = function(str) {
405   var ch, st, re = 0;
406   for (var i = 0; i < str.length; i++ ) {
407     ch = str.charCodeAt(i);  // get char
408     st = [];                 // set up "stack"
409     do {
410       st.push( ch & 0xFF );  // push byte to stack
411       ch = ch >> 8;          // shift value down by 1 byte
412     }
413     while ( ch );
414     // add stack contents to result
415     // done because chars have "wrong" endianness
416     re = re + st.length;
417   }
418   // return an array of bytes
419   return re;
420 }
421
422 /**
423  * @ignore
424  * @api private
425  */
426 var writeToTypedArray = function(buffer, string, index) {
427   var bytes = stringToBytes(string);
428   for(var i = 0; i < bytes.length; i++) {
429     buffer[index + i] = bytes[i];
430   }
431   return bytes.length;
432 }
433
434 /**
435  * @ignore
436  * @api private
437  */
438 var supportsBuffer = typeof Buffer != 'undefined';
439
440 /**
441  * @ignore
442  * @api private
443  */
444 var packElement = function(name, value, checkKeys, buffer, index, serializeFunctions) {
445
446   // If we have toBSON defined, override the current object
447   if(value && value.toBSON){
448         value = value.toBSON();
449   }
450
451   var startIndex = index;
452
453   switch(typeof value) {
454     case 'string':
455       // console.log("+++++++++++ index string:: " + index)
456       // Encode String type
457       buffer[index++] = BSON.BSON_DATA_STRING;
458       // Number of written bytes
459       var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
460       // Encode the name
461       index = index + numberOfWrittenBytes + 1;
462       buffer[index - 1] = 0;
463
464       // Calculate size
465       var size = supportsBuffer ? Buffer.byteLength(value) + 1 : numberOfBytes(value) + 1;
466       // console.log("====== key :: " + name + " size ::" + size)
467       // Write the size of the string to buffer
468       buffer[index + 3] = (size >> 24) & 0xff;
469       buffer[index + 2] = (size >> 16) & 0xff;
470       buffer[index + 1] = (size >> 8) & 0xff;
471       buffer[index] = size & 0xff;
472       // Ajust the index
473       index = index + 4;
474       // Write the string
475       supportsBuffer ? buffer.write(value, index, 'utf8') : writeToTypedArray(buffer, value, index);
476       // Update index
477       index = index + size - 1;
478       // Write zero
479       buffer[index++] = 0;
480       // Return index
481       return index;
482     case 'number':
483       // We have an integer value
484       if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
485         // If the value fits in 32 bits encode as int, if it fits in a double
486         // encode it as a double, otherwise long
487         if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) {
488           // Set int type 32 bits or less
489           buffer[index++] = BSON.BSON_DATA_INT;
490           // Number of written bytes
491           var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
492           // Encode the name
493           index = index + numberOfWrittenBytes + 1;
494           buffer[index - 1] = 0;
495           // Write the int value
496           buffer[index++] = value & 0xff;
497           buffer[index++] = (value >> 8) & 0xff;
498           buffer[index++] = (value >> 16) & 0xff;
499           buffer[index++] = (value >> 24) & 0xff;
500         } else if(value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
501           // Encode as double
502           buffer[index++] = BSON.BSON_DATA_NUMBER;
503           // Number of written bytes
504           var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
505           // Encode the name
506           index = index + numberOfWrittenBytes + 1;
507           buffer[index - 1] = 0;
508           // Write float
509           writeIEEE754(buffer, value, index, 'little', 52, 8);
510           // Ajust index
511           index = index + 8;
512         } else {
513           // Set long type
514           buffer[index++] = BSON.BSON_DATA_LONG;
515           // Number of written bytes
516           var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
517           // Encode the name
518           index = index + numberOfWrittenBytes + 1;
519           buffer[index - 1] = 0;
520           var longVal = Long.fromNumber(value);
521           var lowBits = longVal.getLowBits();
522           var highBits = longVal.getHighBits();
523           // Encode low bits
524           buffer[index++] = lowBits & 0xff;
525           buffer[index++] = (lowBits >> 8) & 0xff;
526           buffer[index++] = (lowBits >> 16) & 0xff;
527           buffer[index++] = (lowBits >> 24) & 0xff;
528           // Encode high bits
529           buffer[index++] = highBits & 0xff;
530           buffer[index++] = (highBits >> 8) & 0xff;
531           buffer[index++] = (highBits >> 16) & 0xff;
532           buffer[index++] = (highBits >> 24) & 0xff;
533         }
534       } else {
535         // Encode as double
536         buffer[index++] = BSON.BSON_DATA_NUMBER;
537         // Number of written bytes
538         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
539         // Encode the name
540         index = index + numberOfWrittenBytes + 1;
541         buffer[index - 1] = 0;
542         // Write float
543         writeIEEE754(buffer, value, index, 'little', 52, 8);
544         // Ajust index
545         index = index + 8;
546       }
547
548       return index;
549     case 'undefined':
550       // Set long type
551       buffer[index++] = BSON.BSON_DATA_NULL;
552       // Number of written bytes
553       var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
554       // Encode the name
555       index = index + numberOfWrittenBytes + 1;
556       buffer[index - 1] = 0;
557       return index;
558     case 'boolean':
559       // Write the type
560       buffer[index++] = BSON.BSON_DATA_BOOLEAN;
561       // Number of written bytes
562       var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
563       // Encode the name
564       index = index + numberOfWrittenBytes + 1;
565       buffer[index - 1] = 0;
566       // Encode the boolean value
567       buffer[index++] = value ? 1 : 0;
568       return index;
569     case 'object':
570       if(value === null || value instanceof MinKey || value instanceof MaxKey
571           || value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
572         // Write the type of either min or max key
573         if(value === null) {
574           buffer[index++] = BSON.BSON_DATA_NULL;
575         } else if(value instanceof MinKey) {
576           buffer[index++] = BSON.BSON_DATA_MIN_KEY;
577         } else {
578           buffer[index++] = BSON.BSON_DATA_MAX_KEY;
579         }
580
581         // Number of written bytes
582         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
583         // Encode the name
584         index = index + numberOfWrittenBytes + 1;
585         buffer[index - 1] = 0;
586         return index;
587       } else if(value instanceof ObjectID || value['_bsontype'] == 'ObjectID') {
588         // console.log("+++++++++++ index OBJECTID:: " + index)
589         // Write the type
590         buffer[index++] = BSON.BSON_DATA_OID;
591         // Number of written bytes
592         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
593         // Encode the name
594         index = index + numberOfWrittenBytes + 1;
595         buffer[index - 1] = 0;
596
597         // Write objectid
598         supportsBuffer ? buffer.write(value.id, index, 'binary') : writeToTypedArray(buffer, value.id, index);
599         // Ajust index
600         index = index + 12;
601         return index;
602       } else if(value instanceof Date || isDate(value)) {
603         // Write the type
604         buffer[index++] = BSON.BSON_DATA_DATE;
605         // Number of written bytes
606         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
607         // Encode the name
608         index = index + numberOfWrittenBytes + 1;
609         buffer[index - 1] = 0;
610
611         // Write the date
612         var dateInMilis = Long.fromNumber(value.getTime());
613         var lowBits = dateInMilis.getLowBits();
614         var highBits = dateInMilis.getHighBits();
615         // Encode low bits
616         buffer[index++] = lowBits & 0xff;
617         buffer[index++] = (lowBits >> 8) & 0xff;
618         buffer[index++] = (lowBits >> 16) & 0xff;
619         buffer[index++] = (lowBits >> 24) & 0xff;
620         // Encode high bits
621         buffer[index++] = highBits & 0xff;
622         buffer[index++] = (highBits >> 8) & 0xff;
623         buffer[index++] = (highBits >> 16) & 0xff;
624         buffer[index++] = (highBits >> 24) & 0xff;
625         return index;
626       } else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
627         // Write the type
628         buffer[index++] = BSON.BSON_DATA_BINARY;
629         // Number of written bytes
630         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
631         // Encode the name
632         index = index + numberOfWrittenBytes + 1;
633         buffer[index - 1] = 0;
634         // Get size of the buffer (current write point)
635         var size = value.length;
636         // Write the size of the string to buffer
637         buffer[index++] = size & 0xff;
638         buffer[index++] = (size >> 8) & 0xff;
639         buffer[index++] = (size >> 16) & 0xff;
640         buffer[index++] = (size >> 24) & 0xff;
641         // Write the default subtype
642         buffer[index++] = BSON.BSON_BINARY_SUBTYPE_DEFAULT;
643         // Copy the content form the binary field to the buffer
644         value.copy(buffer, index, 0, size);
645         // Adjust the index
646         index = index + size;
647         return index;
648       } else if(value instanceof Long || value instanceof Timestamp || value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') {
649         // Write the type
650         buffer[index++] = value instanceof Long || value['_bsontype'] == 'Long' ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP;
651         // Number of written bytes
652         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
653         // Encode the name
654         index = index + numberOfWrittenBytes + 1;
655         buffer[index - 1] = 0;
656         // Write the date
657         var lowBits = value.getLowBits();
658         var highBits = value.getHighBits();
659         // Encode low bits
660         buffer[index++] = lowBits & 0xff;
661         buffer[index++] = (lowBits >> 8) & 0xff;
662         buffer[index++] = (lowBits >> 16) & 0xff;
663         buffer[index++] = (lowBits >> 24) & 0xff;
664         // Encode high bits
665         buffer[index++] = highBits & 0xff;
666         buffer[index++] = (highBits >> 8) & 0xff;
667         buffer[index++] = (highBits >> 16) & 0xff;
668         buffer[index++] = (highBits >> 24) & 0xff;
669         return index;
670       } else if(value instanceof Double || value['_bsontype'] == 'Double') {
671         // Encode as double
672         buffer[index++] = BSON.BSON_DATA_NUMBER;
673         // Number of written bytes
674         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
675         // Encode the name
676         index = index + numberOfWrittenBytes + 1;
677         buffer[index - 1] = 0;
678         // Write float
679         writeIEEE754(buffer, value, index, 'little', 52, 8);
680         // Ajust index
681         index = index + 8;
682         return index;
683       } else if(value instanceof Code || value['_bsontype'] == 'Code') {
684         if(value.scope != null && Object.keys(value.scope).length > 0) {
685           // Write the type
686           buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE;
687           // Number of written bytes
688           var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
689           // Encode the name
690           index = index + numberOfWrittenBytes + 1;
691           buffer[index - 1] = 0;
692           // Calculate the scope size
693           var scopeSize = BSON.calculateObjectSize(value.scope, serializeFunctions);
694           // Function string
695           var functionString = value.code.toString();
696           // Function Size
697           var codeSize = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
698
699           // Calculate full size of the object
700           var totalSize = 4 + codeSize + scopeSize + 4;
701
702           // Write the total size of the object
703           buffer[index++] = totalSize & 0xff;
704           buffer[index++] = (totalSize >> 8) & 0xff;
705           buffer[index++] = (totalSize >> 16) & 0xff;
706           buffer[index++] = (totalSize >> 24) & 0xff;
707
708           // Write the size of the string to buffer
709           buffer[index++] = codeSize & 0xff;
710           buffer[index++] = (codeSize >> 8) & 0xff;
711           buffer[index++] = (codeSize >> 16) & 0xff;
712           buffer[index++] = (codeSize >> 24) & 0xff;
713
714           // Write the string
715           supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
716           // Update index
717           index = index + codeSize - 1;
718           // Write zero
719           buffer[index++] = 0;
720           // Serialize the scope object
721           var scopeObjectBuffer = supportsBuffer ? new Buffer(scopeSize) : new Uint8Array(new ArrayBuffer(scopeSize));
722           // Execute the serialization into a seperate buffer
723           serializeObject(value.scope, checkKeys, scopeObjectBuffer, 0, serializeFunctions);
724
725           // Adjusted scope Size (removing the header)
726           var scopeDocSize = scopeSize;
727           // Write scope object size
728           buffer[index++] = scopeDocSize & 0xff;
729           buffer[index++] = (scopeDocSize >> 8) & 0xff;
730           buffer[index++] = (scopeDocSize >> 16) & 0xff;
731           buffer[index++] = (scopeDocSize >> 24) & 0xff;
732
733           // Write the scopeObject into the buffer
734           supportsBuffer ? scopeObjectBuffer.copy(buffer, index, 0, scopeSize) : buffer.set(scopeObjectBuffer, index);
735           // Adjust index, removing the empty size of the doc (5 bytes 0000000005)
736           index = index + scopeDocSize - 5;
737           // Write trailing zero
738           buffer[index++] = 0;
739           return index
740         } else {
741           buffer[index++] = BSON.BSON_DATA_CODE;
742           // Number of written bytes
743           var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
744           // Encode the name
745           index = index + numberOfWrittenBytes + 1;
746           buffer[index - 1] = 0;
747           // Function string
748           var functionString = value.code.toString();
749           // Function Size
750           var size = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
751           // Write the size of the string to buffer
752           buffer[index++] = size & 0xff;
753           buffer[index++] = (size >> 8) & 0xff;
754           buffer[index++] = (size >> 16) & 0xff;
755           buffer[index++] = (size >> 24) & 0xff;
756           // Write the string
757           supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
758           // Update index
759           index = index + size - 1;
760           // Write zero
761           buffer[index++] = 0;
762           return index;
763         }
764       } else if(value instanceof Binary || value['_bsontype'] == 'Binary') {
765         // Write the type
766         buffer[index++] = BSON.BSON_DATA_BINARY;
767         // Number of written bytes
768         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
769         // Encode the name
770         index = index + numberOfWrittenBytes + 1;
771         buffer[index - 1] = 0;
772         // Extract the buffer
773         var data = value.value(true);
774         // Calculate size
775         var size = value.position;
776         // Write the size of the string to buffer
777         buffer[index++] = size & 0xff;
778         buffer[index++] = (size >> 8) & 0xff;
779         buffer[index++] = (size >> 16) & 0xff;
780         buffer[index++] = (size >> 24) & 0xff;
781         // Write the subtype to the buffer
782         buffer[index++] = value.sub_type;
783
784         // If we have binary type 2 the 4 first bytes are the size
785         if(value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) {
786           buffer[index++] = size & 0xff;
787           buffer[index++] = (size >> 8) & 0xff;
788           buffer[index++] = (size >> 16) & 0xff;
789           buffer[index++] = (size >> 24) & 0xff;
790         }
791
792         // Write the data to the object
793         supportsBuffer ? data.copy(buffer, index, 0, value.position) : buffer.set(data, index);
794         // Ajust index
795         index = index + value.position;
796         return index;
797       } else if(value instanceof Symbol || value['_bsontype'] == 'Symbol') {
798         // Write the type
799         buffer[index++] = BSON.BSON_DATA_SYMBOL;
800         // Number of written bytes
801         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
802         // Encode the name
803         index = index + numberOfWrittenBytes + 1;
804         buffer[index - 1] = 0;
805         // Calculate size
806         var size = supportsBuffer ? Buffer.byteLength(value.value) + 1 : numberOfBytes(value.value) + 1;
807         // Write the size of the string to buffer
808         buffer[index++] = size & 0xff;
809         buffer[index++] = (size >> 8) & 0xff;
810         buffer[index++] = (size >> 16) & 0xff;
811         buffer[index++] = (size >> 24) & 0xff;
812         // Write the string
813         buffer.write(value.value, index, 'utf8');
814         // Update index
815         index = index + size - 1;
816         // Write zero
817         buffer[index++] = 0x00;
818         return index;
819       } else if(value instanceof DBRef || value['_bsontype'] == 'DBRef') {
820         // Write the type
821         buffer[index++] = BSON.BSON_DATA_OBJECT;
822         // Number of written bytes
823         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
824         // Encode the name
825         index = index + numberOfWrittenBytes + 1;
826         buffer[index - 1] = 0;
827         // Set up correct object for serialization
828         var ordered_values = {
829             '$ref': value.namespace
830           , '$id' : value.oid
831         };
832
833         // Add db reference if it exists
834         if(null != value.db) {
835           ordered_values['$db'] = value.db;
836         }
837
838         // Message size
839         var size = BSON.calculateObjectSize(ordered_values, serializeFunctions);
840         // Serialize the object
841         var endIndex = BSON.serializeWithBufferAndIndex(ordered_values, checkKeys, buffer, index, serializeFunctions);
842         // Write the size of the string to buffer
843         buffer[index++] = size & 0xff;
844         buffer[index++] = (size >> 8) & 0xff;
845         buffer[index++] = (size >> 16) & 0xff;
846         buffer[index++] = (size >> 24) & 0xff;
847         // Write zero for object
848         buffer[endIndex++] = 0x00;
849         // Return the end index
850         return endIndex;
851       } else if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]') {
852         // Write the type
853         buffer[index++] = BSON.BSON_DATA_REGEXP;
854         // Number of written bytes
855         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
856         // Encode the name
857         index = index + numberOfWrittenBytes + 1;
858         buffer[index - 1] = 0;
859
860         // Write the regular expression string
861         supportsBuffer ? buffer.write(value.source, index, 'utf8') : writeToTypedArray(buffer, value.source, index);
862         // Adjust the index
863         index = index + (supportsBuffer ? Buffer.byteLength(value.source) : numberOfBytes(value.source));
864         // Write zero
865         buffer[index++] = 0x00;
866         // Write the parameters
867         if(value.global) buffer[index++] = 0x73; // s
868         if(value.ignoreCase) buffer[index++] = 0x69; // i
869         if(value.multiline) buffer[index++] = 0x6d; // m
870         // Add ending zero
871         buffer[index++] = 0x00;
872         return index;
873       } else {
874         // Write the type
875         buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT;
876         // Number of written bytes
877         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
878         // Adjust the index
879         index = index + numberOfWrittenBytes + 1;
880         buffer[index - 1] = 0;
881               var endIndex = serializeObject(value, checkKeys, buffer, index + 4, serializeFunctions);
882         // Write size
883         var size = endIndex - index;
884         // Write the size of the string to buffer
885         buffer[index++] = size & 0xff;
886         buffer[index++] = (size >> 8) & 0xff;
887         buffer[index++] = (size >> 16) & 0xff;
888         buffer[index++] = (size >> 24) & 0xff;
889         return endIndex;
890       }
891     case 'function':
892       // WTF for 0.4.X where typeof /someregexp/ === 'function'
893       if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]' || String.call(value) == '[object RegExp]') {
894         // Write the type
895         buffer[index++] = BSON.BSON_DATA_REGEXP;
896         // Number of written bytes
897         var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
898         // Encode the name
899         index = index + numberOfWrittenBytes + 1;
900         buffer[index - 1] = 0;
901
902         // Write the regular expression string
903         buffer.write(value.source, index, 'utf8');
904         // Adjust the index
905         index = index + (supportsBuffer ? Buffer.byteLength(value.source) : numberOfBytes(value.source));
906         // Write zero
907         buffer[index++] = 0x00;
908         // Write the parameters
909         if(value.global) buffer[index++] = 0x73; // s
910         if(value.ignoreCase) buffer[index++] = 0x69; // i
911         if(value.multiline) buffer[index++] = 0x6d; // m
912         // Add ending zero
913         buffer[index++] = 0x00;
914         return index;
915       } else {
916         if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
917           // Write the type
918           buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE;
919           // Number of written bytes
920           var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
921           // Encode the name
922           index = index + numberOfWrittenBytes + 1;
923           buffer[index - 1] = 0;
924           // Calculate the scope size
925           var scopeSize = BSON.calculateObjectSize(value.scope, serializeFunctions);
926           // Function string
927           var functionString = value.toString();
928           // Function Size
929           var codeSize = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
930
931           // Calculate full size of the object
932           var totalSize = 4 + codeSize + scopeSize;
933
934           // Write the total size of the object
935           buffer[index++] = totalSize & 0xff;
936           buffer[index++] = (totalSize >> 8) & 0xff;
937           buffer[index++] = (totalSize >> 16) & 0xff;
938           buffer[index++] = (totalSize >> 24) & 0xff;
939
940           // Write the size of the string to buffer
941           buffer[index++] = codeSize & 0xff;
942           buffer[index++] = (codeSize >> 8) & 0xff;
943           buffer[index++] = (codeSize >> 16) & 0xff;
944           buffer[index++] = (codeSize >> 24) & 0xff;
945
946           // Write the string
947           supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
948           // Update index
949           index = index + codeSize - 1;
950           // Write zero
951           buffer[index++] = 0;
952           // Serialize the scope object
953           var scopeObjectBuffer = new Buffer(scopeSize);
954           // Execute the serialization into a seperate buffer
955           serializeObject(value.scope, checkKeys, scopeObjectBuffer, 0, serializeFunctions);
956
957           // Adjusted scope Size (removing the header)
958           var scopeDocSize = scopeSize - 4;
959           // Write scope object size
960           buffer[index++] = scopeDocSize & 0xff;
961           buffer[index++] = (scopeDocSize >> 8) & 0xff;
962           buffer[index++] = (scopeDocSize >> 16) & 0xff;
963           buffer[index++] = (scopeDocSize >> 24) & 0xff;
964
965           // Write the scopeObject into the buffer
966           scopeObjectBuffer.copy(buffer, index, 0, scopeSize);
967
968           // Adjust index, removing the empty size of the doc (5 bytes 0000000005)
969           index = index + scopeDocSize - 5;
970           // Write trailing zero
971           buffer[index++] = 0;
972           return index
973         } else if(serializeFunctions) {
974           buffer[index++] = BSON.BSON_DATA_CODE;
975           // Number of written bytes
976           var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
977           // Encode the name
978           index = index + numberOfWrittenBytes + 1;
979           buffer[index - 1] = 0;
980           // Function string
981           var functionString = value.toString();
982           // Function Size
983           var size = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
984           // Write the size of the string to buffer
985           buffer[index++] = size & 0xff;
986           buffer[index++] = (size >> 8) & 0xff;
987           buffer[index++] = (size >> 16) & 0xff;
988           buffer[index++] = (size >> 24) & 0xff;
989           // Write the string
990           supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
991           // Update index
992           index = index + size - 1;
993           // Write zero
994           buffer[index++] = 0;
995           return index;
996         }
997       }
998   }
999
1000   // If no value to serialize
1001   return index;
1002 }
1003
1004 /**
1005  * Serialize a Javascript object.
1006  *
1007  * @param {Object} object the Javascript object to serialize.
1008  * @param {Boolean} checkKeys the serializer will check if keys are valid.
1009  * @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**.
1010  * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
1011  * @return {Buffer} returns the Buffer object containing the serialized object.
1012  * @api public
1013  */
1014 BSON.serialize = function(object, checkKeys, asBuffer, serializeFunctions) {
1015   // Throw error if we are trying serialize an illegal type
1016   if(object == null || typeof object != 'object' || Array.isArray(object))
1017     throw new Error("Only javascript objects supported");
1018
1019   // Emoty target buffer
1020   var buffer = null;
1021   // Calculate the size of the object
1022   var size = BSON.calculateObjectSize(object, serializeFunctions);
1023   // Fetch the best available type for storing the binary data
1024   if(buffer = typeof Buffer != 'undefined') {
1025     buffer = new Buffer(size);
1026     asBuffer = true;
1027   } else if(typeof Uint8Array != 'undefined') {
1028     buffer = new Uint8Array(new ArrayBuffer(size));
1029   } else {
1030     buffer = new Array(size);
1031   }
1032
1033   // If asBuffer is false use typed arrays
1034   BSON.serializeWithBufferAndIndex(object, checkKeys, buffer, 0, serializeFunctions);
1035   // console.log("++++++++++++++++++++++++++++++++++++ OLDJS :: " + buffer.length)
1036   // console.log(buffer.toString('hex'))
1037   // console.log(buffer.toString('ascii'))
1038   return buffer;
1039 }
1040
1041 /**
1042  * Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5
1043  *
1044  * @ignore
1045  * @api private
1046  */
1047 var functionCache = BSON.functionCache = {};
1048
1049 /**
1050  * Crc state variables shared by function
1051  *
1052  * @ignore
1053  * @api private
1054  */
1055 var table = [0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D];
1056
1057 /**
1058  * CRC32 hash method, Fast and enough versitility for our usage
1059  *
1060  * @ignore
1061  * @api private
1062  */
1063 var crc32 =  function(string, start, end) {
1064   var crc = 0
1065   var x = 0;
1066   var y = 0;
1067   crc = crc ^ (-1);
1068
1069   for(var i = start, iTop = end; i < iTop;i++) {
1070         y = (crc ^ string[i]) & 0xFF;
1071     x = table[y];
1072         crc = (crc >>> 8) ^ x;
1073   }
1074
1075   return crc ^ (-1);
1076 }
1077
1078 /**
1079  * Deserialize stream data as BSON documents.
1080  *
1081  * Options
1082  *  - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
1083  *  - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
1084  *  - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
1085  *  - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
1086  *
1087  * @param {Buffer} data the buffer containing the serialized set of BSON documents.
1088  * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
1089  * @param {Number} numberOfDocuments number of documents to deserialize.
1090  * @param {Array} documents an array where to store the deserialized documents.
1091  * @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
1092  * @param {Object} [options] additional options used for the deserialization.
1093  * @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
1094  * @api public
1095  */
1096 BSON.deserializeStream = function(data, startIndex, numberOfDocuments, documents, docStartIndex, options) {
1097   // if(numberOfDocuments !== documents.length) throw new Error("Number of expected results back is less than the number of documents");
1098   options = options != null ? options : {};
1099   var index = startIndex;
1100   // Loop over all documents
1101   for(var i = 0; i < numberOfDocuments; i++) {
1102     // Find size of the document
1103     var size = data[index] | data[index + 1] << 8 | data[index + 2] << 16 | data[index + 3] << 24;
1104     // Update options with index
1105     options['index'] = index;
1106     // Parse the document at this point
1107     documents[docStartIndex + i] = BSON.deserialize(data, options);
1108     // Adjust index by the document size
1109     index = index + size;
1110   }
1111
1112   // Return object containing end index of parsing and list of documents
1113   return index;
1114 }
1115
1116 /**
1117  * Ensure eval is isolated.
1118  *
1119  * @ignore
1120  * @api private
1121  */
1122 var isolateEvalWithHash = function(functionCache, hash, functionString, object) {
1123   // Contains the value we are going to set
1124   var value = null;
1125
1126   // Check for cache hit, eval if missing and return cached function
1127   if(functionCache[hash] == null) {
1128     eval("value = " + functionString);
1129     functionCache[hash] = value;
1130   }
1131   // Set the object
1132   return functionCache[hash].bind(object);
1133 }
1134
1135 /**
1136  * Ensure eval is isolated.
1137  *
1138  * @ignore
1139  * @api private
1140  */
1141 var isolateEval = function(functionString) {
1142   // Contains the value we are going to set
1143   var value = null;
1144   // Eval the function
1145   eval("value = " + functionString);
1146   return value;
1147 }
1148
1149 /**
1150  * Convert Uint8Array to String
1151  *
1152  * @ignore
1153  * @api private
1154  */
1155 var convertUint8ArrayToUtf8String = function(byteArray, startIndex, endIndex) {
1156   return BinaryParser.decode_utf8(convertArraytoUtf8BinaryString(byteArray, startIndex, endIndex));
1157 }
1158
1159 var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) {
1160   var result = "";
1161   for(var i = startIndex; i < endIndex; i++) {
1162     result = result + String.fromCharCode(byteArray[i]);
1163   }
1164
1165   return result;
1166 };
1167
1168 /**
1169  * Deserialize data as BSON.
1170  *
1171  * Options
1172  *  - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
1173  *  - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
1174  *  - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
1175  *  - **promoteLongs** {Boolean, default:true}, when deserializing a Long will fit it into a Number if it's smaller than 53 bits
1176  *
1177  * @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
1178  * @param {Object} [options] additional options used for the deserialization.
1179  * @param {Boolean} [isArray] ignore used for recursive parsing.
1180  * @return {Object} returns the deserialized Javascript Object.
1181  * @api public
1182  */
1183 BSON.deserialize = function(buffer, options, isArray) {
1184   // Options
1185   options = options == null ? {} : options;
1186   var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
1187   var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
1188   var cacheFunctionsCrc32 = options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32'];
1189   var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
1190
1191   // Validate that we have at least 4 bytes of buffer
1192   if(buffer.length < 5) throw new Error("corrupt bson message < 5 bytes long");
1193
1194   // Set up index
1195   var index = typeof options['index'] == 'number' ? options['index'] : 0;
1196   // Reads in a C style string
1197   var readCStyleString = function() {
1198     // Get the start search index
1199     var i = index;
1200     // Locate the end of the c string
1201     while(buffer[i] !== 0x00 && i < buffer.length) {
1202       i++
1203     }
1204     // If are at the end of the buffer there is a problem with the document
1205     if(i >= buffer.length) throw new Error("Bad BSON Document: illegal CString")
1206     // Grab utf8 encoded string
1207     var string = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, i) : convertUint8ArrayToUtf8String(buffer, index, i);
1208     // Update index position
1209     index = i + 1;
1210     // Return string
1211     return string;
1212   }
1213
1214   // Create holding object
1215   var object = isArray ? [] : {};
1216
1217   // Read the document size
1218   var size = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1219
1220   // Ensure buffer is valid size
1221   if(size < 5 || size > buffer.length) throw new Error("corrupt bson message");
1222
1223   // While we have more left data left keep parsing
1224   while(true) {
1225     // Read the type
1226     var elementType = buffer[index++];
1227     // If we get a zero it's the last byte, exit
1228     if(elementType == 0) break;
1229     // Read the name of the field
1230     var name = readCStyleString();
1231     // Switch on the type
1232     switch(elementType) {
1233       case BSON.BSON_DATA_OID:
1234         var string = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('binary', index, index + 12) : convertArraytoUtf8BinaryString(buffer, index, index + 12);
1235         // Decode the oid
1236         object[name] = new ObjectID(string);
1237         // Update index
1238         index = index + 12;
1239         break;
1240       case BSON.BSON_DATA_STRING:
1241         // Read the content of the field
1242         var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1243         // Add string to object
1244         object[name] = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1);
1245         // Update parse index position
1246         index = index + stringSize;
1247         break;
1248       case BSON.BSON_DATA_INT:
1249         // Decode the 32bit value
1250         object[name] = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1251         break;
1252       case BSON.BSON_DATA_NUMBER:
1253         // Decode the double value
1254         object[name] = readIEEE754(buffer, index, 'little', 52, 8);
1255         // Update the index
1256         index = index + 8;
1257         break;
1258       case BSON.BSON_DATA_DATE:
1259         // Unpack the low and high bits
1260         var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1261         var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1262         // Set date object
1263         object[name] = new Date(new Long(lowBits, highBits).toNumber());
1264         break;
1265       case BSON.BSON_DATA_BOOLEAN:
1266         // Parse the boolean value
1267         object[name] = buffer[index++] == 1;
1268         break;
1269       case BSON.BSON_DATA_UNDEFINED:
1270       case BSON.BSON_DATA_NULL:
1271         // Parse the boolean value
1272         object[name] = null;
1273         break;
1274       case BSON.BSON_DATA_BINARY:
1275         // Decode the size of the binary blob
1276         var binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1277         // Decode the subtype
1278         var subType = buffer[index++];
1279         // Decode as raw Buffer object if options specifies it
1280         if(buffer['slice'] != null) {
1281           // If we have subtype 2 skip the 4 bytes for the size
1282           if(subType == Binary.SUBTYPE_BYTE_ARRAY) {
1283             binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1284           }
1285           // Slice the data
1286           object[name] = new Binary(buffer.slice(index, index + binarySize), subType);
1287         } else {
1288           var _buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(binarySize)) : new Array(binarySize);
1289           // If we have subtype 2 skip the 4 bytes for the size
1290           if(subType == Binary.SUBTYPE_BYTE_ARRAY) {
1291             binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1292           }
1293           // Copy the data
1294           for(var i = 0; i < binarySize; i++) {
1295             _buffer[i] = buffer[index + i];
1296           }
1297           // Create the binary object
1298           object[name] = new Binary(_buffer, subType);
1299         }
1300         // Update the index
1301         index = index + binarySize;
1302         break;
1303       case BSON.BSON_DATA_ARRAY:
1304         options['index'] = index;
1305         // Decode the size of the array document
1306         var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
1307         // Set the array to the object
1308         object[name] = BSON.deserialize(buffer, options, true);
1309         // Adjust the index
1310         index = index + objectSize;
1311         break;
1312       case BSON.BSON_DATA_OBJECT:
1313         options['index'] = index;
1314         // Decode the size of the object document
1315         var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
1316         // Set the array to the object
1317         object[name] = BSON.deserialize(buffer, options, false);
1318         // Adjust the index
1319         index = index + objectSize;
1320         break;
1321       case BSON.BSON_DATA_REGEXP:
1322         // Create the regexp
1323         var source = readCStyleString();
1324         var regExpOptions = readCStyleString();
1325         // For each option add the corresponding one for javascript
1326         var optionsArray = new Array(regExpOptions.length);
1327
1328         // Parse options
1329         for(var i = 0; i < regExpOptions.length; i++) {
1330           switch(regExpOptions[i]) {
1331             case 'm':
1332               optionsArray[i] = 'm';
1333               break;
1334             case 's':
1335               optionsArray[i] = 'g';
1336               break;
1337             case 'i':
1338               optionsArray[i] = 'i';
1339               break;
1340           }
1341         }
1342
1343         object[name] = new RegExp(source, optionsArray.join(''));
1344         break;
1345       case BSON.BSON_DATA_LONG:
1346         // Unpack the low and high bits
1347         var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1348         var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1349         // Create long object
1350         var long = new Long(lowBits, highBits);
1351         // Promote the long if possible
1352         if(promoteLongs) {
1353           object[name] = long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG) ? long.toNumber() : long;
1354         } else {
1355           object[name] = long;
1356         }
1357         break;
1358       case BSON.BSON_DATA_SYMBOL:
1359         // Read the content of the field
1360         var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1361         // Add string to object
1362         object[name] = new Symbol(buffer.toString('utf8', index, index + stringSize - 1));
1363         // Update parse index position
1364         index = index + stringSize;
1365         break;
1366       case BSON.BSON_DATA_TIMESTAMP:
1367         // Unpack the low and high bits
1368         var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1369         var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1370         // Set the object
1371         object[name] = new Timestamp(lowBits, highBits);
1372         break;
1373       case BSON.BSON_DATA_MIN_KEY:
1374         // Parse the object
1375         object[name] = new MinKey();
1376         break;
1377       case BSON.BSON_DATA_MAX_KEY:
1378         // Parse the object
1379         object[name] = new MaxKey();
1380         break;
1381       case BSON.BSON_DATA_CODE:
1382         // Read the content of the field
1383         var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1384         // Function string
1385         var functionString = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1);
1386
1387         // If we are evaluating the functions
1388         if(evalFunctions) {
1389           // Contains the value we are going to set
1390           var value = null;
1391           // If we have cache enabled let's look for the md5 of the function in the cache
1392           if(cacheFunctions) {
1393             var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString;
1394             // Got to do this to avoid V8 deoptimizing the call due to finding eval
1395             object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
1396           } else {
1397             // Set directly
1398             object[name] = isolateEval(functionString);
1399           }
1400         } else {
1401           object[name]  = new Code(functionString, {});
1402         }
1403
1404         // Update parse index position
1405         index = index + stringSize;
1406         break;
1407       case BSON.BSON_DATA_CODE_W_SCOPE:
1408         // Read the content of the field
1409         var totalSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1410         var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1411         // Javascript function
1412         var functionString = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1);
1413         // Update parse index position
1414         index = index + stringSize;
1415         // Parse the element
1416         options['index'] = index;
1417         // Decode the size of the object document
1418         var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
1419         // Decode the scope object
1420         var scopeObject = BSON.deserialize(buffer, options, false);
1421         // Adjust the index
1422         index = index + objectSize;
1423
1424         // If we are evaluating the functions
1425         if(evalFunctions) {
1426           // Contains the value we are going to set
1427           var value = null;
1428           // If we have cache enabled let's look for the md5 of the function in the cache
1429           if(cacheFunctions) {
1430             var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString;
1431             // Got to do this to avoid V8 deoptimizing the call due to finding eval
1432             object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
1433           } else {
1434             // Set directly
1435             object[name] = isolateEval(functionString);
1436           }
1437
1438           // Set the scope on the object
1439           object[name].scope = scopeObject;
1440         } else {
1441           object[name]  = new Code(functionString, scopeObject);
1442         }
1443
1444         // Add string to object
1445         break;
1446     }
1447   }
1448
1449   // Check if we have a db ref object
1450   if(object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']);
1451
1452   // Return the final objects
1453   return object;
1454 }
1455
1456 /**
1457  * Check if key name is valid.
1458  *
1459  * @ignore
1460  * @api private
1461  */
1462 BSON.checkKey = function checkKey (key, dollarsAndDotsOk) {
1463   if (!key.length) return;
1464   // Check if we have a legal key for the object
1465   if (!!~key.indexOf("\x00")) {
1466     // The BSON spec doesn't allow keys with null bytes because keys are
1467     // null-terminated.
1468     throw Error("key " + key + " must not contain null bytes");
1469   }
1470   if (!dollarsAndDotsOk) {
1471     if('$' == key[0]) {
1472       throw Error("key " + key + " must not start with '$'");
1473     } else if (!!~key.indexOf('.')) {
1474       throw Error("key " + key + " must not contain '.'");
1475     }
1476   }
1477 };
1478
1479 /**
1480  * Deserialize data as BSON.
1481  *
1482  * Options
1483  *  - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
1484  *  - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
1485  *  - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
1486  *
1487  * @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
1488  * @param {Object} [options] additional options used for the deserialization.
1489  * @param {Boolean} [isArray] ignore used for recursive parsing.
1490  * @return {Object} returns the deserialized Javascript Object.
1491  * @api public
1492  */
1493 BSON.prototype.deserialize = function(data, options) {
1494   return BSON.deserialize(data, options);
1495 }
1496
1497 /**
1498  * Deserialize stream data as BSON documents.
1499  *
1500  * Options
1501  *  - **evalFunctions** {Boolean, default:false}, evaluate functions in the BSON document scoped to the object deserialized.
1502  *  - **cacheFunctions** {Boolean, default:false}, cache evaluated functions for reuse.
1503  *  - **cacheFunctionsCrc32** {Boolean, default:false}, use a crc32 code for caching, otherwise use the string of the function.
1504  *
1505  * @param {Buffer} data the buffer containing the serialized set of BSON documents.
1506  * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
1507  * @param {Number} numberOfDocuments number of documents to deserialize.
1508  * @param {Array} documents an array where to store the deserialized documents.
1509  * @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
1510  * @param {Object} [options] additional options used for the deserialization.
1511  * @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
1512  * @api public
1513  */
1514 BSON.prototype.deserializeStream = function(data, startIndex, numberOfDocuments, documents, docStartIndex, options) {
1515   return BSON.deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, options);
1516 }
1517
1518 /**
1519  * Serialize a Javascript object.
1520  *
1521  * @param {Object} object the Javascript object to serialize.
1522  * @param {Boolean} checkKeys the serializer will check if keys are valid.
1523  * @param {Boolean} asBuffer return the serialized object as a Buffer object **(ignore)**.
1524  * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
1525  * @return {Buffer} returns the Buffer object containing the serialized object.
1526  * @api public
1527  */
1528 BSON.prototype.serialize = function(object, checkKeys, asBuffer, serializeFunctions) {
1529   return BSON.serialize(object, checkKeys, asBuffer, serializeFunctions);
1530 }
1531
1532 /**
1533  * Calculate the bson size for a passed in Javascript object.
1534  *
1535  * @param {Object} object the Javascript object to calculate the BSON byte size for.
1536  * @param {Boolean} [serializeFunctions] serialize all functions in the object **(default:false)**.
1537  * @return {Number} returns the number of bytes the BSON object will take up.
1538  * @api public
1539  */
1540 BSON.prototype.calculateObjectSize = function(object, serializeFunctions) {
1541   return BSON.calculateObjectSize(object, serializeFunctions);
1542 }
1543
1544 /**
1545  * Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
1546  *
1547  * @param {Object} object the Javascript object to serialize.
1548  * @param {Boolean} checkKeys the serializer will check if keys are valid.
1549  * @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
1550  * @param {Number} index the index in the buffer where we wish to start serializing into.
1551  * @param {Boolean} serializeFunctions serialize the javascript functions **(default:false)**.
1552  * @return {Number} returns the new write index in the Buffer.
1553  * @api public
1554  */
1555 BSON.prototype.serializeWithBufferAndIndex = function(object, checkKeys, buffer, startIndex, serializeFunctions) {
1556   return BSON.serializeWithBufferAndIndex(object, checkKeys, buffer, startIndex, serializeFunctions);
1557 }
1558
1559 /**
1560  * @ignore
1561  * @api private
1562  */
1563 module.exports = BSON;
1564 module.exports.Code = Code;
1565 module.exports.Symbol = Symbol;
1566 module.exports.BSON = BSON;
1567 module.exports.DBRef = DBRef;
1568 module.exports.Binary = Binary;
1569 module.exports.ObjectID = ObjectID;
1570 module.exports.Long = Long;
1571 module.exports.Timestamp = Timestamp;
1572 module.exports.Double = Double;
1573 module.exports.MinKey = MinKey;
1574 module.exports.MaxKey = MaxKey;