XHR: support typed arrays for request payloads

Summary:
Support `xhr.send(data)` for typed arrays.

**Test plan:** run UIExplorer example on iOS and Android.
Closes https://github.com/facebook/react-native/pull/11904

Differential Revision: D4425551

fbshipit-source-id: 065ab5873407a406ca4a831068ab138606c3361b
This commit is contained in:
Philipp von Weitershausen
2017-01-20 18:40:28 -08:00
committed by Facebook Github Bot
parent 116916b98d
commit 16bb6e87ba
11 changed files with 293 additions and 63 deletions

View File

@@ -15,24 +15,13 @@ const NativeEventEmitter = require('NativeEventEmitter');
const Platform = require('Platform');
const RCTWebSocketModule = require('NativeModules').WebSocketModule;
const WebSocketEvent = require('WebSocketEvent');
const binaryToBase64 = require('binaryToBase64');
const EventTarget = require('event-target-shim');
const base64 = require('base64-js');
import type EventSubscription from 'EventSubscription';
type ArrayBufferView =
Int8Array |
Uint8Array |
Uint8ClampedArray |
Int16Array |
Uint16Array |
Int32Array |
Uint32Array |
Float32Array |
Float64Array |
DataView;
const CONNECTING = 0;
const OPEN = 1;
const CLOSING = 2;
@@ -108,7 +97,7 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
this._close(code, reason);
}
send(data: string | ArrayBuffer | ArrayBufferView): void {
send(data: string | ArrayBuffer | $ArrayBufferView): void {
if (this.readyState === this.CONNECTING) {
throw new Error('INVALID_STATE_ERR');
}
@@ -118,14 +107,8 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
return;
}
if (ArrayBuffer.isView(data)) {
// $FlowFixMe: no way to assert that 'data' is indeed an ArrayBufferView now
data = data.buffer;
}
if (data instanceof ArrayBuffer) {
data = base64.fromByteArray(new Uint8Array(data));
RCTWebSocketModule.sendBinary(data, this._socketId);
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
RCTWebSocketModule.sendBinary(binaryToBase64(data), this._socketId);
return;
}