1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23require('../common'); 24const assert = require('assert'); 25 26const stream = require('stream'); 27 28class MyWritable extends stream.Writable { 29 constructor(fn, options) { 30 super(options); 31 this.fn = fn; 32 } 33 34 _write(chunk, encoding, callback) { 35 this.fn(Buffer.isBuffer(chunk), typeof chunk, encoding); 36 callback(); 37 } 38} 39 40(function defaultCondingIsUtf8() { 41 const m = new MyWritable(function(isBuffer, type, enc) { 42 assert.strictEqual(enc, 'utf8'); 43 }, { decodeStrings: false }); 44 m.write('foo'); 45 m.end(); 46}()); 47 48(function changeDefaultEncodingToAscii() { 49 const m = new MyWritable(function(isBuffer, type, enc) { 50 assert.strictEqual(enc, 'ascii'); 51 }, { decodeStrings: false }); 52 m.setDefaultEncoding('ascii'); 53 m.write('bar'); 54 m.end(); 55}()); 56 57// Change default encoding to invalid value. 58assert.throws(() => { 59 const m = new MyWritable( 60 (isBuffer, type, enc) => {}, 61 { decodeStrings: false }); 62 m.setDefaultEncoding({}); 63 m.write('bar'); 64 m.end(); 65}, { 66 name: 'TypeError', 67 code: 'ERR_UNKNOWN_ENCODING', 68 message: 'Unknown encoding: {}' 69}); 70 71(function checkVairableCaseEncoding() { 72 const m = new MyWritable(function(isBuffer, type, enc) { 73 assert.strictEqual(enc, 'ascii'); 74 }, { decodeStrings: false }); 75 m.setDefaultEncoding('AsCii'); 76 m.write('bar'); 77 m.end(); 78}()); 79