Advertisement
robertbira

Italian Translation Report: Node.js [Part 22 - 1371 words]

Aug 7th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. Three States
  2. The "two modes" of operation for a stream are a simplified abstraction for the more complicated internal state management that is happening within the stream implementation.
  3. Specifically, at any given point in time, every is in one of three possible states:
  4. When is, no mechanism for consuming the streams data is provided so the stream will not generate its data.
  5. While in this state, attaching a listener for the event, calling the method, or calling the method will switch to, causing the, to begin actively emitting events as data is generated.
  6. Calling, or receiving "back pressure" will cause the to be set as, temporarily halting the flowing of events but not halting the generation of data.
  7. While in this state, attaching a listener for the event would not cause to switch to
  8. is now false
  9. will not emit
  10. must be called to make being emitted
  11. While is, data may be accumulating within the streams internal buffer.
  12. Choose One
  13. The stream API evolved across multiple Node.js versions and provides multiple methods of consuming stream data.
  14. In general, developers should choose one of the methods of consuming data and should never use multiple methods to consume data from a single stream.
  15. Use of the method is recommended for most users as it has been implemented to provide the easiest way of consuming stream data.
  16. Developers that require more fine-grained control over the transfer and generation of data can use the and APIs.
  17. The chunk of data.
  18. For streams that are not operating in object mode, the chunk will be either a string or
  19. For streams that are in object mode, the chunk can be any JavaScript value other than
  20. The event is emitted whenever the stream is relinquishing ownership of a chunk of data to a consumer.
  21. This may occur whenever the stream is switched in flowing mode by calling, or by attaching a listener callback to the event.
  22. The event will also be emitted whenever the method is called and a chunk of data is available to be returned.
  23. Attaching a event listener to a stream that has not been explicitly paused will switch the stream into flowing mode.
  24. Data will then be passed as soon as it is available.
  25. The listener callback will be passed the chunk of data as a string if a default encoding has been specified for the stream using the method; otherwise the data will be passed as a
  26. The event is emitted when there is no more data to be consumed from the stream.
  27. The event will not be emitted unless the data is completely consumed.
  28. This can be accomplished by switching the stream into flowing mode, or by calling repeatedly until all data has been consumed.
  29. The event may be emitted by a implementation at any time.
  30. Typically, this may occur if the underlying stream is unable to generate data due to an underlying internal failure, or when a stream implementation attempts to push an invalid chunk of data.
  31. The listener callback will be passed a single object.
  32. The event is emitted when there is data available to be read from the stream
  33. In some cases, attaching a listener for the event will cause some amount of data to be read into an internal buffer.
  34. there is some data to read now
  35. The event will also be emitted once the end of the stream data has been reached but before the event is emitted.
  36. Effectively, the event indicates that the stream has new information: either new data is available or the end of the stream has been reached.
  37. In the former case, will return the available data.
  38. In the latter case, will return
  39. For instance, in the following example, is an empty file:
  40. The output of running this script is:
  41. In general, the and event mechanisms are easier to understand than the event.
  42. However, handling might result in increased throughput.
  43. If both and are used at the same time, takes precedence in controlling the flow, i.e. will be emitted only when is called.
  44. Error which will be passed as payload in event
  45. Destroy the stream, and emit and
  46. After this call, the readable stream will release any internal resources and subsequent calls to will be ignored.
  47. Implementors should not override this method, but instead implement
  48. The method returns the current operating state of the
  49. This is used primarily by the mechanism that underlies the method.
  50. In most typical cases, there will be no reason to use this method directly.
  51. The method will cause a stream in flowing mode to stop emitting events, switching out of flowing mode.
  52. Any data that becomes available will remain in the internal buffer.
  53. The destination for writing data
  54. Pipe options
  55. End the writer when the reader ends.
  56. making it possible to set up chains of piped streams
  57. The method attaches a stream to the causing it to switch automatically into flowing mode and push all of its data to the attached
  58. The flow of data will be automatically managed so that the destination stream is not overwhelmed by a faster stream.
  59. The following example pipes all of the data from the into a file named
  60. All the data from readable goes into
  61. It is possible to attach multiple streams to a single stream.
  62. The method returns a reference to the destination stream making it possible to set up chains of piped streams:
  63. By default, is called on the destination stream when the source stream emits, so that the destination is no longer writable
  64. To disable this default behavior, the option can be passed as, causing the destination stream to remain open, as illustrated in the following example:
  65. One important caveat is that if the stream emits an error during processing, the destination is not closed automatically.
  66. If an error occurs, it will be necessary to manually close each stream in order to prevent memory leaks.
  67. The and streams are never closed until the Node.js process exits, regardless of the specified options.
  68. Optional argument to specify how much data to read.
  69. The method pulls some data out of the internal buffer and returns it.
  70. If no data available to be read, is returned.
  71. By default, the data will be returned as a object unless an encoding has been specified using the method or the stream is operating in object mode.
  72. The optional argument specifies a specific number of bytes to read.
  73. If bytes are not available to be read, will be returned unless the stream has ended, in which case all of the data remaining in the internal buffer will be returned.
  74. If the argument is not specified, all of the data contained in the internal buffer will be returned.
  75. The method should only be called on streams operating in paused mode.
  76. In flowing mode, is called automatically until the internal buffer is fully drained.
  77. A stream in object mode will always return a single item from a call to, regardless of the value of the argument.
  78. If the method returns a chunk of data, a event will also be emitted.
  79. Calling after the event has been emitted will return
  80. No runtime error will be raised.
  81. Returns the value of passed when constructing this
  82. This property contains the number of bytes (or objects) in the queue ready to be read.
  83. The method causes an explicitly paused stream to resume emitting events, switching the stream into flowing mode.
  84. The method can be used to fully consume the data from a stream without actually processing any of that data as illustrated in the following example:
  85. The method has no effect if there is a event listener.
  86. The encoding to use.
  87. The method sets the character encoding for data read from the stream.
  88. By default, no encoding is assigned and stream data will be returned as objects.
  89. Setting an encoding causes the stream data to be returned as strings of the specified encoding rather than as objects.
  90. For instance, calling will cause the output data to be interpreted as UTF-8 data, and passed as strings.
  91. Calling will cause the data to be encoded in hexadecimal string format.
  92. The stream will properly handle multi-byte characters delivered through the stream that would otherwise become improperly decoded if simply pulled from the stream as objects.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement