Skip to content

fix(NODE-3451): fix performance regression from v1 #451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
],
"types": "bson.d.ts",
"version": "4.4.1",
"author": {
"name": "The MongoDB NodeJS Team",
"author": {
"name": "The MongoDB NodeJS Team",
"email": "[email protected]"
},
"license": "Apache-2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/bson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export function deserialize(
buffer: Buffer | ArrayBufferView | ArrayBuffer,
options: DeserializeOptions = {}
): Document {
return internalDeserialize(ensureBuffer(buffer), options);
return internalDeserialize(buffer instanceof Buffer ? buffer : ensureBuffer(buffer), options);
}

/** @public */
Expand Down
31 changes: 18 additions & 13 deletions src/parser/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export function deserialize(
return deserializeObject(buffer, index, options, isArray);
}

const allowedDBRefKeys = /^\$ref$|^\$id$|^\$db$/;

function deserializeObject(
buffer: Buffer,
index: number,
Expand Down Expand Up @@ -134,6 +136,8 @@ function deserializeObject(
let arrayIndex = 0;
const done = false;

let isPossibleDBRef = isArray ? false : null;

// While we have more left data left keep parsing
while (!done) {
// Read the type
Expand All @@ -152,6 +156,9 @@ function deserializeObject(
// If are at the end of the buffer there is a problem with the document
if (i >= buffer.byteLength) throw new Error('Bad BSON Document: illegal CString');
const name = isArray ? arrayIndex++ : buffer.toString('utf8', index, i);
if (isPossibleDBRef !== false && (name as string)[0] === '$') {
isPossibleDBRef = allowedDBRefKeys.test(name as string);
}
let value;

index = i + 1;
Expand All @@ -169,12 +176,17 @@ function deserializeObject(
)
throw new Error('bad string length in bson');

if (!validateUtf8(buffer, index, index + stringSize - 1)) {
throw new Error('Invalid UTF-8 string in BSON document');
}

value = buffer.toString('utf8', index, index + stringSize - 1);

for (let i = 0; i < value.length; i++) {
if (value.charCodeAt(i) === 0xfffd) {
if (!validateUtf8(buffer, index, index + stringSize - 1)) {
throw new Error('Invalid UTF-8 string in BSON document');
}
break;
}
}

index = index + stringSize;
} else if (elementType === constants.BSON_DATA_OID) {
const oid = Buffer.alloc(12);
Expand Down Expand Up @@ -625,15 +637,8 @@ function deserializeObject(
throw new Error('corrupt object bson');
}

// check if object's $ keys are those of a DBRef
const dollarKeys = Object.keys(object).filter(k => k.startsWith('$'));
let valid = true;
dollarKeys.forEach(k => {
if (['$ref', '$id', '$db'].indexOf(k) === -1) valid = false;
});

// if a $key not in "$ref", "$id", "$db", don't make a DBRef
if (!valid) return object;
// if we did not find "$ref", "$id", "$db", or found an extraneous $key, don't make a DBRef
if (!isPossibleDBRef) return object;

if (isDBRefLike(object)) {
const copy = Object.assign({}, object) as Partial<DBRefLike>;
Expand Down