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