Skip to content

Commit 551e4e0

Browse files
committed
ref: Rewrite normalization and removed unused utils
1 parent 3f5123f commit 551e4e0

File tree

17 files changed

+517
-688
lines changed

17 files changed

+517
-688
lines changed

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { API, getCurrentHub } from '@sentry/core';
22
import { Breadcrumb, BreadcrumbHint, Integration, Severity } from '@sentry/types';
3-
import { isFunction, isString } from '@sentry/utils/is';
3+
import { isString } from '@sentry/utils/is';
44
import { logger } from '@sentry/utils/logger';
55
import { getEventDescription, getGlobalObject, parseUrl } from '@sentry/utils/misc';
6-
import { deserialize, fill, serializeObject } from '@sentry/utils/object';
7-
import { includes, safeJoin } from '@sentry/utils/string';
6+
import { fill, normalize } from '@sentry/utils/object';
7+
import { safeJoin } from '@sentry/utils/string';
88
import { supportsBeacon, supportsHistory, supportsNativeFetch } from '@sentry/utils/supports';
99
import { BrowserClient } from '../client';
1010
import { breadcrumbEventHandler, keypressEventHandler, wrap } from './helpers';
@@ -87,7 +87,7 @@ export class Breadcrumbs implements Integration {
8787
const filterUrl = new API(dsn).getStoreEndpoint();
8888
// if Sentry key appears in URL, don't capture it as a request
8989
// but rather as our own 'sentry' type breadcrumb
90-
if (filterUrl && includes(url, filterUrl)) {
90+
if (filterUrl && url.includes(filterUrl)) {
9191
addSentryBreadcrumb(data);
9292
return result;
9393
}
@@ -132,7 +132,7 @@ export class Breadcrumbs implements Integration {
132132
category: 'console',
133133
data: {
134134
extra: {
135-
arguments: serializeObject(args, 2),
135+
arguments: normalize(args, 2),
136136
},
137137
logger: 'console',
138138
},
@@ -143,7 +143,7 @@ export class Breadcrumbs implements Integration {
143143
if (level === 'assert') {
144144
if (args[0] === false) {
145145
breadcrumbData.message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
146-
breadcrumbData.data.extra.arguments = serializeObject(args.slice(1), 2);
146+
breadcrumbData.data.extra.arguments = normalize(args.slice(1), 2);
147147
}
148148
}
149149

@@ -205,7 +205,7 @@ export class Breadcrumbs implements Integration {
205205
const filterUrl = new API(dsn).getStoreEndpoint();
206206
// if Sentry key appears in URL, don't capture it as a request
207207
// but rather as our own 'sentry' type breadcrumb
208-
if (filterUrl && includes(url, filterUrl)) {
208+
if (filterUrl && url.includes(filterUrl)) {
209209
if (method === 'POST' && args[1] && args[1].body) {
210210
addSentryBreadcrumb(args[1].body);
211211
}
@@ -338,7 +338,7 @@ export class Breadcrumbs implements Integration {
338338
/** JSDoc */
339339
function wrapProp(prop: string, xhr: XMLHttpRequest): void {
340340
// TODO: Fix XHR types
341-
if (prop in xhr && isFunction((xhr as { [key: string]: any })[prop])) {
341+
if (prop in xhr && typeof (xhr as { [key: string]: any })[prop] === 'function') {
342342
fill(xhr, prop, original =>
343343
wrap(original, {
344344
mechanism: {
@@ -372,7 +372,7 @@ export class Breadcrumbs implements Integration {
372372
const filterUrl = new API(dsn).getStoreEndpoint();
373373
// if Sentry key appears in URL, don't capture it as a request
374374
// but rather as our own 'sentry' type breadcrumb
375-
if (isString(url) && (filterUrl && includes(url, filterUrl))) {
375+
if (isString(url) && (filterUrl && url.includes(filterUrl))) {
376376
this.__sentry_own_request__ = true;
377377
}
378378
}
@@ -424,7 +424,7 @@ export class Breadcrumbs implements Integration {
424424
wrapProp(prop, xhr);
425425
});
426426

427-
if ('onreadystatechange' in xhr && isFunction(xhr.onreadystatechange)) {
427+
if ('onreadystatechange' in xhr && typeof xhr.onreadystatechange === 'function') {
428428
fill(xhr, 'onreadystatechange', function(original: () => void): void {
429429
return wrap(
430430
original,
@@ -496,7 +496,7 @@ export class Breadcrumbs implements Integration {
496496
function addSentryBreadcrumb(serializedData: string): void {
497497
// There's always something that can go wrong with deserialization...
498498
try {
499-
const event: { [key: string]: any } = deserialize(serializedData);
499+
const event: { [key: string]: any } = JSON.parse(serializedData);
500500
Breadcrumbs.addBreadcrumb(
501501
{
502502
category: 'sentry',

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { getCurrentHub } from '@sentry/core';
22
import { Event, Integration } from '@sentry/types';
33
import { logger } from '@sentry/utils/logger';
4-
import { safeNormalize, serialize } from '@sentry/utils/object';
54
import { truncate } from '@sentry/utils/string';
65
import { addExceptionTypeValue, eventFromStacktrace } from '../parsers';
76
import {
@@ -114,10 +113,7 @@ export class GlobalHandlers implements Integration {
114113
},
115114
};
116115

117-
const fallbackValue =
118-
typeof stacktrace.original !== 'undefined'
119-
? `${truncate(serialize(safeNormalize(stacktrace.original)), 300)}`
120-
: '';
116+
const fallbackValue = stacktrace.original ? truncate(JSON.stringify(stacktrace.original), 300) : '';
121117
const fallbackType = stacktrace.mechanism === 'onunhandledrejection' ? 'UnhandledRejection' : 'Error';
122118

123119
// This makes sure we have type/value in every exception

packages/browser/src/integrations/helpers.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { captureException, getCurrentHub, withScope } from '@sentry/core';
22
import { Event as SentryEvent, Mechanism, WrappedFunction } from '@sentry/types';
3-
import { isFunction } from '@sentry/utils/is';
43
import { htmlTreeAsString } from '@sentry/utils/misc';
5-
import { serializeObject } from '@sentry/utils/object';
4+
import { normalize } from '@sentry/utils/object';
65

76
const debounceDuration: number = 1000;
87
let keypressTimeout: number | undefined;
@@ -42,7 +41,8 @@ export function wrap(
4241
} = {},
4342
before?: WrappedFunction,
4443
): any {
45-
if (!isFunction(fn)) {
44+
// tslint:disable-next-line:strict-type-predicates
45+
if (typeof fn !== 'function') {
4646
return fn;
4747
}
4848

@@ -64,7 +64,8 @@ export function wrap(
6464
}
6565

6666
const sentryWrapped: WrappedFunction = function(this: any): void {
67-
if (before && isFunction(before)) {
67+
// tslint:disable-next-line:strict-type-predicates
68+
if (before && typeof before === 'function') {
6869
before.apply(this, arguments);
6970
}
7071

@@ -96,7 +97,7 @@ export function wrap(
9697

9798
processedEvent.extra = {
9899
...processedEvent.extra,
99-
arguments: serializeObject(args, 2),
100+
arguments: normalize(args, 2),
100101
};
101102

102103
return processedEvent;

packages/browser/src/integrations/pluggable/vue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { captureException, getCurrentHub, withScope } from '@sentry/core';
22
import { Event, Integration } from '@sentry/types';
3-
import { isPlainObject, isUndefined } from '@sentry/utils/is';
3+
import { isPlainObject } from '@sentry/utils/is';
44
import { logger } from '@sentry/utils/logger';
55
import { getGlobalObject } from '@sentry/utils/misc';
66

@@ -82,7 +82,7 @@ export class Vue implements Integration {
8282
}
8383
}
8484

85-
if (!isUndefined(info)) {
85+
if (info !== void 0) {
8686
metadata.lifecycleHook = info;
8787
}
8888

packages/browser/src/parsers.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Event, Exception, StackFrame } from '@sentry/types';
2-
import { limitObjectDepthToSize, serializeKeysToEventMessage } from '@sentry/utils/object';
3-
import { includes } from '@sentry/utils/string';
2+
import { normalizeToSize } from '@sentry/utils/object';
3+
import { keysToEventMessage } from '@sentry/utils/string';
44
import { md5 } from './md5';
55
import { computeStackTrace, StackFrame as TraceKitStackFrame, StackTrace as TraceKitStackTrace } from './tracekit';
66

@@ -38,10 +38,10 @@ export function eventFromPlainObject(exception: {}, syntheticException: Error |
3838
const exceptionKeys = Object.keys(exception).sort();
3939
const event: Event = {
4040
extra: {
41-
__serialized__: limitObjectDepthToSize(exception),
41+
__serialized__: normalizeToSize(exception),
4242
},
4343
fingerprint: [md5(exceptionKeys.join(''))],
44-
message: `Non-Error exception captured with keys: ${serializeKeysToEventMessage(exceptionKeys)}`,
44+
message: `Non-Error exception captured with keys: ${keysToEventMessage(exceptionKeys)}`,
4545
};
4646

4747
if (syntheticException) {
@@ -82,12 +82,12 @@ export function prepareFramesForEvent(stack: TraceKitStackFrame[]): StackFrame[]
8282
const lastFrameFunction = localStack[localStack.length - 1].func || '';
8383

8484
// If stack starts with one of our API calls, remove it (starts, meaning it's the top of the stack - aka last call)
85-
if (includes(firstFrameFunction, 'captureMessage') || includes(firstFrameFunction, 'captureException')) {
85+
if (firstFrameFunction.includes('captureMessage') || firstFrameFunction.includes('captureException')) {
8686
localStack = localStack.slice(1);
8787
}
8888

8989
// If stack ends with one of our internal API calls, remove it (ends, meaning it's the bottom of the stack - aka top-most call)
90-
if (includes(lastFrameFunction, 'sentryWrapped')) {
90+
if (lastFrameFunction.includes('sentryWrapped')) {
9191
localStack = localStack.slice(0, -1);
9292
}
9393

packages/browser/src/tracekit.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// tslint:disable
22

3-
import { isUndefined, isError, isErrorEvent } from '@sentry/utils/is';
3+
import { isError, isErrorEvent } from '@sentry/utils/is';
44
import { getGlobalObject } from '@sentry/utils/misc';
55

66
/**
@@ -712,7 +712,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
712712
for (var i = 0; i < maxLines; ++i) {
713713
line = source[lineNo - i] + line;
714714

715-
if (!isUndefined(line)) {
715+
if (line !== void 0) {
716716
if ((m = reGuessFunction.exec(line))) {
717717
return m[1];
718718
} else if ((m = reFunctionArgNames.exec(line))) {
@@ -751,7 +751,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
751751
line -= 1; // convert to 0-based index
752752

753753
for (var i = start; i < end; ++i) {
754-
if (!isUndefined(source[i])) {
754+
if (source[i] !== void 0) {
755755
context.push(source[i]);
756756
}
757757
}
@@ -845,7 +845,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
845845
* @memberof TraceKit.computeStackTrace
846846
*/
847847
function findSourceByFunctionBody(func: any) {
848-
if (isUndefined(window && window.document)) {
848+
if (window && window.document === void 0) {
849849
return;
850850
}
851851

@@ -1005,7 +1005,7 @@ TraceKit.computeStackTrace = (function computeStackTraceWrapper() {
10051005
// NOTE: It's messing out our integration tests in Karma, let's see if we can live with it – Kamil
10061006
// parts[4] = submatch[2];
10071007
// parts[5] = null; // no column when eval
1008-
} else if (i === 0 && !parts[5] && !isUndefined(ex.columnNumber)) {
1008+
} else if (i === 0 && !parts[5] && ex.columnNumber !== void 0) {
10091009
// FireFox uses this awesome columnNumber property for its top frame
10101010
// Also note, Firefox's column number is 0-based and everything else expects 1-based,
10111011
// so adding 1

packages/browser/test/integrations/linkederrors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import { ExtendedError } from '@sentry/types';
12
import { expect } from 'chai';
23
import { stub } from 'sinon';
34
import { BrowserBackend } from '../../src/backend';
45
import { LinkedErrors } from '../../src/integrations/linkederrors';
5-
import { ExtendedError } from '@sentry/types';
66

77
let linkedErrors: LinkedErrors;
88

packages/core/src/integrations/extraerrordata.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
22
import { Event, EventHint, ExtendedError, Integration } from '@sentry/types';
33
import { isError, isPlainObject } from '@sentry/utils/is';
44
import { logger } from '@sentry/utils/logger';
5-
import { safeNormalize } from '@sentry/utils/object';
5+
import { normalize } from '@sentry/utils/object';
66

77
/** Patch toString calls to return proper name for wrapped functions */
88
export class ExtraErrorData implements Integration {
@@ -44,7 +44,7 @@ export class ExtraErrorData implements Integration {
4444
...event.extra,
4545
};
4646

47-
const normalizedErrorData = safeNormalize(errorData);
47+
const normalizedErrorData = normalize(errorData);
4848
if (isPlainObject(normalizedErrorData)) {
4949
extra = {
5050
...event.extra,
@@ -79,6 +79,7 @@ export class ExtraErrorData implements Integration {
7979
if (isError(value)) {
8080
value = (value as Error).name || (value as Error).constructor.name;
8181
}
82+
// tslint:disable:no-unsafe-any
8283
extraErrorInfo[key] = value;
8384
}
8485
result = {

packages/core/src/integrations/inboundfilters.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Client, Event, Integration } from '@sentry/types';
33
import { isRegExp } from '@sentry/utils/is';
44
import { logger } from '@sentry/utils/logger';
55
import { getEventDescription } from '@sentry/utils/misc';
6-
import { includes } from '@sentry/utils/string';
76

87
// "Script error." is hard coded into browsers for errors that it can't read.
98
// this is the result of a script being pulled in from an external domain and CORS.
@@ -148,7 +147,7 @@ export class InboundFilters implements Integration {
148147
if (isRegExp(pattern)) {
149148
return (pattern as RegExp).test(value);
150149
} else if (typeof pattern === 'string') {
151-
return includes(value, pattern);
150+
return value.includes(pattern);
152151
} else {
153152
return false;
154153
}

packages/hub/src/scope.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Breadcrumb, Event, EventHint, EventProcessor, Scope as ScopeInterface, Severity, User } from '@sentry/types';
2-
import { isFunction, isThenable } from '@sentry/utils/is';
2+
import { isThenable } from '@sentry/utils/is';
33
import { getGlobalObject } from '@sentry/utils/misc';
4-
import { safesafeNormalize } from '@sentry/utils/object';
4+
import { normalize } from '@sentry/utils/object';
55
import { SyncPromise } from '@sentry/utils/syncpromise';
66

77
/**
@@ -60,7 +60,8 @@ export class Scope implements ScopeInterface {
6060
): SyncPromise<Event | null> {
6161
return new SyncPromise<Event | null>((resolve, reject) => {
6262
const processor = processors[index];
63-
if (event === null || !isFunction(processor)) {
63+
// tslint:disable-next-line:strict-type-predicates
64+
if (event === null || typeof processor !== 'function') {
6465
resolve(event);
6566
} else {
6667
const result = processor({ ...event }, hint) as Event | null;
@@ -81,39 +82,39 @@ export class Scope implements ScopeInterface {
8182
* @inheritdoc
8283
*/
8384
public setUser(user: User): Scope {
84-
this.user = safeNormalize(user);
85+
this.user = normalize(user);
8586
return this;
8687
}
8788

8889
/**
8990
* @inheritdoc
9091
*/
9192
public setTag(key: string, value: string): Scope {
92-
this.tags = { ...this.tags, [key]: safeNormalize(value) };
93+
this.tags = { ...this.tags, [key]: normalize(value) };
9394
return this;
9495
}
9596

9697
/**
9798
* @inheritdoc
9899
*/
99100
public setExtra(key: string, extra: any): Scope {
100-
this.extra = { ...this.extra, [key]: safeNormalize(extra) };
101+
this.extra = { ...this.extra, [key]: normalize(extra) };
101102
return this;
102103
}
103104

104105
/**
105106
* @inheritdoc
106107
*/
107108
public setFingerprint(fingerprint: string[]): Scope {
108-
this.fingerprint = safeNormalize(fingerprint);
109+
this.fingerprint = normalize(fingerprint);
109110
return this;
110111
}
111112

112113
/**
113114
* @inheritdoc
114115
*/
115116
public setLevel(level: Severity): Scope {
116-
this.level = safeNormalize(level);
117+
this.level = normalize(level);
117118
return this;
118119
}
119120

@@ -153,8 +154,8 @@ export class Scope implements ScopeInterface {
153154
public addBreadcrumb(breadcrumb: Breadcrumb, maxBreadcrumbs?: number): void {
154155
this.breadcrumbs =
155156
maxBreadcrumbs !== undefined && maxBreadcrumbs >= 0
156-
? [...this.breadcrumbs, safeNormalize(breadcrumb)].slice(-maxBreadcrumbs)
157-
: [...this.breadcrumbs, safeNormalize(breadcrumb)];
157+
? [...this.breadcrumbs, normalize(breadcrumb)].slice(-maxBreadcrumbs)
158+
: [...this.breadcrumbs, normalize(breadcrumb)];
158159
}
159160

160161
/**

packages/node/src/backend.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { BaseBackend, Dsn, getCurrentHub } from '@sentry/core';
22
import { Event, EventHint, Options, Severity, Transport } from '@sentry/types';
33
import { isError, isPlainObject } from '@sentry/utils/is';
4-
import { limitObjectDepthToSize, serializeKeysToEventMessage } from '@sentry/utils/object';
4+
import { normalizeToSize } from '@sentry/utils/object';
5+
import { keysToEventMessage } from '@sentry/utils/string';
56
import { SyncPromise } from '@sentry/utils/syncpromise';
67
import { createHash } from 'crypto';
78
import { extractStackFromError, parseError, parseStack, prepareFramesForEvent } from './parsers';
@@ -81,10 +82,10 @@ export class NodeBackend extends BaseBackend<NodeOptions> {
8182
// This will allow us to group events based on top-level keys
8283
// which is much better than creating new group when any key/value change
8384
const keys = Object.keys(exception as {}).sort();
84-
const message = `Non-Error exception captured with keys: ${serializeKeysToEventMessage(keys)}`;
85+
const message = `Non-Error exception captured with keys: ${keysToEventMessage(keys)}`;
8586

8687
getCurrentHub().configureScope(scope => {
87-
scope.setExtra('__serialized__', limitObjectDepthToSize(exception as {}));
88+
scope.setExtra('__serialized__', normalizeToSize(exception as {}));
8889
scope.setFingerprint([
8990
createHash('md5')
9091
.update(keys.join(''))

0 commit comments

Comments
 (0)