Files
nodeagent/packages/protocol/src/ulid.ts
T
jkooe 77c024703a feat: nodeagent v1 初期开发完成
- packages/protocol: 协议定义(信封/方法/错误码/能力schema/校验/HMAC/ULID)
- packages/client: 控制端客户端库(连接/握手/invoke/超时)
- apps/agent: Windows 被控端(监听/HMAC认证/能力分发/7项能力/进程执行)
- apps/cli: 控制端 CLI(connect/info/status/ps/services/exec/apps/install/list/invoke)
- apps/mcp: MCP server(na_* 工具,接入 WorkBuddy)
- scripts: install.ps1 / uninstall.ps1(开机自启 + 防火墙 + 密钥生成)
- tests/e2e: 端到端测试 13/13 通过(握手/能力/超时强杀/参数校验/鉴权失败/未认证)
2026-09-27 01:06:35 +08:00

36 lines
946 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* ULID 生成器(Crockford Base32,26 字符:10 位时间戳 + 16 位随机)。
* 自实现以避免运行时依赖。
*/
const ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ';
let lastTime = 0;
let lastRand: number[] = [];
/** 生成一个 ULID(单调递增:同一毫秒内保证字典序递增)。 */
export function ulid(now: number = Date.now()): string {
let timePart = '';
let t = now;
for (let i = 9; i >= 0; i--) {
timePart = ENCODING[t % 32]! + timePart;
t = Math.floor(t / 32);
}
// 随机部分:同毫秒内递增,保证唯一且有序
if (now === lastTime) {
for (let i = 15; i >= 0; i--) {
if (lastRand[i]! < 31) {
lastRand[i]! += 1;
break;
}
lastRand[i] = 0;
}
} else {
lastTime = now;
lastRand = Array.from({ length: 16 }, () => Math.floor(Math.random() * 32));
}
return timePart + lastRand.map((n) => ENCODING[n]!).join('');
}