diff --git a/packages/browser-utils/.eslintrc.js b/packages/browser-utils/.eslintrc.js index 50f4342a74c6..7346008450be 100644 --- a/packages/browser-utils/.eslintrc.js +++ b/packages/browser-utils/.eslintrc.js @@ -1,5 +1,8 @@ module.exports = { extends: ['../../.eslintrc.js'], + env: { + browser: true, + }, overrides: [ { files: ['src/**'], diff --git a/packages/browser-utils/src/browser/backgroundtab.ts b/packages/browser-utils/src/browser/backgroundtab.ts index 928503ba6869..a3ed7d88bd3e 100644 --- a/packages/browser-utils/src/browser/backgroundtab.ts +++ b/packages/browser-utils/src/browser/backgroundtab.ts @@ -2,7 +2,7 @@ import { SPAN_STATUS_ERROR, getActiveSpan, getRootSpan } from '@sentry/core'; import { spanToJSON } from '@sentry/core'; import { logger } from '@sentry/utils'; -import { DEBUG_BUILD } from '../common/debug-build'; +import { DEBUG_BUILD } from '../debug-build'; import { WINDOW } from './types'; /** diff --git a/packages/browser-utils/src/browser/browserTracingIntegration.ts b/packages/browser-utils/src/browser/browserTracingIntegration.ts index 5954ca9d4502..02ea72497a3d 100644 --- a/packages/browser-utils/src/browser/browserTracingIntegration.ts +++ b/packages/browser-utils/src/browser/browserTracingIntegration.ts @@ -16,15 +16,10 @@ import { } from '@sentry/core'; import type { Client, IntegrationFn, StartSpanOptions, TransactionSource } from '@sentry/types'; import type { Span } from '@sentry/types'; -import { - addHistoryInstrumentationHandler, - browserPerformanceTimeOrigin, - getDomElement, - logger, - uuid4, -} from '@sentry/utils'; - -import { DEBUG_BUILD } from '../common/debug-build'; +import { browserPerformanceTimeOrigin, getDomElement, logger, uuid4 } from '@sentry/utils'; + +import { DEBUG_BUILD } from '../debug-build'; +import { addHistoryInstrumentationHandler } from '../instrument/history'; import { registerBackgroundTabDetection } from './backgroundtab'; import { addPerformanceEntries, diff --git a/packages/browser-utils/src/browser/instrument.ts b/packages/browser-utils/src/browser/instrument.ts index e4af4805315a..535f584dddb1 100644 --- a/packages/browser-utils/src/browser/instrument.ts +++ b/packages/browser-utils/src/browser/instrument.ts @@ -1,6 +1,6 @@ import { getFunctionName, logger } from '@sentry/utils'; -import { DEBUG_BUILD } from '../common/debug-build'; +import { DEBUG_BUILD } from '../debug-build'; import { onCLS } from './web-vitals/getCLS'; import { onFID } from './web-vitals/getFID'; import { onLCP } from './web-vitals/getLCP'; diff --git a/packages/browser-utils/src/browser/metrics/index.ts b/packages/browser-utils/src/browser/metrics/index.ts index 770ec8354596..ec3de991d593 100644 --- a/packages/browser-utils/src/browser/metrics/index.ts +++ b/packages/browser-utils/src/browser/metrics/index.ts @@ -5,7 +5,7 @@ import type { Measurements, Span, SpanAttributes, StartSpanOptions } from '@sent import { browserPerformanceTimeOrigin, getComponentName, htmlTreeAsString, logger, parseUrl } from '@sentry/utils'; import { spanToJSON } from '@sentry/core'; -import { DEBUG_BUILD } from '../../common/debug-build'; +import { DEBUG_BUILD } from '../../debug-build'; import { addClsInstrumentationHandler, addFidInstrumentationHandler, diff --git a/packages/browser-utils/src/browser/request.ts b/packages/browser-utils/src/browser/request.ts index b808136b0325..73038996d7c0 100644 --- a/packages/browser-utils/src/browser/request.ts +++ b/packages/browser-utils/src/browser/request.ts @@ -16,14 +16,13 @@ import { import type { HandlerDataXhr, SentryWrappedXMLHttpRequest, Span } from '@sentry/types'; import { BAGGAGE_HEADER_NAME, - SENTRY_XHR_DATA_KEY, addFetchInstrumentationHandler, - addXhrInstrumentationHandler, browserPerformanceTimeOrigin, dynamicSamplingContextToSentryBaggageHeader, generateSentryTraceHeader, stringMatchesSomePattern, } from '@sentry/utils'; +import { SENTRY_XHR_DATA_KEY, addXhrInstrumentationHandler } from '../instrument/xhr'; import { addPerformanceInstrumentationHandler } from './instrument'; import { WINDOW } from './types'; diff --git a/packages/browser-utils/src/common/debug-build.ts b/packages/browser-utils/src/debug-build.ts similarity index 100% rename from packages/browser-utils/src/common/debug-build.ts rename to packages/browser-utils/src/debug-build.ts diff --git a/packages/browser-utils/src/index.ts b/packages/browser-utils/src/index.ts index 019639e8c0b3..0824c9f50358 100644 --- a/packages/browser-utils/src/index.ts +++ b/packages/browser-utils/src/index.ts @@ -12,4 +12,13 @@ export { addLcpInstrumentationHandler, } from './browser'; +export { addClickKeypressInstrumentationHandler } from './instrument/dom'; + +export { addHistoryInstrumentationHandler } from './instrument/history'; + +export { + addXhrInstrumentationHandler, + SENTRY_XHR_DATA_KEY, +} from './instrument/xhr'; + export type { RequestInstrumentationOptions } from './browser'; diff --git a/packages/utils/src/instrument/dom.ts b/packages/browser-utils/src/instrument/dom.ts similarity index 94% rename from packages/utils/src/instrument/dom.ts rename to packages/browser-utils/src/instrument/dom.ts index aab64c8c149b..4dfa5992aa07 100644 --- a/packages/utils/src/instrument/dom.ts +++ b/packages/browser-utils/src/instrument/dom.ts @@ -1,13 +1,7 @@ -// 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. - -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/ban-types */ import type { HandlerDataDom } from '@sentry/types'; -import { uuid4 } from '../misc'; -import { addNonEnumerableProperty, fill } from '../object'; -import { GLOBAL_OBJ } from '../worldwide'; -import { addHandler, maybeInstrument, triggerHandlers } from './_handlers'; +import { addHandler, addNonEnumerableProperty, fill, maybeInstrument, triggerHandlers, uuid4 } from '@sentry/utils'; +import { WINDOW } from '../browser/types'; type SentryWrappedTarget = HTMLElement & { _sentryId?: string }; @@ -25,14 +19,13 @@ type RemoveEventListener = ( type InstrumentedElement = Element & { __sentry_instrumentation_handlers__?: { [key in 'click' | 'keypress']?: { - handler?: Function; + handler?: unknown; /** The number of custom listeners attached to this element */ refCount: number; }; }; }; -const WINDOW = GLOBAL_OBJ as unknown as Window; const DEBOUNCE_DURATION = 1000; let debounceTimerID: number | undefined; @@ -71,7 +64,7 @@ export function instrumentDOM(): void { // could potentially prevent the event from bubbling up to our global listeners. This way, our handler are still // guaranteed to fire at least once.) ['EventTarget', 'Node'].forEach((target: string) => { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any const proto = (WINDOW as any)[target] && (WINDOW as any)[target].prototype; // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-prototype-builtins if (!proto || !proto.hasOwnProperty || !proto.hasOwnProperty('addEventListener')) { diff --git a/packages/utils/src/instrument/history.ts b/packages/browser-utils/src/instrument/history.ts similarity index 78% rename from packages/utils/src/instrument/history.ts rename to packages/browser-utils/src/instrument/history.ts index dc144c0e5818..c918cd4ce2dc 100644 --- a/packages/utils/src/instrument/history.ts +++ b/packages/browser-utils/src/instrument/history.ts @@ -1,15 +1,6 @@ -// 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. - -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/ban-types */ import type { HandlerDataHistory } from '@sentry/types'; - -import { fill } from '../object'; -import { supportsHistory } from '../supports'; -import { GLOBAL_OBJ } from '../worldwide'; -import { addHandler, maybeInstrument, triggerHandlers } from './_handlers'; - -const WINDOW = GLOBAL_OBJ as unknown as Window; +import { addHandler, fill, maybeInstrument, supportsHistory, triggerHandlers } from '@sentry/utils'; +import { WINDOW } from '../browser/types'; let lastHref: string | undefined; @@ -33,7 +24,7 @@ function instrumentHistory(): void { } const oldOnPopState = WINDOW.onpopstate; - WINDOW.onpopstate = function (this: WindowEventHandlers, ...args: any[]): any { + WINDOW.onpopstate = function (this: WindowEventHandlers, ...args: unknown[]) { const to = WINDOW.location.href; // keep track of the current URL state, as we always receive only the updated state const from = lastHref; @@ -53,7 +44,7 @@ function instrumentHistory(): void { }; function historyReplacementFunction(originalHistoryFunction: () => void): () => void { - return function (this: History, ...args: any[]): void { + return function (this: History, ...args: unknown[]): void { const url = args.length > 2 ? args[2] : undefined; if (url) { // coerce to string (this is what pushState does) diff --git a/packages/utils/src/instrument/xhr.ts b/packages/browser-utils/src/instrument/xhr.ts similarity index 86% rename from packages/utils/src/instrument/xhr.ts rename to packages/browser-utils/src/instrument/xhr.ts index b00300fd553a..05db402872ed 100644 --- a/packages/utils/src/instrument/xhr.ts +++ b/packages/browser-utils/src/instrument/xhr.ts @@ -1,18 +1,12 @@ -// 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. - -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/ban-types */ import type { HandlerDataXhr, SentryWrappedXMLHttpRequest, WrappedFunction } from '@sentry/types'; -import { isString } from '../is'; -import { fill } from '../object'; -import { GLOBAL_OBJ } from '../worldwide'; -import { addHandler, maybeInstrument, triggerHandlers } from './_handlers'; - -const WINDOW = GLOBAL_OBJ as unknown as Window; +import { addHandler, fill, isString, maybeInstrument, triggerHandlers } from '@sentry/utils'; +import { WINDOW } from '../browser/types'; export const SENTRY_XHR_DATA_KEY = '__sentry_xhr_v3__'; +type WindowWithXhr = Window & { XMLHttpRequest?: typeof XMLHttpRequest }; + /** * Add an instrumentation handler for when an XHR request happens. * The handler function is called once when the request starts and once when it ends, @@ -29,15 +23,14 @@ export function addXhrInstrumentationHandler(handler: (data: HandlerDataXhr) => /** Exported only for tests. */ export function instrumentXHR(): void { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - if (!(WINDOW as any).XMLHttpRequest) { + if (!(WINDOW as WindowWithXhr).XMLHttpRequest) { return; } const xhrproto = XMLHttpRequest.prototype; fill(xhrproto, 'open', function (originalOpen: () => void): () => void { - return function (this: XMLHttpRequest & SentryWrappedXMLHttpRequest, ...args: any[]): void { + return function (this: XMLHttpRequest & SentryWrappedXMLHttpRequest, ...args: unknown[]): void { const startTimestamp = Date.now(); // open() should always be called with two or more arguments @@ -87,8 +80,8 @@ export function instrumentXHR(): void { }; if ('onreadystatechange' in this && typeof this.onreadystatechange === 'function') { - fill(this, 'onreadystatechange', function (original: WrappedFunction): Function { - return function (this: SentryWrappedXMLHttpRequest, ...readyStateArgs: any[]): void { + fill(this, 'onreadystatechange', function (original: WrappedFunction) { + return function (this: SentryWrappedXMLHttpRequest, ...readyStateArgs: unknown[]): void { onreadystatechangeHandler(); return original.apply(this, readyStateArgs); }; @@ -100,7 +93,7 @@ export function instrumentXHR(): void { // Intercepting `setRequestHeader` to access the request headers of XHR instance. // This will only work for user/library defined headers, not for the default/browser-assigned headers. // Request cookies are also unavailable for XHR, as `Cookie` header can't be defined by `setRequestHeader`. - fill(this, 'setRequestHeader', function (original: WrappedFunction): Function { + fill(this, 'setRequestHeader', function (original: WrappedFunction) { return function (this: SentryWrappedXMLHttpRequest, ...setRequestHeaderArgs: unknown[]): void { const [header, value] = setRequestHeaderArgs; @@ -119,7 +112,7 @@ export function instrumentXHR(): void { }); fill(xhrproto, 'send', function (originalSend: () => void): () => void { - return function (this: XMLHttpRequest & SentryWrappedXMLHttpRequest, ...args: any[]): void { + return function (this: XMLHttpRequest & SentryWrappedXMLHttpRequest, ...args: unknown[]): void { const sentryXhrData = this[SENTRY_XHR_DATA_KEY]; if (!sentryXhrData) { diff --git a/packages/browser-utils/test/browser/request.test.ts b/packages/browser-utils/test/browser/request.test.ts index 8855203ce136..0671e8d5805c 100644 --- a/packages/browser-utils/test/browser/request.test.ts +++ b/packages/browser-utils/test/browser/request.test.ts @@ -1,6 +1,7 @@ -/* eslint-disable deprecation/deprecation */ import * as utils from '@sentry/utils'; +import * as xhrInstrumentation from '../../src/instrument/xhr'; + import { extractNetworkProtocol, instrumentOutgoingRequests, shouldAttachHeaders } from '../../src/browser/request'; import { WINDOW } from '../../src/browser/types'; @@ -17,7 +18,7 @@ describe('instrumentOutgoingRequests', () => { it('instruments fetch and xhr requests', () => { const addFetchSpy = jest.spyOn(utils, 'addFetchInstrumentationHandler'); - const addXhrSpy = jest.spyOn(utils, 'addXhrInstrumentationHandler'); + const addXhrSpy = jest.spyOn(xhrInstrumentation, 'addXhrInstrumentationHandler'); instrumentOutgoingRequests(); @@ -34,7 +35,7 @@ describe('instrumentOutgoingRequests', () => { }); it('does not instrument xhr requests if traceXHR is false', () => { - const addXhrSpy = jest.spyOn(utils, 'addXhrInstrumentationHandler'); + const addXhrSpy = jest.spyOn(xhrInstrumentation, 'addXhrInstrumentationHandler'); instrumentOutgoingRequests({ traceXHR: false }); diff --git a/packages/utils/test/instrument/dom.test.ts b/packages/browser-utils/test/instrument/dom.test.ts similarity index 75% rename from packages/utils/test/instrument/dom.test.ts rename to packages/browser-utils/test/instrument/dom.test.ts index 28745109e0f8..ad1dc2fd82fa 100644 --- a/packages/utils/test/instrument/dom.test.ts +++ b/packages/browser-utils/test/instrument/dom.test.ts @@ -1,7 +1,7 @@ import { instrumentDOM } from '../../src/instrument/dom'; -jest.mock('../../src/worldwide', () => { - const original = jest.requireActual('../../src/worldwide'); +jest.mock('@sentry/utils', () => { + const original = jest.requireActual('@sentry/utils'); return { ...original, diff --git a/packages/utils/test/instrument/xhr.test.ts b/packages/browser-utils/test/instrument/xhr.test.ts similarity index 75% rename from packages/utils/test/instrument/xhr.test.ts rename to packages/browser-utils/test/instrument/xhr.test.ts index 60485ff1d398..46fa1a477521 100644 --- a/packages/utils/test/instrument/xhr.test.ts +++ b/packages/browser-utils/test/instrument/xhr.test.ts @@ -1,8 +1,7 @@ import { instrumentXHR } from '../../src/instrument/xhr'; -jest.mock('../../src/worldwide', () => { - const original = jest.requireActual('../../src/worldwide'); - +jest.mock('@sentry/utils', () => { + const original = jest.requireActual('@sentry/utils'); return { ...original, GLOBAL_OBJ: { diff --git a/packages/browser/src/integrations/breadcrumbs.ts b/packages/browser/src/integrations/breadcrumbs.ts index 474432fedb48..e3c1120fca57 100644 --- a/packages/browser/src/integrations/breadcrumbs.ts +++ b/packages/browser/src/integrations/breadcrumbs.ts @@ -1,28 +1,28 @@ +import { + SENTRY_XHR_DATA_KEY, + addClickKeypressInstrumentationHandler, + addHistoryInstrumentationHandler, + addXhrInstrumentationHandler, +} from '@sentry-internal/browser-utils'; import { addBreadcrumb, defineIntegration, getClient } from '@sentry/core'; import type { + Breadcrumb, Client, Event as SentryEvent, + FetchBreadcrumbData, + FetchBreadcrumbHint, HandlerDataConsole, HandlerDataDom, HandlerDataFetch, HandlerDataHistory, HandlerDataXhr, IntegrationFn, -} from '@sentry/types'; -import type { - Breadcrumb, - FetchBreadcrumbData, - FetchBreadcrumbHint, XhrBreadcrumbData, XhrBreadcrumbHint, -} from '@sentry/types/build/types/breadcrumb'; +} from '@sentry/types'; import { - SENTRY_XHR_DATA_KEY, - addClickKeypressInstrumentationHandler, addConsoleInstrumentationHandler, addFetchInstrumentationHandler, - addHistoryInstrumentationHandler, - addXhrInstrumentationHandler, getComponentName, getEventDescription, htmlTreeAsString, diff --git a/packages/browser/src/integrations/httpclient.ts b/packages/browser/src/integrations/httpclient.ts index e8d5d596d975..b48cc69d09c0 100644 --- a/packages/browser/src/integrations/httpclient.ts +++ b/packages/browser/src/integrations/httpclient.ts @@ -1,11 +1,10 @@ +import { SENTRY_XHR_DATA_KEY, addXhrInstrumentationHandler } from '@sentry-internal/browser-utils'; import { captureEvent, defineIntegration, getClient, isSentryRequestUrl } from '@sentry/core'; import type { Client, Event as SentryEvent, IntegrationFn, SentryWrappedXMLHttpRequest } from '@sentry/types'; import { GLOBAL_OBJ, - SENTRY_XHR_DATA_KEY, addExceptionMechanism, addFetchInstrumentationHandler, - addXhrInstrumentationHandler, logger, supportsNativeFetch, } from '@sentry/utils'; diff --git a/packages/browser/src/profiling/integration.ts b/packages/browser/src/profiling/integration.ts index af88bd8d91e6..585ad28802b3 100644 --- a/packages/browser/src/profiling/integration.ts +++ b/packages/browser/src/profiling/integration.ts @@ -1,6 +1,5 @@ import { defineIntegration, getActiveSpan, getRootSpan } from '@sentry/core'; -import type { EventEnvelope, IntegrationFn, Span } from '@sentry/types'; -import type { Profile } from '@sentry/types/src/profiling'; +import type { EventEnvelope, IntegrationFn, Profile, Span } from '@sentry/types'; import { logger } from '@sentry/utils'; import { DEBUG_BUILD } from '../debug-build'; diff --git a/packages/browser/src/profiling/utils.ts b/packages/browser/src/profiling/utils.ts index a9dd735a3812..4682633d101c 100644 --- a/packages/browser/src/profiling/utils.ts +++ b/packages/browser/src/profiling/utils.ts @@ -1,8 +1,17 @@ /* eslint-disable max-lines */ import { DEFAULT_ENVIRONMENT, getClient, spanToJSON } from '@sentry/core'; -import type { DebugImage, Envelope, Event, EventEnvelope, Span, StackFrame, StackParser } from '@sentry/types'; -import type { Profile, ThreadCpuProfile } from '@sentry/types/src/profiling'; +import type { + DebugImage, + Envelope, + Event, + EventEnvelope, + Profile, + Span, + StackFrame, + StackParser, + ThreadCpuProfile, +} from '@sentry/types'; import { GLOBAL_OBJ, browserPerformanceTimeOrigin, forEachEnvelopeItem, logger, uuid4 } from '@sentry/utils'; import { DEBUG_BUILD } from '../debug-build'; diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index 88411c082106..9c25dc9750eb 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -9,14 +9,9 @@ import { startSession, } from '@sentry/core'; import type { DsnLike, Integration, Options, UserFeedback } from '@sentry/types'; -import { - addHistoryInstrumentationHandler, - consoleSandbox, - logger, - stackParserFromStackParserOptions, - supportsFetch, -} from '@sentry/utils'; +import { consoleSandbox, logger, stackParserFromStackParserOptions, supportsFetch } from '@sentry/utils'; +import { addHistoryInstrumentationHandler } from '@sentry-internal/browser-utils'; import { dedupeIntegration } from '@sentry/core'; import type { BrowserClientOptions, BrowserOptions } from './client'; import { BrowserClient } from './client'; diff --git a/packages/deno/src/integrations/breadcrumbs.ts b/packages/deno/src/integrations/breadcrumbs.ts index 58c75624b90d..47953d4d7ce8 100644 --- a/packages/deno/src/integrations/breadcrumbs.ts +++ b/packages/deno/src/integrations/breadcrumbs.ts @@ -1,6 +1,13 @@ import { addBreadcrumb, defineIntegration, getClient } from '@sentry/core'; -import type { Client, Event as SentryEvent, HandlerDataConsole, HandlerDataFetch, IntegrationFn } from '@sentry/types'; -import type { FetchBreadcrumbData, FetchBreadcrumbHint } from '@sentry/types/build/types/breadcrumb'; +import type { + Client, + Event as SentryEvent, + FetchBreadcrumbData, + FetchBreadcrumbHint, + HandlerDataConsole, + HandlerDataFetch, + IntegrationFn, +} from '@sentry/types'; import { addConsoleInstrumentationHandler, addFetchInstrumentationHandler, diff --git a/packages/replay-internal/src/coreHandlers/util/xhrUtils.ts b/packages/replay-internal/src/coreHandlers/util/xhrUtils.ts index 4ce349f1bb2e..fa504dcdeec2 100644 --- a/packages/replay-internal/src/coreHandlers/util/xhrUtils.ts +++ b/packages/replay-internal/src/coreHandlers/util/xhrUtils.ts @@ -1,5 +1,6 @@ +import { SENTRY_XHR_DATA_KEY } from '@sentry-internal/browser-utils'; import type { Breadcrumb, XhrBreadcrumbData } from '@sentry/types'; -import { SENTRY_XHR_DATA_KEY, logger } from '@sentry/utils'; +import { logger } from '@sentry/utils'; import { DEBUG_BUILD } from '../../debug-build'; import type { diff --git a/packages/replay-internal/src/util/addGlobalListeners.ts b/packages/replay-internal/src/util/addGlobalListeners.ts index 164b054ba2ac..70344413d3ba 100644 --- a/packages/replay-internal/src/util/addGlobalListeners.ts +++ b/packages/replay-internal/src/util/addGlobalListeners.ts @@ -1,6 +1,9 @@ +import { + addClickKeypressInstrumentationHandler, + addHistoryInstrumentationHandler, +} from '@sentry-internal/browser-utils'; import { addEventProcessor, getClient } from '@sentry/core'; import type { DynamicSamplingContext } from '@sentry/types'; -import { addClickKeypressInstrumentationHandler, addHistoryInstrumentationHandler } from '@sentry/utils'; import { handleAfterSendEvent } from '../coreHandlers/handleAfterSendEvent'; import { handleBeforeSendEvent } from '../coreHandlers/handleBeforeSendEvent'; diff --git a/packages/replay-internal/test/integration/beforeAddRecordingEvent.test.ts b/packages/replay-internal/test/integration/beforeAddRecordingEvent.test.ts index c6b14d1f1bb8..a8331149838b 100644 --- a/packages/replay-internal/test/integration/beforeAddRecordingEvent.test.ts +++ b/packages/replay-internal/test/integration/beforeAddRecordingEvent.test.ts @@ -1,6 +1,6 @@ +import * as SentryBrowserUtils from '@sentry-internal/browser-utils'; import * as SentryCore from '@sentry/core'; import type { Transport } from '@sentry/types'; -import * as SentryUtils from '@sentry/utils'; import type { Replay } from '../../src/integration'; import type { ReplayContainer } from '../../src/replay'; @@ -31,7 +31,7 @@ describe('Integration | beforeAddRecordingEvent', () => { beforeAll(async () => { jest.setSystemTime(new Date(BASE_TIMESTAMP)); - jest.spyOn(SentryUtils, 'addClickKeypressInstrumentationHandler').mockImplementation(handler => { + jest.spyOn(SentryBrowserUtils, 'addClickKeypressInstrumentationHandler').mockImplementation(handler => { domHandler = handler; }); diff --git a/packages/replay-internal/test/integration/flush.test.ts b/packages/replay-internal/test/integration/flush.test.ts index b40d6c78906d..ee4cf456fbf9 100644 --- a/packages/replay-internal/test/integration/flush.test.ts +++ b/packages/replay-internal/test/integration/flush.test.ts @@ -1,3 +1,4 @@ +import * as SentryBrowserUtils from '@sentry-internal/browser-utils'; import * as SentryUtils from '@sentry/utils'; import { DEFAULT_FLUSH_MIN_DELAY, MAX_REPLAY_DURATION, WINDOW } from '../../src/constants'; @@ -44,7 +45,7 @@ describe('Integration | flush', () => { let mockAddPerformanceEntries: MockAddPerformanceEntries; beforeAll(async () => { - jest.spyOn(SentryUtils, 'addClickKeypressInstrumentationHandler').mockImplementation(handler => { + jest.spyOn(SentryBrowserUtils, 'addClickKeypressInstrumentationHandler').mockImplementation(handler => { domHandler = handler; }); diff --git a/packages/replay-internal/test/integration/sendReplayEvent.test.ts b/packages/replay-internal/test/integration/sendReplayEvent.test.ts index ff887c4b629b..58cdecaf1c65 100644 --- a/packages/replay-internal/test/integration/sendReplayEvent.test.ts +++ b/packages/replay-internal/test/integration/sendReplayEvent.test.ts @@ -1,6 +1,6 @@ +import * as SentryBrowserUtils from '@sentry-internal/browser-utils'; import * as SentryCore from '@sentry/core'; import type { Transport } from '@sentry/types'; -import * as SentryUtils from '@sentry/utils'; import { DEFAULT_FLUSH_MIN_DELAY, WINDOW } from '../../src/constants'; import type { ReplayContainer } from '../../src/replay'; @@ -30,7 +30,7 @@ describe('Integration | sendReplayEvent', () => { beforeAll(async () => { jest.setSystemTime(new Date(BASE_TIMESTAMP)); - jest.spyOn(SentryUtils, 'addClickKeypressInstrumentationHandler').mockImplementation(handler => { + jest.spyOn(SentryBrowserUtils, 'addClickKeypressInstrumentationHandler').mockImplementation(handler => { domHandler = handler; }); diff --git a/packages/replay-internal/test/integration/stop.test.ts b/packages/replay-internal/test/integration/stop.test.ts index d1af26e74776..3afbe3eeef1a 100644 --- a/packages/replay-internal/test/integration/stop.test.ts +++ b/packages/replay-internal/test/integration/stop.test.ts @@ -1,4 +1,4 @@ -import * as SentryUtils from '@sentry/utils'; +import * as SentryBrowserUtils from '@sentry-internal/browser-utils'; import { WINDOW } from '../../src/constants'; import type { Replay } from '../../src/integration'; @@ -27,7 +27,7 @@ describe('Integration | stop', () => { beforeAll(async () => { jest.setSystemTime(new Date(BASE_TIMESTAMP)); - mockAddDomInstrumentationHandler = jest.spyOn(SentryUtils, 'addClickKeypressInstrumentationHandler'); + mockAddDomInstrumentationHandler = jest.spyOn(SentryBrowserUtils, 'addClickKeypressInstrumentationHandler'); ({ replay, integration } = await mockSdk()); diff --git a/packages/replay-internal/test/mocks/resetSdkMock.ts b/packages/replay-internal/test/mocks/resetSdkMock.ts index 3596ad4d29d2..47a1522a4c63 100644 --- a/packages/replay-internal/test/mocks/resetSdkMock.ts +++ b/packages/replay-internal/test/mocks/resetSdkMock.ts @@ -23,8 +23,8 @@ export async function resetSdkMock({ replayOptions, sentryOptions, autoStart }: // Clear all handlers that have been registered resetInstrumentationHandlers(); - const SentryUtils = await import('@sentry/utils'); - jest.spyOn(SentryUtils, 'addClickKeypressInstrumentationHandler').mockImplementation(handler => { + const SentryBrowserUtils = await import('@sentry-internal/browser-utils'); + jest.spyOn(SentryBrowserUtils, 'addClickKeypressInstrumentationHandler').mockImplementation(handler => { domHandler = handler; }); const { mockRrweb } = await import('./mockRrweb'); diff --git a/packages/replay-internal/test/unit/coreHandlers/handleNetworkBreadcrumbs.test.ts b/packages/replay-internal/test/unit/coreHandlers/handleNetworkBreadcrumbs.test.ts index de16549d081e..b3522c0ceb6c 100644 --- a/packages/replay-internal/test/unit/coreHandlers/handleNetworkBreadcrumbs.test.ts +++ b/packages/replay-internal/test/unit/coreHandlers/handleNetworkBreadcrumbs.test.ts @@ -1,3 +1,4 @@ +import { SENTRY_XHR_DATA_KEY } from '@sentry-internal/browser-utils'; import type { Breadcrumb, BreadcrumbHint, @@ -5,7 +6,6 @@ import type { SentryWrappedXMLHttpRequest, XhrBreadcrumbHint, } from '@sentry/types'; -import { SENTRY_XHR_DATA_KEY } from '@sentry/utils'; import { BASE_TIMESTAMP } from '../..'; import { NETWORK_BODY_MAX_SIZE } from '../../../src/constants'; diff --git a/packages/utils/src/instrument/console.ts b/packages/utils/src/instrument/console.ts index 7570f58a55dc..f75faa3346c2 100644 --- a/packages/utils/src/instrument/console.ts +++ b/packages/utils/src/instrument/console.ts @@ -5,7 +5,7 @@ import type { ConsoleLevel, HandlerDataConsole } from '@sentry/types'; import { CONSOLE_LEVELS, originalConsoleMethods } from '../logger'; import { fill } from '../object'; import { GLOBAL_OBJ } from '../worldwide'; -import { addHandler, maybeInstrument, triggerHandlers } from './_handlers'; +import { addHandler, maybeInstrument, triggerHandlers } from './handlers'; /** * Add an instrumentation handler for when a console.xxx method is called. diff --git a/packages/utils/src/instrument/fetch.ts b/packages/utils/src/instrument/fetch.ts index 472f48283a47..9c663f88baf0 100644 --- a/packages/utils/src/instrument/fetch.ts +++ b/packages/utils/src/instrument/fetch.ts @@ -4,7 +4,7 @@ import type { HandlerDataFetch } from '@sentry/types'; import { fill } from '../object'; import { supportsNativeFetch } from '../supports'; import { GLOBAL_OBJ } from '../worldwide'; -import { addHandler, maybeInstrument, triggerHandlers } from './_handlers'; +import { addHandler, maybeInstrument, triggerHandlers } from './handlers'; type FetchResource = string | { toString(): string } | { url: string }; diff --git a/packages/utils/src/instrument/globalError.ts b/packages/utils/src/instrument/globalError.ts index 0bf50c52f2c4..a3ee2149817f 100644 --- a/packages/utils/src/instrument/globalError.ts +++ b/packages/utils/src/instrument/globalError.ts @@ -1,7 +1,7 @@ import type { HandlerDataError } from '@sentry/types'; import { GLOBAL_OBJ } from '../worldwide'; -import { addHandler, maybeInstrument, triggerHandlers } from './_handlers'; +import { addHandler, maybeInstrument, triggerHandlers } from './handlers'; let _oldOnErrorHandler: (typeof GLOBAL_OBJ)['onerror'] | null = null; diff --git a/packages/utils/src/instrument/globalUnhandledRejection.ts b/packages/utils/src/instrument/globalUnhandledRejection.ts index c92f4d723b0d..878540500dac 100644 --- a/packages/utils/src/instrument/globalUnhandledRejection.ts +++ b/packages/utils/src/instrument/globalUnhandledRejection.ts @@ -3,7 +3,7 @@ import type { HandlerDataUnhandledRejection } from '@sentry/types'; import { GLOBAL_OBJ } from '../worldwide'; -import { addHandler, maybeInstrument, triggerHandlers } from './_handlers'; +import { addHandler, maybeInstrument, triggerHandlers } from './handlers'; let _oldOnUnhandledRejectionHandler: (typeof GLOBAL_OBJ)['onunhandledrejection'] | null = null; diff --git a/packages/utils/src/instrument/_handlers.ts b/packages/utils/src/instrument/handlers.ts similarity index 100% rename from packages/utils/src/instrument/_handlers.ts rename to packages/utils/src/instrument/handlers.ts diff --git a/packages/utils/src/instrument/index.ts b/packages/utils/src/instrument/index.ts index 40f2f0fe917c..ee84f1375522 100644 --- a/packages/utils/src/instrument/index.ts +++ b/packages/utils/src/instrument/index.ts @@ -1,23 +1,17 @@ -// TODO(v8): Consider moving this file (or at least parts of it) into the browser package. The registered handlers are mostly non-generic and we risk leaking runtime specific code into generic packages. - -import { resetInstrumentationHandlers } from './_handlers'; import { addConsoleInstrumentationHandler } from './console'; -import { addClickKeypressInstrumentationHandler } from './dom'; import { addFetchInstrumentationHandler } from './fetch'; import { addGlobalErrorInstrumentationHandler } from './globalError'; import { addGlobalUnhandledRejectionInstrumentationHandler } from './globalUnhandledRejection'; -import { addHistoryInstrumentationHandler } from './history'; -import { SENTRY_XHR_DATA_KEY, addXhrInstrumentationHandler } from './xhr'; +import { addHandler, maybeInstrument, resetInstrumentationHandlers, triggerHandlers } from './handlers'; export { addConsoleInstrumentationHandler, - addClickKeypressInstrumentationHandler, - addXhrInstrumentationHandler, addFetchInstrumentationHandler, - addHistoryInstrumentationHandler, addGlobalErrorInstrumentationHandler, addGlobalUnhandledRejectionInstrumentationHandler, - SENTRY_XHR_DATA_KEY, + addHandler, + maybeInstrument, + triggerHandlers, // Only exported for tests resetInstrumentationHandlers, };