Skip to content

Commit 1afc0c9

Browse files
committedJul 10, 2015
fs: fix error on bad listener type
When the listener was truthy but NOT a function, fs.watchFile would throw an error through the EventEmitter. This caused a problem because it would only be thrown after the listener was started, which left the listener on. There should be no backwards compatability issues because the error was always thrown, just in a different manner. Also adds tests for this and other basic functionality. PR-URL: #2093 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 12bc397 commit 1afc0c9

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed
 

‎lib/fs.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -1305,28 +1305,27 @@ StatWatcher.prototype.stop = function() {
13051305

13061306
const statWatchers = new Map();
13071307

1308-
fs.watchFile = function(filename) {
1308+
fs.watchFile = function(filename, options, listener) {
13091309
nullCheck(filename);
13101310
filename = pathModule.resolve(filename);
13111311
var stat;
1312-
var listener;
13131312

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

1322-
if (arguments[1] !== null && typeof arguments[1] === 'object') {
1323-
options = util._extend(options, arguments[1]);
1324-
listener = arguments[2];
1321+
if (options !== null && typeof options === 'object') {
1322+
options = util._extend(defaults, options);
13251323
} else {
1326-
listener = arguments[1];
1324+
listener = options;
1325+
options = defaults;
13271326
}
13281327

1329-
if (!listener) {
1328+
if (typeof listener !== 'function') {
13301329
throw new Error('watchFile requires a listener function');
13311330
}
13321331

‎test/parallel/test-fs-watchfile.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const assert = require('assert');
5+
6+
// Basic usage tests.
7+
assert.throws(function() {
8+
fs.watchFile('./some-file');
9+
}, /watchFile requires a listener function/);
10+
11+
assert.throws(function() {
12+
fs.watchFile('./another-file', {}, 'bad listener');
13+
}, /watchFile requires a listener function/);
14+
15+
assert.throws(function() {
16+
fs.watchFile(new Object(), function() {});
17+
}, /Path must be a string/);

0 commit comments

Comments
 (0)