diff --git a/lib/internal/streams/writable.js b/lib/internal/streams/writable.js index 89d384e9f2fb8f..94913461cb6044 100644 --- a/lib/internal/streams/writable.js +++ b/lib/internal/streams/writable.js @@ -466,6 +466,9 @@ function _write(stream, chunk, encoding, cb) { } if (typeof chunk === 'string') { + if (encoding === 'buffer') { + throw new ERR_UNKNOWN_ENCODING(encoding); + } if ((state[kState] & kDecodeStrings) !== 0) { chunk = Buffer.from(chunk, encoding); encoding = 'buffer'; diff --git a/test/parallel/test-stream-base-typechecking.js b/test/parallel/test-stream-base-typechecking.js deleted file mode 100644 index ae8582642344ee..00000000000000 --- a/test/parallel/test-stream-base-typechecking.js +++ /dev/null @@ -1,18 +0,0 @@ -'use strict'; -const common = require('../common'); -const assert = require('assert'); -const net = require('net'); - -const server = net.createServer().listen(0, common.mustCall(() => { - const client = net.connect(server.address().port, common.mustCall(() => { - assert.throws(() => { - client.write('broken', 'buffer'); - }, { - name: 'TypeError', - code: 'ERR_INVALID_ARG_TYPE', - message: 'Second argument must be a buffer' - }); - client.destroy(); - server.close(); - })); -})); diff --git a/test/parallel/test-stream-writable-write-error.js b/test/parallel/test-stream-writable-write-error.js index 069e32e1be8e3e..76f797fb1bd583 100644 --- a/test/parallel/test-stream-writable-write-error.js +++ b/test/parallel/test-stream-writable-write-error.js @@ -31,7 +31,7 @@ function test(autoDestroy) { { const w = new Writable({ autoDestroy, - _write() {} + write() {} }); w.end(); expectError(w, ['asd'], 'ERR_STREAM_WRITE_AFTER_END'); @@ -40,7 +40,7 @@ function test(autoDestroy) { { const w = new Writable({ autoDestroy, - _write() {} + write() {} }); w.destroy(); } @@ -48,7 +48,7 @@ function test(autoDestroy) { { const w = new Writable({ autoDestroy, - _write() {} + write() {} }); expectError(w, [null], 'ERR_STREAM_NULL_VALUES', true); } @@ -56,18 +56,26 @@ function test(autoDestroy) { { const w = new Writable({ autoDestroy, - _write() {} + write() {} }); expectError(w, [{}], 'ERR_INVALID_ARG_TYPE', true); } { const w = new Writable({ - decodeStrings: false, autoDestroy, - _write() {} + write() {} + }); + expectError(w, ['asd', 'buffer'], 'ERR_UNKNOWN_ENCODING', true); + } + + { + const w = new Writable({ + autoDestroy, + decodeStrings: false, + write() {} }); - expectError(w, ['asd', 'noencoding'], 'ERR_UNKNOWN_ENCODING', true); + expectError(w, ['asd', 'buffer'], 'ERR_UNKNOWN_ENCODING', true); } }