From 49695792040656e511bdb403a0c46213482a1b35 Mon Sep 17 00:00:00 2001 From: Yosuke Furukawa Date: Tue, 14 Apr 2015 02:46:52 +0900 Subject: [PATCH 1/2] fs: check type for option argument --- lib/fs.js | 10 ++++++++++ test/parallel/test-fs-read-stream-throw-type-error.js | 8 ++++++++ test/parallel/test-fs-write-stream-throw-type-error.js | 8 ++++++++ 3 files changed, 26 insertions(+) create mode 100644 test/parallel/test-fs-read-stream-throw-type-error.js create mode 100644 test/parallel/test-fs-write-stream-throw-type-error.js diff --git a/lib/fs.js b/lib/fs.js index 0025e6a69de2ef..cf97eb236da02d 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1615,6 +1615,11 @@ function ReadStream(path, options) { if (!(this instanceof ReadStream)) return new ReadStream(path, options); + if (typeof options === 'string') + options = { encoding: options }; + else if (options && typeof options !== 'object') + throw new TypeError('Bad arguments'); + // a little bit bigger buffer and water marks by default options = Object.create(options || {}); if (options.highWaterMark === undefined) @@ -1781,6 +1786,11 @@ function WriteStream(path, options) { if (!(this instanceof WriteStream)) return new WriteStream(path, options); + if (typeof options === 'string') + options = { encoding: options }; + else if (options && typeof options !== 'object') + throw new TypeError('Bad arguments'); + options = options || {}; Writable.call(this, options); 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..0bc7ccc3b2a3e1 --- /dev/null +++ b/test/parallel/test-fs-read-stream-throw-type-error.js @@ -0,0 +1,8 @@ +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); +assert.throws(function() { fs.createReadStream(example, true); }, TypeError); 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..45500db17e6e57 --- /dev/null +++ b/test/parallel/test-fs-write-stream-throw-type-error.js @@ -0,0 +1,8 @@ +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.createReadStream(example, 123); }, TypeError); +assert.throws(function() { fs.createReadStream(example, true); }, TypeError); From f1fbbb841381bd48fcd5267f737cb13f206819e5 Mon Sep 17 00:00:00 2001 From: Yosuke Furukawa Date: Tue, 14 Apr 2015 02:47:46 +0900 Subject: [PATCH 2/2] fs: set encoding if option argument is string --- lib/fs.js | 24 ++++++++++++------- test/parallel/test-fs-read-stream-encoding.js | 17 +++++++++++++ .../test-fs-read-stream-throw-type-error.js | 15 ++++++++++-- .../parallel/test-fs-write-stream-encoding.js | 23 ++++++++++++++++++ .../test-fs-write-stream-throw-type-error.js | 15 ++++++++++-- 5 files changed, 81 insertions(+), 13 deletions(-) create mode 100644 test/parallel/test-fs-read-stream-encoding.js create mode 100644 test/parallel/test-fs-write-stream-encoding.js diff --git a/lib/fs.js b/lib/fs.js index cf97eb236da02d..f54c3901a6e10e 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1615,13 +1615,15 @@ function ReadStream(path, options) { if (!(this instanceof ReadStream)) return new ReadStream(path, options); - if (typeof options === 'string') + if (options === undefined) + options = {}; + else if (typeof options === 'string') options = { encoding: options }; - else if (options && typeof options !== 'object') - throw new TypeError('Bad arguments'); + 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; @@ -1786,12 +1788,12 @@ function WriteStream(path, options) { if (!(this instanceof WriteStream)) return new WriteStream(path, options); - if (typeof options === 'string') + if (options === undefined || options === null) + options = {}; + else if (typeof options === 'string') options = { encoding: options }; - else if (options && typeof options !== 'object') - throw new TypeError('Bad arguments'); - - options = options || {}; + else if (typeof options !== 'object') + throw new TypeError('options must be a string or an object'); Writable.call(this, options); @@ -1815,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 index 0bc7ccc3b2a3e1..c67bef6a0ef503 100644 --- a/test/parallel/test-fs-read-stream-throw-type-error.js +++ b/test/parallel/test-fs-read-stream-throw-type-error.js @@ -1,8 +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); -assert.throws(function() { fs.createReadStream(example, true); }, TypeError); +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 index 45500db17e6e57..450b7439ab7b10 100644 --- a/test/parallel/test-fs-write-stream-throw-type-error.js +++ b/test/parallel/test-fs-write-stream-throw-type-error.js @@ -1,8 +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.createReadStream(example, 123); }, TypeError); -assert.throws(function() { fs.createReadStream(example, true); }, TypeError); +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');