Skip to content

Commit 690f74f

Browse files
authored
ref(replay): TS fixes for replay (#6286)
1 parent 9193b71 commit 690f74f

23 files changed

+108
-73
lines changed

packages/core/test/mocks/transport.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Transport } from '@sentry/types';
12
import { SyncPromise } from '@sentry/utils';
23
import { TextEncoder } from 'util';
34

@@ -7,7 +8,12 @@ async function sleep(delay: number): Promise<void> {
78
return new SyncPromise(resolve => setTimeout(resolve, delay));
89
}
910

10-
export function makeFakeTransport(delay: number = 2000) {
11+
export function makeFakeTransport(delay: number = 2000): {
12+
makeTransport: () => Transport;
13+
getSendCalled: () => number;
14+
getSentCount: () => number;
15+
delay: number;
16+
} {
1117
let sendCalled = 0;
1218
let sentCount = 0;
1319
const makeTransport = () =>

packages/replay/jest.setup.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ type SentReplayExpected = {
3535
};
3636
};
3737
replayEventHeader?: { type: 'replay_event' };
38-
replayEventPayload?: Record<string, any>;
38+
replayEventPayload?: Record<string, unknown>;
3939
recordingHeader?: { type: 'replay_recording'; length: number };
40-
recordingPayloadHeader?: Record<string, any>;
40+
recordingPayloadHeader?: Record<string, unknown>;
4141
events?: string | Uint8Array;
4242
};
4343

@@ -101,9 +101,13 @@ const toHaveSentReplay = function (
101101
}
102102

