react-native/Libraries/Network
Philipp von Weitershausen 08c375f828 Add responseType as a concept to RCTNetworking, send binary data as base64
Summary:
In preparation for Blob support (wherein binary XHR and WebSocket responses can be retained as native data blobs on the native side and JS receives a web-like opaque Blob object), this change makes RCTNetworking aware of the responseType that JS requests. A `xhr.responseType` of `''` or `'text'` translates to a native response type of `'text'`. A `xhr.responseType` of `arraybuffer` translates to a native response type of `base64`, as we currently lack an API to transmit TypedArrays directly to JS. This is analogous to how the WebSocket module already works, and it's a lot more versatile and much less brittle than converting a JS *string* back to a TypedArray, which is what's currently going on.

Now that we don't always send text down to JS, JS consumers might still want to get progress updates about a binary download. This is what the `'progress'` event is designed for, so this change also implements that. This change also follows the XHR spec with regards to `xhr.response` and `xhr.responseText`:

- if the response type is `'text'`, `xhr.responseText` can be peeked at by the JS consumer. It will be updated periodically as the download progresses, so long as there's either an `onreadystatechange` or `onprogress` handler on the XHR.

- if the response type is not `'text'`, `xhr.responseText` can't be accessed and `xhr.response` remains `null` until the response is fully received. `'progress'` events containing response details (total bytes, downloaded so far) are dispatched if there's an `onprogress` handler.

Once Blobs are landed, `xhr.responseType` of `'blob'` will correspond to the same native response type, which will cause RCTNetworking to only send a blob ID down to JS, which can then create a `Blob` object from that for consumers.

Closes https://github.com/facebook/react-native/pull/8324

Reviewed By: javache

Differential Revision: D3508822

Pulled By: davidaurelio

fbshipit-source-id: 441b2d4d40265b6036559c3ccb9fa962999fa5df
2016-07-13 04:58:37 -07:00
..
RCTNetwork.xcodeproj Added RCTDataRequestHandler 2015-10-19 09:07:06 -07:00
__tests__ Add responseType as a concept to RCTNetworking, send binary data as base64 2016-07-13 04:58:37 -07:00
FormData.js v0.27.0: fbsource 2016-06-13 14:28:25 -07:00
NetInfo.js Update RCTNetworking, RCTNetInfo and RCTLocationManager to use new events system 2016-05-25 04:28:36 -07:00
RCTDataRequestHandler.h Added RCTDataRequestHandler 2015-10-19 09:07:06 -07:00
RCTDataRequestHandler.m Fix retain cyles in RCTNetworkTask when used with RCTFileRequestHandler and RCTDataRequestHandler 2015-11-04 07:16:26 -08:00
RCTFileRequestHandler.h Added RCTFileRequestHandler 2015-10-13 08:14:28 -07:00
RCTFileRequestHandler.m Fix missing images 2016-06-09 09:58:31 -07:00
RCTHTTPRequestHandler.h Refactored networking logic out into RCTDownloadTask 2015-07-23 04:00:31 -08:00
RCTHTTPRequestHandler.m Refactored module access to allow for lazy loading 2015-11-25 04:49:45 -08:00
RCTNetInfo.h Update RCTNetworking, RCTNetInfo and RCTLocationManager to use new events system 2016-05-25 04:28:36 -07:00
RCTNetInfo.m Update RCTNetworking, RCTNetInfo and RCTLocationManager to use new events system 2016-05-25 04:28:36 -07:00
RCTNetworkTask.h Add responseType as a concept to RCTNetworking, send binary data as base64 2016-07-13 04:58:37 -07:00
RCTNetworkTask.m Add responseType as a concept to RCTNetworking, send binary data as base64 2016-07-13 04:58:37 -07:00
RCTNetworking.android.js Add responseType as a concept to RCTNetworking, send binary data as base64 2016-07-13 04:58:37 -07:00
RCTNetworking.h Update RCTNetworking, RCTNetInfo and RCTLocationManager to use new events system 2016-05-25 04:28:36 -07:00
RCTNetworking.ios.js Add responseType as a concept to RCTNetworking, send binary data as base64 2016-07-13 04:58:37 -07:00
RCTNetworking.m Add responseType as a concept to RCTNetworking, send binary data as base64 2016-07-13 04:58:37 -07:00
XMLHttpRequest.js Add responseType as a concept to RCTNetworking, send binary data as base64 2016-07-13 04:58:37 -07:00