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
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]';
21 * Create a new BSON instance
24 * @return {BSON} instance of BSON Parser.
33 BSON.BSON_INT32_MAX = 0x7FFFFFFF;
34 BSON.BSON_INT32_MIN = -0x80000000;
36 BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
37 BSON.BSON_INT64_MIN = -Math.pow(2, 63);
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.
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.
50 * @classconstant BSON_DATA_NUMBER
52 BSON.BSON_DATA_NUMBER = 1;
56 * @classconstant BSON_DATA_STRING
58 BSON.BSON_DATA_STRING = 2;
62 * @classconstant BSON_DATA_OBJECT
64 BSON.BSON_DATA_OBJECT = 3;
68 * @classconstant BSON_DATA_ARRAY
70 BSON.BSON_DATA_ARRAY = 4;
74 * @classconstant BSON_DATA_BINARY
76 BSON.BSON_DATA_BINARY = 5;
80 * @classconstant BSON_DATA_UNDEFINED
82 BSON.BSON_DATA_UNDEFINED = 6;
86 * @classconstant BSON_DATA_OID
88 BSON.BSON_DATA_OID = 7;
92 * @classconstant BSON_DATA_BOOLEAN
94 BSON.BSON_DATA_BOOLEAN = 8;
98 * @classconstant BSON_DATA_DATE
100 BSON.BSON_DATA_DATE = 9;
104 * @classconstant BSON_DATA_NULL
106 BSON.BSON_DATA_NULL = 10;
110 * @classconstant BSON_DATA_REGEXP
112 BSON.BSON_DATA_REGEXP = 11;
116 * @classconstant BSON_DATA_CODE
118 BSON.BSON_DATA_CODE = 13;
122 * @classconstant BSON_DATA_SYMBOL
124 BSON.BSON_DATA_SYMBOL = 14;
126 * Code with Scope BSON Type
128 * @classconstant BSON_DATA_CODE_W_SCOPE
130 BSON.BSON_DATA_CODE_W_SCOPE = 15;
132 * 32 bit Integer BSON Type
134 * @classconstant BSON_DATA_INT
136 BSON.BSON_DATA_INT = 16;
138 * Timestamp BSON Type
140 * @classconstant BSON_DATA_TIMESTAMP
142 BSON.BSON_DATA_TIMESTAMP = 17;
146 * @classconstant BSON_DATA_LONG
148 BSON.BSON_DATA_LONG = 18;
152 * @classconstant BSON_DATA_MIN_KEY
154 BSON.BSON_DATA_MIN_KEY = 0xff;
158 * @classconstant BSON_DATA_MAX_KEY
160 BSON.BSON_DATA_MAX_KEY = 0x7f;
163 * Binary Default Type
165 * @classconstant BSON_BINARY_SUBTYPE_DEFAULT
167 BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
169 * Binary Function Type
171 * @classconstant BSON_BINARY_SUBTYPE_FUNCTION
173 BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
175 * Binary Byte Array Type
177 * @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
179 BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
183 * @classconstant BSON_BINARY_SUBTYPE_UUID
185 BSON.BSON_BINARY_SUBTYPE_UUID = 3;
189 * @classconstant BSON_BINARY_SUBTYPE_MD5
191 BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
193 * Binary User Defined Type
195 * @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
197 BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
200 * Calculate the bson size for a passed in Javascript object.
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.
207 BSON.calculateObjectSize = function calculateObjectSize(object, serializeFunctions) {
208 var totalLength = (4 + 1);
210 if(Array.isArray(object)) {
211 for(var i = 0; i < object.length; i++) {
212 totalLength += calculateElement(i.toString(), object[i], serializeFunctions)
215 // If we have toBSON defined, override the current object
217 object = object.toBSON();
221 for(var key in object) {
222 totalLength += calculateElement(key, object[key], serializeFunctions)
233 function calculateElement(name, value, serializeFunctions) {
234 var isBuffer = typeof Buffer !== 'undefined';
236 // If we have toBSON defined, override the current object
237 if(value && value.toBSON){
238 value = value.toBSON();
241 switch(typeof value) {
243 return 1 + (!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1 + 4 + (!isBuffer ? numberOfBytes(value) : Buffer.byteLength(value, 'utf8')) + 1;
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);
249 return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
252 return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (8 + 1);
255 return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1);
257 return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (1 + 1);
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);
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;
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);
282 return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + (value.position + 1 + 4 + 1);
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
293 // Add db reference if it exists
294 if(null != value.db) {
295 ordered_values['$db'] = value.db;
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
303 return (name != null ? ((!isBuffer ? numberOfBytes(name) : Buffer.byteLength(name, 'utf8')) + 1) : 0) + BSON.calculateObjectSize(value, serializeFunctions) + 1;
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
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;
323 * Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
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.
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;
350 var serializeObject = function(object, checkKeys, buffer, index, serializeFunctions) {
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");
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);
363 // If we have toBSON defined, override the current object
365 object = object.toBSON();
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);
377 index = packElement(key, object[key], checkKeys, buffer, index, serializeFunctions);
386 var stringToBytes = function(str) {
388 for (var i = 0; i < str.length; i++ ) {
389 ch = str.charCodeAt(i); // get char
390 st = []; // set up "stack"
392 st.push( ch & 0xFF ); // push byte to stack
393 ch = ch >> 8; // shift value down by 1 byte
396 // add stack contents to result
397 // done because chars have "wrong" endianness
398 re = re.concat( st.reverse() );
400 // return an array of bytes
404 var numberOfBytes = function(str) {
406 for (var i = 0; i < str.length; i++ ) {
407 ch = str.charCodeAt(i); // get char
408 st = []; // set up "stack"
410 st.push( ch & 0xFF ); // push byte to stack
411 ch = ch >> 8; // shift value down by 1 byte
414 // add stack contents to result
415 // done because chars have "wrong" endianness
418 // return an array of bytes
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];
438 var supportsBuffer = typeof Buffer != 'undefined';
444 var packElement = function(name, value, checkKeys, buffer, index, serializeFunctions) {
446 // If we have toBSON defined, override the current object
447 if(value && value.toBSON){
448 value = value.toBSON();
451 var startIndex = index;
453 switch(typeof value) {
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);
461 index = index + numberOfWrittenBytes + 1;
462 buffer[index - 1] = 0;
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;
475 supportsBuffer ? buffer.write(value, index, 'utf8') : writeToTypedArray(buffer, value, index);
477 index = index + size - 1;
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);
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) {
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);
506 index = index + numberOfWrittenBytes + 1;
507 buffer[index - 1] = 0;
509 writeIEEE754(buffer, value, index, 'little', 52, 8);
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);
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();
524 buffer[index++] = lowBits & 0xff;
525 buffer[index++] = (lowBits >> 8) & 0xff;
526 buffer[index++] = (lowBits >> 16) & 0xff;
527 buffer[index++] = (lowBits >> 24) & 0xff;
529 buffer[index++] = highBits & 0xff;
530 buffer[index++] = (highBits >> 8) & 0xff;
531 buffer[index++] = (highBits >> 16) & 0xff;
532 buffer[index++] = (highBits >> 24) & 0xff;
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);
540 index = index + numberOfWrittenBytes + 1;
541 buffer[index - 1] = 0;
543 writeIEEE754(buffer, value, index, 'little', 52, 8);
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);
555 index = index + numberOfWrittenBytes + 1;
556 buffer[index - 1] = 0;
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);
564 index = index + numberOfWrittenBytes + 1;
565 buffer[index - 1] = 0;
566 // Encode the boolean value
567 buffer[index++] = value ? 1 : 0;
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
574 buffer[index++] = BSON.BSON_DATA_NULL;
575 } else if(value instanceof MinKey) {
576 buffer[index++] = BSON.BSON_DATA_MIN_KEY;
578 buffer[index++] = BSON.BSON_DATA_MAX_KEY;
581 // Number of written bytes
582 var numberOfWrittenBytes = supportsBuffer ? buffer.write(name, index, 'utf8') : writeToTypedArray(buffer, name, index);
584 index = index + numberOfWrittenBytes + 1;
585 buffer[index - 1] = 0;
587 } else if(value instanceof ObjectID || value['_bsontype'] == 'ObjectID') {
588 // console.log("+++++++++++ index OBJECTID:: " + index)
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);
594 index = index + numberOfWrittenBytes + 1;
595 buffer[index - 1] = 0;
598 supportsBuffer ? buffer.write(value.id, index, 'binary') : writeToTypedArray(buffer, value.id, index);
602 } else if(value instanceof Date || isDate(value)) {
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);
608 index = index + numberOfWrittenBytes + 1;
609 buffer[index - 1] = 0;
612 var dateInMilis = Long.fromNumber(value.getTime());
613 var lowBits = dateInMilis.getLowBits();
614 var highBits = dateInMilis.getHighBits();
616 buffer[index++] = lowBits & 0xff;
617 buffer[index++] = (lowBits >> 8) & 0xff;
618 buffer[index++] = (lowBits >> 16) & 0xff;
619 buffer[index++] = (lowBits >> 24) & 0xff;
621 buffer[index++] = highBits & 0xff;
622 buffer[index++] = (highBits >> 8) & 0xff;
623 buffer[index++] = (highBits >> 16) & 0xff;
624 buffer[index++] = (highBits >> 24) & 0xff;
626 } else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
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);
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);
646 index = index + size;
648 } else if(value instanceof Long || value instanceof Timestamp || value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') {
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);
654 index = index + numberOfWrittenBytes + 1;
655 buffer[index - 1] = 0;
657 var lowBits = value.getLowBits();
658 var highBits = value.getHighBits();
660 buffer[index++] = lowBits & 0xff;
661 buffer[index++] = (lowBits >> 8) & 0xff;
662 buffer[index++] = (lowBits >> 16) & 0xff;
663 buffer[index++] = (lowBits >> 24) & 0xff;
665 buffer[index++] = highBits & 0xff;
666 buffer[index++] = (highBits >> 8) & 0xff;
667 buffer[index++] = (highBits >> 16) & 0xff;
668 buffer[index++] = (highBits >> 24) & 0xff;
670 } else if(value instanceof Double || value['_bsontype'] == '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);
676 index = index + numberOfWrittenBytes + 1;
677 buffer[index - 1] = 0;
679 writeIEEE754(buffer, value, index, 'little', 52, 8);
683 } else if(value instanceof Code || value['_bsontype'] == 'Code') {
684 if(value.scope != null && Object.keys(value.scope).length > 0) {
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);
690 index = index + numberOfWrittenBytes + 1;
691 buffer[index - 1] = 0;
692 // Calculate the scope size
693 var scopeSize = BSON.calculateObjectSize(value.scope, serializeFunctions);
695 var functionString = value.code.toString();
697 var codeSize = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
699 // Calculate full size of the object
700 var totalSize = 4 + codeSize + scopeSize + 4;
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;
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;
715 supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
717 index = index + codeSize - 1;
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);
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;
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
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);
745 index = index + numberOfWrittenBytes + 1;
746 buffer[index - 1] = 0;
748 var functionString = value.code.toString();
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;
757 supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
759 index = index + size - 1;
764 } else if(value instanceof Binary || value['_bsontype'] == 'Binary') {
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);
770 index = index + numberOfWrittenBytes + 1;
771 buffer[index - 1] = 0;
772 // Extract the buffer
773 var data = value.value(true);
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;
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;
792 // Write the data to the object
793 supportsBuffer ? data.copy(buffer, index, 0, value.position) : buffer.set(data, index);
795 index = index + value.position;
797 } else if(value instanceof Symbol || value['_bsontype'] == 'Symbol') {
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);
803 index = index + numberOfWrittenBytes + 1;
804 buffer[index - 1] = 0;
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;
813 buffer.write(value.value, index, 'utf8');
815 index = index + size - 1;
817 buffer[index++] = 0x00;
819 } else if(value instanceof DBRef || value['_bsontype'] == 'DBRef') {
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);
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
833 // Add db reference if it exists
834 if(null != value.db) {
835 ordered_values['$db'] = value.db;
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
851 } else if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]') {
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);
857 index = index + numberOfWrittenBytes + 1;
858 buffer[index - 1] = 0;
860 // Write the regular expression string
861 supportsBuffer ? buffer.write(value.source, index, 'utf8') : writeToTypedArray(buffer, value.source, index);
863 index = index + (supportsBuffer ? Buffer.byteLength(value.source) : numberOfBytes(value.source));
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
871 buffer[index++] = 0x00;
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);
879 index = index + numberOfWrittenBytes + 1;
880 buffer[index - 1] = 0;
881 var endIndex = serializeObject(value, checkKeys, buffer, index + 4, serializeFunctions);
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;
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]') {
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);
899 index = index + numberOfWrittenBytes + 1;
900 buffer[index - 1] = 0;
902 // Write the regular expression string
903 buffer.write(value.source, index, 'utf8');
905 index = index + (supportsBuffer ? Buffer.byteLength(value.source) : numberOfBytes(value.source));
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
913 buffer[index++] = 0x00;
916 if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
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);
922 index = index + numberOfWrittenBytes + 1;
923 buffer[index - 1] = 0;
924 // Calculate the scope size
925 var scopeSize = BSON.calculateObjectSize(value.scope, serializeFunctions);
927 var functionString = value.toString();
929 var codeSize = supportsBuffer ? Buffer.byteLength(functionString) + 1 : numberOfBytes(functionString) + 1;
931 // Calculate full size of the object
932 var totalSize = 4 + codeSize + scopeSize;
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;
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;
947 supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
949 index = index + codeSize - 1;
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);
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;
965 // Write the scopeObject into the buffer
966 scopeObjectBuffer.copy(buffer, index, 0, scopeSize);
968 // Adjust index, removing the empty size of the doc (5 bytes 0000000005)
969 index = index + scopeDocSize - 5;
970 // Write trailing zero
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);
978 index = index + numberOfWrittenBytes + 1;
979 buffer[index - 1] = 0;
981 var functionString = value.toString();
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;
990 supportsBuffer ? buffer.write(functionString, index, 'utf8') : writeToTypedArray(buffer, functionString, index);
992 index = index + size - 1;
1000 // If no value to serialize
1005 * Serialize a Javascript object.
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.
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");
1019 // Emoty target buffer
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);
1027 } else if(typeof Uint8Array != 'undefined') {
1028 buffer = new Uint8Array(new ArrayBuffer(size));
1030 buffer = new Array(size);
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'))
1042 * Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5
1047 var functionCache = BSON.functionCache = {};
1050 * Crc state variables shared by function
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];
1058 * CRC32 hash method, Fast and enough versitility for our usage
1063 var crc32 = function(string, start, end) {
1069 for(var i = start, iTop = end; i < iTop;i++) {
1070 y = (crc ^ string[i]) & 0xFF;
1072 crc = (crc >>> 8) ^ x;
1079 * Deserialize stream data as BSON documents.
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
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.
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;
1112 // Return object containing end index of parsing and list of documents
1117 * Ensure eval is isolated.
1122 var isolateEvalWithHash = function(functionCache, hash, functionString, object) {
1123 // Contains the value we are going to set
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;
1132 return functionCache[hash].bind(object);
1136 * Ensure eval is isolated.
1141 var isolateEval = function(functionString) {
1142 // Contains the value we are going to set
1144 // Eval the function
1145 eval("value = " + functionString);
1150 * Convert Uint8Array to String
1155 var convertUint8ArrayToUtf8String = function(byteArray, startIndex, endIndex) {
1156 return BinaryParser.decode_utf8(convertArraytoUtf8BinaryString(byteArray, startIndex, endIndex));
1159 var convertArraytoUtf8BinaryString = function(byteArray, startIndex, endIndex) {
1161 for(var i = startIndex; i < endIndex; i++) {
1162 result = result + String.fromCharCode(byteArray[i]);
1169 * Deserialize data as BSON.
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
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.
1183 BSON.deserialize = function(buffer, options, isArray) {
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'];
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");
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
1200 // Locate the end of the c string
1201 while(buffer[i] !== 0x00 && i < buffer.length) {
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
1214 // Create holding object
1215 var object = isArray ? [] : {};
1217 // Read the document size
1218 var size = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
1220 // Ensure buffer is valid size
1221 if(size < 5 || size > buffer.length) throw new Error("corrupt bson message");
1223 // While we have more left data left keep parsing
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);
1236 object[name] = new ObjectID(string);
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;
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;
1252 case BSON.BSON_DATA_NUMBER:
1253 // Decode the double value
1254 object[name] = readIEEE754(buffer, index, 'little', 52, 8);
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;
1263 object[name] = new Date(new Long(lowBits, highBits).toNumber());
1265 case BSON.BSON_DATA_BOOLEAN:
1266 // Parse the boolean value
1267 object[name] = buffer[index++] == 1;
1269 case BSON.BSON_DATA_UNDEFINED:
1270 case BSON.BSON_DATA_NULL:
1271 // Parse the boolean value
1272 object[name] = null;
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;
1286 object[name] = new Binary(buffer.slice(index, index + binarySize), subType);
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;
1294 for(var i = 0; i < binarySize; i++) {
1295 _buffer[i] = buffer[index + i];
1297 // Create the binary object
1298 object[name] = new Binary(_buffer, subType);
1301 index = index + binarySize;
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);
1310 index = index + objectSize;
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);
1319 index = index + objectSize;
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);
1329 for(var i = 0; i < regExpOptions.length; i++) {
1330 switch(regExpOptions[i]) {
1332 optionsArray[i] = 'm';
1335 optionsArray[i] = 'g';
1338 optionsArray[i] = 'i';
1343 object[name] = new RegExp(source, optionsArray.join(''));
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
1353 object[name] = long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG) ? long.toNumber() : long;
1355 object[name] = long;
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;
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;
1371 object[name] = new Timestamp(lowBits, highBits);
1373 case BSON.BSON_DATA_MIN_KEY:
1375 object[name] = new MinKey();
1377 case BSON.BSON_DATA_MAX_KEY:
1379 object[name] = new MaxKey();
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;
1385 var functionString = supportsBuffer && Buffer.isBuffer(buffer) ? buffer.toString('utf8', index, index + stringSize - 1) : convertUint8ArrayToUtf8String(buffer, index, index + stringSize - 1);
1387 // If we are evaluating the functions
1389 // Contains the value we are going to set
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);
1398 object[name] = isolateEval(functionString);
1401 object[name] = new Code(functionString, {});
1404 // Update parse index position
1405 index = index + stringSize;
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);
1422 index = index + objectSize;
1424 // If we are evaluating the functions
1426 // Contains the value we are going to set
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);
1435 object[name] = isolateEval(functionString);
1438 // Set the scope on the object
1439 object[name].scope = scopeObject;
1441 object[name] = new Code(functionString, scopeObject);
1444 // Add string to object
1449 // Check if we have a db ref object
1450 if(object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']);
1452 // Return the final objects
1457 * Check if key name is valid.
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
1468 throw Error("key " + key + " must not contain null bytes");
1470 if (!dollarsAndDotsOk) {
1472 throw Error("key " + key + " must not start with '$'");
1473 } else if (!!~key.indexOf('.')) {
1474 throw Error("key " + key + " must not contain '.'");
1480 * Deserialize data as BSON.
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.
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.
1493 BSON.prototype.deserialize = function(data, options) {
1494 return BSON.deserialize(data, options);
1498 * Deserialize stream data as BSON documents.
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.
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.
1514 BSON.prototype.deserializeStream = function(data, startIndex, numberOfDocuments, documents, docStartIndex, options) {
1515 return BSON.deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, options);
1519 * Serialize a Javascript object.
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.
1528 BSON.prototype.serialize = function(object, checkKeys, asBuffer, serializeFunctions) {
1529 return BSON.serialize(object, checkKeys, asBuffer, serializeFunctions);
1533 * Calculate the bson size for a passed in Javascript object.
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.
1540 BSON.prototype.calculateObjectSize = function(object, serializeFunctions) {
1541 return BSON.calculateObjectSize(object, serializeFunctions);
1545 * Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
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.
1555 BSON.prototype.serializeWithBufferAndIndex = function(object, checkKeys, buffer, startIndex, serializeFunctions) {
1556 return BSON.serializeWithBufferAndIndex(object, checkKeys, buffer, startIndex, serializeFunctions);
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;