stream-utils / raw-body

Get and validate the raw body of a readable stream

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

raw-body will hang when the param is a parsed body

ziyofun opened this issue · comments

Hi, thanks for your work on this repo.
I use raw-body and koa-joi-router which depends on raw-body in my project the same time.
I found that the body already parsed can not emit any listener in readStream function of raw-body even if the stream.readable is false.
It may be better if throw some error of just return the parsed body this situation.

That sounds like a good idea. Even #32 getting implemented would help here as well, since the timeout would trigger in that situation.

Do you want to make a pull request or provide code that I can run which demonstrates the issue so I can make the change?

OK, I create a PR #58 to check the validation of stream but I dont think timeout is a good solution, it maybe means more work to distinguish error and just timeout.

I'm having a similar issue while using raw-body in Node 12.10 with my in-house body parser for Koa:

app.use(async (ctx, next) => {
  const isJSON = ctx.is('json');
  const options = {
    limit: 1024 * 512,
  };
  if (isJSON) options.encoding = 'utf8';
  ctx.request.rawBody = await raw(ctx.req, options);
  ctx.request.body = ctx.request.rawBody;
  if (isJSON) {
    try {
      ctx.request.body = JSON.parse(ctx.request.rawBody);
    } catch (e) {
      ctx.throw(400);
    }
  }
  return next();
});

POST requests just hang there every now and then and it only happens on GCE instance when deployed, never on localhost .

EDIT: Apparently, only happens with HTTP/2.