103103
const results = expected
104-
? Object.entries(actualObj)
105-
.map(([key, val]: [keyof SentReplayExpected, any]) => {
106-
return [!expectedObj?.[key] || this.equals(expectedObj[key], val), key, expectedObj?.[key], val];
104+
? Object.keys(expectedObj)
105+
.map(key => {
106+
const actualVal = actualObj[key as keyof SentReplayExpected];
107+
const expectedVal = expectedObj[key as keyof SentReplayExpected];
108+
const matches = !expectedVal || this.equals(actualVal, expectedVal);
109+
110+
return [matches, key, expectedVal, actualVal];
107111
})
108112
.filter(([passed]) => !passed)
109113
: [];

packages/replay/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"homepage": "https://github.com/getsentry/sentry-replay#readme",
4646
"devDependencies": {
4747
"@babel/core": "^7.17.5",
48-
"@sentry/browser": "7.21.0",
48+
"@sentry/browser": "7.21.1",
4949
"@types/lodash.debounce": "^4.0.7",
5050
"@types/lodash.throttle": "^4.1.7",
5151
"@types/pako": "^2.0.0",
@@ -54,9 +54,9 @@
5454
"tslib": "^1.9.3"
5555
},
5656
"dependencies": {
57-
"@sentry/core": "7.21.0",
58-
"@sentry/types": "7.21.0",
59-
"@sentry/utils": "7.21.0",
57+
"@sentry/core": "7.21.1",
58+
"@sentry/types": "7.21.1",
59+
"@sentry/utils": "7.21.1",
6060
"lodash.debounce": "^4.0.8",
6161
"rrweb": "^1.1.3"
6262
},
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Breadcrumb, Scope } from '@sentry/types';
2+
3+
import { InstrumentationTypeBreadcrumb } from '../types';
4+
import { DomHandlerData, handleDom } from './handleDom';
5+
import { handleScope } from './handleScope';
6+
7+
export function breadcrumbHandler(type: InstrumentationTypeBreadcrumb, handlerData: unknown): Breadcrumb | null {
8+
if (type === 'scope') {
9+
return handleScope(handlerData as Scope);
10+
}
11+
12+
return handleDom(handlerData as DomHandlerData);
13+
}

packages/replay/src/coreHandlers/getBreadcrumbHandler.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

packages/replay/src/coreHandlers/getSpanHandler.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/replay/src/coreHandlers/handleDom.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import { record } from 'rrweb';
44

55
import { createBreadcrumb } from '../util/createBreadcrumb';
66

7-
export function handleDom(handlerData: any): Breadcrumb | null {
7+
export interface DomHandlerData {
8+
name: string;
9+
event: Node | { target: Node };
10+
}
11+
12+
export function handleDom(handlerData: DomHandlerData): Breadcrumb | null {
813
// Taken from https://github.com/getsentry/sentry-javascript/blob/master/packages/browser/src/integrations/breadcrumbs.ts#L112
914
let target;
1015
let targetNode;
1116

1217
// Accessing event.target can throw (see getsentry/raven-js#838, #768)
1318
try {
14-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
15-
targetNode = (handlerData.event.target as Node) || (handlerData.event as unknown as Node);
19+
targetNode = getTargetNode(handlerData);
1620
target = htmlTreeAsString(targetNode);
1721
} catch (e) {
1822
target = '<unknown>';
@@ -23,12 +27,24 @@ export function handleDom(handlerData: any): Breadcrumb | null {
2327
}
2428

2529
return createBreadcrumb({
26-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
2730
category: `ui.${handlerData.name}`,
2831
message: target,
2932
data: {
3033
// Not sure why this errors, Node should be correct (Argument of type 'Node' is not assignable to parameter of type 'INode')
34+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3135
...(targetNode ? { nodeId: record.mirror.getId(targetNode as any) } : {}),
3236
},
3337
});
3438
}
39+
40+
function getTargetNode(handlerData: DomHandlerData): Node {
41+
if (isEventWithTarget(handlerData.event)) {
42+
return handlerData.event.target;
43+
}
44+
45+
return handlerData.event;
46+
}
47+
48+
function isEventWithTarget(event: unknown): event is { target: Node } {
49+
return !!(event as { target?: Node }).target;
50+
}

packages/replay/src/coreHandlers/handleFetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ReplayPerformanceEntry } from '../createPerformanceEntry';
22
import { isIngestHost } from '../util/isIngestHost';
33

4-
interface FetchHandlerData {
4+
export interface FetchHandlerData {
55
args: Parameters<typeof fetch>;
66
fetchData: {
77
method: string;

packages/replay/src/coreHandlers/handleHistory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReplayPerformanceEntry } from '../createPerformanceEntry';
22

3-
interface HistoryHandlerData {
3+
export interface HistoryHandlerData {
44
from: string;
55
to: string;
66
}

packages/replay/src/coreHandlers/handleXhr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface SentryWrappedXMLHttpRequest extends XMLHttpRequest {
1919
__sentry_own_request__?: boolean;
2020
}
2121

22-
interface XhrHandlerData {
22+
export interface XhrHandlerData {
2323
args: [string, string];
2424
xhr: SentryWrappedXMLHttpRequest;
2525
startTimestamp: number;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ReplayPerformanceEntry } from '../createPerformanceEntry';
2+
import { InstrumentationTypeSpan } from '../types';
3+
import { FetchHandlerData, handleFetch } from './handleFetch';
4+
import { handleHistory, HistoryHandlerData } from './handleHistory';
5+
import { handleXhr, XhrHandlerData } from './handleXhr';
6+
7+
export function spanHandler(type: InstrumentationTypeSpan, handlerData: unknown): null | ReplayPerformanceEntry {
8+
if (type === 'fetch') {
9+
return handleFetch(handlerData as FetchHandlerData);
10+
}
11+
12+
if (type === 'xhr') {
13+
return handleXhr(handlerData as XhrHandlerData);
14+
}
15+
16+
return handleHistory(handlerData as HistoryHandlerData);
17+
}

packages/replay/src/createPerformanceEntry.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,15 @@ function createLargestContentfulPaint(entry: PerformanceEntry & { size: number;
143143
duration,
144144
size,
145145
// Not sure why this errors, Node should be correct (Argument of type 'Node' is not assignable to parameter of type 'INode')
146+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
146147
nodeId: record.mirror.getId(entry.element as any),
147148
},
148149
};
149150
}
150151

151-
// TODO: type definition!
152-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
153-
export function createMemoryEntry(memoryEntry: MemoryInfo) {
152+
type ReplayMemoryEntry = ReplayPerformanceEntry & { data: { memory: MemoryInfo } };
153+
154+
export function createMemoryEntry(memoryEntry: MemoryInfo): ReplayMemoryEntry {
154155
const { jsHeapSizeLimit, totalJSHeapSize, usedJSHeapSize } = memoryEntry;
155156
// we don't want to use `getAbsoluteTime` because it adds the event time to the
156157
// time origin, so we get the current timestamp instead

packages/replay/src/index.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import debounce from 'lodash.debounce';
66
import { PerformanceObserverEntryList } from 'perf_hooks';
77
import { EventType, record } from 'rrweb';
88

9-
import { getBreadcrumbHandler } from './coreHandlers/getBreadcrumbHandler';
10-
import { getSpanHandler } from './coreHandlers/getSpanHandler';
9+
import { breadcrumbHandler } from './coreHandlers/breadcrumbHandler';
10+
import { spanHandler } from './coreHandlers/spanHandler';
1111
import { createMemoryEntry, createPerformanceEntries, ReplayPerformanceEntry } from './createPerformanceEntry';
1212
import { createEventBuffer, IEventBuffer } from './eventBuffer';
1313
import {
@@ -705,14 +705,14 @@ export class Replay implements Integration {
705705
*
706706
* These specific events will create span-like objects in the recording.
707707
*/
708-
handleCoreSpanListener: (type: InstrumentationTypeSpan) => (handlerData: any) => void =
709-
(type: InstrumentationTypeSpan) => (handlerData: any) => {
708+
handleCoreSpanListener: (type: InstrumentationTypeSpan) => (handlerData: unknown) => void =
709+
(type: InstrumentationTypeSpan) =>
710+
(handlerData: unknown): void => {
710711
if (!this.isEnabled) {
711712
return;
712713
}
713714

714-
const handler = getSpanHandler(type);
715-
const result = handler(handlerData);
715+
const result = spanHandler(type, handlerData);
716716

717717
if (result === null) {
718718
return;
@@ -738,14 +738,14 @@ export class Replay implements Integration {
738738
*
739739
* These events will create breadcrumb-like objects in the recording.
740740
*/
741-
handleCoreBreadcrumbListener: (type: InstrumentationTypeBreadcrumb) => (handlerData: any) => void =
742-
(type: InstrumentationTypeBreadcrumb) => (handlerData: any) => {
741+
handleCoreBreadcrumbListener: (type: InstrumentationTypeBreadcrumb) => (handlerData: unknown) => void =
742+
(type: InstrumentationTypeBreadcrumb) =>
743+
(handlerData: unknown): void => {
743744
if (!this.isEnabled) {
744745
return;
745746
}
746747

747-
const handler = getBreadcrumbHandler(type);
748-
const result = handler(handlerData);
748+
const result = breadcrumbHandler(type, handlerData);
749749

750750
if (result === null) {
751751
return;
@@ -1174,7 +1174,7 @@ export class Replay implements Integration {
11741174
* from calling both `flush` and `debouncedFlush`. Otherwise, there could be
11751175
* cases of mulitple flushes happening closely together.
11761176
*/
1177-
flushImmediate(): any {
1177+
flushImmediate(): Promise<void> {
11781178
this.debouncedFlush();
11791179
// `.flush` is provided by lodash.debounce
11801180
return this.debouncedFlush.flush();

packages/replay/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export type InstrumentationType =
3030
export interface WorkerRequest {
3131
id: number;
3232
method: string;
33-
args: any[];
33+
args: unknown[];
3434
}
3535

3636
declare global {

packages/replay/src/util/createPayload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export function createPayload({
55
headers,
66
}: {
77
events: RecordedEvents;
8-
headers: Record<string, any>;
8+
headers: Record<string, unknown>;
99
}): string | Uint8Array {
1010
let payloadWithSequence;
1111

packages/replay/src/util/dedupePerformanceEntries.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ function isNavigationEntryEqual(a: PerformanceNavigationTiming) {
2323
*/
2424
// TODO (high-prio): Figure out wth is returned here
2525
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
26-
export function dedupePerformanceEntries(currentList: PerformanceEntryList, newList: PerformanceEntryList) {
26+
export function dedupePerformanceEntries(
27+
currentList: PerformanceEntryList,
28+
newList: PerformanceEntryList,
29+
): PerformanceEntryList {
2730
// Partition `currentList` into 3 different lists based on entryType
2831
const [existingNavigationEntries, existingLcpEntries, existingEntries] = currentList.reduce(
2932
(acc: [PerformanceNavigationTiming[], PerformancePaintTiming[], PerformanceEntryList], entry) => {

packages/replay/src/util/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { logger as sentryLogger } from '@sentry/utils';
22

33
function wrapLogger(logFn: typeof sentryLogger[keyof typeof sentryLogger]) {
4-
return function wrappedLog(...args: any[]) {
4+
return function wrappedLog(...args: unknown[]) {
55
return logFn.call(sentryLogger, '[Replay]', ...args);
66
};
77
}

packages/replay/test/fixtures/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SeverityLevel } from '@sentry/browser';
22
import { Event } from '@sentry/types';
33

4-
export function Error(obj?: Event) {
4+
export function Error(obj?: Event): any {
55
const timestamp = new Date().getTime() / 1000;
66

77
return {

packages/replay/test/fixtures/transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Event, SeverityLevel } from '@sentry/types';
22

3-
export function Transaction(obj?: Partial<Event>) {
3+
export function Transaction(obj?: Partial<Event>): any {
44
const timestamp = new Date().getTime() / 1000;
55

66
return {

packages/replay/test/mocks/mockRrweb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jest.mock('rrweb', () => {
4949
// XXX: Intended to be after `mock('rrweb')`
5050
import * as rrweb from 'rrweb';
5151

52-
export function mockRrweb() {
52+
export function mockRrweb(): { record: RecordMock } {
5353
return {
5454
record: rrweb.record as RecordMock,
5555
};

packages/replay/test/utils/use-fake-timers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function useFakeTimers() {
1+
export function useFakeTimers(): void {
22
const _setInterval = setInterval;
33
const _clearInterval = clearInterval;
44
jest.useFakeTimers();

packages/replay/worker/src/Compressor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Compressor {
2525
return;
2626
}
2727

28-
public addEvent(data: Record<string, any>): void {
28+
public addEvent(data: Record<string, unknown>): void {
2929
if (!data) {
3030
return;
3131
}

packages/replay/worker/src/handleMessage.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@ import { Compressor } from './Compressor';
33

44
const compressor = new Compressor();
55

6-
const handlers: Record<string, (args: any[]) => void> = {
6+
interface Handlers {
7+
init: () => void;
8+
addEvent: (data: Record<string, unknown>) => void;
9+
finish: () => void;
10+
}
11+
12+
const handlers: Handlers = {
713
init: () => {
814
compressor.init();
915
return '';
1016
},
1117

12-
addEvent: (data: Record<string, any>) => {
18+
addEvent: (data: Record<string, unknown>) => {
1319
compressor.addEvent(data);
1420
return '';
1521
},

0 commit comments

Comments
 (0)