Use passthrough() to scan data while piping
masterThe .passthrough() method returns a Transform stream. This allows you to pipe a ReadableStream through the scanner and into an output destination (like a file or S3) simultaneously.
How it works:
- Data is forked to ClamAV via TCP or Domain Sockets.
- A chunk is only passed to the next output destination if it was successfully sent to and received by ClamAV.
- The returned
Transformstream emits a special'scan-complete'event when ClamAV finishes scanning the stream, providing the scan results.
Example usage:
const av = clamscan.passthrough();
input.pipe(av).pipe(output);
av.on('scan-complete', result => {
const {isInfected, viruses} = result;
// Handle results
});const NodeClam = require('clamscan');
const axios = require('axios');
const fs = require('fs');
const clamscan = new NodeClam().init({
clamdscan: {
socket: '/var/run/clamd.scan/clamd.sock',
host: '127.0.0.1',
port: 3310,
}
});
const input = axios.get(someUrl);
const output = fs.createWriteStream(someLocalFile);
const av = clamscan.passthrough();
input.pipe(av).pipe(output);
av.on('scan-complete', result => {
const {isInfected, viruses} = result;
});