pump, pumpify, and end-of-stream

In notebook:
FrontEndMasters Networking and Streams
Created at:
2017-10-01
Updated:
2017-10-01
Tags:
backend Node JS JavaScript

pump

When you build up your app with several pipe steps, and one of them throws an error, the whole app (NodeJS) will crash. It's by design.

pump will help you make your app more robust.

var pump = require('pump')

pump(stream1, stream2, stream3, ...)

This above pattern will handle errors and is the same as:

stream
  .pipe(a)
  .pipe(b)
  .pipe(c)

So it's like function composition. Instead of . chaining your stream steps, you compose them inside pump(..). You can put your error handling function as the last step.


pumpify

pumpify will return you a stream that you can read and write from. It's more useful.

var pump = require('pumpify')

var stream = pump(stream1, stream2, stream3, ...)

end-of-stream

reliably detect when a stream is finished (enough said...)

It's actually quite complex to detect when a stream has really ended, there are lots of edge cases. This module helps with this. You put your cleanup logic in one place.

var onend = require('end-of-stream')
var net = require('net')

var server = net.createServer(function (stream) {
  var iv = setInterval(function () {
    stream.write(Date.now() + '\n')
  }, 1000)
  // the done function when the stream ends for whatever reason
  onend(stream, function () {
    clearInterval(iv)
  })
})
server.listen(5000)