feat(http): add support for ArrayBuffer

Add the buffer option to store response in ArrayBuffer
Improve the interface to get back response independently of the buffer type
This commit is contained in:
Damien Cassan
2016-02-24 16:37:18 +01:00
committed by Jeff Cross
parent 0ccb6e0dfc
commit 1266460386
12 changed files with 218 additions and 29 deletions

View File

@ -12,7 +12,7 @@ import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';
import {ResponseOptions} from '../base_response_options';
import {ContentType, ReadyState, RequestMethod, ResponseType} from '../enums';
import {ContentType, ReadyState, RequestMethod, ResponseType, ResponseBuffer} from '../enums';
import {isPresent, isString} from '../facade/lang';
import {Headers} from '../headers';
import {getResponseURL, isSuccess} from '../http_utils';
@ -24,6 +24,7 @@ import {BrowserXhr} from './browser_xhr';
const XSSI_PREFIX = /^\)\]\}',?\n/;
/**
* Creates connections using `XMLHttpRequest`. Given a fully-qualified
* request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
@ -51,7 +52,7 @@ export class XHRConnection implements Connection {
_xhr.withCredentials = req.withCredentials;
}
// load event handler
let onLoad = () => {
let onLoad = () => {
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
// response/responseType properties were introduced in XHR Level2 spec (supported by
// IE10)
@ -108,6 +109,20 @@ export class XHRConnection implements Connection {
req.headers.forEach((values, name) => _xhr.setRequestHeader(name, values.join(',')));
}
// Select the correct buffer type to store the response
if (isPresent(req.buffer) && isPresent(_xhr.responseType)) switch (req.buffer) {
case ResponseBuffer.ArrayBuffer:
_xhr.responseType = "arraybuffer";
break;
case ResponseBuffer.Json:
_xhr.responseType = "json";
break;
default:
case ResponseBuffer.Text:
_xhr.responseType = "text";
break;
}
_xhr.addEventListener('load', onLoad);
_xhr.addEventListener('error', onError);