Skip to content

Commit 92729aa

Browse files
authored
chore(utils): Split browser/node compatibility utils into separate module (#3123)
1 parent b80f797 commit 92729aa

File tree

9 files changed

+56
-57
lines changed

9 files changed

+56
-57
lines changed

packages/utils/src/compat.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Integration } from '@sentry/types';
2+
3+
/**
4+
* Checks whether we're in the Node.js or Browser environment
5+
*
6+
* @returns Answer to given question
7+
*/
8+
export function isNodeEnv(): boolean {
9+
return Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
10+
}
11+
12+
/** Internal */
13+
interface SentryGlobal {
14+
Sentry?: {
15+
Integrations?: Integration[];
16+
};
17+
SENTRY_ENVIRONMENT?: string;
18+
SENTRY_DSN?: string;
19+
SENTRY_RELEASE?: {
20+
id?: string;
21+
};
22+
__SENTRY__: {
23+
globalEventProcessors: any;
24+
hub: any;
25+
logger: any;
26+
};
27+
}
28+
29+
const fallbackGlobalObject = {};
30+
31+
/**
32+
* Safely get global scope object
33+
*
34+
* @returns Global scope object
35+
*/
36+
export function getGlobalObject<T>(): T & SentryGlobal {
37+
return (isNodeEnv()
38+
? global
39+
: typeof window !== 'undefined'
40+
? window
41+
: typeof self !== 'undefined'
42+
? self
43+
: fallbackGlobalObject) as T & SentryGlobal;
44+
}

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './async';
22
export * from './browser';
3+
export * from './compat';
34
export * from './dsn';
45
export * from './error';
56
export * from './instrument';

packages/utils/src/instrument.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
/* eslint-disable @typescript-eslint/ban-types */
33
import { WrappedFunction } from '@sentry/types';
44

5+
import { getGlobalObject } from './compat';
56
import { isInstanceOf, isString } from './is';
67
import { logger } from './logger';
7-
import { getGlobalObject } from './misc';
88
import { fill } from './object';
99
import { getFunctionName } from './stacktrace';
1010
import { supportsHistory, supportsNativeFetch } from './supports';

packages/utils/src/logger.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { consoleSandbox, getGlobalObject } from './misc';
2+
import { getGlobalObject } from './compat';
3+
import { consoleSandbox } from './misc';
34

45
// TODO: Implement different loggers for different environments
56
const global = getGlobalObject<Window | NodeJS.Global>();

packages/utils/src/misc.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { Event, Integration, StackFrame, WrappedFunction } from '@sentry/types';
2+
import { Event, StackFrame, WrappedFunction } from '@sentry/types';
33

4-
import { isNodeEnv } from './node';
4+
import { getGlobalObject } from './compat';
55
import { snipLine } from './string';
66

7-
/** Internal */
8-
interface SentryGlobal {
9-
Sentry?: {
10-
Integrations?: Integration[];
11-
};
12-
SENTRY_ENVIRONMENT?: string;
13-
SENTRY_DSN?: string;
14-
SENTRY_RELEASE?: {
15-
id?: string;
16-
};
17-
__SENTRY__: {
18-
globalEventProcessors: any;
19-
hub: any;
20-
logger: any;
21-
};
22-
}
23-
24-
const fallbackGlobalObject = {};
25-
26-
/**
27-
* Safely get global scope object
28-
*
29-
* @returns Global scope object
30-
*/
31-
export function getGlobalObject<T>(): T & SentryGlobal {
32-
return (isNodeEnv()
33-
? global
34-
: typeof window !== 'undefined'
35-
? window
36-
: typeof self !== 'undefined'
37-
? self
38-
: fallbackGlobalObject) as T & SentryGlobal;
39-
}
40-
417
/**
428
* Extended Window interface that allows for Crypto API usage in IE browsers
439
*/

packages/utils/src/node.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { ExtractedNodeRequestData } from '@sentry/types';
33

4+
import { isNodeEnv } from './compat';
45
import { isString } from './is';
56
import { normalize } from './object';
67

7-
/**
8-
* Checks whether we're in the Node.js or Browser environment
9-
*
10-
* @returns Answer to given question
11-
*/
12-
export function isNodeEnv(): boolean {
13-
return Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
14-
}
15-
168
/**
179
* Requires a module which is protected against bundler minification.
1810
*

packages/utils/src/supports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { getGlobalObject } from './compat';
12
import { logger } from './logger';
2-
import { getGlobalObject } from './misc';
33

44
/**
55
* Tells whether current environment supports ErrorEvent objects

packages/utils/src/time.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getGlobalObject } from './misc';
2-
import { dynamicRequire, isNodeEnv } from './node';
1+
import { getGlobalObject, isNodeEnv } from './compat';
2+
import { dynamicRequire } from './node';
33

44
/**
55
* An object that can return the current timestamp in seconds since the UNIX epoch.

packages/utils/test/misc.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { StackFrame } from '@sentry/types';
22

3-
import {
4-
addContextToFrame,
5-
getEventDescription,
6-
getGlobalObject,
7-
parseRetryAfterHeader,
8-
stripUrlQueryAndFragment,
9-
} from '../src/misc';
3+
import { getGlobalObject } from '../src/compat';
4+
import { addContextToFrame, getEventDescription, parseRetryAfterHeader, stripUrlQueryAndFragment } from '../src/misc';
105

116
describe('getEventDescription()', () => {
127
test('message event', () => {

0 commit comments

Comments
 (0)