Skip to content

Buffer#toString should throw error on unsupported encoding #2417

@SheetJSDev

Description

@SheetJSDev

First off, thanks for the amazing work on duktape!

The current behavior of Buffer#toString is always to encode the string as UTF8, even if the encoding is something else (like "binary" for binary strings). Missing encoding issues have been discussed in other issues.

By default, NodeJS throws an error when the encoding is unknown. This behavior has been true since the 0.8.x series in 2014. Testing with new Buffer([1,2,3]).toString("wtf"):

Node Version Behavior
0.8.28 Error: Unknown encoding
0.10.48 TypeError: Unknown encoding: wtf
0.12.18 TypeError: Unknown encoding: wtf
4.9.1 TypeError: Unknown encoding: wtf
6.17.1 TypeError: Unknown encoding: wtf
8.17.0 TypeError: Unknown encoding: wtf
10.24.1 TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: wtf
12.22.6 TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: wtf
14.17.6 TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: wtf
16.8.0 TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: wtf

It is straightforward to test for unexpected behavior downstream, but it would be consistent with NodeJS to throw an error for unsupported encodings. If this change is acceptable we can send a PR

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions