Guest User

Untitled

a guest
Oct 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. # a `Stream` spec
  2.  
  3. This document defines the behaviour that a `Stream` must implement in order to be compatible with `Stream#pipe`. This is not an official document, but is intended as a guide to produce correct behaviour in user-land streams.
  4.  
  5. ## Stream
  6.  
  7. All streams *must* emit `'error'` if writing to or reading from becomes physically impossible. `'error'` implys that the stream has ended.
  8.  
  9. All streams *may* emit `'close'`. `'close'` means that any underlying resources have been disposed of. If a `ReadableStream` has ended normally, it *must not* emit `'close'` before `'end'`.
  10.  
  11. A `Stream` *must not* emit `'error'` if the error is recoverable.
  12. (that is not in the stream spec)
  13.  
  14. All streams *should* implement `destroy` but a `WritableStream` *must* implement `destroy`.
  15.  
  16. ### emit('error')
  17.  
  18. All streams *must* emit `'error'` when an error that is not recoverable has occurred. If it has become physically impossible to write to or read from the `Stream`, then emit `'error'`.
  19.  
  20. A `WriteableStream` *may* throw an error if `write` has been called after `end`.
  21. (which should never happen, in correct usage)
  22.  
  23. otherwise, a stream *must never* throw an error. (always emit)
  24.  
  25. ## WritableStream
  26.  
  27. A `WritableStream` *must* implement methods `write`, `end`, and `destroy`, and `writable` *must* be set to `true`, and *must* inherit `Stream#pipe`
  28.  
  29. ### write(data)
  30.  
  31. `write` must return either `true` or `false`.
  32. (if `false` then the writer *should* pause)
  33. If `write` is called after end, an error *may* be thrown.
  34.  
  35. ### end()
  36.  
  37. calling `end` *may* set `writable` to `false`.
  38. If the `Stream` is also readable, it *must* eventually emit 'end'.
  39.  
  40. ### destroy()
  41.  
  42. Used to dispose of a `Stream`.
  43.  
  44. Calling `destroy` *must* dispose of any underlying resources.
  45. Calling `destroy` *must* emit `'close'` eventually,
  46. once any underlying resources are disposed of.
  47.  
  48. ## ReadableStream
  49.  
  50. A `ReadableStream` *must* inherit `pipe` from `Stream`, and set `readable` to `true`, and *must* emit zero or more 'data' events, followed by a single `end` event. A `ReadableStream` *may* implement `pause` and `resume` methods.
  51.  
  52. * I will not bother to specify the behaviour of `pipe` because I am attempting to document what must be done in order for your `Stream` to be compatible with `pipe`.
  53.  
  54. ### emit('data', data)
  55.  
  56. A `ReadableStream` *may* emit one or more `'data'` events.
  57. A `ReadableStream` *must not* emit emit a `'data'` event after it has emitted `'end'`
  58.  
  59. ### emit('end')
  60.  
  61. A `ReadableStream` *should* emit an `'end'` event when it is not going to emit any more `'data'` events. `'end'` *must not* be emitted more than once. A `ReadableStream` may set `readable` to `false` after it has emitted the `'end'` event.
  62.  
  63. ### pause()
  64.  
  65. A readable `Stream` *may* implement the `pause` method. When `pause` is called, the stream should attempt to emit `'data'` less often. (possibly stopping altogether until `resume` is called)
  66.  
  67. ### resume()
  68.  
  69. A `ReadableStream` *may* implement the `resume` method. If the `Stream` has been paused, it may now emit `'data'` more often, or commence emitting `data` if it has stopped all together.
  70.  
  71. ### destroy()
  72.  
  73. A `ReadableStream` *should* implement `destroy`.
  74.  
  75. Calling `destroy` *must* dispose of any underlying resources.
  76. Calling `destroy` *must* emit `'close'` eventually,
  77. once any underlying resources are disposed of.
  78.  
  79. ## request for comment
  80.  
  81. thank you in advance!
Add Comment
Please, Sign In to add comment