|
| 1 | +/** |
| 2 | + * Mock Service Worker. |
| 3 | + * @see https://github.com/open-draft/msw |
| 4 | + * - Please do NOT modify this file. |
| 5 | + * - Please do NOT serve this file on production. |
| 6 | + */ |
| 7 | +/* eslint-disable */ |
| 8 | +/* tslint:disable */ |
| 9 | + |
| 10 | +const INTEGRITY_CHECKSUM = 'dcff59cce1b1cf75d9d30c0b6a63fdba' |
| 11 | +const bypassHeaderName = 'x-msw-bypass' |
| 12 | + |
| 13 | +let clients = {} |
| 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 | + const client = await event.currentTarget.clients.get(clientId) |
| 26 | + const allClients = await self.clients.matchAll() |
| 27 | + const allClientIds = allClients.map((client) => client.id) |
| 28 | + |
| 29 | + switch (event.data) { |
| 30 | + case 'INTEGRITY_CHECK_REQUEST': { |
| 31 | + sendToClient(client, { |
| 32 | + type: 'INTEGRITY_CHECK_RESPONSE', |
| 33 | + payload: INTEGRITY_CHECKSUM, |
| 34 | + }) |
| 35 | + break |
| 36 | + } |
| 37 | + |
| 38 | + case 'MOCK_ACTIVATE': { |
| 39 | + clients = ensureKeys(allClientIds, clients) |
| 40 | + clients[clientId] = true |
| 41 | + |
| 42 | + sendToClient(client, { |
| 43 | + type: 'MOCKING_ENABLED', |
| 44 | + payload: true, |
| 45 | + }) |
| 46 | + break |
| 47 | + } |
| 48 | + |
| 49 | + case 'MOCK_DEACTIVATE': { |
| 50 | + clients = ensureKeys(allClientIds, clients) |
| 51 | + clients[clientId] = false |
| 52 | + break |
| 53 | + } |
| 54 | + |
| 55 | + case 'CLIENT_CLOSED': { |
| 56 | + delete clients[clientId] |
| 57 | + |
| 58 | + // Unregister itself when there are no more clients |
| 59 | + if (Object.keys(clients).length === 0) { |
| 60 | + self.registration.unregister() |
| 61 | + } |
| 62 | + |
| 63 | + break |
| 64 | + } |
| 65 | + } |
| 66 | +}) |
| 67 | + |
| 68 | +self.addEventListener('fetch', async function (event) { |
| 69 | + const { clientId, request } = event |
| 70 | + const requestClone = request.clone() |
| 71 | + const getOriginalResponse = () => fetch(requestClone) |
| 72 | + |
| 73 | + // Opening the DevTools triggers the "only-if-cached" request |
| 74 | + // that cannot be handled by the worker. Bypass such requests. |
| 75 | + if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + event.respondWith( |
| 80 | + new Promise(async (resolve) => { |
| 81 | + const client = await event.target.clients.get(clientId) |
| 82 | + |
| 83 | + if ( |
| 84 | + // Bypass mocking when no clients active |
| 85 | + !client || |
| 86 | + // Bypass mocking if the current client has mocking disabled |
| 87 | + !clients[clientId] || |
| 88 | + // Bypass mocking for navigation requests |
| 89 | + request.mode === 'navigate' |
| 90 | + ) { |
| 91 | + return resolve(getOriginalResponse()) |
| 92 | + } |
| 93 | + |
| 94 | + // Bypass requests with the explicit bypass header |
| 95 | + if (requestClone.headers.get(bypassHeaderName) === 'true') { |
| 96 | + const modifiedHeaders = serializeHeaders(requestClone.headers) |
| 97 | + // Remove the bypass header to comply with the CORS preflight check |
| 98 | + delete modifiedHeaders[bypassHeaderName] |
| 99 | + |
| 100 | + const originalRequest = new Request(requestClone, { |
| 101 | + headers: new Headers(modifiedHeaders), |
| 102 | + }) |
| 103 | + |
| 104 | + return resolve(fetch(originalRequest)) |
| 105 | + } |
| 106 | + |
| 107 | + const reqHeaders = serializeHeaders(request.headers) |
| 108 | + const body = await request |
| 109 | + .json() |
| 110 | + .catch(() => request.text()) |
| 111 | + .catch(() => null) |
| 112 | + |
| 113 | + const rawClientMessage = await sendToClient(client, { |
| 114 | + type: 'REQUEST', |
| 115 | + payload: { |
| 116 | + url: request.url, |
| 117 | + method: request.method, |
| 118 | + headers: reqHeaders, |
| 119 | + cache: request.cache, |
| 120 | + mode: request.mode, |
| 121 | + credentials: request.credentials, |
| 122 | + destination: request.destination, |
| 123 | + integrity: request.integrity, |
| 124 | + redirect: request.redirect, |
| 125 | + referrer: request.referrer, |
| 126 | + referrerPolicy: request.referrerPolicy, |
| 127 | + body, |
| 128 | + bodyUsed: request.bodyUsed, |
| 129 | + keepalive: request.keepalive, |
| 130 | + }, |
| 131 | + }) |
| 132 | + |
| 133 | + const clientMessage = JSON.parse(rawClientMessage) |
| 134 | + |
| 135 | + switch (clientMessage.type) { |
| 136 | + case 'MOCK_SUCCESS': { |
| 137 | + setTimeout( |
| 138 | + resolve.bind(this, createResponse(clientMessage)), |
| 139 | + clientMessage.delay, |
| 140 | + ) |
| 141 | + break |
| 142 | + } |
| 143 | + |
| 144 | + case 'MOCK_NOT_FOUND': { |
| 145 | + return resolve(getOriginalResponse()) |
| 146 | + } |
| 147 | + |
| 148 | + case 'INTERNAL_ERROR': { |
| 149 | + const parsedBody = JSON.parse(clientMessage.payload.body) |
| 150 | + |
| 151 | + console.error( |
| 152 | + `\ |
| 153 | +[MSW] Request handler function for "%s %s" has thrown the following exception: |
| 154 | +
|
| 155 | +${parsedBody.errorType}: ${parsedBody.message} |
| 156 | +(see more detailed error stack trace in the mocked response body) |
| 157 | +
|
| 158 | +This exception has been gracefully handled as a 500 response, however, it's strongly recommended to resolve this error. |
| 159 | +If you wish to mock an error response, please refer to this guide: https://redd.gitbook.io/msw/recipes/mocking-error-responses\ |
| 160 | + `, |
| 161 | + request.method, |
| 162 | + request.url, |
| 163 | + ) |
| 164 | + |
| 165 | + return resolve(createResponse(clientMessage)) |
| 166 | + } |
| 167 | + } |
| 168 | + }).catch((error) => { |
| 169 | + console.error( |
| 170 | + '[MSW] Failed to mock a "%s" request to "%s": %s', |
| 171 | + request.method, |
| 172 | + request.url, |
| 173 | + error, |
| 174 | + ) |
| 175 | + }), |
| 176 | + ) |
| 177 | +}) |
| 178 | + |
| 179 | +function serializeHeaders(headers) { |
| 180 | + const reqHeaders = {} |
| 181 | + headers.forEach((value, name) => { |
| 182 | + reqHeaders[name] = value |
| 183 | + }) |
| 184 | + return reqHeaders |
| 185 | +} |
| 186 | + |
| 187 | +function sendToClient(client, message) { |
| 188 | + return new Promise((resolve, reject) => { |
| 189 | + const channel = new MessageChannel() |
| 190 | + |
| 191 | + channel.port1.onmessage = (event) => { |
| 192 | + if (event.data && event.data.error) { |
| 193 | + reject(event.data.error) |
| 194 | + } else { |
| 195 | + resolve(event.data) |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + client.postMessage(JSON.stringify(message), [channel.port2]) |
| 200 | + }) |
| 201 | +} |
| 202 | + |
| 203 | +function createResponse(clientMessage) { |
| 204 | + return new Response(clientMessage.payload.body, { |
| 205 | + ...clientMessage.payload, |
| 206 | + headers: clientMessage.payload.headers, |
| 207 | + }) |
| 208 | +} |
| 209 | + |
| 210 | +function ensureKeys(keys, obj) { |
| 211 | + return Object.keys(obj).reduce((acc, key) => { |
| 212 | + if (keys.includes(key)) { |
| 213 | + acc[key] = obj[key] |
| 214 | + } |
| 215 | + |
| 216 | + return acc |
| 217 | + }, {}) |
| 218 | +} |
0 commit comments