Node.JS数据inputstream

有一个inputstream扩展,所以我可以调用像我习惯的方法

例如

stdin.readData(function (err, buffer) { // err if an error event was created, buffer if this is just data, null to both if the end of the stream was reached. // Added bonuses would be other methods I am used to in Java // - readLine // - readFully // - readStringUtf8 // - readInt, readDouble, readBoolean, etc. }) 

后端将监听dataenderror事件,并自动缓冲它们,并在调用readData时使其可用。

这个function并不难。 所有你需要做的就是抓住ReadableStream原型,并实现.read方法

未经testing的代码:

 var ReadableStream = Object.getPrototypeOf(process.stdin); ReadableStream.read = function(cb) { this.on('data', function(buf) { cb(null, buf); }); this.on('error', function(err) { cb(err, null); }); this.on('end', function() { cb(null, null); }); this.on('close', function() { cb(new Error("Stream closed"), null); }); };