Skip to content

Commit

Permalink
ref(replay): Extract types into types.ts
Browse files Browse the repository at this point in the history
To avoid circular dependencies.
  • Loading branch information
mydea committed Dec 7, 2022
1 parent a1343b3 commit 12840da
Show file tree
Hide file tree
Showing 28 changed files with 94 additions and 81 deletions.
7 changes: 0 additions & 7 deletions packages/replay/.madgerc

This file was deleted.

3 changes: 1 addition & 2 deletions packages/replay/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import { getCurrentHub } from '@sentry/core';
import { Transport } from '@sentry/types';

import { ReplayContainer } from './src/replay';
import { Session } from './src/session/Session';
import type { ReplayContainer, Session } from './src/types';

// @ts-ignore TS error, this is replaced in prod builds bc of rollup
global.__SENTRY_REPLAY_VERSION__ = 'version:Test';
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/coreHandlers/breadcrumbHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Breadcrumb, Scope } from '@sentry/types';

import { InstrumentationTypeBreadcrumb } from '../types';
import type { InstrumentationTypeBreadcrumb } from '../types';
import { DomHandlerData, handleDom } from './handleDom';
import { handleScope } from './handleScope';

Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/coreHandlers/handleFetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReplayPerformanceEntry } from '../createPerformanceEntry';
import type { ReplayContainer } from '../replay';
import type { ReplayContainer } from '../types';
import { createPerformanceSpans } from '../util/createPerformanceSpans';
import { isIngestHost } from '../util/isIngestHost';

Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/coreHandlers/handleGlobalEvent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Event } from '@sentry/types';

import { REPLAY_EVENT_NAME, UNABLE_TO_SEND_REPLAY } from '../constants';
import type { ReplayContainer } from '../replay';
import type { ReplayContainer } from '../types';
import { addInternalBreadcrumb } from '../util/addInternalBreadcrumb';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/coreHandlers/handleHistory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReplayPerformanceEntry } from '../createPerformanceEntry';
import type { ReplayContainer } from '../replay';
import type { ReplayContainer } from '../types';
import { createPerformanceSpans } from '../util/createPerformanceSpans';

interface HistoryHandlerData {
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/coreHandlers/handleXhr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReplayPerformanceEntry } from '../createPerformanceEntry';
import type { ReplayContainer } from '../replay';
import type { ReplayContainer } from '../types';
import { createPerformanceSpans } from '../util/createPerformanceSpans';
import { isIngestHost } from '../util/isIngestHost';

Expand Down
3 changes: 1 addition & 2 deletions packages/replay/src/coreHandlers/performanceObserver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ReplayContainer } from '../replay';
import { AllPerformanceEntry } from '../types';
import type { AllPerformanceEntry, ReplayContainer } from '../types';
import { dedupePerformanceEntries } from '../util/dedupePerformanceEntries';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/createPerformanceEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { browserPerformanceTimeOrigin } from '@sentry/utils';
import { record } from 'rrweb';

import { WINDOW } from './constants';
import { AllPerformanceEntry, PerformanceNavigationTiming, PerformancePaintTiming } from './types';
import type { AllPerformanceEntry, PerformanceNavigationTiming, PerformancePaintTiming } from './types';
import { isIngestHost } from './util/isIngestHost';

