fs: add bytesRead to ReadStream · nodejs/node@0bb9d21 · GitHub
Skip to content

Commit 0bb9d21

Browse files
LinusUcjihrig
authored andcommitted
fs: add bytesRead to ReadStream
Add a property named bytesRead that exposes how many bytes that have currently been read from the file. This brings consistency with WriteStream that has bytesWritten and net.Socket which have both bytesRead and bytesWritten. Fixes: https://github.com/nodejs/node/issues/#7938 PR-URL: #7942 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent d9c9e46 commit 0bb9d21

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

doc/api/fs.md

Lines changed: 7 additions & 0 deletions

lib/fs.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,7 @@ function ReadStream(path, options) {
16791679
this.end = options.end;
16801680
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
16811681
this.pos = undefined;
1682+
this.bytesRead = 0;
16821683

16831684
if (this.start !== undefined) {
16841685
if (typeof this.start !== 'number') {
@@ -1774,8 +1775,10 @@ ReadStream.prototype._read = function(n) {
17741775
self.emit('error', er);
17751776
} else {
17761777
var b = null;
1777-
if (bytesRead > 0)
1778+
if (bytesRead > 0) {
1779+
self.bytesRead += bytesRead;
17781780
b = thisPool.slice(start, start + bytesRead);
1781+
}
17791782

17801783
self.push(b);
17811784
}

test/parallel/test-fs-read-stream.js

Lines changed: 12 additions & 0 deletions

0 commit comments

Comments
 (0)