var once=require("once"),eos=require("end-of-stream"),fs=require("fs"),noop=function(){},ancient=/^v?\.0/.test(process.version),isFn=function(e){return"function"==typeof e},isFS=function(e){return!!ancient&&(!!fs&&((e instanceof(fs.ReadStream||noop)||e instanceof(fs.WriteStream||noop))&&isFn(e.close)))},isRequest=function(e){return e.setHeader&&isFn(e.abort)},destroyer=function(e,r,n,o){o=once(o);var t=!1;e.on("close",function(){t=!0}),eos(e,{readable:r,writable:n},function(e){if(e)return o(e);t=!0,o()});var i=!1;return function(r){if(!t&&!i)return i=!0,isFS(e)?e.close(noop):isRequest(e)?e.abort():isFn(e.destroy)?e.destroy():void o(r||new Error("stream was destroyed"))}},call=function(e){e()},pipe=function(e,r){return e.pipe(r)},pump=function(){var e,r=Array.prototype.slice.call(arguments),n=isFn(r[r.length-1]||noop)&&r.pop()||noop;if(Array.isArray(r[0])&&(r=r[0]),r.length<2)throw new Error("pump requires two streams per minimum");var o=r.map(function(t,i){var s=i<r.length-1;return destroyer(t,s,i>0,function(r){e||(e=r),r&&o.forEach(call),s||(o.forEach(call),n(e))})});return r.reduce(pipe)};module.exports=pump;