Code coverage report for graceful-fs.js

# all files / __root__/ graceful-fs.js

79.62% Statements 125/157
48.1% Branches 38/79
82.35% Functions 28/34
79.62% Lines 125/157

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    25416×
    16×
    16×
    16×

    16×

    16×

    16×
    16×
    16×

    16×

    16×
    16×
    8×

    16×
    16×
    35225×
    35225×
    35225×

    35225×
    35225×

    16×
    16×

    8788×
    8788×
    8788×

    16×

    24×
    24×
    24×
    24×
    24×
    24×
    24×
    24×
    16×
    16388×

    16388×

    16×
    16388×
    16388×

    16388×
    16388×
    16388×

    24×
    24×
    16×
    8194×

    8194×

    16×
    8194×
    8194×

    8194×
    8194×
    8194×

    24×
    24×
    24×
    16×

    16×

    24×
    24×
    16×
    2×

    16×
    2×
    2×
    2×

    2×

    2×
    2×
    2×

    24×

    24×
    24×
    24×

    24×
    24×
    24×

    24×
    24×

    16×
    8194×
    8194×

    16×
    8194×
    8194×
    8194×

    8194×
    8194×
    8194×

    16×
    8194×
    8194×

    16×
    8194×
    8194×
    8194×

    8194×
    8194×

    16×
    8194×

    16×
    8194×

    24×
    24×
    16×
    33178×
    12693×

    33178×

    16×
    34202×
    34202×
    1024×

    33178×
    33178×
    33178×

    24×

    16×
    1024×
    1024×

    16×
    101775×
    101775×
    1024×
    1024×

     var fs = require('fs')
    var polyfills = require('./polyfills.js')
    var legacy = require('./legacy-streams.js')
    var queue = []

    var util = require('util')

    function noop () {}

    var debug = noop
    Eif (util.debuglog)
      debug = util.debuglog('gfs4')
    else if (/\bgfs4\b/i.test(process.env.NODE_DEBUG || ''))
      debug = function() {
        var m = util.format.apply(util, arguments)
        m = 'GFS4: ' + m.split(/\n/).join('\nGFS4: ')
        console.error(m)
      }

    Iif (/\bgfs4\b/i.test(process.env.NODE_DEBUG || '')) {
      process.on('exit', function() {
        debug(queue)
        require('assert').equal(queue.length, 0)
      })
    }

    module.exports = patch(require('./fs.js'))
    if (process.env.TEST_GRACEFUL_FS_GLOBAL_PATCH) {
      module.exports = patch(fs)
    }

    // Always patch fs.close/closeSync, because we want to
    // retry() whenever a close happens *anywhere* in the program.
    // This is essential when multiple graceful-fs instances are
    // in play at the same time.
    module.exports.close =
    fs.close = (function (fs$close) { return function (fd, cb) {
      return fs$close.call(fs, fd, function (err) {
        Eif (!err)
          retry()

        Eif (typeof cb === 'function')
          cb.apply(this, arguments)
      })
    }})(fs.close)

    module.exports.closeSync =
    fs.closeSync = (function (fs$closeSync) { return function (fd) {
      // Note that graceful-fs also retries when fs.closeSync() fails.
      // Looks like a bug to me, although it's probably a harmless one.
      var rval = fs$closeSync.apply(fs, arguments)
      retry()
      return rval
    }})(fs.closeSync)

    function patch (fs) {
      // Everything that references the open() function needs to be in here
      polyfills(fs)
      fs.gracefulify = patch
      fs.FileReadStream = ReadStream;  // Legacy name.
      fs.FileWriteStream = WriteStream;  // Legacy name.
      fs.createReadStream = createReadStream
      fs.createWriteStream = createWriteStream
      var fs$readFile = fs.readFile
      fs.readFile = readFile
      function readFile (path, options, cb) {
        Iif (typeof options === 'function')
          cb = options, options = null

        return go$readFile(path, options, cb)

        function go$readFile (path, options, cb) {
          return fs$readFile(path, options, function (err) {
            Iif (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
              enqueue([go$readFile, [path, options, cb]])
            else {
              Eif (typeof cb === 'function')
                cb.apply(this, arguments)
              retry()
            }
          })
        }
      }

      var fs$writeFile = fs.writeFile
      fs.writeFile = writeFile
      function writeFile (path, data, options, cb) {
        Iif (typeof options === 'function')
          cb = options, options = null

        return go$writeFile(path, data, options, cb)

        function go$writeFile (path, data, options, cb) {
          return fs$writeFile(path, data, options, function (err) {
            Iif (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
              enqueue([go$writeFile, [path, data, options, cb]])
            else {
              Eif (typeof cb === 'function')
                cb.apply(this, arguments)
              retry()
            }
          })
        }
      }

      var fs$appendFile = fs.appendFile
      Eif (fs$appendFile)
        fs.appendFile = appendFile
      function appendFile (path, data, options, cb) {
        if (typeof options === 'function')
          cb = options, options = null

        return go$appendFile(path, data, options, cb)

        function go$appendFile (path, data, options, cb) {
          return fs$appendFile(path, data, options, function (err) {
            if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
              enqueue([go$appendFile, [path, data, options, cb]])
            else {
              if (typeof cb === 'function')
                cb.apply(this, arguments)
              retry()
            }
          })
        }
      }

      var fs$readdir = fs.readdir
      fs.readdir = readdir
      function readdir (path, cb) {
        return go$readdir(path, cb)

        function go$readdir () {
          return fs$readdir(path, function (err, files) {
            Eif (files && files.sort)
              files.sort();  // Backwards compatibility with graceful-fs.

            Iif (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
              enqueue([go$readdir, [path, cb]])
            else {
              Eif (typeof cb === 'function')
                cb.apply(this, arguments)
              retry()
            }
          })
        }
      }

      Iif (process.version.substr(0, 4) === 'v0.8') {
        var legStreams = legacy(fs)
        ReadStream = legStreams.ReadStream
        WriteStream = legStreams.WriteStream
      }

      var fs$ReadStream = fs.ReadStream
      ReadStream.prototype = Object.create(fs$ReadStream.prototype)
      ReadStream.prototype.open = ReadStream$open

      var fs$WriteStream = fs.WriteStream
      WriteStream.prototype = Object.create(fs$WriteStream.prototype)
      WriteStream.prototype.open = WriteStream$open

      fs.ReadStream = ReadStream
      fs.WriteStream = WriteStream

      function ReadStream (path, options) {
        Eif (this instanceof ReadStream)
          return fs$ReadStream.apply(this, arguments), this
        else
          return ReadStream.apply(Object.create(ReadStream.prototype), arguments)
      }

      function ReadStream$open () {
        var that = this
        open(that.path, that.flags, that.mode, function (err, fd) {
          Iif (err) {
            if (that.autoClose)
              that.destroy()

            that.emit('error', err)
          } else {
            that.fd = fd
            that.emit('open', fd)
            that.read()
          }
        })
      }

      function WriteStream (path, options) {
        Eif (this instanceof WriteStream)
          return fs$WriteStream.apply(this, arguments), this
        else
          return WriteStream.apply(Object.create(WriteStream.prototype), arguments)
      }

      function WriteStream$open () {
        var that = this
        open(that.path, that.flags, that.mode, function (err, fd) {
          Iif (err) {
            that.destroy()
            that.emit('error', err)
          } else {
            that.fd = fd
            that.emit('open', fd)
          }
        })
      }

      function createReadStream (path, options) {
        return new ReadStream(path, options)
      }

      function createWriteStream (path, options) {
        return new WriteStream(path, options)
      }

      var fs$open = fs.open
      fs.open = open
      function open (path, flags, mode, cb) {
        if (typeof mode === 'function')
          cb = mode, mode = null

        return go$open(path, flags, mode, cb)

        function go$open (path, flags, mode, cb) {
          return fs$open(path, flags, mode, function (err, fd) {
            if (err && (err.code === 'EMFILE' || err.code === 'ENFILE'))
              enqueue([go$open, [path, flags, mode, cb]])
            else {
              Eif (typeof cb === 'function')
                cb.apply(this, arguments)
              retry()
            }
          })
        }
      }

      return fs
    }

    function enqueue (elem) {
      debug('ENQUEUE', elem[0].name, elem[1])
      queue.push(elem)
    }

    function retry () {
      var elem = queue.shift()
      if (elem) {
        debug('RETRY', elem[0].name, elem[1])
        elem[0].apply(null, elem[1])
      }
    }