react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / node_modules / commoner / node_modules / graceful-fs / graceful-fs.js
81164 views// Monkey-patching the fs module.1// It's ugly, but there is simply no other way to do this.2var fs = module.exports = require('./fs.js')34var assert = require('assert')56// fix up some busted stuff, mostly on windows and old nodes7require('./polyfills.js')89var util = require('util')1011function noop () {}1213var debug = noop14if (util.debuglog)15debug = util.debuglog('gfs')16else if (/\bgfs\b/i.test(process.env.NODE_DEBUG || ''))17debug = function() {18var m = util.format.apply(util, arguments)19m = 'GFS: ' + m.split(/\n/).join('\nGFS: ')20console.error(m)21}2223if (/\bgfs\b/i.test(process.env.NODE_DEBUG || '')) {24process.on('exit', function() {25debug('fds', fds)26debug(queue)27assert.equal(queue.length, 0)28})29}303132var originalOpen = fs.open33fs.open = open3435function open(path, flags, mode, cb) {36if (typeof mode === "function") cb = mode, mode = null37if (typeof cb !== "function") cb = noop38new OpenReq(path, flags, mode, cb)39}4041function OpenReq(path, flags, mode, cb) {42this.path = path43this.flags = flags44this.mode = mode45this.cb = cb46Req.call(this)47}4849util.inherits(OpenReq, Req)5051OpenReq.prototype.process = function() {52originalOpen.call(fs, this.path, this.flags, this.mode, this.done)53}5455var fds = {}56OpenReq.prototype.done = function(er, fd) {57debug('open done', er, fd)58if (fd)59fds['fd' + fd] = this.path60Req.prototype.done.call(this, er, fd)61}626364var originalReaddir = fs.readdir65fs.readdir = readdir6667function readdir(path, cb) {68if (typeof cb !== "function") cb = noop69new ReaddirReq(path, cb)70}7172function ReaddirReq(path, cb) {73this.path = path74this.cb = cb75Req.call(this)76}7778util.inherits(ReaddirReq, Req)7980ReaddirReq.prototype.process = function() {81originalReaddir.call(fs, this.path, this.done)82}8384ReaddirReq.prototype.done = function(er, files) {85if (files && files.sort)86files = files.sort()87Req.prototype.done.call(this, er, files)88onclose()89}909192var originalClose = fs.close93fs.close = close9495function close (fd, cb) {96debug('close', fd)97if (typeof cb !== "function") cb = noop98delete fds['fd' + fd]99originalClose.call(fs, fd, function(er) {100onclose()101cb(er)102})103}104105106var originalCloseSync = fs.closeSync107fs.closeSync = closeSync108109function closeSync (fd) {110try {111return originalCloseSync(fd)112} finally {113onclose()114}115}116117118// Req class119function Req () {120// start processing121this.done = this.done.bind(this)122this.failures = 0123this.process()124}125126Req.prototype.done = function (er, result) {127var tryAgain = false128if (er) {129var code = er.code130var tryAgain = code === "EMFILE"131if (process.platform === "win32")132tryAgain = tryAgain || code === "OK"133}134135if (tryAgain) {136this.failures ++137enqueue(this)138} else {139var cb = this.cb140cb(er, result)141}142}143144var queue = []145146function enqueue(req) {147queue.push(req)148debug('enqueue %d %s', queue.length, req.constructor.name, req)149}150151function onclose() {152var req = queue.shift()153if (req) {154debug('process', req.constructor.name, req)155req.process()156}157}158159160