Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Utility for recursive aggregation of directory trees. Useful for creating directory indices, searching by file attributes, performing calculations on a directory tree, building routing tables dynamically, etc.
const aggregate = require('adir'),
tree = {}
function onEntry(stats, subtree) {
var name = stats.basename
if (stats.isDirectory())
return subtree[ name ] = {}
else
subtree[ name ] = stats.size
}
function done(err) {
if (err)
console.error(err.stack)
else
console.log(tree)
}
aggregate('./', onEntry, tree, done)
adir(path, onEntry, [initialValue], [callback])
⇒ Promise
adir.fs
: The file system interface to use.
adir.break
: Reference used to signal the end of an aggregation branch.
adir.version
: The version string from package manifest.
const fs = require('fs'),
adir = require('adir')
typeof adir === 'function'
adir.fs === fs
typeof adir.break === 'object'
typeof adir.version === 'string'
function onEntry(stats, value) {
stats instanceof fs.Stats
typeof stats.path === 'string'
typeof stats.basename === 'string'
value === 0
return value
}
function callback(err) {
err instanceof Error ||
err === null
}
adir('./', onEntry, 0, callback) instanceof Promise
adir
iterates over subdirectories of a folder and calls the given onEntry
handler on each directory or file,
taking an extended fs.Stats
instance and the value previously returned in the last invocation of onEntry
, or initialValue
, if supplied.
You can think of it like a kind of Array.prototype.reduce()
except the reduction forks when it meets a directory.
const aggregate = require('adir')
function onEntry(stats, count) {
if (stats.isDirectory())
return count + 1
else
console.log(stats.path, 'has', count, 'parent directories')
}
aggregate('./', onEntry, 0)
If onEntry
returns a Promise
then it'll be awaited before the aggregation of the corresponding branch continues. See this for a working example.
The aggregation of a branch stops immediately if onEntry
returns with adir.break
(or the Promise
returned by onEntry
resolves with that value).
This example shows that in action.
adir
is compatible with Node 0.8 and above but a Promise
implementation is required even if you're using the callback API only.
Tested with bluebird.
With npm:
npm install adir
FAQs
Utility for recursive aggregation of directory trees
We found that adir demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.