Skip to content

StringDecoder#end doesn't flush state #16564

@jridgewell

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)); // => '€'

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