npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

on-finished-lifo

v3.0.1

Published

Execute a callback when a request closes, finishes, or errors

Downloads

3

Readme

on-finished-lifo

NPM Version NPM Downloads Node.js Version

Execute a callback when a HTTP request closes, finishes, or errors.

Difference from jshttp/on-finished

  • Invoke callbacks in LIFO order (similar to stack unwinding) instead of FIFO.
  • onFinished returns a function for removing listeners instead of msg.

Install

$ npm install on-finished-lifo

API

var onFinished = require('on-finished-lifo')

onFinished(res, listener)

Attach a listener to listen for the response to finish. The listener will be invoked only once when the response finished. If the response finished to an error, the first argument will contain the error. If the response has already finished, the listener will be invoked.

Listening to the end of a response would be used to close things associated with the response, like open files.

Listener is invoked as listener(err, res).

onFinished(res, function (err, res) {
  // clean up open fds, etc.
  // err contains the error is request error'd
})

onFinished(req, listener)

Attach a listener to listen for the request to finish. The listener will be invoked only once when the request finished. If the request finished to an error, the first argument will contain the error. If the request has already finished, the listener will be invoked.

Listening to the end of a request would be used to know when to continue after reading the data.

Listener is invoked as listener(err, req).

var data = ''

req.setEncoding('utf8')
res.on('data', function (str) {
  data += str
})

onFinished(req, function (err, req) {
  // data is read unless there is err
})

onFinished.isFinished(res)

Determine if res is already finished. This would be useful to check and not even start certain operations if the response has already finished.

onFinished.isFinished(req)

Determine if req is already finished. This would be useful to check and not even start certain operations if the request has already finished.

Special Node.js requests

HTTP CONNECT method

The meaning of the CONNECT method from RFC 7231, section 4.3.6:

The CONNECT method requests that the recipient establish a tunnel to the destination origin server identified by the request-target and, if successful, thereafter restrict its behavior to blind forwarding of packets, in both directions, until the tunnel is closed. Tunnels are commonly used to create an end-to-end virtual connection, through one or more proxies, which can then be secured using TLS (Transport Layer Security, [RFC5246]).

In Node.js, these request objects come from the 'connect' event on the HTTP server.

When this module is used on a HTTP CONNECT request, the request is considered "finished" immediately, due to limitations in the Node.js interface. This means if the CONNECT request contains a request entity, the request will be considered "finished" even before it has been read.

There is no such thing as a response object to a CONNECT request in Node.js, so there is no support for for one.

HTTP Upgrade request

The meaning of the Upgrade header from RFC 7230, section 6.1:

The "Upgrade" header field is intended to provide a simple mechanism for transitioning from HTTP/1.1 to some other protocol on the same connection.

In Node.js, these request objects come from the 'upgrade' event on the HTTP server.

When this module is used on a HTTP request with an Upgrade header, the request is considered "finished" immediately, due to limitations in the Node.js interface. This means if the Upgrade request contains a request entity, the request will be considered "finished" even before it has been read.

There is no such thing as a response object to a Upgrade request in Node.js, so there is no support for for one.

Example

The following code ensures that file descriptors are always closed once the response finishes.

var destroy = require('destroy')
var http = require('http')
var onFinished = require('on-finished')

http.createServer(function onRequest(req, res) {
  var stream = fs.createReadStream('package.json')
  stream.pipe(res)
  onFinished(res, function (err) {
    destroy(stream)
  })
})

License

MIT