Skip to content

Commit 5515dd8

Browse files
committed
ref(core): Reduce inboundfilters bundle size
1 parent 995928b commit 5515dd8

File tree

1 file changed

+143
-157
lines changed

1 file changed

+143
-157
lines changed

packages/core/src/integrations/inboundfilters.ts

Lines changed: 143 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -39,195 +39,181 @@ export class InboundFilters implements Integration {
3939
public setupOnce(): void {
4040
addGlobalEventProcessor((event: Event) => {
4141
const hub = getCurrentHub();
42-
if (!hub) {
43-
return event;
44-
}
45-
const self = hub.getIntegration(InboundFilters);
46-
if (self) {
47-
const client = hub.getClient();
48-
const clientOptions = client ? client.getOptions() : {};
49-
// This checks prevents most of the occurrences of the bug linked below:
50-
// https://github.com/getsentry/sentry-javascript/issues/2622
51-
// The bug is caused by multiple SDK instances, where one is minified and one is using non-mangled code.
52-
// Unfortunatelly we cannot fix it reliably (thus reserved property in rollup's terser config),
53-
// as we cannot force people using multiple instances in their apps to sync SDK versions.
54-
const options = typeof self._mergeOptions === 'function' ? self._mergeOptions(clientOptions) : {};
55-
if (typeof self._shouldDropEvent !== 'function') {
56-
return event;
42+
if (hub) {
43+
const self = hub.getIntegration(InboundFilters);
44+
if (self) {
45+
const client = hub.getClient();
46+
const clientOptions = client ? client.getOptions() : {};
47+
const options = _mergeOptions(self._options, clientOptions);
48+
return _shouldDropEvent(event, options) ? null : event;
5749
}
58-
return self._shouldDropEvent(event, options) ? null : event;
5950
}
6051
return event;
6152
});
6253
}
54+
}
6355

64-
/** JSDoc */
65-
private _shouldDropEvent(event: Event, options: Partial<InboundFiltersOptions>): boolean {
66-
if (this._isSentryError(event, options)) {
67-
if (isDebugBuild()) {
68-
logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
69-
}
70-
return true;
56+
/** JSDoc */
57+
export function _mergeOptions(
58+
intOptions: Partial<InboundFiltersOptions>,
59+
clientOptions: Partial<InboundFiltersOptions> = {},
60+
): Partial<InboundFiltersOptions> {
61+
return {
62+
allowUrls: [
63+
// eslint-disable-next-line deprecation/deprecation
64+
...(intOptions.whitelistUrls || []),
65+
...(intOptions.allowUrls || []),
66+
// eslint-disable-next-line deprecation/deprecation
67+
...(clientOptions.whitelistUrls || []),
68+
...(clientOptions.allowUrls || []),
69+
],
70+
denyUrls: [
71+
// eslint-disable-next-line deprecation/deprecation
72+
...(intOptions.blacklistUrls || []),
73+
...(intOptions.denyUrls || []),
74+
// eslint-disable-next-line deprecation/deprecation
75+
...(clientOptions.blacklistUrls || []),
76+
...(clientOptions.denyUrls || []),
77+
],
78+
ignoreErrors: [...(intOptions.ignoreErrors || []), ...(clientOptions.ignoreErrors || []), ...DEFAULT_IGNORE_ERRORS],
79+
ignoreInternal: typeof intOptions.ignoreInternal !== 'undefined' ? intOptions.ignoreInternal : true,
80+
};
81+
}
82+
83+
/** JSDoc */
84+
export function _shouldDropEvent(event: Event, options: Partial<InboundFiltersOptions>): boolean {
85+
if (_isSentryError(event, options.ignoreInternal)) {
86+
if (isDebugBuild()) {
87+
logger.warn(`Event dropped due to being internal Sentry Error.\nEvent: ${getEventDescription(event)}`);
7188
}
72-
if (this._isIgnoredError(event, options)) {
73-
if (isDebugBuild()) {
74-
logger.warn(
75-
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
76-
);
77-
}
78-
return true;
89+
return true;
90+
}
91+
if (_isIgnoredError(event, options.ignoreErrors)) {
92+
if (isDebugBuild()) {
93+
logger.warn(
94+
`Event dropped due to being matched by \`ignoreErrors\` option.\nEvent: ${getEventDescription(event)}`,
95+
);
7996
}
80-
if (this._isDeniedUrl(event, options)) {
81-
if (isDebugBuild()) {
82-
logger.warn(
83-
`Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
84-
event,
85-
)}.\nUrl: ${this._getEventFilterUrl(event)}`,
86-
);
87-
}
88-
return true;
97+
return true;
98+
}
99+
if (_isDeniedUrl(event, options.denyUrls)) {
100+
if (isDebugBuild()) {
101+
logger.warn(
102+
`Event dropped due to being matched by \`denyUrls\` option.\nEvent: ${getEventDescription(
103+
event,
104+
)}.\nUrl: ${_getEventFilterUrl(event)}`,
105+
);
89106
}
90-
if (!this._isAllowedUrl(event, options)) {
91-
if (isDebugBuild()) {
92-
logger.warn(
93-
`Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
94-
event,
95-
)}.\nUrl: ${this._getEventFilterUrl(event)}`,
96-
);
97-
}
98-
return true;
107+
return true;
108+
}
109+
if (!_isAllowedUrl(event, options.allowUrls)) {
110+
if (isDebugBuild()) {
111+
logger.warn(
112+
`Event dropped due to not being matched by \`allowUrls\` option.\nEvent: ${getEventDescription(
113+
event,
114+
)}.\nUrl: ${_getEventFilterUrl(event)}`,
115+
);
99116
}
100-
return false;
117+
return true;
101118
}
119+
return false;
120+
}
102121

103-
/** JSDoc */
104-
private _isSentryError(event: Event, options: Partial<InboundFiltersOptions>): boolean {
105-
if (!options.ignoreInternal) {
106-
return false;
107-
}
122+
/** JSDoc */
123+
function _isIgnoredError(event: Event, ignoreErrors: Partial<InboundFiltersOptions>['ignoreErrors']): boolean {
124+
if (!ignoreErrors || !ignoreErrors.length) {
125+
return false;
126+
}
108127

109-
try {
110-
// @ts-ignore can't be a sentry error if undefined
111-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
112-
return event.exception.values[0].type === 'SentryError';
113-
} catch (e) {
114-
// ignore
115-
}
128+
return _getPossibleEventMessages(event).some(message =>
129+
ignoreErrors.some(pattern => isMatchingPattern(message, pattern)),
130+
);
131+
}
116132

133+
/** JSDoc */
134+
function _isDeniedUrl(event: Event, denyUrls: Partial<InboundFiltersOptions>['denyUrls']): boolean {
135+
// TODO: Use Glob instead?
136+
if (!denyUrls || !denyUrls.length) {
117137
return false;
118138
}
139+
const url = _getEventFilterUrl(event);
140+
return !url ? false : denyUrls.some(pattern => isMatchingPattern(url, pattern));
141+
}
119142

120-
/** JSDoc */
121-
private _isIgnoredError(event: Event, options: Partial<InboundFiltersOptions>): boolean {
122-
if (!options.ignoreErrors || !options.ignoreErrors.length) {
123-
return false;
124-
}
125-
126-
return this._getPossibleEventMessages(event).some(message =>
127-
// Not sure why TypeScript complains here...
128-
(options.ignoreErrors as Array<RegExp | string>).some(pattern => isMatchingPattern(message, pattern)),
129-
);
143+
/** JSDoc */
144+
function _isAllowedUrl(event: Event, allowUrls: Partial<InboundFiltersOptions>['allowUrls']): boolean {
145+
// TODO: Use Glob instead?
146+
if (!allowUrls || !allowUrls.length) {
147+
return true;
130148
}
149+
const url = _getEventFilterUrl(event);
150+
return !url ? true : allowUrls.some(pattern => isMatchingPattern(url, pattern));
151+
}
131152

132-
/** JSDoc */
133-
private _isDeniedUrl(event: Event, options: Partial<InboundFiltersOptions>): boolean {
134-
// TODO: Use Glob instead?
135-
if (!options.denyUrls || !options.denyUrls.length) {
136-
return false;
153+
/** JSDoc */
154+
function _getPossibleEventMessages(event: Event): string[] {
155+
if (event.message) {
156+
return [event.message];
157+
}
158+
if (event.exception) {
159+
try {
160+
const { type = '', value = '' } = (event.exception.values && event.exception.values[0]) || {};
161+
return [`${value}`, `${type}: ${value}`];
162+
} catch (oO) {
163+
if (isDebugBuild()) {
164+
logger.error(`Cannot extract message for event ${getEventDescription(event)}`);
165+
}
166+
return [];
137167
}
138-
const url = this._getEventFilterUrl(event);
139-
return !url ? false : options.denyUrls.some(pattern => isMatchingPattern(url, pattern));
140168
}
169+
return [];
170+
}
141171

142-
/** JSDoc */
143-
private _isAllowedUrl(event: Event, options: Partial<InboundFiltersOptions>): boolean {
144-
// TODO: Use Glob instead?
145-
if (!options.allowUrls || !options.allowUrls.length) {
146-
return true;
172+
/** JSDoc */
173+
function _isSentryError(event: Event, ignoreInternal: Partial<InboundFiltersOptions>['ignoreInternal']): boolean {
174+
if (ignoreInternal) {
175+
try {
176+
// @ts-ignore can't be a sentry error if undefined
177+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
178+
return event.exception.values[0].type === 'SentryError';
179+
} catch (e) {
180+
// ignore
147181
}
148-
const url = this._getEventFilterUrl(event);
149-
return !url ? true : options.allowUrls.some(pattern => isMatchingPattern(url, pattern));
150182
}
183+
return false;
184+
}
151185

152-
/** JSDoc */
153-
private _mergeOptions(clientOptions: Partial<InboundFiltersOptions> = {}): Partial<InboundFiltersOptions> {
154-
return {
155-
allowUrls: [
156-
// eslint-disable-next-line deprecation/deprecation
157-
...(this._options.whitelistUrls || []),
158-
...(this._options.allowUrls || []),
159-
// eslint-disable-next-line deprecation/deprecation
160-
...(clientOptions.whitelistUrls || []),
161-
...(clientOptions.allowUrls || []),
162-
],
163-
denyUrls: [
164-
// eslint-disable-next-line deprecation/deprecation
165-
...(this._options.blacklistUrls || []),
166-
...(this._options.denyUrls || []),
167-
// eslint-disable-next-line deprecation/deprecation
168-
...(clientOptions.blacklistUrls || []),
169-
...(clientOptions.denyUrls || []),
170-
],
171-
ignoreErrors: [
172-
...(this._options.ignoreErrors || []),
173-
...(clientOptions.ignoreErrors || []),
174-
...DEFAULT_IGNORE_ERRORS,
175-
],
176-
ignoreInternal: typeof this._options.ignoreInternal !== 'undefined' ? this._options.ignoreInternal : true,
177-
};
178-
}
186+
/** JSDoc */
187+
function _getLastValidUrl(frames: StackFrame[] = []): string | null {
188+
for (let i = frames.length - 1; i >= 0; i--) {
189+
const frame = frames[i];
179190

180-
/** JSDoc */
181-
private _getPossibleEventMessages(event: Event): string[] {
182-
if (event.message) {
183-
return [event.message];
184-
}
185-
if (event.exception) {
186-
try {
187-
const { type = '', value = '' } = (event.exception.values && event.exception.values[0]) || {};
188-
return [`${value}`, `${type}: ${value}`];
189-
} catch (oO) {
190-
if (isDebugBuild()) {
191-
logger.error(`Cannot extract message for event ${getEventDescription(event)}`);
192-
}
193-
return [];
194-
}
191+
if (frame && frame.filename !== '<anonymous>' && frame.filename !== '[native code]') {
192+
return frame.filename || null;
195193
}
196-
return [];
197194
}
198195

199-
/** JSDoc */
200-
private _getLastValidUrl(frames: StackFrame[] = []): string | null {
201-
for (let i = frames.length - 1; i >= 0; i--) {
202-
const frame = frames[i];
196+
return null;
197+
}
203198

204-
if (frame && frame.filename !== '<anonymous>' && frame.filename !== '[native code]') {
205-
return frame.filename || null;
206-
}
199+
/** JSDoc */
200+
function _getEventFilterUrl(event: Event): string | null {
201+
try {
202+
if (event.stacktrace) {
203+
return _getLastValidUrl(event.stacktrace.frames);
207204
}
208-
209-
return null;
210-
}
211-
212-
/** JSDoc */
213-
private _getEventFilterUrl(event: Event): string | null {
205+
let frames;
214206
try {
215-
if (event.stacktrace) {
216-
return this._getLastValidUrl(event.stacktrace.frames);
217-
}
218-
let frames;
219-
try {
220-
// @ts-ignore we only care about frames if the whole thing here is defined
221-
frames = event.exception.values[0].stacktrace.frames;
222-
} catch (e) {
223-
// ignore
224-
}
225-
return frames ? this._getLastValidUrl(frames) : null;
226-
} catch (oO) {
227-
if (isDebugBuild()) {
228-
logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
229-
}
230-
return null;
207+
// @ts-ignore we only care about frames if the whole thing here is defined
208+
frames = event.exception.values[0].stacktrace.frames;
209+
} catch (e) {
210+
// ignore
231211
}
212+
return frames ? _getLastValidUrl(frames) : null;
213+
} catch (oO) {
214+
if (isDebugBuild()) {
215+
logger.error(`Cannot extract url for event ${getEventDescription(event)}`);
216+
}
217+
return null;
232218
}
233219
}

0 commit comments

Comments
 (0)