-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathhelper.js
215 lines (197 loc) · 5.46 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
var protodef = require(__dirname+'/protodef.js');
var termTypes = protodef.Term.TermType;
var datumTypes = protodef.Datum.DatumType;
var net = require('net');
function createLogger(poolMaster, silent) {
return function(message) {
if (silent !== true) {
console.error(message);
}
poolMaster.emit('log', message);
}
}
module.exports.createLogger = createLogger;
function isPlainObject(obj) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
module.exports.isPlainObject = isPlainObject;
function toArray(args) {
return Array.prototype.slice.call(args);
}
module.exports.toArray = toArray;
function hasImplicit(arg) {
if (Array.isArray(arg)) {
if (arg[0] === termTypes.IMPLICIT_VAR) return true;
if (Array.isArray(arg[1])) {
for(var i=0; i<arg[1].length; i++) {
if (hasImplicit(arg[1][i])) return true;
}
}
if (isPlainObject(arg[2])) {
for(var key in arg[2]) {
if (hasImplicit(arg[2][key])) return true;
}
}
}
else if (isPlainObject(arg)) {
for(var key in arg) {
if (hasImplicit(arg[key])) return true;
}
}
return false;
}
module.exports.hasImplicit = hasImplicit;
function loopKeys(obj, fn) {
var keys = Object.keys(obj);
var result;
var keysLength = keys.length;
for(var i=0; i<keysLength; i++) {
result = fn(obj, keys[i]);
if (result === false) return;
}
}
module.exports.loopKeys = loopKeys;
function convertPseudotype(obj, options) {
var reqlType = obj['$reql_type$'];
if (reqlType === 'TIME' && options['timeFormat'] !== 'raw') {
return new Date(obj['epoch_time'] * 1000);
}
else if (reqlType === 'GROUPED_DATA' && options['groupFormat'] !== 'raw') {
var result = [];
for (var i = 0, len = obj['data'].length, ref; i < len; i++) {
ref = obj.data[i];
result.push({
group: ref[0],
reduction: ref[1]
});
}
return result;
}
else if (reqlType === 'BINARY' && options['binaryFormat'] !== 'raw') {
return new Buffer(obj['data'], 'base64');
}
return obj;
}
function recursivelyConvertPseudotype(obj, options) {
var i, value, len, key;
if (Array.isArray(obj)) {
for (i = 0, len = obj.length; i < len; i++) {
value = obj[i];
obj[i] = recursivelyConvertPseudotype(value, options);
}
}
else if (obj && typeof obj === 'object') {
for (key in obj) {
value = obj[key];
obj[key] = recursivelyConvertPseudotype(value, options);
}
obj = convertPseudotype(obj, options);
}
return obj;
}
function makeAtom(response, options) {
options = options || {};
return recursivelyConvertPseudotype(response.r[0], options);
}
module.exports.makeAtom = makeAtom;
function makeSequence(response, options) {
options = options || {};
return recursivelyConvertPseudotype(response.r, options);
}
module.exports.makeSequence = makeSequence;
function changeProto(object, other) {
object.__proto__ = other.__proto__;
}
module.exports.changeProto = changeProto;
// Try to extract the most global address
// Note: Mutate the input
function getCanonicalAddress(addresses) {
// We suppose that the addresses are all valid, and therefore use loose regex
for(var i=0; i<addresses.length; i++) {
var addresse = addresses[i];
if ((/^127(\.\d{1,3}){3}$/.test(addresse.host)) || (/0?:?0?:?0?:?0?:?0?:?0?:0?:1/.test(addresse.host))) {
addresse.value = 0;
}
else if ((net.isIPv6(addresse.host)) && (/^[fF]|[eE]80:.*\:.*\:/.test(addresse.host))) {
addresse.value = 1;
}
else if (/^169\.254\.\d{1,3}\.\d{1,3}$/.test(addresse.host)) {
addresse.value = 2;
}
else if (/^192\.168\.\d{1,3}\.\d{1,3}$/.test(addresse.host)) {
addresse.value = 3;
}
else if (/^172\.(1\d|2\d|30|31)\.\d{1,3}\.\d{1,3}$/.test(addresse.host)) {
addresse.value = 4;
}
else if (/^10(\.\d{1,3}){3}$/.test(addresse.host)) {
addresse.value = 5;
}
else if ((net.isIPv6(addresse.host)) && (/^[fF]|[cCdD].*\:.*\:/.test('addresse.host'))) {
addresse.value = 6;
}
else {
addresse.value = 7;
}
}
var result = addresses[0];
var max = addresses[0].value;
for(var i=0; i<addresses.length; i++) {
if (addresses[i].value > max) {
result = addresses[i];
max = addresses[i].value;
}
}
return result;
}
module.exports.getCanonicalAddress = getCanonicalAddress;
module.exports.localhostAliases = {
'localhost': true,
'127.0.0.1': true,
'::1': true
}
module.exports.tryCatch = function tryCatch(toTry, handleError) {
try{
toTry()
}
catch(err) {
handleError(err)
}
}
function splitCommaEqual(message) {
var result = {};
var messageParts = message.split(',');
for(var i=0; i<messageParts.length; i++) {
var equalPosition = messageParts[i].indexOf("=")
result[messageParts[i].slice(0, equalPosition)] = messageParts[i].slice(equalPosition+1);
}
return result;
}
module.exports.splitCommaEqual = splitCommaEqual;
function xorBuffer(a, b) {
var result = [];
var len = Math.min(a.length, b.length)
for(var i=0; i<len; i++) {
result.push(a[i] ^ b[i]);
}
return new Buffer(result);
}
module.exports.xorBuffer = xorBuffer;
function compareDigest(a, b) {
var left = undefined
var right = b
var result = undefined
if (a.length === b.length) {
left = a
result = 0
} else {
left = b
result = 1
}
var len = Math.min(a.length, b.length);
for(var i=0; i<len; i++) {
result |= a[i] ^b[i]
}
return result === 0
}
module.exports.compareDigest = compareDigest;