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.6.6'; 12const INTEGRITY_CHECKSUM = 'ca7800994cc8bfb5eb961e037c877074'; 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: { 66 client: { 67 id: client.id, 68 frameType: client.frameType, 69 }, 70 }, 71 }); 72 break; 73 } 74 75 case 'MOCK_DEACTIVATE': { 76 activeClientIds.delete(clientId); 77 break; 78 } 79 80 case 'CLIENT_CLOSED': { 81 activeClientIds.delete(clientId); 82 83 const remainingClients = allClients.filter((client) => { 84 return client.id !== clientId; 85 }); 86 87 // Unregister itself when there are no more clients 88 if (remainingClients.length === 0) { 89 self.registration.unregister(); 90 } 91 92 break; 93 } 94 } 95}); 96 97self.addEventListener('fetch', function (event) { 98 const { request } = event; 99 100 // Only return mock response when request.url starts with http://localhost:3000/api 101 if (!request.url.includes('http://localhost:3000/api')) { 102 return; 103 } 104 105 // Bypass navigation requests. 106 if (request.mode === 'navigate') { 107 return; 108 } 109 110 // Opening the DevTools triggers the "only-if-cached" request 111 // that cannot be handled by the worker. Bypass such requests. 112 if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') { 113 return; 114 } 115 116 // Bypass all requests when there are no active clients. 117 // Prevents the self-unregistered worked from handling requests 118 // after it's been deleted (still remains active until the next reload). 119 if (activeClientIds.size === 0) { 120 return; 121 } 122 123 // Generate unique request ID. 124 const requestId = crypto.randomUUID(); 125 event.respondWith(handleRequest(event, requestId)); 126}); 127 128async function handleRequest(event, requestId) { 129 const client = await resolveMainClient(event); 130 const response = await getResponse(event, client, requestId); 131 132 // Send back the response clone for the "response:*" life-cycle events. 133 // Ensure MSW is active and ready to handle the message, otherwise 134 // this message will pend indefinitely. 135 if (client && activeClientIds.has(client.id)) { 136 (async function () { 137 const responseClone = response.clone(); 138 139 sendToClient( 140 client, 141 { 142 type: 'RESPONSE', 143 payload: { 144 requestId, 145 isMockedResponse: IS_MOCKED_RESPONSE in response, 146 type: responseClone.type, 147 status: responseClone.status, 148 statusText: responseClone.statusText, 149 body: responseClone.body, 150 headers: Object.fromEntries(responseClone.headers.entries()), 151 }, 152 }, 153 [responseClone.body], 154 ); 155 })(); 156 } 157 158 return response; 159} 160 161// Resolve the main client for the given event. 162// Client that issues a request doesn't necessarily equal the client 163// that registered the worker. It's with the latter the worker should 164// communicate with during the response resolving phase. 165async function resolveMainClient(event) { 166 const client = await self.clients.get(event.clientId); 167 168 if (activeClientIds.has(event.clientId)) { 169 return client; 170 } 171 172 if (client?.frameType === 'top-level') { 173 return client; 174 } 175 176 const allClients = await self.clients.matchAll({ 177 type: 'window', 178 }); 179 180 return allClients 181 .filter((client) => { 182 // Get only those clients that are currently visible. 183 return client.visibilityState === 'visible'; 184 }) 185 .find((client) => { 186 // Find the client ID that's recorded in the 187 // set of clients that have registered the worker. 188 return activeClientIds.has(client.id); 189 }); 190} 191 192async function getResponse(event, client, requestId) { 193 const { request } = event; 194 195 // Clone the request because it might've been already used 196 // (i.e. its body has been read and sent to the client). 197 const requestClone = request.clone(); 198 199 function passthrough() { 200 // Cast the request headers to a new Headers instance 201 // so the headers can be manipulated with. 202 const headers = new Headers(requestClone.headers); 203 204 // Remove the "accept" header value that marked this request as passthrough. 205 // This prevents request alteration and also keeps it compliant with the 206 // user-defined CORS policies. 207 headers.delete('accept', 'msw/passthrough'); 208 209 return fetch(requestClone, { headers }); 210 } 211 212 // Bypass mocking when the client is not active. 213 if (!client) { 214 return passthrough(); 215 } 216 217 // Bypass initial page load requests (i.e. static assets). 218 // The absence of the immediate/parent client in the map of the active clients 219 // means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet 220 // and is not ready to handle requests. 221 if (!activeClientIds.has(client.id)) { 222 return passthrough(); 223 } 224 225 // Notify the client that a request has been intercepted. 226 const requestBuffer = await request.arrayBuffer(); 227 const clientMessage = await sendToClient( 228 client, 229 { 230 type: 'REQUEST', 231 payload: { 232 id: requestId, 233 url: request.url, 234 mode: request.mode, 235 method: request.method, 236 headers: Object.fromEntries(request.headers.entries()), 237 cache: request.cache, 238 credentials: request.credentials, 239 destination: request.destination, 240 integrity: request.integrity, 241 redirect: request.redirect, 242 referrer: request.referrer, 243 referrerPolicy: request.referrerPolicy, 244 body: requestBuffer, 245 keepalive: request.keepalive, 246 }, 247 }, 248 [requestBuffer], 249 ); 250 251 switch (clientMessage.type) { 252 case 'MOCK_RESPONSE': { 253 return respondWithMock(clientMessage.data); 254 } 255 256 case 'PASSTHROUGH': { 257 return passthrough(); 258 } 259 } 260 261 return passthrough(); 262} 263 264function sendToClient(client, message, transferrables = []) { 265 return new Promise((resolve, reject) => { 266 const channel = new MessageChannel(); 267 268 channel.port1.onmessage = (event) => { 269 if (event.data && event.data.error) { 270 return reject(event.data.error); 271 } 272 273 resolve(event.data); 274 }; 275 276 client.postMessage(message, [channel.port2].concat(transferrables.filter(
276Boolean))); 277 }); 278} 279 280async function respondWithMock(response) { 281 // Setting response status code to 0 is a no-op. 282 // However, when responding with a "Response.error()", the produced Response 283 // instance will have status code set to 0. Since it's not possible to create 284 // a Response instance with status code 0, handle that use-case separately. 285 if (response.status === 0) { 286 return Response.error(); 287 } 288 289 const mockedResponse = new Response(response.body, response); 290 291 Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, { 292 value: true, 293 enumerable: true, 294 }); 295 296 return mockedResponse; 297}
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.