pompelmi 1.3.0 → 1.4.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.
package/README.md CHANGED
@@ -14,6 +14,7 @@
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
16
  <img src="https://github.com/pompelmi/pompelmi/actions/workflows/ci.yml/badge.svg" alt="Node.js CI">
17
+ <img src="https://github.com/pompelmi/pompelmi/actions/workflows/release.yml/badge.svg" alt="npm publish">
17
18
  </p>
18
19
 
19
20
  ---
@@ -43,6 +44,7 @@ Most integrations require parsing ClamAV's stdout with regex, managing a clamd d
43
44
 
44
45
  - Single `scan(filePath, [options])` function — works locally or against a remote clamd instance
45
46
  - `scanBuffer(buffer, [options])` — scan in-memory Buffers directly, no temp file required in TCP mode
47
+ - `scanStream(stream, [options])` — scan a Readable stream directly. In TCP mode, streamed to clamd with no disk I/O.
46
48
  - Symbol-based verdicts (`Verdict.Clean` / `Verdict.Malicious` / `Verdict.ScanError`) — typo-proof comparisons
47
49
  - Full TCP/clamd support via the INSTREAM protocol with configurable host, port, and timeout
48
50
  - Built-in helpers to install ClamAV and update virus definitions programmatically
@@ -226,6 +228,20 @@ if (result === Verdict.Malicious) throw new Error('Malware detected.');
226
228
  if (result === Verdict.ScanError) console.warn('Scan incomplete.');
