POSIX readdir(3)
streams directory entries, but, alas, Node.js' fs.readdir()
calls back with an array. An array is fine ... unless you've a massive number of files in the directory, and want to handle them one at a time. Frankly, if you want an array, you might as well scandir(3)
, which is exactly what libuv does. There is no readdir(3)
in fs.readdir()
.
This module builds a very thin native Node.js add-on wrapper around opendir(3)
, readdir(3)
, and closedir(3)
for streaming directory reads. The binding is a bit clunky, tracking the C-language API.
Unless you're building a stream, you probably want to use a stream package that in turn uses this package.
Behold! A test suite:
var Reader = var fs = var path = var assert = var series = // Create a directory.var directory = '/tmp/example-' + fs // Write some files in it.fsfs // Create a readdir object. Unfortunately, this object encapsulates// magic: The POSIX `DIR` pointer from `opendir(3)`.var reader = directoryvar entries = { reader}
This package started with Fabian Canas' node-native-boilerplate. Fabian was kind enough to license his boilerplate under The MIT License. I've licensed my code under The MIT License, too.