11import type { RecordingEvent , ReplayContainer } from '@sentry/replay/build/npm/types/types' ;
22import type { Breadcrumb , Event , ReplayEvent } from '@sentry/types' ;
33import pako from 'pako' ;
4- import type { Page , Request } from 'playwright' ;
4+ import type { Page , Request , Response } from 'playwright' ;
55
66import { 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,8 @@ export async function getReplaySnapshot(page: Page): Promise<ReplayContainer> {
7880
7981export const REPLAY_DEFAULT_FLUSH_MAX_DELAY = 5_000 ;
8082
81- export function getReplayEvent ( replayRequest : Request ) : ReplayEvent {
83+ export function getReplayEvent ( resOrReq : Request | Response ) : ReplayEvent {
84+ const replayRequest = getRequest ( resOrReq ) ;
8285 const event = envelopeRequestParser ( replayRequest ) ;
8386 if ( ! isReplayEvent ( event ) ) {
8487 throw new Error ( 'Request is not a replay event' ) ;
@@ -103,7 +106,8 @@ type RecordingContent = {
103106 * @param replayRequest
104107 * @returns an object containing the replay breadcrumbs and performance spans
105108 */
106- export function getCustomRecordingEvents ( replayRequest : Request ) : CustomRecordingContent {
109+ export function getCustomRecordingEvents ( resOrReq : Request | Response ) : CustomRecordingContent {
110+ const replayRequest = getRequest ( resOrReq ) ;
107111 const recordingEvents = getDecompressedRecordingEvents ( replayRequest ) ;
108112
109113 const breadcrumbs = getReplayBreadcrumbs ( recordingEvents ) ;
@@ -128,21 +132,25 @@ function getReplayPerformanceSpans(recordingEvents: RecordingEvent[]): Performan
128132 . map ( data => data . payload ) as PerformanceSpan [ ] ;
129133}
130134
131- export function getFullRecordingSnapshots ( replayRequest : Request ) : RecordingSnapshot [ ] {
135+ export function getFullRecordingSnapshots ( resOrReq : Request | Response ) : RecordingSnapshot [ ] {
136+ const replayRequest = getRequest ( resOrReq ) ;
132137 const events = getDecompressedRecordingEvents ( replayRequest ) as RecordingEvent [ ] ;
133138 return events . filter ( event => event . type === 2 ) . map ( event => event . data as RecordingSnapshot ) ;
134139}
135140
136- function getIncrementalRecordingSnapshots ( replayRequest : Request ) : RecordingSnapshot [ ] {
141+ function getIncrementalRecordingSnapshots ( resOrReq : Request | Response ) : RecordingSnapshot [ ] {
142+ const replayRequest = getRequest ( resOrReq ) ;
137143 const events = getDecompressedRecordingEvents ( replayRequest ) as RecordingEvent [ ] ;
138144 return events . filter ( event => event . type === 3 ) . map ( event => event . data as RecordingSnapshot ) ;
139145}
140146
141- function getDecompressedRecordingEvents ( replayRequest : Request ) : RecordingEvent [ ] {
147+ function getDecompressedRecordingEvents ( resOrReq : Request | Response ) : RecordingEvent [ ] {
148+ const replayRequest = getRequest ( resOrReq ) ;
142149 return replayEnvelopeRequestParser ( replayRequest , 5 ) as RecordingEvent [ ] ;
143150}
144151
145- export function getReplayRecordingContent ( replayRequest : Request ) : RecordingContent {
152+ export function getReplayRecordingContent ( resOrReq : Request | Response ) : RecordingContent {
153+ const replayRequest = getRequest ( resOrReq ) ;
146154 const fullSnapshots = getFullRecordingSnapshots ( replayRequest ) ;
147155 const incrementalSnapshots = getIncrementalRecordingSnapshots ( replayRequest ) ;
148156 const customEvents = getCustomRecordingEvents ( replayRequest ) ;
@@ -255,3 +263,9 @@ function normalizeNumberAttribute(num: number): string {
255263
256264 return `[${ stepCount * step } -${ ( stepCount + 1 ) * step } ]` ;
257265}
266+
267+ /** Get a request from either a request or a response */
268+ function getRequest ( resOrReq : Request | Response ) : Request {
269+ // @ts -ignore we check this
270+ return typeof resOrReq . request === 'function' ? ( resOrReq as Response ) . request ( ) : ( resOrReq as Request ) ;
271+ }
0 commit comments