JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3787182
  • Score
    100M100P100Q238443F
  • License MIT

Package Exports

  • stoppable

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stoppable) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Stoppable

const server = stoppable(http.createServer(handler))
server.stop()

This module implements Node's server.close() in the way you probably expected it to work by default: It stops accepting new connections and closes existing, idle connections (including keep-alives) without killing requests that are in-flight.

Installation

yarn add stoppable

(or use npm)

Usage

constructor

stoppable(server, grace)

Decorates the server instance with a stop method. Returns the server instance, so can be chained, or can be run as a standalone statement.

  • server: Any HTTP or HTTPS Server instance
  • grace: Milliseconds to wait before force-closing connections

grace defaults to Infinity (don't force-close). If you want to immediately kill all sockets you can use a grace of 0.

stop()

server.stop(callback)

Closes the server.

  • callback: passed along to the existing server.close function to auto-register a 'close' event

Design decisions

  • Monkey patching generally sucks, but in this case it's the nicest API. Let's call it "decorating."
  • grace could be specified on stop, but it's better to match the existing server.close API.
  • Clients should be handled respectfully, so we aren't just destroying sockets, we're sending FIN packets first.
  • Any solution to this problem requires bookkeeping on every connection and request/response. We're doing a minimum of work on these "hot" code paths and delaying as much as possible to the actual stop method.