Comparing version 2.0.1 to 3.0.0
80
index.js
@@ -25,6 +25,2 @@ "use strict"; | ||
// uv_fs_scandir / withFileTypes is supported in Node.js 10.10 or greater | ||
const [_match, major, minor] = (/([0-9]+)\.([0-9]+)\./.exec(process.versions.node) || []); | ||
const scandir = (Number(major) > 10) || (Number(major) === 10 && Number(minor) >= 10); | ||
const rrdir = module.exports = async (dir, opts) => { | ||
@@ -43,3 +39,3 @@ opts = Object.assign({}, defaults, opts); | ||
if (!exclude) { | ||
entries = await readdir(dir, {encoding: opts.encoding, withFileTypes: scandir}); | ||
entries = await readdir(dir, {encoding: opts.encoding, withFileTypes: true}); | ||
} | ||
@@ -59,4 +55,3 @@ } catch (err) { | ||
for (const entry of entries) { | ||
const name = scandir ? entry.name : entry; | ||
const path = join(dir, name); | ||
const path = join(dir, entry.name); | ||
@@ -68,3 +63,3 @@ if (opts.exclude.length && !!(multimatch(path, opts.exclude, opts.minimatch).length)) { | ||
let stats; | ||
if (scandir && !opts.stats) { | ||
if (!opts.stats) { | ||
stats = entry; | ||
@@ -108,3 +103,3 @@ } else { | ||
if (!exclude) { | ||
entries = fs.readdirSync(dir, {encoding: opts.encoding, withFileTypes: scandir}); | ||
entries = fs.readdirSync(dir, {encoding: opts.encoding, withFileTypes: true}); | ||
} | ||
@@ -124,4 +119,3 @@ } catch (err) { | ||
for (const entry of entries) { | ||
const name = scandir ? entry.name : entry; | ||
const path = join(dir, name); | ||
const path = join(dir, entry.name); | ||
@@ -133,3 +127,3 @@ if (opts.exclude.length && !!(multimatch(path, opts.exclude, opts.minimatch).length)) { | ||
let stats; | ||
if (scandir && !opts.stats) { | ||
if (!opts.stats) { | ||
stats = entry; | ||
@@ -163,1 +157,63 @@ } else { | ||
}; | ||
module.exports.stream = async function* (dir, opts) { | ||
opts = Object.assign({}, defaults, opts); | ||
if (!dir || !typeof dir === "string") { | ||
throw new Error(`Expected a string, got '${dir}'`); | ||
} | ||
let entries = []; | ||
try { | ||
const exclude = (opts.exclude.length) && !!(multimatch(dir, opts.exclude, opts.minimatch).length); | ||
if (!exclude) { | ||
entries = await readdir(dir, {encoding: opts.encoding, withFileTypes: true}); | ||
} | ||
} catch (err) { | ||
if (opts.strict) { | ||
throw err; | ||
} else { | ||
yield {path: dir, err}; | ||
} | ||
} | ||
if (!entries.length) { | ||
return; | ||
} | ||
for (const entry of entries) { | ||
const path = join(dir, entry.name); | ||
if (opts.exclude.length && !!(multimatch(path, opts.exclude, opts.minimatch).length)) { | ||
continue; | ||
} | ||
let stats; | ||
if (!opts.stats) { | ||
stats = entry; | ||
} else { | ||
try { | ||
stats = await (opts.followSymlinks ? stat(path) : lstat(path)); | ||
} catch (err) { | ||
if (opts.strict) { | ||
throw err; | ||
} else { | ||
yield {path, err}; | ||
} | ||
} | ||
} | ||
if (stats) { | ||
const directory = stats.isDirectory(); | ||
const symlink = stats.isSymbolicLink(); | ||
const entry = {path, directory, symlink}; | ||
if (opts.stats) entry.stats = stats; | ||
yield entry; | ||
if (directory) { | ||
yield * await rrdir(path, opts); | ||
} | ||
} | ||
} | ||
}; |
{ | ||
"name": "rrdir", | ||
"version": "2.0.1", | ||
"description": "The fastest recursive readdir in town", | ||
"version": "3.0.0", | ||
"description": "Recursive directory crawler with a delightful API", | ||
"author": "silverwind <me@silverwind.io>", | ||
@@ -12,3 +12,3 @@ "repository": "silverwind/rrdir", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10.10" | ||
}, | ||
@@ -25,13 +25,14 @@ "files": [ | ||
"crawl", | ||
"directory" | ||
"directory", | ||
"scandir" | ||
], | ||
"devDependencies": { | ||
"eslint": "^5.14.1", | ||
"eslint-config-silverwind": "^2.1.0", | ||
"updates": "^7.2.0", | ||
"ver": "^4.0.1" | ||
"eslint": "^5.16.0", | ||
"eslint-config-silverwind": "^2.1.1", | ||
"updates": "^8.0.2", | ||
"ver": "^4.2.0" | ||
}, | ||
"dependencies": { | ||
"multimatch": "^3.0.0" | ||
"multimatch": "^4.0.0" | ||
} | ||
} |
# rrdir | ||
[![](https://img.shields.io/npm/v/rrdir.svg?style=flat)](https://www.npmjs.org/package/rrdir) [![](https://img.shields.io/npm/dm/rrdir.svg)](https://www.npmjs.org/package/rrdir) [![](https://api.travis-ci.org/silverwind/rrdir.svg?style=flat)](https://travis-ci.org/silverwind/rrdir) | ||
> The fastest recursive readdir in town | ||
> Recursive directory crawler with a delightful API | ||
Recursively crawls a directory to obtain paths and information on directory/symlink on each entry. Takes advantage of `uv_fs_scandir` in Node.js 10.10 or higher, which increases performance significantly. | ||
Comparison against the `walkdir` module crawling the [Node.js repository](https://github.com/nodejs/node) on a NVMe SSD: | ||
| Test | Engine | OS | Runtime | | ||
|-----------------|-----------------|--------------|---------| | ||
| **rrdir** sync | Node.js 10.10.0 | Linux 4.18.4 | 0.289s | | ||
| **rrdir** async | Node.js 10.10.0 | Linux 4.18.4 | 0.400s | | ||
| walkdir sync | Node.js 10.10.0 | Linux 4.18.4 | 0.423s | | ||
| walkdir async | Node.js 10.10.0 | Linux 4.18.4 | 1.557s | | ||
| **rrdir** sync | Node.js 8.11.4 | Linux 4.18.4 | 0.383s | | ||
| walkdir sync | Node.js 8.11.4 | Linux 4.18.4 | 0.416s | | ||
| **rrdir** async | Node.js 8.11.4 | Linux 4.18.4 | 1.148s | | ||
| walkdir async | Node.js 8.11.4 | Linux 4.18.4 | 1.813s | | ||
## Installation | ||
@@ -28,5 +13,14 @@ ```console | ||
```js | ||
const rrdir = require('rrdir'); | ||
const entries = await rrdir('../dir'); // => [{path: '../dir/file1', directory: false, symlink: true}] | ||
const entries = rrdir.sync('../dir'); // => [{path: '../dir/file1', directory: false, symlink: true}] | ||
const rrdir = require("rrdir"); | ||
const entries = await rrdir("../dir"); | ||
// => [{path: '../dir/file1', directory: false, symlink: true}] | ||
const entries = rrdir.sync("../dir"); | ||
// => [{path: '../dir/file1', directory: false, symlink: true}] | ||
for await (const entry of rrdir.stream("../dir")) { | ||
// => {path: '../dir/file1', directory: false, symlink: true} | ||
} | ||
``` | ||
@@ -38,4 +32,5 @@ | ||
### `rrdir.sync(dir, [options])` | ||
### `rrdir.stream(dir, [options])` | ||
Recursively searches a directory for entries contained within. Both functions will reject or throw on unexpected errors, but can optionally ignore errors encountered on individual files. Returns an array of `entry`. | ||
Recursively searches a directory for entries contained within. Will reject or throw on unexpected errors, but can optionally ignore errors encountered on individual files. `rrdir` and `rrdir.sync` return an array of `entry`, `rrdir.stream` is a async iterator which yields individual entries. | ||
@@ -59,2 +54,18 @@ #### `entry` | ||
#### Benchmarks | ||
Comparison against the `walkdir` module crawling the [Node.js repository](https://github.com/nodejs/node) on a NVMe SSD: | ||
| Test | Engine | OS | Runtime | | ||
|-----------------|-----------------|--------------|---------| | ||
| **rrdir** sync | Node.js 10.10.0 | Linux 4.18.4 | 0.289s | | ||
| **rrdir** async | Node.js 10.10.0 | Linux 4.18.4 | 0.400s | | ||
| walkdir sync | Node.js 10.10.0 | Linux 4.18.4 | 0.423s | | ||
| walkdir async | Node.js 10.10.0 | Linux 4.18.4 | 1.557s | | ||
| **rrdir** sync | Node.js 8.11.4 | Linux 4.18.4 | 0.383s | | ||
| walkdir sync | Node.js 8.11.4 | Linux 4.18.4 | 0.416s | | ||
| **rrdir** async | Node.js 8.11.4 | Linux 4.18.4 | 1.148s | | ||
| walkdir async | Node.js 8.11.4 | Linux 4.18.4 | 1.813s | | ||
© [silverwind](https://github.com/silverwind), distributed under BSD licence |
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
9911
177
69
+ Added@types/minimatch@3.0.5(transitive)
+ Addedarray-differ@3.0.0(transitive)
+ Addedarray-union@2.1.0(transitive)
+ Addedarrify@2.0.1(transitive)
+ Addedmultimatch@4.0.0(transitive)
- Removedarray-differ@2.1.0(transitive)
- Removedarray-union@1.0.2(transitive)
- Removedarray-uniq@1.0.3(transitive)
- Removedarrify@1.0.1(transitive)
- Removedmultimatch@3.0.0(transitive)
Updatedmultimatch@^4.0.0