Skip to content
This repository was archived by the owner on Mar 21, 2026. It is now read-only.

Commit f6a794d

Browse files
committed
refactor platform slightly, expose to module
1 parent fd4edd8 commit f6a794d

File tree

2 files changed

+87
-30
lines changed

2 files changed

+87
-30
lines changed

dns.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222

2323
"use strict";
2424

25+
exports.platform = require('./lib/platform');
26+
2527
exports.createServer = require('./lib/server').createServer;
2628
exports.createUDPServer = require('./lib/server').createUDPServer;
2729
exports.createTCPServer = require('./lib/server').createTCPServer;

lib/platform.js

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,62 +28,117 @@ var fs = require('fs'),
2828
util = require('util');
2929

3030
var Platform = function () {
31-
this.name_server_ready = false;
32-
this.hosts_file_ready = false;
31+
this._nsReady = false;
32+
this._hostsReady = false;
3333

3434
Object.defineProperty(this, 'ready', {
3535
get: function () {
36-
return this.name_server_ready && this.hosts_file_ready;
36+
return this._nsReady && this._hostsReady;
3737
},
3838
});
3939

40-
this.initNameServers();
41-
this.initHostsFile();
42-
this.populate();
40+
this._watches = {};
41+
42+
Object.defineProperty(this, 'watching', {
43+
get: function () {
44+
return Object.keys(this._watches).length > 0;
45+
},
46+
set: function (value) {
47+
if (value)
48+
this._watchFiles();
49+
else {
50+
for (k in this._watches) {
51+
this._watches[k].close();
52+
delete this._watches[k];
53+
}
54+
}
55+
},
56+
});
57+
58+
this._initNameServers();
59+
this._initHostsFile();
60+
this._populate();
4361
};
4462
util.inherits(Platform, EventEmitter);
4563

46-
Platform.prototype.initNameServers = function () {
47-
this.name_server_ready = false;
64+
Platform.prototype.realod = function () {
65+
this.emit('unready');
66+
this._initNameServers();
67+
this._initHostsFile();
68+
this._populate();
69+
};
70+
71+
Platform.prototype._initNameServers = function () {
72+
this._nsReady = false;
4873
this.name_servers = [];
4974
this.search_path = [];
5075
this.timeout = 5 * 1000;
5176
this.attempts = 5;
5277
this.edns = false;
5378
};
5479

55-
Platform.prototype.initHostsFile = function () {
56-
this.hosts_file_ready = false;
80+
Platform.prototype._initHostsFile = function () {
81+
this._hostsReady = false;
5782
this.hosts = {};
5883
};
5984

60-
Platform.prototype.populate = function () {
85+
Platform.prototype._populate = function () {
6186
var hostsfile, self = this;
6287

6388
switch (os.platform()) {
89+
case 'win32':
90+
this.name_servers = [
91+
{
92+
address: '8.8.8.8',
93+
port: 53,
94+
},
95+
{
96+
address: '8.8.4.4.',
97+
port: 53,
98+
},
99+
];
100+
hosts = path.join(process.env.SystemRoot, '\\System32\\drivers\\etc\\hosts');
101+
break;
64102
default:
65-
fs.watchFile('/etc/resolv.conf', {persistent: false}, function (cur, prev) {
66-
if (cur.mtime !== prev.mtime) {
67-
self.initNameServers();
68-
self.parseResolv();
69-
}
70-
});
71103
this.parseResolv();
72104
hostsfile = '/etc/hosts';
73105
break;
74106
}
75107

76-
fs.watchFile(hostsfile, { persistent: false }, function (cur, prev) {
77-
if (cur.mtime !== prev.mtime) {
78-
self.initHostsFile();
79-
self.parseHosts(hostsfile);
80-
}
81-
});
108+
this._parseHosts(hostsfile);
109+
};
110+
111+
Platform.prototype._watchFiles = function () {
112+
var self = this, watchParams;
113+
114+
watchParams = {
115+
persistent: false,
116+
};
82117

83-
this.parseHosts(hostsfile);
118+
switch (os.platform()) {
119+
case 'win32':
120+
//TODO XXX FIXME: it would be nice if this existed
121+
break;
122+
default:
123+
this._watches.resolve = fs.watch('/etc/resolv.conf', watchParams, function (event, filename) {
124+
if (event === 'change') {
125+
self.emit('unready');
126+
self._initNameServers();
127+
self.parseResolv();
128+
}
129+
});
130+
this._watches.hosts = fs.watch('/etc/hosts', watchParams, function (event, filename) {
131+
if (event === 'change') {
132+
self.emit('unready');
133+
self._initHostsFile();
134+
self._parseHosts(hostsfile);
135+
}
136+
});
137+
break;
138+
}
84139
};
85140

86-
Platform.prototype.checkReady = function () {
141+
Platform.prototype._checkReady = function () {
87142
if (this.ready) {
88143
this.emit('ready');
89144
}
@@ -135,12 +190,12 @@ Platform.prototype.parseResolv = function () {
135190
}
136191
});
137192

138-
self.name_server_ready = true;
139-
self.checkReady();
193+
self._nsReady = true;
194+
self._checkReady();
140195
});
141196
};
142197

143-
Platform.prototype.parseHosts = function (hostsfile) {
198+
Platform.prototype._parseHosts = function (hostsfile) {
144199
var self = this;
145200

146201
fs.readFile(hostsfile, 'ascii', function (err, file) {
@@ -164,8 +219,8 @@ Platform.prototype.parseHosts = function (hostsfile) {
164219
}
165220
});
166221

167-
self.hosts_file_ready = true;
168-
self.checkReady();
222+
self._hostsReady = true;
223+
self._checkReady();
169224
});
170225
};
171226

0 commit comments

Comments
 (0)