diff options
author | isaacs <i@izs.me> | 2013-07-10 11:50:28 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-07-10 11:51:25 +0400 |
commit | e3007309aa93e94b69b5094fa1b11070b2ef993c (patch) | |
tree | e229ad930d447f1aeb3e92d4e260d28270d443f2 /node_modules/graceful-fs | |
parent | 28607ac2f54729a73244948212e436c65fea4bb2 (diff) |
graceful-fs@1.2.3
Related to #3259
Because readFile and writeFile were not wrapped, there were cases where
a lot of package.json reading would cause problems. This wasn't an
issue with earlier versions of graceful-fs, because it monkey-patched
the builtin FS module's open() method, so that readFile and writeFile
would Just Work.
However, that caused other problems.
Note that fs.ReadStream and fs.WriteStream are still not covered in the
open fd guard. That can be done later, but this extends EMFILE coverage
to the more commonly used methods first.
Diffstat (limited to 'node_modules/graceful-fs')
-rw-r--r-- | node_modules/graceful-fs/graceful-fs.js | 83 | ||||
-rw-r--r-- | node_modules/graceful-fs/package.json | 4 |
2 files changed, 85 insertions, 2 deletions
diff --git a/node_modules/graceful-fs/graceful-fs.js b/node_modules/graceful-fs/graceful-fs.js index cc2f19e58..ca9115243 100644 --- a/node_modules/graceful-fs/graceful-fs.js +++ b/node_modules/graceful-fs/graceful-fs.js @@ -82,6 +82,12 @@ function flush () { case 'ReaddirReq': readdir(req.path, req.cb) break + case 'ReadFileReq': + readFile(req.path, req.options, req.cb) + break + case 'WriteFileReq': + writeFile(req.path, req.data, req.options, req.cb) + break default: throw new Error('Unknown req type: ' + req.constructor.name) } @@ -144,6 +150,83 @@ function ReaddirReq (path, cb) { } +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. var constants = require("constants") diff --git a/node_modules/graceful-fs/package.json b/node_modules/graceful-fs/package.json index 4884b29f6..83fd6db67 100644 --- a/node_modules/graceful-fs/package.json +++ b/node_modules/graceful-fs/package.json @@ -6,7 +6,7 @@ }, "name": "graceful-fs", "description": "A drop-in replacement for fs, making various improvements.", - "version": "1.2.2", + "version": "1.2.3", "repository": { "type": "git", "url": "git://github.com/isaacs/node-graceful-fs.git" @@ -43,6 +43,6 @@ "bugs": { "url": "https://github.com/isaacs/node-graceful-fs/issues" }, - "_id": "graceful-fs@1.2.2", + "_id": "graceful-fs@1.2.3", "_from": "graceful-fs@latest" } |