llm-router

工具模块 (006) 功能规格

模块路径: src/utils/ 生成时间: 2026-05-11

概述

工具模块提供无状态的通用工具函数,包括日志脱敏、API 密钥生成、请求体处理、流式响应处理和响应录制。

功能需求

FR-UTL-001: Authorization 头脱敏

FR-UTL-002: API 密钥生成

FR-UTL-003: 请求体处理

FR-UTL-004: 流式响应处理

FR-UTL-005: 响应录制

关键导出

函数 签名 说明
redactAuthorization (auth?: string) => string Authorization 头脱敏
generateStrongAPIKey () => string 生成随机 API 密钥
drainBody (stream: Readable) => Promise<Buffer> 读取完整请求体
recordResponse (res: IncomingMessage) => Promise<RecordedResponse> 录制响应

与 Go 版本对照

Go 版本 TypeScript 版本 说明
utils.RedactAuthorization() redactAuthorization() 脱敏函数
utils.GenerateStrongAPIKey() generateStrongAPIKey() 密钥生成
utils.DrainBody() drainBody() 请求体读取
utils.DrainAndCapture() drainAndCapture() 读取并捕获
utils.ResponseRecorder ResponseRecorder class 响应录制
utils.LogRequestResponse() logRequestResponse() 请求/响应日志
crypto/rand crypto.randomBytes 密码学安全随机