|
| 1 | +/* eslint-disable */ |
| 2 | +/* tslint:disable */ |
| 3 | + |
| 4 | +/** |
| 5 | + * Mock Service Worker (0.36.8). |
| 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 | + |
| 11 | +const INTEGRITY_CHECKSUM = '02f4ad4a2797f85668baf196e553d929'; |
| 12 | +const bypassHeaderName = 'x-msw-bypass'; |
| 13 | +const activeClientIds = new Set(); |
| 14 | + |
| 15 | +self.addEventListener('install', function () { |
| 16 | + return self.skipWaiting(); |
| 17 | +}); |
| 18 | + |
| 19 | +self.addEventListener('activate', async function (event) { |
| 20 | + return self.clients.claim(); |
| 21 | +}); |
| 22 | + |
| 23 | +self.addEventListener('message', async function (event) { |
| 24 | + const clientId = event.source.id; |
| 25 | + |
| 26 | + if (!clientId || !self.clients) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + const client = await self.clients.get(clientId); |
| 31 | + |
| 32 | + if (!client) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const allClients = await self.clients.matchAll(); |
| 37 | + |
| 38 | + switch (event.data) { |
| 39 | + case 'KEEPALIVE_REQUEST': { |
| 40 | + sendToClient(client, { |
| 41 | + type: 'KEEPALIVE_RESPONSE', |
| 42 | + }); |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + case 'INTEGRITY_CHECK_REQUEST': { |
| 47 | + sendToClient(client, { |
| 48 | + type: 'INTEGRITY_CHECK_RESPONSE', |
| 49 | + payload: INTEGRITY_CHECKSUM, |
| 50 | + }); |
| 51 | + break; |
| 52 | + } |
| 53 | + |
| 54 | + case 'MOCK_ACTIVATE': { |
| 55 | + activeClientIds.add(clientId); |
| 56 | + |
| 57 | + sendToClient(client, { |
| 58 | + type: 'MOCKING_ENABLED', |
| 59 | + payload: true, |
| 60 | + }); |
| 61 | + break; |
| 62 | + } |
| 63 | + |
| 64 | + case 'MOCK_DEACTIVATE': { |
| 65 | + activeClientIds.delete(clientId); |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + case 'CLIENT_CLOSED': { |
| 70 | + activeClientIds.delete(clientId); |
| 71 | + |
| 72 | + const remainingClients = allClients.filter(client => { |
| 73 | + return client.id !== clientId; |
| 74 | + }); |
| 75 | + |
| 76 | + // Unregister itself when there are no more clients |
| 77 | + if (remainingClients.length === 0) { |
| 78 | + self.registration.unregister(); |
| 79 | + } |
| 80 | + |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | +}); |
| 85 | + |
| 86 | +// Resolve the "main" client for the given event. |
| 87 | +// Client that issues a request doesn't necessarily equal the client |
| 88 | +// that registered the worker. It's with the latter the worker should |
| 89 | +// communicate with during the response resolving phase. |
| 90 | +async function resolveMainClient(event) { |
| 91 | + const client = await self.clients.get(event.clientId); |
| 92 | + |
| 93 | + if (client.frameType === 'top-level') { |
| 94 | + return client; |
| 95 | + } |
| 96 | + |
| 97 | + const allClients = await self.clients.matchAll(); |
| 98 | + |
| 99 | + return allClients |
| 100 | + .filter(client => { |
| 101 | + // Get only those clients that are currently visible. |
| 102 | + return client.visibilityState === 'visible'; |
| 103 | + }) |
| 104 | + .find(client => { |
| 105 | + // Find the client ID that's recorded in the |
| 106 | + // set of clients that have registered the worker. |
| 107 | + return activeClientIds.has(client.id); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +async function handleRequest(event, requestId) { |
| 112 | + const client = await resolveMainClient(event); |
| 113 | + const response = await getResponse(event, client, requestId); |
| 114 | + |
| 115 | + // Send back the response clone for the "response:*" life-cycle events. |
| 116 | + // Ensure MSW is active and ready to handle the message, otherwise |
| 117 | + // this message will pend indefinitely. |
| 118 | + if (client && activeClientIds.has(client.id)) { |
| 119 | + (async function () { |
| 120 | + const clonedResponse = response.clone(); |
| 121 | + sendToClient(client, { |
| 122 | + type: 'RESPONSE', |
| 123 | + payload: { |
| 124 | + requestId, |
| 125 | + type: clonedResponse.type, |
| 126 | + ok: clonedResponse.ok, |
| 127 | + status: clonedResponse.status, |
| 128 | + statusText: clonedResponse.statusText, |
| 129 | + body: clonedResponse.body === null ? null : await clonedResponse.text(), |
| 130 | + headers: serializeHeaders(clonedResponse.headers), |
| 131 | + redirected: clonedResponse.redirected, |
| 132 | + }, |
| 133 | + }); |
| 134 | + })(); |
| 135 | + } |
| 136 | + |
| 137 | + return response; |
| 138 | +} |
| 139 | + |
| 140 | +async function getResponse(event, client, requestId) { |
| 141 | + const { request } = event; |
| 142 | + const requestClone = request.clone(); |
| 143 | + const getOriginalResponse = () => fetch(requestClone); |
| 144 | + |
| 145 | + // Bypass mocking when the request client is not active. |
| 146 | + if (!client) { |
| 147 | + return getOriginalResponse(); |
| 148 | + } |
| 149 | + |
| 150 | + // Bypass initial page load requests (i.e. static assets). |
| 151 | + // The absence of the immediate/parent client in the map of the active clients |
| 152 | + // means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet |
| 153 | + // and is not ready to handle requests. |
| 154 | + if (!activeClientIds.has(client.id)) { |
| 155 | + return await getOriginalResponse(); |
| 156 | + } |
| 157 | + |
| 158 | + // Bypass requests with the explicit bypass header |
| 159 | + if (requestClone.headers.get(bypassHeaderName) === 'true') { |
| 160 | + const cleanRequestHeaders = serializeHeaders(requestClone.headers); |
| 161 | + |
| 162 | + // Remove the bypass header to comply with the CORS preflight check. |
| 163 | + delete cleanRequestHeaders[bypassHeaderName]; |
| 164 | + |
| 165 | + const originalRequest = new Request(requestClone, { |
| 166 | + headers: new Headers(cleanRequestHeaders), |
| 167 | + }); |
| 168 | + |
| 169 | + return fetch(originalRequest); |
| 170 | + } |
| 171 | + |
| 172 | + // Send the request to the client-side MSW. |
| 173 | + const reqHeaders = serializeHeaders(request.headers); |
| 174 | + const body = await request.text(); |
| 175 | + |
| 176 | + const clientMessage = await sendToClient(client, { |
| 177 | + type: 'REQUEST', |
| 178 | + payload: { |
| 179 | + id: requestId, |
| 180 | + url: request.url, |
| 181 | + method: request.method, |
| 182 | + headers: reqHeaders, |
| 183 | + cache: request.cache, |
| 184 | + mode: request.mode, |
| 185 | + credentials: request.credentials, |
| 186 | + destination: request.destination, |
| 187 | + integrity: request.integrity, |
| 188 | + redirect: request.redirect, |
| 189 | + referrer: request.referrer, |
| 190 | + referrerPolicy: request.referrerPolicy, |
| 191 | + body, |
| 192 | + bodyUsed: request.bodyUsed, |
| 193 | + keepalive: request.keepalive, |
| 194 | + }, |
| 195 | + }); |
| 196 | + |
| 197 | + switch (clientMessage.type) { |
| 198 | + case 'MOCK_SUCCESS': { |
| 199 | + return delayPromise(() => respondWithMock(clientMessage), clientMessage.payload.delay); |
| 200 | + } |
| 201 | + |
| 202 | + case 'MOCK_NOT_FOUND': { |
| 203 | + return getOriginalResponse(); |
| 204 | + } |
| 205 | + |
| 206 | + case 'NETWORK_ERROR': { |
| 207 | + const { name, message } = clientMessage.payload; |
| 208 | + const networkError = new Error(message); |
| 209 | + networkError.name = name; |
| 210 | + |
| 211 | + // Rejecting a request Promise emulates a network error. |
| 212 | + throw networkError; |
| 213 | + } |
| 214 | + |
| 215 | + case 'INTERNAL_ERROR': { |
| 216 | + const parsedBody = JSON.parse(clientMessage.payload.body); |
| 217 | + |
| 218 | + console.error( |
| 219 | + `\ |
| 220 | +[MSW] Uncaught exception in the request handler for "%s %s": |
| 221 | +
|
| 222 | +${parsedBody.location} |
| 223 | +
|
| 224 | +This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error, as it indicates a mistake in your code. If you wish to mock an error response, please see this guide: https://mswjs.io/docs/recipes/mocking-error-responses\ |
| 225 | +`, |
| 226 | + request.method, |
| 227 | + request.url, |
| 228 | + ); |
| 229 | + |
| 230 | + return respondWithMock(clientMessage); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + return getOriginalResponse(); |
| 235 | +} |
| 236 | + |
| 237 | +self.addEventListener('fetch', function (event) { |
| 238 | + const { request } = event; |
| 239 | + const accept = request.headers.get('accept') || ''; |
| 240 | + |
| 241 | + // Bypass server-sent events. |
| 242 | + if (accept.includes('text/event-stream')) { |
| 243 | + return; |
| 244 | + } |
| 245 | + |
| 246 | + // Bypass navigation requests. |
| 247 | + if (request.mode === 'navigate') { |
| 248 | + return; |
| 249 | + } |
| 250 | + |
| 251 | + // Opening the DevTools triggers the "only-if-cached" request |
| 252 | + // that cannot be handled by the worker. Bypass such requests. |
| 253 | + if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') { |
| 254 | + return; |
| 255 | + } |
| 256 | + |
| 257 | + // Bypass all requests when there are no active clients. |
| 258 | + // Prevents the self-unregistered worked from handling requests |
| 259 | + // after it's been deleted (still remains active until the next reload). |
| 260 | + if (activeClientIds.size === 0) { |
| 261 | + return; |
| 262 | + } |
| 263 | + |
| 264 | + const requestId = uuidv4(); |
| 265 | + |
| 266 | + return event.respondWith( |
| 267 | + handleRequest(event, requestId).catch(error => { |
| 268 | + if (error.name === 'NetworkError') { |
| 269 | + console.warn( |
| 270 | + '[MSW] Successfully emulated a network error for the "%s %s" request.', |
| 271 | + request.method, |
| 272 | + request.url, |
| 273 | + ); |
| 274 | + return; |
| 275 | + } |
| 276 | + |
| 277 | + // At this point, any exception indicates an issue with the original request/response. |
| 278 | + console.error( |
| 279 | + `\ |
| 280 | +[MSW] Caught an exception from the "%s %s" request (%s). This is probably not a problem with Mock Service Worker. There is likely an additional logging output above.`, |
| 281 | + request.method, |
| 282 | + request.url, |
| 283 | + `${error.name}: ${error.message}`, |
| 284 | + ); |
| 285 | + }), |
| 286 | + ); |
| 287 | +}); |
| 288 | + |
| 289 | +function serializeHeaders(headers) { |
| 290 | + const reqHeaders = {}; |
| 291 | + headers.forEach((value, name) => { |
| 292 | + reqHeaders[name] = reqHeaders[name] ? [].concat(reqHeaders[name]).concat(value) : value; |
| 293 | + }); |
| 294 | + return reqHeaders; |
| 295 | +} |
| 296 | + |
| 297 | +function sendToClient(client, message) { |
| 298 | + return new Promise((resolve, reject) => { |
| 299 | + const channel = new MessageChannel(); |
| 300 | + |
| 301 | + channel.port1.onmessage = event => { |
| 302 | + if (event.data && event.data.error) { |
| 303 | + return reject(event.data.error); |
| 304 | + } |
| 305 | + |
| 306 | + resolve(event.data); |
| 307 | + }; |
| 308 | + |
| 309 | + client.postMessage(JSON.stringify(message), [channel.port2]); |
| 310 | + }); |
| 311 | +} |
| 312 | + |
| 313 | +function delayPromise(cb, duration) { |
| 314 | + return new Promise(resolve => { |
| 315 | + setTimeout(() => resolve(cb()), duration); |
| 316 | + }); |
| 317 | +} |
| 318 | + |
| 319 | +function respondWithMock(clientMessage) { |
| 320 | + return new Response(clientMessage.payload.body, { |
| 321 | + ...clientMessage.payload, |
| 322 | + headers: clientMessage.payload.headers, |
| 323 | + }); |
| 324 | +} |
| 325 | + |
| 326 | +function uuidv4() { |
| 327 | + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { |
| 328 | + const r = (Math.random() * 16) | 0; |
| 329 | + const v = c == 'x' ? r : (r & 0x3) | 0x8; |
| 330 | + return v.toString(16); |
| 331 | + }); |
| 332 | +} |
0 commit comments