Skip to content

Commit

Permalink
fix: fs.readdir() on ancient nodes that don't know about options
Browse files Browse the repository at this point in the history
Fix: #218
  • Loading branch information
isaacs committed Feb 26, 2022
1 parent e973238 commit 1cc1135
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions graceful-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,24 +191,43 @@ function patch (fs) {

var fs$readdir = fs.readdir
fs.readdir = readdir
var noReaddirOptionVersions = /^v[0-5]\./
function readdir (path, options, cb) {
if (typeof options === 'function')
cb = options, options = null

var go$readdir = noReaddirOptionVersions.test(process.version)
? function go$readdir (path, options, cb, startTime) {
return fs$readdir(path, fs$readdirCallback(
path, options, cb, startTime
))
}
: function go$readdir (path, options, cb, startTime) {
return fs$readdir(path, options, fs$readdirCallback(
path, options, cb, startTime
))
}

return go$readdir(path, options, cb)

function go$readdir (path, options, cb, startTime) {
return fs$readdir(path, options, function (err, files) {
function fs$readdirCallback (path, options, cb, startTime) {
return function (err, files) {
if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
enqueue([go$readdir, [path, options, cb], err, startTime || Date.now(), Date.now()])
enqueue([
go$readdir,
[path, options, cb],
err,
startTime || Date.now(),
Date.now()
])
else {
if (files && files.sort)
files.sort()

if (typeof cb === 'function')
cb.call(this, err, files)
}
})
}
}
}

Expand Down

0 comments on commit 1cc1135

Please sign in to comment.