[d24f17c] | 1 | # destroy
|
---|
| 2 |
|
---|
| 3 | [![NPM version][npm-image]][npm-url]
|
---|
| 4 | [![Build Status][github-actions-ci-image]][github-actions-ci-url]
|
---|
| 5 | [![Test coverage][coveralls-image]][coveralls-url]
|
---|
| 6 | [![License][license-image]][license-url]
|
---|
| 7 | [![Downloads][downloads-image]][downloads-url]
|
---|
| 8 |
|
---|
| 9 | Destroy a stream.
|
---|
| 10 |
|
---|
| 11 | This module is meant to ensure a stream gets destroyed, handling different APIs
|
---|
| 12 | and Node.js bugs.
|
---|
| 13 |
|
---|
| 14 | ## API
|
---|
| 15 |
|
---|
| 16 | ```js
|
---|
| 17 | var destroy = require('destroy')
|
---|
| 18 | ```
|
---|
| 19 |
|
---|
| 20 | ### destroy(stream [, suppress])
|
---|
| 21 |
|
---|
| 22 | Destroy the given stream, and optionally suppress any future `error` events.
|
---|
| 23 |
|
---|
| 24 | In most cases, this is identical to a simple `stream.destroy()` call. The rules
|
---|
| 25 | are as follows for a given stream:
|
---|
| 26 |
|
---|
| 27 | 1. If the `stream` is an instance of `ReadStream`, then call `stream.destroy()`
|
---|
| 28 | and add a listener to the `open` event to call `stream.close()` if it is
|
---|
| 29 | fired. This is for a Node.js bug that will leak a file descriptor if
|
---|
| 30 | `.destroy()` is called before `open`.
|
---|
| 31 | 2. If the `stream` is an instance of a zlib stream, then call `stream.destroy()`
|
---|
| 32 | and close the underlying zlib handle if open, otherwise call `stream.close()`.
|
---|
| 33 | This is for consistency across Node.js versions and a Node.js bug that will
|
---|
| 34 | leak a native zlib handle.
|
---|
| 35 | 3. If the `stream` is not an instance of `Stream`, then nothing happens.
|
---|
| 36 | 4. If the `stream` has a `.destroy()` method, then call it.
|
---|
| 37 |
|
---|
| 38 | The function returns the `stream` passed in as the argument.
|
---|
| 39 |
|
---|
| 40 | ## Example
|
---|
| 41 |
|
---|
| 42 | ```js
|
---|
| 43 | var destroy = require('destroy')
|
---|
| 44 |
|
---|
| 45 | var fs = require('fs')
|
---|
| 46 | var stream = fs.createReadStream('package.json')
|
---|
| 47 |
|
---|
| 48 | // ... and later
|
---|
| 49 | destroy(stream)
|
---|
| 50 | ```
|
---|
| 51 |
|
---|
| 52 | [npm-image]: https://img.shields.io/npm/v/destroy.svg?style=flat-square
|
---|
| 53 | [npm-url]: https://npmjs.org/package/destroy
|
---|
| 54 | [github-tag]: http://img.shields.io/github/tag/stream-utils/destroy.svg?style=flat-square
|
---|
| 55 | [github-url]: https://github.com/stream-utils/destroy/tags
|
---|
| 56 | [coveralls-image]: https://img.shields.io/coveralls/stream-utils/destroy.svg?style=flat-square
|
---|
| 57 | [coveralls-url]: https://coveralls.io/r/stream-utils/destroy?branch=master
|
---|
| 58 | [license-image]: http://img.shields.io/npm/l/destroy.svg?style=flat-square
|
---|
| 59 | [license-url]: LICENSE.md
|
---|
| 60 | [downloads-image]: http://img.shields.io/npm/dm/destroy.svg?style=flat-square
|
---|
| 61 | [downloads-url]: https://npmjs.org/package/destroy
|
---|
| 62 | [github-actions-ci-image]: https://img.shields.io/github/workflow/status/stream-utils/destroy/ci/master?label=ci&style=flat-square
|
---|
| 63 | [github-actions-ci-url]: https://github.com/stream-utils/destroy/actions/workflows/ci.yml
|
---|