Skip to content

Commit 8f90a29

Browse files
committed
WIP wait for response instead of request
1 parent a2a627f commit 8f90a29

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

packages/integration-tests/utils/replayHelpers.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { RecordingEvent, ReplayContainer } from '@sentry/replay/build/npm/types/types';
22
import type { Breadcrumb, Event, ReplayEvent } from '@sentry/types';
33
import pako from 'pako';
4-
import type { Page, Request } from 'playwright';
4+
import type { Page, Request, Response } from 'playwright';
55

66
import { envelopeRequestParser } from './helpers';
77

@@ -37,8 +37,10 @@ type SnapshotNode = {
3737
* @param segmentId the segment_id of the replay event
3838
* @returns
3939
*/
40-
export function waitForReplayRequest(page: Page, segmentId?: number): Promise<Request> {
41-
return page.waitForRequest(req => {
40+
export function waitForReplayRequest(page: Page, segmentId?: number): Promise<Response> {
41+
return page.waitForResponse(res => {
42+
const req = res.request();
43+
4244
const postData = req.postData();
4345
if (!postData) {
4446
return false;
@@ -78,7 +80,13 @@ export async function getReplaySnapshot(page: Page): Promise<ReplayContainer> {
7880

7981
export const REPLAY_DEFAULT_FLUSH_MAX_DELAY = 5_000;
8082

81-
export function getReplayEvent(replayRequest: Request): ReplayEvent {
83+
function getRequest(resOrReq: Request | Response): Request {
84+
// @ts-ignore we check this
85+
return typeof resOrReq.request === 'function' ? (resOrReq as Response).request() : (resOrReq as Request);
86+
}
87+
88+
export function getReplayEvent(resOrReq: Request | Response): ReplayEvent {
89+
const replayRequest = getRequest(resOrReq);
8290
const event = envelopeRequestParser(replayRequest);
8391
if (!isReplayEvent(event)) {
8492
throw new Error('Request is not a replay event');
@@ -103,7 +111,8 @@ type RecordingContent = {
103111
* @param replayRequest
104112
* @returns an object containing the replay breadcrumbs and performance spans
105113
*/
106-
export function getCustomRecordingEvents(replayRequest: Request): CustomRecordingContent {
114+
export function getCustomRecordingEvents(resOrReq: Request | Response): CustomRecordingContent {
115+
const replayRequest = getRequest(resOrReq);
107116
const recordingEvents = getDecompressedRecordingEvents(replayRequest);
108117

109118
const breadcrumbs = getReplayBreadcrumbs(recordingEvents);
@@ -128,21 +137,25 @@ function getReplayPerformanceSpans(recordingEvents: RecordingEvent[]): Performan
128137
.map(data => data.payload) as PerformanceSpan[];
129138
}
130139

131-
export function getFullRecordingSnapshots(replayRequest: Request): RecordingSnapshot[] {
140+
export function getFullRecordingSnapshots(resOrReq: Request | Response): RecordingSnapshot[] {
141+
const replayRequest = getRequest(resOrReq);
132142
const events = getDecompressedRecordingEvents(replayRequest) as RecordingEvent[];
133143
return events.filter(event => event.type === 2).map(event => event.data as RecordingSnapshot);
134144
}
135145

136-
function getIncrementalRecordingSnapshots(replayRequest: Request): RecordingSnapshot[] {
146+
function getIncrementalRecordingSnapshots(resOrReq: Request | Response): RecordingSnapshot[] {
147+
const replayRequest = getRequest(resOrReq);
137148
const events = getDecompressedRecordingEvents(replayRequest) as RecordingEvent[];
138149
return events.filter(event => event.type === 3).map(event => event.data as RecordingSnapshot);
139150
}
140151

141-
function getDecompressedRecordingEvents(replayRequest: Request): RecordingEvent[] {
152+
function getDecompressedRecordingEvents(resOrReq: Request | Response): RecordingEvent[] {
153+
const replayRequest = getRequest(resOrReq);
142154
return replayEnvelopeRequestParser(replayRequest, 5) as RecordingEvent[];
143155
}
144156

145-
export function getReplayRecordingContent(replayRequest: Request): RecordingContent {
157+
export function getReplayRecordingContent(resOrReq: Request | Response): RecordingContent {
158+
const replayRequest = getRequest(resOrReq);
146159
const fullSnapshots = getFullRecordingSnapshots(replayRequest);
147160
const incrementalSnapshots = getIncrementalRecordingSnapshots(replayRequest);
148161
const customEvents = getCustomRecordingEvents(replayRequest);

0 commit comments

Comments
 (0)