bson.js 16 KB

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