fix: 状态镜像防冲掉——服务端按 key 合并,客户端读不到宿主就不写
This commit is contained in:
+11
-2
@@ -571,8 +571,17 @@ export function apply(ctx: Context) {
|
||||
if (req.method !== 'POST') { json(res, 405, { error: 'method not allowed' }); return }
|
||||
try {
|
||||
const body = await readJsonBody(req)
|
||||
const state = body && typeof body.state === 'object' && body.state !== null ? body.state : null
|
||||
if (!state) { json(res, 400, { error: 'missing state' }); return }
|
||||
const incoming = body && typeof body.state === 'object' && body.state !== null ? body.state : null
|
||||
if (!incoming) { json(res, 400, { error: 'missing state' }); return }
|
||||
// 按 key 合并:客户端只带 view 的半截写入不能把 projects 抹掉
|
||||
let previous: any = null
|
||||
try {
|
||||
const raw = await readFile(file, 'utf8')
|
||||
previous = JSON.parse(raw.charCodeAt(0) === 0xFEFF ? raw.slice(1) : raw)
|
||||
} catch {}
|
||||
const state = previous && typeof previous === 'object' && !Array.isArray(previous)
|
||||
? { ...previous, ...incoming }
|
||||
: incoming
|
||||
const text = JSON.stringify(state)
|
||||
if (text.length > 20 * 1024 * 1024) { json(res, 413, { error: 'state too large' }); return }
|
||||
const fsx = await import('node:fs/promises')
|
||||
|
||||
Reference in New Issue
Block a user