-
Notifications
You must be signed in to change notification settings - Fork 2
/
appToolBelt.js
54 lines (54 loc) · 1.61 KB
/
appToolBelt.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
// appToolBelt (atb)
// handy little functions to make get an app on its feet easier/faster.
var myname = "atb: ";
// --
var logHelp = require("./log.js");
// --
module.exports = function(settings){
var exports = {};
var atb = exports;
// --
exports.express = require("express");
exports.underscore = require("underscore");
// --
exports.getClientIp = function(req){
var ipAddress;
var forwardedIpsStr = req.header('x-forwarded-for');
if(forwardedIpsStr){
ipAddress = forwardedIpsStr.split(',')[0];
}
if(!ipAddress){
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
exports.getClientIpBase36 = function(req){
var ip = atb.getClientIp(req)||"";
var ipa = ip.split(".")||[];
var a = parseInt(ipa[0]||0, 10);
var b = parseInt(ipa[1]||0, 10);
var c = parseInt(ipa[2]||0, 10);
var d = parseInt(ipa[3]||0, 10);
var num = a*256*256*256;
num += b*256*256;
num += c*256;
num += d;
return num.toString(36);
};
exports.escapeHTML = function(msg){
return (msg||"").replace(/\&/g, "&").replace(/</g, "<").replace(/\>/g, ">");
};
// --
exports.enableHighAvailability = function(http){
log3("Enabling (posix) high availibility -> go go go!");
var posix = require('posix');
var limits = posix.getrlimit('nofile');
log('* Default limits: soft=' + limits.soft + ', hard=' + limits.hard);
posix.setrlimit('nofile', { soft: 16384, hard: 32768 });
var limitsNow = posix.getrlimit('nofile');
log('* --> New limits: soft=' + limitsNow.soft + ', hard=' + limitsNow.hard);
http.globalAgent.maxSockets = limitsNow.soft;
};
// --
return exports;
};