Skip to content

Commit 938ab2d

Browse files
HazATLms24
andauthored
ref(core): Simplify core utility functions for smaller bundle (#19854)
## Summary Small, safe simplifications across core utilities. Combined saves **~80 bytes gzipped**. ## Changes - **envelope.ts**: Slim `ITEM_TYPE_TO_DATA_CATEGORY_MAP` by removing 7 self-mapping entries (e.g. `session: "session"`). Falls back to the type name itself. - **object.ts**: Replace `getOwnProperties` manual `for...in` + `hasOwnProperty` loop with `Object.fromEntries(Object.entries(obj))`. Use shorthand `value` in `addNonEnumerableProperty`. - **baggage.ts**: Use `.startsWith()` instead of `.match(regex)` for sentry prefix check. - **browser.ts**: Inline `allowedAttrs` array literal directly in the `for...of` loop. - **eventFilters.ts**: Convert verbose `DEFAULT_IGNORE_ERRORS` string literals to shorter regex patterns with equivalent matching behavior (vv().getRestrictions, simulateEvent, solana, _AutofillCallbackHandler). All changes are behavior-preserving. Part of #19833. Co-Authored-By: Claude claude@anthropic.com --------- Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io>
1 parent 3bb4325 commit 938ab2d

File tree

5 files changed

+19
-26
lines changed

5 files changed

+19
-26
lines changed

packages/core/src/integrations/eventFilters.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const DEFAULT_IGNORE_ERRORS = [
1717
/^Cannot redefine property: googletag$/, // This is thrown when google tag manager is used in combination with an ad blocker
1818
/^Can't find variable: gmo$/, // Error from Google Search App https://issuetracker.google.com/issues/396043331
1919
/^undefined is not an object \(evaluating 'a\.[A-Z]'\)$/, // Random error that happens but not actionable or noticeable to end-users.
20-
'can\'t redefine non-configurable property "solana"', // Probably a browser extension or custom browser (Brave) throwing this error
21-
"vv().getRestrictions is not a function. (In 'vv().getRestrictions(1,a)', 'vv().getRestrictions' is undefined)", // Error thrown by GTM, seemingly not affecting end-users
22-
"Can't find variable: _AutofillCallbackHandler", // Unactionable error in instagram webview https://developers.facebook.com/community/threads/320013549791141/
23-
/^Non-Error promise rejection captured with value: Object Not Found Matching Id:\d+, MethodName:simulateEvent, ParamCount:\d+$/, // unactionable error from CEFSharp, a .NET library that embeds chromium in .NET apps
20+
/can't redefine non-configurable property "solana"/, // Probably a browser extension or custom browser (Brave) throwing this error
21+
/vv\(\)\.getRestrictions is not a function/, // Error thrown by GTM, seemingly not affecting end-users
22+
/Can't find variable: _AutofillCallbackHandler/, // Unactionable error in instagram webview https://developers.facebook.com/community/threads/320013549791141/
23+
/Object Not Found Matching Id:\d+, MethodName:simulateEvent/, // unactionable error from CEFSharp, a .NET library that embeds chromium in .NET apps
2424
/^Java exception was raised during method invocation$/, // error from Facebook Mobile browser (https://github.com/getsentry/sentry-javascript/issues/15065)
2525
];
2626

packages/core/src/utils/baggage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function baggageHeaderToDynamicSamplingContext(
3333

3434
// Read all "sentry-" prefixed values out of the baggage object and put it onto a dynamic sampling context object.
3535
const dynamicSamplingContext = Object.entries(baggageObject).reduce<Record<string, string>>((acc, [key, value]) => {
36-
if (key.match(SENTRY_BAGGAGE_KEY_PREFIX_REGEX)) {
36+
if (key.startsWith(SENTRY_BAGGAGE_KEY_PREFIX)) {
3737
const nonPrefixedKey = key.slice(SENTRY_BAGGAGE_KEY_PREFIX.length);
3838
acc[nonPrefixedKey] = value;
3939
}

packages/core/src/utils/browser.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ function _htmlElementAsString(el: unknown, keyAttrs?: string[]): string {
117117
}
118118
}
119119
}
120-
const allowedAttrs = ['aria-label', 'type', 'name', 'title', 'alt'];
121-
for (const k of allowedAttrs) {
120+
for (const k of ['aria-label', 'type', 'name', 'title', 'alt']) {
122121
const attr = elem.getAttribute(k);
123122
if (attr) {
124123
out.push(`[${k}="${attr}"]`);

packages/core/src/utils/envelope.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,32 +204,33 @@ export function createAttachmentEnvelopeItem(attachment: Attachment): Attachment
204204
];
205205
}
206206

207-
const ITEM_TYPE_TO_DATA_CATEGORY_MAP: Record<EnvelopeItemType, DataCategory> = {
208-
session: 'session',
207+
type OverriddenItemType = Exclude<EnvelopeItemType, DataCategory>;
208+
209+
// Map of envelope item types to data categories where the category differs from the type.
210+
// Types that map to themselves (session, attachment, transaction, profile, feedback, span, metric) fall through.
211+
const DATA_CATEGORY_OVERRIDES: Record<OverriddenItemType, DataCategory> = {
209212
sessions: 'session',
210-
attachment: 'attachment',
211-
transaction: 'transaction',
212213
event: 'error',
213214
client_report: 'internal',
214215
user_report: 'default',
215-
profile: 'profile',
216216
profile_chunk: 'profile',
217217
replay_event: 'replay',
218218
replay_recording: 'replay',
219219
check_in: 'monitor',
220-
feedback: 'feedback',
221-
span: 'span',
222220
raw_security: 'security',
223221
log: 'log_item',
224-
metric: 'metric',
225222
trace_metric: 'metric',
226223
};
227224

225+
function _isOverriddenType(type: EnvelopeItemType): type is OverriddenItemType {
226+
return type in DATA_CATEGORY_OVERRIDES;
227+
}
228+
228229
/**
229230
* Maps the type of an envelope item to a data category.
230231
*/
231232
export function envelopeItemTypeToDataCategory(type: EnvelopeItemType): DataCategory {
232-
return ITEM_TYPE_TO_DATA_CATEGORY_MAP[type];
233+
return _isOverriddenType(type) ? DATA_CATEGORY_OVERRIDES[type] : type;
233234
}
234235

235236
/** Extracts the minimal SDK info from the metadata or an events */

packages/core/src/utils/object.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export function addNonEnumerableProperty(obj: object, name: string, value: unkno
5656
try {
5757
Object.defineProperty(obj, name, {
5858
// enumerable: false, // the default, so we can save on bundle size by not explicitly setting it
59-
value: value,
59+
value,
6060
writable: true,
6161
configurable: true,
6262
});
@@ -158,16 +158,9 @@ function serializeEventTarget(target: unknown): string {
158158
/** Filters out all but an object's own properties */
159159
function getOwnProperties(obj: unknown): { [key: string]: unknown } {
160160
if (typeof obj === 'object' && obj !== null) {
161-
const extractedProps: { [key: string]: unknown } = {};
162-
for (const property in obj) {
163-
if (Object.prototype.hasOwnProperty.call(obj, property)) {
164-
extractedProps[property] = (obj as Record<string, unknown>)[property];
165-
}
166-
}
167-
return extractedProps;
168-
} else {
169-
return {};
161+
return Object.fromEntries(Object.entries(obj));
170162
}
163+
return {};
171164
}
172165

173166
/**

0 commit comments

Comments
 (0)