You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.8 KiB

  1. # delayed-stream
  2. Buffers events from a stream until you are ready to handle them.
  3. ## Installation
  4. ``` bash
  5. npm install delayed-stream
  6. ```
  7. ## Usage
  8. The following example shows how to write a http echo server that delays its
  9. response by 1000 ms.
  10. ``` javascript
  11. var DelayedStream = require('delayed-stream');
  12. var http = require('http');
  13. http.createServer(function(req, res) {
  14. var delayed = DelayedStream.create(req);
  15. setTimeout(function() {
  16. res.writeHead(200);
  17. delayed.pipe(res);
  18. }, 1000);
  19. });
  20. ```
  21. If you are not using `Stream#pipe`, you can also manually release the buffered
  22. events by calling `delayedStream.resume()`:
  23. ``` javascript
  24. var delayed = DelayedStream.create(req);
  25. setTimeout(function() {
  26. // Emit all buffered events and resume underlaying source
  27. delayed.resume();
  28. }, 1000);
  29. ```
  30. ## Implementation
  31. In order to use this meta stream properly, here are a few things you should
  32. know about the implementation.
  33. ### Event Buffering / Proxying
  34. All events of the `source` stream are hijacked by overwriting the `source.emit`
  35. method. Until node implements a catch-all event listener, this is the only way.
  36. However, delayed-stream still continues to emit all events it captures on the
  37. `source`, regardless of whether you have released the delayed stream yet or
  38. not.
  39. Upon creation, delayed-stream captures all `source` events and stores them in
  40. an internal event buffer. Once `delayedStream.release()` is called, all
  41. buffered events are emitted on the `delayedStream`, and the event buffer is
  42. cleared. After that, delayed-stream merely acts as a proxy for the underlaying
  43. source.
  44. ### Error handling
  45. Error events on `source` are buffered / proxied just like any other events.
  46. However, `delayedStream.create` attaches a no-op `'error'` listener to the
  47. `source`. This way you only have to handle errors on the `delayedStream`
  48. object, rather than in two places.
  49. ### Buffer limits
  50. delayed-stream provides a `maxDataSize` property that can be used to limit
  51. the amount of data being buffered. In order to protect you from bad `source`
  52. streams that don't react to `source.pause()`, this feature is enabled by
  53. default.
  54. ## API
  55. ### DelayedStream.create(source, [options])
  56. Returns a new `delayedStream`. Available options are:
  57. * `pauseStream`
  58. * `maxDataSize`
  59. The description for those properties can be found below.
  60. ### delayedStream.source
  61. The `source` stream managed by this object. This is useful if you are
  62. passing your `delayedStream` around, and you still want to access properties
  63. on the `source` object.
  64. ### delayedStream.pauseStream = true
  65. Whether to pause the underlaying `source` when calling
  66. `DelayedStream.create()`. Modifying this property afterwards has no effect.
  67. ### delayedStream.maxDataSize = 1024 * 1024
  68. The amount of data to buffer before emitting an `error`.
  69. If the underlaying source is emitting `Buffer` objects, the `maxDataSize`
  70. refers to bytes.
  71. If the underlaying source is emitting JavaScript strings, the size refers to
  72. characters.
  73. If you know what you are doing, you can set this property to `Infinity` to
  74. disable this feature. You can also modify this property during runtime.
  75. ### delayedStream.dataSize = 0
  76. The amount of data buffered so far.
  77. ### delayedStream.readable
  78. An ECMA5 getter that returns the value of `source.readable`.
  79. ### delayedStream.resume()
  80. If the `delayedStream` has not been released so far, `delayedStream.release()`
  81. is called.
  82. In either case, `source.resume()` is called.
  83. ### delayedStream.pause()
  84. Calls `source.pause()`.
  85. ### delayedStream.pipe(dest)
  86. Calls `delayedStream.resume()` and then proxies the arguments to `source.pipe`.
  87. ### delayedStream.release()
  88. Emits and clears all events that have been buffered up so far. This does not
  89. resume the underlaying source, use `delayedStream.resume()` instead.
  90. ## License
  91. delayed-stream is licensed under the MIT license.