1/* eslint-disable */ 2/* tslint:disable */ 3 4/** 5 * Mock Service Worker. 6 * @see https://github.com/mswjs/msw 7 * - Please do NOT modify this file. 8 * - Please do NOT serve this file on production. 9 */ 10 11const PACKAGE_VERSION = '2.2.10'; 12const INTEGRITY_CHECKSUM = '26357c79639bfa20d64c0efca2a87423'; 13const IS_MOCKED_RESPONSE = Symbol('isMockedResponse'); 14const activeClientIds = new Set(); 15 16self.addEventListener('install', function () { 17 self.skipWaiting(); 18}); 19 20self.addEventListener('activate', function (event) { 21 event.waitUntil(self.clients.claim()); 22}); 23 24self.addEventListener('message', async function (event) { 25 const clientId = event.source.id; 26 27 if (!clientId || !self.clients) { 28 return; 29 } 30 31 const client = await self.clients.get(clientId); 32 33 if (!client) { 34 return; 35 } 36 37 const allClients = await self.clients.matchAll({ 38 type: 'window', 39 }); 40 41 switch (event.data) { 42 case 'KEEPALIVE_REQUEST': { 43 sendToClient(client, { 44 type: 'KEEPALIVE_RESPONSE', 45 }); 46 break; 47 } 48 49 case 'INTEGRITY_CHECK_REQUEST': { 50 sendToClient(client, { 51 type: 'INTEGRITY_CHECK_RESPONSE', 52 payload: { 53 packageVersion: PACKAGE_VERSION, 54 checksum: INTEGRITY_CHECKSUM, 55 }, 56 }); 57 break; 58 } 59 60 case 'MOCK_ACTIVATE': { 61 activeClientIds.add(clientId); 62 63 sendToClient(client, { 64 type: 'MOCKING_ENABLED', 65 payload: true, 66 }); 67 break; 68 } 69 70 case 'MOCK_DEACTIVATE': { 71 activeClientIds.delete(clientId); 72 break; 73 } 74 75 case 'CLIENT_CLOSED': { 76 activeClientIds.delete(clientId); 77 78 const remainingClients = allClients.filter((client) => { 79 return client.id !== clientId; 80 }); 81 82 // Unregister itself when there are no more clients 83 if (remainingClients.length === 0) { 84 self.registration.unregister(); 85 } 86 87 break; 88 } 89 } 90}); 91 92self.addEventListener('fetch', function (event) { 93 const { request } = event; 94 95 // Bypass navigation requests. 96 if (request.mode === 'navigate') { 97 return; 98 } 99 100 // Opening the DevTools triggers the "only-if-cached" request 101 // that cannot be handled by the worker. Bypass such requests. 102 if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') { 103 return; 104 } 105 106 // Bypass all requests when there are no active clients. 107 // Prevents the self-unregistered worked from handling requests 108 // after it's been deleted (still remains active until the next reload). 109 if (activeClientIds.size === 0) { 110 return; 111 } 112 113 // Generate unique request ID. 114 const requestId = crypto.randomUUID(); 115 event.respondWith(handleRequest(event, requestId)); 116}); 117 118async function handleRequest(event, requestId) { 119 const client = await resolveMainClient(event); 120 const response = await getResponse(event, client, requestId); 121 122 // Send back the response clone for the "response:*" life-cycle events. 123 // Ensure MSW is active and ready to handle the message, otherwise 124 // this message will pend indefinitely. 125 if (client && activeClientIds.has(client.id)) { 126 (async function () { 127 const responseClone = response.clone(); 128 129 sendToClient( 130 client, 131 { 132 type: 'RESPONSE', 133 payload: { 134 requestId, 135 isMockedResponse: IS_MOCKED_RESPONSE in response, 136 type: responseClone.type, 137 status: responseClone.status, 138 statusText: responseClone.statusText, 139 body: responseClone.body, 140 headers: Object.fromEntries(responseClone.headers.entries()), 141 }, 142 }, 143 [responseClone.body] 144 ); 145 })(); 146 } 147 148 return response; 149} 150 151// Resolve the main client for the given event. 152// Client that issues a request doesn't necessarily equal the client 153// that registered the worker. It's with the latter the worker should 154// communicate with during the response resolving phase. 155async function resolveMainClient(event) { 156 const client = await self.clients.get(event.clientId); 157 158 if (client?.frameType === 'top-level') { 159 return client; 160 } 161 162 const allClients = await self.clients.matchAll({ 163 type: 'window', 164 }); 165 166 return allClients 167 .filter((client) => { 168 // Get only those clients that are currently visible. 169 return client.visibilityState === 'visible'; 170 }) 171 .find((client) => { 172 // Find the client ID that's recorded in the 173 // set of clients that have registered the worker. 174 return activeClientIds.has(client.id); 175 }); 176} 177 178async function getResponse(event, client, requestId) { 179 const { request } = event; 180 181 // Clone the request because it might've been already used 182 // (i.e. its body has been read and sent to the client). 183 const requestClone = request.clone(); 184 185 function passthrough() { 186 const headers = Object.fromEntries(requestClone.headers.entries()); 187 188 // Remove internal MSW request header so the passthrough request 189 // complies with any potential CORS preflight checks on the server. 190 // Some servers forbid unknown request headers. 191 delete headers['x-msw-intention']; 192 193 return fetch(requestClone, { headers }); 194 } 195 196 // Bypass mocking when the client is not active. 197 if (!client) { 198 return passthrough(); 199 } 200 201 // Bypass initial page load requests (i.e. static assets). 202 // The absence of the immediate/parent client in the map of the active clients 203 // means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet 204 // and is not ready to handle requests. 205 if (!activeClientIds.has(client.id)) { 206 return passthrough(); 207 } 208 209 // Notify the client that a request has been intercepted. 210 const requestBuffer = await request.arrayBuffer(); 211 const clientMessage = await sendToClient( 212 client, 213 { 214 type: 'REQUEST', 215 payload: { 216 id: requestId, 217 url: request.url, 218 mode: request.mode, 219 method: request.method, 220 headers: Object.fromEntries(request.headers.entries()), 221 cache: request.cache, 222 credentials: request.credentials, 223 destination: request.destination, 224 integrity: request.integrity, 225 redirect: request.redirect, 226 referrer: request.referrer, 227 referrerPolicy: request.referrerPolicy, 228 body: requestBuffer, 229 keepalive: request.keepalive, 230 }, 231 }, 232 [requestBuffer] 233 ); 234 235 switch (clientMessage.type) { 236 case 'MOCK_RESPONSE': { 237 return respondWithMock(clientMessage.data); 238 } 239 240 case 'PASSTHROUGH': { 241 return passthrough(); 242 } 243 } 244 245 return passthrough(); 246} 247 248function sendToClient(client, message, transferrables = []) { 249 return new Promise((resolve, reject) => { 250 const channel = new MessageChannel(); 251 252 channel.port1.onmessage = (event) => { 253 if (event.data && event.data.error) { 254 return reject(event.data.error); 255 } 256 257 resolve(event.data); 258 }; 259 260 client.postMessage(message, [channel.port2].concat(transferrables.filter(
260Boolean))); 261 }); 262} 263 264async function respondWithMock(response) { 265 // Setting response status code to 0 is a no-op. 266 // However, when responding with a "Response.error()", the produced Response 267 // instance will have status code set to 0. Since it's not possible to create 268 // a Response instance with status code 0, handle that use-case separately. 269 if (response.status === 0) { 270 return Response.error(); 271 } 272 273 const mockedResponse = new Response(response.body, response); 274 275 Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, { 276 value: true, 277 enumerable: true, 278 }); 279 280 return mockedResponse; 281}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.