Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Throw if fs start arg is not number #2780

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1038,8 +1038,13 @@ var ReadStream = fs.ReadStream = function(path, options) {
if (this.encoding) this.setEncoding(this.encoding);

if (this.start !== undefined) {
if ('number' !== typeof this.start) {
throw TypeError("'start' must be a Number or left undefined, not a string containing a number");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor beans, but I think the "left" should be removed (sounds a little better IMO), as well as "not a string containing a number" (you're not sure that this is the case, what if it's an Object of some sort?)

}
if (this.end === undefined) {
this.end = Infinity;
} else if ('number' !== typeof this.end) {
throw TypeError("'end' must be a Number or left undefined, not a string containing a number");
}

if (this.start > this.end) {
Expand Down Expand Up @@ -1226,6 +1231,9 @@ var WriteStream = fs.WriteStream = function(path, options) {
}

if (this.start !== undefined) {
if ('number' !== typeof this.start) {
throw TypeError("'start' must be a Number or left undefined, not a string containing a number");
}
if (this.start < 0) {
throw new Error('start must be >= zero');
}
Expand Down
26 changes: 26 additions & 0 deletions test/simple/test-fs-non-number-arguments-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(function () {
"use strict";

var assert = require('assert'),
fs = require('./fs'),
saneEmitter;

saneEmitter = fs.createReadStream(__filename, { start: 17, end: 29 });

assert.throws(function () {
fs.createReadStream(__filename, { start: "17", end: 29 });
}, "start as string didn't throw an error for createReadStream");

assert.throws(function () {
fs.createReadStream(__filename, { start: 17, end: "29" });
}, "end as string didn't throw an error");

assert.throws(function () {
fs.createWriteStream(__filename, { start: "17" });
}, "start as string didn't throw an error for createWriteStream");

saneEmitter.on('data', function (data) {
// a sanity check when using numbers instead of strings
assert.strictEqual('"use strict";', data.toString('utf8'), 'read ' + data.toString('utf8') + ' instead of "use strict";');
});
}());