Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async setHeaders callback #21

Closed
amasad opened this issue Oct 27, 2014 · 4 comments
Closed

async setHeaders callback #21

amasad opened this issue Oct 27, 2014 · 4 comments
Labels

Comments

@amasad
Copy link
Contributor

amasad commented Oct 27, 2014

Most API's in express (and node) are async, but setHeaders callback here isn't. Here is a current use case that I have:

setHeaders: function(path, res) {
  fs.stat(path, (e, stat) => res.set('X-Raw-Length', stat.size));
}

I'm doing this to show a progress bar on the client-side when loading a big resource. Content-length won't work when the content is gzipped.

Of course, this doesn't work because by the time fs.stat comes back the response might have ended.

@dougwilson
Copy link
Contributor

The way the send module works, it is just sync. Besides, the Content-Length header at this time is never the gzip'd length, always that from the disk, as this module and below does not gzip compression, so I'm not sure why you'd have to worry about getting the gzip'd length.

But even then, it looks like you just want to do something using the file's stat, which is actually the third argument. Could you do this?

setHeaders: function(res, path, stat) {
  res.set('X-Raw-Length', stat.size)
}

@amasad
Copy link
Contributor Author

amasad commented Oct 27, 2014

I use the compression module and the Content-Length is the gzip'd length. Also generally Content-Length wouldn't be reliable for my use case because some files would be streamed (Transfer-Encoding:chunked).

But this works perfectly! Thanks for the super quick reply. I'll update the documentation to add the stat arg.

@dougwilson
Copy link
Contributor

Ah. I took a closer look into the send module and see that it doesn't even set the Content-Length until after the setHeaders anyway (this isn't really an issue, because, really, it's for setting headers in a particular pipeline, not really for altering headers).

Ans the reason it's sync instead of async is because send is a stream and this function is run as a EE listener, and EE listeners are only sync in Node.js, there is no async version of listeners, haha.

But thanks for the docs contribution, and I'm glad we could solve your problem with something that was just accidentally not documented (it's documented all the way down in send module)!

@amasad
Copy link
Contributor Author

amasad commented Oct 27, 2014

Happy to help. These projects are getting even better than last time I used them. You seem to run a tight ship :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants