Skip to content

Commit

Permalink
Redo logic of one()
Browse files Browse the repository at this point in the history
  • Loading branch information
scravy committed May 18, 2020
1 parent ba7cf2c commit 86fc63f
Showing 1 changed file with 44 additions and 82 deletions.
126 changes: 44 additions & 82 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,12 @@
/* jshint node: true */
'use strict';
"use strict";

var util = require("./lib/util.js");
var lib = {};
var nextTick = process.nextTick || global.setImmediate || global.setTimeout;

function parallel(tasks, done) {
var results = [];
var errs = [];
var length = 0;
var doneLength = 0;
function doneIt(ix, err, result) {
if (err) {
errs[ix] = err;
} else {
results[ix] = result;
}
doneLength += 1;
if (doneLength >= length) {
done(errs.length > 0 ? errs : errs, results);
}
}
Object.keys(tasks).forEach(function (key) {
length += 1;
var task = tasks[key];
nextTick(function () {
task(doneIt.bind(null, key), 1);
});
});
}

lib.getMacAddress = require('./lib/getmacaddress.js');
lib.getAllInterfaces = require('./lib/getallinterfaces.js');

lib.networkInterfaces = require('./lib/networkinterfaces.js');

function promisify(func) {
return new Promise(function (resolve, reject) {
func(function (err, data) {
if (err) {
if (!err instanceof Error) {
err = new Error(err);
}
reject(err);
return;
}
resolve(data);
});
});
}
lib.getMacAddress = require("./lib/getmacaddress.js");
lib.getAllInterfaces = require("./lib/getallinterfaces.js");
lib.networkInterfaces = require("./lib/networkinterfaces.js");

lib.one = function () {
// one() can be invoked in several ways:
Expand All @@ -58,58 +17,61 @@ lib.one = function () {
var iface = null;
var callback = null;
if (arguments.length >= 1) {
if (typeof arguments[0] === 'function') {
if (typeof arguments[0] === "function") {
callback = arguments[0];
} else if (typeof arguments[0] === 'string') {
} else if (typeof arguments[0] === "string") {
iface = arguments[0];
}
if (arguments.length >= 2) {
if (typeof arguments[1] === 'function') {
if (typeof arguments[1] === "function") {
callback = arguments[1];
}
}
}
if (!callback) {
return promisify(function (callback) {
return util.promisify(function (callback) {
lib.one(iface, callback);
});
}

if (iface) {
lib.getMacAddress(iface, callback);
} else {
var ifaces = lib.networkInterfaces();
var alleged = [ 'eth0', 'eth1', 'en0', 'en1', 'en2', 'en3', 'en4' ];
iface = Object.keys(ifaces)[0];
for (var i = 0; i < alleged.length; i++) {
if (ifaces[alleged[i]]) {
iface = alleged[i];
break;
}
}
if (!ifaces[iface]) {
if (typeof callback === 'function') {
nextTick(function() {
callback(new Error("no interfaces found"), null);
});
}
return null;
}
if (ifaces[iface].mac) {
if (typeof callback === 'function') {
nextTick(function() {
callback(null, ifaces[iface].mac);
});
return;
}
var ifaces = lib.networkInterfaces();
var addresses = {};
var best = [];
var args = [];
Object.keys(ifaces).forEach(function (d) {
args.push(d);
if (typeof ifaces[d].mac === "string" && ifaces[d].mac !== "00:00:00:00:00:00") {
addresses[d] = ifaces[d].mac;
if (ifaces[d].ipv4 || ifaces[d].ipv6) {
if (ifaces[d].ipv4 && ifaces[d].ipv6) {
best.unshift(addresses[d]);
} else {
best.push(addresses[d]);
}
}
return ifaces[iface].mac;
}
});
if (best.length > 0) {
util.nextTick(callback.bind(null, best[0]));
return;
}
return null;
args.push(lib.getAllInterfaces);
var getMacAddress = function (d, cb) {
if (addresses[d]) {
cb(null, addresses[d]);
return;
}
lib.getMacAddress(d, cb);
};
util.iterate(args, lib.getMacAddress, callback);
};

lib.all = function (callback) {
if (typeof callback !== 'function') {
return promisify(lib.all);
if (typeof callback !== "function") {
return util.promisify(lib.all);
}

var ifaces = lib.networkInterfaces();
Expand All @@ -122,17 +84,17 @@ lib.all = function (callback) {
});

if (Object.keys(resolve).length === 0) {
if (typeof callback === 'function') {
nextTick(callback.bind(null, null, ifaces));
if (typeof callback === "function") {
util.nextTick(callback.bind(null, null, ifaces));
}
return ifaces;
}

parallel(resolve, function (err, result) {
util.parallel(resolve, function (err, result) {
Object.keys(result).forEach(function (iface) {
ifaces[iface].mac = result[iface];
});
if (typeof callback === 'function') {
if (typeof callback === "function") {
callback(null, ifaces);
}
});
Expand Down

0 comments on commit 86fc63f

Please sign in to comment.