pompelmi 1.2.4 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -45,7 +45,8 @@
45
45
  "Bash(wait)",
46
46
  "Bash(git rebase *)",
47
47
  "Bash(python3 *)",
48
- "Bash(git *)"
48
+ "Bash(git *)",
49
+ "Bash(npm test *)"
49
50
  ]
50
51
  }
51
52
  }
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  <img src="./src/grapefruit.png" width="88" alt="pompelmi logo">
3
3
  </p>
4
4
 
5
- # pompelmi — ClamAV Antivirus Scanning for Node.js
5
+ <h1 align="center">pompelmi — ClamAV Antivirus Scanning for Node.js</h1>
6
6
 
7
7
  <p align="center"><strong>ClamAV antivirus scanning for Node.js — clean, typed, zero dependencies.</strong></p>
8
8
 
@@ -13,10 +13,9 @@
13
13
  <img src="https://img.shields.io/badge/docker-available-blue?logo=docker" alt="Docker available">
14
14
  <img src="https://img.shields.io/badge/license-ISC-blue.svg" alt="license">
15
15
  <img src="https://img.shields.io/badge/dependencies-0-brightgreen" alt="zero dependencies">
16
+ <img src="https://github.com/pompelmi/pompelmi/actions/workflows/ci.yml/badge.svg" alt="Node.js CI">
16
17
  </p>
17
18
 
