-
Notifications
You must be signed in to change notification settings - Fork 457
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
Allow json
to be called multiple times
#129
Conversation
@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 |
@@ -45,7 +45,9 @@ async function json(req, {limit = '1mb'} = {}) { | |||
const type = req.headers['content-type'] | |||
const length = req.headers['content-length'] | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should use a WeakMap
so that we don't touch req
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where would you store the WeakMap
? Just outside the function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep
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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good 👍
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
json
function, while wrapping another function that calls thejson
function as well, e.g.The call to
json
by the wrapper would block the next call indefinitely, since the stream fromreq
has already been read.This PR solves that.