In Node.js, I've got a Readable stream:
var rs = new (require('stream').Readable);
rs._read = function(size) {
// Implementation.
}
This stream gets read in this HTTP server instance:
var http = require('http');
var server = http.createServer(function(req, res) {
var size = require('url').parse(req.url, true).query.size;
if (size) {
rs.pipe(res);
rs.read(parseInt(size));
}
res.end('');
});
server.listen(3001, function() {
console.log('Listening on 3001');
});
Now, when I curl localhost:3001/?size=n, where n is any integer, size in rs._read(size) is always 16384! This number is the default value of the stream's state.highWaterMark, because rs._read() is called as this._read(state.highWaterMark); in lib/_stream_readable.js. Can anyone explain this? What's the point of the size param if it's not used?
Thank you.
Looks like that's just the highWaterMark of the stream and not coincidentally the default size. Read more here
var stream = require("stream");
var rs = new stream.Readable();
{ _readableState:
{ highWaterMark: 16384,
buffer: [],
length: 0,
pipes: null,
pipesCount: 0,
flowing: false,
ended: false,
endEmitted: false,
reading: false,
calledRead: false,
sync: true,
needReadable: false,
emittedReadable: false,
readableListening: false,
objectMode: false,
defaultEncoding: 'utf8',
ranOut: false,
awaitDrain: 0,
readingMore: false,
decoder: null,
encoding: null },
readable: true,
domain: null,
_events: {},
_maxListeners: 10 }
I think it's wrong to call
rs.read(size).pipe(res);
This is because according to the docs, rs.read(size) returns a buffer, a string, or null. However, pipe() is a method of Readable. You should probably be using
rs.pipe(res);
rs.read(size);