BlueWallet/blue_modules/tls.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-06-11 19:34:28 +02:00
/**
* @fileOverview adapter for ReactNative TCP module
* This module mimics the nodejs tls api and is intended to work in RN environment.
* @see https://github.com/Rapsssito/react-native-tcp-socket
*/
import TcpSocket from 'react-native-tcp-socket';
/**
* Constructor function. Mimicking nodejs/tls api
*
* @constructor
*/
function connect(config, callback) {
const client = TcpSocket.createConnection(
{
port: config.port,
host: config.host,
tls: true,
tlsCheckValidity: config.rejectUnauthorized,
},
callback,
);
2020-06-15 12:05:11 +02:00
// defaults:
this._noDelay = true;
2020-06-11 19:34:28 +02:00
// functions not supported by RN module, yet:
2020-06-15 12:05:11 +02:00
client.setTimeout = () => {};
2020-06-11 19:34:28 +02:00
client.setEncoding = () => {};
client.setKeepAlive = () => {};
2020-06-15 12:05:11 +02:00
// we will save `noDelay` and proxy it to socket object when its actually created and connected:
const realSetNoDelay = client.setNoDelay; // reference to real setter
client.setNoDelay = noDelay => {
this._noDelay = noDelay;
};
client.on('connect', () => {
realSetNoDelay.apply(client, [this._noDelay]);
});
2020-06-11 19:34:28 +02:00
return client;
}
module.exports.connect = connect;