export interface ReplayPerformanceEntry {
Expand Down
9 changes: 1 addition & 8 deletions packages/replay/src/eventBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { captureException } from '@sentry/core';
import { logger } from '@sentry/utils';

import { RecordingEvent, WorkerRequest, WorkerResponse } from './types';
import type { EventBuffer, RecordingEvent, WorkerRequest, WorkerResponse } from './types';
import workerString from './worker/worker.js';

interface CreateEventBufferParams {
Expand Down Expand Up @@ -35,13 +35,6 @@ export function createEventBuffer({ useCompression }: CreateEventBufferParams):
return new EventBufferArray();
}

export interface EventBuffer {
readonly length: number;
destroy(): void;
addEvent(event: RecordingEvent, isCheckout?: boolean): void;
finish(): Promise<string | Uint8Array>;
}

class EventBufferArray implements EventBuffer {
private _events: RecordingEvent[];

Expand Down
12 changes: 7 additions & 5 deletions packages/replay/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,23 @@ import { handleHistorySpanListener } from './coreHandlers/handleHistory';
import { handleXhrSpanListener } from './coreHandlers/handleXhr';
import { setupPerformanceObserver } from './coreHandlers/performanceObserver';
import { createPerformanceEntries } from './createPerformanceEntry';
import { createEventBuffer, EventBuffer } from './eventBuffer';
import { createEventBuffer } from './eventBuffer';
import { deleteSession } from './session/deleteSession';
import { getSession } from './session/getSession';
import { saveSession } from './session/saveSession';
import { Session } from './session/Session';
import type {
AddUpdateCallback,
AllPerformanceEntry,
EventBuffer,
InstrumentationTypeBreadcrumb,
InternalEventContext,
PopEventContext,
RecordingEvent,
RecordingOptions,
ReplayContainer as ReplayContainerInterface,
ReplayPluginOptions,
SendReplay,
Session,
} from './types';
import { addEvent } from './util/addEvent';
import { addMemoryEntry } from './util/addMemoryEntry';
Expand All @@ -48,12 +51,11 @@ import { overwriteRecordDroppedEvent, restoreRecordDroppedEvent } from './util/m
/**
* Returns true to return control to calling function, otherwise continue with normal batching
*/
type AddUpdateCallback = () => boolean | void;

const BASE_RETRY_INTERVAL = 5000;
const MAX_RETRY_COUNT = 3;

export class ReplayContainer {
export class ReplayContainer implements ReplayContainerInterface {
public eventBuffer: EventBuffer | null = null;

/**
Expand Down Expand Up @@ -398,7 +400,7 @@ export class ReplayContainer {
* Accepts a callback to perform side-effects and returns true to stop batch
* processing and hand back control to caller.
*/
addUpdate(cb?: AddUpdateCallback): void {
addUpdate(cb: AddUpdateCallback): void {
// We need to always run `cb` (e.g. in the case of `this._waitForError == true`)
const cbResult = cb?.();

Expand Down
33 changes: 1 addition & 32 deletions packages/replay/src/session/Session.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
import { uuid4 } from '@sentry/utils';

import type { Sampled, Session } from '../types';
import { isSampled } from '../util/isSampled';

type Sampled = false | 'session' | 'error';

export interface Session {
id: string;

/**
* Start time of current session
*/
started: number;

/**
* Last known activity of the session
*/
lastActivity: number;

/**
* Segment ID for replay events
*/
segmentId: number;

/**
* The ID of the previous session.
* If this is empty, there was no previous session.
*/
previousSessionId?: string;

/**
* Is the session sampled? `false` if not sampled, otherwise, `session` or `error`
*/
sampled: Sampled;
}

/**
* Get a session with defaults & applied sampling.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/replay/src/session/createSession.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { logger } from '@sentry/utils';

import { SessionOptions } from '../types';
import type { Session, SessionOptions } from '../types';
import { saveSession } from './saveSession';
import { getSessionSampleType, makeSession, Session } from './Session';
import { getSessionSampleType, makeSession } from './Session';

/**
* Create a new session, which in its current implementation is a Sentry event
Expand Down
3 changes: 2 additions & 1 deletion packages/replay/src/session/fetchSession.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { REPLAY_SESSION_KEY, WINDOW } from '../constants';
import { makeSession, Session } from './Session';
import type { Session } from '../types';
import { makeSession } from './Session';

/**
* Fetches a session from storage
Expand Down
3 changes: 1 addition & 2 deletions packages/replay/src/session/getSession.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { logger } from '@sentry/utils';

import { SessionOptions } from '../types';
import type { Session, SessionOptions } from '../types';
import { isSessionExpired } from '../util/isSessionExpired';
import { createSession } from './createSession';
import { fetchSession } from './fetchSession';
import { Session } from './Session';

interface GetSessionParams extends SessionOptions {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/session/saveSession.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { REPLAY_SESSION_KEY, WINDOW } from '../constants';
import { Session } from './Session';
import type { Session } from '../types';

export function saveSession(session: Session): void {
const hasSessionStorage = 'sessionStorage' in WINDOW;
Expand Down
59 changes: 59 additions & 0 deletions packages/replay/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,62 @@ export interface InternalEventContext extends CommonEventContext {
*/
earliestEvent: number | null;
}

export type Sampled = false | 'session' | 'error';

export interface Session {
id: string;

/**
* Start time of current session
*/
started: number;

/**
* Last known activity of the session
*/
lastActivity: number;

/**
* Segment ID for replay events
*/
segmentId: number;

/**
* The ID of the previous session.
* If this is empty, there was no previous session.
*/
previousSessionId?: string;

/**
* Is the session sampled? `false` if not sampled, otherwise, `session` or `error`
*/
sampled: Sampled;
}

export interface EventBuffer {
readonly length: number;
destroy(): void;
addEvent(event: RecordingEvent, isCheckout?: boolean): void;
finish(): Promise<string | Uint8Array>;
}

export type AddUpdateCallback = () => boolean | void;

export interface ReplayContainer {
eventBuffer: EventBuffer | null;
performanceEvents: AllPerformanceEntry[];
session: Session | undefined;
isEnabled(): boolean;
isPaused(): boolean;
getContext(): InternalEventContext;
start(): void;
stop(): void;
pause(): void;
resume(): void;
startRecording(): void;
stopRecording(): boolean;
flushImmediate(): void;
triggerUserActivity(): void;
addUpdate(cb: AddUpdateCallback): void;
}
3 changes: 1 addition & 2 deletions packages/replay/src/util/addEvent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SESSION_IDLE_DURATION } from '../constants';
import type { ReplayContainer } from '../replay';
import { RecordingEvent } from '../types';
import type { RecordingEvent, ReplayContainer } from '../types';

/**
* Add an event to the event buffer
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/util/addMemoryEntry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WINDOW } from '../constants';
import type { ReplayContainer } from '../replay';
import type { ReplayContainer } from '../types';
import { createPerformanceSpans } from './createPerformanceSpans';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/util/createPayload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RecordedEvents } from '../types';
import type { RecordedEvents } from '../types';

export function createPayload({
events,
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/util/createPerformanceSpans.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventType } from 'rrweb';

import { ReplayPerformanceEntry } from '../createPerformanceEntry';
import type { ReplayContainer } from '../replay';
import type { ReplayContainer } from '../types';
import { addEvent } from './addEvent';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/util/isSessionExpired.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MAX_SESSION_LIFE } from '../constants';
import { Session } from '../session/Session';
import type { Session } from '../types';
import { isExpired } from './isExpired';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/test/fixtures/performanceEntry/lcp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PerformancePaintTiming } from '../../../src/types';
import type { PerformancePaintTiming } from '../../../src/types';

export function PerformanceEntryLcp(obj?: Partial<PerformancePaintTiming>): PerformancePaintTiming {
const entry = {
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/test/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getCurrentHub } from '@sentry/core';

import { ReplayContainer } from '../../src/replay';
import { BASE_TIMESTAMP, RecordMock } from './../index';
import { DomHandler, MockTransportSend } from './../types';
import type { DomHandler, MockTransportSend } from './../types';
import { MockSdkParams } from './mockSdk';

export async function resetSdkMock({ replayOptions, sentryOptions }: MockSdkParams): Promise<{
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/test/mocks/mockRrweb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { record as rrwebRecord } from 'rrweb';

import { RecordingEvent } from '../../src/types';
import type { RecordingEvent } from '../../src/types';

type RecordAdditionalProperties = {
takeFullSnapshot: jest.Mock;
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/test/mocks/mockSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Envelope, Transport } from '@sentry/types';

import { Replay as ReplayIntegration } from '../../src';
import { ReplayContainer } from '../../src/replay';
import { ReplayConfiguration } from '../../src/types';
import type { ReplayConfiguration } from '../../src/types';

export interface MockSdkParams {
replayOptions?: ReplayConfiguration;
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/test/unit/index-errorSampleRate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ReplayContainer } from './../../src/replay';
import { PerformanceEntryResource } from './../fixtures/performanceEntry/resource';
import { BASE_TIMESTAMP, RecordMock } from './../index';
import { resetSdkMock } from './../mocks';
import { DomHandler, MockTransportSend } from './../types';
import type { DomHandler, MockTransportSend } from './../types';
import { useFakeTimers } from './../utils/use-fake-timers';

useFakeTimers();
Expand Down
4 changes: 2 additions & 2 deletions packages/replay/test/unit/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { EventType } from 'rrweb';

import { MAX_SESSION_LIFE, REPLAY_SESSION_KEY, VISIBILITY_CHANGE_TIMEOUT, WINDOW } from '../../src/constants';
import { ReplayContainer } from '../../src/replay';
import { RecordingEvent } from '../../src/types';
import type { RecordingEvent } from '../../src/types';
import { addEvent } from '../../src/util/addEvent';
import { createPerformanceSpans } from '../../src/util/createPerformanceSpans';
import { useFakeTimers } from '../utils/use-fake-timers';
import { PerformanceEntryResource } from './../fixtures/performanceEntry/resource';
import { BASE_TIMESTAMP, RecordMock } from './../index';
import { resetSdkMock } from './../mocks';
import { DomHandler, MockTransportSend } from './../types';
import type { DomHandler, MockTransportSend } from './../types';

useFakeTimers();

Expand Down

0 comments on commit 12840da

Please sign in to comment.