Closed trylovetom closed 3 years ago
You should only respond when isLast is set. Http does not allow the server to refuse a big payload, you still have to receive it (but you can ignore it). That's as far as I know at least.
What if you use res.close()
instead? That one should "Immediately force closes the connection." as per doc.
If that doesn't work either (for whatever reason, since it should), just move data = Buffer.concat([data, Buffer.from(chunk)])
down a few lines, after the isAborted
check. That should do it as a workaround...
@mikeandtherest If use res.close()
instead of res.end()
, it will be close immediately. But the client side will not get any response like status code 413 Payload Too Large.
You can res.close in process.nextTick but that's a hacky solution. This feature is being tracked in the main repo.
I have thought about it and the most reliable and simple solution I can think of is to add res.endAndClose() as an alternative to res.end() where once drained, the connection is shutdown. You still have to ignore any further data you might get above your limit.
You can't really make good heuristics such as "if res.end() is called before seeing all data then make it res.endAndClose()" because some users simply ignore the data and in that case it would be really bad to close down the connection rather than keep it keep-alive. And in cases where you would like to stream back whatever data you got, it becomes messy.
I think the simplest solution is res.endAndClose(). The name is debatable - endWithClose?
Another solution is to have a second argument like res.end("data", shutdown true/false);
You can't really make good heuristics such as "if res.end() is called before seeing all data then make it res.endAndClose()"
I totally agree that no assumptions should be made, since they could indeed start as good intentions but can end up as restrictions and make the whole thing less flexible.
I think the simplest solution is res.endAndClose(). The name is debatable - endWithClose?
Another solution is to have a second argument like res.end("data", shutdown true/false);
Tbh I would look at it from the opposite angle, and by that I mean that what I think it's missing is a "close with a response/payload". So maybe res.closeWithResponse()
or res.closeWithData()
could be better?
Those are good names but they can be difficult to connect with "end" which is really the same thing. I ended up making it res.end("data", true) for closeConnection since I did not really want to add more functions than needed. This is committed and working in main repo now, so next js release should have it supported.
Ok, as long as the default value for the new arg is false (which I see it's already the case in your commit), I think we're all good since it fixes the problem while also keeping it safe for existing code. Thx!
Expected Behavior
When res.end is is executed, res.onData event will not be emit.
Actual Behavior
I sent the sample image (3.9mb), after res.end is is executed, res.onData still emit and receive the data from client.
server
client
How to Reproduce
httpie command
my code
smaple.jpg
Environment