2015-12-04 17:44:16 +00:00
|
|
|
/**
|
2015-12-23 18:56:33 +00:00
|
|
|
* Copyright (c) 2015-present, Peel Technologies, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2015-12-04 17:44:16 +00:00
|
|
|
* @providesModule TcpSockets
|
|
|
|
* @flow
|
|
|
|
*/
|
2015-12-23 18:56:33 +00:00
|
|
|
|
2015-12-04 17:44:16 +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-28 19:12:21 +00:00
|
|
|
// TODO : determine how to properly overload this with flow
|
|
|
|
exports.connect = exports.createConnection = function() : Socket {
|
2015-12-22 23:53:08 +00:00
|
|
|
var tcpSocket = new Socket();
|
2015-12-28 19:12:21 +00:00
|
|
|
return Socket.prototype.connect.apply(tcpSocket, tcpSocket._normalizeConnectArgs(arguments));
|
2015-12-04 17:44:16 +00:00
|
|
|
};
|
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;
|