forked from alibaba/anyproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
157 lines (129 loc) · 4.29 KB
/
util.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
var fs = require("fs"),
path = require("path"),
mime = require('mime-types'),
color = require('colorful'),
logUtil = require("./log"),
exec = require('child_process').exec;
const changeCase = require('change-case');
// {"Content-Encoding":"gzip"} --> {"content-encoding":"gzip"}
module.exports.lower_keys = function(obj){
for(var key in obj){
var val = obj[key];
delete obj[key];
obj[key.toLowerCase()] = val;
}
return obj;
}
module.exports.merge = function(baseObj, extendObj){
for(var key in extendObj){
baseObj[key] = extendObj[key];
}
return baseObj;
}
function getUserHome(){
return process.env.HOME || process.env.USERPROFILE;
}
module.exports.getUserHome = getUserHome;
module.exports.getAnyProxyHome = function(){
var home = path.join(util.getUserHome(),"/.anyproxy/");
if(!fs.existsSync(home)){
try{
fs.mkdirSync(home, '0777');
}catch(e){
return null;
}
}
return home;
}
var CACHE_DIR_PREFIX = "cache_r";
module.exports.generateCacheDir = function(){
var rand = Math.floor(Math.random() * 1000000),
cachePath = path.join(util.getAnyProxyHome(),"./" + CACHE_DIR_PREFIX + rand);
fs.mkdirSync(cachePath, '0777');
return cachePath;
}
module.exports.clearCacheDir = function(cb){
var home = util.getAnyProxyHome(),
isWin = /^win/.test(process.platform);
var dirNameWildCard = CACHE_DIR_PREFIX + "*";
if(isWin){
exec("for /D %f in (" + dirNameWildCard + ") do rmdir %f /s /q",{cwd : home},cb);
}else{
exec("rm -rf " + dirNameWildCard + "",{cwd : home},cb);
}
}
module.exports.simpleRender = function(str, object, regexp){
return String(str).replace(regexp || (/\{\{([^{}]+)\}\}/g), function(match, name){
if (match.charAt(0) == '\\') return match.slice(1);
return (object[name] != null) ? object[name] : '';
});
}
module.exports.filewalker = function(root,cb){
root = root || process.cwd();
var ret = {
directory :[],
file :[]
};
fs.readdir(root,function(err, list){
if(list && list.length){
list.map(function(item){
var fullPath = path.join(root,item),
stat = fs.lstatSync(fullPath);
if(stat.isFile()){
ret.file.push({
name : item,
fullPath : fullPath
});
}else if(stat.isDirectory()){
ret.directory.push({
name : item,
fullPath : fullPath
});
}
});
}
cb && cb.apply(null,[null,ret]);
});
};
/*
* 获取文件所对应的content-type以及content-length等信息
* 比如在useLocalResponse的时候会使用到
*/
module.exports.contentType = function (filepath) {
return mime.contentType(path.extname(filepath));
};
/*
* 读取file的大小,以byte为单位
*/
module.exports.contentLength = function (filepath) {
try {
var stat = fs.statSync(filepath);
return stat.size;
} catch (e) {
logUtil.printLog(color.red("\nfailed to ready local file : " + filepath));
logUtil.printLog(color.red(e));
return 0;
}
};
module.exports.showRootInstallTip = function () {
logUtil.printLog(color.red("can not find rootCA.crt or rootCA.key"), logUtil.T_ERR);
logUtil.printLog(color.red("you may generate one by the following methods"), logUtil.T_ERR);
logUtil.printLog(color.red("\twhen using globally : anyproxy --root"), logUtil.T_ERR);
logUtil.printLog(color.red("\twhen using as a module : require(\"anyproxy\").generateRootCA();"), logUtil.T_ERR);
logUtil.printLog(color.red("\tmore info : https://github.com/alibaba/anyproxy/wiki/How-to-config-https-proxy"), logUtil.T_ERR);
};
/*
* remove the cache before requering, the path SHOULD BE RELATIVE TO UTIL.JS
*/
module.exports.freshRequire = function (path) {
delete require.cache[require.resolve(path)];
return require(path);
};
module.exports.upper_keys = function (obj) {
var upperObject = {};
for(var key in obj) {
var upperKey = changeCase.headerCase(key);
upperObject[upperKey] = obj[key];
}
return upperObject;
};