From 82bdf8fba2d3f197522e31ee49f3cc4f5f52bd53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=88=9A?= Date: Fri, 12 Jan 2018 23:21:44 +0800 Subject: [PATCH] fs: fix options.end of fs.ReadStream() Fixes: https://github.com/nodejs/node/issues/18116 PR-URL: https://github.com/nodejs/node/pull/18121 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Joyee Cheung Reviewed-By: Weijia Wang --- lib/fs.js | 3 ++- test/parallel/test-fs-read-stream.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/fs.js b/lib/fs.js index 39919306de1b1b..db262f5da8fc22 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -2267,7 +2267,8 @@ function ReadStream(path, options) { this.flags = options.flags === undefined ? 'r' : options.flags; this.mode = options.mode === undefined ? 0o666 : options.mode; - this.start = options.start; + this.start = typeof this.fd !== 'number' && options.start === undefined ? + 0 : options.start; this.end = options.end; this.autoClose = options.autoClose === undefined ? true : options.autoClose; this.pos = undefined; diff --git a/test/parallel/test-fs-read-stream.js b/test/parallel/test-fs-read-stream.js index 1b9cc4aa90f523..7fc7a0d56bce37 100644 --- a/test/parallel/test-fs-read-stream.js +++ b/test/parallel/test-fs-read-stream.js @@ -164,6 +164,20 @@ common.expectsError( })); } +{ + // Verify that end works when start is not specified. + const stream = new fs.createReadStream(rangeFile, { end: 1 }); + stream.data = ''; + + stream.on('data', function(chunk) { + stream.data += chunk; + }); + + stream.on('end', common.mustCall(function() { + assert.strictEqual('xy', stream.data); + })); +} + { // pause and then resume immediately. const pauseRes = fs.createReadStream(rangeFile);