Skip to content

Commit 874dd59

Browse files
mscdextrevnorris
authored andcommitted
streams: make setDefaultEncoding() throw
PR-URL: nodejs/node-v0.x-archive#8529 Fixes: f04f3a0 "streams: set default encoding for writable streams" [[email protected]: update tests to check if throws] Signed-off-by: Trevor Norris <[email protected]>
1 parent db7df57 commit 874dd59

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

lib/_stream_writable.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,12 @@ Writable.prototype.uncork = function() {
222222
};
223223

224224
Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
225-
if (typeof encoding !== 'string')
226-
return false;
227225
// node::ParseEncoding() requires lower case.
228-
encoding = encoding.toLowerCase();
226+
if (typeof encoding === 'string')
227+
encoding = encoding.toLowerCase();
229228
if (!Buffer.isEncoding(encoding))
230-
return false;
229+
throw new TypeError('Unknown encoding: ' + encoding);
231230
this._writableState.defaultEncoding = encoding;
232-
return true;
233231
};
234232

235233
function decodeChunk(state, chunk, encoding) {

test/simple/test-stream-writable-change-default-encoding.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,24 @@ MyWritable.prototype._write = function (chunk, encoding, callback) {
4949
var m = new MyWritable(function(isBuffer, type, enc) {
5050
assert.equal(enc, 'ascii');
5151
}, { decodeStrings: false });
52-
var status = m.setDefaultEncoding('ascii');
53-
assert.equal(status, true);
52+
m.setDefaultEncoding('ascii');
5453
m.write('bar');
5554
m.end();
5655
}());
5756

58-
(function changeDefaultEncodingToInvalidValue() {
57+
assert.throws(function changeDefaultEncodingToInvalidValue() {
5958
var m = new MyWritable(function(isBuffer, type, enc) {
60-
assert.equal(enc, 'utf8');
6159
}, { decodeStrings: false });
62-
var status = m.setDefaultEncoding({});
63-
assert.equal(status, false);
60+
m.setDefaultEncoding({});
6461
m.write('bar');
6562
m.end();
66-
}());
63+
}, TypeError);
6764

6865
(function checkVairableCaseEncoding() {
6966
var m = new MyWritable(function(isBuffer, type, enc) {
7067
assert.equal(enc, 'ascii');
7168
}, { decodeStrings: false });
72-
var status = m.setDefaultEncoding('AsCii');
73-
assert.equal(status, true);
69+
m.setDefaultEncoding('AsCii');
7470
m.write('bar');
7571
m.end();
7672
}());

0 commit comments

Comments
 (0)