Skip to content

Commit 00ca435

Browse files
committed
ref: implemented includes, assign and isNaN polyfills
1 parent 1ff49f4 commit 00ca435

File tree

8 files changed

+70
-12
lines changed

8 files changed

+70
-12
lines changed

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Integration, Severity } from '@sentry/types';
33
import { isFunction, isString } from '@sentry/utils/is';
44
import { getEventDescription, getGlobalObject, parseUrl } from '@sentry/utils/misc';
55
import { deserialize, fill } from '@sentry/utils/object';
6-
import { safeJoin } from '@sentry/utils/string';
6+
import { includes, safeJoin } from '@sentry/utils/string';
77
import { supportsBeacon, supportsHistory, supportsNativeFetch } from '@sentry/utils/supports';
88
import { BrowserOptions } from '../backend';
99
import { breadcrumbEventHandler, keypressEventHandler, wrap } from './helpers';
@@ -97,7 +97,7 @@ export class Breadcrumbs implements Integration {
9797

9898
// if Sentry key appears in URL, don't capture it as a request
9999
// but rather as our own 'sentry' type breadcrumb
100-
if (options.filterUrl && url.includes(options.filterUrl)) {
100+
if (options.filterUrl && includes(url, options.filterUrl)) {
101101
addSentryBreadcrumb(data);
102102
return result;
103103
}
@@ -210,7 +210,7 @@ export class Breadcrumbs implements Integration {
210210

211211
// if Sentry key appears in URL, don't capture it as a request
212212
// but rather as our own 'sentry' type breadcrumb
213-
if (options.filterUrl && url.includes(options.filterUrl)) {
213+
if (options.filterUrl && includes(url, options.filterUrl)) {
214214
if (method === 'POST' && args[1] && args[1].body) {
215215
addSentryBreadcrumb(args[1].body);
216216
}
@@ -371,7 +371,7 @@ export class Breadcrumbs implements Integration {
371371
};
372372
// if Sentry key appears in URL, don't capture it as a request
373373
// but rather as our own 'sentry' type breadcrumb
374-
if (isString(url) && (options.filterUrl && url.includes(options.filterUrl))) {
374+
if (isString(url) && (options.filterUrl && includes(url, options.filterUrl))) {
375375
this.__sentry_own_request__ = true;
376376
}
377377
return originalOpen.apply(this, args);

packages/browser/src/parsers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SentryEvent, SentryException, StackFrame } from '@sentry/types';
22
import { limitObjectDepthToSize, serializeKeysToEventMessage } from '@sentry/utils/object';
3+
import { includes } from '@sentry/utils/string';
34
import { md5 } from './md5';
45
import { computeStackTrace, StackFrame as TraceKitStackFrame, StackTrace as TraceKitStackTrace } from './tracekit';
56

@@ -65,7 +66,7 @@ export function prepareFramesForEvent(stack: TraceKitStackFrame[]): StackFrame[]
6566
let localStack = stack;
6667
const firstFrameFunction = localStack[0].func || '';
6768

68-
if (firstFrameFunction.includes('captureMessage') || firstFrameFunction.includes('captureException')) {
69+
if (includes(firstFrameFunction, 'captureMessage') || includes(firstFrameFunction, 'captureException')) {
6970
localStack = localStack.slice(1);
7071
}
7172

packages/core/src/dsn.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { DsnComponents, DsnLike, DsnProtocol } from '@sentry/types';
2+
import { isNaN } from '@sentry/utils/is';
3+
import { assign } from '@sentry/utils/object';
24
import { SentryError } from './error';
35

46
/** Regular expression used to parse a Dsn. */
@@ -65,7 +67,7 @@ export class Dsn implements DsnComponents {
6567
path = split.slice(0, -1).join('/');
6668
projectId = split.pop() as string;
6769
}
68-
Object.assign(this, { host, pass, path, projectId, port, protocol, user });
70+
assign(this, { host, pass, path, projectId, port, protocol, user });
6971
}
7072

7173
/** Maps Dsn components into this instance. */

packages/core/src/integrations/inboundfilters.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { configureScope } from '@sentry/minimal';
22
import { Integration, SentryEvent } from '@sentry/types';
33
import { isRegExp } from '@sentry/utils/is';
44
import { getEventDescription } from '@sentry/utils/misc';
5+
import { includes } from '@sentry/utils/string';
56
import { logger } from '../logger';
67

78
// "Script error." is hard coded into browsers for errors that it can't read.
@@ -108,7 +109,7 @@ export class InboundFilters implements Integration {
108109
if (isRegExp(pattern)) {
109110
return (pattern as RegExp).test(value);
110111
} else if (typeof pattern === 'string') {
111-
return value.includes(pattern);
112+
return includes(value, pattern);
112113
} else {
113114
return false;
114115
}

packages/hub/src/scope.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Breadcrumb, SentryEvent, SentryEventHint, Severity, User } from '@sentry/types';
2+
import { assign } from '@sentry/utils/object';
23

34
/**
45
* Holds additional event information. {@link Scope.applyToEvent} will be
@@ -134,7 +135,7 @@ export class Scope {
134135
*/
135136
public static clone(scope?: Scope): Scope {
136137
const newScope = new Scope();
137-
Object.assign(newScope, scope, {
138+
assign(newScope, scope, {
138139
scopeListeners: [],
139140
});
140141
if (scope) {

packages/utils/src/is.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,14 @@ export function isPlainObject(wat: any): boolean {
116116
export function isRegExp(wat: any): boolean {
117117
return Object.prototype.toString.call(wat) === '[object RegExp]';
118118
}
119+
120+
/**
121+
* Checks whether given value's type is a NaN
122+
* {@link isNaN}.
123+
*
124+
* @param wat A value to be checked.
125+
* @returns A boolean representing the result.
126+
*/
127+
export function isNaN(wat: any): boolean {
128+
return wat !== wat;
129+
}

packages/utils/src/object.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SentryWrappedFunction } from '@sentry/types';
2-
import { isPlainObject, isUndefined } from './is';
2+
import { isNaN, isPlainObject, isUndefined } from './is';
33

44
/**
55
* Just an Error object with arbitrary attributes attached to it.
@@ -59,8 +59,7 @@ function serializer(): (key: string, value: any) => any {
5959
let currentValue: any = value;
6060

6161
// NaN and undefined are not JSON.parseable, but we want to preserve this information
62-
// tslint:disable-next-line:no-unsafe-any
63-
if (Number.isNaN(value)) {
62+
if (isNaN(value)) {
6463
currentValue = NAN_VALUE;
6564
} else if (isUndefined(value)) {
6665
currentValue = UNDEFINED_VALUE;
@@ -207,7 +206,7 @@ function serializeValue<T>(value: T): T | string {
207206
return value.length <= maxLength ? value : `${value.substr(0, maxLength - 1)}\u2026`;
208207
} else if (typeof value === 'number' || typeof value === 'boolean' || typeof value === 'undefined') {
209208
return value;
210-
} else if (Number.isNaN(value as any)) {
209+
} else if (isNaN(value)) {
211210
// NaN and undefined are not JSON.parseable, but we want to preserve this information
212211
return '[NaN]';
213212
} else if (isUndefined(value)) {
@@ -294,3 +293,31 @@ export function serializeKeysToEventMessage(keys: string[], maxLength: number =
294293

295294
return '';
296295
}
296+
297+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill
298+
/** JSDoc */
299+
export function assign(target: any, ...args: any[]): object {
300+
if (target === null || target === undefined) {
301+
throw new TypeError('Cannot convert undefined or null to object');
302+
}
303+
304+
const to = Object(target) as {
305+
[key: string]: any;
306+
};
307+
308+
for (const source of args) {
309+
if (source !== null) {
310+
for (const nextKey in source as {
311+
[key: string]: any;
312+
}) {
313+
if (Object.prototype.hasOwnProperty.call(source, nextKey)) {
314+
to[nextKey] = (source as {
315+
[key: string]: any;
316+
})[nextKey];
317+
}
318+
}
319+
}
320+
}
321+
322+
return to;
323+
}

packages/utils/src/string.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,18 @@ export function safeJoin(input: any[], delimiter?: string): string {
8181

8282
return output.join(delimiter);
8383
}
84+
85+
/**
86+
* Checks if given value is included in the target
87+
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Polyfill
88+
* @param target source string
89+
* @param search string to be looked for
90+
* @returns An answer
91+
*/
92+
export function includes(target: string, search: string): boolean {
93+
if (search.length > target.length) {
94+
return false;
95+
} else {
96+
return target.indexOf(search) !== -1;
97+
}
98+
}

0 commit comments

Comments
 (0)