Detecting successful read stream open

2019-07-16 08:31发布

I'm implementing cache for static serving middleware for Express.js, which works as follows — when request comes, middleware first tries to serve file from filesystem, and if there is none, file is fetched from upstream and stored in file system.

Problem is I don't know how to properly detect “cache hit” event.

staticMiddleware = function(req, res, next) {
    // try to read file from fs
    filename = urlToFilename(req.url);
    stream = fs.createReadStream(filename);

    // cache miss - file not found
    stream.on('error', function() {
        console.log('miss ' + req.url);

        // get file from upstream, store it into fs and serve as response
        stream = fetchFromUpstream(url);
        stream.pipe(fs.createWriteStream(filename));
        stream.pipe(res);
    });

    // cache hit - file is being read
    I_DONT_KNOW_WHAT_TO_PUT_HERE(function() {
        console.log('hit ' + req.url);

        stream.pipe(res);
    });
}

So, basically, how can I detect succesful file reading? If I listen to 'data' event, I guess I miss first chunk of data. If I just pipe() it to response, response stream gets closed on error, and I can't serve it with fetched data, and this approach really lacks flexibility. I wonder if there is way to listen for event like fdcreated or opened or similar, or way to push back data I've got in data event, so it will be resent in next data event.

1条回答
贪生不怕死
2楼-- · 2019-07-16 09:17

Method createReadStream returns a ReadableStream which also an event open. You can add an event handler for the open event so you will know when the resource is valid before piping:

stream.on('open', function() {
    console.log('hit ' + req.url);
    stream.pipe(res);
});
查看更多
登录 后发表回答