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

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