@sentry/core
Version:
Base implementation for all Sentry JavaScript SDKs
84 lines (80 loc) • 2.78 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const is = require('./utils/is.js');
function isAttributeObject(maybeObj) {
return typeof maybeObj === "object" && maybeObj != null && !Array.isArray(maybeObj) && Object.keys(maybeObj).includes("value");
}
function attributeValueToTypedAttributeValue(rawValue, useFallback) {
const { value, unit } = isAttributeObject(rawValue) ? rawValue : { value: rawValue, unit: void 0 };
const attributeValue = getTypedAttributeValue(value);
const checkedUnit = unit && typeof unit === "string" ? { unit } : {};
if (attributeValue) {
return { ...attributeValue, ...checkedUnit };
}
if (!useFallback || useFallback === "skip-undefined" && value === void 0) {
return;
}
let stringValue = "";
try {
stringValue = JSON.stringify(value) ?? "";
} catch {
}
return {
value: stringValue,
type: "string",
...checkedUnit
};
}
function serializeAttributes(attributes, fallback = false) {
const serializedAttributes = {};
for (const [key, value] of Object.entries(attributes ?? {})) {
const typedValue = attributeValueToTypedAttributeValue(value, fallback);
if (typedValue) {
serializedAttributes[key] = typedValue;
}
}
return serializedAttributes;
}
function estimateTypedAttributesSizeInBytes(attributes) {
if (!attributes) {
return 0;
}
let weight = 0;
for (const [key, attr] of Object.entries(attributes)) {
weight += key.length * 2;
weight += attr.type.length * 2;
weight += (attr.unit?.length ?? 0) * 2;
const val = attr.value;
if (Array.isArray(val)) {
weight += estimatePrimitiveSizeInBytes(val[0]) * val.length;
} else if (is.isPrimitive(val)) {
weight += estimatePrimitiveSizeInBytes(val);
} else {
weight += 100;
}
}
return weight;
}
function estimatePrimitiveSizeInBytes(value) {
if (typeof value === "string") {
return value.length * 2;
} else if (typeof value === "boolean") {
return 4;
} else if (typeof value === "number") {
return 8;
}
return 0;
}
function getTypedAttributeValue(value) {
if (Array.isArray(value)) {
return { value, type: "array" };
}
const primitiveType = typeof value === "string" ? "string" : typeof value === "boolean" ? "boolean" : typeof value === "number" && !Number.isNaN(value) ? Number.isInteger(value) ? "integer" : "double" : null;
if (primitiveType) {
return { value, type: primitiveType };
}
}
exports.attributeValueToTypedAttributeValue = attributeValueToTypedAttributeValue;
exports.estimateTypedAttributesSizeInBytes = estimateTypedAttributesSizeInBytes;
exports.isAttributeObject = isAttributeObject;
exports.serializeAttributes = serializeAttributes;
//# sourceMappingURL=attributes.js.map