Skip to content

Commit faee062

Browse files
authored
chore(globalWindow): revert using Faro's globalObject (#915)
1 parent c66a1b7 commit faee062

File tree

8 files changed

+23
-24
lines changed

8 files changed

+23
-24
lines changed

experimental/instrumentation-fetch/src/instrumentation.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { globalObject, initializeFaro } from '@grafana/faro-core';
1+
import { initializeFaro } from '@grafana/faro-core';
22
import { mockConfig } from '@grafana/faro-core/src/testUtils';
33
import { FetchTransport, makeCoreConfig, SessionInstrumentation } from '@grafana/faro-web-sdk';
44

@@ -135,7 +135,7 @@ describe('FetchInstrumentation', () => {
135135
};
136136
};
137137

138-
const requestUrl = globalObject.location.origin + '/test';
138+
const requestUrl = window.location.origin + '/test';
139139

140140
const actualResult = parseActualResult(
141141
instrumentation.buildRequestAndInit(new Request(requestUrl), {
@@ -257,7 +257,7 @@ describe('FetchInstrumentation', () => {
257257
const mockFetch = jest.fn();
258258
jest.spyOn(global, 'fetch').mockImplementationOnce(mockFetch);
259259

260-
globalObject.fetch('https://grafana.com');
260+
window.fetch('https://grafana.com');
261261

262262
expect(mockFetch).toHaveBeenCalledTimes(1);
263263
});
@@ -279,7 +279,7 @@ describe('FetchInstrumentation', () => {
279279
const mockPushEventApi = jest.fn();
280280
jest.spyOn(faro.api, 'pushEvent').mockImplementationOnce(mockPushEventApi);
281281

282-
globalObject.fetch('https://example.com');
282+
window.fetch('https://example.com');
283283

284284
expect(mockPushEventApi).toHaveBeenCalledTimes(0);
285285
});

experimental/instrumentation-fetch/src/instrumentation.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseInstrumentation, faro, globalObject, isString, VERSION } from '@grafana/faro-core';
1+
import { BaseInstrumentation, faro, isString, VERSION } from '@grafana/faro-core';
22
import type { Patterns } from '@grafana/faro-core';
33

44
import {
@@ -22,21 +22,21 @@ function isRequest(input: any): input is Request {
2222
export class FetchInstrumentation extends BaseInstrumentation {
2323
readonly name = '@grafana/faro-web-sdk:instrumentation-fetch';
2424
readonly version = VERSION;
25-
readonly originalFetch: WindowFetch = globalObject.fetch.bind(globalObject);
25+
readonly originalFetch: WindowFetch = window.fetch.bind(window);
2626
private ignoredUrls: FetchInstrumentationOptions['ignoredUrls'];
2727

2828
constructor(private options?: FetchInstrumentationOptions) {
2929
super();
3030
}
3131

3232
/**
33-
* Initialize fetch instrumentation - globalObject.fetch becomes instrumented fetch, assign original fetch to globalObject.originalFetch
33+
* Initialize fetch instrumentation - window.fetch becomes instrumented fetch, assign original fetch to window.originalFetch
3434
*/
3535
initialize(): void {
3636
this.internalLogger.info('Initializing fetch instrumentation');
3737
this.ignoredUrls = [...(this.options?.ignoredUrls ?? []), ...(this.getTransportIgnoreUrls() ?? [])];
3838

39-
Object.defineProperty(globalObject, fetchGlobalObjectKey, {
39+
Object.defineProperty(window, fetchGlobalObjectKey, {
4040
configurable: true,
4141
writable: this.options?.testing ? true : false, // necessary for testing, instrumented fetch should not be writeable by default
4242
value: this.instrumentFetch().bind(this),
@@ -88,7 +88,7 @@ export class FetchInstrumentation extends BaseInstrumentation {
8888
}
8989

9090
// add Faro RUM header to the request headers
91-
const windowOrigin = globalObject.location.origin;
91+
const windowOrigin = window.location.origin;
9292
const shouldAddRumHeaderToUrl = shouldPropagateRumHeaders(this.getRequestUrl(input), [
9393
...(this.options?.propagateRumHeaderCorsUrls ?? []),
9494
windowOrigin,

experimental/instrumentation-xhr/src/instrumentation.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { globalObject, initializeFaro } from '@grafana/faro-core';
1+
import { initializeFaro } from '@grafana/faro-core';
22
import { mockConfig, MockTransport } from '@grafana/faro-core/src/testUtils';
33
import { FetchTransport, makeCoreConfig, SessionInstrumentation } from '@grafana/faro-web-sdk';
44

@@ -54,7 +54,7 @@ describe('XHRInstrumentation', () => {
5454

5555
const xhr = new XMLHttpRequest();
5656
// auto adds rum headers to requests sent to the same origin
57-
xhr.open('GET', globalObject.location.origin + '/test');
57+
xhr.open('GET', window.location.origin + '/test');
5858
expect(mockFetchSpyOpen).toHaveBeenCalledTimes(1);
5959

6060
xhr.send();

experimental/instrumentation-xhr/src/instrumentation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseInstrumentation, faro, globalObject, VERSION } from '@grafana/faro-core';
1+
import { BaseInstrumentation, faro, VERSION } from '@grafana/faro-core';
22

33
import { faroRumHeader, makeFaroRumHeaderValue, XHREventType, XHRInstrumentationOptions } from './types';
44
import { parseXHREvent, parseXHRHeaders, shouldPropagateRumHeaders } from './utils';
@@ -70,7 +70,7 @@ export class XHRInstrumentation extends BaseInstrumentation {
7070
}
7171

7272
// add Faro RUM header to the request headers
73-
const windowOrigin = globalObject.location.origin;
73+
const windowOrigin = window.location.origin;
7474
const shouldAddRumHeaderToUrl = shouldPropagateRumHeaders(requestUrl, [
7575
...(instrumentation?.options?.propagateRumHeaderCorsUrls ?? []),
7676
windowOrigin,

packages/web-sdk/src/instrumentations/errors/registerOnerror.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { globalObject } from '@grafana/faro-core';
21
import { mockConfig, MockTransport } from '@grafana/faro-core/src/testUtils';
32

43
import { initializeFaro } from '../../initialize';
@@ -9,7 +8,7 @@ describe('registerOnerror', () => {
98
it('will preserve the old callback', () => {
109
let called = false;
1110

12-
globalObject.onerror = () => {
11+
window.onerror = () => {
1312
called = true;
1413
};
1514

@@ -22,7 +21,7 @@ describe('registerOnerror', () => {
2221

2322
registerOnerror(api);
2423

25-
globalObject.onerror('boo', 'some file', 10, 10, new Error('boo'));
24+
window.onerror('boo', 'some file', 10, 10, new Error('boo'));
2625
expect(called).toBe(true);
2726
expect(transport.items).toHaveLength(1);
2827
});

packages/web-sdk/src/instrumentations/errors/registerOnerror.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { globalObject, isString } from '@grafana/faro-core';
1+
import { isString } from '@grafana/faro-core';
22
import type { API, ExceptionStackFrame } from '@grafana/faro-core';
33

44
import { unknownSymbolString } from './const';
@@ -7,9 +7,9 @@ import { getValueAndTypeFromMessage } from './getValueAndTypeFromMessage';
77
import { buildStackFrame } from './stackFrames';
88

99
export function registerOnerror(api: API): void {
10-
const oldOnerror = globalObject.onerror;
10+
const oldOnerror = window.onerror;
1111

12-
globalObject.onerror = (...args) => {
12+
window.onerror = (...args) => {
1313
try {
1414
const [evt, source, lineno, colno, error] = args;
1515
let value: string | undefined;

packages/web-sdk/src/instrumentations/errors/registerOnunhandledrejection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ExceptionStackFrame, globalObject, isPrimitive } from '@grafana/faro-core';
1+
import { ExceptionStackFrame, isPrimitive } from '@grafana/faro-core';
22
import type { API } from '@grafana/faro-core';
33

44
import { primitiveUnhandledType, primitiveUnhandledValue } from './const';
55
import { getErrorDetails } from './getErrorDetails';
66
import type { ExtendedPromiseRejectionEvent } from './types';
77

88
export function registerOnunhandledrejection(api: API): void {
9-
globalObject.addEventListener('unhandledrejection', (evt: ExtendedPromiseRejectionEvent) => {
9+
window.addEventListener('unhandledrejection', (evt: ExtendedPromiseRejectionEvent) => {
1010
let error = evt;
1111

1212
if (error.reason) {

packages/web-sdk/src/metas/browser/meta.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { UAParser } from 'ua-parser-js';
22

3-
import { globalObject, unknownString } from '@grafana/faro-core';
3+
import { unknownString } from '@grafana/faro-core';
44
import type { Meta, MetaBrowser, MetaItem } from '@grafana/faro-core';
55

66
export const browserMeta: MetaItem<Pick<Meta, 'browser'>> = () => {
@@ -21,8 +21,8 @@ export const browserMeta: MetaItem<Pick<Meta, 'browser'>> = () => {
2121
language: language ?? unknownString,
2222
mobile,
2323
brands: brands ?? unknownString,
24-
viewportWidth: `${globalObject.innerWidth}`,
25-
viewportHeight: `${globalObject.innerHeight}`,
24+
viewportWidth: `${window.innerWidth}`,
25+
viewportHeight: `${window.innerHeight}`,
2626
},
2727
};
2828

0 commit comments

Comments
 (0)