mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-13 03:02:49 +00:00
updated bunch of file paths and changed the way posts are loaded
This commit is contained in:
310
node_modules/bson/lib/bson/parser/calculate_size.js
generated
vendored
Normal file
310
node_modules/bson/lib/bson/parser/calculate_size.js
generated
vendored
Normal file
@@ -0,0 +1,310 @@
|
||||
"use strict"
|
||||
|
||||
var writeIEEE754 = require('../float_parser').writeIEEE754
|
||||
, readIEEE754 = require('../float_parser').readIEEE754
|
||||
, Long = require('../long').Long
|
||||
, Double = require('../double').Double
|
||||
, Timestamp = require('../timestamp').Timestamp
|
||||
, ObjectID = require('../objectid').ObjectID
|
||||
, Symbol = require('../symbol').Symbol
|
||||
, BSONRegExp = require('../regexp').BSONRegExp
|
||||
, Code = require('../code').Code
|
||||
, MinKey = require('../min_key').MinKey
|
||||
, MaxKey = require('../max_key').MaxKey
|
||||
, DBRef = require('../db_ref').DBRef
|
||||
, Binary = require('../binary').Binary;
|
||||
|
||||
// To ensure that 0.4 of node works correctly
|
||||
var isDate = function isDate(d) {
|
||||
return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
|
||||
}
|
||||
|
||||
var calculateObjectSize = function calculateObjectSize(object, serializeFunctions, ignoreUndefined) {
|
||||
var totalLength = (4 + 1);
|
||||
|
||||
if(Array.isArray(object)) {
|
||||
for(var i = 0; i < object.length; i++) {
|
||||
totalLength += calculateElement(i.toString(), object[i], serializeFunctions, true, ignoreUndefined)
|
||||
}
|
||||
} else {
|
||||
// If we have toBSON defined, override the current object
|
||||
if(object.toBSON) {
|
||||
object = object.toBSON();
|
||||
}
|
||||
|
||||
// Calculate size
|
||||
for(var key in object) {
|
||||
totalLength += calculateElement(key, object[key], serializeFunctions, false, ignoreUndefined)
|
||||
}
|
||||
}
|
||||
|
||||
return totalLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
* @api private
|
||||
*/
|
||||
function calculateElement(name, value, serializeFunctions, isArray, ignoreUndefined) {
|
||||
// If we have toBSON defined, override the current object
|
||||
if(value && value.toBSON){
|
||||
value = value.toBSON();
|
||||
}
|
||||
|
||||
switch(typeof value) {
|
||||
case 'string':
|
||||
return 1 + Buffer.byteLength(name, 'utf8') + 1 + 4 + Buffer.byteLength(value, 'utf8') + 1;
|
||||
case 'number':
|
||||
if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
||||
if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) { // 32 bit
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (4 + 1);
|
||||
} else {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (8 + 1);
|
||||
}
|
||||
} else { // 64 bit
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (8 + 1);
|
||||
}
|
||||
case 'undefined':
|
||||
if(isArray || !ignoreUndefined) return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (1);
|
||||
return 0;
|
||||
case 'boolean':
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (1 + 1);
|
||||
case 'object':
|
||||
if(value == null || value instanceof MinKey || value instanceof MaxKey || value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (1);
|
||||
} else if(value instanceof ObjectID || value['_bsontype'] == 'ObjectID') {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (12 + 1);
|
||||
} else if(value instanceof Date || isDate(value)) {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (8 + 1);
|
||||
} else if(typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (1 + 4 + 1) + value.length;
|
||||
} else if(value instanceof Long || value instanceof Double || value instanceof Timestamp
|
||||
|| value['_bsontype'] == 'Long' || value['_bsontype'] == 'Double' || value['_bsontype'] == 'Timestamp') {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (8 + 1);
|
||||
} else if(value instanceof Code || value['_bsontype'] == 'Code') {
|
||||
// Calculate size depending on the availability of a scope
|
||||
if(value.scope != null && Object.keys(value.scope).length > 0) {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + 4 + 4 + Buffer.byteLength(value.code.toString(), 'utf8') + 1 + calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined);
|
||||
} else {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + 4 + Buffer.byteLength(value.code.toString(), 'utf8') + 1;
|
||||
}
|
||||
} else if(value instanceof Binary || value['_bsontype'] == 'Binary') {
|
||||
// Check what kind of subtype we have
|
||||
if(value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (value.position + 1 + 4 + 1 + 4);
|
||||
} else {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + (value.position + 1 + 4 + 1);
|
||||
}
|
||||
} else if(value instanceof Symbol || value['_bsontype'] == 'Symbol') {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + Buffer.byteLength(value.value, 'utf8') + 4 + 1 + 1;
|
||||
} else if(value instanceof DBRef || value['_bsontype'] == 'DBRef') {
|
||||
// Set up correct object for serialization
|
||||
var ordered_values = {
|
||||
'$ref': value.namespace
|
||||
, '$id' : value.oid
|
||||
};
|
||||
|
||||
// Add db reference if it exists
|
||||
if(null != value.db) {
|
||||
ordered_values['$db'] = value.db;
|
||||
}
|
||||
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined);
|
||||
} else if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]') {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + Buffer.byteLength(value.source, 'utf8') + 1
|
||||
+ (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1
|
||||
} else if(value instanceof BSONRegExp || value['_bsontype'] == 'BSONRegExp') {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + Buffer.byteLength(value.pattern, 'utf8') + 1
|
||||
+ Buffer.byteLength(value.options, 'utf8') + 1
|
||||
} else {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + calculateObjectSize(value, serializeFunctions, ignoreUndefined) + 1;
|
||||
}
|
||||
case 'function':
|
||||
// WTF for 0.4.X where typeof /someregexp/ === 'function'
|
||||
if(value instanceof RegExp || Object.prototype.toString.call(value) === '[object RegExp]' || String.call(value) == '[object RegExp]') {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + Buffer.byteLength(value.source, 'utf8') + 1
|
||||
+ (value.global ? 1 : 0) + (value.ignoreCase ? 1 : 0) + (value.multiline ? 1 : 0) + 1
|
||||
} else {
|
||||
if(serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + 4 + 4 + Buffer.byteLength(value.toString(), 'utf8') + 1 + calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined);
|
||||
} else if(serializeFunctions) {
|
||||
return (name != null ? (Buffer.byteLength(name, 'utf8') + 1) : 0) + 1 + 4 + Buffer.byteLength(value.toString(), 'utf8') + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
var BSON = {};
|
||||
|
||||
/**
|
||||
* Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5
|
||||
*
|
||||
* @ignore
|
||||
* @api private
|
||||
*/
|
||||
var functionCache = BSON.functionCache = {};
|
||||
|
||||
/**
|
||||
* Number BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_NUMBER
|
||||
**/
|
||||
BSON.BSON_DATA_NUMBER = 1;
|
||||
/**
|
||||
* String BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_STRING
|
||||
**/
|
||||
BSON.BSON_DATA_STRING = 2;
|
||||
/**
|
||||
* Object BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_OBJECT
|
||||
**/
|
||||
BSON.BSON_DATA_OBJECT = 3;
|
||||
/**
|
||||
* Array BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_ARRAY
|
||||
**/
|
||||
BSON.BSON_DATA_ARRAY = 4;
|
||||
/**
|
||||
* Binary BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_BINARY
|
||||
**/
|
||||
BSON.BSON_DATA_BINARY = 5;
|
||||
/**
|
||||
* ObjectID BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_OID
|
||||
**/
|
||||
BSON.BSON_DATA_OID = 7;
|
||||
/**
|
||||
* Boolean BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_BOOLEAN
|
||||
**/
|
||||
BSON.BSON_DATA_BOOLEAN = 8;
|
||||
/**
|
||||
* Date BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_DATE
|
||||
**/
|
||||
BSON.BSON_DATA_DATE = 9;
|
||||
/**
|
||||
* null BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_NULL
|
||||
**/
|
||||
BSON.BSON_DATA_NULL = 10;
|
||||
/**
|
||||
* RegExp BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_REGEXP
|
||||
**/
|
||||
BSON.BSON_DATA_REGEXP = 11;
|
||||
/**
|
||||
* Code BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_CODE
|
||||
**/
|
||||
BSON.BSON_DATA_CODE = 13;
|
||||
/**
|
||||
* Symbol BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_SYMBOL
|
||||
**/
|
||||
BSON.BSON_DATA_SYMBOL = 14;
|
||||
/**
|
||||
* Code with Scope BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_CODE_W_SCOPE
|
||||
**/
|
||||
BSON.BSON_DATA_CODE_W_SCOPE = 15;
|
||||
/**
|
||||
* 32 bit Integer BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_INT
|
||||
**/
|
||||
BSON.BSON_DATA_INT = 16;
|
||||
/**
|
||||
* Timestamp BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_TIMESTAMP
|
||||
**/
|
||||
BSON.BSON_DATA_TIMESTAMP = 17;
|
||||
/**
|
||||
* Long BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_LONG
|
||||
**/
|
||||
BSON.BSON_DATA_LONG = 18;
|
||||
/**
|
||||
* MinKey BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_MIN_KEY
|
||||
**/
|
||||
BSON.BSON_DATA_MIN_KEY = 0xff;
|
||||
/**
|
||||
* MaxKey BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_MAX_KEY
|
||||
**/
|
||||
BSON.BSON_DATA_MAX_KEY = 0x7f;
|
||||
|
||||
/**
|
||||
* Binary Default Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_DEFAULT
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
||||
/**
|
||||
* Binary Function Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_FUNCTION
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
|
||||
/**
|
||||
* Binary Byte Array Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
|
||||
/**
|
||||
* Binary UUID Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_UUID
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_UUID = 3;
|
||||
/**
|
||||
* Binary MD5 Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_MD5
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
|
||||
/**
|
||||
* Binary User Defined Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
|
||||
|
||||
// BSON MAX VALUES
|
||||
BSON.BSON_INT32_MAX = 0x7FFFFFFF;
|
||||
BSON.BSON_INT32_MIN = -0x80000000;
|
||||
|
||||
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
||||
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
|
||||
|
||||
// JS MAX PRECISE VALUES
|
||||
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
|
||||
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
|
||||
|
||||
// Internal long versions
|
||||
var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
|
||||
var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.
|
||||
|
||||
module.exports = calculateObjectSize;
|
||||
555
node_modules/bson/lib/bson/parser/deserializer.js
generated
vendored
Normal file
555
node_modules/bson/lib/bson/parser/deserializer.js
generated
vendored
Normal file
@@ -0,0 +1,555 @@
|
||||
"use strict"
|
||||
|
||||
var writeIEEE754 = require('../float_parser').writeIEEE754,
|
||||
readIEEE754 = require('../float_parser').readIEEE754,
|
||||
f = require('util').format,
|
||||
Long = require('../long').Long,
|
||||
Double = require('../double').Double,
|
||||
Timestamp = require('../timestamp').Timestamp,
|
||||
ObjectID = require('../objectid').ObjectID,
|
||||
Symbol = require('../symbol').Symbol,
|
||||
Code = require('../code').Code,
|
||||
MinKey = require('../min_key').MinKey,
|
||||
MaxKey = require('../max_key').MaxKey,
|
||||
DBRef = require('../db_ref').DBRef,
|
||||
BSONRegExp = require('../regexp').BSONRegExp,
|
||||
Binary = require('../binary').Binary;
|
||||
|
||||
var deserialize = function(buffer, options, isArray) {
|
||||
var index = 0;
|
||||
// Read the document size
|
||||
var size = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
|
||||
// Ensure buffer is valid size
|
||||
if(size < 5 || buffer.length < size) {
|
||||
throw new Error("corrupt bson message");
|
||||
}
|
||||
|
||||
// Illegal end value
|
||||
if(buffer[size - 1] != 0) {
|
||||
throw new Error("One object, sized correctly, with a spot for an EOO, but the EOO isn't 0x00");
|
||||
}
|
||||
|
||||
// Start deserializtion
|
||||
return deserializeObject(buffer, options, isArray);
|
||||
}
|
||||
|
||||
// Reads in a C style string
|
||||
var readCStyleStringSpecial = function(buffer, index) {
|
||||
// Get the start search index
|
||||
var i = index;
|
||||
// Locate the end of the c string
|
||||
while(buffer[i] !== 0x00 && i < buffer.length) {
|
||||
i++
|
||||
}
|
||||
// If are at the end of the buffer there is a problem with the document
|
||||
if(i >= buffer.length) throw new Error("Bad BSON Document: illegal CString")
|
||||
// Grab utf8 encoded string
|
||||
var string = buffer.toString('utf8', index, i);
|
||||
// Update index position
|
||||
index = i + 1;
|
||||
// Return string
|
||||
return {s: string, i: index};
|
||||
}
|
||||
|
||||
var deserializeObject = function(buffer, options, isArray) {
|
||||
// Options
|
||||
options = options == null ? {} : options;
|
||||
var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions'];
|
||||
var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions'];
|
||||
var cacheFunctionsCrc32 = options['cacheFunctionsCrc32'] == null ? false : options['cacheFunctionsCrc32'];
|
||||
var promoteLongs = options['promoteLongs'] == null ? true : options['promoteLongs'];
|
||||
var fieldsAsRaw = options['fieldsAsRaw'] == null ? {} : options['fieldsAsRaw'];
|
||||
// Return BSONRegExp objects instead of native regular expressions
|
||||
var bsonRegExp = typeof options['bsonRegExp'] == 'boolean' ? options['bsonRegExp'] : false;
|
||||
var promoteBuffers = options['promoteBuffers'] == null ? false : options['promoteBuffers'];
|
||||
|
||||
// Validate that we have at least 4 bytes of buffer
|
||||
if(buffer.length < 5) throw new Error("corrupt bson message < 5 bytes long");
|
||||
|
||||
// Set up index
|
||||
var index = typeof options['index'] == 'number' ? options['index'] : 0;
|
||||
|
||||
// Read the document size
|
||||
var size = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
|
||||
// Ensure buffer is valid size
|
||||
if(size < 5 || size > buffer.length) throw new Error("corrupt bson message");
|
||||
|
||||
// Create holding object
|
||||
var object = isArray ? [] : {};
|
||||
|
||||
// While we have more left data left keep parsing
|
||||
while(true) {
|
||||
// Read the type
|
||||
var elementType = buffer[index++];
|
||||
// If we get a zero it's the last byte, exit
|
||||
if(elementType == 0) break;
|
||||
// Read the name of the field
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var name = r.s;
|
||||
index = r.i;
|
||||
|
||||
// Switch on the type
|
||||
if(elementType == BSON.BSON_DATA_OID) {
|
||||
var string = buffer.toString('binary', index, index + 12);
|
||||
// Decode the oid
|
||||
object[name] = new ObjectID(string);
|
||||
// Update index
|
||||
index = index + 12;
|
||||
} else if(elementType == BSON.BSON_DATA_STRING) {
|
||||
// Read the content of the field
|
||||
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(stringSize <= 0 || stringSize > (buffer.length - index) || buffer[index + stringSize - 1] != 0) throw new Error("bad string length in bson");
|
||||
// Add string to object
|
||||
object[name] = buffer.toString('utf8', index, index + stringSize - 1);
|
||||
// Update parse index position
|
||||
index = index + stringSize;
|
||||
} else if(elementType == BSON.BSON_DATA_INT) {
|
||||
// Decode the 32bit value
|
||||
object[name] = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
} else if(elementType == BSON.BSON_DATA_NUMBER) {
|
||||
// Decode the double value
|
||||
object[name] = readIEEE754(buffer, index, 'little', 52, 8);
|
||||
// Update the index
|
||||
index = index + 8;
|
||||
} else if(elementType == BSON.BSON_DATA_DATE) {
|
||||
// Unpack the low and high bits
|
||||
var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Set date object
|
||||
object[name] = new Date(new Long(lowBits, highBits).toNumber());
|
||||
} else if(elementType == BSON.BSON_DATA_BOOLEAN) {
|
||||
// Parse the boolean value
|
||||
object[name] = buffer[index++] == 1;
|
||||
} else if(elementType == BSON.BSON_DATA_UNDEFINED || elementType == BSON.BSON_DATA_NULL) {
|
||||
// Parse the boolean value
|
||||
object[name] = null;
|
||||
} else if(elementType == BSON.BSON_DATA_BINARY) {
|
||||
// Decode the size of the binary blob
|
||||
var binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Decode the subtype
|
||||
var subType = buffer[index++];
|
||||
// Decode as raw Buffer object if options specifies it
|
||||
if(buffer['slice'] != null) {
|
||||
// If we have subtype 2 skip the 4 bytes for the size
|
||||
if(subType == Binary.SUBTYPE_BYTE_ARRAY) {
|
||||
binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
}
|
||||
if(promoteBuffers) {
|
||||
// assign reference to sliced Buffer object
|
||||
object[name] = buffer.slice(index, index + binarySize);
|
||||
} else {
|
||||
// Slice the data
|
||||
object[name] = new Binary(buffer.slice(index, index + binarySize), subType);
|
||||
}
|
||||
} else {
|
||||
var _buffer = typeof Uint8Array != 'undefined' ? new Uint8Array(new ArrayBuffer(binarySize)) : new Array(binarySize);
|
||||
// If we have subtype 2 skip the 4 bytes for the size
|
||||
if(subType == Binary.SUBTYPE_BYTE_ARRAY) {
|
||||
binarySize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
}
|
||||
// Copy the data
|
||||
for(var i = 0; i < binarySize; i++) {
|
||||
_buffer[i] = buffer[index + i];
|
||||
}
|
||||
if(promoteBuffers) {
|
||||
// assign reference to Buffer object
|
||||
object[name] = _buffer;
|
||||
} else {
|
||||
// Create the binary object
|
||||
object[name] = new Binary(_buffer, subType);
|
||||
}
|
||||
}
|
||||
// Update the index
|
||||
index = index + binarySize;
|
||||
} else if(elementType == BSON.BSON_DATA_ARRAY) {
|
||||
options['index'] = index;
|
||||
// Decode the size of the array document
|
||||
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
||||
var arrayOptions = options;
|
||||
|
||||
// All elements of array to be returned as raw bson
|
||||
if(fieldsAsRaw[name]) {
|
||||
arrayOptions = {};
|
||||
for(var n in options) arrayOptions[n] = options[n];
|
||||
arrayOptions['raw'] = true;
|
||||
}
|
||||
|
||||
// Set the array to the object
|
||||
object[name] = deserializeObject(buffer, arrayOptions, true);
|
||||
// Adjust the index
|
||||
index = index + objectSize;
|
||||
} else if(elementType == BSON.BSON_DATA_OBJECT) {
|
||||
options['index'] = index;
|
||||
// Decode the size of the object document
|
||||
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(objectSize <= 0 || objectSize > (buffer.length - index)) throw new Error("bad embedded document length in bson");
|
||||
|
||||
// We have a raw value
|
||||
if(options['raw']) {
|
||||
// Set the array to the object
|
||||
object[name] = buffer.slice(index, index + objectSize);
|
||||
} else {
|
||||
// Set the array to the object
|
||||
object[name] = deserializeObject(buffer, options, false);
|
||||
}
|
||||
|
||||
// Adjust the index
|
||||
index = index + objectSize;
|
||||
} else if(elementType == BSON.BSON_DATA_REGEXP && bsonRegExp == false) {
|
||||
// Create the regexp
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var source = r.s;
|
||||
index = r.i;
|
||||
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var regExpOptions = r.s;
|
||||
index = r.i;
|
||||
|
||||
// For each option add the corresponding one for javascript
|
||||
var optionsArray = new Array(regExpOptions.length);
|
||||
|
||||
// Parse options
|
||||
for(var i = 0; i < regExpOptions.length; i++) {
|
||||
switch(regExpOptions[i]) {
|
||||
case 'm':
|
||||
optionsArray[i] = 'm';
|
||||
break;
|
||||
case 's':
|
||||
optionsArray[i] = 'g';
|
||||
break;
|
||||
case 'i':
|
||||
optionsArray[i] = 'i';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
object[name] = new RegExp(source, optionsArray.join(''));
|
||||
} else if(elementType == BSON.BSON_DATA_REGEXP && bsonRegExp == true) {
|
||||
// Create the regexp
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var source = r.s;
|
||||
index = r.i;
|
||||
|
||||
var r = readCStyleStringSpecial(buffer, index);
|
||||
var regExpOptions = r.s;
|
||||
index = r.i;
|
||||
|
||||
// Set the object
|
||||
object[name] = new BSONRegExp(source, regExpOptions);
|
||||
} else if(elementType == BSON.BSON_DATA_LONG) {
|
||||
// Unpack the low and high bits
|
||||
var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Create long object
|
||||
var long = new Long(lowBits, highBits);
|
||||
// Promote the long if possible
|
||||
if(promoteLongs) {
|
||||
object[name] = long.lessThanOrEqual(JS_INT_MAX_LONG) && long.greaterThanOrEqual(JS_INT_MIN_LONG) ? long.toNumber() : long;
|
||||
} else {
|
||||
object[name] = long;
|
||||
}
|
||||
} else if(elementType == BSON.BSON_DATA_SYMBOL) {
|
||||
// Read the content of the field
|
||||
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(stringSize <= 0 || stringSize > (buffer.length - index) || buffer[index + stringSize - 1] != 0) throw new Error("bad string length in bson");
|
||||
// Add string to object
|
||||
object[name] = new Symbol(buffer.toString('utf8', index, index + stringSize - 1));
|
||||
// Update parse index position
|
||||
index = index + stringSize;
|
||||
} else if(elementType == BSON.BSON_DATA_TIMESTAMP) {
|
||||
// Unpack the low and high bits
|
||||
var lowBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
var highBits = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Set the object
|
||||
object[name] = new Timestamp(lowBits, highBits);
|
||||
} else if(elementType == BSON.BSON_DATA_MIN_KEY) {
|
||||
// Parse the object
|
||||
object[name] = new MinKey();
|
||||
} else if(elementType == BSON.BSON_DATA_MAX_KEY) {
|
||||
// Parse the object
|
||||
object[name] = new MaxKey();
|
||||
} else if(elementType == BSON.BSON_DATA_CODE) {
|
||||
// Read the content of the field
|
||||
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(stringSize <= 0 || stringSize > (buffer.length - index) || buffer[index + stringSize - 1] != 0) throw new Error("bad string length in bson");
|
||||
// Function string
|
||||
var functionString = buffer.toString('utf8', index, index + stringSize - 1);
|
||||
|
||||
// If we are evaluating the functions
|
||||
if(evalFunctions) {
|
||||
// Contains the value we are going to set
|
||||
var value = null;
|
||||
// If we have cache enabled let's look for the md5 of the function in the cache
|
||||
if(cacheFunctions) {
|
||||
var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString;
|
||||
// Got to do this to avoid V8 deoptimizing the call due to finding eval
|
||||
object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
|
||||
} else {
|
||||
// Set directly
|
||||
object[name] = isolateEval(functionString);
|
||||
}
|
||||
} else {
|
||||
object[name] = new Code(functionString, {});
|
||||
}
|
||||
|
||||
// Update parse index position
|
||||
index = index + stringSize;
|
||||
} else if(elementType == BSON.BSON_DATA_CODE_W_SCOPE) {
|
||||
// Read the content of the field
|
||||
var totalSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
|
||||
// Validate if string Size is larger than the actual provided buffer
|
||||
if(stringSize <= 0 || stringSize > (buffer.length - index) || buffer[index + stringSize - 1] != 0) throw new Error("bad string length in bson");
|
||||
// Javascript function
|
||||
var functionString = buffer.toString('utf8', index, index + stringSize - 1);
|
||||
// Update parse index position
|
||||
index = index + stringSize;
|
||||
// Parse the element
|
||||
options['index'] = index;
|
||||
// Decode the size of the object document
|
||||
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24;
|
||||
// Decode the scope object
|
||||
var scopeObject = deserializeObject(buffer, options, false);
|
||||
// Adjust the index
|
||||
index = index + objectSize;
|
||||
|
||||
// If we are evaluating the functions
|
||||
if(evalFunctions) {
|
||||
// Contains the value we are going to set
|
||||
var value = null;
|
||||
// If we have cache enabled let's look for the md5 of the function in the cache
|
||||
if(cacheFunctions) {
|
||||
var hash = cacheFunctionsCrc32 ? crc32(functionString) : functionString;
|
||||
// Got to do this to avoid V8 deoptimizing the call due to finding eval
|
||||
object[name] = isolateEvalWithHash(functionCache, hash, functionString, object);
|
||||
} else {
|
||||
// Set directly
|
||||
object[name] = isolateEval(functionString);
|
||||
}
|
||||
|
||||
// Set the scope on the object
|
||||
object[name].scope = scopeObject;
|
||||
} else {
|
||||
object[name] = new Code(functionString, scopeObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we have a db ref object
|
||||
if(object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']);
|
||||
|
||||
// Return the final objects
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure eval is isolated.
|
||||
*
|
||||
* @ignore
|
||||
* @api private
|
||||
*/
|
||||
var isolateEvalWithHash = function(functionCache, hash, functionString, object) {
|
||||
// Contains the value we are going to set
|
||||
var value = null;
|
||||
|
||||
// Check for cache hit, eval if missing and return cached function
|
||||
if(functionCache[hash] == null) {
|
||||
eval("value = " + functionString);
|
||||
functionCache[hash] = value;
|
||||
}
|
||||
// Set the object
|
||||
return functionCache[hash].bind(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure eval is isolated.
|
||||
*
|
||||
* @ignore
|
||||
* @api private
|
||||
*/
|
||||
var isolateEval = function(functionString) {
|
||||
// Contains the value we are going to set
|
||||
var value = null;
|
||||
// Eval the function
|
||||
eval("value = " + functionString);
|
||||
return value;
|
||||
}
|
||||
|
||||
var BSON = {};
|
||||
|
||||
/**
|
||||
* Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5
|
||||
*
|
||||
* @ignore
|
||||
* @api private
|
||||
*/
|
||||
var functionCache = BSON.functionCache = {};
|
||||
|
||||
/**
|
||||
* Number BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_NUMBER
|
||||
**/
|
||||
BSON.BSON_DATA_NUMBER = 1;
|
||||
/**
|
||||
* String BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_STRING
|
||||
**/
|
||||
BSON.BSON_DATA_STRING = 2;
|
||||
/**
|
||||
* Object BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_OBJECT
|
||||
**/
|
||||
BSON.BSON_DATA_OBJECT = 3;
|
||||
/**
|
||||
* Array BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_ARRAY
|
||||
**/
|
||||
BSON.BSON_DATA_ARRAY = 4;
|
||||
/**
|
||||
* Binary BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_BINARY
|
||||
**/
|
||||
BSON.BSON_DATA_BINARY = 5;
|
||||
/**
|
||||
* ObjectID BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_OID
|
||||
**/
|
||||
BSON.BSON_DATA_OID = 7;
|
||||
/**
|
||||
* Boolean BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_BOOLEAN
|
||||
**/
|
||||
BSON.BSON_DATA_BOOLEAN = 8;
|
||||
/**
|
||||
* Date BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_DATE
|
||||
**/
|
||||
BSON.BSON_DATA_DATE = 9;
|
||||
/**
|
||||
* null BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_NULL
|
||||
**/
|
||||
BSON.BSON_DATA_NULL = 10;
|
||||
/**
|
||||
* RegExp BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_REGEXP
|
||||
**/
|
||||
BSON.BSON_DATA_REGEXP = 11;
|
||||
/**
|
||||
* Code BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_CODE
|
||||
**/
|
||||
BSON.BSON_DATA_CODE = 13;
|
||||
/**
|
||||
* Symbol BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_SYMBOL
|
||||
**/
|
||||
BSON.BSON_DATA_SYMBOL = 14;
|
||||
/**
|
||||
* Code with Scope BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_CODE_W_SCOPE
|
||||
**/
|
||||
BSON.BSON_DATA_CODE_W_SCOPE = 15;
|
||||
/**
|
||||
* 32 bit Integer BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_INT
|
||||
**/
|
||||
BSON.BSON_DATA_INT = 16;
|
||||
/**
|
||||
* Timestamp BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_TIMESTAMP
|
||||
**/
|
||||
BSON.BSON_DATA_TIMESTAMP = 17;
|
||||
/**
|
||||
* Long BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_LONG
|
||||
**/
|
||||
BSON.BSON_DATA_LONG = 18;
|
||||
/**
|
||||
* MinKey BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_MIN_KEY
|
||||
**/
|
||||
BSON.BSON_DATA_MIN_KEY = 0xff;
|
||||
/**
|
||||
* MaxKey BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_MAX_KEY
|
||||
**/
|
||||
BSON.BSON_DATA_MAX_KEY = 0x7f;
|
||||
|
||||
/**
|
||||
* Binary Default Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_DEFAULT
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
||||
/**
|
||||
* Binary Function Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_FUNCTION
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
|
||||
/**
|
||||
* Binary Byte Array Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
|
||||
/**
|
||||
* Binary UUID Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_UUID
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_UUID = 3;
|
||||
/**
|
||||
* Binary MD5 Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_MD5
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
|
||||
/**
|
||||
* Binary User Defined Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
|
||||
|
||||
// BSON MAX VALUES
|
||||
BSON.BSON_INT32_MAX = 0x7FFFFFFF;
|
||||
BSON.BSON_INT32_MIN = -0x80000000;
|
||||
|
||||
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
||||
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
|
||||
|
||||
// JS MAX PRECISE VALUES
|
||||
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
|
||||
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
|
||||
|
||||
// Internal long versions
|
||||
var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
|
||||
var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.
|
||||
|
||||
module.exports = deserialize
|
||||
912
node_modules/bson/lib/bson/parser/serializer.js
generated
vendored
Normal file
912
node_modules/bson/lib/bson/parser/serializer.js
generated
vendored
Normal file
@@ -0,0 +1,912 @@
|
||||
"use strict"
|
||||
|
||||
var writeIEEE754 = require('../float_parser').writeIEEE754,
|
||||
readIEEE754 = require('../float_parser').readIEEE754,
|
||||
Long = require('../long').Long,
|
||||
Map = require('../map'),
|
||||
Double = require('../double').Double,
|
||||
Timestamp = require('../timestamp').Timestamp,
|
||||
ObjectID = require('../objectid').ObjectID,
|
||||
Symbol = require('../symbol').Symbol,
|
||||
Code = require('../code').Code,
|
||||
BSONRegExp = require('../regexp').BSONRegExp,
|
||||
MinKey = require('../min_key').MinKey,
|
||||
MaxKey = require('../max_key').MaxKey,
|
||||
DBRef = require('../db_ref').DBRef,
|
||||
Binary = require('../binary').Binary;
|
||||
|
||||
var regexp = /\x00/
|
||||
|
||||
// To ensure that 0.4 of node works correctly
|
||||
var isDate = function isDate(d) {
|
||||
return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
|
||||
}
|
||||
|
||||
var isRegExp = function isRegExp(d) {
|
||||
return Object.prototype.toString.call(d) === '[object RegExp]';
|
||||
}
|
||||
|
||||
var serializeString = function(buffer, key, value, index) {
|
||||
// Encode String type
|
||||
buffer[index++] = BSON.BSON_DATA_STRING;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes + 1;
|
||||
buffer[index - 1] = 0;
|
||||
// Write the string
|
||||
var size = buffer.write(value, index + 4, 'utf8');
|
||||
// Write the size of the string to buffer
|
||||
buffer[index + 3] = (size + 1 >> 24) & 0xff;
|
||||
buffer[index + 2] = (size + 1 >> 16) & 0xff;
|
||||
buffer[index + 1] = (size + 1 >> 8) & 0xff;
|
||||
buffer[index] = size + 1 & 0xff;
|
||||
// Update index
|
||||
index = index + 4 + size;
|
||||
// Write zero
|
||||
buffer[index++] = 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeNumber = function(buffer, key, value, index) {
|
||||
// We have an integer value
|
||||
if(Math.floor(value) === value && value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
||||
// If the value fits in 32 bits encode as int, if it fits in a double
|
||||
// encode it as a double, otherwise long
|
||||
if(value >= BSON.BSON_INT32_MIN && value <= BSON.BSON_INT32_MAX) {
|
||||
// Set int type 32 bits or less
|
||||
buffer[index++] = BSON.BSON_DATA_INT;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write the int value
|
||||
buffer[index++] = value & 0xff;
|
||||
buffer[index++] = (value >> 8) & 0xff;
|
||||
buffer[index++] = (value >> 16) & 0xff;
|
||||
buffer[index++] = (value >> 24) & 0xff;
|
||||
} else if(value >= BSON.JS_INT_MIN && value <= BSON.JS_INT_MAX) {
|
||||
// Encode as double
|
||||
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write float
|
||||
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
||||
// Ajust index
|
||||
index = index + 8;
|
||||
} else {
|
||||
// Set long type
|
||||
buffer[index++] = BSON.BSON_DATA_LONG;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
var longVal = Long.fromNumber(value);
|
||||
var lowBits = longVal.getLowBits();
|
||||
var highBits = longVal.getHighBits();
|
||||
// Encode low bits
|
||||
buffer[index++] = lowBits & 0xff;
|
||||
buffer[index++] = (lowBits >> 8) & 0xff;
|
||||
buffer[index++] = (lowBits >> 16) & 0xff;
|
||||
buffer[index++] = (lowBits >> 24) & 0xff;
|
||||
// Encode high bits
|
||||
buffer[index++] = highBits & 0xff;
|
||||
buffer[index++] = (highBits >> 8) & 0xff;
|
||||
buffer[index++] = (highBits >> 16) & 0xff;
|
||||
buffer[index++] = (highBits >> 24) & 0xff;
|
||||
}
|
||||
} else {
|
||||
// Encode as double
|
||||
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write float
|
||||
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
||||
// Ajust index
|
||||
index = index + 8;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeUndefined = function(buffer, key, value, index) {
|
||||
// Set long type
|
||||
buffer[index++] = BSON.BSON_DATA_NULL;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeBoolean = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_BOOLEAN;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Encode the boolean value
|
||||
buffer[index++] = value ? 1 : 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeDate = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_DATE;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
|
||||
// Write the date
|
||||
var dateInMilis = Long.fromNumber(value.getTime());
|
||||
var lowBits = dateInMilis.getLowBits();
|
||||
var highBits = dateInMilis.getHighBits();
|
||||
// Encode low bits
|
||||
buffer[index++] = lowBits & 0xff;
|
||||
buffer[index++] = (lowBits >> 8) & 0xff;
|
||||
buffer[index++] = (lowBits >> 16) & 0xff;
|
||||
buffer[index++] = (lowBits >> 24) & 0xff;
|
||||
// Encode high bits
|
||||
buffer[index++] = highBits & 0xff;
|
||||
buffer[index++] = (highBits >> 8) & 0xff;
|
||||
buffer[index++] = (highBits >> 16) & 0xff;
|
||||
buffer[index++] = (highBits >> 24) & 0xff;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeRegExp = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_REGEXP;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
if (value.source && value.source.match(regexp) != null) {
|
||||
throw Error("value " + value.source + " must not contain null bytes");
|
||||
}
|
||||
// Adjust the index
|
||||
index = index + buffer.write(value.source, index, 'utf8');
|
||||
// Write zero
|
||||
buffer[index++] = 0x00;
|
||||
// Write the parameters
|
||||
if(value.global) buffer[index++] = 0x73; // s
|
||||
if(value.ignoreCase) buffer[index++] = 0x69; // i
|
||||
if(value.multiline) buffer[index++] = 0x6d; // m
|
||||
// Add ending zero
|
||||
buffer[index++] = 0x00;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeBSONRegExp = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_REGEXP;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Adjust the index
|
||||
index = index + buffer.write(value.pattern, index, 'utf8');
|
||||
// Write zero
|
||||
buffer[index++] = 0x00;
|
||||
// Write the options
|
||||
index = index + buffer.write(value.options, index, 'utf8');
|
||||
// Add ending zero
|
||||
buffer[index++] = 0x00;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeMinMax = function(buffer, key, value, index) {
|
||||
// Write the type of either min or max key
|
||||
if(value === null) {
|
||||
buffer[index++] = BSON.BSON_DATA_NULL;
|
||||
} else if(value instanceof MinKey) {
|
||||
buffer[index++] = BSON.BSON_DATA_MIN_KEY;
|
||||
} else {
|
||||
buffer[index++] = BSON.BSON_DATA_MAX_KEY;
|
||||
}
|
||||
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeObjectId = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_OID;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
|
||||
// Write the objectId into the shared buffer
|
||||
buffer.write(value.id, index, 'binary')
|
||||
|
||||
// Ajust index
|
||||
return index + 12;
|
||||
}
|
||||
|
||||
var serializeBuffer = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_BINARY;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Get size of the buffer (current write point)
|
||||
var size = value.length;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index++] = size & 0xff;
|
||||
buffer[index++] = (size >> 8) & 0xff;
|
||||
buffer[index++] = (size >> 16) & 0xff;
|
||||
buffer[index++] = (size >> 24) & 0xff;
|
||||
// Write the default subtype
|
||||
buffer[index++] = BSON.BSON_BINARY_SUBTYPE_DEFAULT;
|
||||
// Copy the content form the binary field to the buffer
|
||||
value.copy(buffer, index, 0, size);
|
||||
// Adjust the index
|
||||
index = index + size;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeObject = function(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined) {
|
||||
// Write the type
|
||||
buffer[index++] = Array.isArray(value) ? BSON.BSON_DATA_ARRAY : BSON.BSON_DATA_OBJECT;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
var endIndex = serializeInto(buffer, value, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined);
|
||||
// Write size
|
||||
var size = endIndex - index;
|
||||
return endIndex;
|
||||
}
|
||||
|
||||
var serializeLong = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = value instanceof Long ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write the date
|
||||
var lowBits = value.getLowBits();
|
||||
var highBits = value.getHighBits();
|
||||
// Encode low bits
|
||||
buffer[index++] = lowBits & 0xff;
|
||||
buffer[index++] = (lowBits >> 8) & 0xff;
|
||||
buffer[index++] = (lowBits >> 16) & 0xff;
|
||||
buffer[index++] = (lowBits >> 24) & 0xff;
|
||||
// Encode high bits
|
||||
buffer[index++] = highBits & 0xff;
|
||||
buffer[index++] = (highBits >> 8) & 0xff;
|
||||
buffer[index++] = (highBits >> 16) & 0xff;
|
||||
buffer[index++] = (highBits >> 24) & 0xff;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeDouble = function(buffer, key, value, index) {
|
||||
// Encode as double
|
||||
buffer[index++] = BSON.BSON_DATA_NUMBER;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write float
|
||||
writeIEEE754(buffer, value, index, 'little', 52, 8);
|
||||
// Ajust index
|
||||
index = index + 8;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeFunction = function(buffer, key, value, index, checkKeys, depth) {
|
||||
buffer[index++] = BSON.BSON_DATA_CODE;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Function string
|
||||
var functionString = value.toString();
|
||||
// Write the string
|
||||
var size = buffer.write(functionString, index + 4, 'utf8') + 1;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index] = size & 0xff;
|
||||
buffer[index + 1] = (size >> 8) & 0xff;
|
||||
buffer[index + 2] = (size >> 16) & 0xff;
|
||||
buffer[index + 3] = (size >> 24) & 0xff;
|
||||
// Update index
|
||||
index = index + 4 + size - 1;
|
||||
// Write zero
|
||||
buffer[index++] = 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeCode = function(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined) {
|
||||
if(value.scope != null && Object.keys(value.scope).length > 0) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_CODE_W_SCOPE;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
|
||||
// Starting index
|
||||
var startIndex = index;
|
||||
|
||||
// Serialize the function
|
||||
// Get the function string
|
||||
var functionString = typeof value.code == 'string' ? value.code : value.code.toString();
|
||||
// Index adjustment
|
||||
index = index + 4;
|
||||
// Write string into buffer
|
||||
var codeSize = buffer.write(functionString, index + 4, 'utf8') + 1;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index] = codeSize & 0xff;
|
||||
buffer[index + 1] = (codeSize >> 8) & 0xff;
|
||||
buffer[index + 2] = (codeSize >> 16) & 0xff;
|
||||
buffer[index + 3] = (codeSize >> 24) & 0xff;
|
||||
// Write end 0
|
||||
buffer[index + 4 + codeSize - 1] = 0;
|
||||
// Write the
|
||||
index = index + codeSize + 4;
|
||||
|
||||
//
|
||||
// Serialize the scope value
|
||||
var endIndex = serializeInto(buffer, value.scope, checkKeys, index, depth + 1, serializeFunctions, ignoreUndefined)
|
||||
index = endIndex - 1;
|
||||
|
||||
// Writ the total
|
||||
var totalSize = endIndex - startIndex;
|
||||
|
||||
// Write the total size of the object
|
||||
buffer[startIndex++] = totalSize & 0xff;
|
||||
buffer[startIndex++] = (totalSize >> 8) & 0xff;
|
||||
buffer[startIndex++] = (totalSize >> 16) & 0xff;
|
||||
buffer[startIndex++] = (totalSize >> 24) & 0xff;
|
||||
// Write trailing zero
|
||||
buffer[index++] = 0;
|
||||
} else {
|
||||
buffer[index++] = BSON.BSON_DATA_CODE;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Function string
|
||||
var functionString = value.code.toString();
|
||||
// Write the string
|
||||
var size = buffer.write(functionString, index + 4, 'utf8') + 1;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index] = size & 0xff;
|
||||
buffer[index + 1] = (size >> 8) & 0xff;
|
||||
buffer[index + 2] = (size >> 16) & 0xff;
|
||||
buffer[index + 3] = (size >> 24) & 0xff;
|
||||
// Update index
|
||||
index = index + 4 + size - 1;
|
||||
// Write zero
|
||||
buffer[index++] = 0;
|
||||
}
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeBinary = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_BINARY;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Extract the buffer
|
||||
var data = value.value(true);
|
||||
// Calculate size
|
||||
var size = value.position;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index++] = size & 0xff;
|
||||
buffer[index++] = (size >> 8) & 0xff;
|
||||
buffer[index++] = (size >> 16) & 0xff;
|
||||
buffer[index++] = (size >> 24) & 0xff;
|
||||
// Write the subtype to the buffer
|
||||
buffer[index++] = value.sub_type;
|
||||
|
||||
// If we have binary type 2 the 4 first bytes are the size
|
||||
if(value.sub_type == Binary.SUBTYPE_BYTE_ARRAY) {
|
||||
buffer[index++] = size & 0xff;
|
||||
buffer[index++] = (size >> 8) & 0xff;
|
||||
buffer[index++] = (size >> 16) & 0xff;
|
||||
buffer[index++] = (size >> 24) & 0xff;
|
||||
}
|
||||
|
||||
// Write the data to the object
|
||||
data.copy(buffer, index, 0, value.position);
|
||||
// Adjust the index
|
||||
index = index + value.position;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeSymbol = function(buffer, key, value, index) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_SYMBOL;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
// Write the string
|
||||
var size = buffer.write(value.value, index + 4, 'utf8') + 1;
|
||||
// Write the size of the string to buffer
|
||||
buffer[index] = size & 0xff;
|
||||
buffer[index + 1] = (size >> 8) & 0xff;
|
||||
buffer[index + 2] = (size >> 16) & 0xff;
|
||||
buffer[index + 3] = (size >> 24) & 0xff;
|
||||
// Update index
|
||||
index = index + 4 + size - 1;
|
||||
// Write zero
|
||||
buffer[index++] = 0x00;
|
||||
return index;
|
||||
}
|
||||
|
||||
var serializeDBRef = function(buffer, key, value, index, depth, serializeFunctions) {
|
||||
// Write the type
|
||||
buffer[index++] = BSON.BSON_DATA_OBJECT;
|
||||
// Number of written bytes
|
||||
var numberOfWrittenBytes = buffer.write(key, index, 'utf8');
|
||||
|
||||
// Encode the name
|
||||
index = index + numberOfWrittenBytes;
|
||||
buffer[index++] = 0;
|
||||
|
||||
var startIndex = index;
|
||||
var endIndex;
|
||||
|
||||
// Serialize object
|
||||
if(null != value.db) {
|
||||
endIndex = serializeInto(buffer, {
|
||||
'$ref': value.namespace
|
||||
, '$id' : value.oid
|
||||
, '$db' : value.db
|
||||
}, false, index, depth + 1, serializeFunctions);
|
||||
} else {
|
||||
endIndex = serializeInto(buffer, {
|
||||
'$ref': value.namespace
|
||||
, '$id' : value.oid
|
||||
}, false, index, depth + 1, serializeFunctions);
|
||||
}
|
||||
|
||||
// Calculate object size
|
||||
var size = endIndex - startIndex;
|
||||
// Write the size
|
||||
buffer[startIndex++] = size & 0xff;
|
||||
buffer[startIndex++] = (size >> 8) & 0xff;
|
||||
buffer[startIndex++] = (size >> 16) & 0xff;
|
||||
buffer[startIndex++] = (size >> 24) & 0xff;
|
||||
// Set index
|
||||
return endIndex;
|
||||
}
|
||||
|
||||
var serializeInto = function serializeInto(buffer, object, checkKeys, startingIndex, depth, serializeFunctions, ignoreUndefined) {
|
||||
startingIndex = startingIndex || 0;
|
||||
|
||||
// Start place to serialize into
|
||||
var index = startingIndex + 4;
|
||||
var self = this;
|
||||
|
||||
// Special case isArray
|
||||
if(Array.isArray(object)) {
|
||||
// Get object keys
|
||||
for(var i = 0; i < object.length; i++) {
|
||||
var key = "" + i;
|
||||
var value = object[i];
|
||||
|
||||
// Is there an override value
|
||||
if(value && value.toBSON) {
|
||||
if(typeof value.toBSON != 'function') throw new Error("toBSON is not a function");
|
||||
value = value.toBSON();
|
||||
}
|
||||
|
||||
var type = typeof value;
|
||||
if(type == 'string') {
|
||||
index = serializeString(buffer, key, value, index);
|
||||
} else if(type == 'number') {
|
||||
index = serializeNumber(buffer, key, value, index);
|
||||
} else if(type == 'boolean') {
|
||||
index = serializeBoolean(buffer, key, value, index);
|
||||
} else if(value instanceof Date || isDate(value)) {
|
||||
index = serializeDate(buffer, key, value, index);
|
||||
} else if(type == 'undefined' || value == null) {
|
||||
index = serializeUndefined(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'ObjectID') {
|
||||
index = serializeObjectId(buffer, key, value, index);
|
||||
} else if(Buffer.isBuffer(value)) {
|
||||
index = serializeBuffer(buffer, key, value, index);
|
||||
} else if(value instanceof RegExp || isRegExp(value)) {
|
||||
index = serializeRegExp(buffer, key, value, index);
|
||||
} else if(type == 'object' && value['_bsontype'] == null) {
|
||||
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') {
|
||||
index = serializeLong(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Double') {
|
||||
index = serializeDouble(buffer, key, value, index);
|
||||
} else if(typeof value == 'function' && serializeFunctions) {
|
||||
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'Code') {
|
||||
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(value['_bsontype'] == 'Binary') {
|
||||
index = serializeBinary(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Symbol') {
|
||||
index = serializeSymbol(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'DBRef') {
|
||||
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'BSONRegExp') {
|
||||
index = serializeBSONRegExp(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
|
||||
index = serializeMinMax(buffer, key, value, index);
|
||||
}
|
||||
}
|
||||
} else if(object instanceof Map) {
|
||||
var iterator = object.entries();
|
||||
var done = false;
|
||||
|
||||
while(!done) {
|
||||
// Unpack the next entry
|
||||
var entry = iterator.next();
|
||||
done = entry.done;
|
||||
// Are we done, then skip and terminate
|
||||
if(done) continue;
|
||||
|
||||
// Get the entry values
|
||||
var key = entry.value[0];
|
||||
var value = entry.value[1];
|
||||
|
||||
// Check the type of the value
|
||||
var type = typeof value;
|
||||
|
||||
// Check the key and throw error if it's illegal
|
||||
if(key != '$db' && key != '$ref' && key != '$id') {
|
||||
if (key.match(regexp) != null) {
|
||||
// The BSON spec doesn't allow keys with null bytes because keys are
|
||||
// null-terminated.
|
||||
throw Error("key " + key + " must not contain null bytes");
|
||||
}
|
||||
|
||||
if (checkKeys) {
|
||||
if('$' == key[0]) {
|
||||
throw Error("key " + key + " must not start with '$'");
|
||||
} else if (!!~key.indexOf('.')) {
|
||||
throw Error("key " + key + " must not contain '.'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// console.log("---------------------------------------------------")
|
||||
// console.dir("key = " + key)
|
||||
// console.dir("value = " + value)
|
||||
|
||||
if(type == 'string') {
|
||||
index = serializeString(buffer, key, value, index);
|
||||
} else if(type == 'number') {
|
||||
index = serializeNumber(buffer, key, value, index);
|
||||
} else if(type == 'boolean') {
|
||||
index = serializeBoolean(buffer, key, value, index);
|
||||
} else if(value instanceof Date || isDate(value)) {
|
||||
index = serializeDate(buffer, key, value, index);
|
||||
} else if(value === undefined && ignoreUndefined == true) {
|
||||
} else if(value === null || value === undefined) {
|
||||
index = serializeUndefined(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'ObjectID') {
|
||||
index = serializeObjectId(buffer, key, value, index);
|
||||
} else if(Buffer.isBuffer(value)) {
|
||||
index = serializeBuffer(buffer, key, value, index);
|
||||
} else if(value instanceof RegExp || isRegExp(value)) {
|
||||
index = serializeRegExp(buffer, key, value, index);
|
||||
} else if(type == 'object' && value['_bsontype'] == null) {
|
||||
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') {
|
||||
index = serializeLong(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Double') {
|
||||
index = serializeDouble(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Code') {
|
||||
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(typeof value == 'function' && serializeFunctions) {
|
||||
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'Binary') {
|
||||
index = serializeBinary(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Symbol') {
|
||||
index = serializeSymbol(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'DBRef') {
|
||||
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'BSONRegExp') {
|
||||
index = serializeBSONRegExp(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
|
||||
index = serializeMinMax(buffer, key, value, index);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Did we provide a custom serialization method
|
||||
if(object.toBSON) {
|
||||
if(typeof object.toBSON != 'function') throw new Error("toBSON is not a function");
|
||||
object = object.toBSON();
|
||||
if(object != null && typeof object != 'object') throw new Error("toBSON function did not return an object");
|
||||
}
|
||||
|
||||
// Iterate over all the keys
|
||||
for(var key in object) {
|
||||
var value = object[key];
|
||||
// Is there an override value
|
||||
if(value && value.toBSON) {
|
||||
if(typeof value.toBSON != 'function') throw new Error("toBSON is not a function");
|
||||
value = value.toBSON();
|
||||
}
|
||||
|
||||
// Check the type of the value
|
||||
var type = typeof value;
|
||||
|
||||
// Check the key and throw error if it's illegal
|
||||
if(key != '$db' && key != '$ref' && key != '$id') {
|
||||
if (key.match(regexp) != null) {
|
||||
// The BSON spec doesn't allow keys with null bytes because keys are
|
||||
// null-terminated.
|
||||
throw Error("key " + key + " must not contain null bytes");
|
||||
}
|
||||
|
||||
if (checkKeys) {
|
||||
if('$' == key[0]) {
|
||||
throw Error("key " + key + " must not start with '$'");
|
||||
} else if (!!~key.indexOf('.')) {
|
||||
throw Error("key " + key + " must not contain '.'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(type == 'string') {
|
||||
index = serializeString(buffer, key, value, index);
|
||||
} else if(type == 'number') {
|
||||
index = serializeNumber(buffer, key, value, index);
|
||||
} else if(type == 'boolean') {
|
||||
index = serializeBoolean(buffer, key, value, index);
|
||||
} else if(value instanceof Date || isDate(value)) {
|
||||
index = serializeDate(buffer, key, value, index);
|
||||
} else if(value === undefined && ignoreUndefined == true) {
|
||||
} else if(value === null || value === undefined) {
|
||||
index = serializeUndefined(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'ObjectID') {
|
||||
index = serializeObjectId(buffer, key, value, index);
|
||||
} else if(Buffer.isBuffer(value)) {
|
||||
index = serializeBuffer(buffer, key, value, index);
|
||||
} else if(value instanceof RegExp || isRegExp(value)) {
|
||||
index = serializeRegExp(buffer, key, value, index);
|
||||
} else if(type == 'object' && value['_bsontype'] == null) {
|
||||
index = serializeObject(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(value['_bsontype'] == 'Long' || value['_bsontype'] == 'Timestamp') {
|
||||
index = serializeLong(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Double') {
|
||||
index = serializeDouble(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Code') {
|
||||
index = serializeCode(buffer, key, value, index, checkKeys, depth, serializeFunctions, ignoreUndefined);
|
||||
} else if(typeof value == 'function' && serializeFunctions) {
|
||||
index = serializeFunction(buffer, key, value, index, checkKeys, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'Binary') {
|
||||
index = serializeBinary(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'Symbol') {
|
||||
index = serializeSymbol(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'DBRef') {
|
||||
index = serializeDBRef(buffer, key, value, index, depth, serializeFunctions);
|
||||
} else if(value['_bsontype'] == 'BSONRegExp') {
|
||||
index = serializeBSONRegExp(buffer, key, value, index);
|
||||
} else if(value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') {
|
||||
index = serializeMinMax(buffer, key, value, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Final padding byte for object
|
||||
buffer[index++] = 0x00;
|
||||
|
||||
// Final size
|
||||
var size = index - startingIndex;
|
||||
// Write the size of the object
|
||||
buffer[startingIndex++] = size & 0xff;
|
||||
buffer[startingIndex++] = (size >> 8) & 0xff;
|
||||
buffer[startingIndex++] = (size >> 16) & 0xff;
|
||||
buffer[startingIndex++] = (size >> 24) & 0xff;
|
||||
return index;
|
||||
}
|
||||
|
||||
var BSON = {};
|
||||
|
||||
/**
|
||||
* Contains the function cache if we have that enable to allow for avoiding the eval step on each deserialization, comparison is by md5
|
||||
*
|
||||
* @ignore
|
||||
* @api private
|
||||
*/
|
||||
var functionCache = BSON.functionCache = {};
|
||||
|
||||
/**
|
||||
* Number BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_NUMBER
|
||||
**/
|
||||
BSON.BSON_DATA_NUMBER = 1;
|
||||
/**
|
||||
* String BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_STRING
|
||||
**/
|
||||
BSON.BSON_DATA_STRING = 2;
|
||||
/**
|
||||
* Object BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_OBJECT
|
||||
**/
|
||||
BSON.BSON_DATA_OBJECT = 3;
|
||||
/**
|
||||
* Array BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_ARRAY
|
||||
**/
|
||||
BSON.BSON_DATA_ARRAY = 4;
|
||||
/**
|
||||
* Binary BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_BINARY
|
||||
**/
|
||||
BSON.BSON_DATA_BINARY = 5;
|
||||
/**
|
||||
* ObjectID BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_OID
|
||||
**/
|
||||
BSON.BSON_DATA_OID = 7;
|
||||
/**
|
||||
* Boolean BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_BOOLEAN
|
||||
**/
|
||||
BSON.BSON_DATA_BOOLEAN = 8;
|
||||
/**
|
||||
* Date BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_DATE
|
||||
**/
|
||||
BSON.BSON_DATA_DATE = 9;
|
||||
/**
|
||||
* null BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_NULL
|
||||
**/
|
||||
BSON.BSON_DATA_NULL = 10;
|
||||
/**
|
||||
* RegExp BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_REGEXP
|
||||
**/
|
||||
BSON.BSON_DATA_REGEXP = 11;
|
||||
/**
|
||||
* Code BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_CODE
|
||||
**/
|
||||
BSON.BSON_DATA_CODE = 13;
|
||||
/**
|
||||
* Symbol BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_SYMBOL
|
||||
**/
|
||||
BSON.BSON_DATA_SYMBOL = 14;
|
||||
/**
|
||||
* Code with Scope BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_CODE_W_SCOPE
|
||||
**/
|
||||
BSON.BSON_DATA_CODE_W_SCOPE = 15;
|
||||
/**
|
||||
* 32 bit Integer BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_INT
|
||||
**/
|
||||
BSON.BSON_DATA_INT = 16;
|
||||
/**
|
||||
* Timestamp BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_TIMESTAMP
|
||||
**/
|
||||
BSON.BSON_DATA_TIMESTAMP = 17;
|
||||
/**
|
||||
* Long BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_LONG
|
||||
**/
|
||||
BSON.BSON_DATA_LONG = 18;
|
||||
/**
|
||||
* MinKey BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_MIN_KEY
|
||||
**/
|
||||
BSON.BSON_DATA_MIN_KEY = 0xff;
|
||||
/**
|
||||
* MaxKey BSON Type
|
||||
*
|
||||
* @classconstant BSON_DATA_MAX_KEY
|
||||
**/
|
||||
BSON.BSON_DATA_MAX_KEY = 0x7f;
|
||||
|
||||
/**
|
||||
* Binary Default Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_DEFAULT
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
|
||||
/**
|
||||
* Binary Function Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_FUNCTION
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
|
||||
/**
|
||||
* Binary Byte Array Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
|
||||
/**
|
||||
* Binary UUID Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_UUID
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_UUID = 3;
|
||||
/**
|
||||
* Binary MD5 Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_MD5
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
|
||||
/**
|
||||
* Binary User Defined Type
|
||||
*
|
||||
* @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
|
||||
**/
|
||||
BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
|
||||
|
||||
// BSON MAX VALUES
|
||||
BSON.BSON_INT32_MAX = 0x7FFFFFFF;
|
||||
BSON.BSON_INT32_MIN = -0x80000000;
|
||||
|
||||
BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
|
||||
BSON.BSON_INT64_MIN = -Math.pow(2, 63);
|
||||
|
||||
// JS MAX PRECISE VALUES
|
||||
BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
|
||||
BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
|
||||
|
||||
// Internal long versions
|
||||
var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
|
||||
var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.
|
||||
|
||||
module.exports = serializeInto;
|
||||
Reference in New Issue
Block a user