Вход Регистрация
Файл: error-kitty/node_modules/mocha/node_modules/glob/node_modules/graceful-fs/graceful-fs.js
Строк: 131
<?php
// Monkey-patching the fs module.
// It's ugly, but there is simply no other way to do this.
var fs module.exports = require('fs')

var 
assert = require('assert')

// fix up some busted stuff, mostly on windows and old nodes
require('./polyfills.js')

// The EMFILE enqueuing stuff

var util = require('util')

function 
noop () {}

var 
debug noop
if (util.debuglog)
  
debug util.debuglog('gfs')
else if (/
bgfsb/i.test(process.env.NODE_DEBUG || ''))
  
debug = function() {
    var 
util.format.apply(utilarguments)
    
'GFS: ' m.split(/n/).join('nGFS: ')
    
console.error(m)
  }

if (/
bgfsb/i.test(process.env.NODE_DEBUG || '')) {
  
process.on('exit', function() {
    
debug('fds'fds)
    
debug(queue)
    
assert.equal(queue.length0)
  })
}


var 
originalOpen fs.open
fs
.open open

function open(pathflagsmodecb) {
  if (
typeof mode === "function"cb modemode null
  
if (typeof cb !== "function"cb noop
  
new OpenReq(pathflagsmodecb)
}

function 
OpenReq(pathflagsmodecb) {
  
this.path path
  this
.flags flags
  this
.mode mode
  this
.cb cb
  Req
.call(this)
}

util.inherits(OpenReqReq)

OpenReq.prototype.process = function() {
  
originalOpen.call(fsthis.paththis.flagsthis.modethis.done)
}

var 
fds = {}
OpenReq.prototype.done = function(erfd) {
  
debug('open done'erfd)
  if (
fd)
    
fds['fd' fd] = this.path
  Req
.prototype.done.call(thiserfd)
}


var 
originalReaddir fs.readdir
fs
.readdir readdir

function readdir(pathcb) {
  if (
typeof cb !== "function"cb noop
  
new ReaddirReq(pathcb)
}

function 
ReaddirReq(pathcb) {
  
this.path path
  this
.cb cb
  Req
.call(this)
}

util.inherits(ReaddirReqReq)

ReaddirReq.prototype.process = function() {
  
originalReaddir.call(fsthis.paththis.done)
}

ReaddirReq.prototype.done = function(erfiles) {
  if (
files && files.sort)
    
files files.sort()
  
Req.prototype.done.call(thiserfiles)
  
onclose()
}


var 
originalClose fs.close
fs
.close close

function close (fdcb) {
  
debug('close'fd)
  if (
typeof cb !== "function"cb noop
  delete fds
['fd' fd]
  
originalClose.call(fsfd, function(er) {
    
onclose()
    
cb(er)
  })
}


var 
originalCloseSync fs.closeSync
fs
.closeSync closeSync

function closeSync (fd) {
  try {
    return 
originalCloseSync(fd)
  } finally {
    
onclose()
  }
}


// Req class
function Req () {
  
// start processing
  
this.done this.done.bind(this)
  
this.failures 0
  this
.process()
}

Req.prototype.done = function (erresult) {
  var 
tryAgain false
  
if (er) {
    var 
code er.code
    
var tryAgain code === "EMFILE"
    
if (process.platform === "win32")
      
tryAgain tryAgain || code === "OK"
  
}

  if (
tryAgain) {
    
this.failures ++
    
enqueue(this)
  } else {
    var 
cb this.cb
    cb
(erresult)
  }
}

var 
queue = []

function 
enqueue(req) {
  
queue.push(req)
  
debug('enqueue %d %s'queue.lengthreq.constructor.namereq)
}

function 
onclose() {
  var 
req queue.shift()
  if (
req) {
    
debug('process'req.constructor.namereq)
    
req.process()
  }
}
?>
Онлайн: 3
Реклама