2025-08-16 20:37:58 +00:00
# @push.rocks/smartantivirus 🛡️
2025-01-10 03:09:32 +01:00
2025-08-16 20:37:58 +00:00
**Enterprise-grade antivirus scanning for Node.js applications** - Seamlessly integrate ClamAV's powerful virus detection into your TypeScript/JavaScript projects with zero hassle.
2025-01-10 03:09:32 +01:00
2025-08-16 20:37:58 +00:00
## Why SmartAntivirus? 🚀
In today's digital landscape, security is paramount. Whether you're building a file-sharing platform, processing user uploads, or handling sensitive data streams, you need reliable virus protection that just works. SmartAntivirus gives you:
- **🐳 Docker-based or Direct Connection** - Choose your deployment style
- **⚡ In-memory Scanning** - Lightning-fast scanning without disk I/O
- **🌊 Stream Processing** - Scan data on-the-fly as it flows through your app
- **🎯 TypeScript First** - Full type safety and IntelliSense support
- **📦 Zero Config** - Works out of the box with sensible defaults
- **🔄 Auto-updating** - Virus definitions stay current automatically
2025-01-10 03:09:32 +01:00
2025-08-16 20:37:58 +00:00
## Install
2025-01-10 03:25:22 +01:00
```bash
npm install @push .rocks/smartantivirus
```
2025-08-16 20:37:58 +00:00
Or if you're using pnpm (recommended):
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
```bash
pnpm add @push .rocks/smartantivirus
```
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
## Quick Start 🏃♂️
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
### The 5-Minute Setup
2025-02-03 13:55:15 +01:00
```typescript
2025-08-16 20:37:58 +00:00
import { ClamAvService } from '@push .rocks/smartantivirus';
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
// That's it! The service automatically manages a Docker container
const scanner = new ClamAvService();
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
// Scan a suspicious string
const result = await scanner.scanString('Is this text safe?');
console.log(result.isInfected ? '⚠️ Threat detected!' : '✅ All clear!');
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
// Scan a buffer
const fileBuffer = await fs.readFile('./upload.pdf');
const scanResult = await scanner.scanBuffer(fileBuffer);
```
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
## Core Concepts 💡
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
SmartAntivirus provides two main classes:
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
### 🎯 ClamAvService
The high-level interface for virus scanning. It handles all the complexity behind a simple, intuitive API.
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
### 🐳 ClamAVManager
Low-level Docker container management for advanced use cases. Most users won't need to interact with this directly.
2025-02-07 20:47:30 +01:00
2025-08-16 20:37:58 +00:00
## Real-World Examples 🌍
2025-02-07 20:47:30 +01:00
2025-08-16 20:37:58 +00:00
### Protecting File Uploads
2025-01-10 03:25:22 +01:00
```typescript
import { ClamAvService } from '@push .rocks/smartantivirus';
2025-08-16 20:37:58 +00:00
import express from 'express';
import multer from 'multer';
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
const app = express();
const scanner = new ClamAvService();
const upload = multer({ storage: multer.memoryStorage() });
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
app.post('/upload', upload.single('file'), async (req, res) => {
try {
// Scan the uploaded file buffer
const result = await scanner.scanBuffer(req.file.buffer);
if (result.isInfected) {
return res.status(400).json({
error: 'File rejected',
threat: result.reason
});
}
// File is safe, proceed with storage
await saveFile(req.file);
res.json({ message: 'File uploaded successfully' });
} catch (error) {
res.status(500).json({ error: 'Scan failed' });
}
});
2025-01-10 03:25:22 +01:00
```
2025-08-16 20:37:58 +00:00
### Streaming Large Files
2025-02-05 10:49:46 +01:00
2025-08-16 20:37:58 +00:00
Never load huge files into memory! Stream them instead:
2025-02-05 10:49:46 +01:00
```typescript
import { ClamAvService } from '@push .rocks/smartantivirus';
import { createReadStream } from 'fs';
2025-08-16 20:37:58 +00:00
const scanner = new ClamAvService();
async function scanLargeFile(filePath: string) {
const stream = createReadStream(filePath);
const result = await scanner.scanStream(stream);
if (result.isInfected) {
console.log(`🚨 Threat found: ${result.reason}` );
// Quarantine or delete the file
} else {
console.log('✅ File is clean');
}
2025-02-05 10:49:46 +01:00
}
```
2025-08-16 20:37:58 +00:00
### Scanning Remote Content
Perfect for proxies, CDNs, or content moderation:
2025-02-07 20:47:30 +01:00
```typescript
2025-08-16 20:37:58 +00:00
const scanner = new ClamAvService();
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
// Scan a file from a URL
const result = await scanner.scanFileFromWebAsStream('https://example.com/document.pdf');
// For browser environments using Web Streams API
async function scanInBrowser(url: string) {
2025-02-07 20:47:30 +01:00
const response = await fetch(url);
const webStream = response.body as ReadableStream< Uint8Array > ;
2025-08-16 20:37:58 +00:00
2025-02-07 20:47:30 +01:00
if (webStream) {
2025-08-16 20:37:58 +00:00
const result = await scanner.scanWebStream(webStream);
return result;
2025-02-07 20:47:30 +01:00
}
}
```
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
### Advanced Container Management
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
For production environments requiring fine-grained control:
2025-01-10 03:25:22 +01:00
```typescript
2025-08-16 20:37:58 +00:00
import { ClamAVManager } from '@push .rocks/smartantivirus';
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
class AntivirusService {
private manager: ClamAVManager;
async initialize() {
this.manager = new ClamAVManager();
// Start the container
await this.manager.startContainer();
// Set up log monitoring
this.manager.on('log', (event) => {
if (event.type === 'error') {
console.error(`ClamAV Error: ${event.message}` );
// Send to your logging service
}
});
// Update virus definitions
await this.manager.updateDatabase();
// Get database info
const dbInfo = await this.manager.getDatabaseInfo();
console.log(`Virus DB Version: ${dbInfo}` );
}
async shutdown() {
await this.manager.stopContainer();
2025-01-10 03:25:22 +01:00
}
}
```
2025-08-16 20:37:58 +00:00
## Testing 🧪
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
We use the industry-standard EICAR test string for verification:
2025-01-10 03:25:22 +01:00
```typescript
2025-08-16 20:37:58 +00:00
import { ClamAvService } from '@push .rocks/smartantivirus';
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
const scanner = new ClamAvService();
2025-02-07 20:47:30 +01:00
2025-08-16 20:37:58 +00:00
// This is the EICAR test string - it's harmless but triggers antivirus
const EICAR = 'X5O!P%@AP [4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*';
2025-02-07 20:47:30 +01:00
2025-08-16 20:37:58 +00:00
const result = await scanner.scanString(EICAR);
console.log(result.isInfected); // true
console.log(result.reason); // 'Eicar-Test-Signature'
```
2025-02-07 20:47:30 +01:00
2025-08-16 20:37:58 +00:00
Run the test suite:
2025-01-10 03:25:22 +01:00
2025-08-16 20:37:58 +00:00
```bash
npm test
2025-01-10 03:25:22 +01:00
```
2025-08-16 20:37:58 +00:00
## API Reference 📚
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
### ClamAvService
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
#### Constructor
2025-02-03 13:55:15 +01:00
```typescript
2025-08-16 20:37:58 +00:00
new ClamAvService(host?: string, port?: number)
2025-02-03 13:55:15 +01:00
```
2025-08-16 20:37:58 +00:00
- `host` - ClamAV daemon host (default: '127.0.0.1')
- `port` - ClamAV daemon port (default: 3310)
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
#### Methods
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
##### `scanString(text: string): Promise<ScanResult>`
Scan a text string for threats.
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
##### `scanBuffer(buffer: Buffer): Promise<ScanResult>`
Scan binary data in a Buffer.
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
##### `scanStream(stream: NodeJS.ReadableStream): Promise<ScanResult>`
Scan a Node.js readable stream.
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
##### `scanWebStream(stream: ReadableStream<Uint8Array>): Promise<ScanResult>`
Scan a Web Streams API stream (browser-compatible).
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
##### `scanFileFromWebAsStream(url: string): Promise<ScanResult>`
Download and scan a file from a URL.
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
##### `verifyConnection(): Promise<boolean>`
Test the connection to ClamAV daemon.
2025-02-03 13:55:15 +01:00
2025-08-16 20:37:58 +00:00
#### ScanResult Type
```typescript
interface ScanResult {
isInfected: boolean;
reason?: string; // Threat name if infected
}
2025-02-03 13:55:15 +01:00
```
2025-08-16 20:37:58 +00:00
### ClamAVManager
2025-02-03 13:56:42 +01:00
2025-08-16 20:37:58 +00:00
Advanced container management for production deployments:
2025-02-03 13:56:42 +01:00
2025-08-16 20:37:58 +00:00
- `startContainer()` - Launch ClamAV in Docker
- `stopContainer()` - Gracefully shutdown
- `updateDatabase()` - Update virus definitions
- `getDatabaseInfo()` - Get current DB version
- `getLogs()` - Retrieve container logs
- Event: `'log'` - Real-time log streaming
## Production Considerations 🏭
### Performance Tips
1. **Reuse connections** - Create one `ClamAvService` instance and reuse it
2. **Stream large files** - Don't load them into memory
3. **Implement timeouts** - Protect against hanging scans
4. **Monitor logs** - Watch for database update failures
### Security Best Practices
- Run ClamAV container with limited resources
- Implement rate limiting on scan endpoints
- Log all detected threats for audit trails
- Regularly update virus definitions
- Use separate containers for different environments
### Deployment Options
#### Docker Compose
```yaml
services:
clamav:
image: clamav/clamav:latest
ports:
- "3310:3310"
volumes:
- clamav-db:/var/lib/clamav
2025-02-07 20:47:30 +01:00
```
2025-02-03 13:56:42 +01:00
2025-08-16 20:37:58 +00:00
#### Kubernetes
The service automatically manages containers, but you can also deploy ClamAV separately and connect directly to the daemon.
## Troubleshooting 🔧
### Common Issues
**Container won't start**
- Ensure Docker is running
- Check port 3310 isn't already in use
- Verify sufficient disk space for virus definitions
**Scans timing out**
- Large files may take time - implement appropriate timeouts
- Check container resources (CPU/Memory)
- Ensure virus database is not updating
**False positives**
- Some packers/obfuscators trigger detection
- Whitelist known-safe patterns if needed
- Keep virus definitions updated
## Contributing & Support 🤝
- 🐛 [Report Issues ](https://code.foss.global/push.rocks/smartantivirus/issues )
- 📖 [Documentation ](https://code.foss.global/push.rocks/smartantivirus )
- 💬 [Discussions ](https://code.foss.global/push.rocks/smartantivirus/issues )
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license ](license ) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2025-02-03 13:56:42 +01:00
2025-08-16 20:37:58 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task .vc.
2025-02-03 13:56:42 +01:00
2025-08-16 20:37:58 +00:00
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.