-
-
Save SQLServerIO/1757691 to your computer and use it in GitHub Desktop.
A utility object that caches inbound HTTP data, allow you to attach your event handlers after you make other asynchronous requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function StreamBuffer(req) { | |
var self = this | |
var buffer = [] | |
var ended = false | |
var ondata = null | |
var onend = null | |
self.ondata = function(f) { | |
for(var i = 0; i < buffer.length; i++ ) { | |
f(buffer[i]) | |
} | |
ondata = f | |
} | |
self.onend = function(f) { | |
onend = f | |
if( ended ) { | |
onend() | |
} | |
} | |
req.on('data', function(chunk) { | |
if( ondata ) { | |
ondata(chunk) | |
} | |
else { | |
buffer.push(chunk) | |
} | |
}) | |
req.on('end', function() { | |
ended = true | |
if( onend ) { | |
onend() | |
} | |
}) | |
req.streambuffer = self | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment