-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathplaywright-source.ts
More file actions
55 lines (47 loc) · 1.7 KB
/
Copy pathplaywright-source.ts
File metadata and controls
55 lines (47 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { BrowserContext, Route } from '@playwright/test'
import { isCommonAssetRequest } from 'msw'
import { NetworkSource } from 'msw/experimental'
import { PlaywrightHttpNetworkFrame } from './frames/http-frame.js'
import {
convertToRequest,
handleRouteSafely,
INTERNAL_MATCH_ALL_REG_EXP,
} from './utils.js'
interface PlaywrightSourceOptions {
context: BrowserContext
skipAssetRequests?: boolean
}
export class PlaywrightSource extends NetworkSource<PlaywrightHttpNetworkFrame> {
#context: BrowserContext
#skipAssetRequests: boolean
constructor(options: PlaywrightSourceOptions) {
super()
this.#context = options.context
this.#skipAssetRequests = options.skipAssetRequests ?? true
}
override async enable(): Promise<void> {
await this.#context.route(
INTERNAL_MATCH_ALL_REG_EXP,
this.#handleRouteRequest.bind(this),
)
}
override async disable(): Promise<void> {
super.disable()
await this.#context.unroute(INTERNAL_MATCH_ALL_REG_EXP)
}
async #handleRouteRequest(route: Route): Promise<void> {
const request = await convertToRequest(route)
/**
* @note Skip common asset requests (default).
* Playwright seems to experience performance degradation when routing all
* requests through the matching logic below.
* @see https://github.com/mswjs/playwright/issues/13
*/
if (this.#skipAssetRequests && isCommonAssetRequest(request)) {
return await handleRouteSafely(() => route.fallback())
}
// TODO: Do we need a id for the frame? Do we need to store the frame in a map like Interceptor- and ServiceWorkerSource?
const frame = new PlaywrightHttpNetworkFrame({ route, request })
await this.queue(frame)
}
}