示例工程 31-notifications-taskbar

31-notifications-taskbar/app/.tsc/shared/redact.js

javascript · 1.3 KB

function apply(text, pattern, replacement, label, hits) {
    return text.replace(pattern, (...args) => {
        hits.add(label);
        return typeof replacement === "function"
            ? replacement(...args)
            : replacement;
    });
}
export function redactText(input) {
    const hits = new Set();
    let text = input;
    text = apply(text, /\b[\w.+-]+@[\w.-]+\.[A-Za-z]{2,}\b/g, "<email>", "邮箱", hits);
    text = apply(text, /(\/Users\/|\/home\/|C:\\Users\\)[^\\/\s]+/g, (_match, prefix) => `${String(prefix)}<user>`, "用户路径", hits);
    text = apply(text, /\bsk-[A-Za-z0-9_-]{8,}\b/g, "sk-***", "API Key", hits);
    text = apply(text, /\bBearer\s+[A-Za-z0-9._-]{8,}/gi, "Bearer ***", "Bearer Token", hits);
    text = apply(text, /([?&](?:token|key|secret|password)=)[^&\s]+/gi, (_match, prefix) => `${String(prefix)}***`, "查询参数密钥", hits);
    text = apply(text, /\b[a-f0-9]{32,}\b/gi, "<hex-token>", "长十六进制串", hits);
    return { text, hits: [...hits] };
}
export function redactPayload(title, body) {
    const titleResult = redactText(title);
    const bodyResult = redactText(body);
    return {
        title: titleResult.text,
        body: bodyResult.text,
        hits: [...new Set([...titleResult.hits, ...bodyResult.hits])],
    };
}
//# sourceMappingURL=redact.js.map