227
229
  ```
228
230
 
231
+ ### Scan a Stream
232
+
233
+ ```js
234
+ const { scanStream, Verdict } = require('pompelmi');
235
+ const { Readable } = require('stream');
236
+
237
+ // Useful for S3 getObject, HTTP downloads, or any piped source
238
+ const stream = s3.getObject({ Bucket, Key }).createReadStream();
239
+ const result = await scanStream(stream);
240
+
241
+ if (result === Verdict.Malicious) throw new Error('Malware detected.');
242
+ if (result === Verdict.ScanError) console.warn('Scan incomplete.');
243
+ ```
244
+
229
245
  ---
230
246
 
231
247
  ## Docker / Remote Scanning
@@ -325,6 +341,33 @@ In TCP mode (`host` or `port` provided), the buffer is streamed directly to clam
325
341
 
326
342
  ---
327
343
 
344
+ ### `scanStream(stream, [options])`
345
+
346
+ ```ts
347
+ scanStream(
348
+ stream: Readable,
349
+ options?: { host?: string; port?: number; timeout?: number }
350
+ ): Promise<symbol>
351
+ ```
352
+
353
+ | Parameter | Type | Description |
354
+ |---|---|---|
355
+ | `stream` | `Readable` | Node.js Readable stream to scan |
356
+ | `options` | `object` | Same options as `scan()` — host, port, timeout |
357
+
358
+ **Returns** the same three Symbol verdicts as `scan()`: `Verdict.Clean`, `Verdict.Malicious`, `Verdict.ScanError`.
359
+
360
+ **Rejects** with the same error types as `scan()` where applicable, plus:
361
+
362
+ | Condition | Error message |
363
+ |---|---|
364
+ | `stream` is not a Readable | `stream must be a Readable` |
365
+ | Stream emits error | propagated as-is |
366
+
367
+ In TCP mode (`host` or `port` provided), the stream is piped directly to clamd via the INSTREAM protocol — no data is written to disk. In local mode, the stream is piped to a temp file in `os.tmpdir()` that is deleted automatically in a `finally` block.
368
+
369
+ ---
370
+
328
371
  ### `ClamAVInstaller()` _(internal)_
329
372
 
330
373
  Installs ClamAV using the platform's native package manager. Resolves immediately if ClamAV is already installed.
@@ -388,6 +431,7 @@ The [`examples/`](./examples/) directory contains standalone runnable scripts. E
388
431
  | [`scan-multiple-files.js`](examples/scan-multiple-files.js) | Concurrent scans with `Promise.all` |
389
432
  | [`scan-directory.js`](examples/scan-directory.js) | Recursively scan every file in a directory |
390
433
  | [`scan-buffer.js`](examples/scan-buffer.js) | Scan an in-memory Buffer via a temp-file shim |
434
+ | [`scan-stream.js`](examples/scan-stream.js) | Scan an S3 getObject Readable stream with scanStream() |
391
435
  | [`rest-api-server.js`](examples/rest-api-server.js) | Minimal HTTP server exposing `POST /scan` |
392
436
  | [`s3-scan-before-upload.js`](examples/s3-scan-before-upload.js) | Scan locally, then upload to S3 only if clean |
393
437
  | [`cli-scan.js`](examples/cli-scan.js) | CLI tool: scan file paths, exit non-zero on threats |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pompelmi",
3
- "version": "1.3.0",
3
+ "version": "1.4.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",
@@ -1,10 +1,12 @@
1
- const { nativeSpawn: spawn } = require('./spawn.js');
1
+ const { nativeSpawn: spawn } = require('./spawn.js');
2
2
  const fs = require('fs');
3
3
  const os = require('os');
4
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
+ const { Readable } = require('stream');
6
+ const { SCAN_RESULTS } = require('./config.js');
7
+ const { scanViaClamd } = require('./ClamdScanner.js');
8
+ const { scanBufferViaClamd } = require('./BufferScanner.js');
9
+ const { scanStreamViaClamd } = require('./StreamScanner.js');
8
10
 
9
11
  const MESSAGES = {
10
12
  FILE_NOT_FOUND: (filePath) => `File not found: ${filePath}`,
@@ -62,4 +64,40 @@ async function scanBuffer(buffer, options = {}) {
62
64
  }
63
65
  }
64
66
 
65
- module.exports = { scan, scanBuffer };
67
+ async function scanStream(stream, options = {}) {
68
+ if (!(stream instanceof Readable)) {
69
+ throw new Error('stream must be a Readable');
70
+ }
71
+
72
+ if (options.host !== undefined || options.port !== undefined) {
73
+ return scanStreamViaClamd(stream, options);
74
+ }
75
+
76
+ const tmpPath = path.join(
77
+ os.tmpdir(),
78
+ `pompelmi-${Date.now()}-${Math.random().toString(36).slice(2)}`
79
+ );
80
+
81
+ await new Promise((resolve, reject) => {
82
+ let settled = false;
83
+ function settle(err) {
84
+ if (settled) return;
85
+ settled = true;
86
+ if (err) reject(err);
87
+ else resolve();
88
+ }
89
+ const writable = fs.createWriteStream(tmpPath);
90
+ stream.on('error', settle);
91
+ writable.on('error', settle);
92
+ writable.on('finish', () => settle(null));
93
+ stream.pipe(writable);
94
+ });
95
+
96
+ try {
97
+ return await scan(tmpPath);
98
+ } finally {
99
+ fs.unlink(tmpPath, () => {});
100
+ }
101
+ }
102
+
103
+ module.exports = { scan, scanBuffer, scanStream };
@@ -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
+
8
+ function parseClamdResponse(raw) {
9
+ const text = raw.toString('utf8').trim();
10
+ if (text === 'stream: OK') return Verdict.Clean;
11
+ if (text.endsWith(' FOUND')) return Verdict.Malicious;
12
+ return Verdict.ScanError;
13
+ }
14
+
15
+ /**
16
+ * Scan a Readable stream by piping it to a running clamd instance over TCP.
17
+ * No data is written to disk.
18
+ *
19
+ * @param {import('stream').Readable} stream
20
+ * @param {object} [options]
21
+ * @param {string} [options.host='127.0.0.1']
22
+ * @param {number} [options.port=3310]
23
+ * @param {number} [options.timeout=15000]
24
+ * @returns {Promise<symbol>}
25
+ */
26
+ function scanStreamViaClamd(stream, { host = '127.0.0.1', port = 3310, timeout = 15_000 } = {}) {
27
+ return new Promise((resolve, reject) => {
28
+ const socket = net.createConnection({ host, port });
29
+ const chunks = [];
30
+ let settled = false;
31
+
32
+ function settle(fn, value) {
33
+ if (settled) return;
34
+ settled = true;
35
+ socket.destroy();
36
+ fn(value);
37
+ }
38
+
39
+ socket.setTimeout(timeout);
40
+ socket.on('timeout', () =>
41
+ settle(reject, new Error(`clamd connection timed out after ${timeout}ms`))
42
+ );
43
+ socket.on('error', (err) => settle(reject, err));
44
+ socket.on('data', (chunk) => chunks.push(chunk));
45
+ socket.on('end', () => settle(resolve, parseClamdResponse(Buffer.concat(chunks))));
46
+
47
+ socket.on('connect', () => {
48
+ socket.write(CLAMD_INSTREAM);
49
+
50
+ stream.on('error', (err) => settle(reject, err));
51
+
52
+ stream.on('data', (chunk) => {
53
+ const header = Buffer.allocUnsafe(4);
54
+ header.writeUInt32BE(chunk.length, 0);
55
+ socket.write(header);
56
+ socket.write(chunk);
57
+ });
58
+
59
+ stream.on('end', () => {
60
+ socket.write(Buffer.alloc(4)); // terminating zero-length chunk
61
+ socket.end();
62
+ });
63
+ });
64
+ });
65
+ }
66
+
67
+ module.exports = { scanStreamViaClamd };
package/src/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const { scan, scanBuffer } = require('./ClamAVScanner.js');
2
- const { Verdict } = require('./verdicts.js');
1
+ const { scan, scanBuffer, scanStream } = require('./ClamAVScanner.js');
2
+ const { Verdict } = require('./verdicts.js');
3
3
 
4
- module.exports = { scan, scanBuffer, Verdict };
4
+ module.exports = { scan, scanBuffer, scanStream, Verdict };
@@ -1,52 +0,0 @@
1
- {
2
- "permissions": {
3
- "allow": [
4
- "Bash(node:*)",
5
- "Bash(echo \"EXIT:$?\")",
6
- "Bash(echo \"EXIT_CODE:$?\")",
7
- "Bash(tee /tmp/pompelmi_test_out.txt)",
8
- "Bash(echo \"done: $?\")",
9
- "Bash(ls /Users/tommy/pompelmi/pompelmi/*.md)",
10
- "Bash(ls /Users/tommy/pompelmi/pompelmi/LICENSE*)",
11
- "WebSearch",
12
- "WebFetch(domain:pompelmi.app)",
13
- "WebFetch(domain:news.ycombinator.com)",
14
- "WebFetch(domain:dev.to)",
15
- "WebFetch(domain:socket.dev)",
16
- "WebFetch(domain:helpnetsecurity.com)",
17
- "WebFetch(domain:nodeweekly.com)",
18
- "WebFetch(domain:bytes.dev)",
19
- "WebFetch(domain:www.helpnetsecurity.com)",
20
- "WebFetch(domain:www.enveil.com)",
21
- "WebFetch(domain:img.helpnetsecurity.com)",
22
- "WebFetch(domain:logo.clearbit.com)",
23
- "WebFetch(domain:cdn.brandfetch.io)",
24
- "WebFetch(domain:cooperpress.com)",
25
- "WebFetch(domain:wirexsystems.com)",
26
- "WebFetch(domain:www.zlti.com)",
27
- "Bash(gh run:*)",
28
- "Bash(gh api:*)",
29
- "WebFetch(domain:api.github.com)",
30
- "WebFetch(domain:raw.githubusercontent.com)",
31
- "Bash(git -C /Users/tommy/pompelmi/pompelmi status)",
32
- "Bash(git -C /Users/tommy/pompelmi/pompelmi log --oneline -5)",
33
- "Bash(git pull:*)",
34
- "Bash(git add:*)",
35
- "Bash(git commit -m ':*)",
36
- "Bash(git push:*)",
37
- "Bash(grep -E '\\\\.\\(png|svg|ico|webp\\)$')",
38
- "Bash(ls -d /Users/tommy/pompelmi/pompelmi/*/)",
39
- "Bash(node --test test/unit.test.js)",
40
- "Bash(echo \"exit:$?\")",
41
- "Read(//tmp/**)",
42
- "Bash(tee /Users/tommy/pompelmi/pompelmi/test_out.txt)",
43
- "Bash(npm install:*)",
44
- "Bash(npm ls:*)",
45
- "Bash(wait)",
46
- "Bash(git rebase *)",
47
- "Bash(python3 *)",
48
- "Bash(git *)",
49
- "Bash(npm test *)"
50
- ]
51
- }
52
- }