BVB Source Codes

mongo-express Show bson.js Source code

Return Download mongo-express: download bson.js Source code - Download mongo-express Source code - Type:.js
  1. 'use strict';
  2.  
  3. var mongodb = require('mongodb');
  4. var vm      = require('vm');
  5. var json    = require('./json');
  6.  
  7. //Adaptors for BSON types
  8.  
  9. var DBRef = function (namespace, oid, db) {
  10.   //Allow empty/undefined db value
  11.   if (db === undefined || db === null) {
  12.     db = '';
  13.   }
  14.  
  15.   var objectId = exports.toObjectId(oid);
  16.   if (objectId === false) {
  17.     objectId = mongodb.ObjectId(oid);
  18.   }
  19.   return mongodb.DBRef(namespace, objectId, db);
  20. };
  21.  
  22. var Timestamp = function (high, low) {
  23.   //Switch low/high bits to Timestamp constructor
  24.   return mongodb.Timestamp(low, high);
  25. };
  26.  
  27. //Create sandbox with BSON data types
  28. exports.getSandbox = function () {
  29.   return {
  30.     Long: mongodb.Long,
  31.     NumberLong: mongodb.Long,
  32.     Double: mongodb.Double,
  33.     NumberDouble: mongodb.Double,
  34.     ObjectId: mongodb.ObjectID,
  35.     ObjectID: mongodb.ObjectID,
  36.     Timestamp: Timestamp,
  37.     DBRef: DBRef,
  38.     Dbref: DBRef,
  39.     Binary: mongodb.Binary,
  40.     BinData: mongodb.Binary,
  41.     Code: mongodb.Code,
  42.     Symbol: mongodb.Symbol,
  43.     MinKey: mongodb.MinKey,
  44.     MaxKey: mongodb.MaxKey,
  45.     ISODate: Date,
  46.     Date: Date,
  47.     Buffer: Buffer,
  48.   };
  49. };
  50.  
  51. //JSON.parse doesn't support BSON data types
  52. //Document is evaluated in a vm in order to support BSON data types
  53. //Sandbox contains BSON data type functions from node-mongodb-native
  54. exports.toBSON = function (string) {
  55.   var sandbox = exports.getSandbox();
  56.  
  57.   string = string.replace(/ISODate\(/g, 'new ISODate(');
  58.   string = string.replace(/Binary\(("[^"]+"),/g, 'Binary(new Buffer($1, "base64"),');
  59.  
  60.   vm.runInNewContext('doc = eval((' + string + '));', sandbox);
  61.  
  62.   return sandbox.doc;
  63. };
  64.  
  65. // This function as the name suggests attempts to parse
  66. // the free form string in to BSON, since the possibilities of failure
  67. // are higher, this function uses a try..catch
  68. exports.toSafeBSON = function (string) {
  69.   try {
  70.     var bsonObject = exports.toBSON(string);
  71.     return bsonObject;
  72.   } catch (err) {
  73.     return null;
  74.   }
  75. };
  76.  
  77. // Converts string to ObjectID.
  78. exports.toObjectId = function (string) {
  79.   var sandbox = exports.getSandbox();
  80.  
  81.   // Validation
  82.   if (
  83.     !string ||                                            // No input at all
  84.     string === '' ||                                      // empty string
  85.     string.toUpperCase().indexOf('OBJECTID(') === -1 ||   // missing the opening 'ObjectID('
  86.     string.indexOf(')') === -1                            // missing the closing '('
  87.   ) {
  88.     return false;
  89.   }
  90.  
  91.   // Strip quotes
  92.   string = string.replace('"', '').replace('"', '');
  93.  
  94.   // Convert ObjectId("526ddf5a9f610ffd26000001") to 526ddf5a9f610ffd26000001
  95.   string = string.replace(/ObjectID\(/i, '').replace(')', '');
  96.  
  97.   // Make sure it's a 24-character string to prevent errors.
  98.   if (string.length === 24) {
  99.     return sandbox.ObjectID(string);
  100.   } else {
  101.     return false;
  102.   }
  103. };
  104.  
  105. // Convert 24-digit hex string to ObjectId
  106. exports.hexToObjectId = function (string) {
  107.   if (!/^[0-9a-f]{24}$/.test(string)) {
  108.     return false;
  109.   }
  110.   var sandbox = exports.getSandbox();
  111.   return sandbox.ObjectID(string);
  112. };
  113.  
  114. //Convert BSON documents to string
  115. exports.toString = function (doc) {
  116.   //Use custom json stringify function from json.js
  117.   return json.stringify(doc, null, '    ');
  118. };
  119.  
  120. exports.toJsonString = function (doc) {
  121.   let sJson = json.stringify(doc, null);
  122.   sJson = sJson.replace(/ObjectID\(([^)]+)\)/g, '{ "$oid": $1 }');
  123.   sJson = sJson.replace(/DBRef\(([^)]+)\)/g, '{ "$ref": $1 }');
  124.   sJson = sJson.replace(/ISODate\(([^)]+)\)/g, '$1');
  125.   return sJson;
  126. };
  127.  
downloadbson.js Source code - Download mongo-express Source code
Related Source Codes/Software:
XCL-Charts - Android charting libraries (XCL-Charts is a free c... 2017-05-17
scrollMonitor - A simple and fast API to monitor elements as you s... 2017-05-16
XCDYouTubeKit - YouTube video player for iOS, tvOS and macOS 2017-05-16
rails-erd - Generate Entity-Relationship Diagrams for Rails ap... 2017-05-16
np - A better `npm publish` 2017-05-16
kaminari - A Scope & Engine based, clean, powerful, custo... 2017-06-04
python-fire - Python Fire is a library for automatically generat... 2017-06-04
Dapper - Dapper - a simple object mapper for .Net 2017-06-05
pkg - Package your Node.js project into an executable ... 2017-06-05
packer - Packer is a tool for creating identical machine im... 2017-06-05
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top