Skip to content

Commit fc5f405

Browse files
committed
Added DBPointer upconversion to DBRef
1 parent d09c909 commit fc5f405

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lib/bson/parser/deserializer.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,30 @@ var deserializeObject = function(buffer, index, options, isArray) {
388388
} else {
389389
object[name] = new Code(functionString, scopeObject);
390390
}
391+
} else if(elementType == BSON_DATA_DBPOINTER) {
392+
// Get the code string size
393+
var stringSize = buffer[index++] | buffer[index++] << 8 | buffer[index++] << 16 | buffer[index++] << 24;
394+
// Check if we have a valid string
395+
if(stringSize <= 0 || stringSize > (buffer.length - index) || buffer[index + stringSize - 1] != 0) throw new Error("bad string length in bson");
396+
// Namespace
397+
var namespace = buffer.toString('utf8', index, index + stringSize - 1);
398+
// Update parse index position
399+
index = index + stringSize;
400+
401+
// Read the oid
402+
var oidBuffer = new Buffer(12);
403+
buffer.copy(oidBuffer, 0, index, index + 12);
404+
var oid = new ObjectID(oidBuffer);
405+
406+
// Update the index
407+
index = index + 12;
408+
409+
// Split the namespace
410+
var parts = namespace.split('.');
411+
var db = parts.shift();
412+
var collection = parts.join('.');
413+
// Upgrade to DBRef type
414+
object[name] = new DBRef(collection, oid, db);
391415
} else {
392416
throw new Error("Detected unknown BSON type " + elementType.toString(16) + " for fieldname \"" + name + "\", are you using the latest BSON parser");
393417
}
@@ -513,6 +537,12 @@ BSON.BSON_DATA_NULL = 10;
513537
* @classconstant BSON_DATA_REGEXP
514538
**/
515539
BSON.BSON_DATA_REGEXP = 11;
540+
/**
541+
* Code BSON Type
542+
*
543+
* @classconstant BSON_DATA_DBPOINTER
544+
**/
545+
BSON.BSON_DATA_DBPOINTER = 12;
516546
/**
517547
* Code BSON Type
518548
*

0 commit comments

Comments
 (0)