feat: relay-key 共用模块(三桥 + homeagent)

Sha256 clamp relay_key 过 160 字节上限,避免服务端 400
被 worker 当暂时失败让位,导致邮件驱动会话无本地 UI 静默挂死。

新增 relay_key.go / relay-key.js + 15 个纯函数测试。
This commit is contained in:
2026-09-06 15:16:34 +08:00
parent a101c2fada
commit 13fcb00acc
9 changed files with 1338 additions and 0 deletions

View File

@ -0,0 +1,20 @@
/** 服务端 relay_key 列宽(字节)。 */
export const RELAY_KEY_MAX_BYTES: number;
/** UTF-8 字节数。 */
export function byteLength(s: string | null | undefined): number;
/** 按字节截断,回退到最近的字符边界(不产生半个字符)。 */
export function truncateToBytes(s: string | null | undefined, maxBytes: number): string;
/**
* 把 relay_key 收敛到服务端能接受的长度。
* 未超限时原样返回;超限时保留可读前缀 + `:sha256:<原始键的完整哈希>`。
*/
export function clampRelayKey(key: string | null | undefined, limit?: number): string;
/**
* 这次失败是不是「永远不会成功」。
* 4xx(除 408 / 429)= 永久;408 / 429 / 5xx / 无 status = 暂时。
*/
export function isPermanentFailure(err: { status?: number | string } | null | undefined): boolean;