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