@@ -69,9 +69,12 @@ lib.networkInterfaces = function () {
69
69
} ;
70
70
71
71
var _getMacAddress ;
72
+ var _validIfaceRegExp = '^[a-z0-9]+$' ;
72
73
switch ( os . platform ( ) ) {
73
74
74
75
case 'win32' :
76
+ // windows has long interface names which may contain spaces and dashes
77
+ _validIfaceRegExp = '^[a-z0-9 -]+$' ;
75
78
_getMacAddress = require ( './lib/windows.js' ) ;
76
79
break ;
77
80
@@ -92,12 +95,14 @@ switch (os.platform()) {
92
95
93
96
}
94
97
95
- var validIfaceRegExp = '^[a-z0-9]+$' ;
96
- var validIfaceRegExpObj = new RegExp ( validIfaceRegExp , 'i' ) ;
98
+ var validIfaceRegExp = new RegExp ( _validIfaceRegExp , 'i' ) ;
97
99
98
100
function getMacAddress ( iface , callback ) {
99
101
100
- if ( ! validIfaceRegExpObj . test ( iface ) ) {
102
+ // some platform specific ways of resolving the mac address pass the name
103
+ // of the interface down to some command processor, so check for a well
104
+ // formed string here.
105
+ if ( ! validIfaceRegExp . test ( iface ) ) {
101
106
callback ( new Error ( [
102
107
'invalid iface: \'' , iface ,
103
108
'\' (must conform to reg exp /' ,
@@ -124,16 +129,35 @@ function promisify(func) {
124
129
} ) ;
125
130
}
126
131
127
- lib . one = function ( iface , callback ) {
128
- if ( ! callback && typeof iface !== 'function' ) {
132
+ lib . one = function ( ) {
133
+ // one() can be invoked in several ways:
134
+ // one() -> Promise<string>
135
+ // one(iface: string) -> Promise<string>
136
+ // one(iface: string, callback) -> async, yields a string
137
+ // one(callback) -> async, yields a string
138
+ var iface = null ;
139
+ var callback = null ;
140
+ if ( arguments . length >= 1 ) {
141
+ if ( typeof arguments [ 0 ] === 'function' ) {
142
+ callback = arguments [ 0 ] ;
143
+ } else if ( typeof arguments [ 0 ] === 'string' ) {
144
+ iface = arguments [ 0 ] ;
145
+ }
146
+ if ( arguments . length >= 2 ) {
147
+ if ( typeof arguments [ 1 ] === 'function' ) {
148
+ callback = arguments [ 1 ] ;
149
+ }
150
+ }
151
+ }
152
+ if ( ! callback ) {
129
153
return promisify ( function ( callback ) {
130
154
lib . one ( iface , callback ) ;
131
155
} ) ;
132
156
}
133
157
134
- if ( typeof iface === 'function' ) {
135
- callback = iface ;
136
-
158
+ if ( iface ) {
159
+ getMacAddress ( iface , callback ) ;
160
+ } else {
137
161
var ifaces = lib . networkInterfaces ( ) ;
138
162
var alleged = [ 'eth0' , 'eth1' , 'en0' , 'en1' , 'en2' , 'en3' , 'en4' ] ;
139
163
iface = Object . keys ( ifaces ) [ 0 ] ;
@@ -160,9 +184,6 @@ lib.one = function (iface, callback) {
160
184
return ifaces [ iface ] . mac ;
161
185
}
162
186
}
163
- if ( typeof callback === 'function' ) {
164
- getMacAddress ( iface , callback ) ;
165
- }
166
187
return null ;
167
188
} ;
168
189
0 commit comments