webpack-bootstrap-ui-kit/src/_graphql/mockServiceWorker.js

293 lines
7.8 KiB
JavaScript
Raw Permalink Normal View History

2021-08-09 15:10:02 +02:00
/* eslint-disable */
/* tslint:disable */
2021-02-12 19:00:26 +01:00
/**
* Mock Service Worker (2.0.4).
2021-02-12 19:00:26 +01:00
* @see https://github.com/mswjs/msw
* - Please do NOT modify this file.
* - Please do NOT serve this file on production.
*/
const INTEGRITY_CHECKSUM = '0877fcdc026242810f5bfde0d7178db4'
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
2021-09-05 21:21:22 +02:00
const activeClientIds = new Set()
2021-02-12 19:00:26 +01:00
2021-09-05 21:21:22 +02:00
self.addEventListener('install', function () {
2022-07-12 16:45:34 +02:00
self.skipWaiting()
2021-09-05 21:21:22 +02:00
})
2021-02-12 19:00:26 +01:00
2022-07-12 16:45:34 +02:00
self.addEventListener('activate', function (event) {
event.waitUntil(self.clients.claim())
2021-09-05 21:21:22 +02:00
})
2021-02-12 19:00:26 +01:00
2021-09-05 21:21:22 +02:00
self.addEventListener('message', async function (event) {
const clientId = event.source.id
2021-02-12 19:00:26 +01:00
if (!clientId || !self.clients) {
2021-09-05 21:21:22 +02:00
return
2021-02-12 19:00:26 +01:00
}
2021-09-05 21:21:22 +02:00
const client = await self.clients.get(clientId)
2021-02-12 19:00:26 +01:00
if (!client) {
2021-09-05 21:21:22 +02:00
return
2021-02-12 19:00:26 +01:00
}
2022-07-12 16:45:34 +02:00
const allClients = await self.clients.matchAll({
type: 'window',
})
2021-02-12 19:00:26 +01:00
switch (event.data) {
2021-09-05 21:21:22 +02:00
case 'KEEPALIVE_REQUEST': {
2021-02-12 19:00:26 +01:00
sendToClient(client, {
2021-09-05 21:21:22 +02:00
type: 'KEEPALIVE_RESPONSE',
})
break
2021-02-12 19:00:26 +01:00
}
2021-09-05 21:21:22 +02:00
case 'INTEGRITY_CHECK_REQUEST': {
2021-02-12 19:00:26 +01:00
sendToClient(client, {
2021-09-05 21:21:22 +02:00
type: 'INTEGRITY_CHECK_RESPONSE',
2021-02-12 19:00:26 +01:00
payload: INTEGRITY_CHECKSUM,
2021-09-05 21:21:22 +02:00
})
break
2021-02-12 19:00:26 +01:00
}
2021-09-05 21:21:22 +02:00
case 'MOCK_ACTIVATE': {
activeClientIds.add(clientId)
2021-02-12 19:00:26 +01:00
sendToClient(client, {
2021-09-05 21:21:22 +02:00
type: 'MOCKING_ENABLED',
2021-02-12 19:00:26 +01:00
payload: true,
2021-09-05 21:21:22 +02:00
})
break
2021-02-12 19:00:26 +01:00
}
2021-09-05 21:21:22 +02:00
case 'MOCK_DEACTIVATE': {
activeClientIds.delete(clientId)
break
2021-02-12 19:00:26 +01:00
}
2021-09-05 21:21:22 +02:00
case 'CLIENT_CLOSED': {
activeClientIds.delete(clientId)
2021-08-09 15:10:02 +02:00
2021-02-12 19:00:26 +01:00
const remainingClients = allClients.filter((client) => {
2021-09-05 21:21:22 +02:00
return client.id !== clientId
})
2021-02-12 19:00:26 +01:00
// Unregister itself when there are no more clients
if (remainingClients.length === 0) {
2021-09-05 21:21:22 +02:00
self.registration.unregister()
2021-02-12 19:00:26 +01:00
}
2021-09-05 21:21:22 +02:00
break
2021-02-12 19:00:26 +01:00
}
}
2021-09-05 21:21:22 +02:00
})
2021-02-12 19:00:26 +01:00
2022-07-12 16:45:34 +02:00
self.addEventListener('fetch', function (event) {
const { request } = event
2021-08-09 15:10:02 +02:00
2022-07-12 16:45:34 +02:00
// Bypass navigation requests.
if (request.mode === 'navigate') {
return
}
2021-08-09 15:10:02 +02:00
2022-07-12 16:45:34 +02:00
// Opening the DevTools triggers the "only-if-cached" request
// that cannot be handled by the worker. Bypass such requests.
if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') {
return
}
// Bypass all requests when there are no active clients.
// Prevents the self-unregistered worked from handling requests
// after it's been deleted (still remains active until the next reload).
if (activeClientIds.size === 0) {
return
}
// Generate unique request ID.
const requestId = crypto.randomUUID()
event.respondWith(handleRequest(event, requestId))
2022-07-12 16:45:34 +02:00
})
2021-08-09 15:10:02 +02:00
async function handleRequest(event, requestId) {
2021-12-28 03:37:16 +01:00
const client = await resolveMainClient(event)
2021-09-05 21:21:22 +02:00
const response = await getResponse(event, client, requestId)
2021-08-09 15:10:02 +02:00
// Send back the response clone for the "response:*" life-cycle events.
// Ensure MSW is active and ready to handle the message, otherwise
// this message will pend indefinitely.
if (client && activeClientIds.has(client.id)) {
2021-09-05 21:21:22 +02:00
;(async function () {
const responseClone = response.clone()
// When performing original requests, response body will
// always be a ReadableStream, even for 204 responses.
// But when creating a new Response instance on the client,
// the body for a 204 response must be null.
const responseBody = response.status === 204 ? null : responseClone.body
sendToClient(
client,
{
type: 'RESPONSE',
payload: {
requestId,
isMockedResponse: IS_MOCKED_RESPONSE in response,
type: responseClone.type,
status: responseClone.status,
statusText: responseClone.statusText,
body: responseBody,
headers: Object.fromEntries(responseClone.headers.entries()),
},
2021-08-09 15:10:02 +02:00
},
[responseBody],
)
2021-09-05 21:21:22 +02:00
})()
2021-08-09 15:10:02 +02:00
}
2021-09-05 21:21:22 +02:00
return response
2021-08-09 15:10:02 +02:00
}
2022-07-12 16:45:34 +02:00
// Resolve the main client for the given event.
// Client that issues a request doesn't necessarily equal the client
// that registered the worker. It's with the latter the worker should
// communicate with during the response resolving phase.
async function resolveMainClient(event) {
const client = await self.clients.get(event.clientId)
2023-03-28 13:25:33 +02:00
if (client?.frameType === 'top-level') {
2022-07-12 16:45:34 +02:00
return client
}
const allClients = await self.clients.matchAll({
type: 'window',
})
return allClients
.filter((client) => {
// Get only those clients that are currently visible.
return client.visibilityState === 'visible'
})
.find((client) => {
// Find the client ID that's recorded in the
// set of clients that have registered the worker.
return activeClientIds.has(client.id)
})
}
2021-08-09 15:10:02 +02:00
async function getResponse(event, client, requestId) {
2021-09-05 21:21:22 +02:00
const { request } = event
// Clone the request because it might've been already used
// (i.e. its body has been read and sent to the client).
const requestClone = request.clone()
2022-07-12 16:45:34 +02:00
function passthrough() {
const headers = Object.fromEntries(requestClone.headers.entries())
2021-02-12 19:00:26 +01:00
// Remove internal MSW request header so the passthrough request
// complies with any potential CORS preflight checks on the server.
// Some servers forbid unknown request headers.
delete headers['x-msw-intention']
2022-07-12 16:45:34 +02:00
return fetch(requestClone, { headers })
2022-07-12 16:45:34 +02:00
}
// Bypass mocking when the client is not active.
2021-08-09 15:10:02 +02:00
if (!client) {
2022-07-12 16:45:34 +02:00
return passthrough()
2021-02-12 19:00:26 +01:00
}
2021-08-09 15:10:02 +02:00
// Bypass initial page load requests (i.e. static assets).
// The absence of the immediate/parent client in the map of the active clients
// means that MSW hasn't dispatched the "MOCK_ACTIVATE" event yet
// and is not ready to handle requests.
if (!activeClientIds.has(client.id)) {
2022-07-12 16:45:34 +02:00
return passthrough()
2021-02-12 19:00:26 +01:00
}
2022-07-12 16:45:34 +02:00
// Bypass requests with the explicit bypass header.
// Such requests can be issued by "ctx.fetch()".
const mswIntention = request.headers.get('x-msw-intention')
if (['bypass', 'passthrough'].includes(mswIntention)) {
2022-07-12 16:45:34 +02:00
return passthrough()
2021-02-12 19:00:26 +01:00
}
2022-07-12 16:45:34 +02:00
// Notify the client that a request has been intercepted.
const requestBuffer = await request.arrayBuffer()
const clientMessage = await sendToClient(
client,
{
type: 'REQUEST',
payload: {
id: requestId,
url: request.url,
mode: request.mode,
method: request.method,
headers: Object.fromEntries(request.headers.entries()),
cache: request.cache,
credentials: request.credentials,
destination: request.destination,
integrity: request.integrity,
redirect: request.redirect,
referrer: request.referrer,
referrerPolicy: request.referrerPolicy,
body: requestBuffer,
keepalive: request.keepalive,
},
2021-08-09 15:10:02 +02:00
},
[requestBuffer],
)
2021-02-12 19:00:26 +01:00
2021-08-09 15:10:02 +02:00
switch (clientMessage.type) {
2022-07-12 16:45:34 +02:00
case 'MOCK_RESPONSE': {
2023-03-28 13:25:33 +02:00
return respondWithMock(clientMessage.data)
2021-08-09 15:10:02 +02:00
}
2021-02-12 19:00:26 +01:00
2021-09-05 21:21:22 +02:00
case 'MOCK_NOT_FOUND': {
2022-07-12 16:45:34 +02:00
return passthrough()
2021-08-09 15:10:02 +02:00
}
}
2022-07-12 16:45:34 +02:00
return passthrough()
2021-02-12 19:00:26 +01:00
}
function sendToClient(client, message, transferrables = []) {
2021-02-12 19:00:26 +01:00
return new Promise((resolve, reject) => {
2021-09-05 21:21:22 +02:00
const channel = new MessageChannel()
2021-02-12 19:00:26 +01:00
channel.port1.onmessage = (event) => {
if (event.data && event.data.error) {
2021-09-05 21:21:22 +02:00
return reject(event.data.error)
2021-02-12 19:00:26 +01:00
}
2021-08-09 15:10:02 +02:00
2021-09-05 21:21:22 +02:00
resolve(event.data)
}
2021-02-12 19:00:26 +01:00
client.postMessage(
message,
[channel.port2].concat(transferrables.filter(Boolean)),
)
2021-09-05 21:21:22 +02:00
})
2021-02-12 19:00:26 +01:00
}
async function respondWithMock(response) {
// Setting response status code to 0 is a no-op.
// However, when responding with a "Response.error()", the produced Response
// instance will have status code set to 0. Since it's not possible to create
// a Response instance with status code 0, handle that use-case separately.
if (response.status === 0) {
return Response.error()
}
const mockedResponse = new Response(response.body, response)
Reflect.defineProperty(mockedResponse, IS_MOCKED_RESPONSE, {
value: true,
enumerable: true,
2021-09-05 21:21:22 +02:00
})
2021-08-09 15:10:02 +02:00
return mockedResponse
2021-02-12 19:00:26 +01:00
}