bson.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ObjectID = exports.Decimal128 = exports.BSONRegExp = exports.MaxKey = exports.MinKey = exports.Int32 = exports.Double = exports.Timestamp = exports.Long = exports.UUID = exports.ObjectId = exports.Binary = exports.DBRef = exports.BSONSymbol = exports.Map = exports.Code = exports.LongWithoutOverridesClass = exports.EJSON = exports.BSON_INT64_MIN = exports.BSON_INT64_MAX = exports.BSON_INT32_MIN = exports.BSON_INT32_MAX = exports.BSON_DATA_UNDEFINED = exports.BSON_DATA_TIMESTAMP = exports.BSON_DATA_SYMBOL = exports.BSON_DATA_STRING = exports.BSON_DATA_REGEXP = exports.BSON_DATA_OID = exports.BSON_DATA_OBJECT = exports.BSON_DATA_NUMBER = exports.BSON_DATA_NULL = exports.BSON_DATA_MIN_KEY = exports.BSON_DATA_MAX_KEY = exports.BSON_DATA_LONG = exports.BSON_DATA_INT = exports.BSON_DATA_DECIMAL128 = exports.BSON_DATA_DBPOINTER = exports.BSON_DATA_DATE = exports.BSON_DATA_CODE_W_SCOPE = exports.BSON_DATA_CODE = exports.BSON_DATA_BOOLEAN = exports.BSON_DATA_BINARY = exports.BSON_DATA_ARRAY = exports.BSON_BINARY_SUBTYPE_UUID_NEW = exports.BSON_BINARY_SUBTYPE_UUID = exports.BSON_BINARY_SUBTYPE_USER_DEFINED = exports.BSON_BINARY_SUBTYPE_MD5 = exports.BSON_BINARY_SUBTYPE_FUNCTION = exports.BSON_BINARY_SUBTYPE_DEFAULT = exports.BSON_BINARY_SUBTYPE_BYTE_ARRAY = void 0;
  4. exports.deserializeStream = exports.calculateObjectSize = exports.deserialize = exports.serializeWithBufferAndIndex = exports.serialize = exports.setInternalBufferSize = void 0;
  5. var buffer_1 = require("buffer");
  6. var binary_1 = require("./binary");
  7. Object.defineProperty(exports, "Binary", { enumerable: true, get: function () { return binary_1.Binary; } });
  8. var code_1 = require("./code");
  9. Object.defineProperty(exports, "Code", { enumerable: true, get: function () { return code_1.Code; } });
  10. var db_ref_1 = require("./db_ref");
  11. Object.defineProperty(exports, "DBRef", { enumerable: true, get: function () { return db_ref_1.DBRef; } });
  12. var decimal128_1 = require("./decimal128");
  13. Object.defineProperty(exports, "Decimal128", { enumerable: true, get: function () { return decimal128_1.Decimal128; } });
  14. var double_1 = require("./double");
  15. Object.defineProperty(exports, "Double", { enumerable: true, get: function () { return double_1.Double; } });
  16. var ensure_buffer_1 = require("./ensure_buffer");
  17. var extended_json_1 = require("./extended_json");
  18. var int_32_1 = require("./int_32");
  19. Object.defineProperty(exports, "Int32", { enumerable: true, get: function () { return int_32_1.Int32; } });
  20. var long_1 = require("./long");
  21. Object.defineProperty(exports, "Long", { enumerable: true, get: function () { return long_1.Long; } });
  22. var map_1 = require("./map");
  23. Object.defineProperty(exports, "Map", { enumerable: true, get: function () { return map_1.Map; } });
  24. var max_key_1 = require("./max_key");
  25. Object.defineProperty(exports, "MaxKey", { enumerable: true, get: function () { return max_key_1.MaxKey; } });
  26. var min_key_1 = require("./min_key");
  27. Object.defineProperty(exports, "MinKey", { enumerable: true, get: function () { return min_key_1.MinKey; } });
  28. var objectid_1 = require("./objectid");
  29. Object.defineProperty(exports, "ObjectId", { enumerable: true, get: function () { return objectid_1.ObjectId; } });
  30. Object.defineProperty(exports, "ObjectID", { enumerable: true, get: function () { return objectid_1.ObjectId; } });
  31. var calculate_size_1 = require("./parser/calculate_size");
  32. // Parts of the parser
  33. var deserializer_1 = require("./parser/deserializer");
  34. var serializer_1 = require("./parser/serializer");
  35. var regexp_1 = require("./regexp");
  36. Object.defineProperty(exports, "BSONRegExp", { enumerable: true, get: function () { return regexp_1.BSONRegExp; } });
  37. var symbol_1 = require("./symbol");
  38. Object.defineProperty(exports, "BSONSymbol", { enumerable: true, get: function () { return symbol_1.BSONSymbol; } });
  39. var timestamp_1 = require("./timestamp");
  40. Object.defineProperty(exports, "Timestamp", { enumerable: true, get: function () { return timestamp_1.Timestamp; } });
  41. var uuid_1 = require("./uuid");
  42. Object.defineProperty(exports, "UUID", { enumerable: true, get: function () { return uuid_1.UUID; } });
  43. var constants_1 = require("./constants");
  44. Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_BYTE_ARRAY", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_BYTE_ARRAY; } });
  45. Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_DEFAULT", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_DEFAULT; } });
  46. Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_FUNCTION", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_FUNCTION; } });
  47. Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_MD5", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_MD5; } });
  48. Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_USER_DEFINED", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_USER_DEFINED; } });
  49. Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_UUID", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_UUID; } });
  50. Object.defineProperty(exports, "BSON_BINARY_SUBTYPE_UUID_NEW", { enumerable: true, get: function () { return constants_1.BSON_BINARY_SUBTYPE_UUID_NEW; } });
  51. Object.defineProperty(exports, "BSON_DATA_ARRAY", { enumerable: true, get: function () { return constants_1.BSON_DATA_ARRAY; } });
  52. Object.defineProperty(exports, "BSON_DATA_BINARY", { enumerable: true, get: function () { return constants_1.BSON_DATA_BINARY; } });
  53. Object.defineProperty(exports, "BSON_DATA_BOOLEAN", { enumerable: true, get: function () { return constants_1.BSON_DATA_BOOLEAN; } });
  54. Object.defineProperty(exports, "BSON_DATA_CODE", { enumerable: true, get: function () { return constants_1.BSON_DATA_CODE; } });
  55. Object.defineProperty(exports, "BSON_DATA_CODE_W_SCOPE", { enumerable: true, get: function () { return constants_1.BSON_DATA_CODE_W_SCOPE; } });
  56. Object.defineProperty(exports, "BSON_DATA_DATE", { enumerable: true, get: function () { return constants_1.BSON_DATA_DATE; } });
  57. Object.defineProperty(exports, "BSON_DATA_DBPOINTER", { enumerable: true, get: function () { return constants_1.BSON_DATA_DBPOINTER; } });
  58. Object.defineProperty(exports, "BSON_DATA_DECIMAL128", { enumerable: true, get: function () { return constants_1.BSON_DATA_DECIMAL128; } });
  59. Object.defineProperty(exports, "BSON_DATA_INT", { enumerable: true, get: function () { return constants_1.BSON_DATA_INT; } });
  60. Object.defineProperty(exports, "BSON_DATA_LONG", { enumerable: true, get: function () { return constants_1.BSON_DATA_LONG; } });
  61. Object.defineProperty(exports, "BSON_DATA_MAX_KEY", { enumerable: true, get: function () { return constants_1.BSON_DATA_MAX_KEY; } });
  62. Object.defineProperty(exports, "BSON_DATA_MIN_KEY", { enumerable: true, get: function () { return constants_1.BSON_DATA_MIN_KEY; } });
  63. Object.defineProperty(exports, "BSON_DATA_NULL", { enumerable: true, get: function () { return constants_1.BSON_DATA_NULL; } });
  64. Object.defineProperty(exports, "BSON_DATA_NUMBER", { enumerable: true, get: function () { return constants_1.BSON_DATA_NUMBER; } });
  65. Object.defineProperty(exports, "BSON_DATA_OBJECT", { enumerable: true, get: function () { return constants_1.BSON_DATA_OBJECT; } });
  66. Object.defineProperty(exports, "BSON_DATA_OID", { enumerable: true, get: function () { return constants_1.BSON_DATA_OID; } });
  67. Object.defineProperty(exports, "BSON_DATA_REGEXP", { enumerable: true, get: function () { return constants_1.BSON_DATA_REGEXP; } });
  68. Object.defineProperty(exports, "BSON_DATA_STRING", { enumerable: true, get: function () { return constants_1.BSON_DATA_STRING; } });
  69. Object.defineProperty(exports, "BSON_DATA_SYMBOL", { enumerable: true, get: function () { return constants_1.BSON_DATA_SYMBOL; } });
  70. Object.defineProperty(exports, "BSON_DATA_TIMESTAMP", { enumerable: true, get: function () { return constants_1.BSON_DATA_TIMESTAMP; } });
  71. Object.defineProperty(exports, "BSON_DATA_UNDEFINED", { enumerable: true, get: function () { return constants_1.BSON_DATA_UNDEFINED; } });
  72. Object.defineProperty(exports, "BSON_INT32_MAX", { enumerable: true, get: function () { return constants_1.BSON_INT32_MAX; } });
  73. Object.defineProperty(exports, "BSON_INT32_MIN", { enumerable: true, get: function () { return constants_1.BSON_INT32_MIN; } });
  74. Object.defineProperty(exports, "BSON_INT64_MAX", { enumerable: true, get: function () { return constants_1.BSON_INT64_MAX; } });
  75. Object.defineProperty(exports, "BSON_INT64_MIN", { enumerable: true, get: function () { return constants_1.BSON_INT64_MIN; } });
  76. var extended_json_2 = require("./extended_json");
  77. Object.defineProperty(exports, "EJSON", { enumerable: true, get: function () { return extended_json_2.EJSON; } });
  78. var timestamp_2 = require("./timestamp");
  79. Object.defineProperty(exports, "LongWithoutOverridesClass", { enumerable: true, get: function () { return timestamp_2.LongWithoutOverridesClass; } });
  80. /** @internal */
  81. // Default Max Size
  82. var MAXSIZE = 1024 * 1024 * 17;
  83. // Current Internal Temporary Serialization Buffer
  84. var buffer = buffer_1.Buffer.alloc(MAXSIZE);
  85. /**
  86. * Sets the size of the internal serialization buffer.
  87. *
  88. * @param size - The desired size for the internal serialization buffer
  89. * @public
  90. */
  91. function setInternalBufferSize(size) {
  92. // Resize the internal serialization buffer if needed
  93. if (buffer.length < size) {
  94. buffer = buffer_1.Buffer.alloc(size);
  95. }
  96. }
  97. exports.setInternalBufferSize = setInternalBufferSize;
  98. /**
  99. * Serialize a Javascript object.
  100. *
  101. * @param object - the Javascript object to serialize.
  102. * @returns Buffer object containing the serialized object.
  103. * @public
  104. */
  105. function serialize(object, options) {
  106. if (options === void 0) { options = {}; }
  107. // Unpack the options
  108. var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
  109. var serializeFunctions = typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
  110. var ignoreUndefined = typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
  111. var minInternalBufferSize = typeof options.minInternalBufferSize === 'number' ? options.minInternalBufferSize : MAXSIZE;
  112. // Resize the internal serialization buffer if needed
  113. if (buffer.length < minInternalBufferSize) {
  114. buffer = buffer_1.Buffer.alloc(minInternalBufferSize);
  115. }
  116. // Attempt to serialize
  117. var serializationIndex = serializer_1.serializeInto(buffer, object, checkKeys, 0, 0, serializeFunctions, ignoreUndefined, []);
  118. // Create the final buffer
  119. var finishedBuffer = buffer_1.Buffer.alloc(serializationIndex);
  120. // Copy into the finished buffer
  121. buffer.copy(finishedBuffer, 0, 0, finishedBuffer.length);
  122. // Return the buffer
  123. return finishedBuffer;
  124. }
  125. exports.serialize = serialize;
  126. /**
  127. * Serialize a Javascript object using a predefined Buffer and index into the buffer,
  128. * useful when pre-allocating the space for serialization.
  129. *
  130. * @param object - the Javascript object to serialize.
  131. * @param finalBuffer - the Buffer you pre-allocated to store the serialized BSON object.
  132. * @returns the index pointing to the last written byte in the buffer.
  133. * @public
  134. */
  135. function serializeWithBufferAndIndex(object, finalBuffer, options) {
  136. if (options === void 0) { options = {}; }
  137. // Unpack the options
  138. var checkKeys = typeof options.checkKeys === 'boolean' ? options.checkKeys : false;
  139. var serializeFunctions = typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
  140. var ignoreUndefined = typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
  141. var startIndex = typeof options.index === 'number' ? options.index : 0;
  142. // Attempt to serialize
  143. var serializationIndex = serializer_1.serializeInto(buffer, object, checkKeys, 0, 0, serializeFunctions, ignoreUndefined);
  144. buffer.copy(finalBuffer, startIndex, 0, serializationIndex);
  145. // Return the index
  146. return startIndex + serializationIndex - 1;
  147. }
  148. exports.serializeWithBufferAndIndex = serializeWithBufferAndIndex;
  149. /**
  150. * Deserialize data as BSON.
  151. *
  152. * @param buffer - the buffer containing the serialized set of BSON documents.
  153. * @returns returns the deserialized Javascript Object.
  154. * @public
  155. */
  156. function deserialize(buffer, options) {
  157. if (options === void 0) { options = {}; }
  158. return deserializer_1.deserialize(ensure_buffer_1.ensureBuffer(buffer), options);
  159. }
  160. exports.deserialize = deserialize;
  161. /**
  162. * Calculate the bson size for a passed in Javascript object.
  163. *
  164. * @param object - the Javascript object to calculate the BSON byte size for
  165. * @returns size of BSON object in bytes
  166. * @public
  167. */
  168. function calculateObjectSize(object, options) {
  169. if (options === void 0) { options = {}; }
  170. options = options || {};
  171. var serializeFunctions = typeof options.serializeFunctions === 'boolean' ? options.serializeFunctions : false;
  172. var ignoreUndefined = typeof options.ignoreUndefined === 'boolean' ? options.ignoreUndefined : true;
  173. return calculate_size_1.calculateObjectSize(object, serializeFunctions, ignoreUndefined);
  174. }
  175. exports.calculateObjectSize = calculateObjectSize;
  176. /**
  177. * Deserialize stream data as BSON documents.
  178. *
  179. * @param data - the buffer containing the serialized set of BSON documents.
  180. * @param startIndex - the start index in the data Buffer where the deserialization is to start.
  181. * @param numberOfDocuments - number of documents to deserialize.
  182. * @param documents - an array where to store the deserialized documents.
  183. * @param docStartIndex - the index in the documents array from where to start inserting documents.
  184. * @param options - additional options used for the deserialization.
  185. * @returns next index in the buffer after deserialization **x** numbers of documents.
  186. * @public
  187. */
  188. function deserializeStream(data, startIndex, numberOfDocuments, documents, docStartIndex, options) {
  189. var internalOptions = Object.assign({ allowObjectSmallerThanBufferSize: true, index: 0 }, options);
  190. var bufferData = ensure_buffer_1.ensureBuffer(data);
  191. var index = startIndex;
  192. // Loop over all documents
  193. for (var i = 0; i < numberOfDocuments; i++) {
  194. // Find size of the document
  195. var size = bufferData[index] |
  196. (bufferData[index + 1] << 8) |
  197. (bufferData[index + 2] << 16) |
  198. (bufferData[index + 3] << 24);
  199. // Update options with index
  200. internalOptions.index = index;
  201. // Parse the document at this point
  202. documents[docStartIndex + i] = deserializer_1.deserialize(bufferData, internalOptions);
  203. // Adjust index by the document size
  204. index = index + size;
  205. }
  206. // Return object containing end index of parsing and list of documents
  207. return index;
  208. }
  209. exports.deserializeStream = deserializeStream;
  210. /**
  211. * BSON default export
  212. * @deprecated Please use named exports
  213. * @privateRemarks
  214. * We want to someday deprecate the default export,
  215. * so none of the new TS types are being exported on the default
  216. * @public
  217. */
  218. var BSON = {
  219. Binary: binary_1.Binary,
  220. Code: code_1.Code,
  221. DBRef: db_ref_1.DBRef,
  222. Decimal128: decimal128_1.Decimal128,
  223. Double: double_1.Double,
  224. Int32: int_32_1.Int32,
  225. Long: long_1.Long,
  226. UUID: uuid_1.UUID,
  227. Map: map_1.Map,
  228. MaxKey: max_key_1.MaxKey,
  229. MinKey: min_key_1.MinKey,
  230. ObjectId: objectid_1.ObjectId,
  231. ObjectID: objectid_1.ObjectId,
  232. BSONRegExp: regexp_1.BSONRegExp,
  233. BSONSymbol: symbol_1.BSONSymbol,
  234. Timestamp: timestamp_1.Timestamp,
  235. EJSON: extended_json_1.EJSON,
  236. setInternalBufferSize: setInternalBufferSize,
  237. serialize: serialize,
  238. serializeWithBufferAndIndex: serializeWithBufferAndIndex,
  239. deserialize: deserialize,
  240. calculateObjectSize: calculateObjectSize,
  241. deserializeStream: deserializeStream
  242. };
  243. exports.default = BSON;
  244. //# sourceMappingURL=bson.js.map