查看“︁Node.js/stream/拷贝”︁的源代码
来自康健生活
←
Node.js/stream/拷贝
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
<syntaxhighlight lang="javascript"> const fs = require('fs') rs = fs.createReadStream('./sss.txt',{ highWaterMark:4 }) ws = fs.createWriteStream('./jj.txt',{ highWaterMark:1 }) rs.pipe(ws)</syntaxhighlight> 通过读流和写流实现拷贝 <syntaxhighlight lang="javascript"> const fs = require('fs') rs = fs.createReadStream('./sss.txt',{ highWaterMark:4//读的多,写的少 }) ws = fs.createWriteStream('./jj.txt',{ highWaterMark:1 }) rs.on('data',function (chunk) { let flag=ws.write(chunk) if(!flag){ //说明满了 rs.pause()//暂停读取 } }) ws.on('drain',function(){ rs.resume()//写完继续读,重新触发data })</syntaxhighlight> === PassThrough === <syntaxhighlight lang="javascript"> const spawn = require('child_process').spawn; const PassThrough = require('stream').PassThrough; const a = spawn('echo', ['hi user']); const b = new PassThrough(); const c = new PassThrough(); a.stdout.pipe(b); a.stdout.pipe(c); let count = 0; b.on('data', function (chunk) { count += chunk.length; }); b.on('end', function () { console.log(count); c.pipe(process.stdout); });</syntaxhighlight> 注意,仅当生成的命令输出的字节数未填充反压缓冲区时,此技术才有效。以不同的速率读取流,则会产生问题。 === concat-stream === 要缓冲结果,您可以使用concat-stream <syntaxhighlight lang="javascript"> const Promise = require('bluebird'); const concat = require('concat-stream'); const getBuffer = function(stream){ return new Promise(function(resolve, reject){ var gotBuffer = function(buffer){ resolve(buffer); } var concatStream = concat(gotBuffer); stream.on('error', reject); stream.pipe(concatStream); }); }</syntaxhighlight> 要从缓冲区创建流,可以使用: <syntaxhighlight lang="javascript"> const { Readable } = require('stream'); const getBufferStream = function(buffer){ const stream = new Readable(); stream.push(buffer); stream.push(null); return Promise.resolve(stream); }</syntaxhighlight> === 克隆 === <syntaxhighlight lang="javascript"> let Readable = require('stream').Readable; let fs = require('fs'); let path = require('path'); class NewReadable extends Readable{ constructor(originReadable){ super(); this.originReadable = originReadable; this.start(); } start() { this.originReadable.on('data',(chunck)=>{ this.push(chunck); }); this.originReadable.on('end',()=>{ this.push(null); }); this.originReadable.on('error',(e)=>{ this.push(e); }); } // 作为Readable的实现类,必须实现_read函数,否则会throw Error _read(){ } } app.use(async (ctx,next) => { let cloneReq = new NewReadable(ctx.req); let cloneReq2 = new NewReadable(ctx.req); // 此时,ctx.req已被消费完(没有内容),所有的数据都完全在克隆出的两个流上 // 消费cloneReq,获取认证数据 let body = JSON.parse(decodeURIComponent(await parse({req: cloneReq}))); // 将克隆出的cloneReq2重新设置原型链,继承ctx.req原有属性 cloneReq2.__proto__ = ctx.req; // 此后重新给ctx.req复制,留给后续过滤器消费 ctx.req = cloneReq2; if(body.name != 'admin'){ return ctx.body = 'permission denied!' } await next(); })</syntaxhighlight> === Transform === <syntaxhighlight lang="javascript"> // 认证 app.use(async (ctx,next) => { // let cloneReq = new NewReadable(ctx.req); // let cloneReq2 = new NewReadable(ctx.req); let cloneReq = new Transform({ highWaterMark: 10*1024*1024, transform: (chunk,encode,next)=>{ next(null,chunk); } }); let cloneReq2 = new Transform({ highWaterMark: 10*1024*1024, transform: (chunk,encode,next)=>{ next(null,chunk); } }); ctx.req.pipe(cloneReq) ctx.req.pipe(cloneReq2) // 此时,ctx.req已被消费完(没有内容),所有的数据都完全在克隆出的两个流上 // 消费cloneReq,获取认证数据 let body = JSON.parse(decodeURIComponent(await parse({req: cloneReq}))); // 将克隆出的cloneReq2重新设置原型链,继承ctx.req原有属性 cloneReq2.__proto__ = ctx.req; // 此后重新给ctx.req复制,留给后续过滤器消费 ctx.req = cloneReq2; if(body.name != 'admin'){ return ctx.body = 'permission denied!' } await next(); })</syntaxhighlight>
返回
Node.js/stream/拷贝
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。