function collect(stream){function save(b){typeof b=="string"&&(b=new Buffer(b));if(Buffer.isBuffer(b)&&!b.length)return;buf.push(b)}function saveEntry(e){collect(e),entryBuffer.push(e)}function proxyPause(p){p.pause()}if(stream._collected)return;stream._collected=!0,stream.pause(),stream.on("data",save),stream.on("end",save);var buf=[];stream.on("entry",saveEntry);var entryBuffer=[];stream.on("proxy",proxyPause),stream.pipe=function(orig){return function(dest){function resume(){stream.removeListener("entry",saveEntry),stream.removeListener("data",save),stream.removeListener("end",save),stream.pipe=orig,dest&&stream.pipe(dest),buf.forEach(function(b){b?stream.emit("data",b):stream.emit("end")}),stream.resume()}var e=0;return function unblockEntry(){var entry=entryBuffer[e++];if(!entry)return resume();entry.on("end",unblockEntry),dest?dest.add(entry):stream.emit("entry",entry)}(),dest}}(stream.pipe)}module.exports=collect;