Allow json to be called multiple times#129
Allow json to be called multiple times#129timneutkens merged 7 commits intovercel:masterfrom onbjerg:patch-1
json to be called multiple times#129Conversation
|
@rauchg I monkey patched this in micro-dev. What would you prefer? If it's in micro core, then please do merge 😄 |
|
@timneutkens It shouldn't be a monkeypatch since it's true for all wrapper functions that need to use |
lib/index.js
Outdated
| const encoding = typer.parse(type).parameters.charset | ||
| const str = await getRawBody(req, {limit, length, encoding}) | ||
|
|
||
| req.rawBody = req.rawBody || getRawBody(req, {limit, length, encoding}) |
There was a problem hiding this comment.
Maybe we should use a WeakMap so that we don't touch req?
There was a problem hiding this comment.
Where would you store the WeakMap? Just outside the function?
|
This is a pretty big semantic change. We would have to document it. |
|
Sure, I'll document it, too. |
|
Made a little clarification in the code. Let me know what you think |
|
Yeah, I think it makes sense to add a comment to clarify what's happening. I'm fixing the test. |
|
I documented the behavior. Any thoughts? |
|
Thing I'm wondering, should we add a |
|
@timneutkens You can read the raw body with other modules from the request, since bodies can be formatted in different ways and thus are parsed differently. I don't think having a But maybe I'm wrong |
|
Yeah it's just the case of calling |
|
@timneutkens Is it even possible to have multiple bodies in the same request? I don't think I'm following |
|
I was explaining a pretty far fetched use case where you'd want to do something with the request body if json parsing fails. But nvm that, it's not important 👍 |
Consider a wrapper function that calls the
jsonfunction, while wrapping another function that calls thejsonfunction as well, e.g.The call to
jsonby the wrapper would block the next call indefinitely, since the stream fromreqhas already been read.This PR solves that.