react-native-tcp/TcpSockets.js

46 lines
1.1 KiB
JavaScript
Raw Normal View History

/**
2015-12-23 18:56:33 +00:00
* Copyright (c) 2015-present, Peel Technologies, Inc.
* All rights reserved.
*
* @providesModule TcpSockets
* @flow
*/
2015-12-23 18:56:33 +00:00
'use strict';
2015-12-15 00:16:45 +00:00
var ipRegex = require('ip-regex');
2015-12-23 18:56:33 +00:00
var Socket = require('./TcpSocket');
var Server = require('./TcpServer');
2015-12-22 23:53:08 +00:00
2015-12-23 18:56:33 +00:00
exports.createServer = function(connectionListener: (socket: Socket) => void) : Server {
return new Server(connectionListener);
2015-12-22 23:53:08 +00:00
};
2015-12-24 19:18:51 +00:00
exports.connect = exports.createConnection = function(options: ?{ port: ?number | ?string, host: ?string, localAddress: ?string, localPort: ?number}, callback: ?() => void) : Socket {
2015-12-22 23:53:08 +00:00
var tcpSocket = new Socket();
tcpSocket.connect(options, callback);
return tcpSocket;
};
2015-12-15 00:16:45 +00:00
2015-12-16 23:05:08 +00:00
exports.isIP = function(input: string) : number {
2015-12-15 00:16:45 +00:00
var result = 0;
if (ipRegex.v4({exact: true}).test(input)) {
result = 4;
} else if (ipRegex.v6({exact: true}).test(input)) {
result = 6;
}
2015-12-16 23:05:08 +00:00
return result;
2015-12-15 00:16:45 +00:00
};
2015-12-16 23:05:08 +00:00
exports.isIPv4 = function(input: string) : boolean {
2015-12-15 00:16:45 +00:00
return exports.isIP(input) === 4;
};
2015-12-16 23:05:08 +00:00
exports.isIPv6 = function(input: string) : boolean {
2015-12-15 00:16:45 +00:00
return exports.isIP(input) === 6;
};
2015-12-22 23:53:08 +00:00
exports.Socket = Socket;
exports.Server = Server;