stream-utils / raw-body

Get and validate the raw body of a readable stream

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

If the stream has already emitted its end event, raw-body never yeilds

carlbennettnz opened this issue · comments

getRawBody(res, function(err, string) {
    getRawBody(res, function(err, string) {
        console.log('never called')
    })
}

Or, more realistically, you're using app.use(bodyParser) and a library like json-api which both try to run getRawBody.

Not sure if it's raw-body's job to check the stream hasn't already ended, or the user's job to make sure he's not passing getRawBody an expired stream.

Hi! Sadly this is the way streams work in Node.js: once an event has been emitted, you cannot tell if you missed it or not. raw-body fits into the standard Node.js stream ecosystem and you will find this same behavior with pretty much any stream-based module.

The body-parser module in the 2.x branch (https://github.com/expressjs/body-parser/tree/2.x) will check if the stream has already ended before passing to this module, but it's breaking behavior, while is why the change is slated for 2.0 in that module.

In the meantime, you may want to move to the recommend usage of body-parser, which is not using it as a top-level app.use(), but rather only on the routes you need it (and thus, on routes that are not handled by other body parsers, like json-api). https://github.com/expressjs/body-parser#express-route-specific

Will do, thanks

No problem :) I also noticed that I had someone request for the 2.x body-parser to be possibly published to npm as a alpha/beta, so I may very well do that :)!