All Projects → jshttp → On Finished

jshttp / On Finished

Licence: mit
Execute a callback when a request closes, finishes, or errors

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to On Finished

purescript-wire
Events and Signals for FRP. Monad instances included
Stars: ✭ 13 (-96.12%)
Mutual labels:  event
growth.dev
[EVENT] API & IPA
Stars: ✭ 46 (-86.27%)
Mutual labels:  event
spa-bus
🔥Tools for multilevel components to pass values in any SPA
Stars: ✭ 15 (-95.52%)
Mutual labels:  event
react-known-props
About 700 props React recognizes
Stars: ✭ 45 (-86.57%)
Mutual labels:  event
event
The implementation of the pattern observer
Stars: ✭ 45 (-86.57%)
Mutual labels:  event
summit-app-ios
The official app for the OpenStack Summit
Stars: ✭ 35 (-89.55%)
Mutual labels:  event
ReactiveBus
🚍 Reactive Event Bus for JVM (1.7+) and Android apps built with RxJava 2
Stars: ✭ 17 (-94.93%)
Mutual labels:  event
Ics Py
Pythonic and easy iCalendar library (rfc5545)
Stars: ✭ 322 (-3.88%)
Mutual labels:  event
proffy
Plataforma de estudos online para conectar alunos e professores de forma rápida e fácil. @Rocketseat
Stars: ✭ 21 (-93.73%)
Mutual labels:  event
Multiplatform-Bus
Kotlin event-bus compatible with Android & native iOS
Stars: ✭ 43 (-87.16%)
Mutual labels:  event
moodle-tool trigger
Like IFTTT for Moodle: events to trigger external services. https://moodle.org/plugins/tool_trigger
Stars: ✭ 32 (-90.45%)
Mutual labels:  event
nativescript-calendar
📅 NativeScript plugin to Create, Delete and Find Events in the native Calendar
Stars: ✭ 44 (-86.87%)
Mutual labels:  event
jsheroes.io
The official JSHeroes website
Stars: ✭ 35 (-89.55%)
Mutual labels:  event
egg-bus
🐣 用 egg 编写优雅的队列与事件
Stars: ✭ 38 (-88.66%)
Mutual labels:  event
dom-event-simulate
simulate user interaction with DOM events.
Stars: ✭ 23 (-93.13%)
Mutual labels:  event
add2calendar
📆 Allow you to add event to calendar easier
Stars: ✭ 51 (-84.78%)
Mutual labels:  event
async-script-loader
Asynchronous script loading for SPAs
Stars: ✭ 15 (-95.52%)
Mutual labels:  event
Swoole Src
🚀 Coroutine-based concurrency library for PHP
Stars: ✭ 17,175 (+5026.87%)
Mutual labels:  event
Event
The Hoa\Event library
Stars: ✭ 319 (-4.78%)
Mutual labels:  event
EventHandlerInSingleApplication
A sample project about how to create event subscribe/publish feature in single application in asp.net core
Stars: ✭ 16 (-95.22%)
Mutual labels:  event

on-finished

NPM Version NPM Downloads Node.js Version Build Status Coverage Status

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

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install on-finished

API

var onFinished = require('on-finished')

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 if 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')
req.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 fs = require('fs')
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 () {
    destroy(stream)
  })
})

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].