From bb6b0365d9da07ba67619683f2b0275b62bc05e3 Mon Sep 17 00:00:00 2001 From: ali Date: Thu, 7 Jun 2018 19:50:11 +0430 Subject: [PATCH 1/2] Target added to deserialize --- lib/bson/parser/deserializer.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/bson/parser/deserializer.js b/lib/bson/parser/deserializer.js index b7f45d76..35cf1200 100644 --- a/lib/bson/parser/deserializer.js +++ b/lib/bson/parser/deserializer.js @@ -38,7 +38,7 @@ var deserialize = function(buffer, options, isArray) { return deserializeObject(buffer, index, options, isArray); }; -var deserializeObject = function(buffer, index, options, isArray) { +var deserializeObject = function(buffer, index, options, isArray, target) { var evalFunctions = options['evalFunctions'] == null ? false : options['evalFunctions']; var cacheFunctions = options['cacheFunctions'] == null ? false : options['cacheFunctions']; var cacheFunctionsCrc32 = @@ -73,12 +73,14 @@ var deserializeObject = function(buffer, index, options, isArray) { if (size < 5 || size > buffer.length) throw new Error('corrupt bson message'); // Create holding object - var object = isArray ? [] : {}; + var object = (target instanceof Function) ? new target() : (isArray ? [] : {}); // Used for arrays to skip having to perform utf8 decoding var arrayIndex = 0; var done = false; + if (isArray && target) target = target['$']; + // While we have more left data left keep parsing // while (buffer[index + 1] !== 0) { while (!done) { @@ -98,6 +100,9 @@ var deserializeObject = function(buffer, index, options, isArray) { if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString'); var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i); + if (name === 'firstBatch' || name === 'nextBatch') target = {$: options.target}; + else if (!isArray && target) target = target['$' + name]; + index = i + 1; if (elementType === BSON.BSON_DATA_STRING) { @@ -164,7 +169,7 @@ var deserializeObject = function(buffer, index, options, isArray) { if (raw) { object[name] = buffer.slice(index, index + objectSize); } else { - object[name] = deserializeObject(buffer, _index, options, false); + object[name] = deserializeObject(buffer, _index, options, false, target); } index = index + objectSize; @@ -187,7 +192,7 @@ var deserializeObject = function(buffer, index, options, isArray) { arrayOptions['raw'] = true; } - object[name] = deserializeObject(buffer, _index, arrayOptions, true); + object[name] = deserializeObject(buffer, _index, arrayOptions, true, target); index = index + objectSize; if (buffer[index - 1] !== 0) throw new Error('invalid array terminator byte'); From da6562c43a6c41ad78f37682e1d036236825eb1f Mon Sep 17 00:00:00 2001 From: ali Date: Thu, 7 Jun 2018 20:12:13 +0430 Subject: [PATCH 2/2] inner target changed to newTarget --- lib/bson/parser/deserializer.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/bson/parser/deserializer.js b/lib/bson/parser/deserializer.js index 35cf1200..92e48a48 100644 --- a/lib/bson/parser/deserializer.js +++ b/lib/bson/parser/deserializer.js @@ -78,8 +78,9 @@ var deserializeObject = function(buffer, index, options, isArray, target) { var arrayIndex = 0; var done = false; + var newTarget = null; - if (isArray && target) target = target['$']; + if (isArray && target) newTarget = target['$']; // While we have more left data left keep parsing // while (buffer[index + 1] !== 0) { @@ -100,8 +101,8 @@ var deserializeObject = function(buffer, index, options, isArray, target) { if (i >= buffer.length) throw new Error('Bad BSON Document: illegal CString'); var name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i); - if (name === 'firstBatch' || name === 'nextBatch') target = {$: options.target}; - else if (!isArray && target) target = target['$' + name]; + if (name === 'firstBatch' || name === 'nextBatch') newTarget = {$: options.target}; + else if (!isArray && target) newTarget = target['$' + name]; index = i + 1; @@ -169,7 +170,7 @@ var deserializeObject = function(buffer, index, options, isArray, target) { if (raw) { object[name] = buffer.slice(index, index + objectSize); } else { - object[name] = deserializeObject(buffer, _index, options, false, target); + object[name] = deserializeObject(buffer, _index, options, false, newTarget); } index = index + objectSize; @@ -192,7 +193,7 @@ var deserializeObject = function(buffer, index, options, isArray, target) { arrayOptions['raw'] = true; } - object[name] = deserializeObject(buffer, _index, arrayOptions, true, target); + object[name] = deserializeObject(buffer, _index, arrayOptions, true, newTarget); index = index + objectSize; if (buffer[index - 1] !== 0) throw new Error('invalid array terminator byte');