koa: better stream error handling

Some ideas how to better handle streams, the biggest problem being error handling.

As in https://github.com/koajs/koa/issues/180#issuecomment-32126654

// what you have to do currently
// ugly as fuck
app.use(function*(){
  var a = streamA().on('error', this.onerror);
  var b = streamB().on('error', this.onerror);
  this.body = a.pipe(b).pipe(streamC());
});

With multipipe.

// with `multipipe`
app.use(function*(){
  this.body = pipe(streamA(), streamB(), streamC());
});
// for fs leaks, also accept functions as this.body
// and call them when no other handler is set
// leaky as fuck
app.use(function*(){
  this.body = fs.createReadStream.bind(null, 'file.html');
});

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 36 (34 by maintainers)

Most upvoted comments

@rtm

var archiver = new Archiver('zip');
this.body = archiver;
// ...
archiver.finalize();