Skip to content

Commit 8aca02d

Browse files
authored
ref(browser): Move utils to @sentry-internal/browser-utils (#11451)
ref #9832 Building off #11381, this moves browser related utils into the browser utils package. Next step is to move `browserTracingIntegration` into browser!
1 parent a1e4efe commit 8aca02d

34 files changed

+105
-114
lines changed

packages/browser-utils/.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module.exports = {
22
extends: ['../../.eslintrc.js'],
3+
env: {
4+
browser: true,
5+
},
36
overrides: [
47
{
58
files: ['src/**'],

packages/browser-utils/src/browser/backgroundtab.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SPAN_STATUS_ERROR, getActiveSpan, getRootSpan } from '@sentry/core';
22
import { spanToJSON } from '@sentry/core';
33
import { logger } from '@sentry/utils';
44

5-
import { DEBUG_BUILD } from '../common/debug-build';
5+
import { DEBUG_BUILD } from '../debug-build';
66
import { WINDOW } from './types';
77

88
/**

packages/browser-utils/src/browser/browserTracingIntegration.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,10 @@ import {
1616
} from '@sentry/core';
1717
import type { Client, IntegrationFn, StartSpanOptions, TransactionSource } from '@sentry/types';
1818
import type { Span } from '@sentry/types';
19-
import {
20-
addHistoryInstrumentationHandler,
21-
browserPerformanceTimeOrigin,
22-
getDomElement,
23-
logger,
24-
uuid4,
25-
} from '@sentry/utils';
26-
27-
import { DEBUG_BUILD } from '../common/debug-build';
19+
import { browserPerformanceTimeOrigin, getDomElement, logger, uuid4 } from '@sentry/utils';
20+
21+
import { DEBUG_BUILD } from '../debug-build';
22+
import { addHistoryInstrumentationHandler } from '../instrument/history';
2823
import { registerBackgroundTabDetection } from './backgroundtab';
2924
import {
3025
addPerformanceEntries,

packages/browser-utils/src/browser/instrument.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getFunctionName, logger } from '@sentry/utils';
22

3-
import { DEBUG_BUILD } from '../common/debug-build';
3+
import { DEBUG_BUILD } from '../debug-build';
44
import { onCLS } from './web-vitals/getCLS';
55
import { onFID } from './web-vitals/getFID';
66
import { onLCP } from './web-vitals/getLCP';

packages/browser-utils/src/browser/metrics/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Measurements, Span, SpanAttributes, StartSpanOptions } from '@sent
55
import { browserPerformanceTimeOrigin, getComponentName, htmlTreeAsString, logger, parseUrl } from '@sentry/utils';
66

77
import { spanToJSON } from '@sentry/core';
8-
import { DEBUG_BUILD } from '../../common/debug-build';
8+
import { DEBUG_BUILD } from '../../debug-build';
99
import {
1010
addClsInstrumentationHandler,
1111
addFidInstrumentationHandler,

packages/browser-utils/src/browser/request.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ import {
1616
import type { HandlerDataXhr, SentryWrappedXMLHttpRequest, Span } from '@sentry/types';
1717
import {
1818
BAGGAGE_HEADER_NAME,
19-
SENTRY_XHR_DATA_KEY,
2019
addFetchInstrumentationHandler,
21-
addXhrInstrumentationHandler,
2220
browserPerformanceTimeOrigin,
2321
dynamicSamplingContextToSentryBaggageHeader,
2422
generateSentryTraceHeader,
2523
stringMatchesSomePattern,
2624
} from '@sentry/utils';
25+
import { SENTRY_XHR_DATA_KEY, addXhrInstrumentationHandler } from '../instrument/xhr';
2726

2827
import { addPerformanceInstrumentationHandler } from './instrument';
2928
import { WINDOW } from './types';

packages/browser-utils/src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,13 @@ export {
1212
addLcpInstrumentationHandler,
1313
} from './browser';
1414

15+
export { addClickKeypressInstrumentationHandler } from './instrument/dom';
16+
17+
export { addHistoryInstrumentationHandler } from './instrument/history';
18+
19+
export {
20+
addXhrInstrumentationHandler,
21+
SENTRY_XHR_DATA_KEY,
22+
} from './instrument/xhr';
23+
1524
export type { RequestInstrumentationOptions } from './browser';

packages/utils/src/instrument/dom.ts renamed to packages/browser-utils/src/instrument/dom.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
// TODO(v8): Move everything in this file into the browser package. Nothing here is generic and we run risk of leaking browser types into non-browser packages.
2-
3-
/* eslint-disable @typescript-eslint/no-explicit-any */
4-
/* eslint-disable @typescript-eslint/ban-types */
51
import type { HandlerDataDom } from '@sentry/types';
62

7-
import { uuid4 } from '../misc';
8-
import { addNonEnumerableProperty, fill } from '../object';
9-
import { GLOBAL_OBJ } from '../worldwide';
10-
import { addHandler, maybeInstrument, triggerHandlers } from './_handlers';
3+
import { addHandler, addNonEnumerableProperty, fill, maybeInstrument, triggerHandlers, uuid4 } from '@sentry/utils';
4+
import { WINDOW } from '../browser/types';
115

126
type SentryWrappedTarget = HTMLElement & { _sentryId?: string };
137

@@ -25,14 +19,13 @@ type RemoveEventListener = (
2519
type InstrumentedElement = Element & {
2620
__sentry_instrumentation_handlers__?: {
2721
[key in 'click' | 'keypress']?: {
28-
handler?: Function;
22+
handler?: unknown;
2923
/** The number of custom listeners attached to this element */
3024
refCount: number;
3125
};
3226
};
3327
};
3428

35-
const WINDOW = GLOBAL_OBJ as unknown as Window;
3629
const DEBOUNCE_DURATION = 1000;
3730

3831
let debounceTimerID: number | undefined;
@@ -71,7 +64,7 @@ export function instrumentDOM(): void {
7164
// could potentially prevent the event from bubbling up to our global listeners. This way, our handler are still
7265
// guaranteed to fire at least once.)
7366
['EventTarget', 'Node'].forEach((target: string) => {
74-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
67+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
7568
const proto = (WINDOW as any)[target] && (WINDOW as any)[target].prototype;
7669
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-prototype-builtins
7770
if (!proto || !proto.hasOwnProperty || !proto.hasOwnProperty('addEventListener')) {

packages/utils/src/instrument/history.ts renamed to packages/browser-utils/src/instrument/history.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
// TODO(v8): Move everything in this file into the browser package. Nothing here is generic and we run risk of leaking browser types into non-browser packages.
2-
3-
/* eslint-disable @typescript-eslint/no-explicit-any */
4-
/* eslint-disable @typescript-eslint/ban-types */
51
import type { HandlerDataHistory } from '@sentry/types';
6-
7-
import { fill } from '../object';
8-
import { supportsHistory } from '../supports';
9-
import { GLOBAL_OBJ } from '../worldwide';
10-
import { addHandler, maybeInstrument, triggerHandlers } from './_handlers';
11-
12-
const WINDOW = GLOBAL_OBJ as unknown as Window;
2+
import { addHandler, fill, maybeInstrument, supportsHistory, triggerHandlers } from '@sentry/utils';
3+
import { WINDOW } from '../browser/types';
134

145
let lastHref: string | undefined;
156

@@ -33,7 +24,7 @@ function instrumentHistory(): void {
3324
}
3425

3526
const oldOnPopState = WINDOW.onpopstate;
36-
WINDOW.onpopstate = function (this: WindowEventHandlers, ...args: any[]): any {
27+
WINDOW.onpopstate = function (this: WindowEventHandlers, ...args: unknown[]) {
3728
const to = WINDOW.location.href;
3829
// keep track of the current URL state, as we always receive only the updated state
3930
const from = lastHref;
@@ -53,7 +44,7 @@ function instrumentHistory(): void {
5344
};
5445

5546
function historyReplacementFunction(originalHistoryFunction: () => void): () => void {
56-
return function (this: History, ...args: any[]): void {
47+
return function (this: History, ...args: unknown[]): void {
5748
const url = args.length > 2 ? args[2] : undefined;
5849
if (url) {
5950
// coerce to string (this is what pushState does)

0 commit comments

Comments
 (0)