Comparing version 0.4.20 to 0.4.21
@@ -20,3 +20,3 @@ try { | ||
, './long'].forEach(function (path) { | ||
var module = require('./' + path); | ||
var module = require(path); | ||
for (var i in module) { | ||
@@ -44,4 +44,4 @@ exports[i] = module[i]; | ||
, './long' | ||
, '././bson'].forEach(function (path) { | ||
var module = require('./' + path); | ||
, './bson'].forEach(function (path) { | ||
var module = require(path); | ||
for (var i in module) { | ||
@@ -73,3 +73,3 @@ classes[i] = module[i]; | ||
].forEach(function (path) { | ||
var module = require('./' + path); | ||
var module = require(path); | ||
for (var i in module) { | ||
@@ -76,0 +76,0 @@ classes[i] = module[i]; |
"use strict" | ||
var writeIEEE754 = require('../float_parser').writeIEEE754, | ||
readIEEE754 = require('../float_parser').readIEEE754, | ||
var readIEEE754 = require('../float_parser').readIEEE754, | ||
f = require('util').format, | ||
@@ -19,3 +18,4 @@ Long = require('../long').Long, | ||
var deserialize = function(buffer, options, isArray) { | ||
var index = 0; | ||
options = options == null ? {} : options; | ||
var index = options && options.index ? options.index : 0; | ||
// Read the document size | ||
@@ -35,31 +35,12 @@ var size = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; | ||
// Start deserializtion | ||
return deserializeObject(buffer, options, isArray); | ||
return deserializeObject(buffer, index - 4, 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 deserializeObject = function(buffer, index, options, isArray) { | ||
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']; | ||
var fieldsAsRaw = options['fieldsAsRaw'] == null ? null : options['fieldsAsRaw']; | ||
var raw = options['raw'] == null ? false : options['raw']; | ||
// Return BSONRegExp objects instead of native regular expressions | ||
@@ -72,5 +53,2 @@ var bsonRegExp = typeof options['bsonRegExp'] == 'boolean' ? options['bsonRegExp'] : false; | ||
// Set up index | ||
var index = typeof options['index'] == 'number' ? options['index'] : 0; | ||
// Read the document size | ||
@@ -91,47 +69,78 @@ var size = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24; | ||
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 | ||
// 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") | ||
var name = buffer.toString('utf8', index, i); | ||
index = i + 1; | ||
if(elementType == BSON.BSON_DATA_STRING) { | ||
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_OID) { | ||
var string = buffer.toString('binary', index, index + 12); | ||
object[name] = new ObjectID(string); | ||
index = index + 12; | ||
} 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 | ||
object[name] = buffer.readDoubleLE(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_OBJECT) { | ||
var _index = index; | ||
var objectSize = buffer[index] | buffer[index + 1] << 8 | buffer[index + 2] << 16 | buffer[index + 3] << 24; | ||
if(objectSize <= 0 || objectSize > (buffer.length - index)) throw new Error("bad embedded document length in bson"); | ||
// We have a raw value | ||
if(raw) { | ||
object[name] = buffer.slice(index, index + objectSize); | ||
} else { | ||
object[name] = deserializeObject(buffer, _index, options, false); | ||
} | ||
index = index + objectSize; | ||
} else if(elementType == BSON.BSON_DATA_ARRAY) { | ||
var _index = index; | ||
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 && fieldsAsRaw[name]) { | ||
arrayOptions = {}; | ||
for(var n in options) arrayOptions[n] = options[n]; | ||
arrayOptions['raw'] = true; | ||
} | ||
object[name] = deserializeObject(buffer, _index, arrayOptions, true); | ||
index = index + objectSize; | ||
} 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_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; | ||
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_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++]; | ||
@@ -145,6 +154,4 @@ // Decode as raw Buffer object if options specifies it | ||
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); | ||
@@ -163,6 +170,4 @@ } | ||
if(promoteBuffers) { | ||
// assign reference to Buffer object | ||
object[name] = _buffer; | ||
} else { | ||
// Create the binary object | ||
object[name] = new Binary(_buffer, subType); | ||
@@ -173,47 +178,28 @@ } | ||
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; | ||
} else if(elementType == BSON.BSON_DATA_REGEXP && bsonRegExp == false) { | ||
// 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") | ||
// Return the C string | ||
var source = buffer.toString('utf8', index, i); | ||
// Create the regexp | ||
index = i + 1; | ||
// 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); | ||
// 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") | ||
// Return the C string | ||
var regExpOptions = buffer.toString('utf8', index, i); | ||
index = i + 1; | ||
// 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 | ||
@@ -239,52 +225,44 @@ var optionsArray = new Array(regExpOptions.length); | ||
} else if(elementType == BSON.BSON_DATA_REGEXP && bsonRegExp == true) { | ||
// Create the regexp | ||
var r = readCStyleStringSpecial(buffer, index); | ||
var source = r.s; | ||
index = r.i; | ||
// 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") | ||
// Return the C string | ||
var source = buffer.toString('utf8', index, i); | ||
index = i + 1; | ||
var r = readCStyleStringSpecial(buffer, index); | ||
var regExpOptions = r.s; | ||
index = r.i; | ||
// 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") | ||
// Return the C string | ||
var regExpOptions = buffer.toString('utf8', index, i); | ||
index = i + 1; | ||
// 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; | ||
} | ||
object[name] = new BSONRegExp(source, regExpOptions); | ||
} 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); | ||
@@ -294,3 +272,2 @@ | ||
if(evalFunctions) { | ||
// Contains the value we are going to set | ||
var value = null; | ||
@@ -303,3 +280,2 @@ // If we have cache enabled let's look for the md5 of the function in the cache | ||
} else { | ||
// Set directly | ||
object[name] = isolateEval(functionString); | ||
@@ -314,6 +290,4 @@ } | ||
} 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"); | ||
@@ -325,7 +299,7 @@ // Javascript function | ||
// Parse the element | ||
options['index'] = index; | ||
var _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); | ||
var scopeObject = deserializeObject(buffer, _index, options, false); | ||
// Adjust the index | ||
@@ -344,7 +318,5 @@ index = index + objectSize; | ||
} else { | ||
// Set directly | ||
object[name] = isolateEval(functionString); | ||
} | ||
// Set the scope on the object | ||
object[name].scope = scopeObject; | ||
@@ -359,4 +331,2 @@ } else { | ||
if(object['$id'] != null) object = new DBRef(object['$ref'], object['$id'], object['$db']); | ||
// Return the final objects | ||
return object; | ||
@@ -363,0 +333,0 @@ } |
@@ -288,3 +288,3 @@ "use strict" | ||
// Write the type | ||
buffer[index++] = value instanceof Long ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP; | ||
buffer[index++] = value._bsontype == 'Long' ? BSON.BSON_DATA_LONG : BSON.BSON_DATA_TIMESTAMP; | ||
// Number of written bytes | ||
@@ -568,3 +568,3 @@ var numberOfWrittenBytes = buffer.write(key, index, 'utf8'); | ||
} else if(value['_bsontype'] == 'BSONRegExp') { | ||
index = serializeBSONRegExp(buffer, key, value, index); | ||
index = serializeBSONRegExp(buffer, key, value, index); | ||
} else if(value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') { | ||
@@ -608,3 +608,3 @@ index = serializeMinMax(buffer, key, value, index); | ||
} | ||
// console.log("---------------------------------------------------") | ||
@@ -648,3 +648,3 @@ // console.dir("key = " + key) | ||
} else if(value['_bsontype'] == 'BSONRegExp') { | ||
index = serializeBSONRegExp(buffer, key, value, index); | ||
index = serializeBSONRegExp(buffer, key, value, index); | ||
} else if(value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') { | ||
@@ -725,3 +725,3 @@ index = serializeMinMax(buffer, key, value, index); | ||
} else if(value['_bsontype'] == 'BSONRegExp') { | ||
index = serializeBSONRegExp(buffer, key, value, index); | ||
index = serializeBSONRegExp(buffer, key, value, index); | ||
} else if(value['_bsontype'] == 'MinKey' || value['_bsontype'] == 'MaxKey') { | ||
@@ -728,0 +728,0 @@ index = serializeMinMax(buffer, key, value, index); |
{ "name" : "bson" | ||
, "description" : "A bson parser for node.js and the browser" | ||
, "keywords" : ["mongodb", "bson", "parser"] | ||
, "version" : "0.4.20" | ||
, "version" : "0.4.21" | ||
, "author" : "Christian Amor Kvalheim <christkv@gmail.com>" | ||
@@ -6,0 +6,0 @@ , "contributors" : [] |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
434730
30
11345
8