-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): Prevent
logger
circular dependencies (#4069)
This aims to prevent the `logger` -> `misc` -> `logger` dependency cycle from ever happening again. - Move `getGlobalObject` to its own module. - Move `consoleSandbox` into the logger module itself, since that's the only place it's used. - Given how easy it would be to recreate this problem - just add to `logger.ts`'s dependencies (not knowing that that's what they are) any function which logs to the console using the logger - add a warning comment at the top of both dependencies. As a bonus, move `getLocationHref` into the browser module, a move which was missed in an earlier rearrangement.
- Loading branch information
1 parent
f44d370
commit 84a6dc0
Showing
11 changed files
with
128 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* NOTE: In order to avoid circular dependencies, if you add a function to this module and it needs to print something, | ||
* you must either a) use `console.log` rather than the logger, or b) put your function elsewhere. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { Integration } from '@sentry/types'; | ||
|
||
import { isNodeEnv } from './node'; | ||
|
||
/** 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' // eslint-disable-line no-restricted-globals | ||
? window // eslint-disable-line no-restricted-globals | ||
: typeof self !== 'undefined' | ||
? self | ||
: fallbackGlobalObject) as T & SentryGlobal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { getGlobalObject } from '../src/global'; | ||
|
||
describe('getGlobalObject()', () => { | ||
test('should return the same object', () => { | ||
const backup = global.process; | ||
delete global.process; | ||
const first = getGlobalObject(); | ||
const second = getGlobalObject(); | ||
expect(first).toEqual(second); | ||
global.process = backup; | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters