You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Buffer.isEncoding regards an empty string as a valid encoding #9654
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.
IMO, it would be consistent for Buffer.isEncoding() to accept any encoding that Buffer.from() accepts. That is:
Buffer.from('x','')// ok, defaults to utf-8Buffer.from('x',true)// ok, defaults to utf-8Buffer.from('x',false)// ok, defaults to utf-8// etc.
The alternative is to make Buffer.from() and friends reject such values but that is almost certainly going to result in (possibly significant) ecosystem breakage.
Another alternative is to simply state in the documentation that Buffer.from() accepts them from legacy reasons but that Buffer.isEncoding() is the one source of truth. Not a bad option either.
Another alternative is to simply state in the documentation that Buffer.from() accepts them from legacy reasons but that Buffer.isEncoding() is the one source of truth. Not a bad option either.
I'd definitely support this, Buffer.isEncoding() should only return true for strings that specify an existing encoding, while Buffer.from may accept other inputs for backward compatibility.
Another alternative is to simply state in the documentation that Buffer.from() accepts them from legacy reasons but that Buffer.isEncoding() is the one source of truth. Not a bad option either.
I'm going to abstain from taking a position on this because the implications could be quite broad, but I will point out that this logic isn't quite right:
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.
Just because '' is falsy doesn't mean we should include other falsy values in reasoning about acceptable categories. The broadest category of acceptable values is Strings, so we should just be restricting our arguments to what acceptable string contents are and just because a particular string is also in the category of "falsy" doesn't mean we should consider those values—similarly if we had an encoding that was also a number doesn't mean we should include numbers in our thinking about acceptable categories. A non-string value is clearly not an encoding type, so I believe we should be limiting the discussion to what string values are acceptable and whether '' can be treated as "default encoding", which seems reasonable, on the surface anyway.
Activity
BrandonZacharie commentedon Nov 18, 2016
I also vote for option 2 (
'' !== 'utf8').Trott commentedon Jul 15, 2017
@nodejs/buffer
Trott commentedon Aug 10, 2017
I'm going to put a
ctc-reviewlabel on this to at least try to get some input on whether this should be changed or not. @nodejs/ctcbnoordhuis commentedon Aug 10, 2017
IMO, it would be consistent for
Buffer.isEncoding()to accept any encoding thatBuffer.from()accepts. That is:The alternative is to make
Buffer.from()and friends reject such values but that is almost certainly going to result in (possibly significant) ecosystem breakage.Another alternative is to simply state in the documentation that
Buffer.from()accepts them from legacy reasons but thatBuffer.isEncoding()is the one source of truth. Not a bad option either.tniessen commentedon Aug 10, 2017
I'd definitely support this,
Buffer.isEncoding()should only return true for strings that specify an existing encoding, whileBuffer.frommay accept other inputs for backward compatibility.cjihrig commentedon Aug 10, 2017
I'm in favor of that approach.
rvagg commentedon Aug 14, 2017
I'm going to abstain from taking a position on this because the implications could be quite broad, but I will point out that this logic isn't quite right:
Just because
''is falsy doesn't mean we should include other falsy values in reasoning about acceptable categories. The broadest category of acceptable values is Strings, so we should just be restricting our arguments to what acceptable string contents are and just because a particular string is also in the category of "falsy" doesn't mean we should consider those values—similarly if we had an encoding that was also a number doesn't mean we should include numbers in our thinking about acceptable categories. A non-string value is clearly not an encoding type, so I believe we should be limiting the discussion to what string values are acceptable and whether''can be treated as "default encoding", which seems reasonable, on the surface anyway.BridgeAR commentedon Apr 22, 2018
This got fixed in 452eed9