Skip to content

Commit 0a4cd53

Browse files
committed
stream: disallow writing string chunk with 'buffer' encoding
Signed-off-by: Renegade334 <[email protected]>
1 parent 9c01018 commit 0a4cd53

3 files changed

Lines changed: 18 additions & 25 deletions

File tree

lib/internal/streams/writable.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ function _write(stream, chunk, encoding, cb) {
466466
}
467467

468468
if (typeof chunk === 'string') {
469+
if (encoding === 'buffer') {
470+
throw new ERR_UNKNOWN_ENCODING(encoding);
471+
}
469472
if ((state[kState] & kDecodeStrings) !== 0) {
470473
chunk = Buffer.from(chunk, encoding);
471474
encoding = 'buffer';

test/parallel/test-stream-base-typechecking.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/parallel/test-stream-writable-write-error.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function test(autoDestroy) {
3131
{
3232
const w = new Writable({
3333
autoDestroy,
34-
_write() {}
34+
write() {}
3535
});
3636
w.end();
3737
expectError(w, ['asd'], 'ERR_STREAM_WRITE_AFTER_END');
@@ -40,34 +40,42 @@ function test(autoDestroy) {
4040
{
4141
const w = new Writable({
4242
autoDestroy,
43-
_write() {}
43+
write() {}
4444
});
4545
w.destroy();
4646
}
4747

4848
{
4949
const w = new Writable({
5050
autoDestroy,
51-
_write() {}
51+
write() {}
5252
});
5353
expectError(w, [null], 'ERR_STREAM_NULL_VALUES', true);
5454
}
5555

5656
{
5757
const w = new Writable({
5858
autoDestroy,
59-
_write() {}
59+
write() {}
6060
});
6161
expectError(w, [{}], 'ERR_INVALID_ARG_TYPE', true);
6262
}
6363

6464
{
6565
const w = new Writable({
66-
decodeStrings: false,
6766
autoDestroy,
68-
_write() {}
67+
write() {}
68+
});
69+
expectError(w, ['asd', 'buffer'], 'ERR_UNKNOWN_ENCODING', true);
70+
}
71+
72+
{
73+
const w = new Writable({
74+
autoDestroy,
75+
decodeStrings: false,
76+
write() {}
6977
});
70-
expectError(w, ['asd', 'noencoding'], 'ERR_UNKNOWN_ENCODING', true);
78+
expectError(w, ['asd', 'buffer'], 'ERR_UNKNOWN_ENCODING', true);
7179
}
7280
}
7381

0 commit comments

Comments
 (0)