calculate_size.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.calculateObjectSize = void 0;
  4. var buffer_1 = require("buffer");
  5. var binary_1 = require("../binary");
  6. var constants = require("../constants");
  7. var utils_1 = require("./utils");
  8. function calculateObjectSize(object, serializeFunctions, ignoreUndefined) {
  9. var totalLength = 4 + 1;
  10. if (Array.isArray(object)) {
  11. for (var i = 0; i < object.length; i++) {
  12. totalLength += calculateElement(i.toString(), object[i], serializeFunctions, true, ignoreUndefined);
  13. }
  14. }
  15. else {
  16. // If we have toBSON defined, override the current object
  17. if (typeof (object === null || object === void 0 ? void 0 : object.toBSON) === 'function') {
  18. object = object.toBSON();
  19. }
  20. // Calculate size
  21. for (var key in object) {
  22. totalLength += calculateElement(key, object[key], serializeFunctions, false, ignoreUndefined);
  23. }
  24. }
  25. return totalLength;
  26. }
  27. exports.calculateObjectSize = calculateObjectSize;
  28. /** @internal */
  29. function calculateElement(name,
  30. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  31. value, serializeFunctions, isArray, ignoreUndefined) {
  32. if (serializeFunctions === void 0) { serializeFunctions = false; }
  33. if (isArray === void 0) { isArray = false; }
  34. if (ignoreUndefined === void 0) { ignoreUndefined = false; }
  35. // If we have toBSON defined, override the current object
  36. if (typeof (value === null || value === void 0 ? void 0 : value.toBSON) === 'function') {
  37. value = value.toBSON();
  38. }
  39. switch (typeof value) {
  40. case 'string':
  41. return 1 + buffer_1.Buffer.byteLength(name, 'utf8') + 1 + 4 + buffer_1.Buffer.byteLength(value, 'utf8') + 1;
  42. case 'number':
  43. if (Math.floor(value) === value &&
  44. value >= constants.JS_INT_MIN &&
  45. value <= constants.JS_INT_MAX) {
  46. if (value >= constants.BSON_INT32_MIN && value <= constants.BSON_INT32_MAX) {
  47. // 32 bit
  48. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (4 + 1);
  49. }
  50. else {
  51. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
  52. }
  53. }
  54. else {
  55. // 64 bit
  56. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
  57. }
  58. case 'undefined':
  59. if (isArray || !ignoreUndefined)
  60. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
  61. return 0;
  62. case 'boolean':
  63. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 1);
  64. case 'object':
  65. if (value == null || value['_bsontype'] === 'MinKey' || value['_bsontype'] === 'MaxKey') {
  66. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + 1;
  67. }
  68. else if (value['_bsontype'] === 'ObjectId' || value['_bsontype'] === 'ObjectID') {
  69. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (12 + 1);
  70. }
  71. else if (value instanceof Date || utils_1.isDate(value)) {
  72. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
  73. }
  74. else if (ArrayBuffer.isView(value) ||
  75. value instanceof ArrayBuffer ||
  76. utils_1.isAnyArrayBuffer(value)) {
  77. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (1 + 4 + 1) + value.byteLength);
  78. }
  79. else if (value['_bsontype'] === 'Long' ||
  80. value['_bsontype'] === 'Double' ||
  81. value['_bsontype'] === 'Timestamp') {
  82. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (8 + 1);
  83. }
  84. else if (value['_bsontype'] === 'Decimal128') {
  85. return (name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (16 + 1);
  86. }
  87. else if (value['_bsontype'] === 'Code') {
  88. // Calculate size depending on the availability of a scope
  89. if (value.scope != null && Object.keys(value.scope).length > 0) {
  90. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  91. 1 +
  92. 4 +
  93. 4 +
  94. buffer_1.Buffer.byteLength(value.code.toString(), 'utf8') +
  95. 1 +
  96. calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined));
  97. }
  98. else {
  99. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  100. 1 +
  101. 4 +
  102. buffer_1.Buffer.byteLength(value.code.toString(), 'utf8') +
  103. 1);
  104. }
  105. }
  106. else if (value['_bsontype'] === 'Binary') {
  107. // Check what kind of subtype we have
  108. if (value.sub_type === binary_1.Binary.SUBTYPE_BYTE_ARRAY) {
  109. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  110. (value.position + 1 + 4 + 1 + 4));
  111. }
  112. else {
  113. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) + (value.position + 1 + 4 + 1));
  114. }
  115. }
  116. else if (value['_bsontype'] === 'Symbol') {
  117. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  118. buffer_1.Buffer.byteLength(value.value, 'utf8') +
  119. 4 +
  120. 1 +
  121. 1);
  122. }
  123. else if (value['_bsontype'] === 'DBRef') {
  124. // Set up correct object for serialization
  125. var ordered_values = Object.assign({
  126. $ref: value.collection,
  127. $id: value.oid
  128. }, value.fields);
  129. // Add db reference if it exists
  130. if (value.db != null) {
  131. ordered_values['$db'] = value.db;
  132. }
  133. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  134. 1 +
  135. calculateObjectSize(ordered_values, serializeFunctions, ignoreUndefined));
  136. }
  137. else if (value instanceof RegExp || utils_1.isRegExp(value)) {
  138. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  139. 1 +
  140. buffer_1.Buffer.byteLength(value.source, 'utf8') +
  141. 1 +
  142. (value.global ? 1 : 0) +
  143. (value.ignoreCase ? 1 : 0) +
  144. (value.multiline ? 1 : 0) +
  145. 1);
  146. }
  147. else if (value['_bsontype'] === 'BSONRegExp') {
  148. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  149. 1 +
  150. buffer_1.Buffer.byteLength(value.pattern, 'utf8') +
  151. 1 +
  152. buffer_1.Buffer.byteLength(value.options, 'utf8') +
  153. 1);
  154. }
  155. else {
  156. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  157. calculateObjectSize(value, serializeFunctions, ignoreUndefined) +
  158. 1);
  159. }
  160. case 'function':
  161. // WTF for 0.4.X where typeof /someregexp/ === 'function'
  162. if (value instanceof RegExp || utils_1.isRegExp(value) || String.call(value) === '[object RegExp]') {
  163. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  164. 1 +
  165. buffer_1.Buffer.byteLength(value.source, 'utf8') +
  166. 1 +
  167. (value.global ? 1 : 0) +
  168. (value.ignoreCase ? 1 : 0) +
  169. (value.multiline ? 1 : 0) +
  170. 1);
  171. }
  172. else {
  173. if (serializeFunctions && value.scope != null && Object.keys(value.scope).length > 0) {
  174. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  175. 1 +
  176. 4 +
  177. 4 +
  178. buffer_1.Buffer.byteLength(utils_1.normalizedFunctionString(value), 'utf8') +
  179. 1 +
  180. calculateObjectSize(value.scope, serializeFunctions, ignoreUndefined));
  181. }
  182. else if (serializeFunctions) {
  183. return ((name != null ? buffer_1.Buffer.byteLength(name, 'utf8') + 1 : 0) +
  184. 1 +
  185. 4 +
  186. buffer_1.Buffer.byteLength(utils_1.normalizedFunctionString(value), 'utf8') +
  187. 1);
  188. }
  189. }
  190. }
  191. return 0;
  192. }
  193. //# sourceMappingURL=calculate_size.js.map