123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- import { isNaN, isVueViewModel, isSyntheticEvent } from './is.js';
- import { memoBuilder } from './memo.js';
- import { convertToPlainObject } from './object.js';
- import { getFunctionName } from './stacktrace.js';
- function normalize(input, depth = 100, maxProperties = +Infinity) {
- try {
-
- return visit('', input, depth, maxProperties);
- } catch (err) {
- return { ERROR: `**non-serializable** (${err})` };
- }
- }
- function normalizeToSize(
-
- object,
-
- depth = 3,
-
- maxSize = 100 * 1024,
- ) {
- const normalized = normalize(object, depth);
- if (jsonSize(normalized) > maxSize) {
- return normalizeToSize(object, depth - 1, maxSize);
- }
- return normalized ;
- }
- function visit(
- key,
- value,
- depth = +Infinity,
- maxProperties = +Infinity,
- memo = memoBuilder(),
- ) {
- const [memoize, unmemoize] = memo;
-
- if (
- value == null ||
- (['number', 'boolean', 'string'].includes(typeof value) && !isNaN(value))
- ) {
- return value ;
- }
- const stringified = stringifyValue(key, value);
-
-
- if (!stringified.startsWith('[object ')) {
- return stringified;
- }
-
-
-
-
- if ((value )['__sentry_skip_normalization__']) {
- return value ;
- }
-
-
-
- const remainingDepth =
- typeof (value )['__sentry_override_normalization_depth__'] === 'number'
- ? ((value )['__sentry_override_normalization_depth__'] )
- : depth;
-
- if (remainingDepth === 0) {
-
- return stringified.replace('object ', '');
- }
-
- if (memoize(value)) {
- return '[Circular ~]';
- }
-
- const valueWithToJSON = value ;
- if (valueWithToJSON && typeof valueWithToJSON.toJSON === 'function') {
- try {
- const jsonValue = valueWithToJSON.toJSON();
-
- return visit('', jsonValue, remainingDepth - 1, maxProperties, memo);
- } catch (err) {
-
- }
- }
-
-
-
- const normalized = (Array.isArray(value) ? [] : {}) ;
- let numAdded = 0;
-
-
- const visitable = convertToPlainObject(value );
- for (const visitKey in visitable) {
-
- if (!Object.prototype.hasOwnProperty.call(visitable, visitKey)) {
- continue;
- }
- if (numAdded >= maxProperties) {
- normalized[visitKey] = '[MaxProperties ~]';
- break;
- }
-
- const visitValue = visitable[visitKey];
- normalized[visitKey] = visit(visitKey, visitValue, remainingDepth - 1, maxProperties, memo);
- numAdded++;
- }
-
- unmemoize(value);
-
- return normalized;
- }
- function stringifyValue(
- key,
-
-
- value,
- ) {
- try {
- if (key === 'domain' && value && typeof value === 'object' && (value )._events) {
- return '[Domain]';
- }
- if (key === 'domainEmitter') {
- return '[DomainEmitter]';
- }
-
-
- if (typeof global !== 'undefined' && value === global) {
- return '[Global]';
- }
-
- if (typeof window !== 'undefined' && value === window) {
- return '[Window]';
- }
-
- if (typeof document !== 'undefined' && value === document) {
- return '[Document]';
- }
- if (isVueViewModel(value)) {
- return '[VueViewModel]';
- }
-
- if (isSyntheticEvent(value)) {
- return '[SyntheticEvent]';
- }
- if (typeof value === 'number' && value !== value) {
- return '[NaN]';
- }
- if (typeof value === 'function') {
- return `[Function: ${getFunctionName(value)}]`;
- }
- if (typeof value === 'symbol') {
- return `[${String(value)}]`;
- }
-
- if (typeof value === 'bigint') {
- return `[BigInt: ${String(value)}]`;
- }
-
-
-
-
- const objName = getConstructorName(value);
-
- if (/^HTML(\w*)Element$/.test(objName)) {
- return `[HTMLElement: ${objName}]`;
- }
- return `[object ${objName}]`;
- } catch (err) {
- return `**non-serializable** (${err})`;
- }
- }
- function getConstructorName(value) {
- const prototype = Object.getPrototypeOf(value);
- return prototype ? prototype.constructor.name : 'null prototype';
- }
- function utf8Length(value) {
-
- return ~-encodeURI(value).split(/%..|./).length;
- }
- function jsonSize(value) {
- return utf8Length(JSON.stringify(value));
- }
- function normalizeUrlToBase(url, basePath) {
- const escapedBase = basePath
-
- .replace(/\\/g, '/')
-
- .replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
- let newUrl = url;
- try {
- newUrl = decodeURI(url);
- } catch (_Oo) {
-
- }
- return (
- newUrl
- .replace(/\\/g, '/')
- .replace(/webpack:\/?/g, '')
-
- .replace(new RegExp(`(file://)?/*${escapedBase}/*`, 'ig'), 'app:///')
- );
- }
- export { normalize, normalizeToSize, normalizeUrlToBase, visit as walk };
|