8
8
* - Please do NOT serve this file on production.
9
9
*/
10
10
11
- const PACKAGE_VERSION = '2.7.0' ;
12
- const INTEGRITY_CHECKSUM = '00729d72e3b82faf54ca8b9621dbb96f' ;
13
- const IS_MOCKED_RESPONSE = Symbol ( 'isMockedResponse' ) ;
14
- const activeClientIds = new Set ( ) ;
11
+ const PACKAGE_VERSION = '2.7.0'
12
+ const INTEGRITY_CHECKSUM = '00729d72e3b82faf54ca8b9621dbb96f'
13
+ const IS_MOCKED_RESPONSE = Symbol ( 'isMockedResponse' )
14
+ const activeClientIds = new Set ( )
15
15
16
16
self . addEventListener ( 'install' , function ( ) {
17
- self . skipWaiting ( ) ;
18
- } ) ;
17
+ self . skipWaiting ( )
18
+ } )
19
19
20
20
self . addEventListener ( 'activate' , function ( event ) {
21
- event . waitUntil ( self . clients . claim ( ) ) ;
22
- } ) ;
21
+ event . waitUntil ( self . clients . claim ( ) )
22
+ } )
23
23
24
24
self . addEventListener ( 'message' , async function ( event ) {
25
- const clientId = event . source . id ;
25
+ const clientId = event . source . id
26
26
27
27
if ( ! clientId || ! self . clients ) {
28
- return ;
28
+ return
29
29
}
30
30
31
- const client = await self . clients . get ( clientId ) ;
31
+ const client = await self . clients . get ( clientId )
32
32
33
33
if ( ! client ) {
34
- return ;
34
+ return
35
35
}
36
36
37
37
const allClients = await self . clients . matchAll ( {
38
38
type : 'window' ,
39
- } ) ;
39
+ } )
40
40
41
41
switch ( event . data ) {
42
42
case 'KEEPALIVE_REQUEST' : {
43
43
sendToClient ( client , {
44
44
type : 'KEEPALIVE_RESPONSE' ,
45
- } ) ;
46
- break ;
45
+ } )
46
+ break
47
47
}
48
48
49
49
case 'INTEGRITY_CHECK_REQUEST' : {
@@ -53,12 +53,12 @@ self.addEventListener('message', async function (event) {
53
53
packageVersion : PACKAGE_VERSION ,
54
54
checksum : INTEGRITY_CHECKSUM ,
55
55
} ,
56
- } ) ;
57
- break ;
56
+ } )
57
+ break
58
58
}
59
59
60
60
case 'MOCK_ACTIVATE' : {
61
- activeClientIds . add ( clientId ) ;
61
+ activeClientIds . add ( clientId )
62
62
63
63
sendToClient ( client , {
64
64
type : 'MOCKING_ENABLED' ,
@@ -68,68 +68,68 @@ self.addEventListener('message', async function (event) {
68
68
frameType : client . frameType ,
69
69
} ,
70
70
} ,
71
- } ) ;
72
- break ;
71
+ } )
72
+ break
73
73
}
74
74
75
75
case 'MOCK_DEACTIVATE' : {
76
- activeClientIds . delete ( clientId ) ;
77
- break ;
76
+ activeClientIds . delete ( clientId )
77
+ break
78
78
}
79
79
80
80
case 'CLIENT_CLOSED' : {
81
- activeClientIds . delete ( clientId ) ;
81
+ activeClientIds . delete ( clientId )
82
82
83
- const remainingClients = allClients . filter ( client => {
84
- return client . id !== clientId ;
85
- } ) ;
83
+ const remainingClients = allClients . filter ( ( client ) => {
84
+ return client . id !== clientId
85
+ } )
86
86
87
87
// Unregister itself when there are no more clients
88
88
if ( remainingClients . length === 0 ) {
89
- self . registration . unregister ( ) ;
89
+ self . registration . unregister ( )
90
90
}
91
91
92
- break ;
92
+ break
93
93
}
94
94
}
95
- } ) ;
95
+ } )
96
96
97
97
self . addEventListener ( 'fetch' , function ( event ) {
98
- const { request } = event ;
98
+ const { request } = event
99
99
100
100
// Bypass navigation requests.
101
101
if ( request . mode === 'navigate' ) {
102
- return ;
102
+ return
103
103
}
104
104
105
105
// Opening the DevTools triggers the "only-if-cached" request
106
106
// that cannot be handled by the worker. Bypass such requests.
107
107
if ( request . cache === 'only-if-cached' && request . mode !== 'same-origin' ) {
108
- return ;
108
+ return
109
109
}
110
110
111
111
// Bypass all requests when there are no active clients.
112
112
// Prevents the self-unregistered worked from handling requests
113
113
// after it's been deleted (still remains active until the next reload).
114
114
if ( activeClientIds . size === 0 ) {
115
- return ;
115
+ return
116
116
}
117
117
118
118
// Generate unique request ID.
119
- const requestId = crypto . randomUUID ( ) ;
120
- event . respondWith ( handleRequest ( event , requestId ) ) ;
121
- } ) ;
119
+ const requestId = crypto . randomUUID ( )
120
+ event . respondWith ( handleRequest ( event , requestId ) )
121
+ } )
122
122
123
123
async function handleRequest ( event , requestId ) {
124
- const client = await resolveMainClient ( event ) ;
125
- const response = await getResponse ( event , client , requestId ) ;
124
+ const client = await resolveMainClient ( event )
125
+ const response = await getResponse ( event , client , requestId )
126
126
127
127
// Send back the response clone for the "response:*" life-cycle events.
128
128
// Ensure MSW is active and ready to handle the message, otherwise
129
129
// this message will pend indefinitely.
130
130
if ( client && activeClientIds . has ( client . id ) ) {
131
- ( async function ( ) {
132
- const responseClone = response . clone ( ) ;
131
+ ; ( async function ( ) {
132
+ const responseClone = response . clone ( )
133
133
134
134
sendToClient (
135
135
client ,
@@ -146,91 +146,91 @@ async function handleRequest(event, requestId) {
146
146
} ,
147
147
} ,
148
148
[ responseClone . body ] ,
149
- ) ;
150
- } ) ( ) ;
149
+ )
150
+ } ) ( )
151
151
}
152
152
153
- return response ;
153
+ return response
154
154
}
155
155
156
156
// Resolve the main client for the given event.
157
157
// Client that issues a request doesn't necessarily equal the client
158
158
// that registered the worker. It's with the latter the worker should
159
159
// communicate with during the response resolving phase.
160
160
async function resolveMainClient ( event ) {
161
- const client = await self . clients . get ( event . clientId ) ;
161
+ const client = await self . clients . get ( event . clientId )
162
162
163
163
if ( activeClientIds . has ( event . clientId ) ) {
164
- return client ;
164
+ return client
165
165
}
166
166
167
167
if ( client ?. frameType === 'top-level' ) {
168
- return client ;
168
+ return client
169
169
}
170
170
171
171
const allClients = await self . clients . matchAll ( {
172
172
type : 'window' ,
173
- } ) ;
173
+ } )
174
174
175
175
return allClients
176
- . filter ( client => {
176
+ . filter ( ( client ) => {
177
177
// Get only those clients that are currently visible.
178
- return client . visibilityState === 'visible' ;
178
+ return client . visibilityState === 'visible'
179
179
} )
180
- . find ( client => {
180
+ . find ( ( client ) => {
181
181
// Find the client ID that's recorded in the
182
182
// set of clients that have registered the worker.
183
- return activeClientIds . has ( client . id ) ;
184
- } ) ;
183
+ return activeClientIds . has ( client . id )
184
+ } )
185
185
}
186
186
187
187
async function getResponse ( event , client , requestId ) {
188
- const { request } = event ;
188
+ const { request } = event
189
189
190
190
// Clone the request because it might've been already used
191
191
// (i.e. its body has been read and sent to the client).
192
- const requestClone = request . clone ( ) ;
192
+ const requestClone = request . clone ( )
193
193
194
194
function passthrough ( ) {
195
195
// Cast the request headers to a new Headers instance
196
196
// so the headers can be manipulated with.
197
- const headers = new Headers ( requestClone . headers ) ;
197
+ const headers = new Headers ( requestClone . headers )
198
198
199
199
// Remove the "accept" header value that marked this request as passthrough.
200
200
// This prevents request alteration and also keeps it compliant with the
201
201
// user-defined CORS policies.
202
- const acceptHeader = headers . get ( 'accept' ) ;
202
+ const acceptHeader = headers . get ( 'accept' )
203
203
if ( acceptHeader ) {
204
- const values = acceptHeader . split ( ',' ) . map ( value => value . trim ( ) ) ;
204
+ const values = acceptHeader . split ( ',' ) . map ( ( value ) => value . trim ( ) )
205
205
const filteredValues = values . filter (
206
- value => value !== 'msw/passthrough' ,
207
- ) ;
206
+ ( value ) => value !== 'msw/passthrough' ,
207
+ )
208
208
209
209
if ( filteredValues . length > 0 ) {
210
- headers . set ( 'accept' , filteredValues . join ( ', ' ) ) ;
210
+ headers . set ( 'accept' , filteredValues . join ( ', ' ) )
211
211
} else {
212
- headers . delete ( 'accept' ) ;
212
+ headers . delete ( 'accept' )
213
213
}
214
214
}
215
215
216
- return fetch ( requestClone , { headers } ) ;
216
+ return fetch ( requestClone , { headers } )
217
217
}
218
218
219
219
// Bypass mocking when the client is not active.
220
220
if ( ! client ) {
221
- return passthrough ( ) ;
221
+ return passthrough ( )
222
222
}
223
223
224
224
// Bypass initial page load requests (i.e. static assets).
225
225
// The absence of the immediate/parent client in the map of the active clients
226
226
// means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
227
227
// and is not ready to handle requests.
228
228
if ( ! activeClientIds . has ( client . id ) ) {
229
- return passthrough ( ) ;
229
+ return passthrough ( )
230
230
}
231
231
232
232
// Notify the client that a request has been intercepted.
233
- const requestBuffer = await request . arrayBuffer ( ) ;
233
+ const requestBuffer = await request . arrayBuffer ( )
234
234
const clientMessage = await sendToClient (
235
235
client ,
236
236
{
@@ -253,38 +253,38 @@ async function getResponse(event, client, requestId) {
253
253
} ,
254
254
} ,
255
255
[ requestBuffer ] ,
256
- ) ;
256
+ )
257
257
258
258
switch ( clientMessage . type ) {
259
259
case 'MOCK_RESPONSE' : {
260
- return respondWithMock ( clientMessage . data ) ;
260
+ return respondWithMock ( clientMessage . data )
261
261
}
262
262
263
263
case 'PASSTHROUGH' : {
264
- return passthrough ( ) ;
264
+ return passthrough ( )
265
265
}
266
266
}
267
267
268
- return passthrough ( ) ;
268
+ return passthrough ( )
269
269
}
270
270
271
271
function sendToClient ( client , message , transferrables = [ ] ) {
272
272
return new Promise ( ( resolve , reject ) => {
273
- const channel = new MessageChannel ( ) ;
273
+ const channel = new MessageChannel ( )
274
274
275
- channel . port1 . onmessage = event => {
275
+ channel . port1 . onmessage = ( event ) => {
276
276
if ( event . data && event . data . error ) {
277
- return reject ( event . data . error ) ;
277
+ return reject ( event . data . error )
278
278
}
279
279
280
- resolve ( event . data ) ;
281
- } ;
280
+ resolve ( event . data )
281
+ }
282
282
283
283
client . postMessage (
284
284
message ,
285
285
[ channel . port2 ] . concat ( transferrables . filter ( Boolean ) ) ,
286
- ) ;
287
- } ) ;
286
+ )
287
+ } )
288
288
}
289
289
290
290
async function respondWithMock ( response ) {
@@ -293,15 +293,15 @@ async function respondWithMock(response) {
293
293
// instance will have status code set to 0. Since it's not possible to create
294
294
// a Response instance with status code 0, handle that use-case separately.
295
295
if ( response . status === 0 ) {
296
- return Response . error ( ) ;
296
+ return Response . error ( )
297
297
}
298
298
299
- const mockedResponse = new Response ( response . body , response ) ;
299
+ const mockedResponse = new Response ( response . body , response )
300
300
301
301
Reflect . defineProperty ( mockedResponse , IS_MOCKED_RESPONSE , {
302
302
value : true ,
303
303
enumerable : true ,
304
- } ) ;
304
+ } )
305
305
306
- return mockedResponse ;
306
+ return mockedResponse
307
307
}
0 commit comments