Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

macaddress

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

macaddress - npm Package Compare versions

Comparing version 0.4.4 to 0.5.0

.editorconfig

210

index.js
/* jshint node: true */
'use strict';
"use strict";
var os = require('os');
var util = require("./lib/util.js");
var lib = {};
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];
(process.nextTick || global.setImmediate || global.setTimeout)(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");
// Retrieves all interfaces that do feature some non-internal address.
// This function does NOT employ caching as to reflect the current state
// of the machine accurately.
lib.networkInterfaces = function () {
var allAddresses = {};
try {
var ifaces = os.networkInterfaces();
} catch (e) {
// At October 2016 WSL does not support os.networkInterfaces() and throws
// Return empty object as if no interfaces were found
// https://github.com/Microsoft/BashOnWindows/issues/468
if (e.syscall === 'uv_interface_addresses') {
return allAddresses;
} else {
throw e;
};
};
Object.keys(ifaces).forEach(function (iface) {
var addresses = {};
var hasAddresses = false;
ifaces[iface].forEach(function (address) {
if (!address.internal) {
addresses[(address.family || "").toLowerCase()] = address.address;
hasAddresses = true;
if (address.mac && address.mac !== '00:00:00:00:00:00') {
addresses.mac = address.mac;
}
}
});
if (hasAddresses) {
allAddresses[iface] = addresses;
}
});
return allAddresses;
};
var _getMacAddress;
var _validIfaceRegExp = '^[a-z0-9]+$';
switch (os.platform()) {
case 'win32':
// windows has long interface names which may contain spaces and dashes
_validIfaceRegExp = '^[a-z0-9 -]+$';
_getMacAddress = require('./lib/windows.js');
break;
case 'linux':
_getMacAddress = require('./lib/linux.js');
break;
case 'darwin':
case 'sunos':
case 'freebsd':
_getMacAddress = require('./lib/unix.js');
break;
default:
console.warn("node-macaddress: Unknown os.platform(), defaulting to 'unix'.");
_getMacAddress = require('./lib/unix.js');
break;
}
var validIfaceRegExp = new RegExp(_validIfaceRegExp, 'i');
function getMacAddress(iface, callback) {
// some platform specific ways of resolving the mac address pass the name
// of the interface down to some command processor, so check for a well
// formed string here.
if (!validIfaceRegExp.test(iface)) {
callback(new Error([
'invalid iface: \'', iface,
'\' (must conform to reg exp /',
validIfaceRegExp, '/)'
].join('')), null);
return;
}
_getMacAddress(iface, callback);
}
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.one = function () {

@@ -141,9 +20,9 @@ // one() can be invoked in several ways:

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];

@@ -154,42 +33,45 @@ }

if (!callback) {
return promisify(function (callback) {
return util.promisify(function (callback) {
lib.one(iface, callback);
});
}
if (iface) {
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;
lib.getMacAddress(iface, callback);
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]);
}
}
}
if (!ifaces[iface]) {
if (typeof callback === 'function') {
process.nextTick(function() {
callback(new Error("no interfaces found"), null);
});
}
return null;
});
if (best.length > 0) {
util.nextTick(callback.bind(null, null, best[0]));
return;
}
args.push(lib.getAllInterfaces);
var getMacAddress = function (d, cb) {
if (addresses[d]) {
cb(null, addresses[d]);
return;
}
if (ifaces[iface].mac) {
if (typeof callback === 'function') {
process.nextTick(function() {
callback(null, ifaces[iface].mac);
});
}
return ifaces[iface].mac;
}
}
return null;
lib.getMacAddress(d, cb);
};
util.iterate(args, getMacAddress, callback);
};
lib.all = function (callback) {
if (typeof callback !== 'function') {
return promisify(lib.all);
if (typeof callback !== "function") {
return util.promisify(lib.all);
}

@@ -202,3 +84,3 @@

if (!ifaces[iface].mac) {
resolve[iface] = getMacAddress.bind(null, iface);
resolve[iface] = lib.getMacAddress.bind(null, iface);
}

@@ -208,4 +90,4 @@ });

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

@@ -215,7 +97,7 @@ 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);

@@ -222,0 +104,0 @@ }

{
"name": "macaddress",
"version": "0.4.4",
"version": "0.5.0",
"description": "Get the MAC addresses (hardware addresses) of the hosts network interfaces.",

@@ -5,0 +5,0 @@ "main": "index.js",

/* jshint node: true */
'use strict';
var macaddress = require('./index');
var macaddress = require('./index.js');
var sync = macaddress.one(function (err, mac) {
macaddress.one(function (err, mac) {
if (err || !/[a-f0-9]{2}(:[a-f0-9]{2}){5}/.test(mac)) {
console.log(mac + " does not work");
throw err || mac;
}
console.log("Mac address for this host: %s", mac);
console.log("Mac address for this host: %s", mac);
});
console.log("Mac address obtained synchronously: %s", sync);

@@ -14,0 +14,0 @@ macaddress.all(function (err, all) {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc