nodereadablestream.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2015 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview the API spec for the closure polyfill of Node stream.Readable.
  16. *
  17. * Node streams API is specified at https://nodejs.org/api/stream.html
  18. *
  19. * Only a subset of Node streams API (under the object mode) will be supported.
  20. *
  21. * It's our belief that Node and whatwg streams will eventually
  22. * converge. As it happens, we will add a whatwg streams polyfill too.
  23. * (https://github.com/whatwg/streams)
  24. *
  25. * This API requires no special server-side support other than the standard
  26. * HTTP semantics. Message framing only relies on MIME types such as JSON
  27. * to support atomic message delivery (e.g. elements of a JSON array).
  28. * Other streaming-related features such as cancellation and keep-alive are
  29. * exposed/constrained by the Node streams API semantics.
  30. *
  31. * Flow-control support is limited due to the underlying use of XHR. That is,
  32. * this version will assume the "flowing mode", and the read method is not
  33. * provided.
  34. *
  35. */
  36. goog.provide('goog.net.streams.NodeReadableStream');
  37. /**
  38. * This interface represents a readable stream.
  39. *
  40. * @interface
  41. */
  42. goog.net.streams.NodeReadableStream = function() {};
  43. /**
  44. * Read events for the stream.
  45. * @enum {string}
  46. */
  47. goog.net.streams.NodeReadableStream.EventType = {
  48. READABLE: 'readable',
  49. DATA: 'data',
  50. END: 'end',
  51. CLOSE: 'close',
  52. ERROR: 'error'
  53. };
  54. /**
  55. * Register a callback to handle I/O events.
  56. *
  57. * See https://iojs.org/api/events.html
  58. *
  59. * Note that under the object mode, an event of DATA will deliver a message
  60. * of 1) JSON compliant JS object, including arrays; or 2) an ArrayBuffer.
  61. *
  62. * Ordering: messages will be delivered to callbacks in their registration
  63. * order. There is no ordering between on() and once() callbacks.
  64. *
  65. * Exceptions from callbacks will be caught and ignored.
  66. *
  67. * @param {string} eventType The event type
  68. * @param {function(!Object=)} callback The call back to handle the event with
  69. * an optional input object
  70. * @return {goog.net.streams.NodeReadableStream} this object
  71. */
  72. goog.net.streams.NodeReadableStream.prototype.on = goog.abstractMethod;
  73. /**
  74. * Register a callback to handle I/O events. This is an alias to on().
  75. *
  76. * @param {string} eventType The event type
  77. * @param {function(!Object=)} callback The call back to handle the event with
  78. * an optional input object
  79. * @return {goog.net.streams.NodeReadableStream} this object
  80. */
  81. goog.net.streams.NodeReadableStream.prototype.addListener = goog.abstractMethod;
  82. /**
  83. * Unregister an existing callback, including one-time callbacks.
  84. *
  85. * @param {string} eventType The event type
  86. * @param {function(!Object=)} callback The call back to unregister
  87. * @return {goog.net.streams.NodeReadableStream} this object
  88. */
  89. goog.net.streams.NodeReadableStream.prototype.removeListener =
  90. goog.abstractMethod;
  91. /**
  92. * Register a one-time callback to handle I/O events.
  93. *
  94. * @param {string} eventType The event type
  95. * @param {function(!Object=)} callback The call back to handle the event with
  96. * an optional input object
  97. * @return {goog.net.streams.NodeReadableStream} this object
  98. */
  99. goog.net.streams.NodeReadableStream.prototype.once = goog.abstractMethod;