CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
README.md163 linesDownload Raw Back to on-finished
1# on-finished2 3[![NPM Version][npm-version-image]][npm-url]4[![NPM Downloads][npm-downloads-image]][npm-url]5[![Node.js Version][node-image]][node-url]6[![Build Status][ci-image]][ci-url]7[![Coverage Status][coveralls-image]][coveralls-url]8 9Execute a callback when a HTTP request closes, finishes, or errors.10 11## Install12 13This is a [Node.js](https://nodejs.org/en/) module available through the14[npm registry](https://www.npmjs.com/). Installation is done using the15[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):16 17```sh18$ npm install on-finished19```20 21## API22 23```js24var onFinished = require('on-finished')25```26 27### onFinished(res, listener)28 29Attach a listener to listen for the response to finish. The listener will30be invoked only once when the response finished. If the response finished31to an error, the first argument will contain the error. If the response32has already finished, the listener will be invoked.33 34Listening to the end of a response would be used to close things associated35with the response, like open files.36 37Listener is invoked as `listener(err, res)`.38 39<!-- eslint-disable handle-callback-err -->40 41```js42onFinished(res, function (err, res) {43  // clean up open fds, etc.44  // err contains the error if request error'd45})46```47 48### onFinished(req, listener)49 50Attach a listener to listen for the request to finish. The listener will51be invoked only once when the request finished. If the request finished52to an error, the first argument will contain the error. If the request53has already finished, the listener will be invoked.54 55Listening to the end of a request would be used to know when to continue56after reading the data.57 58Listener is invoked as `listener(err, req)`.59 60<!-- eslint-disable handle-callback-err -->61 62```js63var data = ''64 65req.setEncoding('utf8')66req.on('data', function (str) {67  data += str68})69 70onFinished(req, function (err, req) {71  // data is read unless there is err72})73```74 75### onFinished.isFinished(res)76 77Determine if `res` is already finished. This would be useful to check and78not even start certain operations if the response has already finished.79 80### onFinished.isFinished(req)81 82Determine if `req` is already finished. This would be useful to check and83not even start certain operations if the request has already finished.84 85## Special Node.js requests86 87### HTTP CONNECT method88 89The meaning of the `CONNECT` method from RFC 7231, section 4.3.6:90 91> The CONNECT method requests that the recipient establish a tunnel to92> the destination origin server identified by the request-target and,93> if successful, thereafter restrict its behavior to blind forwarding94> of packets, in both directions, until the tunnel is closed.  Tunnels95> are commonly used to create an end-to-end virtual connection, through96> one or more proxies, which can then be secured using TLS (Transport97> Layer Security, [RFC5246]).98 99In Node.js, these request objects come from the `'connect'` event on100the HTTP server.101 102When this module is used on a HTTP `CONNECT` request, the request is103considered "finished" immediately, **due to limitations in the Node.js104interface**. This means if the `CONNECT` request contains a request entity,105the request will be considered "finished" even before it has been read.106 107There is no such thing as a response object to a `CONNECT` request in108Node.js, so there is no support for one.109 110### HTTP Upgrade request111 112The meaning of the `Upgrade` header from RFC 7230, section 6.1:113 114> The "Upgrade" header field is intended to provide a simple mechanism115> for transitioning from HTTP/1.1 to some other protocol on the same116> connection.117 118In Node.js, these request objects come from the `'upgrade'` event on119the HTTP server.120 121When this module is used on a HTTP request with an `Upgrade` header, the122request is considered "finished" immediately, **due to limitations in the123Node.js interface**. This means if the `Upgrade` request contains a request124entity, the request will be considered "finished" even before it has been125read.126 127There is no such thing as a response object to a `Upgrade` request in128Node.js, so there is no support for one.129 130## Example131 132The following code ensures that file descriptors are always closed133once the response finishes.134 135```js136var destroy = require('destroy')137var fs = require('fs')138var http = require('http')139var onFinished = require('on-finished')140 141http.createServer(function onRequest (req, res) {142  var stream = fs.createReadStream('package.json')143  stream.pipe(res)144  onFinished(res, function () {145    destroy(stream)146  })147})148```149 150## License151 152[MIT](LICENSE)153 154[ci-image]: https://badgen.net/github/checks/jshttp/on-finished/master?label=ci155[ci-url]: https://github.com/jshttp/on-finished/actions/workflows/ci.yml156[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/on-finished/master157[coveralls-url]: https://coveralls.io/r/jshttp/on-finished?branch=master158[node-image]: https://badgen.net/npm/node/on-finished159[node-url]: https://nodejs.org/en/download160[npm-downloads-image]: https://badgen.net/npm/dm/on-finished161[npm-url]: https://npmjs.org/package/on-finished162[npm-version-image]: https://badgen.net/npm/v/on-finished163