bson.js 16 KB

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