镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

StringDecoder#end doesn't flush state #16564

Description

@jridgewell
  • v8.6.0
  • Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64
  • StringDecoder

StringDecoder#end doesn't flush the state, allowing it to accept the completing byte afterwards:

const { StringDecoder } = require('string_decoder');
const decoder = new StringDecoder('utf8');

decoder.write(Buffer.from([0xE2, 0x82])); // => ''
// We're waiting for the final byte (currently at byte 2 of a 3 byte sequence)
// Now, end the buffer because, eg, the client disconnected.
decoder.end(); // => '�' (UTF-8 Replacement Character)

Now, there's our setup. If we've ended the buffer and it has outputted a replacement character, it kind of implies it's back at its initial state. But:

decoder.write(Buffer.of(0x61)); // => '�a'

Or, we could "finish" that 3 byte sequence from above (if we haven't already written the 'a'):

decoder.write(Buffer.of(0xAC)); // => '€'

Activity

  1. added
    string_decoderIssues and PRs related to the string_decoder subsystem.
    on Oct 27, 2017
  2. addaleax commented on Oct 27, 2017

    @addaleax
    Member

    @jridgewell Do you have any suggestions on what to do here? Should .write() after .end() throw, like it does for streams?

  3. TimothyGu commented on Oct 27, 2017

    @TimothyGu
    Member

    TextDecoder resets its state after the equivalent of an end call, and functions as a new TextDecoder instance basically. That sounds fair to me.

  4. mscdex commented on Oct 27, 2017

    @mscdex
    Contributor

    Make sure to check benchmarks on this one because of how widely string_decoder is used.

  5. jridgewell commented on Oct 28, 2017

    @jridgewell
    ContributorAuthor

    I think throwing is better than the current behavior, but I’d prefer it just reset and behave like a brand new instance.

  6. thefourtheye commented on Oct 29, 2017

    @thefourtheye
    Contributor

    Proposed a fix in #16594. I chose to reset silently instead of throwing.

  7. added a commit that references this issue on Feb 1, 2018
  8. added a commit that references this issue on May 8, 2018
  9. added a commit that references this issue on Jul 27, 2026
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

    string_decoderIssues and PRs related to the string_decoder subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions