graceful-fs
Advanced tools
Comparing version 1.2.2 to 1.2.3
@@ -85,2 +85,8 @@ // this keeps a queue of opened file descriptors, and will make | ||
break | ||
case 'ReadFileReq': | ||
readFile(req.path, req.options, req.cb) | ||
break | ||
case 'WriteFileReq': | ||
writeFile(req.path, req.data, req.options, req.cb) | ||
break | ||
default: | ||
@@ -148,2 +154,79 @@ throw new Error('Unknown req type: ' + req.constructor.name) | ||
fs.readFile = gracefulReadFile | ||
function gracefulReadFile(path, options, cb) { | ||
if (typeof options === "function") cb = options, options = null | ||
if (typeof cb !== "function") cb = noop | ||
if (fs._curOpen >= fs.MAX_OPEN) { | ||
queue.push(new ReadFileReq(path, options, cb)) | ||
setTimeout(flush) | ||
return | ||
} | ||
readFile(path, options, function (er, data) { | ||
if (er && er.code === "EMFILE" && fs._curOpen > fs.MIN_MAX_OPEN) { | ||
fs.MAX_OPEN = fs._curOpen - 1 | ||
return fs.readFile(path, options, cb) | ||
} | ||
cb(er, data) | ||
}) | ||
} | ||
function readFile (path, options, cb) { | ||
cb = cb || noop | ||
fs._curOpen ++ | ||
fs._originalFs.readFile.call(fs, path, options, function (er, data) { | ||
onclose() | ||
cb(er, data) | ||
}) | ||
} | ||
function ReadFileReq (path, options, cb) { | ||
this.path = path | ||
this.options = options | ||
this.cb = cb | ||
} | ||
fs.writeFile = gracefulWriteFile | ||
function gracefulWriteFile(path, data, options, cb) { | ||
if (typeof options === "function") cb = options, options = null | ||
if (typeof cb !== "function") cb = noop | ||
if (fs._curOpen >= fs.MAX_OPEN) { | ||
queue.push(new WriteFileReq(path, data, options, cb)) | ||
setTimeout(flush) | ||
return | ||
} | ||
writeFile(path, data, options, function (er) { | ||
if (er && er.code === "EMFILE" && fs._curOpen > fs.MIN_MAX_OPEN) { | ||
fs.MAX_OPEN = fs._curOpen - 1 | ||
return fs.writeFile(path, data, options, cb) | ||
} | ||
cb(er) | ||
}) | ||
} | ||
function writeFile (path, data, options, cb) { | ||
cb = cb || noop | ||
fs._curOpen ++ | ||
fs._originalFs.writeFile.call(fs, path, data, options, function (er) { | ||
onclose() | ||
cb(er) | ||
}) | ||
} | ||
function WriteFileReq (path, data, options, cb) { | ||
this.path = path | ||
this.data = data | ||
this.options = options | ||
this.cb = cb | ||
} | ||
// (re-)implement some things that are known busted or missing. | ||
@@ -150,0 +233,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "A drop-in replacement for fs, making various improvements.", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
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
18914
553