/node_modules/mongoose/node_modules/bson/lib/bson/bson.js

https://gitlab.com/junxianlim/twitter_clone_node · JavaScript · 321 lines · 93 code · 17 blank · 211 comment · 4 complexity · b5cbb7788f219c835457d3cac5cc3f4c MD5 · raw file

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