bc7ededd4f7b27cb28a47895e9ff239e55dce77dc1cddbe3bb603462d44f564fd1c4a536f626b3d69cd1bab15920f65cf7ab0683fcfc7f33872b1242f2e208 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # on-finished
  2. [![NPM Version][npm-version-image]][npm-url]
  3. [![NPM Downloads][npm-downloads-image]][npm-url]
  4. [![Node.js Version][node-image]][node-url]
  5. [![Build Status][ci-image]][ci-url]
  6. [![Coverage Status][coveralls-image]][coveralls-url]
  7. Execute a callback when a HTTP request closes, finishes, or errors.
  8. ## Install
  9. This is a [Node.js](https://nodejs.org/en/) module available through the
  10. [npm registry](https://www.npmjs.com/). Installation is done using the
  11. [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
  12. ```sh
  13. $ npm install on-finished
  14. ```
  15. ## API
  16. ```js
  17. var onFinished = require('on-finished')
  18. ```
  19. ### onFinished(res, listener)
  20. Attach a listener to listen for the response to finish. The listener will
  21. be invoked only once when the response finished. If the response finished
  22. to an error, the first argument will contain the error. If the response
  23. has already finished, the listener will be invoked.
  24. Listening to the end of a response would be used to close things associated
  25. with the response, like open files.
  26. Listener is invoked as `listener(err, res)`.
  27. <!-- eslint-disable handle-callback-err -->
  28. ```js
  29. onFinished(res, function (err, res) {
  30. // clean up open fds, etc.
  31. // err contains the error if request error'd
  32. })
  33. ```
  34. ### onFinished(req, listener)
  35. Attach a listener to listen for the request to finish. The listener will
  36. be invoked only once when the request finished. If the request finished
  37. to an error, the first argument will contain the error. If the request
  38. has already finished, the listener will be invoked.
  39. Listening to the end of a request would be used to know when to continue
  40. after reading the data.
  41. Listener is invoked as `listener(err, req)`.
  42. <!-- eslint-disable handle-callback-err -->
  43. ```js
  44. var data = ''
  45. req.setEncoding('utf8')
  46. req.on('data', function (str) {
  47. data += str
  48. })
  49. onFinished(req, function (err, req) {
  50. // data is read unless there is err
  51. })
  52. ```
  53. ### onFinished.isFinished(res)
  54. Determine if `res` is already finished. This would be useful to check and
  55. not even start certain operations if the response has already finished.
  56. ### onFinished.isFinished(req)
  57. Determine if `req` is already finished. This would be useful to check and
  58. not even start certain operations if the request has already finished.
  59. ## Special Node.js requests
  60. ### HTTP CONNECT method
  61. The meaning of the `CONNECT` method from RFC 7231, section 4.3.6:
  62. > The CONNECT method requests that the recipient establish a tunnel to
  63. > the destination origin server identified by the request-target and,
  64. > if successful, thereafter restrict its behavior to blind forwarding
  65. > of packets, in both directions, until the tunnel is closed. Tunnels
  66. > are commonly used to create an end-to-end virtual connection, through
  67. > one or more proxies, which can then be secured using TLS (Transport
  68. > Layer Security, [RFC5246]).
  69. In Node.js, these request objects come from the `'connect'` event on
  70. the HTTP server.
  71. When this module is used on a HTTP `CONNECT` request, the request is
  72. considered "finished" immediately, **due to limitations in the Node.js
  73. interface**. This means if the `CONNECT` request contains a request entity,
  74. the request will be considered "finished" even before it has been read.
  75. There is no such thing as a response object to a `CONNECT` request in
  76. Node.js, so there is no support for one.
  77. ### HTTP Upgrade request
  78. The meaning of the `Upgrade` header from RFC 7230, section 6.1:
  79. > The "Upgrade" header field is intended to provide a simple mechanism
  80. > for transitioning from HTTP/1.1 to some other protocol on the same
  81. > connection.
  82. In Node.js, these request objects come from the `'upgrade'` event on
  83. the HTTP server.
  84. When this module is used on a HTTP request with an `Upgrade` header, the
  85. request is considered "finished" immediately, **due to limitations in the
  86. Node.js interface**. This means if the `Upgrade` request contains a request
  87. entity, the request will be considered "finished" even before it has been
  88. read.
  89. There is no such thing as a response object to a `Upgrade` request in
  90. Node.js, so there is no support for one.
  91. ## Example
  92. The following code ensures that file descriptors are always closed
  93. once the response finishes.
  94. ```js
  95. var destroy = require('destroy')
  96. var fs = require('fs')
  97. var http = require('http')
  98. var onFinished = require('on-finished')
  99. http.createServer(function onRequest (req, res) {
  100. var stream = fs.createReadStream('package.json')
  101. stream.pipe(res)
  102. onFinished(res, function () {
  103. destroy(stream)
  104. })
  105. })
  106. ```
  107. ## License
  108. [MIT](LICENSE)
  109. [ci-image]: https://badgen.net/github/checks/jshttp/on-finished/master?label=ci
  110. [ci-url]: https://github.com/jshttp/on-finished/actions/workflows/ci.yml
  111. [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/on-finished/master
  112. [coveralls-url]: https://coveralls.io/r/jshttp/on-finished?branch=master
  113. [node-image]: https://badgen.net/npm/node/on-finished
  114. [node-url]: https://nodejs.org/en/download
  115. [npm-downloads-image]: https://badgen.net/npm/dm/on-finished
  116. [npm-url]: https://npmjs.org/package/on-finished
  117. [npm-version-image]: https://badgen.net/npm/v/on-finished