yuanchuan / node-watch

A wrapper and enhancements for fs.watch
https://npm.im/node-watch
MIT License
339 stars 44 forks source link

ENOENT caused by race condition in is.js #82

Closed szankrisz closed 5 years ago

szankrisz commented 5 years ago

Hi,

The below issue:

https://github.com/yuanchuan/node-watch/issues/19

...still exists in the latest 0.6.0 version. The recommended try-catch approach can fix this, I believe.

Would it be possible to incorporate it in the new version?

Thanks!

Regards, Krisztian

szankrisz commented 5 years ago

Below is the fixed lib/is.js. I wanted to put it into a pull request but I was not able to.

var fs = require('fs');
var path = require('path');

function matchObject(item, str) {
  return Object.prototype.toString.call(item)
    === '[object ' + str + ']';
}

var is = {
  nil: function(item) {
    return item == null;
  },
  array: function(item) {
    return Array.isArray(item);
  },
  emptyObject: function(item) {
    for (var key in item) {
      return false;
    }
    return true;
  },
  buffer: function(item) {
    return Buffer.isBuffer(item);
  },
  regExp: function(item) {
    return matchObject(item, 'RegExp');
  },
  string: function(item) {
    return matchObject(item, 'String');
  },
  func: function(item) {
    return typeof item === 'function';
  },
  number: function(item) {
    return matchObject(item, 'Number');
  },
  exists: function(name) {
    return fs.existsSync(name);
  },
  file: function(name) {
    try {
      return fs.statSync(name).isFile();
    } catch (e) {
      if (e.code === 'ENOENT')
        return false;
      else
        throw e;
    }
  },
  sameFile: function(a, b) {
    return path.resolve(a) === path.resolve(b);
  },
  directory: function(name) {
    try {
      return fs.statSync(name).isDirectory();
    } catch (e) {
      if (e.code === 'ENOENT')
        return false;
      else
        throw e;
    }
  },
  symbolicLink: function(name) {
    try {
      return fs.lstatSync(name).isSymbolicLink();
    } catch (e) {
      if (e.code === 'ENOENT')
        return false;
      else
        throw e;
    }
  }
};

module.exports = is;
yuanchuan commented 5 years ago

Thanks for reporting. I'll test for it.

szankrisz commented 5 years ago

Hi,

I really don't want to pressure you too much and I really appreciate for considering this fix but is there any update to when this fix can be merged in? It happens quite often still.

Thanks in advance!

yuanchuan commented 5 years ago

Please try 0.6.1

szankrisz commented 5 years ago

I've tested it and it is working nicely!

Much appreciated! Thank you!