Juul/tail-stream

Crash on end()

Opened this issue · 1 comments

Hello,

I met an error with this plugin : When the .end() event is called before the end of the file is reached (and so when the watcher is not started yet), an error happens :

/home/dagrut/myproject/node_modules/tail-stream/index.js:155
            this.watcher.close();
                        ^

TypeError: Cannot read property 'close' of undefined
    at TailStream.end (/home/dagrut/myproject/node_modules/tail-stream/index.js:155:25)

[...]

The problem is here :

    this.end = function(errCode) {
        if(errCode != 'EBADF') {
            fs.close(this.fd);
        }
        this.push(null);
        if(this.watcher === true) {
            fs.unwatchFile(this.path, this.watchFileCallback);
        } else {
            this.watcher.close();
        }
    };

the .close() method is called on watcher, which is undefined when the end of the file was not reached before. Checking for undefined (or using instanceof or typeof) should fix this issue.

I don't have enough time to make a pull request, sorry!

(seems to have been patched?)