diff --git a/lib/buffer.js b/lib/buffer.js index 4467a555c7180e..578b8df8a40e50 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -476,7 +476,7 @@ function createFromString(string, ops, length = ops.byteLength(string)) { function fromString(string, encoding) { let ops; - if (!encoding || encoding === 'utf8' || typeof encoding !== 'string') { + if (encoding === undefined || encoding === 'utf8') { ops = encodingOps.utf8; } else { ops = getEncodingOps(encoding); diff --git a/test/parallel/test-buffer-from.js b/test/parallel/test-buffer-from.js index 416a3b3a3105b5..fd95c1b8850a1e 100644 --- a/test/parallel/test-buffer-from.js +++ b/test/parallel/test-buffer-from.js @@ -140,5 +140,8 @@ throws(() => { }) ); -// Invalid encoding is allowed -Buffer.from('asd', 1); +throws(() => { + Buffer.from('asd', 1); +}, { + code: 'ERR_UNKNOWN_ENCODING' +});