实现内容(仲裁裁决驱动,首次即最终方案): P4 核心功能 - 认证:localStorage token 存储(F12)+ REST 登录(ISSUE-004)+ refreshAccessToken 竞态防护 - 子女切换:ChildSwitcher(Tab ≤3 / 下拉 ≥4)+ Zustand store(ISSUE-009 纯前端切换) - 数据查询:urql GraphQL 消费 parent-bff(F9)+ TanStack Query 缓存 - 通知中心:NotificationFeed + 已读/全部已读 mutations - 通知偏好:三维矩阵 + ISSUE-033 localStorage 降级 - 5 层状态管理:URL/Server/Client/Global UI/Form - 跨标签同步:BroadcastChannel + storage 事件 P5 实时推送 - WebSocket 连接 push-gateway + 指数退避重连 - HTTP 轮询降级(60s)+ 实时通知 Hook P6 硬化 - Web Vitals 上报 + OTel trace - i18n 5 语言(zh-CN/en-US/zh-TW/ja-JP/ar-SA 含 RTL) - PWA manifest + Service Worker - CSP 安全头 + 权限点 F7 命名 + 设计令牌三层 测试与构建 - Vitest 92 测试全通过(utils/auth/child-store/ChildSwitcher/NotificationFeed/login) - MSW mock 未就绪上游(parent-bff GraphQL + iam REST + iam GetChildrenByParent P0 阻塞用 fixtures) - Dockerfile 多阶段构建(G1,端口 4002,HEALTHCHECK /api/health) - typecheck + lint 零错误 经验沉淀 - known-issues.md §2.13 追加 12 条实现期经验(无 AI 身份标注) - arch.db 已更新(15 TS 模块 / 482 符号 / 138 proto) 依据:02-architecture-design.md(回写总裁裁决)、coord-final-decisions.md、 president-final-rulings.md、parent-portal_workline.md、parent-portal_contract.md
276 lines
6.6 KiB
JavaScript
276 lines
6.6 KiB
JavaScript
/* eslint-disable */
|
|
/* tslint:disable */
|
|
|
|
/**
|
|
* Mock Service Worker (2.4.0).
|
|
* @see https://github.com/mswjs/msw
|
|
* - Please do NOT modify this file.
|
|
* - Please do NOT serve this file on production.
|
|
*/
|
|
|
|
const PACKAGE_VERSION = '2.4.0'
|
|
const INTEGRITY_CHECKSUM = '0070d3e49f0d1f2c83d8e93b2c2c1a35'
|
|
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
|
|
const activeClientIds = new Set()
|
|
|
|
self.addEventListener('install', function () {
|
|
self.skipWaiting()
|
|
})
|
|
|
|
self.addEventListener('activate', function (event) {
|
|
event.waitUntil(self.clients.claim())
|
|
})
|
|
|
|
self.addEventListener('message', async function (event) {
|
|
const clientId = event.source.id
|
|
|
|
if (!clientId || !self.clients) {
|
|
return
|
|
}
|
|
|
|
const client = await self.clients.get(clientId)
|
|
|
|
if (!client) {
|
|
return
|
|
}
|
|
|
|
const allClients = await self.clients.matchAll({
|
|
type: 'window',
|
|
})
|
|
|
|
switch (event.data) {
|
|
case 'KEEPALIVE_REQUEST': {
|
|
sendToClient(client, {
|
|
type: 'KEEPALIVE_RESPONSE',
|
|
})
|
|
break
|
|
}
|
|
|
|
case 'INTEGRITY_CHECK_REQUEST': {
|
|
sendToClient(client, {
|
|
type: 'INTEGRITY_CHECK_RESPONSE',
|
|
payload: {
|
|
packageVersion: PACKAGE_VERSION,
|
|
checksum: INTEGRITY_CHECKSUM,
|
|
},
|
|
})
|
|
break
|
|
}
|
|
|
|
case 'MOCK_ACTIVATE': {
|
|
activeClientIds.add(clientId)
|
|
|
|
sendToClient(client, {
|
|
type: 'MOCKING_ENABLED',
|
|
payload: {
|
|
client: {
|
|
id: client.id,
|
|
frameType: client.frameType,
|
|
},
|
|
},
|
|
})
|
|
break
|
|
}
|
|
|
|
case 'MOCK_DEACTIVATE': {
|
|
activeClientIds.delete(clientId)
|
|
break
|
|
}
|
|
|
|
case 'CLIENT_CLOSED': {
|
|
activeClientIds.delete(clientId)
|
|
|
|
const remainingClients = allClients.filter((client) => {
|
|
return client.id !== clientId
|
|
})
|
|
|
|
if (remainingClients.length === 0) {
|
|
self.registration.unregister()
|
|
}
|
|
|
|
break
|
|
}
|
|
}
|
|
})
|
|
|
|
self.addEventListener('fetch', function (event) {
|
|
const { request } = event
|
|
|
|
// Bypass navigation requests.
|
|
if (request.mode === 'navigate') {
|
|
return
|
|
}
|
|
|
|
// Opening the DevTools triggers the "only-if-cached" request
|
|
// that cannot be handled by the Mock Service Worker.
|
|
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.
|
|
if (activeClientIds.size === 0) {
|
|
return
|
|
}
|
|
|
|
// Generate unique request ID.
|
|
const requestId = crypto.randomUUID()
|
|
event.respondWith(handleRequest(event, requestId))
|
|
})
|
|
|
|
async function handleRequest(event, requestId) {
|
|
const client = await resolveMainClient(event)
|
|
const response = await getResponse(event, client, requestId)
|
|
|
|
// 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)) {
|
|
;(async function () {
|
|
const responseClone = response.clone()
|
|
|
|
sendToClient(
|
|
client,
|
|
{
|
|
type: 'RESPONSE',
|
|
payload: {
|
|
requestId,
|
|
isMockedResponse: IS_MOCKED_RESPONSE in response,
|
|
type: responseClone.type,
|
|
status: responseClone.status,
|
|
statusText: responseClone.statusText,
|
|
body: responseClone.body,
|
|
headers: Object.fromEntries(responseClone.headers.entries()),
|
|
},
|
|
},
|
|
[responseClone.body],
|
|
)
|
|
})()
|
|
}
|
|
|
|
return response
|
|
}
|
|
|
|
async function resolveMainClient(event) {
|
|
const client = await self.clients.get(event.clientId)
|
|
|
|
if (client?.frameType === 'top-level') {
|
|
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)
|
|
})
|
|
}
|
|
|
|
async function getResponse(event, client, requestId) {
|
|
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()
|
|
|
|
function passthrough() {
|
|
const headers = Object.fromEntries(requestClone.headers.entries())
|
|
|
|
// Remove internal MSW request header so the passthrough request
|
|
// matches the original request body.
|
|
delete headers['x-msw-intention']
|
|
|
|
return fetch(requestClone, { headers })
|
|
}
|
|
|
|
// Bypass mocking when the client is not active.
|
|
if (!client) {
|
|
return passthrough()
|
|
}
|
|
|
|
// Bypass initial page load requests.
|
|
if (request.mode === 'same-origin' && request.destination === 'document') {
|
|
return passthrough()
|
|
}
|
|
|
|
// 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,
|
|
},
|
|
},
|
|
[requestBuffer],
|
|
)
|
|
|
|
switch (clientMessage.type) {
|
|
case 'MOCK_RESPONSE': {
|
|
await respondWithMock(event, clientMessage.data)
|
|
return respondWithMock(event, clientMessage.data)
|
|
}
|
|
|
|
case 'PASSTHROUGH': {
|
|
return passthrough()
|
|
}
|
|
}
|
|
|
|
return passthrough()
|
|
}
|
|
|
|
function sendToClient(client, message, transferrables = []) {
|
|
return new Promise((resolve, reject) => {
|
|
const channel = new MessageChannel()
|
|
|
|
channel.port1.onmessage = (event) => {
|
|
if (event.data && event.data.error) {
|
|
return reject(event.data.error)
|
|
}
|
|
|
|
resolve(event.data)
|
|
}
|
|
|
|
client.postMessage(
|
|
message,
|
|
[channel.port2].concat(transferrables.filter(Boolean)),
|
|
)
|
|
})
|
|
}
|
|
|
|
async function respondWithMock(event, response) {
|
|
// Setting response status code to 0 is a known no-op.
|
|
if (response.status === 0) {
|
|
return
|
|
}
|
|
|
|
const mockedResponse = new Response(response.body, response)
|
|
|
|
// Mark the mocked response for MSW internals.
|
|
mockedResponse[IS_MOCKED_RESPONSE] = true
|
|
|
|
return mockedResponse
|
|
}
|