Node.js数据流Stream之Duplex流和Transform流
2023-09-14 08:57:34 时间
Duplex流一个很好的例子是TCP套接字连接.需要实现_read(size)和_Write(data,encoding,callback)方法.
var stream = require('stream'); var util = require('util'); util.inherits(Duplexer, stream.Duplex); function Duplexer(opt) { stream.Duplex.call(this, opt); this.data = []; } Duplexer.prototype._read = function readItem(size) { var chunk = this.data.shift(); if (chunk == "stop"){ this.push(null); } else{ if(chunk){ this.push(chunk); } } }; Duplexer.prototype._write = function(data, encoding, callback) { this.data.push(data); callback(); }; var d = new Duplexer({allowHalfOpen:true}); d.on('data', function(chunk){ console.log('read: ', chunk.toString()); }); d.on('readable',function(){ console.log("readable"); }) d.on('end', function(){ console.log('Message Complete'); }); d.write("I think, "); d.write("therefore "); d.write("I am."); d.write("Rene Descartes"); d.write("stop");
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe stream_duplex.js read: I think, read: therefore read: I am. read: Rene Descartes readable readable readable Message Complete Process finished with exit code 0
Transform变换流扩展了Duplex流,不需要实现而是直接提供。但要实现_transform(chunk,encoding,callback)._flush()这个方法不知道用来做什么的目前
var stream = require("stream"); var util = require("util"); util.inherits(JSONObjectStream, stream.Transform); function JSONObjectStream (opt) { stream.Transform.call(this, opt); }; JSONObjectStream.prototype._transform = function (data, encoding, callback) { object = data ? JSON.parse(data.toString()) : ""; this.emit("object", object); object.handled = true; this.push(JSON.stringify(object)); callback(); }; var tc = new JSONObjectStream(); tc.on("object", function(object){ console.log("Name: %s", object.name); console.log("Color: %s", object.color); }); tc.on("data", function(data){ console.log("Data: %s", data.toString()); }); tc.write('{"name":"Carolinus", "color": "Green"}'); tc.write('{"name":"Solarius", "color": "Blue"}'); tc.write('{"name":"Lo Tae Zhao", "color": "Gold"}'); tc.write('{"name":"Ommadon", "color": "Red"}');
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe stream_transform.js Name: Carolinus Color: Green Data: {"name":"Carolinus","color":"Green","handled":true} Name: Solarius Color: Blue Data: {"name":"Solarius","color":"Blue","handled":true} Name: Lo Tae Zhao Color: Gold Data: {"name":"Lo Tae Zhao","color":"Gold","handled":true} Name: Ommadon Color: Red Data: {"name":"Ommadon","color":"Red","handled":true} Process finished with exit code 0
相关文章
- 【nodejs原理&源码赏析(4)】深度剖析cluster模块源码与node.js多线程(上)
- 19 — node 模块化 及 CommonJS规范 — CommonJS 的由来及各组织与 JS 的关系
- Node.js数据流Stream之Readable流和Writable流
- Node.js文件操作一
- Node.js
- js requestAnimationFrame 每秒执行一次
- 10.Node.js核心模块
- [WASM] Run WebAssembly in Node.js using the node-loader
- [Node.js] Create a model to persist data in a Node.js LoopBack API
- [Node.js] Using ES6 and beyond with Node.js
- [Whole Web, Node.js, PM2] Restarting your node.js app on code change using pm2
- 使用PM2管理Node.js集群
- Node.js is only supported on Windows 8.1, Windows Server 2012 R2, or higher
- Node.js 和 C++ 之间的类型转换
- [Node.js] Mock an API for Local Development in React with Mirage JS
- [Node.js] Child Process with fork() to handle heavy calculation process
- [Node.js] Gzip + crypto in stream
- [Node.js] node-persist: localStorage on the server
- node.js常见的一些错误信息
- Atitit 深入理解命名空间namespace java c# php js
- Atititjs javascript异常处理机制与java异常的转换.js exception process
- Node.js:pm2管理进程启动npm run dev和开机自启
- Node.js(一)概述、全局对象/函数、node各模块使用介绍
- i18n 和 基于js的网页多语言框架 jquery.i18n的介绍
- 【JS高级】js面向对象三大特性之继承_06
- Node.js stream模块(四)双工流和转换流