feross / hostile

Simple, programmatic `/etc/hosts` manipulation (in node.js)
MIT License
783 stars 52 forks source link

NotFound: No such file or directory (os error 2): open '/etc/hosts' #57

Open masx200 opened 1 hour ago

masx200 commented 1 hour ago

NotFound: No such file or directory (os error 2): open '/etc/hosts'

   at Object.open (ext:deno_fs/30_fs.js:633:21)
    at Object.open (ext:deno_node/_fs/_fs_open.ts:80:10)
    at ReadStream._construct (ext:deno_node/internal/fs/streams.mjs:63:17)
    at constructNT (ext:deno_node/_stream.mjs:1672:16)
    at processTicksAndRejections (ext:deno_node/_next_tick.ts:33:15)
    at runNextTicks (ext:deno_node/_next_tick.ts:75:3)
    at eventLoopTick (ext:core/01_core.js:182:21)
 fs.createReadStream(filePath, { encoding: 'utf8' }) .on('error', cb)

exports.getFile = function (filePath, preserveFormatting, cb) {
  var lines = []
  if (typeof cb !== 'function') {
    fs.readFileSync(filePath, { encoding: 'utf8' }).split(/\r?\n/).forEach(online)
    return lines
  }

  cb = once(cb)
  fs.createReadStream(filePath, { encoding: 'utf8' }) //.on('error', cb)
    .pipe(split())
    .pipe(through(online))
    .on('close', function () {
      cb(null, lines)
    })
    .on('error', cb)

  function online (line) {
    // Remove all comment text from the line
    var lineSansComments = line.replace(/#.*/, '')
    var matches = /^\s*?(.+?)\s+(.+?)\s*$/.exec(lineSansComments)
    if (matches && matches.length === 3) {
      // Found a hosts entry
      var ip = matches[1]
      var host = matches[2]
      lines.push([ip, host])
    } else {
      // Found a comment, blank line, or something else
      if (preserveFormatting) {
        lines.push(line)
      }
    }
  }
}
masx200 commented 1 hour ago

https://github.com/feross/hostile/blob/c99539ecf4003e11a299bb2217b524764b223e76/index.js#L34

masx200 commented 1 hour ago

the error should be caught