Path: blob/trunk/third_party/closure/goog/net/streams/streamfactory.js
1865 views
// Copyright 2015 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview the factory for creating stream objects.16*17*/1819goog.provide('goog.net.streams.createXhrNodeReadableStream');2021goog.require('goog.asserts');22goog.require('goog.net.streams.XhrNodeReadableStream');23goog.require('goog.net.streams.XhrStreamReader');242526/**27* Creates a new NodeReadableStream object using goog.net.xhrio as the28* underlying HTTP request.29*30* The XhrIo object should not have been sent to the network via its send()31* method. NodeReadableStream callbacks are expected to be registered before32* XhrIo.send() is invoked. The behavior of the stream is undefined if33* otherwise. After send() is called, the lifecycle events are expected to34* be handled directly via the stream API.35*36* If a binary response (e.g. protobuf) is expected, the caller should configure37* the xhrIo by setResponseType(goog.net.XhrIo.ResponseType.ARRAY_BUFFER)38* before xhrIo.send() is invoked.39*40* States specific to the xhr may be accessed before or after send() is called41* as long as those operations are safe, e.g. configuring headers and options.42*43* Timeout (deadlines), cancellation (abort) should be applied to44* XhrIo directly and the stream object will respect any life cycle events45* trigger by those actions.46*47* Note for the release pkg:48* "--define goog.net.XmlHttpDefines.ASSUME_NATIVE_XHR=true"49* disable asserts50*51* @param {!goog.net.XhrIo} xhr The XhrIo object with its response body to52* be handled by NodeReadableStream.53* @return {goog.net.streams.NodeReadableStream} the newly created stream or54* null if streaming response is not supported by the current User Agent.55*/56goog.net.streams.createXhrNodeReadableStream = function(xhr) {57goog.asserts.assert(!xhr.isActive(), 'XHR is already sent.');5859if (!goog.net.streams.XhrStreamReader.isStreamingSupported()) {60return null;61}6263var reader = new goog.net.streams.XhrStreamReader(xhr);64return new goog.net.streams.XhrNodeReadableStream(reader);65};666768