1
1
import type { RecordingEvent , ReplayContainer } from '@sentry/replay/build/npm/types/types' ;
2
2
import type { Breadcrumb , Event , ReplayEvent } from '@sentry/types' ;
3
3
import pako from 'pako' ;
4
- import type { Page , Request } from 'playwright' ;
4
+ import type { Page , Request , Response } from 'playwright' ;
5
5
6
6
import { envelopeRequestParser } from './helpers' ;
7
7
@@ -37,8 +37,10 @@ type SnapshotNode = {
37
37
* @param segmentId the segment_id of the replay event
38
38
* @returns
39
39
*/
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
+
42
44
const postData = req . postData ( ) ;
43
45
if ( ! postData ) {
44
46
return false ;
@@ -78,7 +80,13 @@ export async function getReplaySnapshot(page: Page): Promise<ReplayContainer> {
78
80
79
81
export const REPLAY_DEFAULT_FLUSH_MAX_DELAY = 5_000 ;
80
82
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 ) ;
82
90
const event = envelopeRequestParser ( replayRequest ) ;
83
91
if ( ! isReplayEvent ( event ) ) {
84
92
throw new Error ( 'Request is not a replay event' ) ;
@@ -103,7 +111,8 @@ type RecordingContent = {
103
111
* @param replayRequest
104
112
* @returns an object containing the replay breadcrumbs and performance spans
105
113
*/
106
- export function getCustomRecordingEvents ( replayRequest : Request ) : CustomRecordingContent {
114
+ export function getCustomRecordingEvents ( resOrReq : Request | Response ) : CustomRecordingContent {
115
+ const replayRequest = getRequest ( resOrReq ) ;
107
116
const recordingEvents = getDecompressedRecordingEvents ( replayRequest ) ;
108
117
109
118
const breadcrumbs = getReplayBreadcrumbs ( recordingEvents ) ;
@@ -128,21 +137,25 @@ function getReplayPerformanceSpans(recordingEvents: RecordingEvent[]): Performan
128
137
. map ( data => data . payload ) as PerformanceSpan [ ] ;
129
138
}
130
139
131
- export function getFullRecordingSnapshots ( replayRequest : Request ) : RecordingSnapshot [ ] {
140
+ export function getFullRecordingSnapshots ( resOrReq : Request | Response ) : RecordingSnapshot [ ] {
141
+ const replayRequest = getRequest ( resOrReq ) ;
132
142
const events = getDecompressedRecordingEvents ( replayRequest ) as RecordingEvent [ ] ;
133
143
return events . filter ( event => event . type === 2 ) . map ( event => event . data as RecordingSnapshot ) ;
134
144
}
135
145
136
- function getIncrementalRecordingSnapshots ( replayRequest : Request ) : RecordingSnapshot [ ] {
146
+ function getIncrementalRecordingSnapshots ( resOrReq : Request | Response ) : RecordingSnapshot [ ] {
147
+ const replayRequest = getRequest ( resOrReq ) ;
137
148
const events = getDecompressedRecordingEvents ( replayRequest ) as RecordingEvent [ ] ;
138
149
return events . filter ( event => event . type === 3 ) . map ( event => event . data as RecordingSnapshot ) ;
139
150
}
140
151
141
- function getDecompressedRecordingEvents ( replayRequest : Request ) : RecordingEvent [ ] {
152
+ function getDecompressedRecordingEvents ( resOrReq : Request | Response ) : RecordingEvent [ ] {
153
+ const replayRequest = getRequest ( resOrReq ) ;
142
154
return replayEnvelopeRequestParser ( replayRequest , 5 ) as RecordingEvent [ ] ;
143
155
}
144
156
145
- export function getReplayRecordingContent ( replayRequest : Request ) : RecordingContent {
157
+ export function getReplayRecordingContent ( resOrReq : Request | Response ) : RecordingContent {
158
+ const replayRequest = getRequest ( resOrReq ) ;
146
159
const fullSnapshots = getFullRecordingSnapshots ( replayRequest ) ;
147
160
const incrementalSnapshots = getIncrementalRecordingSnapshots ( replayRequest ) ;
148
161
const customEvents = getCustomRecordingEvents ( replayRequest ) ;
0 commit comments