88 * - Please do NOT serve this file on production.
99 */
1010
11- const INTEGRITY_CHECKSUM = 'b3066ef78c2f9090b4ce87e874965995' ;
12- const activeClientIds = new Set ( ) ;
11+ const INTEGRITY_CHECKSUM = 'b3066ef78c2f9090b4ce87e874965995'
12+ const activeClientIds = new Set ( )
1313
1414self . addEventListener ( 'install' , function ( ) {
15- self . skipWaiting ( ) ;
16- } ) ;
15+ self . skipWaiting ( )
16+ } )
1717
1818self . addEventListener ( 'activate' , function ( event ) {
19- event . waitUntil ( self . clients . claim ( ) ) ;
20- } ) ;
19+ event . waitUntil ( self . clients . claim ( ) )
20+ } )
2121
2222self . addEventListener ( 'message' , async function ( event ) {
23- const clientId = event . source . id ;
23+ const clientId = event . source . id
2424
2525 if ( ! clientId || ! self . clients ) {
26- return ;
26+ return
2727 }
2828
29- const client = await self . clients . get ( clientId ) ;
29+ const client = await self . clients . get ( clientId )
3030
3131 if ( ! client ) {
32- return ;
32+ return
3333 }
3434
3535 const allClients = await self . clients . matchAll ( {
3636 type : 'window' ,
37- } ) ;
37+ } )
3838
3939 switch ( event . data ) {
4040 case 'KEEPALIVE_REQUEST' : {
4141 sendToClient ( client , {
4242 type : 'KEEPALIVE_RESPONSE' ,
43- } ) ;
44- break ;
43+ } )
44+ break
4545 }
4646
4747 case 'INTEGRITY_CHECK_REQUEST' : {
4848 sendToClient ( client , {
4949 type : 'INTEGRITY_CHECK_RESPONSE' ,
5050 payload : INTEGRITY_CHECKSUM ,
51- } ) ;
52- break ;
51+ } )
52+ break
5353 }
5454
5555 case 'MOCK_ACTIVATE' : {
56- activeClientIds . add ( clientId ) ;
56+ activeClientIds . add ( clientId )
5757
5858 sendToClient ( client , {
5959 type : 'MOCKING_ENABLED' ,
6060 payload : true ,
61- } ) ;
62- break ;
61+ } )
62+ break
6363 }
6464
6565 case 'MOCK_DEACTIVATE' : {
66- activeClientIds . delete ( clientId ) ;
67- break ;
66+ activeClientIds . delete ( clientId )
67+ break
6868 }
6969
7070 case 'CLIENT_CLOSED' : {
71- activeClientIds . delete ( clientId ) ;
71+ activeClientIds . delete ( clientId )
7272
73- const remainingClients = allClients . filter ( client => {
74- return client . id !== clientId ;
75- } ) ;
73+ const remainingClients = allClients . filter ( ( client ) => {
74+ return client . id !== clientId
75+ } )
7676
7777 // Unregister itself when there are no more clients
7878 if ( remainingClients . length === 0 ) {
79- self . registration . unregister ( ) ;
79+ self . registration . unregister ( )
8080 }
8181
82- break ;
82+ break
8383 }
8484 }
85- } ) ;
85+ } )
8686
8787self . addEventListener ( 'fetch' , function ( event ) {
88- const { request } = event ;
89- const accept = request . headers . get ( 'accept' ) || '' ;
88+ const { request } = event
89+ const accept = request . headers . get ( 'accept' ) || ''
9090
9191 // Bypass server-sent events.
9292 if ( accept . includes ( 'text/event-stream' ) ) {
93- return ;
93+ return
9494 }
9595
9696 // Bypass navigation requests.
9797 if ( request . mode === 'navigate' ) {
98- return ;
98+ return
9999 }
100100
101101 // Opening the DevTools triggers the "only-if-cached" request
102102 // that cannot be handled by the worker. Bypass such requests.
103103 if ( request . cache === 'only-if-cached' && request . mode !== 'same-origin' ) {
104- return ;
104+ return
105105 }
106106
107107 // Bypass all requests when there are no active clients.
108108 // Prevents the self-unregistered worked from handling requests
109109 // after it's been deleted (still remains active until the next reload).
110110 if ( activeClientIds . size === 0 ) {
111- return ;
111+ return
112112 }
113113
114114 // Generate unique request ID.
115- const requestId = Math . random ( ) . toString ( 16 ) . slice ( 2 ) ;
115+ const requestId = Math . random ( ) . toString ( 16 ) . slice ( 2 )
116116
117117 event . respondWith (
118- handleRequest ( event , requestId ) . catch ( error => {
118+ handleRequest ( event , requestId ) . catch ( ( error ) => {
119119 if ( error . name === 'NetworkError' ) {
120120 console . warn (
121121 '[MSW] Successfully emulated a network error for the "%s %s" request.' ,
122122 request . method ,
123123 request . url ,
124- ) ;
125- return ;
124+ )
125+ return
126126 }
127127
128128 // At this point, any exception indicates an issue with the original request/response.
@@ -132,21 +132,21 @@ self.addEventListener('fetch', function (event) {
132132 request . method ,
133133 request . url ,
134134 `${ error . name } : ${ error . message } ` ,
135- ) ;
135+ )
136136 } ) ,
137- ) ;
138- } ) ;
137+ )
138+ } )
139139
140140async function handleRequest ( event , requestId ) {
141- const client = await resolveMainClient ( event ) ;
142- const response = await getResponse ( event , client , requestId ) ;
141+ const client = await resolveMainClient ( event )
142+ const response = await getResponse ( event , client , requestId )
143143
144144 // Send back the response clone for the "response:*" life-cycle events.
145145 // Ensure MSW is active and ready to handle the message, otherwise
146146 // this message will pend indefinitely.
147147 if ( client && activeClientIds . has ( client . id ) ) {
148- ( async function ( ) {
149- const clonedResponse = response . clone ( ) ;
148+ ; ( async function ( ) {
149+ const clonedResponse = response . clone ( )
150150 sendToClient ( client , {
151151 type : 'RESPONSE' ,
152152 payload : {
@@ -155,79 +155,80 @@ async function handleRequest(event, requestId) {
155155 ok : clonedResponse . ok ,
156156 status : clonedResponse . status ,
157157 statusText : clonedResponse . statusText ,
158- body : clonedResponse . body === null ? null : await clonedResponse . text ( ) ,
158+ body :
159+ clonedResponse . body === null ? null : await clonedResponse . text ( ) ,
159160 headers : Object . fromEntries ( clonedResponse . headers . entries ( ) ) ,
160161 redirected : clonedResponse . redirected ,
161162 } ,
162- } ) ;
163- } ) ( ) ;
163+ } )
164+ } ) ( )
164165 }
165166
166- return response ;
167+ return response
167168}
168169
169170// Resolve the main client for the given event.
170171// Client that issues a request doesn't necessarily equal the client
171172// that registered the worker. It's with the latter the worker should
172173// communicate with during the response resolving phase.
173174async function resolveMainClient ( event ) {
174- const client = await self . clients . get ( event . clientId ) ;
175+ const client = await self . clients . get ( event . clientId )
175176
176177 if ( client . frameType === 'top-level' ) {
177- return client ;
178+ return client
178179 }
179180
180181 const allClients = await self . clients . matchAll ( {
181182 type : 'window' ,
182- } ) ;
183+ } )
183184
184185 return allClients
185- . filter ( client => {
186+ . filter ( ( client ) => {
186187 // Get only those clients that are currently visible.
187- return client . visibilityState === 'visible' ;
188+ return client . visibilityState === 'visible'
188189 } )
189- . find ( client => {
190+ . find ( ( client ) => {
190191 // Find the client ID that's recorded in the
191192 // set of clients that have registered the worker.
192- return activeClientIds . has ( client . id ) ;
193- } ) ;
193+ return activeClientIds . has ( client . id )
194+ } )
194195}
195196
196197async function getResponse ( event , client , requestId ) {
197- const { request } = event ;
198- const clonedRequest = request . clone ( ) ;
198+ const { request } = event
199+ const clonedRequest = request . clone ( )
199200
200201 function passthrough ( ) {
201202 // Clone the request because it might've been already used
202203 // (i.e. its body has been read and sent to the client).
203- const headers = Object . fromEntries ( clonedRequest . headers . entries ( ) ) ;
204+ const headers = Object . fromEntries ( clonedRequest . headers . entries ( ) )
204205
205206 // Remove MSW-specific request headers so the bypassed requests
206207 // comply with the server's CORS preflight check.
207208 // Operate with the headers as an object because request "Headers"
208209 // are immutable.
209- delete headers [ 'x-msw-bypass' ] ;
210+ delete headers [ 'x-msw-bypass' ]
210211
211- return fetch ( clonedRequest , { headers } ) ;
212+ return fetch ( clonedRequest , { headers } )
212213 }
213214
214215 // Bypass mocking when the client is not active.
215216 if ( ! client ) {
216- return passthrough ( ) ;
217+ return passthrough ( )
217218 }
218219
219220 // Bypass initial page load requests (i.e. static assets).
220221 // The absence of the immediate/parent client in the map of the active clients
221222 // means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
222223 // and is not ready to handle requests.
223224 if ( ! activeClientIds . has ( client . id ) ) {
224- return passthrough ( ) ;
225+ return passthrough ( )
225226 }
226227
227228 // Bypass requests with the explicit bypass header.
228229 // Such requests can be issued by "ctx.fetch()".
229230 if ( request . headers . get ( 'x-msw-bypass' ) === 'true' ) {
230- return passthrough ( ) ;
231+ return passthrough ( )
231232 }
232233
233234 // Notify the client that a request has been intercepted.
@@ -250,53 +251,53 @@ async function getResponse(event, client, requestId) {
250251 bodyUsed : request . bodyUsed ,
251252 keepalive : request . keepalive ,
252253 } ,
253- } ) ;
254+ } )
254255
255256 switch ( clientMessage . type ) {
256257 case 'MOCK_RESPONSE' : {
257- return respondWithMock ( clientMessage . data ) ;
258+ return respondWithMock ( clientMessage . data )
258259 }
259260
260261 case 'MOCK_NOT_FOUND' : {
261- return passthrough ( ) ;
262+ return passthrough ( )
262263 }
263264
264265 case 'NETWORK_ERROR' : {
265- const { name, message } = clientMessage . data ;
266- const networkError = new Error ( message ) ;
267- networkError . name = name ;
266+ const { name, message } = clientMessage . data
267+ const networkError = new Error ( message )
268+ networkError . name = name
268269
269270 // Rejecting a "respondWith" promise emulates a network error.
270- throw networkError ;
271+ throw networkError
271272 }
272273 }
273274
274- return passthrough ( ) ;
275+ return passthrough ( )
275276}
276277
277278function sendToClient ( client , message ) {
278279 return new Promise ( ( resolve , reject ) => {
279- const channel = new MessageChannel ( ) ;
280+ const channel = new MessageChannel ( )
280281
281- channel . port1 . onmessage = event => {
282+ channel . port1 . onmessage = ( event ) => {
282283 if ( event . data && event . data . error ) {
283- return reject ( event . data . error ) ;
284+ return reject ( event . data . error )
284285 }
285286
286- resolve ( event . data ) ;
287- } ;
287+ resolve ( event . data )
288+ }
288289
289- client . postMessage ( message , [ channel . port2 ] ) ;
290- } ) ;
290+ client . postMessage ( message , [ channel . port2 ] )
291+ } )
291292}
292293
293294function sleep ( timeMs ) {
294- return new Promise ( resolve => {
295- setTimeout ( resolve , timeMs ) ;
296- } ) ;
295+ return new Promise ( ( resolve ) => {
296+ setTimeout ( resolve , timeMs )
297+ } )
297298}
298299
299300async function respondWithMock ( response ) {
300- await sleep ( response . delay ) ;
301- return new Response ( response . body , response ) ;
301+ await sleep ( response . delay )
302+ return new Response ( response . body , response )
302303}
0 commit comments