Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.watchFile fixes #2093

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ These stat objects are instances of `fs.Stat`.
If you want to be notified when the file was modified, not just accessed
you need to compare `curr.mtime` and `prev.mtime`.

_Note: when an `fs.watchFile` operation results in an `ENOENT` error, it will
invoke the callback once. This is a change in functionality since v0.10._

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that it would be better if we can have an example here to show how to differentiate between ENONET case and the normal case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, but #2093 (comment).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @thefourtheye

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we don't mention the changed versions in the docs I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a doc that mentions a change in versions:

Prior to io.js v1.0 and Node v0.12, the ctime held the birthtime on Windows systems. Note that as of v0.12, ctime is not "creation time", and on Unix systems, it never was.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is an instance of fs.Stat, I think that would belong there, rather than in the fs.watchFile docs. Perhaps this should be done in a separate PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ya, it makes sense.

_Note: `fs.watch` is more efficient than `fs.watchFile` and `fs.unwatchFile`.
`fs.watch` should be used instead of `fs.watchFile` and `fs.unwatchFile`
when possible._
Expand Down
15 changes: 7 additions & 8 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1305,28 +1305,27 @@ StatWatcher.prototype.stop = function() {

const statWatchers = new Map();

fs.watchFile = function(filename) {
fs.watchFile = function(filename, options, listener) {
nullCheck(filename);
filename = pathModule.resolve(filename);
var stat;
var listener;

var options = {
var defaults = {
// Poll interval in milliseconds. 5007 is what libev used to use. It's
// a little on the slow side but let's stick with it for now to keep
// behavioral changes to a minimum.
interval: 5007,
persistent: true
};

if (arguments[1] !== null && typeof arguments[1] === 'object') {
options = util._extend(options, arguments[1]);
listener = arguments[2];
if (options !== null && typeof options === 'object') {
options = util._extend(defaults, options);
} else {
listener = arguments[1];
listener = options;
options = defaults;
}

if (!listener) {
if (typeof listener !== 'function') {
throw new Error('watchFile requires a listener function');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit late, since already merged, but shouldn't this be a TypeError?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never too late :) you're right though, if you wanted to submit a PR I'd review it.

}

Expand Down
26 changes: 26 additions & 0 deletions test/parallel/test-fs-watchfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const fs = require('fs');
const path = require('path');
const assert = require('assert');
const common = require('../common');
const fixtures = path.join(__dirname, '..', 'fixtures');

// Basic usage tests.
assert.throws(function() {
fs.watchFile('./some-file');
}, /watchFile requires a listener function/);

assert.throws(function() {
fs.watchFile('./another-file', {}, 'bad listener');
}, /watchFile requires a listener function/);

assert.throws(function() {
fs.watchFile(new Object(), function() {});
}, /Path must be a string/);

// Test ENOENT. Should fire once.
const enoentFile = path.join(fixtures, 'empty', 'non-existent-file');
fs.watchFile(enoentFile, common.mustCall(function(curr, prev) {
fs.unwatchFile(enoentFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, we are not doing inode check to confirm to see if the file really doesn't exist. Meh. Its okay I guess. LGTM 👍

}));