diff --git a/lib/fs.js b/lib/fs.js index 0025e6a69de2ef..f54c3901a6e10e 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1615,8 +1615,15 @@ function ReadStream(path, options) { if (!(this instanceof ReadStream)) return new ReadStream(path, options); + if (options === undefined) + options = {}; + else if (typeof options === 'string') + options = { encoding: options }; + else if (typeof options !== 'object') + throw new TypeError('options must be a string or an object'); + // a little bit bigger buffer and water marks by default - options = Object.create(options || {}); + options = Object.create(options); if (options.highWaterMark === undefined) options.highWaterMark = 64 * 1024; @@ -1781,7 +1788,12 @@ function WriteStream(path, options) { if (!(this instanceof WriteStream)) return new WriteStream(path, options); - options = options || {}; + if (options === undefined || options === null) + options = {}; + else if (typeof options === 'string') + options = { encoding: options }; + else if (typeof options !== 'object') + throw new TypeError('options must be a string or an object'); Writable.call(this, options); @@ -1805,6 +1817,10 @@ function WriteStream(path, options) { this.pos = this.start; } + if (options.encoding) { + this.setDefaultEncoding(options.encoding); + } + if (typeof this.fd !== 'number') this.open(); diff --git a/test/parallel/test-fs-read-stream-encoding.js b/test/parallel/test-fs-read-stream-encoding.js new file mode 100644 index 00000000000000..68699136ea6366 --- /dev/null +++ b/test/parallel/test-fs-read-stream-encoding.js @@ -0,0 +1,17 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const stream = require('stream'); +const encoding = 'base64'; + +const example = path.join(common.fixturesDir, 'x.txt'); +const assertStream = new stream.Writable({ + write: function(chunk, enc, next) { + const expected = new Buffer('xyz'); + assert(chunk.equals(expected)); + } +}); +assertStream.setDefaultEncoding(encoding); +fs.createReadStream(example, encoding).pipe(assertStream); diff --git a/test/parallel/test-fs-read-stream-throw-type-error.js b/test/parallel/test-fs-read-stream-throw-type-error.js new file mode 100644 index 00000000000000..c67bef6a0ef503 --- /dev/null +++ b/test/parallel/test-fs-read-stream-throw-type-error.js @@ -0,0 +1,19 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const example = path.join(common.fixturesDir, 'x.txt'); +assert.throws(function() { + fs.createReadStream(example, 123); +}, TypeError, 'options must be a string or an object'); +assert.throws(function() { + fs.createReadStream(example, 0); +}, TypeError, 'options must be a string or an object'); +assert.throws(function() { + fs.createReadStream(example, true); +}, TypeError, 'options must be a string or an object'); +assert.throws(function() { + fs.createReadStream(example, false); +}, TypeError, 'options must be a string or an object'); diff --git a/test/parallel/test-fs-write-stream-encoding.js b/test/parallel/test-fs-write-stream-encoding.js new file mode 100644 index 00000000000000..3d6dbeb37e94ab --- /dev/null +++ b/test/parallel/test-fs-write-stream-encoding.js @@ -0,0 +1,23 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); +const stream = require('stream'); +const firstEncoding = 'base64'; +const secondEncoding = 'binary'; + +const example = path.join(common.fixturesDir, 'x.txt'); +const dummy = path.join(common.tmpDir, '/x.txt'); +const exampleReadStream = fs.createReadStream(example, firstEncoding); +const dummyWriteStream = fs.createWriteStream(dummy, firstEncoding); +exampleReadStream.pipe(dummyWriteStream).on('finish', function(){ + const assertWriteStream = new stream.Writable({ + write: function(chunk, enc, next) { + const expected = new Buffer('xyz\n'); + assert(chunk.equals(expected)); + } + }); + assertWriteStream.setDefaultEncoding(secondEncoding); + fs.createReadStream(dummy, secondEncoding).pipe(assertWriteStream); +}); diff --git a/test/parallel/test-fs-write-stream-throw-type-error.js b/test/parallel/test-fs-write-stream-throw-type-error.js new file mode 100644 index 00000000000000..450b7439ab7b10 --- /dev/null +++ b/test/parallel/test-fs-write-stream-throw-type-error.js @@ -0,0 +1,19 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const example = path.join(common.tmpDir, '/dummy'); +assert.throws(function() { + fs.createWriteStream(example, 123); +}, TypeError, 'options must be a string or an object'); +assert.throws(function() { + fs.createWriteStream(example, 0); +}, TypeError, 'options must be a string or an object'); +assert.throws(function() { + fs.createWriteStream(example, true); +}, TypeError, 'options must be a string or an object'); +assert.throws(function() { + fs.createWriteStream(example, false); +}, TypeError, 'options must be a string or an object');