Skip to content

Commit 8e08b29

Browse files
timfishHazAT
authored andcommitted
Fix: Ensure single instance of logger (getsentry#1954)
1 parent c33425a commit 8e08b29

File tree

15 files changed

+41
-42
lines changed

15 files changed

+41
-42
lines changed

packages/browser/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export class BrowserClient extends BaseClient<BrowserBackend, BrowserOptions> {
7676
*/
7777
public showReportDialog(options: ReportDialogOptions = {}): void {
7878
// doesn't work without a document (React Native)
79-
const document = (getGlobalObject() as Window).document;
79+
const document = getGlobalObject<Window>().document;
8080
if (!document) {
8181
return;
8282
}

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { BrowserClient } from '../client';
1111

1212
import { breadcrumbEventHandler, keypressEventHandler, wrap } from './helpers';
1313

14-
const global = getGlobalObject() as Window;
14+
const global = getGlobalObject<Window>();
1515
let lastHref: string | undefined;
1616

1717
/**

packages/browser/src/integrations/useragent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
22
import { Event, Integration } from '@sentry/types';
33
import { getGlobalObject } from '@sentry/utils/misc';
44

5-
const global = getGlobalObject() as Window;
5+
const global = getGlobalObject<Window>();
66

77
/** UserAgent */
88
export class UserAgent implements Integration {

packages/browser/src/tracekit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ interface ComputeStackTrace {
5555
* @namespace TraceKit
5656
*/
5757

58-
var window = getGlobalObject() as Window;
58+
var window = getGlobalObject<Window>();
5959

6060
interface TraceKit {
6161
report: any;

packages/browser/src/transports/fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { supportsReferrerPolicy } from '@sentry/utils/supports';
44

55
import { BaseTransport } from './base';
66

7-
const global = getGlobalObject() as Window;
7+
const global = getGlobalObject<Window>();
88

99
/** `fetch` based transport */
1010
export class FetchTransport extends BaseTransport {

packages/hub/src/hub.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ export class Hub implements HubInterface {
267267

268268
/** Returns the global shim registry. */
269269
export function getMainCarrier(): Carrier {
270-
const carrier: any = getGlobalObject();
270+
const carrier = getGlobalObject();
271271
carrier.__SENTRY__ = carrier.__SENTRY__ || {
272272
hub: undefined,
273273
};
@@ -333,7 +333,7 @@ export function getCurrentHub(): Hub {
333333
* This will tell whether a carrier has a hub on it or not
334334
* @param carrier object
335335
*/
336-
function hasHubOnCarrier(carrier: any): boolean {
336+
function hasHubOnCarrier(carrier: Carrier): boolean {
337337
if (carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub) {
338338
return true;
339339
}
@@ -346,7 +346,7 @@ function hasHubOnCarrier(carrier: any): boolean {
346346
* @param carrier object
347347
* @hidden
348348
*/
349-
export function getHubFromCarrier(carrier: any): Hub {
349+
export function getHubFromCarrier(carrier: Carrier): Hub {
350350
if (carrier && carrier.__SENTRY__ && carrier.__SENTRY__.hub) {
351351
return carrier.__SENTRY__.hub;
352352
}
@@ -360,7 +360,7 @@ export function getHubFromCarrier(carrier: any): Hub {
360360
* @param carrier object
361361
* @param hub Hub
362362
*/
363-
export function setHubOnCarrier(carrier: any, hub: Hub): boolean {
363+
export function setHubOnCarrier(carrier: Carrier, hub: Hub): boolean {
364364
if (!carrier) {
365365
return false;
366366
}

packages/hub/src/scope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export class Scope implements ScopeInterface {
282282
* Retruns the global event processors.
283283
*/
284284
function getGlobalEventProcessors(): EventProcessor[] {
285-
const global: any = getGlobalObject();
285+
const global = getGlobalObject<Window | NodeJS.Global>();
286286
global.__SENTRY__ = global.__SENTRY__ || {};
287287
global.__SENTRY__.globalEventProcessors = global.__SENTRY__.globalEventProcessors || [];
288288
return global.__SENTRY__.globalEventProcessors;

packages/hub/test/global.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ describe('global', () => {
88

99
test('getHubFromCarrier', () => {
1010
const bla = { a: 'b' };
11-
getHubFromCarrier(bla);
11+
getHubFromCarrier(bla as any);
1212
expect((bla as any).__SENTRY__.hub).toBeTruthy();
1313
expect((bla as any).__SENTRY__.hub).toBe((bla as any).__SENTRY__.hub);
14-
getHubFromCarrier(bla);
14+
getHubFromCarrier(bla as any);
1515
});
1616

1717
test('getGlobalHub', () => {

packages/integrations/src/angular.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,8 @@ export class Angular implements Integration {
3939
* @inheritDoc
4040
*/
4141
public constructor(options: { angular?: ng.IAngularStatic } = {}) {
42-
this._angular =
43-
options.angular ||
44-
(getGlobalObject() as {
45-
angular: ng.IAngularStatic;
46-
}).angular;
42+
// tslint:disable-next-line: no-unsafe-any
43+
this._angular = options.angular || (getGlobalObject().angular as ng.IAngularStatic);
4744
}
4845

4946
/**

packages/integrations/src/ember.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ export class Ember implements Integration {
2121
* @inheritDoc
2222
*/
2323
public constructor(options: { Ember?: any } = {}) {
24-
this._Ember =
25-
options.Ember ||
26-
(getGlobalObject() as {
27-
Ember: any;
28-
}).Ember;
24+
// tslint:disable-next-line: no-unsafe-any
25+
this._Ember = options.Ember || getGlobalObject().Ember;
2926
}
3027

3128
/**

packages/integrations/src/reportingobserver.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ export class ReportingObserver implements Integration {
9090

9191
this._getCurrentHub = getCurrentHub;
9292

93-
const observer = new (getGlobalObject() as {
94-
ReportingObserver: any;
95-
}).ReportingObserver(this.handler.bind(this), {
93+
const observer = new (getGlobalObject()).ReportingObserver(this.handler.bind(this), {
9694
buffered: true,
9795
types: this._options.types,
9896
});

packages/integrations/src/vue.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ export class Vue implements Integration {
3838
* @inheritDoc
3939
*/
4040
public constructor(options: { Vue?: any; attachProps?: boolean } = {}) {
41-
this._Vue =
42-
options.Vue ||
43-
(getGlobalObject() as {
44-
Vue: any;
45-
}).Vue;
41+
// tslint:disable-next-line: no-unsafe-any
42+
this._Vue = options.Vue || getGlobalObject().Vue;
4643
this._attachProps = options.attachProps || true;
4744
}
4845

packages/utils/src/logger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { consoleSandbox, getGlobalObject } from './misc';
22

33
// TODO: Implement different loggers for different environments
4-
const global = getGlobalObject() as Window;
4+
const global = getGlobalObject<Window | NodeJS.Global>();
55

66
/** Prefix for logging strings */
77
const PREFIX = 'Sentry Logger ';
@@ -57,6 +57,8 @@ class Logger {
5757
}
5858
}
5959

60-
const logger = new Logger();
60+
// Ensure we only have a single logger instance, even if multiple versions of @sentry/utils are being used
61+
global.__SENTRY__ = global.__SENTRY__ || {};
62+
const logger = (global.__SENTRY__.logger as Logger) || (global.__SENTRY__.logger = new Logger());
6163

6264
export { logger };

packages/utils/src/misc.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { Event, Mechanism, WrappedFunction } from '@sentry/types';
22

33
import { isString } from './is';
44

5+
/** Internal */
6+
interface SentryGlobal {
7+
__SENTRY__: {
8+
globalEventProcessors: any;
9+
hub: any;
10+
logger: any;
11+
};
12+
}
13+
514
/**
615
* Requires a module which is protected _against bundler minification.
716
*
@@ -29,14 +38,14 @@ const fallbackGlobalObject = {};
2938
* @returns Global scope object
3039
*/
3140
// tslint:disable:strict-type-predicates
32-
export function getGlobalObject(): Window | NodeJS.Global | {} {
33-
return isNodeEnv()
41+
export function getGlobalObject<T extends Window | NodeJS.Global = any>(): T & SentryGlobal {
42+
return (isNodeEnv()
3443
? global
3544
: typeof window !== 'undefined'
3645
? window
3746
: typeof self !== 'undefined'
3847
? self
39-
: fallbackGlobalObject;
48+
: fallbackGlobalObject) as T & SentryGlobal;
4049
}
4150
// tslint:enable:strict-type-predicates
4251

@@ -228,7 +237,7 @@ interface ExtensibleConsole extends Console {
228237

229238
/** JSDoc */
230239
export function consoleSandbox(callback: () => any): any {
231-
const global = getGlobalObject() as Window;
240+
const global = getGlobalObject<Window>();
232241
const levels = ['debug', 'info', 'warn', 'error', 'log'];
233242

234243
if (!('console' in global)) {

packages/utils/src/supports.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function supportsDOMException(): boolean {
5959
* @returns Answer to the given question.
6060
*/
6161
export function supportsFetch(): boolean {
62-
if (!('fetch' in getGlobalObject())) {
62+
if (!('fetch' in getGlobalObject<Window>())) {
6363
return false;
6464
}
6565

@@ -86,10 +86,8 @@ export function supportsNativeFetch(): boolean {
8686
if (!supportsFetch()) {
8787
return false;
8888
}
89-
const global = getGlobalObject();
90-
const fetch = (global as any).fetch;
91-
// tslint:disable-next-line:no-unsafe-any
92-
return fetch.toString().indexOf('native') !== -1;
89+
const global = getGlobalObject<Window>();
90+
return global.fetch.toString().indexOf('native') !== -1;
9391
}
9492

9593
/**
@@ -99,6 +97,7 @@ export function supportsNativeFetch(): boolean {
9997
* @returns Answer to the given question.
10098
*/
10199
export function supportsReportingObserver(): boolean {
100+
// tslint:disable-next-line: no-unsafe-any
102101
return 'ReportingObserver' in getGlobalObject();
103102
}
104103

@@ -139,7 +138,7 @@ export function supportsHistory(): boolean {
139138
// NOTE: in Chrome App environment, touching history.pushState, *even inside
140139
// a try/catch block*, will cause Chrome to output an error to console.error
141140
// borrowed from: https://github.com/angular/angular.js/pull/13945/files
142-
const global = getGlobalObject();
141+
const global = getGlobalObject<Window>();
143142
const chrome = (global as any).chrome;
144143
// tslint:disable-next-line:no-unsafe-any
145144
const isChromePackagedApp = chrome && chrome.app && chrome.app.runtime;

0 commit comments

Comments
 (0)