Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bson.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. 'use strict';
  2. var Map = require('./map'),
  3. Long = require('./long'),
  4. Double = require('./double'),
  5. Timestamp = require('./timestamp'),
  6. ObjectID = require('./objectid'),
  7. BSONRegExp = require('./regexp'),
  8. Symbol = require('./symbol'),
  9. Int32 = require('./int_32'),
  10. Code = require('./code'),
  11. Decimal128 = require('./decimal128'),
  12. MinKey = require('./min_key'),
  13. MaxKey = require('./max_key'),
  14. DBRef = require('./db_ref'),
  15. Binary = require('./binary');
  16. // Parts of the parser
  17. var deserialize = require('./parser/deserializer'),
  18. serializer = require('./parser/serializer'),
  19. calculateObjectSize = require('./parser/calculate_size');
  20. /**
  21. * @ignore
  22. * @api private
  23. */
  24. // Default Max Size
  25. var MAXSIZE = 1024 * 1024 * 17;
  26. // Current Internal Temporary Serialization Buffer
  27. var buffer = new Buffer(MAXSIZE);
  28. var BSON = function() {};
  29. /**
  30. * Serialize a Javascript object.
  31. *
  32. * @param {Object} object the Javascript object to serialize.
  33. * @param {Boolean} [options.checkKeys] the serializer will check if keys are valid.
  34. * @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**.
  35. * @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**.
  36. * @param {Number} [options.minInternalBufferSize=1024*1024*17] minimum size of the internal temporary serialization buffer **(default:1024*1024*17)**.
  37. * @return {Buffer} returns the Buffer object containing the serialized object.
  38. * @api public
  39. */
  40. BSON.prototype.serialize = function serialize(object, options) {
  41. options = options || {};
  42. // Unpack the options
  43. var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
  44. var serializeFunctions =
  45. typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
  46. var ignoreUndefined =
  47. typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
  48. var minInternalBufferSize =
  49. typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;
  50. // Resize the internal serialization buffer if needed
  51. if (buffer.length < minInternalBufferSize) {
  52. buffer = new Buffer(minInternalBufferSize);
  53. }
  54. // Attempt to serialize
  55. var serializationIndex = serializer(
  56. buffer,
  57. object,
  58. checkKeys,
  59. 0,
  60. 0,
  61. serializeFunctions,
  62. ignoreUndefined,
  63. []
  64. );
  65. // Create the final buffer
  66. var finishedBuffer = new Buffer(serializationIndex);
  67. // Copy into the finished buffer
  68. buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
  69. // Return the buffer
  70. return finishedBuffer;
  71. };
  72. /**
  73. * Serialize a Javascript object using a predefined Buffer and index into the buffer, useful when pre-allocating the space for serialization.
  74. *
  75. * @param {Object} object the Javascript object to serialize.
  76. * @param {Buffer} buffer the Buffer you pre-allocated to store the serialized BSON object.
  77. * @param {Boolean} [options.checkKeys] the serializer will check if keys are valid.
  78. * @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**.
  79. * @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**.
  80. * @param {Number} [options.index] the index in the buffer where we wish to start serializing into.
  81. * @return {Number} returns the index pointing to the last written byte in the buffer.
  82. * @api public
  83. */
  84. BSON.prototype.serializeWithBufferAndIndex = function(object, finalBuffer, options) {
  85. options = options || {};
  86. // Unpack the options
  87. var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
  88. var serializeFunctions =
  89. typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
  90. var ignoreUndefined =
  91. typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
  92. var startIndex = typeof options.index === 'number' ? options.index : 0;
  93. // Attempt to serialize
  94. var serializationIndex = serializer(
  95. finalBuffer,
  96. object,
  97. checkKeys,
  98. startIndex || 0,
  99. 0,
  100. serializeFunctions,
  101. ignoreUndefined
  102. );
  103. // Return the index
  104. return serializationIndex - 1;
  105. };
  106. /**
  107. * Deserialize data as BSON.
  108. *
  109. * @param {Buffer} buffer the buffer containing the serialized set of BSON documents.
  110. * @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
  111. * @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
  112. * @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
  113. * @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
  114. * @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a node.js Buffer instance.
  115. * @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
  116. * @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
  117. * @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
  118. * @return {Object} returns the deserialized Javascript Object.
  119. * @api public
  120. */
  121. BSON.prototype.deserialize = function(buffer, options) {
  122. return deserialize(buffer, options);
  123. };
  124. /**
  125. * Calculate the bson size for a passed in Javascript object.
  126. *
  127. * @param {Object} object the Javascript object to calculate the BSON byte size for.
  128. * @param {Boolean} [options.serializeFunctions=false] serialize the javascript functions **(default:false)**.
  129. * @param {Boolean} [options.ignoreUndefined=true] ignore undefined fields **(default:true)**.
  130. * @return {Number} returns the number of bytes the BSON object will take up.
  131. * @api public
  132. */
  133. BSON.prototype.calculateObjectSize = function(object, options) {
  134. options = options || {};
  135. var serializeFunctions =
  136. typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
  137. var ignoreUndefined =
  138. typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
  139. return calculateObjectSize(object, serializeFunctions, ignoreUndefined);
  140. };
  141. /**
  142. * Deserialize stream data as BSON documents.
  143. *
  144. * @param {Buffer} data the buffer containing the serialized set of BSON documents.
  145. * @param {Number} startIndex the start index in the data Buffer where the deserialization is to start.
  146. * @param {Number} numberOfDocuments number of documents to deserialize.
  147. * @param {Array} documents an array where to store the deserialized documents.
  148. * @param {Number} docStartIndex the index in the documents array from where to start inserting documents.
  149. * @param {Object} [options] additional options used for the deserialization.
  150. * @param {Object} [options.evalFunctions=false] evaluate functions in the BSON document scoped to the object deserialized.
  151. * @param {Object} [options.cacheFunctions=false] cache evaluated functions for reuse.
  152. * @param {Object} [options.cacheFunctionsCrc32=false] use a crc32 code for caching, otherwise use the string of the function.
  153. * @param {Object} [options.promoteLongs=true] when deserializing a Long will fit it into a Number if it's smaller than 53 bits
  154. * @param {Object} [options.promoteBuffers=false] when deserializing a Binary will return it as a node.js Buffer instance.
  155. * @param {Object} [options.promoteValues=false] when deserializing will promote BSON values to their Node.js closest equivalent types.
  156. * @param {Object} [options.fieldsAsRaw=null] allow to specify if there what fields we wish to return as unserialized raw buffer.
  157. * @param {Object} [options.bsonRegExp=false] return BSON regular expressions as BSONRegExp instances.
  158. * @return {Number} returns the next index in the buffer after deserialization **x** numbers of documents.
  159. * @api public
  160. */
  161. BSON.prototype.deserializeStream = function(
  162. data,
  163. startIndex,
  164. numberOfDocuments,
  165. documents,
  166. docStartIndex,
  167. options
  168. ) {
  169. options = options != null ? options : {};
  170. var index = startIndex;
  171. // Loop over all documents
  172. for (var i = 0; i < numberOfDocuments; i++) {
  173. // Find size of the document
  174. var size =
  175. data[index] | (data[index + 1] << 8) | (data[index + 2] << 16) | (data[index + 3] << 24);
  176. // Update options with index
  177. options['index'] = index;
  178. // Parse the document at this point
  179. documents[docStartIndex + i] = this.deserialize(data, options);
  180. // Adjust index by the document size
  181. index = index + size;
  182. }
  183. // Return object containing end index of parsing and list of documents
  184. return index;
  185. };
  186. /**
  187. * @ignore
  188. * @api private
  189. */
  190. // BSON MAX VALUES
  191. BSON.BSON_INT32_MAX = 0x7fffffff;
  192. BSON.BSON_INT32_MIN = -0x80000000;
  193. BSON.BSON_INT64_MAX = Math.pow(2, 63) - 1;
  194. BSON.BSON_INT64_MIN = -Math.pow(2, 63);
  195. // JS MAX PRECISE VALUES
  196. BSON.JS_INT_MAX = 0x20000000000000; // Any integer up to 2^53 can be precisely represented by a double.
  197. BSON.JS_INT_MIN = -0x20000000000000; // Any integer down to -2^53 can be precisely represented by a double.
  198. // Internal long versions
  199. // var JS_INT_MAX_LONG = Long.fromNumber(0x20000000000000); // Any integer up to 2^53 can be precisely represented by a double.
  200. // var JS_INT_MIN_LONG = Long.fromNumber(-0x20000000000000); // Any integer down to -2^53 can be precisely represented by a double.
  201. /**
  202. * Number BSON Type
  203. *
  204. * @classconstant BSON_DATA_NUMBER
  205. **/
  206. BSON.BSON_DATA_NUMBER = 1;
  207. /**
  208. * String BSON Type
  209. *
  210. * @classconstant BSON_DATA_STRING
  211. **/
  212. BSON.BSON_DATA_STRING = 2;
  213. /**
  214. * Object BSON Type
  215. *
  216. * @classconstant BSON_DATA_OBJECT
  217. **/
  218. BSON.BSON_DATA_OBJECT = 3;
  219. /**
  220. * Array BSON Type
  221. *
  222. * @classconstant BSON_DATA_ARRAY
  223. **/
  224. BSON.BSON_DATA_ARRAY = 4;
  225. /**
  226. * Binary BSON Type
  227. *
  228. * @classconstant BSON_DATA_BINARY
  229. **/
  230. BSON.BSON_DATA_BINARY = 5;
  231. /**
  232. * ObjectID BSON Type
  233. *
  234. * @classconstant BSON_DATA_OID
  235. **/
  236. BSON.BSON_DATA_OID = 7;
  237. /**
  238. * Boolean BSON Type
  239. *
  240. * @classconstant BSON_DATA_BOOLEAN
  241. **/
  242. BSON.BSON_DATA_BOOLEAN = 8;
  243. /**
  244. * Date BSON Type
  245. *
  246. * @classconstant BSON_DATA_DATE
  247. **/
  248. BSON.BSON_DATA_DATE = 9;
  249. /**
  250. * null BSON Type
  251. *
  252. * @classconstant BSON_DATA_NULL
  253. **/
  254. BSON.BSON_DATA_NULL = 10;
  255. /**
  256. * RegExp BSON Type
  257. *
  258. * @classconstant BSON_DATA_REGEXP
  259. **/
  260. BSON.BSON_DATA_REGEXP = 11;
  261. /**
  262. * Code BSON Type
  263. *
  264. * @classconstant BSON_DATA_CODE
  265. **/
  266. BSON.BSON_DATA_CODE = 13;
  267. /**
  268. * Symbol BSON Type
  269. *
  270. * @classconstant BSON_DATA_SYMBOL
  271. **/
  272. BSON.BSON_DATA_SYMBOL = 14;
  273. /**
  274. * Code with Scope BSON Type
  275. *
  276. * @classconstant BSON_DATA_CODE_W_SCOPE
  277. **/
  278. BSON.BSON_DATA_CODE_W_SCOPE = 15;
  279. /**
  280. * 32 bit Integer BSON Type
  281. *
  282. * @classconstant BSON_DATA_INT
  283. **/
  284. BSON.BSON_DATA_INT = 16;
  285. /**
  286. * Timestamp BSON Type
  287. *
  288. * @classconstant BSON_DATA_TIMESTAMP
  289. **/
  290. BSON.BSON_DATA_TIMESTAMP = 17;
  291. /**
  292. * Long BSON Type
  293. *
  294. * @classconstant BSON_DATA_LONG
  295. **/
  296. BSON.BSON_DATA_LONG = 18;
  297. /**
  298. * MinKey BSON Type
  299. *
  300. * @classconstant BSON_DATA_MIN_KEY
  301. **/
  302. BSON.BSON_DATA_MIN_KEY = 0xff;
  303. /**
  304. * MaxKey BSON Type
  305. *
  306. * @classconstant BSON_DATA_MAX_KEY
  307. **/
  308. BSON.BSON_DATA_MAX_KEY = 0x7f;
  309. /**
  310. * Binary Default Type
  311. *
  312. * @classconstant BSON_BINARY_SUBTYPE_DEFAULT
  313. **/
  314. BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
  315. /**
  316. * Binary Function Type
  317. *
  318. * @classconstant BSON_BINARY_SUBTYPE_FUNCTION
  319. **/
  320. BSON.BSON_BINARY_SUBTYPE_FUNCTION = 1;
  321. /**
  322. * Binary Byte Array Type
  323. *
  324. * @classconstant BSON_BINARY_SUBTYPE_BYTE_ARRAY
  325. **/
  326. BSON.BSON_BINARY_SUBTYPE_BYTE_ARRAY = 2;
  327. /**
  328. * Binary UUID Type
  329. *
  330. * @classconstant BSON_BINARY_SUBTYPE_UUID
  331. **/
  332. BSON.BSON_BINARY_SUBTYPE_UUID = 3;
  333. /**
  334. * Binary MD5 Type
  335. *
  336. * @classconstant BSON_BINARY_SUBTYPE_MD5
  337. **/
  338. BSON.BSON_BINARY_SUBTYPE_MD5 = 4;
  339. /**
  340. * Binary User Defined Type
  341. *
  342. * @classconstant BSON_BINARY_SUBTYPE_USER_DEFINED
  343. **/
  344. BSON.BSON_BINARY_SUBTYPE_USER_DEFINED = 128;
  345. // Return BSON
  346. module.exports = BSON;
  347. module.exports.Code = Code;
  348. module.exports.Map = Map;
  349. module.exports.Symbol = Symbol;
  350. module.exports.BSON = BSON;
  351. module.exports.DBRef = DBRef;
  352. module.exports.Binary = Binary;
  353. module.exports.ObjectID = ObjectID;
  354. module.exports.Long = Long;
  355. module.exports.Timestamp = Timestamp;
  356. module.exports.Double = Double;
  357. module.exports.Int32 = Int32;
  358. module.exports.MinKey = MinKey;
  359. module.exports.MaxKey = MaxKey;
  360. module.exports.BSONRegExp = BSONRegExp;
  361. module.exports.Decimal128 = Decimal128;