Skip to content

chore(utils): Split browser/node compatibility utils into separate module #3123

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
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
44 changes: 44 additions & 0 deletions packages/utils/src/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Integration } from '@sentry/types';

/**
* Checks whether we're in the Node.js or Browser environment
*
* @returns Answer to given question
*/
export function isNodeEnv(): boolean {
return Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
}

/** Internal */
interface SentryGlobal {
Sentry?: {
Integrations?: Integration[];
};
SENTRY_ENVIRONMENT?: string;
SENTRY_DSN?: string;
SENTRY_RELEASE?: {
id?: string;
};
__SENTRY__: {
globalEventProcessors: any;
hub: any;
logger: any;
};
}

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;
}
1 change: 1 addition & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './async';
export * from './browser';
export * from './compat';
export * from './dsn';
export * from './error';
export * from './instrument';
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
/* eslint-disable @typescript-eslint/ban-types */
import { WrappedFunction } from '@sentry/types';

import { getGlobalObject } from './compat';
import { isInstanceOf, isString } from './is';
import { logger } from './logger';
import { getGlobalObject } from './misc';
import { fill } from './object';
import { getFunctionName } from './stacktrace';
import { supportsHistory, supportsNativeFetch } from './supports';
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { consoleSandbox, getGlobalObject } from './misc';
import { getGlobalObject } from './compat';
import { consoleSandbox } from './misc';

// TODO: Implement different loggers for different environments
const global = getGlobalObject<Window | NodeJS.Global>();
Expand Down
38 changes: 2 additions & 36 deletions packages/utils/src/misc.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Event, Integration, StackFrame, WrappedFunction } from '@sentry/types';
import { Event, StackFrame, WrappedFunction } from '@sentry/types';

import { isNodeEnv } from './node';
import { getGlobalObject } from './compat';
import { snipLine } from './string';

/** Internal */
interface SentryGlobal {
Sentry?: {
Integrations?: Integration[];
};
SENTRY_ENVIRONMENT?: string;
SENTRY_DSN?: string;
SENTRY_RELEASE?: {
id?: string;
};
__SENTRY__: {
globalEventProcessors: any;
hub: any;
logger: any;
};
}

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;
}

/**
* Extended Window interface that allows for Crypto API usage in IE browsers
*/
Expand Down
10 changes: 1 addition & 9 deletions packages/utils/src/node.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ExtractedNodeRequestData } from '@sentry/types';

import { isNodeEnv } from './compat';
import { isString } from './is';
import { normalize } from './object';

/**
* Checks whether we're in the Node.js or Browser environment
*
* @returns Answer to given question
*/
export function isNodeEnv(): boolean {
return Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
}

/**
* Requires a module which is protected against bundler minification.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/supports.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getGlobalObject } from './compat';
import { logger } from './logger';
import { getGlobalObject } from './misc';

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

/**
* An object that can return the current timestamp in seconds since the UNIX epoch.
Expand Down
9 changes: 2 additions & 7 deletions packages/utils/test/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { StackFrame } from '@sentry/types';

import {
addContextToFrame,
getEventDescription,
getGlobalObject,
parseRetryAfterHeader,
stripUrlQueryAndFragment,
} from '../src/misc';
import { getGlobalObject } from '../src/compat';
import { addContextToFrame, getEventDescription, parseRetryAfterHeader, stripUrlQueryAndFragment } from '../src/misc';

describe('getEventDescription()', () => {
test('message event', () => {
Expand Down