Skip to content

Buffer.isEncoding regards an empty string as a valid encoding #9654

@shinnn

Description

@shinnn
  • Version: Node.js v7.1.0
  • Platform: Darwin Kernel Version 16.1.0

As stated in the issue title, Buffer.isEncoding('') returns true.

Actually, passing an empty string to encoding options of fs.readFile, Buffer.from or other functions supporting encoding doesn't result in an error, because they use the default encoding utf8 as a fallback in that case.

From this point of view, '' is also a valid encoding and Buffer.isEncoding('') === true looks fine. However, if we approve this behavior of Buffer.isEncoding, I think it's inconsistent that Buffer.isEncoding returns false when it takes other falsy values − false, null, undefined etc.

So, which design of Buffer.isEncoding is most reasonable?

  1. There is no problem with the current behavior of Buffer.isEncoding.
  2. Buffer.isEncoding should return false when it takes ''.
  3. Buffer.isEncoding should return true when it takes any falsy values.

I support the second one and can create a pull request.

I'd like to hear your opinion. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.docIssues and PRs related to the documentations.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions