Skip to content

Manually Closing Streams

janek42 edited this page Jul 24, 2022 · 4 revisions

When using Knex's stream interface, you can typically just pipe the return stream to any writable stream. However, with HTTPIncomingMessage, you'll need to take special care to handle aborted requests.

An HTTPIncomingMessage object is typically called request. This is the first argument in 'request' events emitted on http.Server instances. Express's req implements a compatible interface and Hapi exposes this object on its request objects as request.raw.req.

You need to explicitly handle the case where an HTTPIncomingMessage is closed prematurely when streaming from a database with Knex. The easiest way to cause this is:

  1. Visit an endpoint that takes several seconds to fully transmit a response
  2. Close the browser window immediately after beginning the request

When this happens while you are streaming a query to a client, you need to manually tell Knex that it can release the database connection in use back to the connection pool.

server.on('request', function (request, response) {
  var stream = knex.select('*').from('items').stream();
  request.on('close', stream.end.bind(stream));
});