You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.1 KiB

4 years ago
  1. # Destroy
  2. [![NPM version][npm-image]][npm-url]
  3. [![Build status][travis-image]][travis-url]
  4. [![Test coverage][coveralls-image]][coveralls-url]
  5. [![License][license-image]][license-url]
  6. [![Downloads][downloads-image]][downloads-url]
  7. [![Gittip][gittip-image]][gittip-url]
  8. Destroy a stream.
  9. This module is meant to ensure a stream gets destroyed, handling different APIs
  10. and Node.js bugs.
  11. ## API
  12. ```js
  13. var destroy = require('destroy')
  14. ```
  15. ### destroy(stream)
  16. Destroy the given stream. In most cases, this is identical to a simple
  17. `stream.destroy()` call. The rules are as follows for a given stream:
  18. 1. If the `stream` is an instance of `ReadStream`, then call `stream.destroy()`
  19. and add a listener to the `open` event to call `stream.close()` if it is
  20. fired. This is for a Node.js bug that will leak a file descriptor if
  21. `.destroy()` is called before `open`.
  22. 2. If the `stream` is not an instance of `Stream`, then nothing happens.
  23. 3. If the `stream` has a `.destroy()` method, then call it.
  24. The function returns the `stream` passed in as the argument.
  25. ## Example
  26. ```js
  27. var destroy = require('destroy')
  28. var fs = require('fs')
  29. var stream = fs.createReadStream('package.json')
  30. // ... and later
  31. destroy(stream)
  32. ```
  33. [npm-image]: https://img.shields.io/npm/v/destroy.svg?style=flat-square
  34. [npm-url]: https://npmjs.org/package/destroy
  35. [github-tag]: http://img.shields.io/github/tag/stream-utils/destroy.svg?style=flat-square
  36. [github-url]: https://github.com/stream-utils/destroy/tags
  37. [travis-image]: https://img.shields.io/travis/stream-utils/destroy.svg?style=flat-square
  38. [travis-url]: https://travis-ci.org/stream-utils/destroy
  39. [coveralls-image]: https://img.shields.io/coveralls/stream-utils/destroy.svg?style=flat-square
  40. [coveralls-url]: https://coveralls.io/r/stream-utils/destroy?branch=master
  41. [license-image]: http://img.shields.io/npm/l/destroy.svg?style=flat-square
  42. [license-url]: LICENSE.md
  43. [downloads-image]: http://img.shields.io/npm/dm/destroy.svg?style=flat-square
  44. [downloads-url]: https://npmjs.org/package/destroy
  45. [gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square
  46. [gittip-url]: https://www.gittip.com/jonathanong/