forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.js
577 lines (501 loc) · 17.6 KB
/
watch.js
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
var fs = require("fs");
var path = require("path");
var _ = require('underscore');
var Future = require('fibers/future');
var fiberHelpers = require('./fiber-helpers.js');
// Watch for changes to a set of files, and the first time that any of
// the files change, call a user-provided callback. (If you want a
// second callback, you'll need to create a second Watcher.)
//
// You describe the structure you want to watch in a WatchSet; you then create a
// Watcher to watch it. Watcher does not mutate WatchSet, so you can create
// several Watchers from the same WatchSet. WatchSet can be easily converted to
// and from JSON for serialization.
//
// You can set up two kinds of watches, file and directory watches.
//
// In a file watch, you provide an absolute path to a file and a SHA1 (encoded
// as hex) of the contents of that file. If the file ever changes so that its
// contents no longer match that SHA1, the callback triggers. You can also
// provide `null` for the SHA1, which means the file should not exist.
//
// In a directory watch, you provide an absolute path to a directory,
// two lists of regular expressions specifying the entries to
// include and exclude, and an array of which entries to expect.
//
// For directory watches, the regular expressions work as follows. You provide
// two arrays of regular expressions, an include list and an exclude list. An
// entry in the directory matches if it matches at least one regular expression
// in the include list, and doesn't match any regular expressions in the exclude
// list. The string that is matched against the regular expression ends with a
// '/' if the entry is directory. There is NO IMPLICIT RECURSION here: a
// directory watch ONLY watches the immediate children of the directory! If you
// want a recursive watch, you need to do the recursive walk while building the
// WatchSet and add a bunch of separate directory watches.
//
// There can be multiple directory watches on the same directory. There is no
// relationship between the files found in directory watches and the files
// watched by file watches; they are parallel mechanisms.
//
// Regular expressions are checked only against individual path components (the
// actual name of the file or the subdirectory) plus the trailing '/' for
// directories, not against the entire path.
//
// You can call stop() to stop watching and tear down the
// watcher. Calling stop() guarantees that you will not receive a
// callback (if you have not already). Calling stop() is unnecessary
// if you've received a callback.
//
// To do a "one-shot" (to see if any files have been modified, compared to the
// dependencies, at a particular point in time), use the isUpToDate function.
//
// XXX Symlinks are currently treated transparently: we treat them as the thing
// they point to (ie, as a directory if they point to a directory, as
// nonexistent if they point to something nonexist, etc). Not sure if this is
// correct.
var WatchSet = function () {
var self = this;
// Set this to true if any Watcher built on this WatchSet must immediately
// fire (eg, if this WatchSet was given two different sha1 for the same file).
self.alwaysFire = false;
// Map from the absolute path to a file, to a sha1 hash, or null if the file
// should not exist. A Watcher created from this set fires when the file
// changes from that sha, or is deleted (if non-null) or created (if null).
self.files = {};
// Array of object with keys:
// - 'absPath': absolute path to a directory
// - 'include': array of RegExps
// - 'exclude': array of RegExps
// - 'contents': array of strings, or null if the directory should not exist
//
// This represents the assertion that 'absPath' is a directory and that
// 'contents' is its immediate contents, as filtered by the regular
// expressions. Entries in 'contents' are file and subdirectory names;
// directory names end with '/'. 'contents' is sorted. An entry is in
// 'contents' if its value (including the slash, for directories) matches at
// least one regular expression in 'include' and no regular expressions in
// 'exclude'.
//
// There is no recursion here: files contained in subdirectories never appear.
//
// A directory may have multiple entries (presumably with different
// include/exclude filters).
self.directories = [];
};
_.extend(WatchSet.prototype, {
addFile: function (filePath, hash) {
var self = this;
// No need to update if this is in always-fire mode already.
if (self.alwaysFire)
return;
if (_.has(self.files, filePath)) {
// Redundant?
if (self.files[filePath] === hash)
return;
// Nope, inconsistent.
self.alwaysFire = true;
return;
}
self.files[filePath] = hash;
},
// Takes options absPath, include, exclude, and contents, as described
// above. contents does not need to be pre-sorted.
addDirectory: function (options) {
var self = this;
if (self.alwaysFire)
return;
if (_.isEmpty(options.include))
return;
var contents = _.clone(options.contents);
if (contents)
contents.sort();
self.directories.push({
absPath: options.absPath,
include: options.include,
exclude: options.exclude,
contents: contents
});
},
// Merges another WatchSet into this one. This one will now fire if either
// WatchSet would have fired.
merge: function (other) {
var self = this;
if (self.alwaysFire)
return;
if (other.alwaysFire) {
self.alwaysFire = true;
return;
}
_.each(other.files, function (hash, name) {
self.addFile(name, hash);
});
_.each(other.directories, function (dir) {
// XXX this doesn't deep-clone the directory, but I think these objects
// are never mutated #WatchSetShallowClone
self.directories.push(dir);
});
},
clone: function () {
var self = this;
var ret = new WatchSet;
// XXX doesn't bother to deep-clone the directory info
// #WatchSetShallowClone
ret.alwaysFire = self.alwaysFire;
ret.files = _.clone(self.files);
ret.directories = _.clone(self.directories);
return ret;
},
toJSON: function () {
var self = this;
if (self.alwaysFire)
return {alwaysFire: true};
var ret = {files: self.files};
var reToJSON = function (r) {
var options = '';
if (r.ignoreCase)
options += 'i';
if (r.multiline)
options += 'm';
if (r.global)
options += 'g';
if (options)
return {$regex: r.source, $options: options};
return r.source;
};
ret.directories = _.map(self.directories, function (d) {
return {
absPath: d.absPath,
include: _.map(d.include, reToJSON),
exclude: _.map(d.exclude, reToJSON),
contents: d.contents
};
});
return ret;
}
});
WatchSet.fromJSON = function (json) {
var set = new WatchSet;
if (!json)
return set;
if (json.alwaysFire) {
set.alwaysFire = true;
return set;
}
set.files = _.clone(json.files);
var reFromJSON = function (j) {
if (_.has(j, '$regex'))
return new RegExp(j.$regex, j.$options);
return new RegExp(j);
};
set.directories = _.map(json.directories, function (d) {
return {
absPath: d.absPath,
include: _.map(d.include, reFromJSON),
exclude: _.map(d.exclude, reFromJSON),
contents: d.contents
};
});
return set;
};
var readDirectory = function (options) {
var yielding = !!options._yielding;
// Read the directory.
try {
var contents = readdirSyncOrYield(options.absPath, yielding);
} catch (e) {
// If the path is not a directory, return null; let other errors through.
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))
return null;
throw e;
}
// Add slashes to the end of directories.
var contentsWithSlashes = [];
_.each(contents, function (entry) {
try {
// We do stat instead of lstat here, so that we treat symlinks to
// directories just like directories themselves.
// XXX Does the treatment of symlinks make sense?
var stats = statSyncOrYield(path.join(options.absPath, entry), yielding);
} catch (e) {
if (e && (e.code === 'ENOENT')) {
// Disappeared after the readdirSync (or a dangling symlink)? Eh,
// pretend it was never there in the first place.
return;
}
throw e;
}
// XXX if we're on windows, I guess it's possible for files to end with '/'.
if (stats.isDirectory())
entry += '/';
contentsWithSlashes.push(entry);
});
// Filter based on regexps.
var filtered = _.filter(contentsWithSlashes, function (entry) {
return _.any(options.include, function (re) {
return re.test(entry);
}) && !_.any(options.exclude, function (re) {
return re.test(entry);
});
});
// Sort it!
filtered.sort();
return filtered;
};
// All fields are private.
var Watcher = function (options) {
var self = this;
// The set to watch.
self.watchSet = options.watchSet;
if (! self.watchSet)
throw new Error("watchSet option is required");
// Function to call when a change is detected according to one of
// the above.
self.onChange = options.onChange;
if (! self.onChange)
throw new Error("onChange option is required");
self.stopped = false;
self.justCheckOnce = !!options._justCheckOnce;
self.fileWatches = []; // array of paths
// We track all of the currently active timers so that we can cancel
// them at stop() time. This stops the process from hanging at
// shutdown until all of the timers have fired. An alternate
// approach would be to use the unref() timer handle method present
// in modern node.
var nextTimerId = 1;
self.timers = {}; // map from arbitrary number (nextTimerId) to timer handle
// Were we given an inconsistent WatchSet? Fire now and be done with it.
if (self.watchSet.alwaysFire) {
self._fire();
return;
}
self._startFileWatches();
self._checkDirectories();
};
_.extend(Watcher.prototype, {
_fireIfFileChanged: function (absPath) {
var self = this;
if (self.stopped)
return true;
var oldHash = self.watchSet.files[absPath];
if (oldHash === undefined)
throw new Error("Checking unknown file " + absPath);
var contents = readFile(absPath);
if (contents === null) {
// File does not exist (or is a directory).
// Is this what we expected?
if (oldHash === null)
return false;
// Nope, not what we expected.
self._fire();
return true;
}
// File exists! Is that what we expected?
if (oldHash === null) {
self._fire();
return true;
}
var newHash = sha1(contents);
// Unchanged?
if (newHash === oldHash)
return false;
self._fire();
return true;
},
_fireIfDirectoryChanged: function (info, yielding) {
var self = this;
if (self.stopped)
return true;
var newContents = exports.readDirectory({
absPath: info.absPath,
include: info.include,
exclude: info.exclude,
_yielding: yielding
});
// If the directory has changed (including being deleted or created).
if (!_.isEqual(info.contents, newContents)) {
self._fire();
return true;
}
return false;
},
_startFileWatches: function () {
var self = this;
// Set up a watch for each file
_.each(self.watchSet.files, function (hash, absPath) {
if (self.stopped)
return;
// Check for the case where by the time we created the watch,
// the file had already changed from the sha we were provided.
if (self._fireIfFileChanged(absPath))
return;
if (self.justCheckOnce)
return;
// Intentionally not using fs.watch since it doesn't play well with
// vim (https://github.com/joyent/node/issues/3172)
// Note that we poll very frequently (500 ms)
fs.watchFile(absPath, {interval: 500}, function () {
// Fire only if the contents of the file actually changed (eg,
// maybe just its atime changed)
self._fireIfFileChanged(absPath);
});
self.fileWatches.push(absPath);
});
if (self.stopped || self.justCheckOnce)
return;
// One second later, check the files again, because fs.watchFile
// is actually implemented by polling the file's mtime, and some
// filesystems (OSX HFS+) only keep mtimes to a resolution of one
// second. This handles the case where we check the hash and set
// up the watch, but then the file change before the clock rolls
// over to the next second, and fs.watchFile doesn't notice and
// doesn't call us back. #WorkAroundLowPrecisionMtimes
var timerId = self.nextTimerId++;
self.timers[timerId] = setTimeout(function () {
delete self.timers[timerId];
_.each(self.watchSet.files, function (hash, absPath) {
self._fireIfFileChanged(absPath);
});
}, 1000);
},
_checkDirectories: function (yielding) {
var self = this;
// fs.watchFile doesn't work for directories (as tested on ubuntu)
// and fs.watch has serious issues on MacOS (at least in node 0.10)
// https://github.com/meteor/meteor/issues/1483
// https://groups.google.com/forum/#!topic/meteor-talk/Zy1XxEcxe8o
// https://github.com/joyent/node/issues/5463
// https://github.com/joyent/libuv/commit/38df93cf
//
// Instead, just check periodically with setTimeout. (We use setTimeout to
// ensure that there is a 500 ms pause between the *end* of one poll cycle
// and the *beginning* of another instead of using setInterval which still
// can lead to permanent 100% CPU usage.) When node has a stable directory
// watching API that is more efficient than just polling, look at the
// history for this file around release 0.6.5 for a version that uses
// fs.watch.
if (self.stopped)
return;
_.each(self.watchSet.directories, function (info) {
if (self.stopped)
return;
// Check for the case where by the time we created the watch, the
// directory has already changed.
if (self._fireIfDirectoryChanged(info, yielding))
return;
});
if (!self.stopped && !self.justCheckOnce) {
setTimeout(fiberHelpers.inBareFiber(function () {
self._checkDirectories(true);
}), 500);
}
},
_fire: function () {
var self = this;
if (self.stopped)
return;
self.stop();
self.onChange();
},
stop: function () {
var self = this;
self.stopped = true;
// Clean up timers
_.each(self.timers, function (timer, id) {
clearTimeout(timer);
});
self.timers = {};
// Clean up file watches
_.each(self.fileWatches, function (absPath) {
fs.unwatchFile(absPath);
});
self.fileWatches = [];
}
});
// Given a WatchSet, returns true if it currently describes the state of the
// disk.
var isUpToDate = function (watchSet) {
var upToDate = true;
var watcher = new Watcher({
watchSet: watchSet,
onChange: function () {
upToDate = false;
},
// internal flag which prevents us from starting watches and timers that
// we're about to cancel anyway
_justCheckOnce: true
});
watcher.stop();
return upToDate;
};
// Options should have absPath/include/exclude.
var readAndWatchDirectory = function (watchSet, options) {
var contents = readDirectory(options);
watchSet.addDirectory(_.extend({contents: contents}, options));
return contents;
};
// Calculating the sha hash can be expensive for large files. By
// returning the calculated hash along with the file contents, the
// hash doesn't need to be calculated again for static files.
//
// We only calculate the hash if needed here, so callers must not
// *rely* on the hash being returned; merely that if the hash is
// present, it is the correct hash of the contents.
var readAndWatchFileWithHash = function (watchSet, absPath) {
var contents = readFile(absPath);
var hash = null;
// Allow null watchSet, if we want to use readFile-style error handling in a
// context where we might not always have a WatchSet (eg, reading
// settings.json where we watch for "meteor run" but not for "meteor deploy").
if (watchSet) {
hash = contents === null ? null : sha1(contents);
watchSet.addFile(absPath, hash);
}
return {contents: contents, hash: hash};
};
var readAndWatchFile = function (watchSet, absPath) {
return readAndWatchFileWithHash(watchSet, absPath).contents;
};
var readFile = function (absPath) {
try {
return fs.readFileSync(absPath);
} catch (e) {
// Rethrow most errors.
if (!e || (e.code !== 'ENOENT' && e.code !== 'EISDIR'))
throw e;
// File does not exist (or is a directory).
return null;
}
};
var sha1 = function (contents) {
var crypto = require('crypto');
var hash = crypto.createHash('sha1');
hash.update(contents);
return hash.digest('hex');
};
// XXX We should eventually rewrite the whole meteor tools to use yielding fs
// calls instead of sync (so that meteor is responsive to C-c during bundling,
// so that the proxy accepts connections, etc) but we don't want to do this in
// the point release in which we are adding these functions.
var readdirSyncOrYield = function (path, yielding) {
if (yielding) {
return Future.wrap(fs.readdir)(path).wait();
} else {
return fs.readdirSync(path);
}
};
var statSyncOrYield = function (path, yielding) {
if (yielding) {
return Future.wrap(fs.stat)(path).wait();
} else {
return fs.statSync(path);
}
};
_.extend(exports, {
WatchSet: WatchSet,
Watcher: Watcher,
readDirectory: readDirectory,
isUpToDate: isUpToDate,
readAndWatchDirectory: readAndWatchDirectory,
readAndWatchFile: readAndWatchFile,
readAndWatchFileWithHash: readAndWatchFileWithHash
});