-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: add bytesRead property for readable
Add a bytesRead property for readable is useful in some use cases. When user want know how many bytes read of readable, need to caculate it in userland. If encoding is specificed, get the value is very slowly. PR-URL: #4372 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
- Loading branch information
1 parent
ebd9add
commit bfb2cd0
Showing
4 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const Readable = require('stream').Readable; | ||
const Duplex = require('stream').Duplex; | ||
const Transform = require('stream').Transform; | ||
|
||
(function() { | ||
const readable = new Readable({ | ||
read: function(n) { | ||
var i = this._index++; | ||
if (i > this._max) | ||
this.push(null); | ||
else | ||
this.push(new Buffer('a')); | ||
} | ||
}); | ||
|
||
readable._max = 1000; | ||
readable._index = 1; | ||
|
||
var total = 0; | ||
readable.on('data', function(chunk) { | ||
total += chunk.length; | ||
}); | ||
|
||
readable.on('end', function() { | ||
assert.equal(total, readable.bytesRead); | ||
}); | ||
})(); | ||
|
||
(function() { | ||
const readable = new Readable({ | ||
read: function(n) { | ||
var i = this._index++; | ||
if (i > this._max) | ||
this.push(null); | ||
else | ||
this.push(new Buffer('a')); | ||
} | ||
}); | ||
|
||
readable._max = 1000; | ||
readable._index = 1; | ||
|
||
var total = 0; | ||
readable.setEncoding('utf8'); | ||
readable.on('data', function(chunk) { | ||
total += Buffer.byteLength(chunk); | ||
}); | ||
|
||
readable.on('end', function() { | ||
assert.equal(total, readable.bytesRead); | ||
}); | ||
})(); | ||
|
||
(function() { | ||
const duplex = new Duplex({ | ||
read: function(n) { | ||
var i = this._index++; | ||
if (i > this._max) | ||
this.push(null); | ||
else | ||
this.push(new Buffer('a')); | ||
}, | ||
write: function(chunk, encoding, next) { | ||
next(); | ||
} | ||
}); | ||
|
||
duplex._max = 1000; | ||
duplex._index = 1; | ||
|
||
var total = 0; | ||
duplex.setEncoding('utf8'); | ||
duplex.on('data', function(chunk) { | ||
total += Buffer.byteLength(chunk); | ||
}); | ||
|
||
duplex.on('end', function() { | ||
assert.equal(total, duplex.bytesRead); | ||
}); | ||
})(); | ||
|
||
(function() { | ||
const readable = new Readable({ | ||
read: function(n) { | ||
var i = this._index++; | ||
if (i > this._max) | ||
this.push(null); | ||
else | ||
this.push(new Buffer('{"key":"value"}')); | ||
} | ||
}); | ||
readable._max = 1000; | ||
readable._index = 1; | ||
|
||
const transform = new Transform({ | ||
readableObjectMode : true, | ||
transform: function(chunk, encoding, next) { | ||
next(null, JSON.parse(chunk)); | ||
}, | ||
flush: function(done) { | ||
done(); | ||
} | ||
}); | ||
|
||
var total = 0; | ||
readable.on('data', function(chunk) { | ||
total += chunk.length; | ||
}); | ||
|
||
transform.on('end', function() { | ||
assert.equal(0, transform.bytesRead); | ||
assert.equal(total, readable.bytesRead); | ||
}); | ||
readable.pipe(transform); | ||
})(); |