fix: P0 计分根因 + 奖牌结算/奖牌通道 + 身份口径改回证件号 + 19 条 P1(dev_0.1.8)
## P0:双通道项目重复发布跳分(45→85→收敛) results.rank 一列同时充当「个人名次输出」「手工名次输入」「榜单数据源」, 团体手工通道把个人名次当团体名次又按团体表发一遍分。 - 新增 channel_ranks(每个计分通道各自持久化名次,发布时全量重建) 与 channel_manual_ranks(按单位录入的团体手工名次,故意不挂通道外键, 避免规则包重导时级联删掉录入数据) - results.rank 语义收窄为「只表示个人名次」,只由 scope=athlete 通道写回; 团体手工名次的来源改为 TeamRanks 优先、团体报名项兜底, 个人报名项的名次绝不进团体通道 - writeLedgerTx 内重建 channel_ranks;buildRanked / export.loadRanked / realtime.loadRanked 三份同源 SQL 一起改读新表 - 回归:发布两次流水与名次逐字节一致(domain + httpapi 各一条,含变异验证) ## 奖牌结算开关与奖牌通道(P1(5),新功能) - events.award_medals + 通道 count_to_medals / count_to_team_medals (迁移默认 1,升级不改变既有榜单) - 单位榜入选 = 小项结算奖牌 && 通道发奖牌 && 前三名 &&(团体项目 || 个人奖牌并入单位) - 个人榜只取个人通道;金牌榜与积分榜同源同数据、按通道过滤独立计算 - 前端:小项「该项启用奖牌结算」、通道「发奖牌/个人奖牌并入单位金牌榜」、 录入页新增「团体名次」区块(含新接口 GET/PUT .../team-ranks) ## 身份口径改回证件号(P1(15),推翻第三十六轮) - 匹配顺序恢复 证件号 → 号码布 → 姓名+单位;新增 athleteByIDCard - 同表重复证件号 = 同一人报多个组别(notices 提示,不报错); 只有「同证件号 + 同小项同组别」才是真重复(conflicts[].duplicate) - 补齐后端缺失但前端/文档已在依赖的三处契约 ## 其余 P1 - 安全:会话撤销断开 ws、实时层按角色裁剪、整库快照限流(含 hello 路径, 被限流的请求挂起并由心跳兜底补发,不丢刷新) - 错误分级:手填名次非正数 / gender / 组别年龄 / 导出不存在的小项与单位 / 积分模板 awardRanks → 400 或 404,不再 500 - 算分一致性:破纪录加分看得失状态;批量退赛·恢复·移出、作废成绩、 报名导入改状态一律无条件重算推榜(不受延迟结算约束,与实时模式逐字节等价) - 配置改动触发重算:通道 / 小项参数 / 积分模板 - 升级自愈:channel_ranks 空而已有已发布成绩时,启动时一次性全量重算 (服务对外监听之前完成,避免升级后奖牌榜全 0) - 示例数据模式下破坏性操作被阻断,不再假成功 - Console:修复「只剩一块屏」后屏幕监听器被永久摘掉 - 大屏模板引擎:嵌套深度与体积安全阀,任何输入都不抛异常 ## 文档 - docs/:使用手册(奖牌开关、奖牌通道、团体名次、枚举列留空口径)、 常见问题(奖牌排查、升级后奖牌榜为 0、历史重放已知限制)、 部署指南、上线检查清单 - 版本 dev_0.1.7 → dev_0.1.8(console/package.json 同步) 自检:服务端 13 包 ×2 全绿;前端 vue-tsc + 449 例 ×2 + pnpm build; Console 35 例 + 语法检查。
This commit is contained in:
1 parent
53a0cb225c
commit
45edfbeb71
115 files changed
+10104
-755
No files matched your search
@@ -1,4 +1,9 @@
|
||||
import { normalizeAvgMode, normalizeCountToTeam } from '@/shared'
|
||||
import {
|
||||
normalizeAvgMode,
|
||||
normalizeCountToMedals,
|
||||
normalizeCountToTeam,
|
||||
normalizeCountToTeamMedals,
|
||||
} from '@/shared'
|
||||
import type {
|
||||
AthleteStanding,
|
||||
BreakdownItem,
|
||||
@@ -142,6 +147,9 @@ export function parseScoreChannel(source: unknown): ScoreChannel {
|
||||
profileId: num(rec.profileId),
|
||||
sortOrder: num(rec.sortOrder),
|
||||
countToTeam: normalizeCountToTeam(rec.countToTeam),
|
||||
// 旧服务端 / 旧快照不下发奖牌开关:缺字段一律按「发牌 / 并入单位榜」处理。
|
||||
countToMedals: normalizeCountToMedals(rec.countToMedals),
|
||||
countToTeamMedals: normalizeCountToTeamMedals(rec.countToTeamMedals),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,6 +166,8 @@ export function parseEvent(source: unknown): Event {
|
||||
compare: oneOf(rec.compare, COMPARE_MODES, 'none'),
|
||||
recordEligible: bool(rec.recordEligible),
|
||||
scored: bool(rec.scored, true),
|
||||
// 旧服务端不下发 awardMedals,按 true 处理:升级前的口径就是「计分的小项发牌」。
|
||||
awardMedals: bool(rec.awardMedals, true),
|
||||
multiplier: num(rec.multiplier, 1),
|
||||
status: oneOf(rec.status, EVENT_STATUSES, 'pending'),
|
||||
scheduledAt: strOrNull(rec.scheduledAt),
|
||||
|
||||
@@ -109,6 +109,8 @@ function buildEvents(): Event[] {
|
||||
compare,
|
||||
recordEligible: index < 3,
|
||||
scored: true,
|
||||
// 示例大屏里每个小项都正常结算奖牌(缺字段时 guards 也按 true 处理)。
|
||||
awardMedals: true,
|
||||
multiplier: 1,
|
||||
status: index < 3 ? 'published' : 'finished',
|
||||
scheduledAt: null,
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
*
|
||||
* 失败口径按设计稿 §3.1 / §9:取不到值 → 空串 + 一条警告,不打断渲染;
|
||||
* 块结构错误 → 返回结构化 error(带行列号),**不抛异常**,由调用方决定要不要降级。
|
||||
* 超过安全阀(嵌套太深 / 体积太大,见 MAX_TEMPLATE_DEPTH、MAX_TEMPLATE_SOURCE_LENGTH)
|
||||
* 也走同一条 error 路径——不能抛,否则 Promise 里的 RangeError 会把整块屏打白。
|
||||
*/
|
||||
|
||||
/** 一条警告的成因。渲染期只产这些码,解析期不产警告。 */
|
||||
@@ -47,6 +49,10 @@ export type TemplateErrorCode =
|
||||
| 'duplicate-else'
|
||||
| 'unknown-block'
|
||||
| 'empty-block-path'
|
||||
/** 嵌套层数超过 MAX_TEMPLATE_DEPTH(安全阀,位置指向超出的那个块标记) */
|
||||
| 'too-deep'
|
||||
/** 单个模板字符串超过 MAX_TEMPLATE_SOURCE_LENGTH(安全阀,位置恒为 1:1) */
|
||||
| 'too-large'
|
||||
|
||||
export interface TemplateError {
|
||||
code: TemplateErrorCode
|
||||
@@ -118,6 +124,32 @@ const LOOP_KEYS = new Set(['@index', '@first', '@last'])
|
||||
|
||||
const BLOCK_KEYWORDS = new Set(['each', 'if'])
|
||||
|
||||
/**
|
||||
* 嵌套层数上限(安全阀)。
|
||||
*
|
||||
* 为什么需要:渲染是递归下降(renderNodes),而模板按设计是**不可信输入**。
|
||||
* 实测(node 22 默认栈)3000 层 {{#if}} 还能跑完,3200 层就抛
|
||||
* `RangeError: Maximum call stack size exceeded`(源码只有约 51 KB,远不到 §6.2 的 1 MiB 上传上限),
|
||||
* 违反「任何情况下都不抛异常」。所以上限钉在**解析期**:AST 深度一旦有界,渲染的递归深度
|
||||
* 自然有界(AST 只能由 parseTemplate 产出)。模板语法里没有 {{> partial}} 之类的 include,
|
||||
* 不存在跨模板的循环引用;数据侧的循环引用也只是沿 AST 一层层走,同样受这里约束。
|
||||
*
|
||||
* 为什么是 128:设计稿 §3.7 的示例模板最深 3 层(#if → #each → #if),人工/社区模板再复杂
|
||||
* 也就是个位数;128 相对崩溃点(约 3100 层)留了 20 倍以上余量——正常模板碰不到,
|
||||
* 极端模板也不会把调用栈打爆。
|
||||
*/
|
||||
export const MAX_TEMPLATE_DEPTH = 128
|
||||
|
||||
/**
|
||||
* 单个模板字符串的长度上限(UTF-16 码元数,即 `source.length`)。
|
||||
*
|
||||
* 设计稿 §6.2 的「单文件 1 MiB」是**上传**侧的口径(服务端 413),引擎这层只做兜底:
|
||||
* 留 4 倍余量,既不顶掉调用方自己的限制(1 MiB 附近的模板照常渲染,见 template-limits.test.ts),
|
||||
* 又能挡住把整个赛事包、几十 MB 字符串误传进来的情形——那会让 tokenize 白跑一遍并撑出巨大的 token 数组。
|
||||
* 超限不解析,直接给 too-large。
|
||||
*/
|
||||
export const MAX_TEMPLATE_SOURCE_LENGTH = 4 * 1024 * 1024
|
||||
|
||||
const ESCAPES: Record<string, string> = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
@@ -213,6 +245,8 @@ const ERROR_TEXT: Record<TemplateErrorCode, string> = {
|
||||
'duplicate-else': '同一个 {{#if}} 块里出现了多个 {{else}}',
|
||||
'unknown-block': '不支持的块标记',
|
||||
'empty-block-path': '块标记缺少路径',
|
||||
'too-deep': '模板嵌套太深',
|
||||
'too-large': '模板太大',
|
||||
}
|
||||
|
||||
/** 同一 (code, path) 只留第一条,重复出现只累加 count:循环里的笔误不该刷满警告清单。 */
|
||||
@@ -395,6 +429,19 @@ function buildAst(
|
||||
continue
|
||||
}
|
||||
if (token.kind === 'open') {
|
||||
// 深度上限在解析期卡死(见 MAX_TEMPLATE_DEPTH):stack 里 root 占一格,
|
||||
// 所以这个新块的层数正好是 stack.length;超限就返回结构化 error,绝不继续往下建。
|
||||
if (stack.length > MAX_TEMPLATE_DEPTH) {
|
||||
return {
|
||||
ast: [],
|
||||
error: makeError(
|
||||
'too-deep',
|
||||
starts,
|
||||
token.start,
|
||||
`最多 ${MAX_TEMPLATE_DEPTH} 层,这里已经是第 ${stack.length} 层`,
|
||||
),
|
||||
}
|
||||
}
|
||||
const node: SectionNode = {
|
||||
type: token.block as 'each' | 'if',
|
||||
path: parsePath(token.rawPath),
|
||||
@@ -493,6 +540,10 @@ function resolvePath(ref: PathRef, stack: Frame[]): { value: unknown; code: Temp
|
||||
return { value: current, code: null }
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归下降渲染。它每下一层块就多一帧栈,所以深度必须靠解析期的 MAX_TEMPLATE_DEPTH 兜住
|
||||
* (AST 只由 parseTemplate 产出,深度必然 ≤ 上限)——这里是唯一会把栈打爆的地方。
|
||||
*/
|
||||
function renderNodes(nodes: TemplateNode[], stack: Frame[], out: string[], sink: WarningCollector): void {
|
||||
for (const node of nodes) {
|
||||
if (node.type === 'text') {
|
||||
@@ -543,6 +594,18 @@ function valueToText(value: unknown): { text: string; code: TemplateWarningCode
|
||||
}
|
||||
|
||||
export function parseTemplate(source: string): ParseResult {
|
||||
// 体积上限先判:病态输入连 buildLineStarts 都不必跑(starts 传 [0] 表示不分行,位置就是 1:1)
|
||||
if (source.length > MAX_TEMPLATE_SOURCE_LENGTH) {
|
||||
return {
|
||||
ast: [],
|
||||
error: makeError(
|
||||
'too-large',
|
||||
[0],
|
||||
0,
|
||||
`${source.length} 个字符,最多 ${MAX_TEMPLATE_SOURCE_LENGTH}`,
|
||||
),
|
||||
}
|
||||
}
|
||||
const starts = buildLineStarts(source)
|
||||
const { tokens, error } = tokenize(source, starts)
|
||||
if (error) return { ast: [], error }
|
||||
@@ -550,7 +613,10 @@ export function parseTemplate(source: string): ParseResult {
|
||||
return buildAst(tokens, starts)
|
||||
}
|
||||
|
||||
/** 渲染。任何情况下都不抛异常:结构错误走返回值里的 error,取不到值走 html 空串 + warnings。 */
|
||||
/**
|
||||
* 渲染。任何情况下都不抛异常:结构错误(含超深/超大)走返回值里的 error,
|
||||
* 取不到值走 html 空串 + warnings。
|
||||
*/
|
||||
export function renderTemplate(source: string, data: unknown = {}): RenderResult {
|
||||
const { ast, error } = parseTemplate(source)
|
||||
if (error) return { html: '', warnings: [], error }
|
||||
|
||||
@@ -21,6 +21,34 @@ test('parseScoreChannel 保留 aggregate 并按 true 补 countToTeam', () => {
|
||||
assert.equal(parseScoreChannel({ basis: 'sum' }).basis, 'result')
|
||||
})
|
||||
|
||||
// 奖牌开关是设计 17 新增的:旧服务端 / 旧快照根本没有这些字段,
|
||||
// 缺字段必须按「开」处理,否则大屏与录入页会因为一次升级集体变成不发牌。
|
||||
test('parseEvent 缺 awardMedals 时按「结算奖牌」处理', () => {
|
||||
assert.equal(parseEvent({ id: 1, code: 'TPULL' }).awardMedals, true)
|
||||
assert.equal(parseEvent({ id: 1, awardMedals: false }).awardMedals, false)
|
||||
assert.equal(parseEvent({ id: 1, awardMedals: true }).awardMedals, true)
|
||||
// 脏值(数字 / 字符串)不能把奖牌发出去:只有显式 false 才关。
|
||||
assert.equal(parseEvent({ id: 1, awardMedals: 0 }).awardMedals, true)
|
||||
})
|
||||
|
||||
test('parseScoreChannel 缺两个奖牌开关时都按 true 补', () => {
|
||||
const legacy = parseScoreChannel({ id: 3, eventId: 1, scope: 'athlete', basis: 'result', profileId: 1 })
|
||||
assert.equal(legacy.countToMedals, true)
|
||||
assert.equal(legacy.countToTeamMedals, true)
|
||||
|
||||
const off = parseScoreChannel({
|
||||
id: 4,
|
||||
eventId: 1,
|
||||
scope: 'athlete',
|
||||
basis: 'result',
|
||||
profileId: 1,
|
||||
countToMedals: false,
|
||||
countToTeamMedals: false,
|
||||
})
|
||||
assert.equal(off.countToMedals, false)
|
||||
assert.equal(off.countToTeamMedals, false)
|
||||
})
|
||||
|
||||
test('parseEvent 同时解析详情接口带出的通道', () => {
|
||||
const event = parseEvent({
|
||||
id: 1,
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { renderTemplate } from '../template'
|
||||
import { MAX_TEMPLATE_DEPTH, MAX_TEMPLATE_SOURCE_LENGTH, renderTemplate } from '../template'
|
||||
|
||||
// 设计稿 §10.1 的「边界/性能」组:单文件上限是 1 MiB(§6.2),引擎不能在这个量级上炸或卡死。
|
||||
// 后面几组钉住审查 [11-1] 要求的安全阀:嵌套太深 / 体积太大一律降级成结构化 error,不抛异常。
|
||||
|
||||
test('1 MiB 量级的模板能正常渲染', () => {
|
||||
const filler = 'x'.repeat(1024)
|
||||
@@ -47,3 +48,76 @@ test('深嵌套 #each/#if(30 层)不炸,作用域逐层正确', () => {
|
||||
assert.equal(result.error, null)
|
||||
assert.equal(result.html, `n${depth - 1}`)
|
||||
})
|
||||
|
||||
test('10 层嵌套 #if 渲染正确,没有警告(人工模板的正常量级)', () => {
|
||||
const depth = 10
|
||||
const source = '{{#if ok}}'.repeat(depth) + '{{ title }}' + '{{/if}}'.repeat(depth)
|
||||
|
||||
const result = renderTemplate(source, { ok: true, title: '十层' })
|
||||
assert.equal(result.error, null)
|
||||
assert.equal(result.html, '十层')
|
||||
assert.deepEqual(result.warnings, [])
|
||||
})
|
||||
|
||||
test(`正好 ${MAX_TEMPLATE_DEPTH} 层照常渲染,多一层降级成 too-deep`, () => {
|
||||
const openTag = '{{#if ok}}'
|
||||
const wrap = (depth: number): string =>
|
||||
openTag.repeat(depth) + 'x' + '{{/if}}'.repeat(depth)
|
||||
|
||||
const atLimit = renderTemplate(wrap(MAX_TEMPLATE_DEPTH), { ok: true })
|
||||
assert.equal(atLimit.error, null)
|
||||
assert.equal(atLimit.html, 'x')
|
||||
|
||||
const overLimit = renderTemplate(wrap(MAX_TEMPLATE_DEPTH + 1), { ok: true })
|
||||
assert.equal(overLimit.error?.code, 'too-deep')
|
||||
// 位置指向超出的那个 {{#if}}(全在一行)
|
||||
assert.equal(overLimit.error?.line, 1)
|
||||
assert.equal(overLimit.error?.offset, MAX_TEMPLATE_DEPTH * openTag.length)
|
||||
assert.equal(overLimit.error?.column, overLimit.error!.offset + 1)
|
||||
assert.ok(overLimit.error?.message.includes(String(MAX_TEMPLATE_DEPTH)))
|
||||
assert.equal(overLimit.html, '')
|
||||
assert.deepEqual(overLimit.warnings, [])
|
||||
})
|
||||
|
||||
test('审查 [11-1] 的复现输入(3000 层 {{#if}},约 48 KB)不抛异常,降级为 too-deep', () => {
|
||||
const depth = 3000
|
||||
const source = '{{#if a}}'.repeat(depth) + 'x' + '{{/if}}'.repeat(depth)
|
||||
|
||||
assert.doesNotThrow(() => renderTemplate(source, { a: true }))
|
||||
|
||||
const result = renderTemplate(source, { a: true })
|
||||
assert.equal(result.error?.code, 'too-deep')
|
||||
assert.equal(result.html, '')
|
||||
assert.deepEqual(result.warnings, [])
|
||||
})
|
||||
|
||||
test('超深 {{#each}} 同样降级(两种块共用同一个层数计数器)', () => {
|
||||
const depth = MAX_TEMPLATE_DEPTH + 1
|
||||
const source = '{{#each rows}}'.repeat(depth) + 'x' + '{{/each}}'.repeat(depth)
|
||||
|
||||
assert.doesNotThrow(() => renderTemplate(source, { rows: [] }))
|
||||
assert.equal(renderTemplate(source, { rows: [] }).error?.code, 'too-deep')
|
||||
})
|
||||
|
||||
test(`体积超过 ${MAX_TEMPLATE_SOURCE_LENGTH}:不解析、不抛异常,降级为 too-large`, () => {
|
||||
const source = `{{ a }}${'x'.repeat(MAX_TEMPLATE_SOURCE_LENGTH)}`
|
||||
|
||||
assert.doesNotThrow(() => renderTemplate(source, { a: 1 }))
|
||||
|
||||
const result = renderTemplate(source, { a: 1 })
|
||||
assert.equal(result.error?.code, 'too-large')
|
||||
assert.equal(result.error?.line, 1)
|
||||
assert.equal(result.error?.column, 1)
|
||||
assert.equal(result.error?.offset, 0)
|
||||
assert.equal(result.html, '')
|
||||
assert.deepEqual(result.warnings, [])
|
||||
})
|
||||
|
||||
test('体积正好等于上限:照常解析(上限是「超过才拒」)', () => {
|
||||
const source = '{{ a }}'.padEnd(MAX_TEMPLATE_SOURCE_LENGTH, 'x')
|
||||
assert.equal(source.length, MAX_TEMPLATE_SOURCE_LENGTH)
|
||||
|
||||
const result = renderTemplate(source, { a: 1 })
|
||||
assert.equal(result.error, null)
|
||||
assert.equal(result.html.startsWith('1'), true)
|
||||
})
|
||||
Reference in new issue
Block a user