Comparing version 2.2.3 to 2.3.0
31
API.md
@@ -17,2 +17,11 @@ ## Classes | ||
## Functions | ||
<dl> | ||
<dt><a href="#getFiles">getFiles(dir, [recursive])</a> ⇒ <code>Array</code></dt> | ||
<dd><p>Gets a listing of all files (no directories) within a given path. | ||
By default, it will retrieve files recursively.</p> | ||
</dd> | ||
</dl> | ||
<a name="NodeClam"></a> | ||
@@ -338,7 +347,7 @@ | ||
// Callback Method | ||
clamscan.scanDir('/some/path/to/scan', (err, goodFiles, badFiles, viruses) { | ||
clamscan.scanDir('/some/path/to/scan', (err, goodFiles, badFiles, viruses, numGoodFiles) { | ||
if (err) return console.error(err); | ||
if (badFiles.length > 0) { | ||
console.log(`${path} was infected. The offending files (${badFiles.join (', ')}) have been quarantined.`); | ||
console.log(`${path} was infected. The offending files (${badFiles.map(v => `${v.file} (${v.virus})`).join (', ')}) have been quarantined.`); | ||
console.log(`Viruses Found: ${viruses.join(', ')}`); | ||
@@ -390,5 +399,5 @@ } else { | ||
// Callback Example | ||
clamscan.scanStream(stream, (err, isInfected) => { | ||
clamscan.scanStream(stream, (err, { isInfected, viruses }) => { | ||
if (err) return console.error(err); | ||
if (isInfected) return console.log('Stream is infected! Booo!'); | ||
if (isInfected) return console.log('Stream is infected! Booo!', viruses); | ||
console.log('Stream is not infected! Yay!'); | ||
@@ -467,1 +476,15 @@ }); | ||
<a name="getFiles"></a> | ||
## getFiles(dir, [recursive]) ⇒ <code>Array</code> | ||
Gets a listing of all files (no directories) within a given path. | ||
By default, it will retrieve files recursively. | ||
**Kind**: global function | ||
**Returns**: <code>Array</code> - - List of all requested path files | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| dir | <code>string</code> | | The directory to get all files of | | ||
| [recursive] | <code>boolean</code> | <code>true</code> | If true (default), get all files recursively; False: only get files directly in path | | ||
@@ -6,11 +6,26 @@ // Credit to @qwtel on StackOverflow Question: | ||
const getFiles = async (dir) => { | ||
const dirents = await readdir(dir, { withFileTypes: true }); | ||
const files = await Promise.all(dirents.map((dirent) => { | ||
const res = resolve(dir, dirent.name); | ||
return dirent.isDirectory() ? getFiles(res) : res; | ||
/** | ||
* Gets a listing of all files (no directories) within a given path. | ||
* By default, it will retrieve files recursively. | ||
* | ||
* @param {string} dir - The directory to get all files of | ||
* @param {boolean} [recursive=true] - If true (default), get all files recursively; False: only get files directly in path | ||
* @returns {Array} - List of all requested path files | ||
*/ | ||
const getFiles = async (dir, recursive = true) => { | ||
const items = await readdir(dir, { withFileTypes: true }); | ||
const files = await Promise.all(items.map((item) => { | ||
const res = resolve(dir, item.name); | ||
if (!recursive) { | ||
if (!item.isDirectory()) return res; | ||
return new Promise((resolve) => resolve(null)); | ||
} | ||
return item.isDirectory() ? getFiles(res) : res; | ||
})); | ||
return files.flat(); | ||
return files.filter(Boolean).flat(); | ||
// @todo change to this when package required Node 20+ | ||
// const files = await fs.readdir(dir, { recursive: true }); | ||
} | ||
module.exports = getFiles; |
{ | ||
"name": "clamscan", | ||
"version": "2.2.3", | ||
"version": "2.3.0", | ||
"author": "Kyle Farris <kyle.farris@infotechinc.com> (https://infotechinc.com)", | ||
@@ -5,0 +5,0 @@ "description": "Use Node JS to scan files on your server with ClamAV's clamscan/clamdscan binary or via TCP to a remote server or local UNIX Domain socket. This is especially useful for scanning uploaded files provided by un-trusted sources.", |
@@ -355,7 +355,10 @@ # NodeJS Clamscan Virus Scanning Utility | ||
The `goodFiles` and `badFiles` parameters of the `endCallback` callback in this method will only contain the directories that were scanned in **all** **but** the following scenarios: | ||
The `goodFiles` parameter of the `endCallback` callback in this method will only contain the directory that was scanned in **all** **but** the following scenarios: | ||
- A `fileCallback` callback is provided, and `scanRecursively` is set to _true_. | ||
- The scanner is set to `clamdscan` and `scanRecursively` is set to _false_. | ||
- The scanned directory contains 1 or more viruses. In this case, the `goodFiles` array will be empty. | ||
There will, however, be a total count of the good files which is calculated by determining the total number of files scanned and subtracting the number of bad files from that count. We simply can't provide a list of all good files due to the potential large memory usage implications of scanning a directory with, for example, _millions_ of files. | ||
### Parameters | ||
@@ -367,5 +370,6 @@ | ||
- `err` (object) A standard javascript Error object (null if no error) | ||
- `goodFiles` (array) List of the full paths to all files that are _clean_. | ||
- `goodFiles` (array) An *empty* array if path is _infected_. An array containing the directory name that was passed in if _clean_. | ||
- `badFiles` (array) List of the full paths to all files that are _infected_. | ||
- `viruses` (array) List of all the viruses found (feature request: associate to the bad files). | ||
- `numGoodFiles` (number) Number of files that were found to be clean. | ||
@@ -386,5 +390,6 @@ - `fileCallback` (function) (optional) Will be called after each file in the directory has been scanned. This is useful for keeping track of the progress of the scan. This callback takes 3 parameters: | ||
- `isInfected` (boolean) **True**: File is infected; **False**: File is clean. **NULL**: Unable to scan. | ||
- `goodFiles` (array) List of the full paths to all files that are _clean_. | ||
- `goodFiles` (array) An *empty* array if path is _infected_. An array containing the directory name that was passed in if _clean_. | ||
- `badFiles` (array) List of the full paths to all files that are _infected_. | ||
- `viruses` (array) List of all the viruses found (feature request: associate to the bad files). | ||
- `numGoodFiles` (number) Number of files that were found to be clean. | ||
@@ -394,3 +399,3 @@ ### Callback Example | ||
```javascript | ||
clamscan.scanDir('/some/path/to/scan', (err, goodFiles, badFiles, viruses) { | ||
clamscan.scanDir('/some/path/to/scan', (err, goodFiles, badFiles, viruses, numGoodFiles) { | ||
if (err) return console.error(err); | ||
@@ -402,3 +407,3 @@ | ||
} else { | ||
console.log("Everything looks good! No problems here!."); | ||
console.log(`${goodFiles[0]} looks good! ${numGoodFiles} file scanned and no problems found!.`); | ||
} | ||
@@ -412,3 +417,3 @@ }); | ||
clamscan.scanDir('/some/path/to/scan').then(results => { | ||
const { path, isInfected, goodFiles, badFiles, viruses } = results; | ||
const { path, isInfected, goodFiles, badFiles, viruses, numGoodFiles } = results; | ||
//... | ||
@@ -423,3 +428,3 @@ }).catch(err => { | ||
```javascript | ||
const { path, isInfected, goodFiles, badFiles, viruses } = await clamscan.scanDir('/some/path/to/scan'); | ||
const { path, isInfected, goodFiles, badFiles, viruses, numGoodFiles } = await clamscan.scanDir('/some/path/to/scan'); | ||
``` | ||
@@ -426,0 +431,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
292264
4512
674