Advertisement
robertbira

Italian Translation Report: Node.js [Part 20 - 1133 words]

Aug 3rd, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. Stability: 2 - Stable
  2. A stream is an abstract interface for working with streaming data in Node.js.
  3. The module provides a base API that makes it easy to build objects that implement the stream interface.
  4. There are many stream objects provided by Node.js.
  5. For instance, a and are both stream instances.
  6. Streams can be readable, writable, or both. All streams are instances of
  7. The module can be accessed using:
  8. While it is important to understand how streams work, the module itself is most useful for developers that are creating new types of stream instances. Developers who are primarily consuming stream objects will rarely need to use the module directly.
  9. Organization of this Document
  10. This document is divided into two primary sections with a third section for additional notes. The first section explains the elements of the stream API that are required to use streams within an application. The second section explains the elements of the API that are required to implement new types of streams.
  11. Types of Streams
  12. There are four fundamental stream types within Node.js:
  13. streams from which data can be read (for example
  14. streams to which data can be written
  15. streams that are both and
  16. streams that can modify or transform the data as it is written and read
  17. Additionally this module includes the utility functions and
  18. All streams created by Node.js APIs operate exclusively on strings and (or) objects.
  19. It is possible, however, for stream implementations to work with other types of JavaScript values (with the exception of, which serves a special purpose within streams).
  20. Such streams are considered to operate in "object mode".
  21. Stream instances are switched into object mode using the option when the stream is created. Attempting to switch an existing stream into object mode is not safe.
  22. Both and streams will store data in an internal buffer that can be retrieved using or, respectively.
  23. The amount of data potentially buffered depends on the option passed into the streams constructor.
  24. For normal streams, the option specifies a total number of bytes
  25. For streams operating in object mode, the specifies a total number of objects.
  26. Data is buffered in streams when the implementation calls
  27. If the consumer of the Stream does not call, the data will sit in the internal queue until it is consumed.
  28. Once the total size of the internal read buffer reaches the threshold specified by, the stream will temporarily stop reading data from the underlying resource until the data currently buffered can be consumed (that is, the stream will stop calling the internal method that is used to fill the read buffer).
  29. Data is buffered in streams when the method is called repeatedly.
  30. While the total size of the internal write buffer is below the threshold set by, calls to will return
  31. Once the size of the internal buffer reaches or exceeds the will be returned.
  32. A key goal of the API, particularly the method, is to limit the buffering of data to acceptable levels such that sources and destinations of differing speeds will not overwhelm the available memory.
  33. Because and streams are both and, each maintain two separate internal buffers used for reading and writing, allowing each side to operate independently of the other while maintaining an appropriate and efficient flow of data.
  34. For example, instances are streams whose side allows consumption of data received from the socket and whose side allows writing data to the socket.
  35. Because data may be written to the socket at a faster or slower rate than data is received, it is important for each side to operate (and buffer) independently of the other.
  36. API for Stream Consumers
  37. Almost all Node.js applications, no matter how simple, use streams in some manner. The following is an example of using streams in a Node.js application that implements an HTTP server:
  38. streams (such as in the example) expose methods such as and that are used to write data onto the stream.
  39. streams use the API for notifying application code when data is available to be read off the stream.
  40. That available data can be read from the stream in multiple ways.
  41. Both and streams use the API in various ways to communicate the current state of the stream.
  42. and streams are both and
  43. Applications that are either writing data to or consuming data from a stream are not required to implement the stream interfaces directly and will generally have no reason to call
  44. Developers wishing to implement new types of streams should refer to the section API for Stream Implementers.
  45. Writable Streams
  46. Writable streams are an abstraction for a destination to which data is written.
  47. Examples of streams include:
  48. Some of these examples are actually streams that implement the interface.
  49. All streams implement the interface defined by the class.
  50. While specific instances of streams may differ in various ways, all streams follow the same fundamental usage pattern as illustrated in the example below:
  51. The event is emitted when the stream and any of its underlying resources (a file descriptor, for example) have been closed. The event indicates that no more events will be emitted, and no further computation will occur.
  52. Not all streams will emit the event.
  53. If a call to returns, the event will be emitted when it is appropriate to resume writing data to the stream.
  54. Write the data to the supplied writable stream one million times.
  55. Be attentive to back-pressure.
  56. see if we should continue, or wait
  57. don't pass the callback, because we're not done yet.
  58. had to stop early!
  59. write some more once it drains
  60. The event is emitted if an error occurred while writing or piping data. The listener callback is passed a single argument when called.
  61. The stream is not closed when the event is emitted.
  62. The event is emitted after the method has been called, and all data has been flushed to the underlying system.
  63. source stream that is piping to this writable
  64. The event is emitted when the method is called on a readable stream, adding this writable to its set of destinations.
  65. This is also emitted in case this stream emits an error when a stream pipes into it.
  66. The method forces all written data to be buffered in memory.
  67. The buffered data will be flushed when either the or methods are called.
  68. The primary intent of is to avoid a situation where writing many small chunks of data to a stream do not cause a backup in the internal buffer that would have an adverse impact on performance.
  69. In such situations, implementations that implement the method can perform buffered writes in a more optimized manner.
  70. See also:
  71. Destroy the stream, and emit the passed and a event.
  72. After this call, the writable stream has ended and subsequent calls to will give an error.
  73. Implementors should not override this method, but instead implement
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement