Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changed formatting a little to network, also made it so the host argu…

…ment to isPortOpen is optional, defaulting to localhost is if isn't given but a callback is.
  • Loading branch information...
commit 26ba831d9be8be69a367e0726cda9b76a3657b8c 1 parent 78c928b
@larzconwell larzconwell authored
Showing with 37 additions and 12 deletions.
  1. +37 −12 lib/network.js
View
49 lib/network.js
@@ -1,3 +1,21 @@
+/*
+ * Utilities: A classic collection of JavaScript utilities
+ * Copyright 2112 Matthew Eernisse (mde@fleegix.org)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+*/
+
var network
, net = require('net');
@@ -5,40 +23,47 @@ var network
@name network
@namespace network
*/
-network = new (function () {
- /**
+
+network = new (function () {
+ /**
@name network#isPortOpen
@public
@function
@description Checks if the given port in the given host is open
@param {Number} port number
- @param {String} host
- @param {Function} callback Callback function -- should be in the format of function(err, result) {}
+ @param {String} host
+ @param {Function} callback Callback function -- should be in the format
+ of function(err, result) {}
*/
this.isPortOpen = function (port, host, callback) {
+ if (typeof host === 'function' && !callback) {
+ callback = host;
+ host = 'localhost';
+ }
+
var isOpen = false
, connection
, error;
- connection = net.createConnection(port, host, function () {
+ connection = net.createConnection(port, host, function () {
isOpen = true;
connection.end();
});
- connection.on('error', function (err) {
- // We ignore 'ECONNREFUSED' as it simply indicates the port isn't open. Anything else is reported
+ connection.on('error', function (err) {
+ // We ignore 'ECONNREFUSED' as it simply indicates the port isn't open.
+ // Anything else is reported
if(err.code !== 'ECONNREFUSED') {
error = err;
}
- // the socket emits 'close' after 'error'. No need to do anything here
- })
+ });
- connection.setTimeout(400, function () {
+ connection.setTimeout(400, function () {
connection.end();
});
- connection.on('close', function (had_error) {
- callback(error, isOpen);
+ connection.on('close', function () {
+ callback && callback(error, isOpen);
});
};
Please sign in to comment.
Something went wrong with that request. Please try again.