From f17237cf63423eeacd1edaed9faa3f61cd7797a7 Mon Sep 17 00:00:00 2001 From: Brian White Date: Sun, 16 Apr 2017 03:42:22 -0400 Subject: [PATCH] buffer: fix backwards incompatibility 4a86803f6 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. --- lib/buffer.js | 5 +---- test/parallel/test-buffer-alloc.js | 3 ++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 9163eb17c81afa..f99b2625f0eac7 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -455,10 +455,7 @@ function byteLength(string, encoding) { return len >>> 1; break; } - if (mustMatch) - throw new TypeError('Unknown encoding: ' + encoding); - else - return binding.byteLengthUtf8(string); + return (mustMatch ? -1 : binding.byteLengthUtf8(string)); } Buffer.byteLength = byteLength; diff --git a/test/parallel/test-buffer-alloc.js b/test/parallel/test-buffer-alloc.js index 02ed1bcf63669e..7b868f783ee997 100644 --- a/test/parallel/test-buffer-alloc.js +++ b/test/parallel/test-buffer-alloc.js @@ -885,7 +885,8 @@ assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError); } // Regression test for #5482: should throw but not assert in C++ land. -assert.throws(() => Buffer.from('', 'buffer'), TypeError); +assert.throws(() => Buffer.from('', 'buffer'), + /^TypeError: "encoding" must be a valid string encoding$/); // Regression test for #6111. Constructing a buffer from another buffer // should a) work, and b) not corrupt the source buffer.