How to set a timeout specifically dealing with the absence of a continue event for a node.js http.ClientRequest object

Problem

I am trying to configure a client in node.js that will send data after several seconds if a 100-continue response is not received, but was requested by the client. The snippet below does that. However, if there is for some reason inactivity on the socket after I receive a continue event and start sending data, but before I call req.end(), then again this timeout is hit and the code therein executed. As such, while this is useful to monitor activity on the socket after I start sending data, it's not good for the problem I'm trying to solve -- send the data even if I don't receive a continue event. I've scrubbed the node.js documentation, and while I found the section describing how to listen for a continue event, as well as the section describing how to set a timeout, I can't find any information on setting a timeout specifically when waiting for a continue request. Any bright ideas?

req.on('socket', function(socket) {
    var timeout = 5000
    socket.setTimeout(timeout);
    socket.on('timeout', function() {
        console.log('100-continue not received. Sending data anyway...');
        fs.createReadStream(filepath, byte_range)
            .pipe(req, {end: false}).on('end', function() {
                req.end();
            });
    });
});
req.on('continue', function() {
    console.log('100-continue received. Sending data...');
    fs.createReadStream(filepath, byte_range)
        .pipe(req, { end: false }).on('end', function() {
            req.end();
        });
});
Problem courtesy of: Clandestine

Solution

A simple solution would be to use a flag to determine if you've already sent the data:

var sentData = false;
var sendData = function(req) {
  if (sentData) return;
  sentData = true;
  fs.createReadStream(filepath, byte_range)
    .pipe(req, {end: false}).on('end', function() {
        req.end();
    });
};

req.on('socket', function(socket) {
    var timeout = 5000
    socket.setTimeout(timeout);
    socket.on('timeout', function() {
        console.log('100-continue not received. Sending data anyway...');
        sendData(req);
    });
});
req.on('continue', function() {
    console.log('100-continue received. Sending data...');
    sendData(req);
});
Solution courtesy of: Michelle Tilley

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.