Skip to content

Instantly share code, notes, and snippets.

@stash
Created August 30, 2011 21:29
Show Gist options
  • Save stash/1182108 to your computer and use it in GitHub Desktop.
Save stash/1182108 to your computer and use it in GitHub Desktop.
Strange .pause() behaviour for http client response stream
var fs = require('fs'),
http = require('http');
var ws = fs.createWriteStream('/tmp/get.png',{flags:'w'});
var reqOpts = {
method: 'GET',
path: '/images/nav_logo83.png',
host: 'www.google.ca'
};
var req = http.request(reqOpts, function (res) {
var total = 0, total2 = 0;
console.log(res.headers);
res.once('error', function (err) {
console.log(err);
process.exit(1);
});
res.pause(); // this doesn't work
res.on('data',function (chunk) {
total += chunk.length;
console.log("UNPAUSED chunk len "+chunk.length);
});
res.pause(); // this doesn't work either
setTimeout(function () {
console.log("\nproceeding\n");
res.on('data', function (chunk) { total2 += chunk.length; console.log("chunk write len "+chunk.length); ws.write(chunk); });
res.once('end', function () {
console.log("END",total,total2);
ws.end();
res.removeAllListeners('data');
});
res.resume();
}, 1000);
});
req.once('error', function (err) {
console.log(err);
process.exit(1);
});
req.end();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment