progression callbacks #72

Closed
isaacs opened this Issue Sep 4, 2011 · 6 comments

Projects

None yet

5 participants

@isaacs
Contributor
isaacs commented Sep 4, 2011

For wiring up to progress bars, it'd be nice to have insight into:

  1. How much of the total expected request body has been uploaded.
  2. How much of the total expected response body has been downloaded.

HTTP requests can look at the Content-Length, and fs streams can stat the file and check the size.

This will make it much easier to integrate with things like https://github.com/substack/node-multimeter

@mikeal
Member
mikeal commented Sep 4, 2011

can't you just listen to the data event?

On Sep 4, 2011, at 2:29 PM, isaacsreply@reply.github.com wrote:

For wiring up to progress bars, it'd be nice to have insight into:

  1. How much of the total expected request body has been uploaded.
  2. How much of the total expected response body has been downloaded.

HTTP requests can look at the Content-Length, and fs streams can stat the file and check the size.

This will make it much easier to integrate with things like https://github.com/substack/node-multimeter

Reply to this email directly or view it on GitHub:
#72

@isaacs
Contributor
isaacs commented Sep 4, 2011

Yeah, but it's a bit clunkier. I'll try playing around with that and probably come back with a more fleshed-out suggestion here.

I guess it's not that much of a difference, since you still need to manage a lot of stuff.

var req = request.put("http://foo.com")
var upload = fs.createReadStream("send.txt")
var resp = fs.createWriteStream("result.txt")
var mult = multimeter(process)

mult.drop(function (bar) {
  req.on("requestprogress", function (p) {
    bar.percent(p * 100)
  })
})

// drop a bar for the response when that starts coming in
req.on("response", function (res) {
  mult.drop(function (bar) {
    req.on("responseprogress", function (p) {
      bar.percent(p * 100)
    })
  })
})

upload.pipe(req)
req.pipe(fs.createWriteStream("some-file"))

vs

var req = request.put("http://foo.com")
var upload = fs.createReadStream("send.txt")
var resp = fs.createWriteStream("result.txt")
var mult = multimeter(process)

mult.drop(function (bar) {
  var seen = 0
  upload.on("data", function (chunk) {
    seen += chunk.length
    bar.percent(seen / upload.size * 100)
  })
})

// drop a bar for the response when that starts coming in
req.on("response", function (res) {
  if (res.headers["content-length"]) {
    mult.drop(function (bar) {
      var seen = 0
      req.on("data", function (chunk) {
        seen += chunk.length
        bar.percent(seen / res.headers["content-length"] * 100)
      })
    })
  }
})

upload.pipe(req)
req.pipe(fs.createWriteStream("some-file"))
@mikeal
Member
mikeal commented Feb 18, 2012

this landed right?

@mikeal mikeal closed this Feb 18, 2012
@miukki
miukki commented Dec 23, 2013

i using request.post() multipart/form-data but request.on('data')

r.on("response", function (res) {
console.log('res.headers', res.headers);
var seen = 0
r.on('data', function (chunk) {
seen += chunk.length;
console.log(seen);
});
});
didn't get me chunk.length

@mmalecki
Member

@miukki if you believe that your problem is a bug in request, please open a separate issue.

@naggie
naggie commented Jun 24, 2014

An alternative method mentioned in my comment on #315 : just have an attribute. Often I want to control the frequency I'm updating the bar myself, in case of a large file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment