Skip to content

fix(utils): Remove WINDOW from utils #6024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/angular/src/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable max-lines */
import { AfterViewInit, Directive, Injectable, Input, NgModule, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRouteSnapshot, Event, NavigationEnd, NavigationStart, ResolveEnd, Router } from '@angular/router';
import { getCurrentHub } from '@sentry/browser';
import { getCurrentHub, WINDOW } from '@sentry/browser';
import { Span, Transaction, TransactionContext } from '@sentry/types';
import { logger, stripUrlQueryAndFragment, timestampWithMs, WINDOW } from '@sentry/utils';
import { logger, stripUrlQueryAndFragment, timestampWithMs } from '@sentry/utils';
import { Observable, Subscription } from 'rxjs';
import { filter, tap } from 'rxjs/operators';

Expand Down
10 changes: 2 additions & 8 deletions packages/browser/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { BaseClient, getCurrentHub, getEnvelopeEndpointWithUrlEncodedAuth, Scope, SDK_VERSION } from '@sentry/core';
import { ClientOptions, Event, EventHint, Options, Severity, SeverityLevel } from '@sentry/types';
import {
createClientReportEnvelope,
dsnToString,
getEventDescription,
logger,
serializeEnvelope,
WINDOW,
} from '@sentry/utils';
import { createClientReportEnvelope, dsnToString, getEventDescription, logger, serializeEnvelope } from '@sentry/utils';

import { eventFromException, eventFromMessage } from './eventbuilder';
import { WINDOW } from './helpers';
import { Breadcrumbs } from './integrations';
import { BREADCRUMB_INTEGRATION_ID } from './integrations/breadcrumbs';
import { BrowserTransportOptions } from './transports/types';
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export {
InboundFilters,
} from '@sentry/core';

export { WINDOW } from './helpers';
export { BrowserClient } from './client';
export { makeFetchTransport, makeXHRTransport } from './transports';
export {
Expand Down
3 changes: 3 additions & 0 deletions packages/browser/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import {
addExceptionTypeValue,
addNonEnumerableProperty,
getOriginalFunction,
GLOBAL_OBJ,
markFunctionWrapped,
} from '@sentry/utils';

export const WINDOW = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;

let ignoreOnError: number = 0;

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './exports';

import { Integrations as CoreIntegrations } from '@sentry/core';
import { WINDOW } from '@sentry/utils';

import { WINDOW } from './helpers';
import * as BrowserIntegrations from './integrations';

let windowIntegrations = {};
Expand Down
3 changes: 2 additions & 1 deletion packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
parseUrl,
safeJoin,
severityLevelFromString,
WINDOW,
} from '@sentry/utils';

import { WINDOW } from '../helpers';

/** JSDoc */
interface BreadcrumbsOptions {
console: boolean;
Expand Down
3 changes: 2 additions & 1 deletion packages/browser/src/integrations/httpcontext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
import { Event, Integration } from '@sentry/types';
import { WINDOW } from '@sentry/utils';

import { WINDOW } from '../helpers';

/** HttpContext integration collects information about HTTP request headers */
export class HttpContext implements Integration {
Expand Down
4 changes: 2 additions & 2 deletions packages/browser/src/integrations/trycatch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Integration, WrappedFunction } from '@sentry/types';
import { fill, getFunctionName, getOriginalFunction, WINDOW } from '@sentry/utils';
import { fill, getFunctionName, getOriginalFunction } from '@sentry/utils';

import { wrap } from '../helpers';
import { WINDOW, wrap } from '../helpers';

const DEFAULT_EVENT_TARGET = [
'EventTarget',
Expand Down
3 changes: 1 addition & 2 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import {
resolvedSyncPromise,
stackParserFromStackParserOptions,
supportsFetch,
WINDOW,
} from '@sentry/utils';

import { BrowserClient, BrowserClientOptions, BrowserOptions } from './client';
import { ReportDialogOptions, wrap as internalWrap } from './helpers';
import { ReportDialogOptions, WINDOW, wrap as internalWrap } from './helpers';
import { Breadcrumbs, Dedupe, GlobalHandlers, HttpContext, LinkedErrors, TryCatch } from './integrations';
import { defaultStackParser } from './stack-parsers';
import { makeFetchTransport, makeXHRTransport } from './transports';
Expand Down
4 changes: 3 additions & 1 deletion packages/browser/src/transports/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { isNativeFetch, logger, WINDOW } from '@sentry/utils';
import { isNativeFetch, logger } from '@sentry/utils';

import { WINDOW } from '../helpers';

let cachedFetchImpl: FetchImpl;

Expand Down
4 changes: 3 additions & 1 deletion packages/integrations/src/offline.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
import { Event, EventProcessor, Hub, Integration } from '@sentry/types';
import { logger, normalize, uuid4, WINDOW } from '@sentry/utils';
import { GLOBAL_OBJ, logger, normalize, uuid4 } from '@sentry/utils';
import localForage from 'localforage';

export const WINDOW = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;

type LocalForage = {
setItem<T>(key: string, value: T, callback?: (err: any, value: T) => void): Promise<T>;
iterate<T, U>(
Expand Down
4 changes: 3 additions & 1 deletion packages/integrations/src/reportingobserver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { EventProcessor, Hub, Integration } from '@sentry/types';
import { supportsReportingObserver, WINDOW } from '@sentry/utils';
import { GLOBAL_OBJ, supportsReportingObserver } from '@sentry/utils';

export const WINDOW = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;

interface Report {
[key: string]: unknown;
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/test/offline.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WINDOW } from '@sentry/browser';
import { Event, EventProcessor, Hub, Integration, IntegrationClass } from '@sentry/types';
import { WINDOW } from '@sentry/utils';

import { Item, Offline } from '../src/offline';

Expand Down Expand Up @@ -41,7 +41,7 @@ jest.mock('@sentry/utils', () => {

return {
...originalModule,
get WINDOW() {
get GLOBAL_OBJ() {
return {
addEventListener: (_windowEvent: any, callback: any) => {
eventListeners.push(callback);
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/src/performance/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { getCurrentHub } from '@sentry/core';
import { WINDOW } from '@sentry/react';
import { Primitive, TraceparentData, Transaction, TransactionContext, TransactionSource } from '@sentry/types';
import {
baggageHeaderToDynamicSamplingContext,
extractTraceparentData,
logger,
stripUrlQueryAndFragment,
WINDOW,
} from '@sentry/utils';
import type { NEXT_DATA as NextData } from 'next/dist/next-server/lib/utils';
import { default as Router } from 'next/router';
Expand Down
3 changes: 2 additions & 1 deletion packages/nextjs/test/index.client.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { BaseClient, getCurrentHub } from '@sentry/core';
import * as SentryReact from '@sentry/react';
import { WINDOW } from '@sentry/react';
import { Integrations as TracingIntegrations } from '@sentry/tracing';
import { Integration } from '@sentry/types';
import { logger, WINDOW } from '@sentry/utils';
import { logger } from '@sentry/utils';
import { JSDOM } from 'jsdom';

import { init, Integrations, nextRouterInstrumentation } from '../src/index.client';
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/test/performance/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WINDOW } from '@sentry/react';
import { Transaction } from '@sentry/types';
import { WINDOW } from '@sentry/utils';
import { JSDOM } from 'jsdom';
import { NEXT_DATA as NextData } from 'next/dist/next-server/lib/utils';
import { default as Router } from 'next/router';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/reactrouter.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WINDOW } from '@sentry/browser';
import { Transaction, TransactionSource } from '@sentry/types';
import { WINDOW } from '@sentry/utils';
import hoistNonReactStatics from 'hoist-non-react-statics';
import * as React from 'react';

Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/reactrouterv3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WINDOW } from '@sentry/browser';
import { Primitive, Transaction, TransactionContext, TransactionSource } from '@sentry/types';
import { WINDOW } from '@sentry/utils';

import { Location, ReactRouterInstrumentation } from './types';

Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/reactrouterv6.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Inspired from Donnie McNeal's solution:
// https://gist.github.com/wontondon/e8c4bdf2888875e4c755712e99279536

import { WINDOW } from '@sentry/browser';
import { Transaction, TransactionContext, TransactionSource } from '@sentry/types';
import { getNumberOfUrlSegments, logger, WINDOW } from '@sentry/utils';
import { getNumberOfUrlSegments, logger } from '@sentry/utils';
import hoistNonReactStatics from 'hoist-non-react-statics';
import React from 'react';

Expand Down
5 changes: 2 additions & 3 deletions packages/remix/src/performance/client.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ErrorBoundaryProps } from '@sentry/react';
import { withErrorBoundary } from '@sentry/react';
import { ErrorBoundaryProps, WINDOW , withErrorBoundary } from '@sentry/react';
import { Transaction, TransactionContext } from '@sentry/types';
import { logger, WINDOW } from '@sentry/utils';
import { logger } from '@sentry/utils';
import * as React from 'react';

const DEFAULT_TAGS = {
Expand Down
3 changes: 2 additions & 1 deletion packages/tracing/src/browser/backgroundtab.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { logger, WINDOW } from '@sentry/utils';
import { logger } from '@sentry/utils';

import { IdleTransaction } from '../idletransaction';
import { SpanStatusType } from '../span';
import { getActiveTransaction } from '../utils';
import { WINDOW } from '.';

/**
* Add a listener that cancels and finishes a transaction when the global
Expand Down
3 changes: 2 additions & 1 deletion packages/tracing/src/browser/browsertracing.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable max-lines */
import { Hub } from '@sentry/core';
import { EventProcessor, Integration, Transaction, TransactionContext } from '@sentry/types';
import { baggageHeaderToDynamicSamplingContext, getDomElement, logger, WINDOW } from '@sentry/utils';
import { baggageHeaderToDynamicSamplingContext, getDomElement, logger } from '@sentry/utils';

import { startIdleTransaction } from '../hubextensions';
import { DEFAULT_FINAL_TIMEOUT, DEFAULT_HEARTBEAT_INTERVAL, DEFAULT_IDLE_TIMEOUT } from '../idletransaction';
import { extractTraceparentData } from '../utils';
import { WINDOW } from '.';
import { registerBackgroundTabDetection } from './backgroundtab';
import { addPerformanceEntries, startTrackingLongTasks, startTrackingWebVitals } from './metrics';
import {
Expand Down
4 changes: 4 additions & 0 deletions packages/tracing/src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { GLOBAL_OBJ } from '@sentry/utils';

export type { RequestInstrumentationOptions } from './request';

export { BrowserTracing, BROWSER_TRACING_INTEGRATION_ID } from './browsertracing';
export { instrumentOutgoingRequests, defaultRequestInstrumentationOptions } from './request';

export const WINDOW = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;
3 changes: 2 additions & 1 deletion packages/tracing/src/browser/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable max-lines */
import { Measurements } from '@sentry/types';
import { browserPerformanceTimeOrigin, htmlTreeAsString, logger, WINDOW } from '@sentry/utils';
import { browserPerformanceTimeOrigin, htmlTreeAsString, logger } from '@sentry/utils';

import { WINDOW } from '..';
import { IdleTransaction } from '../../idletransaction';
import { Transaction } from '../../transaction';
import { getActiveTransaction, msToSec } from '../../utils';
Expand Down
4 changes: 3 additions & 1 deletion packages/tracing/src/browser/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Transaction, TransactionContext } from '@sentry/types';
import { addInstrumentationHandler, logger, WINDOW } from '@sentry/utils';
import { addInstrumentationHandler, logger } from '@sentry/utils';

import { WINDOW } from '.';

/**
* Default function implementing pageload and navigation transactions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import { WINDOW } from '@sentry/utils';

import { WINDOW } from '../..';
import { NavigationTimingPolyfillEntry } from '../types';

const getNavigationEntryFromPerformanceTiming = (): NavigationTimingPolyfillEntry => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import { WINDOW } from '@sentry/utils';

import { WINDOW } from '../..';
import { onHidden } from './onHidden';

let firstHiddenTime = -1;
Expand Down
3 changes: 1 addition & 2 deletions packages/tracing/src/browser/web-vitals/lib/initMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import { WINDOW } from '@sentry/utils';

import { WINDOW } from '../..';
import { Metric } from '../types';
import { generateUniqueID } from './generateUniqueID';
import { getActivationStart } from './getActivationStart';
Expand Down
2 changes: 1 addition & 1 deletion packages/tracing/src/browser/web-vitals/lib/onHidden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { WINDOW } from '@sentry/utils';
import { WINDOW } from '../..';

export interface OnHiddenCallback {
(event: Event): void;
Expand Down
4 changes: 2 additions & 2 deletions packages/tracing/test/browser/browsertracing.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BrowserClient } from '@sentry/browser';
import { BrowserClient, WINDOW } from '@sentry/browser';
import { Hub, makeMain } from '@sentry/core';
import type { BaseTransportOptions, ClientOptions, DsnComponents } from '@sentry/types';
import { InstrumentHandlerCallback, InstrumentHandlerType, WINDOW } from '@sentry/utils';
import { InstrumentHandlerCallback, InstrumentHandlerType } from '@sentry/utils';
import { JSDOM } from 'jsdom';

import { BrowserTracing, BrowserTracingOptions, getMetaContent } from '../../src/browser/browsertracing';
Expand Down
8 changes: 3 additions & 5 deletions packages/utils/src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { isString } from './is';
import { GLOBAL_OBJ } from './worldwide';
import { getGlobalObject } from './worldwide';

/**
* TODO: Move me to @sentry/browser when @sentry/utils no longer contains any browser code
*/
export const WINDOW = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;
// eslint-disable-next-line deprecation/deprecation
const WINDOW = getGlobalObject<Window>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this still causes issues right? Since Window is at the top level.

Copy link
Collaborator Author

@timfish timfish Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the user reported issue has anything to do with top-level Window usage since the error directly references browser.d.ts:

node_modules/@sentry/utils/types/browser.d.ts:4:69 - error TS2304: Cannot find name 'Window'.

In master there is Window in browser.d.ts:

export declare const WINDOW: InternalGlobal & Window;
export declare function htmlTreeAsString(elem: unknown, keyAttrs?: string[]): string;
export declare function getLocationHref(): string;
export declare function getDomElement<E = any>(selector: string): E | null;

After this PR changes, browser.d.ts no longer has this:

export declare function htmlTreeAsString(elem: unknown, keyAttrs?: string[]): string;
export declare function getLocationHref(): string;
export declare function getDomElement<E = any>(selector: string): E | null;

And in the outputted JavaScript there are no generics or types so Window is gone too:

// eslint-disable-next-line deprecation/deprecation
const WINDOW = getGlobalObject();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah of course, it's not exported!


/**
* Given a child DOM element, returns a query-selector statement describing that
Expand Down
5 changes: 4 additions & 1 deletion packages/utils/src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
/* eslint-disable @typescript-eslint/ban-types */
import { WrappedFunction } from '@sentry/types';

import { WINDOW } from './browser';
import { isInstanceOf, isString } from './is';
import { CONSOLE_LEVELS, logger } from './logger';
import { fill } from './object';
import { getFunctionName } from './stacktrace';
import { supportsHistory, supportsNativeFetch } from './supports';
import { getGlobalObject } from './worldwide';

// eslint-disable-next-line deprecation/deprecation
const WINDOW = getGlobalObject<Window>();

export type InstrumentHandlerType =
| 'console'
Expand Down
5 changes: 4 additions & 1 deletion packages/utils/src/supports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { WINDOW } from './browser';
import { logger } from './logger';
import { getGlobalObject } from './worldwide';

// eslint-disable-next-line deprecation/deprecation
const WINDOW = getGlobalObject<Window>();

/**
* Tells whether current environment supports ErrorEvent objects
Expand Down
5 changes: 4 additions & 1 deletion packages/utils/src/time.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { WINDOW } from './browser';
import { dynamicRequire, isNodeEnv } from './node';
import { getGlobalObject } from './worldwide';

// eslint-disable-next-line deprecation/deprecation
const WINDOW = getGlobalObject<Window>();

/**
* An object that can return the current timestamp in seconds since the UNIX epoch.
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const GLOBAL_OBJ: InternalGlobal =
{};

/**
* @deprecated Use GLOBAL_OBJ instead. This will be removed in v8
* @deprecated Use GLOBAL_OBJ instead or WINDOW from @sentry/browser. This will be removed in v8
*/
export function getGlobalObject<T>(): T & InternalGlobal {
return GLOBAL_OBJ as T & InternalGlobal;
Expand Down
3 changes: 1 addition & 2 deletions packages/vue/src/index.bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export {
SDK_VERSION,
} from '@sentry/browser';

import { Integrations as BrowserIntegrations } from '@sentry/browser';
import { WINDOW } from '@sentry/utils';
import { Integrations as BrowserIntegrations, WINDOW } from '@sentry/browser';

export { init } from './sdk';
export { vueRouterInstrumentation } from './router';
Expand Down
3 changes: 1 addition & 2 deletions packages/vue/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { captureException } from '@sentry/browser';
import { captureException, WINDOW } from '@sentry/browser';
import { Transaction, TransactionContext, TransactionSource } from '@sentry/types';
import { WINDOW } from '@sentry/utils';

import { getActiveTransaction } from './tracing';

Expand Down