stream: add destroy and _destroy methods.

Adds destroy() and _destroy() methods to Readable, Writable, Duplex
and Transform. It also standardizes the behavior and the implementation
of destroy(), which has been inconsistent in userland and core.
This PR also updates all the subsystems of core to use the new
destroy().

PR-URL: https://github.com/nodejs/node/pull/12925
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Calvin Metcalf <calvin.metcalf@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Matteo Collina committed 2017-05-22 08:34:14 +02:00
1 parent d54ec726cc
commit 330c8d743e
18 files changed
+964 -68

No files matched your search

+47 -1
View File
@@ -499,6 +499,15 @@ write('hello', () => {
A Writable stream in object mode will always ignore the `encoding` argument.
##### writable.destroy([error])
<!-- YAML
added: REPLACEME
-->
Destroy the stream, and emit the passed error. After this call, the
writible stream has ended. Implementors should not override this method,
but instead implement [`writable._destroy`][writable-_destroy].
### Readable Streams
Readable streams are an abstraction for a *source* from which data is
@@ -1070,6 +1079,16 @@ myReader.on('readable', () => {
});
```
##### readable.destroy([error])
<!-- YAML
added: REPLACEME
-->
Destroy the stream, and emit `'error'`. After this call, the
readable stream will release any internal resources.
Implementors should not override this method, but instead implement
[`readable._destroy`][readable-_destroy].
### Duplex and Transform Streams
#### Class: stream.Duplex
@@ -1109,6 +1128,16 @@ Examples of Transform streams include:
* [zlib streams][zlib]
* [crypto streams][crypto]
##### transform.destroy([error])
<!-- YAML
added: REPLACEME
-->
Destroy the stream, and emit `'error'`. After this call, the
transform stream would release any internal resources.
implementors should not override this method, but instead implement
[`readable._destroy`][readable-_destroy].
The default implementation of `_destroy` for `Transform` also emit `'close'`.
## API for Stream Implementers
@@ -1247,6 +1276,8 @@ constructor and implement the `writable._write()` method. The
[`stream._write()`][stream-_write] method.
* `writev` {Function} Implementation for the
[`stream._writev()`][stream-_writev] method.
* `destroy` {Function} Implementation for the
[`stream._destroy()`][writable-_destroy] method.
For example:
@@ -1356,6 +1387,15 @@ The `writable._writev()` method is prefixed with an underscore because it is
internal to the class that defines it, and should never be called directly by
user programs.
#### writable.\_destroy(err, callback)
<!-- YAML
added: REPLACEME
-->
* `err` {Error} An error.
* `callback` {Function} A callback function that takes an optional error argument
which is invoked when the writable is destroyed.
#### Errors While Writing
It is recommended that errors occurring during the processing of the
@@ -1425,6 +1465,8 @@ constructor and implement the `readable._read()` method.
a single value instead of a Buffer of size n. Defaults to `false`
* `read` {Function} Implementation for the [`stream._read()`][stream-_read]
method.
* `destroy` {Function} Implementation for the [`stream._destroy()`][readable-_destroy]
method.
For example:
@@ -2073,4 +2115,8 @@ readable buffer so there is nothing for a user to consume.
[stream-read]: #stream_readable_read_size
[stream-resume]: #stream_readable_resume
[stream-write]: #stream_writable_write_chunk_encoding_callback
[zlib]: zlib.html
[readable-_destroy]: #stream_readable_destroy_err_callback
[writable-_destroy]: #stream_writable_destroy_err_callback
[TCP sockets]: net.html#net_class_net_socket
[Transform]: #stream_class_stream_transform
[Writable]: #stream_class_stream_writable