-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
misc.ts
428 lines (377 loc) · 11.3 KB
/
misc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import { Event, Integration, WrappedFunction } from '@sentry/types';
import { isString } from './is';
/** Internal */
interface SentryGlobal {
Sentry?: {
Integrations?: Integration[];
};
SENTRY_ENVIRONMENT?: string;
SENTRY_DSN?: string;
SENTRY_RELEASE?: {
id?: string;
};
__SENTRY__: {
globalEventProcessors: any;
hub: any;
logger: any;
};
}
/**
* Requires a module which is protected _against bundler minification.
*
* @param request The module path to resolve
*/
export function dynamicRequire(mod: any, request: string): any {
// tslint:disable-next-line: no-unsafe-any
return mod.require(request);
}
/**
* Checks whether we're in the Node.js or Browser environment
*
* @returns Answer to given question
*/
export function isNodeEnv(): boolean {
// tslint:disable:strict-type-predicates
return Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
}
const fallbackGlobalObject = {};
/**
* Safely get global scope object
*
* @returns Global scope object
*/
export function getGlobalObject<T>(): T & SentryGlobal {
return (isNodeEnv()
? global
: typeof window !== 'undefined'
? window
: typeof self !== 'undefined'
? self
: fallbackGlobalObject) as T & SentryGlobal;
}
// tslint:enable:strict-type-predicates
/**
* Extended Window interface that allows for Crypto API usage in IE browsers
*/
interface MsCryptoWindow extends Window {
msCrypto?: Crypto;
}
/**
* UUID4 generator
*
* @returns string Generated UUID4.
*/
export function uuid4(): string {
const global = getGlobalObject() as MsCryptoWindow;
const crypto = global.crypto || global.msCrypto;
if (!(crypto === void 0) && crypto.getRandomValues) {
// Use window.crypto API if available
const arr = new Uint16Array(8);
crypto.getRandomValues(arr);
// set 4 in byte 7
// tslint:disable-next-line:no-bitwise
arr[3] = (arr[3] & 0xfff) | 0x4000;
// set 2 most significant bits of byte 9 to '10'
// tslint:disable-next-line:no-bitwise
arr[4] = (arr[4] & 0x3fff) | 0x8000;
const pad = (num: number): string => {
let v = num.toString(16);
while (v.length < 4) {
v = `0${v}`;
}
return v;
};
return (
pad(arr[0]) + pad(arr[1]) + pad(arr[2]) + pad(arr[3]) + pad(arr[4]) + pad(arr[5]) + pad(arr[6]) + pad(arr[7])
);
}
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523
return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, c => {
// tslint:disable-next-line:no-bitwise
const r = (Math.random() * 16) | 0;
// tslint:disable-next-line:no-bitwise
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
/**
* Parses string form of URL into an object
* // borrowed from https://tools.ietf.org/html/rfc3986#appendix-B
* // intentionally using regex and not <a/> href parsing trick because React Native and other
* // environments where DOM might not be available
* @returns parsed URL object
*/
export function parseUrl(
url: string,
): {
host?: string;
path?: string;
protocol?: string;
relative?: string;
} {
if (!url) {
return {};
}
const match = url.match(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/);
if (!match) {
return {};
}
// coerce to undefined values to empty string so we don't get 'undefined'
const query = match[6] || '';
const fragment = match[8] || '';
return {
host: match[4],
path: match[5],
protocol: match[2],
relative: match[5] + query + fragment, // everything minus origin
};
}
/**
* Extracts either message or type+value from an event that can be used for user-facing logs
* @returns event's description
*/
export function getEventDescription(event: Event): string {
if (event.message) {
return event.message;
}
if (event.exception && event.exception.values && event.exception.values[0]) {
const exception = event.exception.values[0];
if (exception.type && exception.value) {
return `${exception.type}: ${exception.value}`;
}
return exception.type || exception.value || event.event_id || '<unknown>';
}
return event.event_id || '<unknown>';
}
/** JSDoc */
interface ExtensibleConsole extends Console {
[key: string]: any;
}
/** JSDoc */
export function consoleSandbox(callback: () => any): any {
const global = getGlobalObject<Window>();
const levels = ['debug', 'info', 'warn', 'error', 'log', 'assert'];
if (!('console' in global)) {
return callback();
}
const originalConsole = global.console as ExtensibleConsole;
const wrappedLevels: { [key: string]: any } = {};
// Restore all wrapped console methods
levels.forEach(level => {
if (level in global.console && (originalConsole[level] as WrappedFunction).__sentry_original__) {
wrappedLevels[level] = originalConsole[level] as WrappedFunction;
originalConsole[level] = (originalConsole[level] as WrappedFunction).__sentry_original__;
}
});
// Perform callback manipulations
const result = callback();
// Revert restoration to wrapped state
Object.keys(wrappedLevels).forEach(level => {
originalConsole[level] = wrappedLevels[level];
});
return result;
}
/**
* Adds exception values, type and value to an synthetic Exception.
* @param event The event to modify.
* @param value Value of the exception.
* @param type Type of the exception.
* @hidden
*/
export function addExceptionTypeValue(event: Event, value?: string, type?: string): void {
event.exception = event.exception || {};
event.exception.values = event.exception.values || [];
event.exception.values[0] = event.exception.values[0] || {};
event.exception.values[0].value = event.exception.values[0].value || value || '';
event.exception.values[0].type = event.exception.values[0].type || type || 'Error';
}
/**
* Adds exception mechanism to a given event.
* @param event The event to modify.
* @param mechanism Mechanism of the mechanism.
* @hidden
*/
export function addExceptionMechanism(
event: Event,
mechanism: {
[key: string]: any;
} = {},
): void {
// TODO: Use real type with `keyof Mechanism` thingy and maybe make it better?
try {
// @ts-ignore
// tslint:disable:no-non-null-assertion
event.exception!.values![0].mechanism = event.exception!.values![0].mechanism || {};
Object.keys(mechanism).forEach(key => {
// @ts-ignore
event.exception!.values![0].mechanism[key] = mechanism[key];
});
} catch (_oO) {
// no-empty
}
}
/**
* A safe form of location.href
*/
export function getLocationHref(): string {
try {
return document.location.href;
} catch (oO) {
return '';
}
}
/**
* Given a child DOM element, returns a query-selector statement describing that
* and its ancestors
* e.g. [HTMLElement] => body > div > input#foo.btn[name=baz]
* @returns generated DOM path
*/
export function htmlTreeAsString(elem: unknown): string {
type SimpleNode = {
parentNode: SimpleNode;
} | null;
// try/catch both:
// - accessing event.target (see getsentry/raven-js#838, #768)
// - `htmlTreeAsString` because it's complex, and just accessing the DOM incorrectly
// - can throw an exception in some circumstances.
try {
let currentElem = elem as SimpleNode;
const MAX_TRAVERSE_HEIGHT = 5;
const MAX_OUTPUT_LEN = 80;
const out = [];
let height = 0;
let len = 0;
const separator = ' > ';
const sepLength = separator.length;
let nextStr;
while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) {
nextStr = _htmlElementAsString(currentElem);
// bail out if
// - nextStr is the 'html' element
// - the length of the string that would be created exceeds MAX_OUTPUT_LEN
// (ignore this limit if we are on the first iteration)
if (nextStr === 'html' || (height > 1 && len + out.length * sepLength + nextStr.length >= MAX_OUTPUT_LEN)) {
break;
}
out.push(nextStr);
len += nextStr.length;
currentElem = currentElem.parentNode;
}
return out.reverse().join(separator);
} catch (_oO) {
return '<unknown>';
}
}
/**
* Returns a simple, query-selector representation of a DOM element
* e.g. [HTMLElement] => input#foo.btn[name=baz]
* @returns generated DOM path
*/
function _htmlElementAsString(el: unknown): string {
const elem = el as {
getAttribute(key: string): string; // tslint:disable-line:completed-docs
tagName?: string;
id?: string;
className?: string;
};
const out = [];
let className;
let classes;
let key;
let attr;
let i;
if (!elem || !elem.tagName) {
return '';
}
out.push(elem.tagName.toLowerCase());
if (elem.id) {
out.push(`#${elem.id}`);
}
className = elem.className;
if (className && isString(className)) {
classes = className.split(/\s+/);
for (i = 0; i < classes.length; i++) {
out.push(`.${classes[i]}`);
}
}
const attrWhitelist = ['type', 'name', 'title', 'alt'];
for (i = 0; i < attrWhitelist.length; i++) {
key = attrWhitelist[i];
attr = elem.getAttribute(key);
if (attr) {
out.push(`[${key}="${attr}"]`);
}
}
return out.join('');
}
/**
* Returns a timestamp in seconds with milliseconds precision.
*/
export function timestampWithMs(): number {
return new Date().getTime() / 1000;
}
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
const SEMVER_REGEXP = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
/**
* Represents Semantic Versioning object
*/
interface SemVer {
major?: number;
minor?: number;
patch?: number;
prerelease?: string;
buildmetadata?: string;
}
/**
* Parses input into a SemVer interface
* @param input string representation of a semver version
*/
export function parseSemver(input: string): SemVer {
const match = input.match(SEMVER_REGEXP) || [];
const major = parseInt(match[1], 10);
const minor = parseInt(match[2], 10);
const patch = parseInt(match[3], 10);
return {
buildmetadata: match[5],
major: isNaN(major) ? undefined : major,
minor: isNaN(minor) ? undefined : minor,
patch: isNaN(patch) ? undefined : patch,
prerelease: match[4],
};
}
const defaultRetryAfter = 60 * 1000; // 60 seconds
/**
* Extracts Retry-After value from the request header or returns default value
* @param now current unix timestamp
* @param header string representation of 'Retry-After' header
*/
export function parseRetryAfterHeader(now: number, header?: string | number | null): number {
if (!header) {
return defaultRetryAfter;
}
const headerDelay = parseInt(`${header}`, 10);
if (!isNaN(headerDelay)) {
return headerDelay * 1000;
}
const headerDate = Date.parse(`${header}`);
if (!isNaN(headerDate)) {
return headerDate - now;
}
return defaultRetryAfter;
}
const defaultFunctionName = '<anonymous>';
/**
* Safely extract function name from itself
*/
export function getFunctionName(fn: unknown): string {
try {
if (!fn || typeof fn !== 'function') {
return defaultFunctionName;
}
return fn.name || defaultFunctionName;
} catch (e) {
// Just accessing custom props in some Selenium environments
// can cause a "Permission denied" exception (see raven-js#495).
return defaultFunctionName;
}
}