Detecting successful read stream open

陌路散爱 提交于 2020-01-04 12:20:13

问题


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:


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);
});


来源:https://stackoverflow.com/questions/24349896/detecting-successful-read-stream-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!