Skip to content

Commit

Permalink
fs: check type for option argument
Browse files Browse the repository at this point in the history
  • Loading branch information
yosuke-furukawa committed Apr 13, 2015
1 parent 66db924 commit 4969579
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-fs-read-stream-throw-type-error.js
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 8 additions & 0 deletions test/parallel/test-fs-write-stream-throw-type-error.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 4969579

Please sign in to comment.