pompelmi - 为 Node.js 开发者封装的 ClamAV 文件扫描库
工具简介 pompelmi 是一个极简的 Node.js 开源库(ISC 协议),对 ClamAV 进行了封装。 调用 scan() 扫描任意文件,返回一个类型化的 Symbol 结果: Verdict.Clean、Verdict.Malicious 或 Verdict.ScanError。 无需守护进程,无需云服务,无需原生绑定,零运行时依赖。 工作原理 1. 验证 — 检查参数是否为字符串,文件是否存在 2. 扫描 — 以子进程方式执行 clamscan nosummary <filePath,读取退出码 3. 映射 — 将退出码直接映射为结果 Symbol 不解析 stdout,不用正则,没有意外。 基本用法 js const { scan, Verdict } = require('pompelmi'); const result = await scan('/path/to/upload.zip'); if (result === Verdict.Malicious) { throw new Error('文件被拒绝:检测到恶意软件'); } 扫描结果说明 | 结果 | ClamAV 退出码 | 含义 | ||::|| | Verdict.Clean | 0 | 未发现威胁 | | Verdict.Malicious | 1 | 匹配到已知病毒或恶意软件签名 | | Verdict.ScanError | 2 | 扫描失败(I/O 错误、加密压缩包等),状态未知,建议视为不可信 | 每个 Verdict Symbol 都有 .description 属性,方便日志输出: js console.log(Verdict.Clean.description); // = 'Clean' 错误处理示例 js const { scan, Verdict } = require('pompelmi'); const path = require('path'); async function safeScan(filePath) { try { const result = await scan(path.resolve(filePath)); if (result === Verdict.ScanError) { console.warn('扫描未完成,作为预防措施拒绝该文件。'); return null; } return result; // Verdict.Clean 或 Verdict.Malicious } catch (err) { console.error('扫描失败:', err.message); return null; } } Docker / 远程扫描 如果 ClamAV 运行在 Docker 容器或远程服务器上,只需传入 host 和 port,其他不变: js const result = await pompelmi.scan('/path/to/upload.zip', { host: '127.0.0.1', port: 3310, }); 支持平台 | 系统 | ClamAV 安装命令 | ||| | macOS | brew install clamav && freshclam | | Linux | sudo aptget install y clamav clamavdaemon && sudo freshclam | | Windows | choco install clamav y | 项目信息 GitHub: npm: Stars:585+ 已被 Node Weekly、Stack Overflow Blog、Help Net Security 收录 支持 Express、Fastify、Next.js、NestJS、Koa 等主流框架
Original




















DEV TRENDS / COMMUNITY
Discuss this edition
Sign in with GitHub to share a thought, question, or useful follow-up.