-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
HttpServerResponse.setDefaultEncoding() #14146
Comments
Nor does |
If it makes sense to implement these non-existent functions both for From my understanding, something in the lines of |
@joaolucasl Guess this simple line won't suffice according to this note in docs on Class: http.ServerResponse:
Nonetheless, this statement is wrong since setDefaultEncoding(), cork() and uncork() are part of Writable interface. Aside from that I second this issue. Any fix might help me with issues I'm facing when piping from same source into ServerResponse and into fs.WriteStream with the former generating garbage (with some more bytes sent) and the latter resulting in valid output. EDIT: This fix would be useful in LTS release, too. |
I'd be happy to work on this issue this weekend. |
I was looking at this, and can confirm there's "extra bytes" when piping a readable stream into e.g., a This seems to be because To avoid this you can use e.g., I can't reproduce a situation in which a The headers are text, of course--and it's all the same stream--so I can only guess that whatever @shaunc is using may be confused by this? If I'm right, then this issue should be closed, as |
Interesting Discussion! and I agree with @soletan: I took a very close look at the source codes, found that: For ServerResponse, we've inherited from [_http_outgoing.js] function OutgoingMessage() {
Stream.call(this);
……
}
util.inherits(OutgoingMessage, Stream);
…… [_http_server.js] util.inherits(ServerResponse, OutgoingMessage); That's the reason why we cannot implement But if this is changed to implement from Considering this discussion has lasted for long ages without a confirmed result, and in order to make it clear, we have to invite related groups to have a discussion on it to see whether we should change the doc or codes there? /cc:@nodejs/streams, @nodejs/http. |
I have tried several times to make The solution is to implement all methods in |
@mcollina:Thanks for sharing ideas. And I see the post is lasting for ages with And to be honest:Because each request will create a new instance of ServerResponse, so we cannot use this method to re-use the default encoding, so BTW:Until now the doc says (as what @soletan says above):
Is it necessary to change the explaination of doc (because we haven't implemented I'd tend to change this to: The response inherits from [Stream][], with some methods like what we have in [Writable Stream][]. There's an EventEmitter with the following events…… If it's OK, I'd help to submit another PR for that :) |
Definitely. Also, make the same amendment to the HTTP2 docs. |
@mcollina :Thanks for your quick resp! I'll do that :) |
In short: `ServerResponse` acutally inherits from `OutgoingMessage`, with a series of methods like those in `Stream.Writable`. So we cannot use `implements`(this has made poeple feel puzzled because there are still many methods we don't need or have), so `inherits from Stream` is enough, due to some core reasons and performance told by mcollina from the ref (See some latest discussions at Ref). Ref: nodejs#14146. PR-URL: nodejs#22305 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
In short: `ServerResponse` acutally inherits from `OutgoingMessage`, with a series of methods like those in `Stream.Writable`. So we cannot use `implements`(this has made poeple feel puzzled because there are still many methods we don't need or have), so `inherits from Stream` is enough, due to some core reasons and performance told by mcollina from the ref (See some latest discussions at Ref). Ref: #14146. PR-URL: #22305 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
In short: `ServerResponse` acutally inherits from `OutgoingMessage`, with a series of methods like those in `Stream.Writable`. So we cannot use `implements`(this has made poeple feel puzzled because there are still many methods we don't need or have), so `inherits from Stream` is enough, due to some core reasons and performance told by mcollina from the ref (See some latest discussions at Ref). Ref: #14146. PR-URL: #22305 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
can this be closed, given #22305 is landed? |
Closing as it appears this is resolved. Can reopen if it turns out that it's not. |
The documentation of http.ServerResponse claims that it implements the interface of
stream.Writable
, which includessetDefaultEncoding
. However,ServerResponse
does not implementsetDefaultEncoding
.I'm passing a server response to another library, which pipes an archiver.zip to it. The binary data is interpreted as
utf8
. I should be able to avoid this by setting the default encoding.The text was updated successfully, but these errors were encountered: