Skip to content
Permalink
Browse files

Change bson parsing to use mongo-query-parser

  • Loading branch information
dozoisch committed Nov 11, 2019
1 parent ea02b36 commit d8c9bda46a204ecba1d35558452685cd0674e6f2
Showing with 123 additions and 477 deletions.
  1. +10 −101 lib/bson.js
  2. +0 −271 lib/json.js
  3. +1 −21 lib/routes/collection.js
  4. +6 −1 package.json
  5. +19 −0 patches/mongodb-query-parser+1.4.3.patch
  6. +87 −83 test/lib/bsonSpec.js
@@ -1,65 +1,11 @@
'use strict';

var mongodb = require('mongodb');
var vm = require('vm');
var json = require('./json');
const parser = require('mongodb-query-parser');
const ESJON = require('mongodb-extended-json');
const mongodb = require('mongodb');

//Adaptors for BSON types

var DBRef = function (namespace, oid, db) {
//Allow empty/undefined db value
if (db === undefined || db === null) {
db = '';
}

var objectId = exports.toObjectId(oid);
if (objectId === false) {
objectId = mongodb.ObjectId(oid);
}
return mongodb.DBRef(namespace, objectId, db);
};

var Timestamp = function (high, low) {
//Switch low/high bits to Timestamp constructor
return mongodb.Timestamp(low, high);
};

//Create sandbox with BSON data types
exports.getSandbox = function () {
return {
Long: mongodb.Long,
NumberLong: mongodb.Long,
Double: mongodb.Double,
NumberDouble: mongodb.Double,
ObjectId: mongodb.ObjectID,
ObjectID: mongodb.ObjectID,
Timestamp: Timestamp,
DBRef: DBRef,
Dbref: DBRef,
Binary: mongodb.Binary,
BinData: mongodb.Binary,
Code: mongodb.Code,
Symbol: mongodb.Symbol,
MinKey: mongodb.MinKey,
MaxKey: mongodb.MaxKey,
ISODate: Date,
Date: Date,
Buffer: Buffer,
};
};

//JSON.parse doesn't support BSON data types
//Document is evaluated in a vm in order to support BSON data types
//Sandbox contains BSON data type functions from node-mongodb-native
exports.toBSON = function (string) {
var sandbox = exports.getSandbox();

string = string.replace(/ISODate\(/g, 'new ISODate(');
string = string.replace(/Binary\(("[^"]+"),/g, 'Binary(new Buffer($1, "base64"),');

vm.runInNewContext('doc = eval((' + string + '));', sandbox);

return sandbox.doc;
return parser(string);
};

// This function as the name suggests attempts to parse
@@ -74,55 +20,18 @@ exports.toSafeBSON = function (string) {
}
};

// Converts string to ObjectID.
exports.toObjectId = function (string) {
var sandbox = exports.getSandbox();

// Validation
if (
!string || // No input at all
string === '' || // empty string
string.toUpperCase().indexOf('OBJECTID(') === -1 || // missing the opening 'ObjectID('
string.indexOf(')') === -1 // missing the closing '('
) {
return false;
}

// Strip quotes
string = string.replace('"', '').replace('"', '');

// Convert ObjectId("526ddf5a9f610ffd26000001") to 526ddf5a9f610ffd26000001
string = string.replace(/ObjectID\(/i, '').replace(')', '');

// Make sure it's a 24-character string to prevent errors.
if (string.length === 24) {
return sandbox.ObjectID(string);
} else {
return false;
}
};

// Convert 24-digit hex string to ObjectId
exports.hexToObjectId = function (string) {
if (!/^[0-9a-f]{24}$/.test(string)) {
return false;
exports.parseObjectId = function (string) {
if (/^[0-9a-f]{24}$/i.test(string)) {
return mongodb.ObjectId(string);
}
var sandbox = exports.getSandbox();
return sandbox.ObjectID(string);
return exports.toBSON(string);
};

//Convert BSON documents to string
exports.toString = function (doc) {
//Use custom json stringify function from json.js
return json.stringify(doc, null, ' ');
return parser.toJSString(doc, ' ');
};

exports.toJsonString = function (doc) {
let sJson = json.stringify(doc, null);
sJson = sJson.replace(/ObjectID\(([^)]+)\)/g, '{ "$oid": $1 }');
sJson = sJson.replace(/([-])?Infinity/g, '"$1Infinity"');
sJson = sJson.replace(/NaN/g, '"NaN"');
sJson = sJson.replace(/DBRef\(([^)]+)\)/g, '{ "$ref": $1 }');
sJson = sJson.replace(/ISODate\(([^)]+)\)/g, '$1');
return sJson;
return ESJON.stringify(doc);
};

0 comments on commit d8c9bda

Please sign in to comment.
You can’t perform that action at this time.