Skip to content

HttpServerResponse.setDefaultEncoding() #14146

Description

@shaunc
  • Version: 7.10.0
  • Platform: mac
  • Subsystem: http

The documentation of http.ServerResponse claims that it implements the interface of stream.Writable, which includes setDefaultEncoding. However, ServerResponse does not implement setDefaultEncoding.

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.

Activity

  1. added
    httpIssues and PRs related to the http subsystem.
    on Jul 9, 2017
  2. TimothyGu commented on Jul 10, 2017

    @TimothyGu
    Member

    Nor does ServerResponse or OutgoingMessage implement cork or uncork...

  3. joaolucasl commented on Aug 9, 2017

    @joaolucasl
    Contributor

    If it makes sense to implement these non-existent functions both for ServerResponse and for OutgoingMessage, I'd like to take a crack at it!

    From my understanding, something in the lines of util.inherits(ServerResponse, stream.Writable); would suffice?

  4. soletan commented on Aug 11, 2017

    @soletan

    @joaolucasl Guess this simple line won't suffice according to this note in docs on Class: http.ServerResponse:

    The response implements, but does not inherit from, the Writable Stream interface. This is an EventEmitter with the following events:

    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.

  5. added
    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.
    on Apr 13, 2018
  6. mick-io commented on Apr 27, 2018

    @mick-io

    I'd be happy to work on this issue this weekend.

  7. boneskull commented on Jul 31, 2018

    @boneskull
    Member

    I was looking at this, and can confirm there's "extra bytes" when piping a readable stream into e.g., a ServerResponse and process.stdout (to @soletan's) comment.

    This seems to be because Transfer-encoding: chunked (wiki) is set by default (for response codes other than 204 and 304, anyway).

    To avoid this you can use e.g., serverResponse.removeHeader('transfer-encoding').

    I can't reproduce a situation in which a ReadableStream containing a Buffer piped into a ServerResponse inexplicably becomes a string.

    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 setDefaultEncoding() isn't the problem. @apapirovski what do you think?

  8. mcollina commented on Aug 12, 2018

    @mcollina
    SponsorMember

    I have tried several times to make OutgoingMessage a direct descendant of Stream.Writable. Unfortunately, doing so would cause a significant (X0%) degradation of throughput for HTTP servers. I don't think that's acceptable. We end up doing this because otherwise we would have double-buffering in OutgoingMessage  and the Socket, which is costly.

    The solution is to implement all methods in OutgoingMessage, or maybe create a WrappedWritable class that is shared between HTTP, HTTP2 and possibly others. This seems a pretty common case.

  9. mcollina commented on Aug 12, 2018

    @mcollina
    SponsorMember

    Definitely. Also, make the same amendment to the HTTP2 docs.

  10. added a commit that references this issue on Aug 16, 2018
  11. added a commit that references this issue on Sep 3, 2018
  12. gireeshpunathil commented on Dec 30, 2019

    @gireeshpunathil
    Member

    can this be closed, given #22305 is landed?

  13. jasnell commented on Jun 25, 2020

    @jasnell
    Member

    Closing as it appears this is resolved. Can reopen if it turns out that it's not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedIssues that need assistance from volunteers or PRs that need help to proceed.httpIssues and PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions