var once=require("once"),noop=function(){},isRequest=function(e){return e.setHeader&&"function"==typeof e.abort},isChildProcess=function(e){return e.stdio&&Array.isArray(e.stdio)&&3===e.stdio.length},eos=function(e,r,n){if("function"==typeof r)return eos(e,null,r);r||(r={}),n=once(n||noop);var o=e._writableState,t=e._readableState,i=r.readable||!1!==r.readable&&e.readable,s=r.writable||!1!==r.writable&&e.writable,l=function(){e.writable||c()},c=function(){s=!1,i||n.call(e)},a=function(){i=!1,s||n.call(e)},u=function(r){n.call(e,r?new Error("exited with error code: "+r):null)},d=function(r){n.call(e,r)},f=function(){return(!i||t&&t.ended)&&(!s||o&&o.ended)?void 0:n.call(e,new Error("premature close"))},m=function(){e.req.on("finish",c)};return isRequest(e)?(e.on("complete",c),e.on("abort",f),e.req?m():e.on("request",m)):s&&!o&&(e.on("end",l),e.on("close",l)),isChildProcess(e)&&e.on("exit",u),e.on("end",a),e.on("finish",c),!1!==r.error&&e.on("error",d),e.on("close",f),function(){e.removeListener("complete",c),e.removeListener("abort",f),e.removeListener("request",m),e.req&&e.req.removeListener("finish",c),e.removeListener("end",l),e.removeListener("close",l),e.removeListener("finish",c),e.removeListener("exit",u),e.removeListener("end",a),e.removeListener("error",d),e.removeListener("close",f)}};module.exports=eos;