18
- ![Node.js CI](https://github.com/pompelmi/pompelmi/actions/workflows/ci.yml/badge.svg)
19
-
20
19
  ---
21
20
 
22
21
  ## Overview
@@ -43,6 +42,7 @@ Most integrations require parsing ClamAV's stdout with regex, managing a clamd d
43
42
  ## Features
44
43
 
45
44
  - Single `scan(filePath, [options])` function — works locally or against a remote clamd instance
45
+ - `scanBuffer(buffer, [options])` — scan in-memory Buffers directly, no temp file required in TCP mode
46
46
  - Symbol-based verdicts (`Verdict.Clean` / `Verdict.Malicious` / `Verdict.ScanError`) — typo-proof comparisons
47
47
  - Full TCP/clamd support via the INSTREAM protocol with configurable host, port, and timeout
48
48
  - Built-in helpers to install ClamAV and update virus definitions programmatically
@@ -214,6 +214,18 @@ const files = ['/uploads/a.pdf', '/uploads/b.zip', '/uploads/c.png'];
214
214
  const results = await Promise.all(files.map((f) => scan(f)));
215
215
  ```
216
216
 
217
+ ### Scan a Buffer
218
+
219
+ ```js
220
+ const { scanBuffer, Verdict } = require('pompelmi');
221
+
222
+ // Useful with multer memoryStorage or any in-memory upload
223
+ const result = await scanBuffer(req.file.buffer);
224
+
225
+ if (result === Verdict.Malicious) throw new Error('Malware detected.');
226
+ if (result === Verdict.ScanError) console.warn('Scan incomplete.');
227
+ ```
228
+
217
229
  ---
218
230
 
219
231
  ## Docker / Remote Scanning
@@ -286,6 +298,33 @@ Verdict.ScanError.description // 'ScanError'
286
298
 
287
299
  ---
288
300
 
301
+ ### `scanBuffer(buffer, [options])`
302
+
303
+ ```ts
304
+ scanBuffer(
305
+ buffer: Buffer,
306
+ options?: { host?: string; port?: number; timeout?: number }
307
+ ): Promise<symbol>
308
+ ```
309
+
310
+ | Parameter | Type | Description |
311
+ |---|---|---|
312
+ | `buffer` | `Buffer` | The in-memory buffer to scan |
313
+ | `options` | `object` | Same options as `scan()` — host, port, timeout |
314
+
315
+ **Returns** the same three Symbol verdicts as `scan()`: `Verdict.Clean`, `Verdict.Malicious`, `Verdict.ScanError`.
316
+
317
+ **Rejects** with the same error types as `scan()` where applicable, plus:
318
+
319
+ | Condition | Error message |
320
+ |---|---|
321
+ | `buffer` is not a Buffer | `buffer must be a Buffer` |
322
+ | `buffer` is empty | `buffer is empty` |
323
+
324
+ In TCP mode (`host` or `port` provided), the buffer is streamed directly to clamd via the INSTREAM protocol — no data is written to disk. In local mode, a temp file is written to `os.tmpdir()` and deleted automatically in a `finally` block regardless of outcome.
325
+
326
+ ---
327
+
289
328
  ### `ClamAVInstaller()` _(internal)_
290
329
 
291
330
  Installs ClamAV using the platform's native package manager. Resolves immediately if ClamAV is already installed.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pompelmi",
3
- "version": "1.2.4",
3
+ "version": "1.3.0",
4
4
  "description": "ClamAV for humans — scan any file and get back Clean, Malicious, or ScanError. No daemons. No cloud. No native bindings.",
5
5
  "license": "ISC",
6
6
  "author": "pompelmi contributors",
@@ -0,0 +1,67 @@
1
+ 'use strict';
2
+
3
+ const net = require('net');
4
+ const { Verdict } = require('./verdicts.js');
5
+
6
+ const CLAMD_INSTREAM = Buffer.from('zINSTREAM\0');
7
+ const CHUNK_SIZE = 64 * 1024;
8
+
9
+ function parseClamdResponse(raw) {
10
+ const text = raw.toString('utf8').trim();
11
+ if (text === 'stream: OK') return Verdict.Clean;
12
+ if (text.endsWith(' FOUND')) return Verdict.Malicious;
13
+ return Verdict.ScanError;
14
+ }
15
+
16
+ /**
17
+ * Scan an in-memory Buffer by streaming it to a running clamd instance over TCP.
18
+ * No data is written to disk.
19
+ *
20
+ * @param {Buffer} buffer
21
+ * @param {object} [options]
22
+ * @param {string} [options.host='127.0.0.1']
23
+ * @param {number} [options.port=3310]
24
+ * @param {number} [options.timeout=15000]
25
+ * @returns {Promise<symbol>}
26
+ */
27
+ function scanBufferViaClamd(buffer, { host = '127.0.0.1', port = 3310, timeout = 15_000 } = {}) {
28
+ return new Promise((resolve, reject) => {
29
+ const socket = net.createConnection({ host, port });
30
+ const chunks = [];
31
+ let settled = false;
32
+
33
+ function settle(fn, value) {
34
+ if (settled) return;
35
+ settled = true;
36
+ socket.destroy();
37
+ fn(value);
38
+ }
39
+
40
+ socket.setTimeout(timeout);
41
+ socket.on('timeout', () =>
42
+ settle(reject, new Error(`clamd connection timed out after ${timeout}ms`))
43
+ );
44
+ socket.on('error', (err) => settle(reject, err));
45
+ socket.on('data', (chunk) => chunks.push(chunk));
46
+ socket.on('end', () => settle(resolve, parseClamdResponse(Buffer.concat(chunks))));
47
+
48
+ socket.on('connect', () => {
49
+ socket.write(CLAMD_INSTREAM);
50
+
51
+ let offset = 0;
52
+ while (offset < buffer.length) {
53
+ const chunk = buffer.slice(offset, offset + CHUNK_SIZE);
54
+ const header = Buffer.allocUnsafe(4);
55
+ header.writeUInt32BE(chunk.length, 0);
56
+ socket.write(header);
57
+ socket.write(chunk);
58
+ offset += chunk.length;
59
+ }
60
+
61
+ socket.write(Buffer.alloc(4)); // terminating zero-length chunk
62
+ socket.end();
63
+ });
64
+ });
65
+ }
66
+
67
+ module.exports = { scanBufferViaClamd };
@@ -1,7 +1,10 @@
1
1
  const { nativeSpawn: spawn } = require('./spawn.js');
2
- const fs = require("fs");
3
- const { SCAN_RESULTS } = require('./config.js');
4
- const { scanViaClamd } = require('./ClamdScanner.js');
2
+ const fs = require('fs');
3
+ const os = require('os');
4
+ const path = require('path');
5
+ const { SCAN_RESULTS } = require('./config.js');
6
+ const { scanViaClamd } = require('./ClamdScanner.js');
7
+ const { scanBufferViaClamd } = require('./BufferScanner.js');
5
8
 
6
9
  const MESSAGES = {
7
10
  FILE_NOT_FOUND: (filePath) => `File not found: ${filePath}`,
@@ -34,4 +37,29 @@ function scan(filePath, options = {}) {
34
37
  });
35
38
  }
36
39
 
37
- module.exports = { scan };
40
+ async function scanBuffer(buffer, options = {}) {
41
+ if (!Buffer.isBuffer(buffer)) {
42
+ throw new Error('buffer must be a Buffer');
43
+ }
44
+ if (buffer.length === 0) {
45
+ throw new Error('buffer is empty');
46
+ }
47
+
48
+ if (options.host !== undefined || options.port !== undefined) {
49
+ return scanBufferViaClamd(buffer, options);
50
+ }
51
+
52
+ const tmpPath = path.join(
53
+ os.tmpdir(),
54
+ `pompelmi-${Date.now()}-${Math.random().toString(36).slice(2)}`
55
+ );
56
+
57
+ fs.writeFileSync(tmpPath, buffer);
58
+ try {
59
+ return await scan(tmpPath);
60
+ } finally {
61
+ fs.unlink(tmpPath, () => {});
62
+ }
63
+ }
64
+
65
+ module.exports = { scan, scanBuffer };
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const { scan } = require('./ClamAVScanner.js');
2
- const { Verdict } = require('./verdicts.js');
1
+ const { scan, scanBuffer } = require('./ClamAVScanner.js');
2
+ const { Verdict } = require('./verdicts.js');
3
3
 
4
- module.exports = { scan, Verdict };
4
+ module.exports = { scan, scanBuffer, Verdict };