I have a VERY simple Node.js script that watches a directory called 'in' and when a file is dropped in there it gzip's it to 'out', then removes (unlinks) the original file... however at the unlink stage I get an error emitted from the original input stream.
var fs = require('fs');
var zlib = require('zlib');
fs.watch(__dirname + '/in', function (event, filename) {
var gzip = zlib.createGzip();
var inputPath = 'in/' + filename;
var outputPath = 'out/' + filename + '.gz';
var inputStream = fs.createReadStream(inputPath);
var outputStream = fs.createWriteStream(outputPath);
outputStream.on('finish', function () {
fs.unlink(inputPath);
});
inputStream.on('error', function (err) {
console.dir(err); // Error is thrown here
});
inputStream
.pipe(gzip)
.pipe(outputStream);
});
The error is this:
{ [Error: ENOENT, open 'in/test.gif'] errno: 34, code: 'ENOENT', path: 'in/test.gif' }
Which indicates the file no longer exists, however I would have thought the inputStream would have finished with the file by the time outputStream's finish
event is fired...
Any help here to clarify my understanding of streams would be appreciated.