Skip to content

Commit 68b8e8b

Browse files
authored
Merge pull request #9 from esatterwhite/master
Update for node v6
2 parents f0d6094 + fc95b26 commit 68b8e8b

12 files changed

+629
-838
lines changed

lib/common.js

100755100644
+59-49
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,73 @@
1-
// Generated by CoffeeScript 1.6.3
2-
(function() {
3-
var crypto, os, zmq;
1+
/*jshint laxcomma: true, smarttabs: true, node: true, esnext: true*/
2+
'use strict';
3+
const crypto = require('crypto')
4+
, os = require('os')
5+
, zmq = require('zmq')
6+
, tcp_exp = /tcp:\/\/[\d.]+:(\d+)\/?/
7+
, vm_exp = /VMware/
8+
, localip_exp = /0\.0\.0\.0/
9+
, tcp_proto = /^tcp\:\/\//;
10+
;
411

5-
crypto = require('crypto');
6-
7-
os = require('os');
12+
exports.get_random = function(){
13+
return crypto.randomBytes(20).toString('hex');
14+
};
815

9-
zmq = require('zmq');
16+
exports.isNumber = function(n){
17+
return !isNaN(parseFloat(n)) && isFinite(n);
18+
};
1019

11-
exports.get_random = function() {
12-
return crypto.randomBytes(20).toString('hex');
13-
};
14-
15-
exports.list_local_ips = function() {
16-
var a, addrs, iface_name, ips, _i, _len, _ref;
17-
ips = [];
18-
_ref = os.networkInterfaces();
19-
for (iface_name in _ref) {
20-
addrs = _ref[iface_name];
21-
if (/VMware/.test(iface_name)) {
22-
continue;
23-
}
24-
for (_i = 0, _len = addrs.length; _i < _len; _i++) {
25-
a = addrs[_i];
26-
if (!a.internal && a.family === 'IPv4') {
27-
ips.push(a.address);
20+
exports.list_local_ips = function(){
21+
let ips = [];
22+
let interfaces = os.networkInterfaces();
23+
for(let iface_name in interfaces){
24+
if(vm_exp.test(iface_name)){ continue; }
25+
let addrs = interfaces[ iface_name ];
26+
for(let a in addrs){
27+
let iface = addrs[a];
28+
if(!iface.internal && iface.family == 'IPv4'){
29+
ips.push(iface.address);
30+
}
2831
}
29-
}
3032
}
3133
return ips;
32-
};
33-
34-
exports.get_local_endpoint = function(sock) {
35-
var addr, ip;
34+
};
35+
exports.get_local_endpoint = function(sock){
36+
let addr, ip;
3637
addr = sock.getsockopt(zmq.ZMQ_LAST_ENDPOINT);
37-
if (addr.indexOf('tcp://') !== 0) {
38-
return addr;
38+
if(tcp_proto.test( addr )){
39+
return addr;
3940
}
41+
42+
// resolve to a locally reachable IP if tcp://
4043
ip = this.list_local_ips()[0];
41-
return addr.replace(/0\.0\.0\.0/, "" + ip);
42-
};
43-
44-
exports.get_port = function(addr) {
45-
var m;
46-
m = addr.match(/tcp:\/\/[\d.]+:(\d+)\/?/);
47-
if (m) {
48-
return Number(m[1]);
49-
} else {
50-
return -1;
51-
}
52-
};
44+
return addr.replace(localip_exp, `${ip}`);
45+
};
46+
47+
exports.get_port = function(addr){
48+
let m = addr.match(tcp_exp);
49+
return m ? Number(m[1]) : -1;
50+
};
51+
52+
exports.bindAndGetAddr = function(sock_type, addr){
53+
let sock, endpoint, port;
5354

54-
exports.bindAndGetAddr = function(sock_type, addr) {
55-
var port, sock;
5655
sock = zmq.socket(sock_type);
5756
sock.bindSync(addr);
58-
addr = sock.getsockopt(zmq.ZMQ_LAST_ENDPOINT);
57+
endpoint = sock.getsockopt(zmq.ZMQ_LAST_ENDPOINT);
5958
port = exports.get_port(addr);
60-
return [sock, addr, port];
61-
};
59+
return [sock, endpoint, port];
60+
};
61+
62+
exports.lpad = function(value, padding){
63+
let zeroes = '0', idx = padding;
64+
while( idx ){
65+
zeroes += '0';
66+
idx--;
67+
}
68+
return (zeroes + value).slice(padding * - 1);
69+
};
6270

63-
}).call(this);
71+
exports.random = function( length = 10 ){
72+
return Math.floor(Math.random() * 1024 * 1024);
73+
};

0 commit comments

Comments
 (0)