forked from 418sec/clamscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeClamTransform.js
executable file
·43 lines (36 loc) · 1.2 KB
/
NodeClamTransform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* This code was inspired by the clamav.js package by "yongtang"
* https://github.com/yongtang/clamav.js
*/
const { Transform } = require('stream');
class NodeClamTransform extends Transform {
constructor(options, debug_mode=false) {
super(options);
this._streaming = false;
this._num_chunks = 0;
this._total_size = 0;
this._debug_mode = debug_mode;
}
_transform(chunk, encoding, cb) {
if (!this._streaming) {
this.push("zINSTREAM\0");
this._streaming = true;
}
this._total_size += chunk.length;
const size = Buffer.alloc(4);
size.writeInt32BE(chunk.length, 0);
this.push(size);
this.push(chunk);
this._num_chunks++;
// if (this._debug_mode) console.log("node-clam: Transforming for ClamAV...", this._num_chunks, chunk.length, this._total_size);
cb();
}
_flush(cb) {
if (this._debug_mode) console.log("node-clam: Received final data from stream.");
const size = Buffer.alloc(4);
size.writeInt32BE(0, 0);
this.push(size);
cb();
}
}
module.exports = NodeClamTransform;