react-native-tcp/TcpSockets.js
2015-12-23 10:57:51 -08:00

46 lines
1.1 KiB
JavaScript

/**
* Copyright (c) 2015-present, Peel Technologies, Inc.
* All rights reserved.
*
* @providesModule TcpSockets
* @flow
*/
'use strict';
var ipRegex = require('ip-regex');
var Socket = require('./TcpSocket');
var Server = require('./TcpServer');
exports.createServer = function(connectionListener: (socket: Socket) => void) : Server {
return new Server(connectionListener);
};
exports.connect = exports.createConnection = function(options: { port: number, host: ?string, localAddress: ?string, localPort: ?number, family: ?number }, callback: ?() => void) : Socket {
var tcpSocket = new Socket();
tcpSocket.connect(options, callback);
return tcpSocket;
};
exports.isIP = function(input: string) : number {
var result = 0;
if (ipRegex.v4({exact: true}).test(input)) {
result = 4;
} else if (ipRegex.v6({exact: true}).test(input)) {
result = 6;
}
return result;
};
exports.isIPv4 = function(input: string) : boolean {
return exports.isIP(input) === 4;
};
exports.isIPv6 = function(input: string) : boolean {
return exports.isIP(input) === 6;
};
exports.Socket = Socket;
exports.Server = Server;