You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been quite regularly using utils for safe and normalized destruction of streams and streamlike objects. Given @mcollina:s recent comment about making the destroy(err, cb) API public I just wanted to suggest (as an alternative?) including something like this into the stream utils.
If there is interest in something like this I could look into preparing a more complete PR and proposal.
module.exports=functiondestroy(stream,err,cb){// TODO: cb should not be optional, throw if missing?letsync=true;constcallback=once(er=>{if(sync){process.nextTick(cb,er);}else{cb(er);}});consts=stream._writableState||stream._readableState;if(stream.destroyed||(s&&s.destroyed)){// TODO: Move this logic into eos?// TODO: Move this logic into destroy(err, callback)?if(s){if(s.errorEmitted){// TODO: cb with error? Save error in errored?// callback(s.errored);returncallback();}elseif(s.closeEmitted){// TODO: s.closeEmitted doesn't exists yet.// TODO: it's still possible for stream to emit 'error' after 'close'.returncallback();}elseif(typeofs.closeEmitted!=='boolean'&&(s.endEmitted||s.finished)){// TODO: This is not entirely correct for Duplex w/ halfOpensetImmediate(callback);stream.on('error',callback);}else{eos(stream,callback);}if(err&&typeofstream.destroy==='function'){// Forward errorstream.destroy(err);}}else{// TODO: How to handle?callback(newError('invalid stream'));}}elseif(typeofstream.destroy==='function'&&stream.destroy.length===2){stream.destroy(err,callback);// We handle error through callbackstream.on('error',noop);}else{eos(stream,callback);if(typeofstream.abort==='function'){stream.abort();}elseif(stream.req&&typeofstream.req.abort==='function'){// HTTP response.destroy() is slightly broken. Try to avoid.stream.req.abort();}elseif(typeofstream.destroy==='function'){stream.destroy(err);}elseif(typeofstream.end==='function'){stream.end();}else{callback(newError('invalid stream'));}}sync=false;}
I've been quite regularly using utils for safe and normalized destruction of streams and streamlike objects. Given @mcollina:s recent comment about making the
destroy(err, cb)
API public I just wanted to suggest (as an alternative?) including something like this into the stream utils.If there is interest in something like this I could look into preparing a more complete PR and proposal.
Which would be used something like:
The text was updated successfully, but these errors were encountered: