Skip to content

Commit 7cd0d4f

Browse files
mscdexjasnell
authored andcommitted
buffer: fix backwards incompatibility
4a86803 introduced a backwards incompatibility by accident and was not caught due to an existing test that wasn't strict enough. This commit fixes both the backwards incompatibility and the test. PR-URL: #12439 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2519757 commit 7cd0d4f

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

lib/buffer.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,7 @@ function byteLength(string, encoding) {
456456
return len >>> 1;
457457
break;
458458
}
459-
if (mustMatch)
460-
throw new TypeError('Unknown encoding: ' + encoding);
461-
else
462-
return binding.byteLengthUtf8(string);
459+
return (mustMatch ? -1 : binding.byteLengthUtf8(string));
463460
}
464461

465462
Buffer.byteLength = byteLength;

test/parallel/test-buffer-alloc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,8 @@ assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
885885
}
886886

887887
// Regression test for #5482: should throw but not assert in C++ land.
888-
assert.throws(() => Buffer.from('', 'buffer'), TypeError);
888+
assert.throws(() => Buffer.from('', 'buffer'),
889+
/^TypeError: "encoding" must be a valid string encoding$/);
889890

890891
// Regression test for #6111. Constructing a buffer from another buffer
891892
// should a) work, and b) not corrupt the source buffer.

0 commit comments

Comments
 (0)