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,8 @@ 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
+ export function getReplayEvent ( resOrReq : Request | Response ) : ReplayEvent {
84
+ const replayRequest = getRequest ( resOrReq ) ;
82
85
const event = envelopeRequestParser ( replayRequest ) ;
83
86
if ( ! isReplayEvent ( event ) ) {
84
87
throw new Error ( 'Request is not a replay event' ) ;
@@ -103,7 +106,8 @@ type RecordingContent = {
103
106
* @param replayRequest
104
107
* @returns an object containing the replay breadcrumbs and performance spans
105
108
*/
106
- export function getCustomRecordingEvents ( replayRequest : Request ) : CustomRecordingContent {
109
+ export function getCustomRecordingEvents ( resOrReq : Request | Response ) : CustomRecordingContent {
110
+ const replayRequest = getRequest ( resOrReq ) ;
107
111
const recordingEvents = getDecompressedRecordingEvents ( replayRequest ) ;
108
112
109
113
const breadcrumbs = getReplayBreadcrumbs ( recordingEvents ) ;
@@ -128,21 +132,25 @@ function getReplayPerformanceSpans(recordingEvents: RecordingEvent[]): Performan
128
132
. map ( data => data . payload ) as PerformanceSpan [ ] ;
129
133
}
130
134
131
- export function getFullRecordingSnapshots ( replayRequest : Request ) : RecordingSnapshot [ ] {
135
+ export function getFullRecordingSnapshots ( resOrReq : Request | Response ) : RecordingSnapshot [ ] {
136
+ const replayRequest = getRequest ( resOrReq ) ;
132
137
const events = getDecompressedRecordingEvents ( replayRequest ) as RecordingEvent [ ] ;
133
138
return events . filter ( event => event . type === 2 ) . map ( event => event . data as RecordingSnapshot ) ;
134
139
}
135
140
136
- function getIncrementalRecordingSnapshots ( replayRequest : Request ) : RecordingSnapshot [ ] {
141
+ function getIncrementalRecordingSnapshots ( resOrReq : Request | Response ) : RecordingSnapshot [ ] {
142
+ const replayRequest = getRequest ( resOrReq ) ;
137
143
const events = getDecompressedRecordingEvents ( replayRequest ) as RecordingEvent [ ] ;
138
144
return events . filter ( event => event . type === 3 ) . map ( event => event . data as RecordingSnapshot ) ;
139
145
}
140
146
141
- function getDecompressedRecordingEvents ( replayRequest : Request ) : RecordingEvent [ ] {
147
+ function getDecompressedRecordingEvents ( resOrReq : Request | Response ) : RecordingEvent [ ] {
148
+ const replayRequest = getRequest ( resOrReq ) ;
142
149
return replayEnvelopeRequestParser ( replayRequest , 5 ) as RecordingEvent [ ] ;
143
150
}
144
151
145
- export function getReplayRecordingContent ( replayRequest : Request ) : RecordingContent {
152
+ export function getReplayRecordingContent ( resOrReq : Request | Response ) : RecordingContent {
153
+ const replayRequest = getRequest ( resOrReq ) ;
146
154
const fullSnapshots = getFullRecordingSnapshots ( replayRequest ) ;
147
155
const incrementalSnapshots = getIncrementalRecordingSnapshots ( replayRequest ) ;
148
156
const customEvents = getCustomRecordingEvents ( replayRequest ) ;
@@ -255,3 +263,9 @@ function normalizeNumberAttribute(num: number): string {
255
263
256
264
return `[${ stepCount * step } -${ ( stepCount + 1 ) * step } ]` ;
257
265
}
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