unpipe 事件触发后,调用 onunpipe(),清理相关数据
// lib/_stream_readable.js function onunpipe(readable, unpipeInfo) {
debug('onunpipe');
if (readable === src) {
if (unpipeInfo && unpipeInfo.hasUnpiped === false) {
unpipeInfo.hasUnpiped = true;
// 清理相关数据
cleanup();
}
}
}
End
在整个 pipe 的过程中,Readable 是主动方 ( 负责整个 pipe 过程:包括数据传递、unpipe 与异常处理 ),Writable 是被动方 ( 只需要触发 drain 事件 )
总结一下 pipe 的过程:
首先执行 readbable.pipe(writable),将 readable 与 writable 对接上
当 readable 中有数据时,readable.emit(‘data’),将数据写入 writable
如果 writable.write(chunk) 返回 false,则进入 pause 模式,等待 drain 事件触发
drain 事件全部触发后,再次进入 flow 模式,写入数据
不管数据写入完成或发生中断,最后都会调用 unpipe()
unpipe() 调用 Readable.prototype.unpipe(),触发 dest 的 unpipe 事件,清理相关数据
参考:
https://github.com/nodejs/node/blob/master/lib/_stream_readable.js
https://github.com/nodejs/node/blob/master/lib/_stream_writable.js









