Sunday, 19 March 2017

Readable Stream Node : the _read method with the Number of bytes param

I am trying to figure out streams in node and playing around with some examples in the stream handbook

I am trying out the _read method of a readable stream. It says, it takes in a parameter which is the number of bytes the consumer wants to read.

I have two questions here.

  • Is the number of bytes the consumer wants to read the 'watermark'
  • Why do i get an error when I use _read with a parameter.

This is my code.

var Readable = require('stream').Readable;
var rs = Readable();

var c = 97;
rs._read = function (5) {
    rs.push(String.fromCharCode(c++));
    if (c > 'z'.charCodeAt(0)) {
        rs.push('\n');
        rs.push(null);
    }
};

setTimeout(function () {
  rs.pipe(process.stdout);
}, 2000);

And this is the error

/Users/nikhilkuria/Dev/git/node_demo/streams/streamRead.js:5
rs._read = function (5) {
                 ^
SyntaxError: Unexpected number
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:542:28)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3



via Nikhil Kuriakose

No comments:

Post a Comment