Skip to content

Commit

Permalink
Revert back to fs.watchFile(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Apr 28, 2020
1 parent 6ccfdcf commit e0a2e95
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions source/hosts-resolver.js
@@ -1,5 +1,6 @@
'use strict';
const {readFile, stat} = require('fs').promises;
const {watchFile} = require('fs');
const {readFile} = require('fs').promises;
const {isIP} = require('net');

const isWindows = process.platform === 'win32';
Expand All @@ -16,27 +17,6 @@ const whitespaceRegExp = /[^\S\r\n]{2,}/g;
const tabRegExp = /\t+/g;
const startsWithWhitespaceRegExp = /^[^\S\r\n]+/gm;

// TODO: Remove this when https://github.com/nodejs/node/issues/33096 gets fixed
const watchFile = (path, callback, onError) => {
let previousTime = null;

const interval = setInterval(async () => {
try {
const {mtimeMs} = await stat(path);

if (previousTime !== null && mtimeMs !== previousTime) {
callback(mtimeMs, previousTime);
}

previousTime = mtimeMs;
} catch (error) {
clearInterval(interval);

onError(error);
}
}, 1000 * 60).unref();
};

class HostsResolver {
constructor(customHostsPath = hostsPath) {
this._hostsPath = customHostsPath;
Expand All @@ -54,11 +34,13 @@ class HostsResolver {
return;
}

watchFile(this._hostsPath, (currentTime, previousTime) => {
if (currentTime > previousTime) {
watchFile(this._hostsPath, {
persistent: false
}, (current, previous) => {
if (current.mtime > previous.mtime) {
this._update();
}
}, error => {
}).once('error', error => {
this._error = error;
this._hosts = {};
});
Expand Down

0 comments on commit e0a2e95

Please sign in to comment.