streamfactory.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 factory for creating stream objects.
  16. *
  17. */
  18. goog.provide('goog.net.streams.createXhrNodeReadableStream');
  19. goog.require('goog.asserts');
  20. goog.require('goog.net.streams.XhrNodeReadableStream');
  21. goog.require('goog.net.streams.XhrStreamReader');
  22. /**
  23. * Creates a new NodeReadableStream object using goog.net.xhrio as the
  24. * underlying HTTP request.
  25. *
  26. * The XhrIo object should not have been sent to the network via its send()
  27. * method. NodeReadableStream callbacks are expected to be registered before
  28. * XhrIo.send() is invoked. The behavior of the stream is undefined if
  29. * otherwise. After send() is called, the lifecycle events are expected to
  30. * be handled directly via the stream API.
  31. *
  32. * If a binary response (e.g. protobuf) is expected, the caller should configure
  33. * the xhrIo by setResponseType(goog.net.XhrIo.ResponseType.ARRAY_BUFFER)
  34. * before xhrIo.send() is invoked.
  35. *
  36. * States specific to the xhr may be accessed before or after send() is called
  37. * as long as those operations are safe, e.g. configuring headers and options.
  38. *
  39. * Timeout (deadlines), cancellation (abort) should be applied to
  40. * XhrIo directly and the stream object will respect any life cycle events
  41. * trigger by those actions.
  42. *
  43. * Note for the release pkg:
  44. * "--define goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR=true"
  45. * disable asserts
  46. *
  47. * @param {!goog.net.XhrIo} xhr The XhrIo object with its response body to
  48. * be handled by NodeReadableStream.
  49. * @return {goog.net.streams.NodeReadableStream} the newly created stream or
  50. * null if streaming response is not supported by the current User Agent.
  51. */
  52. goog.net.streams.createXhrNodeReadableStream = function(xhr) {
  53. goog.asserts.assert(!xhr.isActive(), 'XHR is already sent.');
  54. if (!goog.net.streams.XhrStreamReader.isStreamingSupported()) {
  55. return null;
  56. }
  57. var reader = new goog.net.streams.XhrStreamReader(xhr);
  58. return new goog.net.streams.XhrNodeReadableStream(reader);
  59. };