I created walk quite literally a decade ago, in the Node v0.x days.
Back then using an EventEmitter seemed like the thing to do. Nowadays,
it seems a bit overkill for the simple task of walking over directories.
There's nothing wrong with walk - it's about the same as it was 10 years ago -
however, at only 50 lines of code long, @root/walk is much simpler and much faster.
Both Asynchronous and Synchronous versions are provided.
'use strict';var walk = require('walk');var fs = require('fs');var options;var walker;options = { followLinks: false, // directories with these keys will be skipped filters: ['Temp', '_Temp'],};walker = walk.walk('/tmp', options);// OR// walker = walk.walkSync("/tmp", options);walker.on('names', function (root, nodeNamesArray) { nodeNamesArray.sort
Sync
Note: You can't use EventEmitter if you want truly synchronous walker
(although it's synchronous under the hood, it appears not to be due to the use of process.nextTick()).
Instead you must use options.listeners for truly synchronous walker.
Although the sync version uses all of the fs.readSync, fs.readdirSync, and other sync methods,
I don't think I can prevent the process.nextTick() that EventEmitter calls.
(function () { 'use strict'; var walk = require('walk'); var fs = require('fs'); var options; var walker; // To be truly synchronous in the emitter and maintain a compatible api, // the listeners must be listed before the object is created options = { listeners: { names: function (root, nodeNamesArray) { nodeNamesArray.sort(function (a, b) { if (a > b) return 1; if (a < b) return -1
API
Emitted Values
on('XYZ', function(root, stats, next) {})
root - the containing the files to be inspected
stats[Array] - a single stats object or an array with some added attributes
type - 'file', 'directory', etc
error
name - the name of the file, dir, etc
next - no more files will be read until this is called
Single Events - fired immediately
end - No files, dirs, etc left to inspect
directoryError - Error when fstat succeeded, but reading path failed (Probably due to permissions).
nodeError - Error fstat did not succeeded.
node - a stats object for a node of any type
file - includes links when followLinks is true
directory - NOTE you could get a recursive loop if followLinks and a directory links to its parent
symbolicLink - always empty when followLinks is true
blockDevice
characterDevice
FIFO
socket
Events with Array Arguments - fired after all files in the dir have been stated
names - before any stat takes place. Useful for sorting and filtering.
Note: the array is an array of strings, not stat objects
Note: the next argument is a noop
errors - errors encountered by fs.stat when reading ndes in a directory
nodes - an array of stats of any type
files
directories - modification of this array - sorting, removing, etc - affects traversal
symbolicLinks
blockDevices
characterDevices
FIFOs
sockets
Warning beware of infinite loops when followLinks is true (using walk-recurse varient).
Comparisons
Tested on my /System containing 59,490 (+ self) directories (and lots of files).
The size of the text output was 6mb.
find:
time bash -c "find /System -type d | wc"
59491 97935 6262916
real 2m27.114suser 0m1.193ssys 0m14.859s
find.js:
Note that find.js omits the start directory
time bash -c "node examples/find.js /System -type d | wc"59490 97934 6262908# Test 1real 2m52.273suser 0m20.374ssys 0m27.800s# Test 2real 2m23.725suser 0m18.019ssys 0m23.202s# Test 3real 2m50.077suser 0m17.661ssys 0m24.008s
In conclusion node.js asynchronous walk is much slower than regular "find".
LICENSE
node-walk is available under the following licenses: