Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TM] Add spec for WebSocketModule #24893

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Libraries/WebSocket/NativeWebSocketModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
*/

'use strict';

import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';

export interface Spec extends TurboModule {
+connect: (
url: string,
protocols: ?Array<string>,
options: ?{headers?: {origin?: string}},
socketID: number,
) => void;
+send: (message: string, socketID: number) => void;
+sendBinary: (base64String: string, socketID: number) => void;
+ping: (socketID: number) => void;
+close: (code: number, reason: string, socketID: number) => void;

// Events
+addListener: (eventName: string) => void;
+removeListeners: (count: number) => void;
}

export default TurboModuleRegistry.getEnforcing<Spec>('WebSocketModule');
15 changes: 7 additions & 8 deletions Libraries/WebSocket/WebSocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ const Blob = require('../Blob/Blob');
const EventTarget = require('event-target-shim');
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');
const BlobManager = require('../Blob/BlobManager');
const NativeModules = require('../BatchedBridge/NativeModules');
const Platform = require('../Utilities/Platform');
const WebSocketEvent = require('./WebSocketEvent');

const base64 = require('base64-js');
const binaryToBase64 = require('../Utilities/binaryToBase64');
const invariant = require('invariant');

const {WebSocketModule} = NativeModules;
import NativeWebSocketModule from './NativeWebSocketModule';

import type EventSubscription from '../vendor/emitter/EventSubscription';

Expand Down Expand Up @@ -128,10 +127,10 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
protocols = null;
}

this._eventEmitter = new NativeEventEmitter(WebSocketModule);
this._eventEmitter = new NativeEventEmitter(NativeWebSocketModule);
this._socketId = nextWebSocketId++;
this._registerEvents();
WebSocketModule.connect(url, protocols, {headers}, this._socketId);
NativeWebSocketModule.connect(url, protocols, {headers}, this._socketId);
}

get binaryType(): ?BinaryType {
Expand Down Expand Up @@ -180,12 +179,12 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
}

if (typeof data === 'string') {
WebSocketModule.send(data, this._socketId);
NativeWebSocketModule.send(data, this._socketId);
return;
}

if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
WebSocketModule.sendBinary(binaryToBase64(data), this._socketId);
NativeWebSocketModule.sendBinary(binaryToBase64(data), this._socketId);
return;
}

Expand All @@ -197,14 +196,14 @@ class WebSocket extends EventTarget(...WEBSOCKET_EVENTS) {
throw new Error('INVALID_STATE_ERR');
}

WebSocketModule.ping(this._socketId);
NativeWebSocketModule.ping(this._socketId);
}

_close(code?: number, reason?: string): void {
// See https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
const statusCode = typeof code === 'number' ? code : CLOSE_NORMAL;
const closeReason = typeof reason === 'string' ? reason : '';
WebSocketModule.close(statusCode, closeReason, this._socketId);
NativeWebSocketModule.close(statusCode, closeReason, this._socketId);

if (BlobManager.isAvailable && this._binaryType === 'blob') {
BlobManager.removeWebSocketHandler(this._socketId);
Expand Down
35 changes: 20 additions & 15 deletions Libraries/WebSocket/WebSocketInterceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

'use strict';

const RCTWebSocketModule = require('../BatchedBridge/NativeModules')
.WebSocketModule;
const NativeEventEmitter = require('../EventEmitter/NativeEventEmitter');

import NativeWebSocketModule from './NativeWebSocketModule';

const base64 = require('base64-js');

const originalRCTWebSocketConnect = RCTWebSocketModule.connect;
const originalRCTWebSocketSend = RCTWebSocketModule.send;
const originalRCTWebSocketSendBinary = RCTWebSocketModule.sendBinary;
const originalRCTWebSocketClose = RCTWebSocketModule.close;
const originalRCTWebSocketConnect = NativeWebSocketModule.connect;
const originalRCTWebSocketSend = NativeWebSocketModule.send;
const originalRCTWebSocketSendBinary = NativeWebSocketModule.sendBinary;
const originalRCTWebSocketClose = NativeWebSocketModule.close;

let eventEmitter: NativeEventEmitter;
let subscriptions: Array<EventSubscription>;
Expand Down Expand Up @@ -135,13 +135,18 @@ const WebSocketInterceptor = {
if (isInterceptorEnabled) {
return;
}
eventEmitter = new NativeEventEmitter(RCTWebSocketModule);
eventEmitter = new NativeEventEmitter(NativeWebSocketModule);
WebSocketInterceptor._registerEvents();

// Override `connect` method for all RCTWebSocketModule requests
// to intercept the request url, protocols, options and socketId,
// then pass them through the `connectCallback`.
RCTWebSocketModule.connect = function(url, protocols, options, socketId) {
NativeWebSocketModule.connect = function(
url,
protocols,
options,
socketId,
) {
if (connectCallback) {
connectCallback(url, protocols, options, socketId);
}
Expand All @@ -150,7 +155,7 @@ const WebSocketInterceptor = {

// Override `send` method for all RCTWebSocketModule requests to intercept
// the data sent, then pass them through the `sendCallback`.
RCTWebSocketModule.send = function(data, socketId) {
NativeWebSocketModule.send = function(data, socketId) {
if (sendCallback) {
sendCallback(data, socketId);
}
Expand All @@ -159,7 +164,7 @@ const WebSocketInterceptor = {

// Override `sendBinary` method for all RCTWebSocketModule requests to
// intercept the data sent, then pass them through the `sendCallback`.
RCTWebSocketModule.sendBinary = function(data, socketId) {
NativeWebSocketModule.sendBinary = function(data, socketId) {
if (sendCallback) {
sendCallback(WebSocketInterceptor._arrayBufferToString(data), socketId);
}
Expand All @@ -168,7 +173,7 @@ const WebSocketInterceptor = {

// Override `close` method for all RCTWebSocketModule requests to intercept
// the close information, then pass them through the `closeCallback`.
RCTWebSocketModule.close = function() {
NativeWebSocketModule.close = function() {
if (closeCallback) {
if (arguments.length === 3) {
closeCallback(arguments[0], arguments[1], arguments[2]);
Expand Down Expand Up @@ -203,10 +208,10 @@ const WebSocketInterceptor = {
return;
}
isInterceptorEnabled = false;
RCTWebSocketModule.send = originalRCTWebSocketSend;
RCTWebSocketModule.sendBinary = originalRCTWebSocketSendBinary;
RCTWebSocketModule.close = originalRCTWebSocketClose;
RCTWebSocketModule.connect = originalRCTWebSocketConnect;
NativeWebSocketModule.send = originalRCTWebSocketSend;
NativeWebSocketModule.sendBinary = originalRCTWebSocketSendBinary;
NativeWebSocketModule.close = originalRCTWebSocketClose;
NativeWebSocketModule.connect = originalRCTWebSocketConnect;

connectCallback = null;
closeCallback = null;
Expand Down