ming_node 2.3.0 → 2.8.0
Sign up to get free protection for your applications and to get access to all the features.
- package/beforeTest/BaseMapperTest1.js +24 -0
- package/beforeTest/CollectUtilsTest.js +10 -0
- package/index.js +2533 -2523
- package/module/BaseMapper.js +38 -1
- package/package.json +1 -1
- package/utils/common/CollectionUtils.js +111 -0
- package/utils/common/DateUtils.js +36 -0
package/index.js
CHANGED
@@ -1,65 +1,65 @@
|
|
1
1
|
/**
|
2
|
-
* File :
|
2
|
+
* File : index.js
|
3
3
|
* By : Minglie
|
4
4
|
* QQ: 934031452
|
5
5
|
* Date :2021.12.01
|
6
|
-
* version :2.
|
6
|
+
* version :2.8.0
|
7
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
|
-
|
8
|
+
var http = require('http');
|
9
|
+
var https = require('https');
|
10
|
+
var url_module = require('url');
|
11
|
+
var querystring = require('querystring');
|
12
|
+
var fs = require('fs');
|
13
|
+
var path = require('path');
|
14
|
+
var child_process = require('child_process');
|
15
|
+
var EventEmitter = require('events').EventEmitter;
|
16
|
+
var event = new EventEmitter();
|
17
|
+
var privateObj = {};//本文件私有对象
|
18
|
+
var M = {};
|
19
|
+
M.privateObj=privateObj;
|
20
|
+
M.sessions = {}//保存session
|
21
|
+
M.con_display_status_enable = false;//是否显示响应状态码
|
22
|
+
M.cookie = "JSESSIONID=" + "6E202D5A022EBD62705AA436EC54963B";//请求携带的cook
|
23
|
+
M.reqComQueryparams = undefined;//请求的公共的查询参数
|
24
|
+
M.reqComHeaders = undefined;//请求的公共请求头
|
25
|
+
M.host = "http://127.0.0.1:7001";
|
26
|
+
M.log_file_enable = true;//将日志输出到文件
|
27
|
+
M.log_console_enable = true;//将日志输出到控制台
|
28
|
+
M.log_path = "./M.log";//输出日志文件路径
|
29
|
+
M.map_path = "./M_map.json";//全局作用域路径
|
30
|
+
M.database_path = "./M_database.json";//文件型数据库路径
|
31
|
+
M.log_display_time = true;//日志是否显示当前时间
|
32
|
+
M.httpProxy = {};// http 代理配置
|
33
|
+
M._sseClientMap=new Map();
|
34
|
+
M._sseHeatTime=3000;
|
35
|
+
M._moduleMap=new Map();//模块map
|
36
|
+
M.httpBefore = (d) => {
|
37
|
+
return d
|
38
|
+
}
|
39
|
+
M.httpEnd = (d) => {
|
40
|
+
}
|
41
|
+
//全局缓存map
|
42
|
+
M._globle_cacheMap = {}
|
43
|
+
//全局对象缓存
|
44
|
+
M._globle_lib_cacheMap={}
|
45
|
+
//全局插件地址缓存
|
46
|
+
M._globle_plugin_url_cacheMap={};
|
47
|
+
//全局插件
|
48
|
+
M._globle_plugin=new Set();
|
49
|
+
M._node_lib_path=process.cwd()+"/.ming_node_cacke";
|
50
|
+
//远程静态资源路径
|
51
|
+
M.remoteStaticPath = "https://minglie.gitee.io/mingpage/static";
|
52
|
+
M.remoteStaticPathEnable = true;
|
53
|
+
//代理服务器配置
|
54
|
+
M.proxyHost = "http://127.0.0.1:8888"
|
55
|
+
M.proxyHost = "";
|
56
|
+
M.IO={}
|
57
|
+
M.setModule=function (key,module){
|
58
58
|
M._moduleMap.set(key,module);
|
59
|
-
|
60
|
-
|
59
|
+
}
|
60
|
+
M.getModule=function (key){
|
61
61
|
M._moduleMap.get(key);
|
62
|
-
|
62
|
+
}
|
63
63
|
|
64
64
|
M.getGloblePlugin=(pluginKey)=>{
|
65
65
|
let plugin=null;
|
@@ -72,296 +72,296 @@ M.getGloblePlugin=(pluginKey)=>{
|
|
72
72
|
}
|
73
73
|
|
74
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
|
-
|
75
|
+
/**
|
76
|
+
* ----------------------客户端START--------------------------------------------
|
77
|
+
*/
|
78
|
+
//解析对象或函数返回值
|
79
|
+
privateObj.getFunctionOrObjResult = function (objOrFunc, obj) {
|
80
|
+
let c1;
|
81
|
+
if (!objOrFunc) {
|
82
|
+
return obj;
|
83
|
+
}
|
84
|
+
if (typeof objOrFunc == "function") {
|
85
|
+
c1 = objOrFunc();
|
86
|
+
} else {
|
87
|
+
c1 = objOrFunc;
|
88
|
+
}
|
89
|
+
return Object.assign(obj, c1);
|
90
|
+
}
|
91
|
+
|
92
|
+
//将对象追加到url上
|
93
|
+
privateObj.appendDataToUrl = function (url, data) {
|
94
|
+
let getData = "";
|
95
|
+
if (data) {
|
96
|
+
getData = querystring.stringify(data);
|
97
|
+
//url携带参数了
|
98
|
+
if (url.indexOf("?") > 0) {
|
99
|
+
getData = "&" + getData;
|
100
|
+
} else {
|
101
|
+
getData = "?" + getData;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
let r = url + getData;
|
105
|
+
return r;
|
106
|
+
}
|
107
|
+
|
108
108
|
M.get = function (url, callback, data, headers) {
|
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
|
-
|
109
|
+
if (typeof callback == "function") {
|
110
|
+
|
111
|
+
} else {
|
112
|
+
headers = data || {};
|
113
|
+
data = callback;
|
114
|
+
callback = () => {
|
115
|
+
};
|
116
|
+
}
|
117
|
+
if (headers) {
|
118
|
+
} else {
|
119
|
+
headers = {
|
120
|
+
'Content-Type': 'application/json',
|
121
|
+
'Cookie': M.cookie
|
122
|
+
}
|
123
|
+
}
|
124
|
+
let getData = "";
|
125
|
+
if (data || M.reqComQueryparams) {
|
126
|
+
data = privateObj.getFunctionOrObjResult(M.reqComQueryparams, data)
|
127
|
+
getData = querystring.stringify(data);
|
128
|
+
//url携带参数了
|
129
|
+
if (url.indexOf("?") > 0) {
|
130
|
+
getData = "&" + getData;
|
131
|
+
} else {
|
132
|
+
getData = "?" + getData;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
//合并请求头
|
136
|
+
headers = privateObj.getFunctionOrObjResult(M.reqComHeaders, headers)
|
137
|
+
var html = '';
|
138
|
+
var urlObj = url_module.parse(url)
|
139
|
+
var options = {
|
140
|
+
hostname: urlObj.hostname,
|
141
|
+
port: urlObj.port,
|
142
|
+
path: urlObj.path + getData,
|
143
|
+
method: 'GET',
|
144
|
+
headers: headers
|
145
|
+
}
|
146
|
+
if (Object.keys(M.httpProxy).length > 0) {
|
147
|
+
options.host = M.httpProxy.host;
|
148
|
+
options.port = M.httpProxy.port;
|
149
|
+
options.path = url + getData;
|
150
|
+
delete options.hostname;
|
151
|
+
}
|
152
|
+
let reqHttp = http;
|
153
|
+
if (url.startsWith("https")) {
|
154
|
+
reqHttp = https;
|
155
|
+
}
|
156
|
+
|
157
|
+
return new Promise((resolve, reject) => {
|
158
|
+
options = M.httpBefore(options);
|
159
|
+
if (options == false) {
|
160
|
+
return;
|
161
|
+
}
|
162
|
+
var req = reqHttp.request(options, function (res) {
|
163
|
+
if (M.con_display_status_enable) console.log('STATUS:' + res.statusCode);
|
164
|
+
if (global.debug && res.statusCode != 200) {
|
165
|
+
while (1) {
|
166
|
+
M.sleep(1000);
|
167
|
+
console.log('STATUS:' + res.statusCode);
|
168
|
+
console.log("--------ERROR:" + res.req.path + "-------------");
|
169
|
+
}
|
170
|
+
}
|
171
|
+
res.setEncoding('utf-8');
|
172
|
+
res.on('data', function (chunk) {
|
173
|
+
html += chunk;
|
174
|
+
});
|
175
|
+
res.on('end', function () {
|
176
|
+
callback(html, res);
|
177
|
+
try {
|
178
|
+
html = JSON.parse(html)
|
179
|
+
} catch (e) {
|
180
|
+
html = html;
|
181
|
+
}
|
182
|
+
M.httpEnd(html);
|
183
|
+
resolve(html);
|
184
|
+
});
|
185
|
+
});
|
186
|
+
req.on('error', function (err) {
|
187
|
+
reject(err);
|
188
|
+
console.error(err);
|
189
|
+
|
190
|
+
});
|
191
|
+
req.end();
|
192
|
+
})
|
193
|
+
}
|
194
194
|
M._request = function (url, callback, data, headers,methed) {
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
195
|
+
if (typeof callback == "function") {
|
196
|
+
|
197
|
+
} else {
|
198
|
+
headers = data || {};
|
199
|
+
data = callback;
|
200
|
+
callback = () => {
|
201
|
+
};
|
202
|
+
}
|
203
|
+
|
204
|
+
url = privateObj.appendDataToUrl(url, M.reqComQueryparams);
|
205
|
+
var html = '';
|
206
|
+
var urlObj = url_module.parse(url)
|
207
|
+
//发送 http Post 请求
|
208
|
+
var postData = querystring.stringify(data);
|
209
|
+
if( headers["Content-Type"]==undefined){
|
210
|
+
headers["Content-Type"] ="application/json";
|
211
|
+
}
|
212
|
+
if (headers["Content-Type"] == "application/json") {
|
213
|
+
postData = JSON.stringify(data);
|
214
|
+
}else {
|
215
|
+
headers = {
|
216
|
+
'Content-Type': 'application/x-www-form-urlencoded; ' +
|
217
|
+
'charset=UTF-8',
|
218
|
+
'Content-Length': Buffer.byteLength(postData)
|
219
|
+
}
|
220
|
+
}
|
221
|
+
|
222
|
+
//合并请求头
|
223
|
+
headers = privateObj.getFunctionOrObjResult(M.reqComHeaders, headers)
|
224
|
+
var options = {
|
225
|
+
hostname: urlObj.hostname,
|
226
|
+
port: urlObj.port,
|
227
|
+
path: urlObj.path,
|
228
|
+
method: methed,
|
229
|
+
headers: headers
|
230
|
+
}
|
231
|
+
if (Object.keys(M.httpProxy).length > 0) {
|
232
|
+
options.host = M.httpProxy.host;
|
233
|
+
options.port = M.httpProxy.port;
|
234
|
+
options.path = url;
|
235
|
+
delete options.hostname;
|
236
|
+
}
|
237
|
+
let reqHttp = http;
|
238
|
+
if (url.startsWith("https")) {
|
239
|
+
reqHttp = https;
|
240
|
+
}
|
241
|
+
|
242
|
+
return new Promise((resolve, reject) => {
|
243
|
+
var req = reqHttp.request(options, function (res) {
|
244
|
+
options = M.httpBefore(options);
|
245
|
+
if (options == false) {
|
246
|
+
return;
|
247
|
+
}
|
248
|
+
if (M.con_display_status_enable) console.log('STATUS:' + res.statusCode);
|
249
|
+
if (global.debug && res.statusCode != 200) {
|
250
|
+
while (1) {
|
251
|
+
M.sleep(1000);
|
252
|
+
console.log('STATUS:' + res.statusCode);
|
253
|
+
console.log("--------ERROR:" + res.req.path + "-------------");
|
254
|
+
}
|
255
|
+
}
|
256
|
+
// console.log('headers:',JSON.stringify(res.headers));
|
257
|
+
res.setEncoding('utf-8');
|
258
|
+
res.on('data', function (chunk) {
|
259
|
+
html += chunk;
|
260
|
+
});
|
261
|
+
res.on('end', function () {
|
262
|
+
callback(html, res);
|
263
|
+
try {
|
264
|
+
html = JSON.parse(html)
|
265
|
+
} catch (e) {
|
266
|
+
html = html;
|
267
|
+
}
|
268
|
+
M.httpEnd(html);
|
269
|
+
resolve(html);
|
270
|
+
});
|
271
|
+
|
272
|
+
});
|
273
|
+
|
274
|
+
req.on('error', function (err) {
|
275
|
+
console.error(err);
|
276
|
+
});
|
277
|
+
if(methed !="GET") req.write(postData);
|
278
|
+
req.end();
|
279
|
+
})
|
280
|
+
}
|
281
281
|
M.post=(url, callback, data, headers)=>M._request(url, callback, data, headers,"POST")
|
282
282
|
M.delete=(url, callback, data, headers)=>M._request(url, callback, data, headers,"DELETE")
|
283
283
|
M.put=(url, callback, data, headers)=>M._request(url, callback, data, headers,"PUT")
|
284
284
|
M.postJson = function (url, callback, data, headers) {
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
285
|
+
if (typeof callback == "function") {
|
286
|
+
|
287
|
+
} else {
|
288
|
+
headers = data || {};
|
289
|
+
data = callback;
|
290
|
+
callback = () => {
|
291
|
+
};
|
292
|
+
}
|
293
|
+
url = privateObj.appendDataToUrl(url, M.reqComQueryparams);
|
294
|
+
var html = '';
|
295
|
+
var urlObj = url_module.parse(url)
|
296
|
+
//发送 http Post 请求
|
297
|
+
var postData = JSON.stringify(data);
|
298
|
+
if (!headers) {
|
299
|
+
headers = {
|
300
|
+
'Content-Type': 'application/json; ' +
|
301
|
+
'charset=UTF-8',
|
302
|
+
'Cookie': M.cookie
|
303
|
+
}
|
304
|
+
}
|
305
|
+
//合并请求头
|
306
|
+
headers = privateObj.getFunctionOrObjResult(M.reqComHeaders, headers)
|
307
|
+
var options = {
|
308
|
+
hostname: urlObj.hostname,
|
309
|
+
port: urlObj.port,
|
310
|
+
path: urlObj.path,
|
311
|
+
method: 'POST',
|
312
|
+
headers: headers
|
313
|
+
}
|
314
|
+
if (Object.keys(M.httpProxy).length > 0) {
|
315
|
+
options.host = M.httpProxy.host;
|
316
|
+
options.port = M.httpProxy.port;
|
317
|
+
options.path = url;
|
318
|
+
delete options.hostname;
|
319
|
+
}
|
320
|
+
let reqHttp = http;
|
321
|
+
if (url.startsWith("https")) {
|
322
|
+
reqHttp = https;
|
323
|
+
}
|
324
|
+
|
325
|
+
return new Promise((resolve, reject) => {
|
326
|
+
|
327
|
+
var req = reqHttp.request(options, function (res) {
|
328
|
+
options = M.httpBefore(options);
|
329
|
+
if (options == false) {
|
330
|
+
return;
|
331
|
+
}
|
332
|
+
if (M.con_display_status_enable) console.log('STATUS:' + res.statusCode);
|
333
|
+
if (global.debug && res.statusCode != 200) {
|
334
|
+
while (1) {
|
335
|
+
M.sleep(1000);
|
336
|
+
console.log('STATUS:' + res.statusCode);
|
337
|
+
console.log("--------ERROR:" + res.req.path + "-------------");
|
338
|
+
}
|
339
|
+
}
|
340
|
+
// console.log('headers:',JSON.stringify(res.headers));
|
341
|
+
res.setEncoding('utf-8');
|
342
|
+
res.on('data', function (chunk) {
|
343
|
+
html += chunk;
|
344
|
+
});
|
345
|
+
res.on('end', function () {
|
346
|
+
callback(html, res);
|
347
|
+
try {
|
348
|
+
html = JSON.parse(html)
|
349
|
+
} catch (e) {
|
350
|
+
html = html;
|
351
|
+
}
|
352
|
+
M.httpEnd(html);
|
353
|
+
resolve(html);
|
354
|
+
});
|
355
|
+
|
356
|
+
});
|
357
|
+
|
358
|
+
req.on('error', function (err) {
|
359
|
+
console.error(err);
|
360
|
+
});
|
361
|
+
req.write(postData);
|
362
|
+
req.end();
|
363
|
+
})
|
364
|
+
}
|
365
365
|
//数据请求规范
|
366
366
|
M.request={}
|
367
367
|
M.request.get=M.get;
|
@@ -370,516 +370,521 @@ M.request.delete=M.delete;
|
|
370
370
|
M.request.put=M.put;
|
371
371
|
|
372
372
|
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
645
|
-
|
646
|
-
|
647
|
-
|
648
|
-
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
673
|
-
|
674
|
-
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
M.
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
|
798
|
-
|
799
|
-
|
800
|
-
|
801
|
-
|
802
|
-
|
803
|
-
|
804
|
-
M.
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
|
822
|
-
|
823
|
-
|
824
|
-
|
825
|
-
|
826
|
-
|
827
|
-
|
828
|
-
|
829
|
-
|
830
|
-
|
373
|
+
M.require =async function (url,noCache) {
|
374
|
+
//如果需要缓存
|
375
|
+
let fileName=M.getFileNameByUrl(url);
|
376
|
+
let cacheFilePath= path.join(M._node_lib_path,fileName);
|
377
|
+
if(!noCache){
|
378
|
+
|
379
|
+
if(fs.existsSync(cacheFilePath)){
|
380
|
+
return require(cacheFilePath)
|
381
|
+
}
|
382
|
+
}
|
383
|
+
let ht = "http";
|
384
|
+
if (url.startsWith("https")) {
|
385
|
+
ht = "https";
|
386
|
+
}
|
387
|
+
console.log("req require remote url:", url);
|
388
|
+
let promise = new Promise(function (reslove, reject) {
|
389
|
+
require(ht).get(url, function (req, res) {
|
390
|
+
var d = '';
|
391
|
+
req.on('data', (data) => {
|
392
|
+
d += data;
|
393
|
+
});
|
394
|
+
req.on('end', () => {
|
395
|
+
let r = "";
|
396
|
+
try {
|
397
|
+
if(fileName.endsWith(".js")){
|
398
|
+
//如果需要缓存
|
399
|
+
if(!noCache){
|
400
|
+
if (!fs.existsSync(M._node_lib_path)) {
|
401
|
+
fs.mkdirSync(M._node_lib_path);
|
402
|
+
}
|
403
|
+
M.writeFile(cacheFilePath,d);
|
404
|
+
}
|
405
|
+
r= eval(d);
|
406
|
+
}else {
|
407
|
+
r = JSON.parse(d)
|
408
|
+
}
|
409
|
+
} catch (e) {
|
410
|
+
r = d;
|
411
|
+
}
|
412
|
+
reslove(r);
|
413
|
+
});
|
414
|
+
req.on('error', (e) => reject(e.message));
|
415
|
+
})
|
416
|
+
});
|
417
|
+
return promise;
|
418
|
+
}
|
419
|
+
|
420
|
+
M.import=async function (url,callback){
|
421
|
+
if(M._globle_lib_cacheMap[url]){
|
422
|
+
return M._globle_lib_cacheMap[url];
|
423
|
+
}
|
424
|
+
if(!callback){
|
425
|
+
let r=await M.get(url)
|
426
|
+
r= eval(r)
|
427
|
+
M._globle_lib_cacheMap[url]=r;
|
428
|
+
return r
|
429
|
+
}else {
|
430
|
+
let r= callback()
|
431
|
+
M._globle_lib_cacheMap[url]=r;
|
432
|
+
return r
|
433
|
+
}
|
434
|
+
|
435
|
+
}
|
436
|
+
|
437
|
+
/**
|
438
|
+
*下载图片
|
439
|
+
*/
|
440
|
+
M.download = function (url, file, callback) {
|
441
|
+
var func = http;
|
442
|
+
if (url.indexOf("https") >= 0) {
|
443
|
+
func = https;
|
444
|
+
}
|
445
|
+
func.get(url, function (res) {
|
446
|
+
res.setEncoding('binary');//转成二进制
|
447
|
+
var content = '';
|
448
|
+
res.on('data', function (data) {
|
449
|
+
content += data;
|
450
|
+
}).on('end', function () {
|
451
|
+
if (callback) callback();
|
452
|
+
fs.writeFile(file, content, 'binary', function (err) {
|
453
|
+
if (err) throw err;
|
454
|
+
});
|
455
|
+
});
|
456
|
+
});
|
457
|
+
}
|
458
|
+
/**
|
459
|
+
*下载所有图片
|
460
|
+
*/
|
461
|
+
M.downloadAllImg = function (url, file, callback) {
|
462
|
+
var urlObj = url_module.parse(url)
|
463
|
+
var options = {
|
464
|
+
hostname: urlObj.hostname,
|
465
|
+
}
|
466
|
+
var func = http;
|
467
|
+
if (url.indexOf("https") >= 0) {
|
468
|
+
func = https;
|
469
|
+
}
|
470
|
+
var req = func.request(options, function (res) {
|
471
|
+
res.on('data', function (data) {
|
472
|
+
//Buffer
|
473
|
+
var string = data.toString();
|
474
|
+
var rule = /https?:\/\/.[^"]+\.(png|jpg|gif|jpeg)/gi;
|
475
|
+
var ary = string.match(rule); //拿到所有jpg结尾的链接集合
|
476
|
+
if (callback) callback(ary);
|
477
|
+
var x = 0;
|
478
|
+
for (var i in ary) {
|
479
|
+
M.download(ary[i], file + (x++) + ary[i].substr(ary[i].lastIndexOf(".")));
|
480
|
+
}
|
481
|
+
});
|
482
|
+
});
|
483
|
+
req.end();
|
484
|
+
}
|
485
|
+
|
486
|
+
/**
|
487
|
+
*打印结果前钩子
|
488
|
+
*/
|
489
|
+
M.beforeLogData = function (res, desc) {
|
490
|
+
console.log("-----" + desc + "-----" + res.req.path + "-------------");
|
491
|
+
}
|
492
|
+
|
493
|
+
|
494
|
+
/**
|
495
|
+
*打印结果后钩子
|
496
|
+
*/
|
497
|
+
M.afterLogData = function () {
|
498
|
+
|
499
|
+
console.log("--END")
|
500
|
+
}
|
501
|
+
|
502
|
+
/**
|
503
|
+
*简化get请求
|
504
|
+
*/
|
505
|
+
M.get0 = function (url, data) {
|
506
|
+
if (Array.isArray(url)) {
|
507
|
+
for (let i = 0; i < url.length; i++) {
|
508
|
+
M.get(
|
509
|
+
M.host + url[i],
|
510
|
+
function (data, res) {
|
511
|
+
console.log("---------" + res.req.path + "------------");
|
512
|
+
console.log(data);
|
513
|
+
}, data
|
514
|
+
);
|
515
|
+
}
|
516
|
+
} else {
|
517
|
+
M.get(
|
518
|
+
M.host + url,
|
519
|
+
function (data) {
|
520
|
+
console.log(data);
|
521
|
+
}, data
|
522
|
+
);
|
523
|
+
}
|
524
|
+
|
525
|
+
}
|
526
|
+
|
527
|
+
/**
|
528
|
+
*简化post请求
|
529
|
+
*/
|
530
|
+
M.post0 = function (url, data) {
|
531
|
+
M.post(
|
532
|
+
M.host + url,
|
533
|
+
function (data) {
|
534
|
+
console.log(data);
|
535
|
+
}, data
|
536
|
+
);
|
537
|
+
}
|
538
|
+
|
539
|
+
M.postJson0 = function (url, data) {
|
540
|
+
M.postJson(
|
541
|
+
M.host + url,
|
542
|
+
function (data) {
|
543
|
+
console.log(data);
|
544
|
+
}, data
|
545
|
+
);
|
546
|
+
}
|
547
|
+
|
548
|
+
M.template = function (str) {
|
549
|
+
return eval("`" + str + "`");
|
550
|
+
}
|
551
|
+
|
552
|
+
|
553
|
+
/**
|
554
|
+
* ----------------------客户端END--------------------------------------------
|
555
|
+
*/
|
556
|
+
|
557
|
+
|
558
|
+
/**
|
559
|
+
* ----------------------数据持久化读写START--------------------------------------------
|
560
|
+
*/
|
561
|
+
|
562
|
+
/**
|
563
|
+
*递归创建文件夹
|
564
|
+
*/
|
565
|
+
M.mkdir = function (dirpath, dirname) {
|
566
|
+
//判断是否是第一次调用
|
567
|
+
if (typeof dirname === "undefined") {
|
568
|
+
|
569
|
+
if (dirpath.indexOf(".") > 0) {
|
570
|
+
dirpath = path.dirname(dirpath);
|
571
|
+
}
|
572
|
+
if (fs.existsSync(dirpath)) {
|
573
|
+
return;
|
574
|
+
} else {
|
575
|
+
M.mkdir(dirpath, path.dirname(dirpath));
|
576
|
+
}
|
577
|
+
} else {
|
578
|
+
//判断第二个参数是否正常,避免调用时传入错误参数
|
579
|
+
if (dirname !== path.dirname(dirpath)) {
|
580
|
+
M.mkdir(dirpath);
|
581
|
+
return;
|
582
|
+
}
|
583
|
+
if (fs.existsSync(dirname)) {
|
584
|
+
fs.mkdirSync(dirpath)
|
585
|
+
} else {
|
586
|
+
M.mkdir(dirname, path.dirname(dirname));
|
587
|
+
fs.mkdirSync(dirpath);
|
588
|
+
}
|
589
|
+
}
|
590
|
+
}
|
591
|
+
/**
|
592
|
+
*文件夹拷贝
|
593
|
+
*/
|
594
|
+
M.copyDir = function (src, dst) {
|
595
|
+
let paths = fs.readdirSync(src); //同步读取当前目录
|
596
|
+
paths.forEach(function (path) {
|
597
|
+
var _src = src + '/' + path;
|
598
|
+
var _dst = dst + '/' + path;
|
599
|
+
fs.stat(_src, function (err, stats) { //stats 该对象 包含文件属性
|
600
|
+
if (err) throw err;
|
601
|
+
if (stats.isFile()) { //如果是个文件则拷贝
|
602
|
+
let readable = fs.createReadStream(_src);//创建读取流
|
603
|
+
let writable = fs.createWriteStream(_dst);//创建写入流
|
604
|
+
readable.pipe(writable);
|
605
|
+
} else if (stats.isDirectory()) { //是目录则 递归
|
606
|
+
privateObj.checkDirectory(_src, _dst, M.copyDir);
|
607
|
+
}
|
608
|
+
});
|
609
|
+
});
|
610
|
+
}
|
611
|
+
|
612
|
+
privateObj.checkDirectory = function (src, dst, callback) {
|
613
|
+
fs.access(dst, fs.constants.F_OK, (err) => {
|
614
|
+
if (err) {
|
615
|
+
fs.mkdirSync(dst);
|
616
|
+
callback(src, dst);
|
617
|
+
} else {
|
618
|
+
callback(src, dst);
|
619
|
+
}
|
620
|
+
});
|
621
|
+
};
|
622
|
+
|
623
|
+
M.readFile = function (file) {
|
624
|
+
if (fs.existsSync(file)) {
|
625
|
+
return fs.readFileSync(file, "utf-8");
|
626
|
+
} else {
|
627
|
+
return;
|
628
|
+
}
|
629
|
+
}
|
630
|
+
M.writeFile = function (file, str) {
|
631
|
+
fs.writeFileSync(file, str);
|
632
|
+
}
|
633
|
+
M.appendFile = function (file, str) {
|
634
|
+
fs.appendFileSync(file, str);
|
635
|
+
}
|
636
|
+
/**
|
637
|
+
文件型数据库第一层封装
|
638
|
+
*/
|
639
|
+
M.getObjByFile = function (file) {
|
640
|
+
data = M.readFile(file) || "[]"
|
641
|
+
var obj = JSON.parse(data.toString());
|
642
|
+
return obj;
|
643
|
+
}
|
644
|
+
M.writeObjToFile = function (file, obj) {
|
645
|
+
M.writeFile(file, JSON.stringify(obj));
|
646
|
+
}
|
647
|
+
|
648
|
+
M.addObjToFile = function (file, obj) {
|
649
|
+
try {
|
650
|
+
var d = M.getObjByFile(file);
|
651
|
+
M.writeObjToFile(file, [...d, obj]);
|
652
|
+
} catch (e) {
|
653
|
+
M.writeObjToFile(file, [obj]);
|
654
|
+
}
|
655
|
+
}
|
656
|
+
M.deleteObjByIdFile = function (file, id) {
|
657
|
+
let ids = [];
|
658
|
+
if (!Array.isArray(id)) {
|
659
|
+
ids.push(id)
|
660
|
+
} else {
|
661
|
+
ids = id;
|
662
|
+
}
|
663
|
+
var d = M.getObjByFile(file);
|
664
|
+
var d1 = M.getObjByFile(file);
|
665
|
+
let d_num = 0;
|
666
|
+
for (let i = 0; i < d1.length; i++) {
|
667
|
+
if (ids.indexOf(d1[i].id) >= 0) {
|
668
|
+
d.splice(i - d_num, 1);
|
669
|
+
d_num++;
|
670
|
+
if (ids.length == 1) break;
|
671
|
+
}
|
672
|
+
}
|
673
|
+
M.writeObjToFile(file, d);
|
674
|
+
}
|
675
|
+
|
676
|
+
M.deleteObjByPropFile = function (file, o) {
|
677
|
+
let o_key = Object.keys(o)[0];
|
678
|
+
let o_val = o[o_key]
|
679
|
+
var d = M.getObjByFile(file);
|
680
|
+
var d1 = M.getObjByFile(file);
|
681
|
+
let d_num = 0;
|
682
|
+
for (let i = 0; i < d1.length; i++) {
|
683
|
+
if (d1[i][o_key] == o_val) {
|
684
|
+
d.splice(i - d_num, 1);
|
685
|
+
d_num++;
|
686
|
+
}
|
687
|
+
}
|
688
|
+
M.writeObjToFile(file, d);
|
689
|
+
return d_num;
|
690
|
+
}
|
691
|
+
|
692
|
+
M.updateObjByIdFile = function (file, obj) {
|
693
|
+
var d = M.getObjByFile(file);
|
694
|
+
for (let i = 0; i < d.length; i++) {
|
695
|
+
if (d[i].id == obj.id) {
|
696
|
+
d.splice(i, 1, Object.assign(d[i],obj));
|
697
|
+
break;
|
698
|
+
}
|
699
|
+
}
|
700
|
+
M.writeObjToFile(file, d);
|
701
|
+
}
|
702
|
+
M.getObjByIdFile = function (file, id) {
|
703
|
+
var d = M.getObjByFile(file);
|
704
|
+
for (let i = 0; i < d.length; i++) {
|
705
|
+
if (d[i].id == id) {
|
706
|
+
return d[i];
|
707
|
+
}
|
708
|
+
}
|
709
|
+
}
|
710
|
+
M.listAllObjByPropFile = function (file, caseObj) {
|
711
|
+
var d = M.getObjByFile(file);
|
712
|
+
let o_keys = Object.keys(caseObj);
|
713
|
+
if (caseObj && o_keys.length>0) {
|
714
|
+
let r_list = [];
|
715
|
+
let o_vals = Object.values(caseObj);
|
716
|
+
for (let i = 0; i < d.length; i++) {
|
717
|
+
let s=0;
|
718
|
+
for (let j=0;j<o_keys.length;j++){
|
719
|
+
if (d[i][o_keys[j]] != o_vals[j]) {
|
720
|
+
break
|
721
|
+
}
|
722
|
+
s++;
|
723
|
+
}
|
724
|
+
if(s==o_keys.length){
|
725
|
+
r_list.push(d[i]);
|
726
|
+
}
|
727
|
+
}
|
728
|
+
return r_list;
|
729
|
+
} else {
|
730
|
+
return d;
|
731
|
+
}
|
732
|
+
}
|
733
|
+
/**
|
734
|
+
* 文件型数据库第二层封装
|
735
|
+
*/
|
736
|
+
M.add = function (obj) {
|
737
|
+
obj.id = M.randomStr();
|
738
|
+
M.addObjToFile(M.database_path, obj);
|
739
|
+
return obj;
|
740
|
+
}
|
741
|
+
M.update = function (obj) {
|
742
|
+
M.updateObjByIdFile(M.database_path, obj);
|
743
|
+
}
|
744
|
+
M.deleteById = function (id) {
|
745
|
+
M.deleteObjByIdFile(M.database_path, id);
|
746
|
+
}
|
747
|
+
M.deleteAll = function (o) {
|
748
|
+
if (o) {
|
749
|
+
M.deleteObjByPropFile(M.database_path, o);
|
750
|
+
} else {
|
751
|
+
M.writeObjToFile(M.database_path, []);
|
752
|
+
}
|
753
|
+
}
|
754
|
+
M.deleteByProp = function (o) {
|
755
|
+
M.deleteObjByPropFile(M.database_path, o);
|
756
|
+
}
|
757
|
+
M.getById = function (id) {
|
758
|
+
return M.getObjByIdFile(M.database_path, id);
|
759
|
+
}
|
760
|
+
M.listAll = function (o) {
|
761
|
+
if (o) {
|
762
|
+
return M.listAllObjByPropFile(M.database_path, o);
|
763
|
+
} else {
|
764
|
+
return M.getObjByFile(M.database_path);
|
765
|
+
}
|
766
|
+
}
|
767
|
+
M.listByProp = function (o) {
|
768
|
+
return M.listAllObjByPropFile(M.database_path, o);
|
769
|
+
}
|
770
|
+
M.listByPage = function (startPage, limit, caseObj) {
|
771
|
+
if (startPage <= 0) startPage = 1;
|
772
|
+
let rows;
|
773
|
+
if (caseObj) {
|
774
|
+
rows = M.listByProp(caseObj);
|
775
|
+
} else {
|
776
|
+
rows = M.listAll();
|
777
|
+
}
|
778
|
+
let total = rows.length;
|
779
|
+
rows = rows.splice((startPage - 1) * limit, limit)
|
780
|
+
return {rows, total}
|
781
|
+
}
|
782
|
+
/**
|
783
|
+
* 全局作用域
|
784
|
+
* @param k
|
785
|
+
* @param v
|
786
|
+
*/
|
787
|
+
M.setAttribute = function (k, v) {
|
788
|
+
let a = {}
|
789
|
+
a[k] = v;
|
790
|
+
a = JSON.stringify(a)
|
791
|
+
a = JSON.parse(a);
|
792
|
+
let preObj;
|
793
|
+
try {
|
794
|
+
preObj = M.getObjByFile(M.map_path);
|
795
|
+
if (Array.isArray(preObj)) preObj = {};
|
796
|
+
} catch (e) {
|
797
|
+
preObj = {};
|
798
|
+
}
|
799
|
+
|
800
|
+
M.writeObjToFile(M.map_path, Object.assign(preObj, a));
|
801
|
+
}
|
802
|
+
|
803
|
+
M.getAttribute = function (k) {
|
804
|
+
return M.getObjByFile(M.map_path)[k];
|
805
|
+
}
|
806
|
+
/**
|
807
|
+
*逐行读取文件
|
808
|
+
*/
|
809
|
+
M.readLine =async function (file, callback) {
|
810
|
+
let lineCount=0;
|
811
|
+
return new Promise((resolve, reject) =>{
|
812
|
+
var remaining = '';
|
813
|
+
var input = fs.createReadStream(file);
|
814
|
+
input.on('data', function (data) {
|
815
|
+
remaining += data;
|
816
|
+
var index = remaining.indexOf('\n');
|
817
|
+
while (index > -1) {
|
818
|
+
var line = remaining.substring(0, index);
|
819
|
+
remaining = remaining.substring(index + 1);
|
820
|
+
lineCount++;
|
821
|
+
callback(line);
|
822
|
+
index = remaining.indexOf('\n');
|
823
|
+
}
|
824
|
+
});
|
825
|
+
input.on('end', function () {
|
826
|
+
if (remaining.length > 0) {
|
827
|
+
resolve(lineCount);
|
828
|
+
callback(remaining);
|
829
|
+
}
|
830
|
+
});
|
831
|
+
} )
|
832
|
+
}
|
833
|
+
|
834
|
+
|
835
|
+
M.readCsvLine =async function (file, callback) {
|
831
836
|
return M.readLine(file, function (line) {
|
832
|
-
|
833
|
-
|
834
|
-
|
835
|
-
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
837
|
+
callback(line.replace("\r", "").split(/(?<!\"[^,]+),(?![^,]+\")/));
|
838
|
+
})
|
839
|
+
}
|
840
|
+
|
841
|
+
M.getFileNameByUrl=function (url){
|
842
|
+
let split= url.split("/");
|
843
|
+
return split[split.length-1]
|
844
|
+
}
|
840
845
|
|
841
846
|
M.getFileList = function (path) {
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
847
|
+
//遍历读取文件
|
848
|
+
function readFile(path, filesList, targetObj) {
|
849
|
+
files = fs.readdirSync(path);//需要用到同步读取
|
850
|
+
files.forEach(walk);
|
851
|
+
|
852
|
+
function walk(file) {
|
853
|
+
states = fs.statSync(path + '/' + file);
|
854
|
+
if (states.isDirectory()) {
|
855
|
+
var item;
|
856
|
+
if (targetObj["children"]) {
|
857
|
+
item = {name: file, children: [], value: path + '/' + file};
|
858
|
+
targetObj["children"].push(item);
|
859
|
+
} else {
|
860
|
+
item = {name: file, children: [], value: path + '/' + file};
|
861
|
+
filesList.push(item);
|
862
|
+
}
|
863
|
+
|
864
|
+
readFile(path + '/' + file, filesList, item);
|
865
|
+
} else {
|
866
|
+
//创建一个对象保存信息
|
867
|
+
var obj = new Object();
|
868
|
+
obj.size = states.size;//文件大小,以字节为单位
|
869
|
+
obj.name = file;//文件名
|
870
|
+
obj.path = path + '/' + file; //文件绝对路径
|
871
|
+
|
872
|
+
if (targetObj["children"]) {
|
873
|
+
var item = {name: file, value: obj.path}
|
874
|
+
targetObj["children"].push(item);
|
875
|
+
} else {
|
876
|
+
var item = {name: file, value: obj.path};
|
877
|
+
filesList.push(item);
|
878
|
+
}
|
879
|
+
}
|
880
|
+
}
|
881
|
+
}
|
882
|
+
|
883
|
+
var filesList = [];
|
884
|
+
var targetObj = {};
|
885
|
+
readFile(path, filesList, targetObj);
|
886
|
+
return filesList;
|
887
|
+
}
|
883
888
|
|
884
889
|
M.getFileDirList = function (path) {
|
885
890
|
//遍历读取文件
|
@@ -937,63 +942,63 @@ M.watchFile=function (watch,callback) {
|
|
937
942
|
|
938
943
|
|
939
944
|
M.log = function (...params) {
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
|
954
|
-
|
955
|
-
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
|
963
|
-
|
964
|
-
|
965
|
-
|
966
|
-
|
967
|
-
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
973
|
-
|
974
|
-
|
975
|
-
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
|
989
|
-
|
990
|
-
|
991
|
-
|
992
|
-
|
993
|
-
|
994
|
-
|
995
|
-
|
996
|
-
|
945
|
+
if (Array.isArray(params[0]) || typeof params[0] == 'object') {
|
946
|
+
params = [JSON.stringify(params[0])]
|
947
|
+
}
|
948
|
+
if (M.log_file_enable || M.log_console_enable) {
|
949
|
+
let r = "";
|
950
|
+
if (M.log_display_time) {
|
951
|
+
r = r + new Date().toLocaleString() + " ";
|
952
|
+
}
|
953
|
+
for (i in params) {
|
954
|
+
r = r + params[i] + " ";
|
955
|
+
}
|
956
|
+
if (M.log_console_enable) console.log(r);
|
957
|
+
r = r + "\n";
|
958
|
+
if (M.log_file_enable) M.appendFile(M.log_path, r);
|
959
|
+
}
|
960
|
+
}
|
961
|
+
|
962
|
+
|
963
|
+
M.getSqlite = function (dbName) {
|
964
|
+
if (M.sqlite) {
|
965
|
+
return M.sqlite;
|
966
|
+
}
|
967
|
+
var SQLite3 = require('sqlite3').verbose();
|
968
|
+
var Db = new SQLite3.Database(dbName || "ming_autotest.db");
|
969
|
+
Db.doSql = function doSql(sql) {
|
970
|
+
var promise = new Promise(function (reslove, reject) {
|
971
|
+
if (Db.display_sql_enable) {
|
972
|
+
M.log(sql)
|
973
|
+
}
|
974
|
+
if (sql.indexOf("select") > -1) {
|
975
|
+
Db.all(sql,
|
976
|
+
function (err, result) {
|
977
|
+
if (err) {
|
978
|
+
M.log(err);
|
979
|
+
reject(err);
|
980
|
+
} else {
|
981
|
+
reslove(result);
|
982
|
+
}
|
983
|
+
});
|
984
|
+
} else {
|
985
|
+
Db.run(sql,
|
986
|
+
function (err) {
|
987
|
+
if (err) {
|
988
|
+
// M.log(err);
|
989
|
+
reject(err);
|
990
|
+
}
|
991
|
+
reslove(null);
|
992
|
+
});
|
993
|
+
}
|
994
|
+
})
|
995
|
+
return promise;
|
996
|
+
}
|
997
|
+
M.sqlite = Db;
|
998
|
+
return Db;
|
999
|
+
}
|
1000
|
+
|
1001
|
+
///////////////////////////////
|
997
1002
|
|
998
1003
|
|
999
1004
|
M.getMySql = function (dbConfig) {
|
@@ -1097,262 +1102,262 @@ M.getMySql = function (dbConfig) {
|
|
1097
1102
|
|
1098
1103
|
|
1099
1104
|
M.getMongoDB = function (dbConfig) {
|
1100
|
-
|
1101
|
-
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
|
1120
|
-
|
1121
|
-
|
1122
|
-
|
1123
|
-
|
1124
|
-
|
1125
|
-
|
1126
|
-
|
1127
|
-
|
1128
|
-
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
|
1146
|
-
|
1147
|
-
|
1148
|
-
|
1149
|
-
|
1150
|
-
|
1151
|
-
|
1152
|
-
|
1153
|
-
|
1154
|
-
|
1155
|
-
|
1156
|
-
|
1157
|
-
|
1158
|
-
|
1159
|
-
|
1160
|
-
|
1161
|
-
|
1162
|
-
|
1163
|
-
|
1164
|
-
|
1165
|
-
|
1166
|
-
|
1167
|
-
|
1168
|
-
|
1169
|
-
|
1170
|
-
|
1171
|
-
|
1172
|
-
|
1173
|
-
|
1174
|
-
|
1175
|
-
|
1176
|
-
|
1177
|
-
|
1178
|
-
|
1179
|
-
|
1180
|
-
|
1181
|
-
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
|
1207
|
-
|
1208
|
-
|
1209
|
-
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1230
|
-
|
1231
|
-
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1257
|
-
|
1258
|
-
|
1259
|
-
|
1260
|
-
|
1261
|
-
|
1262
|
-
|
1263
|
-
|
1264
|
-
|
1265
|
-
|
1266
|
-
|
1267
|
-
|
1268
|
-
|
1269
|
-
|
1270
|
-
|
1271
|
-
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1282
|
-
|
1283
|
-
|
1284
|
-
|
1285
|
-
|
1286
|
-
|
1287
|
-
|
1288
|
-
|
1289
|
-
|
1290
|
-
|
1291
|
-
|
1292
|
-
|
1293
|
-
|
1294
|
-
|
1295
|
-
|
1296
|
-
|
1297
|
-
|
1298
|
-
|
1299
|
-
|
1300
|
-
|
1301
|
-
|
1302
|
-
|
1303
|
-
|
1304
|
-
|
1305
|
-
|
1306
|
-
|
1307
|
-
|
1308
|
-
|
1309
|
-
|
1310
|
-
|
1311
|
-
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
|
1322
|
-
|
1323
|
-
|
1324
|
-
|
1325
|
-
|
1326
|
-
|
1327
|
-
|
1328
|
-
|
1329
|
-
|
1330
|
-
|
1331
|
-
|
1332
|
-
|
1333
|
-
|
1334
|
-
|
1335
|
-
|
1336
|
-
|
1337
|
-
|
1338
|
-
|
1339
|
-
|
1340
|
-
|
1341
|
-
|
1342
|
-
|
1343
|
-
|
1344
|
-
|
1345
|
-
|
1346
|
-
|
1347
|
-
|
1348
|
-
|
1349
|
-
|
1350
|
-
|
1351
|
-
|
1352
|
-
|
1353
|
-
|
1354
|
-
|
1355
|
-
|
1105
|
+
if (M.mongoDb) {
|
1106
|
+
return M.mongoDb;
|
1107
|
+
}
|
1108
|
+
var MongoDB=require('mongodb');
|
1109
|
+
var MongoClient =MongoDB.MongoClient;
|
1110
|
+
const ObjectID = MongoDB.ObjectID;
|
1111
|
+
|
1112
|
+
var Config={
|
1113
|
+
dbUrl: dbConfig.dbUrl|| 'mongodb://localhost:27017/',
|
1114
|
+
dbName: dbConfig.dbName|| 'miapi'
|
1115
|
+
};
|
1116
|
+
|
1117
|
+
class MingMongoClient{
|
1118
|
+
static connect(){
|
1119
|
+
return new Promise((resolve,reject)=>{
|
1120
|
+
if(!MingMongoClient.dbClient){
|
1121
|
+
console.log("connect mongodb", Config)
|
1122
|
+
MongoClient.connect(Config.dbUrl,(err,client)=>{
|
1123
|
+
if(err){
|
1124
|
+
reject(err)
|
1125
|
+
}else{
|
1126
|
+
MingMongoClient.dbClient=client.db(Config.dbName);
|
1127
|
+
resolve(MingMongoClient.dbClient)
|
1128
|
+
}
|
1129
|
+
})
|
1130
|
+
}else{
|
1131
|
+
resolve(MingMongoClient.dbClient);
|
1132
|
+
}
|
1133
|
+
})
|
1134
|
+
}
|
1135
|
+
|
1136
|
+
static find(collectionName,json){
|
1137
|
+
if(!json){
|
1138
|
+
json=collectionName
|
1139
|
+
collectionName= MingMongoClient.collectionName;
|
1140
|
+
}
|
1141
|
+
return new Promise((resolve,reject)=>{
|
1142
|
+
MingMongoClient.connect().then((db)=>{
|
1143
|
+
var result=db.collection(collectionName).find(json);
|
1144
|
+
result.toArray(function(err,docs){
|
1145
|
+
if(err){
|
1146
|
+
reject(err);
|
1147
|
+
return;
|
1148
|
+
}
|
1149
|
+
resolve(docs);
|
1150
|
+
})
|
1151
|
+
|
1152
|
+
})
|
1153
|
+
})
|
1154
|
+
}
|
1155
|
+
static update(collectionName,whereObj,updateObj){
|
1156
|
+
if(!updateObj){
|
1157
|
+
updateObj=whereObj;
|
1158
|
+
whereObj=collectionName
|
1159
|
+
collectionName= MingMongoClient.collectionName;
|
1160
|
+
}
|
1161
|
+
return new Promise((resolve,reject)=>{
|
1162
|
+
MingMongoClient.connect().then((db)=>{
|
1163
|
+
db.collection(collectionName).updateOne(whereObj,{
|
1164
|
+
$set:updateObj
|
1165
|
+
},(err,result)=>{
|
1166
|
+
if(err){
|
1167
|
+
reject(err);
|
1168
|
+
}else{
|
1169
|
+
resolve(result);
|
1170
|
+
}
|
1171
|
+
})
|
1172
|
+
})
|
1173
|
+
})
|
1174
|
+
}
|
1175
|
+
static insert(collectionName,json){
|
1176
|
+
if(!json){
|
1177
|
+
json=collectionName
|
1178
|
+
collectionName= MingMongoClient.collectionName;
|
1179
|
+
}
|
1180
|
+
return new Promise((resolve,reject)=>{
|
1181
|
+
MingMongoClient.connect().then((db)=>{
|
1182
|
+
db.collection(collectionName).insertOne(json,function(err,result){
|
1183
|
+
if(err){
|
1184
|
+
reject(err);
|
1185
|
+
}else{
|
1186
|
+
|
1187
|
+
resolve(result);
|
1188
|
+
}
|
1189
|
+
})
|
1190
|
+
})
|
1191
|
+
})
|
1192
|
+
}
|
1193
|
+
|
1194
|
+
static insertMany(collectionName,json){
|
1195
|
+
if(!json){
|
1196
|
+
json=collectionName
|
1197
|
+
collectionName= MingMongoClient.collectionName;
|
1198
|
+
}
|
1199
|
+
return new Promise((resolve,reject)=>{
|
1200
|
+
MingMongoClient.connect().then((db)=>{
|
1201
|
+
db.collection(collectionName).insertMany(json,function(err,result){
|
1202
|
+
if(err){
|
1203
|
+
reject(err);
|
1204
|
+
}else{
|
1205
|
+
|
1206
|
+
resolve(result);
|
1207
|
+
}
|
1208
|
+
})
|
1209
|
+
})
|
1210
|
+
})
|
1211
|
+
}
|
1212
|
+
|
1213
|
+
static remove(collectionName,json){
|
1214
|
+
if(!json){
|
1215
|
+
json=collectionName
|
1216
|
+
collectionName= MingMongoClient.collectionName;
|
1217
|
+
}
|
1218
|
+
return new Promise((resolve,reject)=>{
|
1219
|
+
MingMongoClient.connect().then((db)=>{
|
1220
|
+
db.collection(collectionName).removeOne(json,function(err,result){
|
1221
|
+
if(err){
|
1222
|
+
reject(err);
|
1223
|
+
}else{
|
1224
|
+
|
1225
|
+
resolve(result);
|
1226
|
+
}
|
1227
|
+
})
|
1228
|
+
})
|
1229
|
+
})
|
1230
|
+
}
|
1231
|
+
|
1232
|
+
static getById(collectionName,id){
|
1233
|
+
if(!id){
|
1234
|
+
id=collectionName
|
1235
|
+
collectionName= MingMongoClient.collectionName;
|
1236
|
+
}
|
1237
|
+
return new Promise((resolve,reject)=>{
|
1238
|
+
MingMongoClient.connect().then((db)=>{
|
1239
|
+
var whereArgs = {
|
1240
|
+
_id: new ObjectID(id)
|
1241
|
+
};
|
1242
|
+
db.collection(collectionName).findOne(whereArgs,{},function(err,result){
|
1243
|
+
if(err){
|
1244
|
+
reject(err);
|
1245
|
+
}else{
|
1246
|
+
resolve(result);
|
1247
|
+
}
|
1248
|
+
})
|
1249
|
+
})
|
1250
|
+
})
|
1251
|
+
}
|
1252
|
+
|
1253
|
+
}
|
1254
|
+
MingMongoClient.ObjectID=(id)=> new ObjectID(id)
|
1255
|
+
let Db=MingMongoClient;
|
1256
|
+
Db.dbConfig=Config;
|
1257
|
+
MingMongoClient.collectionName="test"
|
1258
|
+
M.mongoDb=Db;
|
1259
|
+
return Db;
|
1260
|
+
}
|
1261
|
+
|
1262
|
+
|
1263
|
+
|
1264
|
+
/**
|
1265
|
+
* ----------------------Sql CRUD START-------------------------------------------
|
1266
|
+
*/
|
1267
|
+
M.getInsertObjSql = function (tableName, obj) {
|
1268
|
+
var fields = "(";
|
1269
|
+
var values = "(";
|
1270
|
+
for (let field in obj) {
|
1271
|
+
fields += field + ",";
|
1272
|
+
values += `'${obj[field]}'` + ",";
|
1273
|
+
}
|
1274
|
+
fields = fields.substr(0, fields.lastIndexOf(","));
|
1275
|
+
values = values.substr(0, values.lastIndexOf(","));
|
1276
|
+
fields += ")";
|
1277
|
+
values += ")";
|
1278
|
+
let sql = "insert into " + tableName + fields + " values " + values;
|
1279
|
+
return sql;
|
1280
|
+
}
|
1281
|
+
|
1282
|
+
M.getDeleteObjSql = function (tableName, obj) {
|
1283
|
+
var fields = [];
|
1284
|
+
for (let field in obj) {
|
1285
|
+
fields.push(field);
|
1286
|
+
}
|
1287
|
+
let sql = `delete from ${tableName} where ${fields.map(u => u + "='" + obj[u] + "'")}`;
|
1288
|
+
sql = sql.replace(/,/g, " and ")
|
1289
|
+
return sql;
|
1290
|
+
}
|
1291
|
+
|
1292
|
+
M.getUpdateObjSql = function (tableName, obj, caseObj) {
|
1293
|
+
var fields = [];
|
1294
|
+
for (let field in obj) {
|
1295
|
+
if (field != "id")
|
1296
|
+
fields.push(field);
|
1297
|
+
}
|
1298
|
+
let sql = "";
|
1299
|
+
if (!caseObj) {
|
1300
|
+
sql = `update ${tableName} set ${fields.map(u => u + "='" + obj[u] + "'")} where id=${obj.id}`;
|
1301
|
+
} else {
|
1302
|
+
var caseObjfields = [];
|
1303
|
+
for (let caseObjfield in caseObj) {
|
1304
|
+
caseObjfields.push(caseObjfield)
|
1305
|
+
}
|
1306
|
+
sql = `update ${tableName} set ${fields.map(u => u + "='" + obj[u] + "'")} where ${caseObjfields.map(u => u + "='" + caseObj[u] + "'").join(" and ")}`;
|
1307
|
+
}
|
1308
|
+
|
1309
|
+
return sql;
|
1310
|
+
}
|
1311
|
+
|
1312
|
+
|
1313
|
+
M.getSelectObjSql = function (tableName, obj) {
|
1314
|
+
var fields = [];
|
1315
|
+
for (let field in obj) {
|
1316
|
+
fields.push(field);
|
1317
|
+
}
|
1318
|
+
let sql = `select * from ${tableName} where ${fields.map(u => u + "='" + obj[u] + "'")}`;
|
1319
|
+
sql = sql.replace(/,/g, " and ")
|
1320
|
+
return sql;
|
1321
|
+
}
|
1322
|
+
|
1323
|
+
/**
|
1324
|
+
* ----------------------Sql CRUD START-------------------------------------------
|
1325
|
+
*/
|
1326
|
+
|
1327
|
+
|
1328
|
+
/**
|
1329
|
+
* ----------------------数据持久化读写END--------------------------------------------
|
1330
|
+
*/
|
1331
|
+
|
1332
|
+
|
1333
|
+
/**
|
1334
|
+
* ----------------------服务器端START--------------------------------------------
|
1335
|
+
*/
|
1336
|
+
/**
|
1337
|
+
*封装返回数据
|
1338
|
+
*/
|
1339
|
+
M.result = function (data, success,message) {
|
1340
|
+
var r = {};
|
1341
|
+
if (success == false) {
|
1342
|
+
r.code = -2;
|
1343
|
+
r.msg = message||"操作失败";
|
1344
|
+
} else {
|
1345
|
+
r.code = 0;
|
1346
|
+
r.msg = message||"success"
|
1347
|
+
}
|
1348
|
+
r.requestId=M.req? M.req.requestId:"";
|
1349
|
+
try {
|
1350
|
+
var obj = JSON.parse(data);
|
1351
|
+
if (typeof obj == 'object' && obj) {
|
1352
|
+
r.data = obj;
|
1353
|
+
} else {
|
1354
|
+
r.data = data;
|
1355
|
+
}
|
1356
|
+
} catch (e) {
|
1357
|
+
r.data = data;
|
1358
|
+
}
|
1359
|
+
return JSON.stringify(r);
|
1360
|
+
}
|
1356
1361
|
|
1357
1362
|
M.successResult=(d,msg)=>{
|
1358
1363
|
let r=d;
|
@@ -1372,30 +1377,30 @@ M.failResult=(msg,code,d)=>{
|
|
1372
1377
|
}
|
1373
1378
|
|
1374
1379
|
/**
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1378
|
-
|
1379
|
-
|
1380
|
-
|
1381
|
-
|
1382
|
-
|
1383
|
-
|
1384
|
-
|
1385
|
-
|
1386
|
-
|
1387
|
-
|
1388
|
-
|
1389
|
-
|
1390
|
-
|
1391
|
-
|
1392
|
-
|
1393
|
-
|
1394
|
-
|
1395
|
-
|
1396
|
-
|
1397
|
-
|
1398
|
-
|
1380
|
+
*获取下划线式的对象
|
1381
|
+
*/
|
1382
|
+
M.getUnderlineObj = function (obj) {
|
1383
|
+
var result = {};
|
1384
|
+
for (let field in obj) {
|
1385
|
+
result[field.humpToUnderline()] = obj[field]
|
1386
|
+
}
|
1387
|
+
return result;
|
1388
|
+
}
|
1389
|
+
|
1390
|
+
/**
|
1391
|
+
*获取驼峰式的对象
|
1392
|
+
*/
|
1393
|
+
M.getHumpObj = function (obj) {
|
1394
|
+
var result = {};
|
1395
|
+
for (let field in obj) {
|
1396
|
+
result[field.underlineToHump()] = obj[field]
|
1397
|
+
}
|
1398
|
+
return result;
|
1399
|
+
}
|
1400
|
+
|
1401
|
+
M.randomStr = function () {
|
1402
|
+
return (Math.random().toString(36) + new Date().getTime()).slice(2);
|
1403
|
+
}
|
1399
1404
|
|
1400
1405
|
M.urlStringify = function (obj) {
|
1401
1406
|
if (obj !== null && typeof obj === 'object') {
|
@@ -1430,459 +1435,461 @@ M.urlParse = function (url) {
|
|
1430
1435
|
}
|
1431
1436
|
return s
|
1432
1437
|
};
|
1433
|
-
|
1434
|
-
|
1435
|
-
|
1436
|
-
|
1437
|
-
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
|
1443
|
-
|
1444
|
-
|
1445
|
-
|
1446
|
-
|
1447
|
-
|
1448
|
-
|
1449
|
-
|
1450
|
-
|
1451
|
-
|
1452
|
-
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1457
|
-
|
1458
|
-
|
1459
|
-
|
1460
|
-
|
1461
|
-
|
1462
|
-
|
1463
|
-
|
1464
|
-
|
1465
|
-
|
1466
|
-
|
1467
|
-
|
1468
|
-
|
1469
|
-
|
1470
|
-
|
1471
|
-
|
1472
|
-
|
1473
|
-
|
1474
|
-
|
1475
|
-
|
1476
|
-
|
1477
|
-
|
1478
|
-
|
1479
|
-
|
1480
|
-
|
1481
|
-
|
1482
|
-
|
1483
|
-
|
1484
|
-
|
1485
|
-
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
|
1510
|
-
|
1511
|
-
|
1512
|
-
|
1513
|
-
|
1514
|
-
|
1515
|
-
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
|
1520
|
-
|
1521
|
-
|
1522
|
-
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
|
1530
|
-
|
1531
|
-
|
1532
|
-
|
1533
|
-
|
1534
|
-
|
1535
|
-
|
1536
|
-
|
1537
|
-
|
1538
|
-
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
|
1550
|
-
|
1551
|
-
|
1552
|
-
|
1553
|
-
|
1554
|
-
|
1555
|
-
|
1556
|
-
|
1557
|
-
|
1558
|
-
|
1559
|
-
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1565
|
-
|
1566
|
-
|
1567
|
-
|
1568
|
-
|
1569
|
-
|
1570
|
-
|
1571
|
-
|
1572
|
-
|
1573
|
-
|
1574
|
-
|
1575
|
-
|
1576
|
-
|
1577
|
-
|
1578
|
-
|
1579
|
-
|
1580
|
-
|
1581
|
-
|
1582
|
-
|
1583
|
-
|
1584
|
-
|
1585
|
-
|
1586
|
-
|
1587
|
-
|
1588
|
-
|
1589
|
-
|
1590
|
-
|
1591
|
-
|
1592
|
-
|
1593
|
-
|
1594
|
-
|
1595
|
-
|
1596
|
-
|
1597
|
-
|
1598
|
-
|
1438
|
+
|
1439
|
+
/**
|
1440
|
+
* 异常处理钩子
|
1441
|
+
* @param e
|
1442
|
+
*/
|
1443
|
+
M.err = function (e) {
|
1444
|
+
if (e) {
|
1445
|
+
console.log(e.message);
|
1446
|
+
return false;
|
1447
|
+
}
|
1448
|
+
return true;
|
1449
|
+
}
|
1450
|
+
|
1451
|
+
|
1452
|
+
|
1453
|
+
M.server = function () {
|
1454
|
+
var G = this; /*全局变量,也就是M*/
|
1455
|
+
//静态资源路径
|
1456
|
+
this._views = "static";
|
1457
|
+
//key为去除rest参数的url,val为原始url
|
1458
|
+
this._rest = {};
|
1459
|
+
//通配符
|
1460
|
+
this._use = {};
|
1461
|
+
//处理get和post请求
|
1462
|
+
this._get = {};
|
1463
|
+
this._post = {};
|
1464
|
+
this._put = {};
|
1465
|
+
this._delete = {};
|
1466
|
+
this._mapping = {};
|
1467
|
+
//用于模拟过滤器
|
1468
|
+
this._begin = function () {
|
1469
|
+
}
|
1470
|
+
//服务器响应后的钩子函数
|
1471
|
+
this._end = function () {
|
1472
|
+
}
|
1473
|
+
//如果实现此函数,则只能有一个此服务
|
1474
|
+
this._server = function () {
|
1475
|
+
};
|
1476
|
+
var app =async function (req, res) {
|
1477
|
+
try {
|
1478
|
+
M.req=req;
|
1479
|
+
M.res=res;
|
1480
|
+
//是否已经发送过了
|
1481
|
+
res.alreadySend = false;
|
1482
|
+
req.requestId=M.randomStr();
|
1483
|
+
//是否为静态资源请求
|
1484
|
+
req.isStaticRequest = function () {
|
1485
|
+
if (req.url.indexOf("?") > 0) {
|
1486
|
+
return privateObj.staticMime[path.extname(req.url.substr(0, req.url.indexOf("?")))];
|
1487
|
+
} else {
|
1488
|
+
return privateObj.staticMime[path.extname(req.url)];
|
1489
|
+
}
|
1490
|
+
}
|
1491
|
+
//是否为rest请求
|
1492
|
+
req.isRestRequest = function () {
|
1493
|
+
if (Object.keys(G._rest).length == 0) return false;
|
1494
|
+
var isRest = false;
|
1495
|
+
for (let i = 0; i < Object.keys(G._rest).length; i++) {
|
1496
|
+
if (pathname.startsWith(Object.keys(G._rest)[i])) {
|
1497
|
+
isRest = true;
|
1498
|
+
break;
|
1499
|
+
}
|
1500
|
+
}
|
1501
|
+
return isRest;
|
1502
|
+
}
|
1503
|
+
|
1504
|
+
req.ip = req.headers['x-forwarded-for'] ||
|
1505
|
+
req.connection.remoteAddress ||
|
1506
|
+
req.socket.remoteAddress ||
|
1507
|
+
req.connection.socket.remoteAddress;
|
1508
|
+
//请求cookies封装
|
1509
|
+
req.cookies = querystring.parse(req.headers['cookie'], "; ");
|
1510
|
+
//设置浏览器cookies
|
1511
|
+
res.cookie = function (key, value, cfg) {
|
1512
|
+
let o = {}
|
1513
|
+
o[key] = value;
|
1514
|
+
let r_cookie = Object.assign(o, cfg)
|
1515
|
+
let setCookies= querystring.stringify(r_cookie, " ;");
|
1516
|
+
setCookies= setCookies.replace(/%2F/g,'/');
|
1517
|
+
res.setHeader("Set-Cookie", setCookies);
|
1518
|
+
}
|
1519
|
+
if (true) {
|
1520
|
+
Object.defineProperty(req, 'session', {
|
1521
|
+
set: function (o) {
|
1522
|
+
let sessionValue = req.cookies.sessionid || M.randomStr();
|
1523
|
+
res.cookie("sessionid", sessionValue,{Path:"/"})
|
1524
|
+
M.sessions[sessionValue] = o;
|
1525
|
+
},
|
1526
|
+
get: function () {
|
1527
|
+
return M.sessions[req.cookies.sessionid]
|
1528
|
+
},
|
1529
|
+
configurable: true
|
1530
|
+
})
|
1531
|
+
}
|
1532
|
+
//扩充res一个send方法
|
1533
|
+
res.send = function (data) {
|
1534
|
+
res.alreadySend = true;
|
1535
|
+
let isString = "[object String]" === Object.prototype.toString.call(data)
|
1536
|
+
if (!isString) {
|
1537
|
+
data = JSON.stringify(data);
|
1538
|
+
}
|
1539
|
+
let requestOrigin = "*";
|
1540
|
+
if (req.headers["origin"]) {
|
1541
|
+
requestOrigin = req.headers["origin"];
|
1542
|
+
}
|
1543
|
+
let requestHeaders = "X-Requested-With";
|
1544
|
+
if (req.headers['access-control-request-headers']) {
|
1545
|
+
requestHeaders = req.headers['access-control-request-headers'];
|
1546
|
+
}
|
1547
|
+
res.setHeader("Access-Control-Allow-Origin", requestOrigin);
|
1548
|
+
res.setHeader("Access-Control-Allow-Headers", requestHeaders);
|
1549
|
+
res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
|
1550
|
+
res.setHeader("Access-Control-Allow-Credentials", "true");
|
1551
|
+
res.setHeader("X-Powered-By", ' 3.2.1')
|
1552
|
+
res.setHeader("Content-Type", "application/json;charset=utf-8");
|
1553
|
+
res.end(data);
|
1554
|
+
G._end(req,data);
|
1555
|
+
}
|
1556
|
+
//扩充res一个renderByUrl方法
|
1557
|
+
res.renderUrl = async function (url) {
|
1558
|
+
res.alreadySend = true;
|
1559
|
+
let text="";
|
1560
|
+
if(!url.startsWith("http")&&!url.startsWith("file")){
|
1561
|
+
if(!url.startsWith("/")){
|
1562
|
+
url="/"+url;
|
1563
|
+
}
|
1564
|
+
url=G["_views"]+url;
|
1565
|
+
text = M.readFile(url);
|
1566
|
+
}else {
|
1567
|
+
text = await M.getRemoteCacheByUrl(url)
|
1568
|
+
}
|
1569
|
+
let isString = "[object String]" === Object.prototype.toString.call(text)
|
1570
|
+
if (!isString) {
|
1571
|
+
text = JSON.stringify(text);
|
1572
|
+
}
|
1573
|
+
var pathname = url_module.parse(url).pathname; /*获取url的值*/
|
1574
|
+
//获取文件的后缀名
|
1575
|
+
var extname = path.extname(pathname);
|
1576
|
+
res.writeHead(200, {"Content-Type": "" + (privateObj.staticMime[extname] || 'text/html') + ";charset='utf-8'",});
|
1577
|
+
res.write(text);
|
1578
|
+
res.end();
|
1579
|
+
G._end(req,text);
|
1580
|
+
}
|
1581
|
+
|
1582
|
+
res.render = async function (url) {
|
1583
|
+
res.alreadySend = true;
|
1584
|
+
let text="";
|
1585
|
+
if(!url.startsWith("http")&&!url.startsWith("file")){
|
1586
|
+
if(!url.startsWith("/")){
|
1587
|
+
url="/"+url;
|
1588
|
+
}
|
1589
|
+
url=G["_views"]+url;
|
1590
|
+
text = M.readFile(url);
|
1591
|
+
}else {
|
1592
|
+
text = await M.getRemoteCacheByUrl(url)
|
1593
|
+
}
|
1594
|
+
let isString = "[object String]" === Object.prototype.toString.call(text)
|
1595
|
+
if (!isString) {
|
1596
|
+
text = JSON.stringify(text);
|
1597
|
+
}
|
1598
|
+
var pathname = url_module.parse(url).pathname; /*获取url的值*/
|
1599
|
+
//获取文件的后缀名
|
1600
|
+
var extname = path.extname(pathname);
|
1601
|
+
res.writeHead(200, {"Content-Type": "" + (privateObj.staticMime[extname] || 'text/html') + ";charset='utf-8'",});
|
1602
|
+
let templateStr=""
|
1603
|
+
try {
|
1604
|
+
templateStr= M.template(text)
|
1605
|
+
}catch (e){
|
1599
1606
|
M["_render_exception_handle"](e,req,res);
|
1600
|
-
|
1601
|
-
|
1602
|
-
|
1603
|
-
|
1604
|
-
|
1605
|
-
|
1606
|
-
|
1607
|
-
|
1608
|
-
|
1609
|
-
|
1610
|
-
|
1611
|
-
|
1612
|
-
|
1613
|
-
|
1614
|
-
|
1615
|
-
|
1616
|
-
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
|
1623
|
-
|
1624
|
-
|
1625
|
-
|
1626
|
-
|
1627
|
-
|
1628
|
-
|
1629
|
-
|
1630
|
-
|
1631
|
-
|
1632
|
-
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
|
1638
|
-
|
1639
|
-
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1643
|
-
|
1644
|
-
|
1645
|
-
|
1646
|
-
|
1647
|
-
|
1648
|
-
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1654
|
-
|
1655
|
-
|
1656
|
-
|
1657
|
-
|
1658
|
-
|
1659
|
-
|
1660
|
-
|
1661
|
-
|
1662
|
-
|
1663
|
-
|
1664
|
-
|
1665
|
-
|
1666
|
-
|
1667
|
-
|
1668
|
-
|
1669
|
-
|
1670
|
-
|
1671
|
-
|
1672
|
-
|
1673
|
-
|
1674
|
-
|
1675
|
-
|
1676
|
-
|
1677
|
-
|
1678
|
-
|
1679
|
-
|
1680
|
-
|
1681
|
-
|
1682
|
-
|
1683
|
-
|
1684
|
-
|
1685
|
-
|
1686
|
-
|
1687
|
-
|
1688
|
-
|
1689
|
-
|
1690
|
-
|
1691
|
-
|
1692
|
-
|
1693
|
-
|
1694
|
-
|
1695
|
-
|
1696
|
-
|
1697
|
-
|
1698
|
-
|
1699
|
-
|
1700
|
-
|
1701
|
-
|
1702
|
-
|
1703
|
-
|
1704
|
-
|
1705
|
-
|
1706
|
-
|
1707
|
-
|
1708
|
-
|
1709
|
-
|
1710
|
-
|
1711
|
-
|
1712
|
-
|
1713
|
-
|
1714
|
-
|
1715
|
-
|
1716
|
-
|
1717
|
-
|
1718
|
-
|
1719
|
-
|
1720
|
-
|
1721
|
-
|
1722
|
-
|
1723
|
-
|
1724
|
-
|
1725
|
-
|
1726
|
-
|
1727
|
-
|
1728
|
-
|
1729
|
-
|
1730
|
-
|
1731
|
-
|
1732
|
-
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
1741
|
-
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1755
|
-
|
1756
|
-
|
1757
|
-
|
1758
|
-
|
1759
|
-
|
1760
|
-
|
1761
|
-
|
1762
|
-
|
1763
|
-
|
1764
|
-
|
1765
|
-
|
1766
|
-
|
1767
|
-
|
1768
|
-
|
1769
|
-
|
1770
|
-
|
1771
|
-
|
1772
|
-
|
1773
|
-
|
1774
|
-
|
1775
|
-
|
1776
|
-
|
1777
|
-
|
1778
|
-
|
1779
|
-
|
1780
|
-
|
1781
|
-
|
1782
|
-
|
1783
|
-
|
1784
|
-
|
1785
|
-
|
1786
|
-
|
1787
|
-
|
1788
|
-
|
1789
|
-
|
1790
|
-
|
1791
|
-
|
1792
|
-
|
1793
|
-
|
1794
|
-
|
1795
|
-
|
1796
|
-
|
1797
|
-
|
1798
|
-
|
1799
|
-
|
1800
|
-
|
1801
|
-
|
1802
|
-
|
1803
|
-
|
1804
|
-
|
1805
|
-
|
1806
|
-
|
1807
|
-
|
1808
|
-
|
1809
|
-
|
1810
|
-
|
1811
|
-
|
1812
|
-
|
1813
|
-
|
1814
|
-
|
1815
|
-
|
1816
|
-
|
1817
|
-
|
1818
|
-
|
1819
|
-
|
1820
|
-
|
1821
|
-
|
1822
|
-
|
1823
|
-
|
1824
|
-
|
1825
|
-
|
1826
|
-
|
1827
|
-
|
1828
|
-
|
1829
|
-
|
1830
|
-
|
1831
|
-
|
1832
|
-
|
1833
|
-
|
1834
|
-
|
1835
|
-
|
1836
|
-
|
1837
|
-
|
1838
|
-
|
1839
|
-
|
1840
|
-
|
1841
|
-
|
1842
|
-
|
1843
|
-
|
1844
|
-
|
1845
|
-
|
1846
|
-
|
1847
|
-
|
1848
|
-
|
1849
|
-
|
1850
|
-
|
1851
|
-
|
1852
|
-
|
1853
|
-
|
1854
|
-
|
1855
|
-
|
1856
|
-
|
1857
|
-
|
1858
|
-
|
1859
|
-
|
1860
|
-
|
1861
|
-
|
1862
|
-
|
1863
|
-
|
1864
|
-
|
1865
|
-
|
1866
|
-
|
1867
|
-
|
1607
|
+
}
|
1608
|
+
if(templateStr){
|
1609
|
+
text=templateStr;
|
1610
|
+
}
|
1611
|
+
res.write(text);
|
1612
|
+
res.end();
|
1613
|
+
G._end(req,text);
|
1614
|
+
}
|
1615
|
+
//扩充res一个renderJs方法
|
1616
|
+
res.renderJs = function (text) {
|
1617
|
+
res.alreadySend = true;
|
1618
|
+
res.writeHead(200, {"Content-Type": "application/javascript"});
|
1619
|
+
res.write(text);
|
1620
|
+
res.end();
|
1621
|
+
G._end(req,text);
|
1622
|
+
}
|
1623
|
+
//扩充res一个renderHtml方法
|
1624
|
+
res.renderHtml = function (text) {
|
1625
|
+
res.alreadySend = true;
|
1626
|
+
res.writeHead(200, {"Content-Type": "text/html;charset='utf-8'"});
|
1627
|
+
res.write(text);
|
1628
|
+
res.end();
|
1629
|
+
G._end(req,text);
|
1630
|
+
}
|
1631
|
+
res.redirect = function (url) {
|
1632
|
+
res.alreadySend = true;
|
1633
|
+
res.writeHead(302, {'Content-Type': 'text/html; charset=utf-8', 'Location': url});
|
1634
|
+
res.end();
|
1635
|
+
}
|
1636
|
+
//获取路由
|
1637
|
+
var pathname = url_module.parse(req.url).pathname;
|
1638
|
+
if (!pathname.endsWith('/')) {
|
1639
|
+
pathname = pathname + '/';
|
1640
|
+
}
|
1641
|
+
// pathname.startsWith("/usr/")
|
1642
|
+
//获取请求的方式 get post
|
1643
|
+
var method = req.method.toLowerCase();
|
1644
|
+
if (req.isStaticRequest()) {
|
1645
|
+
|
1646
|
+
await G._begin(req, res);
|
1647
|
+
if (!res.alreadySend) await privateObj.dealUseServer(req, res);
|
1648
|
+
if (!res.alreadySend) await privateObj.staticServer(req, res, G["_views"]);
|
1649
|
+
} else {
|
1650
|
+
|
1651
|
+
//为req加个params用于存放请求参数
|
1652
|
+
req.params = {};
|
1653
|
+
var mapingPath = "";
|
1654
|
+
//如果是rest风格的请求,为其封装请求参数
|
1655
|
+
if (req.isRestRequest()) {
|
1656
|
+
for (let i = 0; i < Object.keys(G._rest).length; i++) {
|
1657
|
+
if (pathname.startsWith(Object.keys(G._rest)[i])) {
|
1658
|
+
pathname = Object.keys(G._rest)[i];
|
1659
|
+
mapingPath = G._rest[pathname];
|
1660
|
+
}
|
1661
|
+
}
|
1662
|
+
var realPathName = url_module.parse(req.url).pathname;
|
1663
|
+
if (!realPathName.endsWith('/')) {
|
1664
|
+
realPathName = realPathName + '/';
|
1665
|
+
}
|
1666
|
+
let s1 = realPathName;
|
1667
|
+
let s2 = mapingPath;
|
1668
|
+
s1 = s1.substring(s2.indexOf(":") - 1, s1.length - 1).split("/").slice(1)
|
1669
|
+
s2 = s2.substring(s2.indexOf(":") - 1, s2.length - 1).split("/:").slice(1)
|
1670
|
+
for (let i = 0; i < s2.length; i++) {
|
1671
|
+
req.params[s2[i]] = s1[i];
|
1672
|
+
}
|
1673
|
+
}
|
1674
|
+
/**
|
1675
|
+
* 加queryParam参数
|
1676
|
+
*/
|
1677
|
+
req.params = Object.assign(req.params, url_module.parse(req.url, true).query);
|
1678
|
+
|
1679
|
+
if ((method == "get" ||
|
1680
|
+
method == "post" ||
|
1681
|
+
method == "put" ||
|
1682
|
+
method == "delete"
|
1683
|
+
) && (G['_' + method][pathname])) {
|
1684
|
+
if (method != 'get') { /*执行post请求*/
|
1685
|
+
var postStr = '';
|
1686
|
+
req.on('data', function (chunk) {
|
1687
|
+
postStr += chunk;
|
1688
|
+
})
|
1689
|
+
req.on('end',async function (err, chunk) {
|
1690
|
+
req.body = postStr; /*表示拿到post的值*/
|
1691
|
+
postData = "";
|
1692
|
+
try {
|
1693
|
+
if(req.headers["content-type"].indexOf("application/json")>=0){
|
1694
|
+
postData = JSON.parse(req.body);
|
1695
|
+
}else {
|
1696
|
+
postData = url_module.parse("?" + req.body, true).query;
|
1697
|
+
}
|
1698
|
+
} catch (e) {
|
1699
|
+
|
1700
|
+
}
|
1701
|
+
req.params = Object.assign(req.params, postData);
|
1702
|
+
await G._begin(req, res);
|
1703
|
+
if (!res.alreadySend) await privateObj.dealUseServer(req, res);
|
1704
|
+
if (!res.alreadySend) await G['_' + method][pathname](req, res); /*执行方法*/
|
1705
|
+
})
|
1706
|
+
} else if (method == "get") { /*执行get请求*/
|
1707
|
+
await G._begin(req, res);
|
1708
|
+
if (!res.alreadySend) await privateObj.dealUseServer(req, res);
|
1709
|
+
if (!res.alreadySend) await G['_' + method][pathname](req, res); /*执行方法*/
|
1710
|
+
}
|
1711
|
+
} else {
|
1712
|
+
if (G['_mapping'][pathname]) {
|
1713
|
+
await G._begin(req, res);
|
1714
|
+
if (!res.alreadySend) await privateObj.dealUseServer(req, res);
|
1715
|
+
if (!res.alreadySend) await G['_mapping'][pathname](req, res); /*执行方法*/
|
1716
|
+
} else {
|
1717
|
+
await G._begin(req, res);
|
1718
|
+
if (!res.alreadySend) await privateObj.dealUseServer(req, res);
|
1719
|
+
if (!res.alreadySend) await G._server(req, res);
|
1720
|
+
if (!res.alreadySend) await G["_no_router_handle"](req,res);
|
1721
|
+
}
|
1722
|
+
}
|
1723
|
+
}
|
1724
|
+
} catch (e) {
|
1725
|
+
console.error(e);
|
1726
|
+
M["_gloable_exception_handle"](e,req,res);
|
1727
|
+
}
|
1728
|
+
}
|
1729
|
+
|
1730
|
+
|
1731
|
+
app.begin = function (callback) {
|
1732
|
+
G._begin = callback;
|
1733
|
+
}
|
1734
|
+
|
1735
|
+
app.end = function (callback) {
|
1736
|
+
G._end = callback;
|
1737
|
+
}
|
1738
|
+
/**
|
1739
|
+
*唯一服务的方法
|
1740
|
+
*/
|
1741
|
+
app.server = function (callback) {
|
1742
|
+
G._server = callback;
|
1743
|
+
}
|
1744
|
+
app.use=function (url,callback){
|
1745
|
+
if(typeof url === 'function' || (typeof url === 'object' && !Array.isArray(url))){
|
1746
|
+
let plugin=url;
|
1747
|
+
let args=callback;
|
1748
|
+
if(plugin.installed){
|
1749
|
+
return app;
|
1750
|
+
}
|
1751
|
+
if (typeof plugin === 'function') {
|
1752
|
+
plugin(app, args);
|
1753
|
+
} else {
|
1754
|
+
plugin.install(app, args);
|
1755
|
+
}
|
1756
|
+
M._globle_plugin.add(plugin);
|
1757
|
+
plugin.installed = true;
|
1758
|
+
}else {
|
1759
|
+
if (Array.isArray(url)) {
|
1760
|
+
url.forEach(u=>{
|
1761
|
+
let regExp=new RegExp(u)
|
1762
|
+
G._use[u] = {url,regExp,callback};
|
1763
|
+
})
|
1764
|
+
} else {
|
1765
|
+
let regExp=new RegExp(url)
|
1766
|
+
G._use[url] = {url,regExp,callback};
|
1767
|
+
}
|
1768
|
+
}
|
1769
|
+
return app;
|
1770
|
+
}
|
1771
|
+
|
1772
|
+
app.installPlugin=async function (pluginUrl,constructorParams,pluginParams){
|
1773
|
+
if(M._globle_plugin_url_cacheMap[pluginUrl]){
|
1774
|
+
return
|
1775
|
+
}
|
1776
|
+
M._globle_plugin_url_cacheMap[pluginUrl]=pluginUrl;
|
1777
|
+
const Plugin= await M.require(pluginUrl);
|
1778
|
+
const plugin= new Plugin(constructorParams);
|
1779
|
+
app.use(plugin,pluginParams)
|
1780
|
+
}
|
1781
|
+
|
1782
|
+
/**
|
1783
|
+
* 注册get请求
|
1784
|
+
*/
|
1785
|
+
app.get = function (url, callback) {
|
1786
|
+
url = M.formatUrl(url);
|
1787
|
+
var realUrl = url;
|
1788
|
+
if (url.indexOf(":") > 0) {
|
1789
|
+
url = url.substr(0, url.indexOf(":"));
|
1790
|
+
G._rest[url] = realUrl;
|
1791
|
+
}
|
1792
|
+
|
1793
|
+
G._get[url] = callback;
|
1794
|
+
}
|
1795
|
+
/**
|
1796
|
+
*注册post请求
|
1797
|
+
*/
|
1798
|
+
app.post = function (url, callback) {
|
1799
|
+
url = M.formatUrl(url);
|
1800
|
+
var realUrl = url;
|
1801
|
+
if (url.indexOf(":") > 0) {
|
1802
|
+
url = url.substr(0, url.indexOf(":"));
|
1803
|
+
G._rest[url] = realUrl;
|
1804
|
+
}
|
1805
|
+
G._post[url] = callback;
|
1806
|
+
}
|
1807
|
+
|
1808
|
+
app.put = function (url, callback) {
|
1809
|
+
url = M.formatUrl(url);
|
1810
|
+
var realUrl = url;
|
1811
|
+
if (url.indexOf(":") > 0) {
|
1812
|
+
url = url.substr(0, url.indexOf(":"));
|
1813
|
+
G._rest[url] = realUrl;
|
1814
|
+
}
|
1815
|
+
G._put[url] = callback;
|
1816
|
+
}
|
1817
|
+
|
1818
|
+
app.delete = function (url, callback) {
|
1819
|
+
url = M.formatUrl(url);
|
1820
|
+
var realUrl = url;
|
1821
|
+
if (url.indexOf(":") > 0) {
|
1822
|
+
url = url.substr(0, url.indexOf(":"));
|
1823
|
+
G._rest[url] = realUrl;
|
1824
|
+
}
|
1825
|
+
G._delete[url] = callback;
|
1826
|
+
}
|
1827
|
+
/**
|
1828
|
+
*注册任意请求方法的请求
|
1829
|
+
*/
|
1830
|
+
app.mapping = function (url, callback) {
|
1831
|
+
url = M.formatUrl(url);
|
1832
|
+
var realUrl = url;
|
1833
|
+
if (url.indexOf(":") > 0) {
|
1834
|
+
url = url.substr(0, url.indexOf(":"));
|
1835
|
+
G._rest[url] = realUrl;
|
1836
|
+
}
|
1837
|
+
G._mapping[url] = callback;
|
1838
|
+
}
|
1839
|
+
|
1840
|
+
|
1841
|
+
M.formatUrl = function (url) {
|
1842
|
+
if (!url.endsWith('/')) {
|
1843
|
+
url = url + '/';
|
1844
|
+
}
|
1845
|
+
if (!url.startsWith('/')) {
|
1846
|
+
url = '/' + url;
|
1847
|
+
}
|
1848
|
+
return url;
|
1849
|
+
}
|
1850
|
+
/**
|
1851
|
+
*转发
|
1852
|
+
*/
|
1853
|
+
app.dispatch = function (url, req, res) {
|
1854
|
+
req.url = url;
|
1855
|
+
app(req, res);
|
1856
|
+
}
|
1857
|
+
|
1858
|
+
/**
|
1859
|
+
*重定向
|
1860
|
+
*/
|
1861
|
+
app.redirect = function (url, req, res) {
|
1862
|
+
res.writeHead(302, {'Content-Type': 'text/html; charset=utf-8', 'Location': url});
|
1863
|
+
res.end();
|
1864
|
+
}
|
1865
|
+
|
1866
|
+
app.set = function (k, v) {
|
1867
|
+
M["_" + k] = v;
|
1868
|
+
}
|
1869
|
+
|
1870
|
+
|
1871
|
+
//全局异常钩子
|
1872
|
+
app.set("gloable_exception_handle",(err,req,res)=>{
|
1873
|
+
console.error(err.stack)
|
1874
|
+
if (res && !res.alreadySend) {
|
1868
1875
|
// res.writeHead(500, { "Content-Type": "text/j;charset='utf-8'" });
|
1869
|
-
|
1870
|
-
|
1871
|
-
|
1872
|
-
|
1873
|
-
|
1874
|
-
|
1875
|
-
|
1876
|
-
|
1877
|
-
|
1878
|
-
|
1879
|
-
|
1880
|
-
|
1881
|
-
|
1882
|
-
|
1883
|
-
|
1884
|
-
|
1885
|
-
|
1876
|
+
res.send(M.result(err.message,false,-1));
|
1877
|
+
}
|
1878
|
+
})
|
1879
|
+
|
1880
|
+
//render异常钩子
|
1881
|
+
app.set("render_exception_handle",(err,req,res)=>{
|
1882
|
+
console.error(err.stack)
|
1883
|
+
})
|
1884
|
+
|
1885
|
+
//没有对应接口时的处理器
|
1886
|
+
app.set("no_router_handle",(req,res)=>{
|
1887
|
+
res.end('no router')
|
1888
|
+
})
|
1889
|
+
//没有对应静态页的处理器
|
1890
|
+
app.set("no_page_handle",(req,res)=>{
|
1891
|
+
res.writeHead(404, { "Content-Type": "text/html;charset='utf-8'" });
|
1892
|
+
res.write(`<!DOCTYPE html>
|
1886
1893
|
<html lang="en">
|
1887
1894
|
<head>
|
1888
1895
|
<meta charset="UTF-8">
|
@@ -1899,102 +1906,102 @@ M.urlParse = function (url) {
|
|
1899
1906
|
<p>no page</p>
|
1900
1907
|
</body>
|
1901
1908
|
</html>`
|
1902
|
-
|
1903
|
-
|
1904
|
-
|
1909
|
+
);
|
1910
|
+
res.end();
|
1911
|
+
})
|
1912
|
+
|
1913
|
+
app.listen = function (port) {
|
1914
|
+
const server= http.createServer(app).listen(port);
|
1915
|
+
console.log("listen on port:" + port);
|
1916
|
+
return server;
|
1917
|
+
}
|
1905
1918
|
|
1919
|
+
global.app=app;
|
1920
|
+
return app;
|
1921
|
+
}
|
1922
|
+
M["_gloable_exception_handle"]=(err)=>{
|
1923
|
+
console.error(err)
|
1924
|
+
}
|
1906
1925
|
|
1926
|
+
//异常捕获
|
1927
|
+
process.on('uncaughtException', function (err) {
|
1928
|
+
M["_gloable_exception_handle"](err,M.req,M.res);
|
1929
|
+
});
|
1930
|
+
//监听Promise没有被捕获的失败函数
|
1931
|
+
process.on('unhandledRejection',function(err,promise){
|
1932
|
+
M["_gloable_exception_handle"](err,M.req,M.res);
|
1933
|
+
});
|
1907
1934
|
|
1908
|
-
|
1909
|
-
|
1910
|
-
|
1911
|
-
|
1912
|
-
|
1913
|
-
|
1914
|
-
}
|
1915
|
-
M
|
1916
|
-
|
1917
|
-
|
1918
|
-
|
1919
|
-
|
1920
|
-
|
1921
|
-
|
1922
|
-
|
1923
|
-
|
1924
|
-
|
1925
|
-
|
1926
|
-
|
1927
|
-
|
1928
|
-
|
1929
|
-
|
1930
|
-
|
1931
|
-
|
1932
|
-
|
1933
|
-
|
1934
|
-
|
1935
|
-
|
1936
|
-
|
1937
|
-
|
1938
|
-
|
1939
|
-
|
1940
|
-
|
1941
|
-
|
1942
|
-
|
1943
|
-
|
1944
|
-
|
1945
|
-
|
1946
|
-
|
1947
|
-
|
1948
|
-
|
1949
|
-
|
1950
|
-
|
1951
|
-
|
1952
|
-
|
1953
|
-
|
1954
|
-
|
1955
|
-
|
1956
|
-
|
1957
|
-
|
1958
|
-
|
1959
|
-
|
1960
|
-
|
1961
|
-
|
1962
|
-
|
1963
|
-
|
1964
|
-
|
1965
|
-
|
1966
|
-
|
1967
|
-
|
1968
|
-
|
1969
|
-
|
1970
|
-
|
1971
|
-
|
1972
|
-
|
1973
|
-
|
1974
|
-
|
1975
|
-
|
1976
|
-
|
1977
|
-
|
1978
|
-
if (options == false) {
|
1979
|
-
return;
|
1980
|
-
}
|
1981
|
-
res.setEncoding('utf-8');
|
1982
|
-
res.on('data', function (chunk) {
|
1983
|
-
html += chunk;
|
1984
|
-
});
|
1985
|
-
res.on('end', function () {
|
1986
|
-
M.httpEnd(html);
|
1987
|
-
resolve(html);
|
1988
|
-
});
|
1989
|
-
|
1990
|
-
});
|
1991
|
-
req.on('error', function (err) {
|
1992
|
-
console.error(err);
|
1993
|
-
});
|
1994
|
-
req.write(axiosConfig.body);
|
1995
|
-
req.end();
|
1996
|
-
})
|
1997
|
-
}
|
1935
|
+
|
1936
|
+
/**
|
1937
|
+
* 代理服务器start
|
1938
|
+
*/
|
1939
|
+
M.getAxiosConfig = async (req) => {
|
1940
|
+
return new Promise((resolve, reject) => {
|
1941
|
+
let axiosConfig = {}
|
1942
|
+
axiosConfig.url = M.proxyHost + req.url
|
1943
|
+
axiosConfig.method = req.method.toLocaleLowerCase();
|
1944
|
+
axiosConfig.headers = req.headers
|
1945
|
+
let postStr = '';
|
1946
|
+
req.on('data', function (chunk) {
|
1947
|
+
postStr += chunk;
|
1948
|
+
})
|
1949
|
+
req.on('end', function (err, chunk) {
|
1950
|
+
req.body = postStr; /*表示拿到post的值*/
|
1951
|
+
postData = "";
|
1952
|
+
try {
|
1953
|
+
if (req.body.indexOf("=") == -1) {
|
1954
|
+
postData = JSON.parse(req.body);
|
1955
|
+
} else {
|
1956
|
+
postData = url_module.parse("?" + req.body, true).query;
|
1957
|
+
}
|
1958
|
+
} catch (e) {
|
1959
|
+
}
|
1960
|
+
axiosConfig.data = postData;
|
1961
|
+
axiosConfig.body = req.body;
|
1962
|
+
resolve(axiosConfig)
|
1963
|
+
})
|
1964
|
+
})
|
1965
|
+
}
|
1966
|
+
|
1967
|
+
M.axios = function (axiosConfig) {
|
1968
|
+
axiosConfig.headers.host = "";
|
1969
|
+
var urlObj = url_module.parse(axiosConfig.url)
|
1970
|
+
var options = {
|
1971
|
+
hostname: urlObj.hostname,
|
1972
|
+
port: urlObj.port,
|
1973
|
+
path: urlObj.path,
|
1974
|
+
method: axiosConfig.method.toLocaleUpperCase(),
|
1975
|
+
headers: axiosConfig.headers
|
1976
|
+
}
|
1977
|
+
let reqHttp = http;
|
1978
|
+
if (axiosConfig.url.startsWith("https")) {
|
1979
|
+
reqHttp = https;
|
1980
|
+
}
|
1981
|
+
var html = '';
|
1982
|
+
return new Promise((resolve, reject) => {
|
1983
|
+
var req = reqHttp.request(options, function (res) {
|
1984
|
+
options = M.httpBefore(options);
|
1985
|
+
if (options == false) {
|
1986
|
+
return;
|
1987
|
+
}
|
1988
|
+
res.setEncoding('utf-8');
|
1989
|
+
res.on('data', function (chunk) {
|
1990
|
+
html += chunk;
|
1991
|
+
});
|
1992
|
+
res.on('end', function () {
|
1993
|
+
M.httpEnd(html);
|
1994
|
+
resolve(html);
|
1995
|
+
});
|
1996
|
+
|
1997
|
+
});
|
1998
|
+
req.on('error', function (err) {
|
1999
|
+
console.error(err);
|
2000
|
+
});
|
2001
|
+
req.write(axiosConfig.body);
|
2002
|
+
req.end();
|
2003
|
+
})
|
2004
|
+
}
|
1998
2005
|
/**
|
1999
2006
|
* 代理服务器end
|
2000
2007
|
*/
|
@@ -2005,213 +2012,213 @@ M.urlParse = function (url) {
|
|
2005
2012
|
privateObj.dealUseServer = async function (req, res) {
|
2006
2013
|
for (let key in M._use){
|
2007
2014
|
if(M._use[key].regExp.test(req.url)){
|
2008
|
-
|
2009
|
-
|
2015
|
+
await M._use[key].callback(req,res);
|
2016
|
+
return;
|
2010
2017
|
}
|
2011
2018
|
}
|
2012
2019
|
}
|
2013
2020
|
|
2014
|
-
|
2015
|
-
|
2016
|
-
|
2017
|
-
|
2018
|
-
|
2019
|
-
|
2020
|
-
|
2021
|
-
|
2022
|
-
|
2023
|
-
|
2024
|
-
|
2025
|
-
|
2026
|
-
|
2027
|
-
|
2028
|
-
|
2029
|
-
|
2030
|
-
|
2031
|
-
|
2032
|
-
|
2033
|
-
|
2034
|
-
|
2035
|
-
|
2036
|
-
|
2037
|
-
|
2038
|
-
|
2039
|
-
|
2040
|
-
|
2041
|
-
|
2042
|
-
|
2043
|
-
|
2044
|
-
|
2045
|
-
|
2046
|
-
|
2047
|
-
|
2048
|
-
|
2049
|
-
|
2050
|
-
|
2051
|
-
|
2052
|
-
|
2053
|
-
|
2054
|
-
|
2055
|
-
|
2056
|
-
|
2057
|
-
|
2058
|
-
|
2059
|
-
|
2060
|
-
|
2061
|
-
|
2062
|
-
|
2063
|
-
|
2064
|
-
|
2021
|
+
privateObj.staticServer = async function (req, res, staticPath) {
|
2022
|
+
if (res.alreadySend) return;
|
2023
|
+
var pathname = url_module.parse(req.url).pathname; /*获取url的值*/
|
2024
|
+
if (pathname == '/') {
|
2025
|
+
pathname = '/index.html'; /*默认加载的首页*/
|
2026
|
+
}
|
2027
|
+
let fileName = pathname.replace("/", "");
|
2028
|
+
//获取文件的后缀名
|
2029
|
+
var extname = path.extname(pathname);
|
2030
|
+
|
2031
|
+
if (fileName.startsWith("__default_")) {
|
2032
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
2033
|
+
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
|
2034
|
+
res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
|
2035
|
+
res.setHeader("X-Powered-By", ' 3.2.1')
|
2036
|
+
res.writeHead(200, {"Content-Type": "" + (privateObj.staticMime[extname] || 'text/html') + ";charset='utf-8'",});
|
2037
|
+
res.write(M.__default_file[fileName]);
|
2038
|
+
res.end(); /*结束响应*/
|
2039
|
+
return;
|
2040
|
+
}
|
2041
|
+
if (M.remoteStaticPathEnable && req.url.endsWith("remote=true")) {
|
2042
|
+
if (!res.alreadySend) await res.renderUrl(M.remoteStaticPath + pathname)
|
2043
|
+
return;
|
2044
|
+
}
|
2045
|
+
|
2046
|
+
if (pathname != '/favicon.ico') { /*过滤请求favicon.ico*/
|
2047
|
+
//文件操作获取 static下面的index.html
|
2048
|
+
fs.readFile(staticPath + '/' + pathname, function (err, data) {
|
2049
|
+
if (err) { /*么有这个文件*/
|
2050
|
+
M["_no_page_handle"](req,res);
|
2051
|
+
} else { /*返回这个文件*/
|
2052
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
2053
|
+
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
|
2054
|
+
res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
|
2055
|
+
res.setHeader("X-Powered-By", ' 3.2.1')
|
2056
|
+
res.writeHead(200, {"Content-Type": "" + (privateObj.staticMime[extname] || 'text/html') + ";charset='utf-8'",});
|
2057
|
+
res.write(data);
|
2058
|
+
res.end(); /*结束响应*/
|
2059
|
+
}
|
2060
|
+
})
|
2061
|
+
} else {
|
2062
|
+
res.writeHead(302, {
|
2063
|
+
'Content-Type': 'image/x-icon; charset=utf-8',
|
2064
|
+
'Location': "https://q.qlogo.cn/g?b=qq&nk=934031452&s=100"
|
2065
|
+
});
|
2066
|
+
res.end();
|
2067
|
+
}
|
2068
|
+
}
|
2069
|
+
|
2070
|
+
/*SSE SERVER */
|
2071
|
+
M.sseServer = function () {
|
2065
2072
|
//sse 心跳
|
2066
|
-
|
2067
|
-
|
2068
|
-
|
2069
|
-
|
2070
|
-
|
2071
|
-
|
2072
|
-
|
2073
|
-
|
2074
|
-
|
2075
|
-
|
2076
|
-
|
2077
|
-
|
2078
|
-
|
2073
|
+
function headBeat(){
|
2074
|
+
try {
|
2075
|
+
let clientIdList= Array.from(M._sseClientMap.keys());
|
2076
|
+
for (let i=0;i<clientIdList.length;i++){
|
2077
|
+
let clientId= clientIdList[i];
|
2078
|
+
let res= M._sseClientMap.get(clientId).res;
|
2079
|
+
let r= res.write(': \n\n');
|
2080
|
+
if(!r){
|
2081
|
+
M._sseClientMap.delete(clientId);
|
2082
|
+
M["_sse_disconnect"](clientId);
|
2083
|
+
}
|
2084
|
+
}
|
2085
|
+
}catch (e){
|
2079
2086
|
console.error(e);
|
2080
|
-
|
2081
|
-
|
2087
|
+
}
|
2088
|
+
}
|
2082
2089
|
|
2083
|
-
|
2084
|
-
|
2085
|
-
|
2090
|
+
M["_sse_connection"]=(clientId)=>{
|
2091
|
+
console.log(clientId+" connection")
|
2092
|
+
}
|
2086
2093
|
|
2087
|
-
|
2088
|
-
|
2089
|
-
|
2094
|
+
M["_sse_disconnect"]=(clientId)=>{
|
2095
|
+
console.log(clientId+" disconnect")
|
2096
|
+
}
|
2090
2097
|
|
2091
|
-
|
2098
|
+
M["_sse_send"]=(clientId,msg)=>{
|
2092
2099
|
// console.log("sse_send",clientId,msg)
|
2093
|
-
|
2094
|
-
|
2095
|
-
|
2096
|
-
|
2097
|
-
|
2098
|
-
|
2099
|
-
|
2100
|
-
|
2101
|
-
|
2102
|
-
|
2103
|
-
|
2104
|
-
|
2105
|
-
|
2106
|
-
|
2107
|
-
|
2108
|
-
|
2109
|
-
|
2110
|
-
|
2111
|
-
|
2112
|
-
|
2113
|
-
|
2114
|
-
|
2115
|
-
|
2116
|
-
|
2117
|
-
|
2118
|
-
|
2119
|
-
|
2120
|
-
|
2121
|
-
|
2122
|
-
|
2123
|
-
|
2124
|
-
|
2125
|
-
|
2126
|
-
|
2127
|
-
|
2128
|
-
|
2129
|
-
|
2130
|
-
|
2131
|
-
|
2132
|
-
|
2133
|
-
|
2134
|
-
|
2135
|
-
|
2136
|
-
|
2137
|
-
|
2138
|
-
|
2139
|
-
|
2140
|
-
|
2141
|
-
|
2142
|
-
|
2143
|
-
|
2144
|
-
|
2145
|
-
|
2146
|
-
|
2147
|
-
|
2148
|
-
|
2149
|
-
|
2150
|
-
|
2151
|
-
|
2152
|
-
|
2153
|
-
|
2154
|
-
|
2155
|
-
|
2156
|
-
|
2157
|
-
|
2158
|
-
|
2159
|
-
|
2160
|
-
|
2161
|
-
|
2162
|
-
|
2163
|
-
|
2164
|
-
|
2165
|
-
|
2166
|
-
|
2167
|
-
|
2168
|
-
|
2169
|
-
|
2170
|
-
|
2171
|
-
|
2172
|
-
|
2173
|
-
|
2174
|
-
|
2175
|
-
|
2176
|
-
|
2177
|
-
|
2178
|
-
|
2179
|
-
|
2180
|
-
|
2181
|
-
|
2182
|
-
|
2183
|
-
|
2184
|
-
|
2185
|
-
|
2186
|
-
|
2187
|
-
|
2188
|
-
|
2189
|
-
|
2190
|
-
|
2191
|
-
|
2192
|
-
|
2193
|
-
|
2194
|
-
|
2195
|
-
|
2196
|
-
|
2197
|
-
|
2198
|
-
|
2199
|
-
|
2200
|
-
|
2201
|
-
|
2100
|
+
}
|
2101
|
+
|
2102
|
+
event.on('sseSendMsg', function (sendData,clientId,eventName="slide",id=+new Date()) {
|
2103
|
+
let sendObj={event:eventName,id:id,data:JSON.stringify(sendData),retry:10000}
|
2104
|
+
if(!clientId){
|
2105
|
+
let clientIdList= Array.from(M._sseClientMap.keys())
|
2106
|
+
for (let i=0;i<clientIdList.length;i++){
|
2107
|
+
let clientId= clientIdList[i];
|
2108
|
+
let res=M._sseClientMap.get(clientId)?M._sseClientMap.get(clientId).res:null;
|
2109
|
+
if(res==null){
|
2110
|
+
continue;
|
2111
|
+
}
|
2112
|
+
res.write(`event: ${sendObj.event}\n`); // 事件类型
|
2113
|
+
res.write(`id: ${sendObj.id}\n`); // 消息 ID
|
2114
|
+
res.write(`data: ${sendObj.data}\n`); // 消息数据
|
2115
|
+
res.write(`retry: ${sendObj.retry}\n`); // 重连时间
|
2116
|
+
res.write('\n\n'); // 消息结束
|
2117
|
+
}
|
2118
|
+
}else {
|
2119
|
+
let res=M._sseClientMap.get(clientId)?M._sseClientMap.get(clientId).res:null;
|
2120
|
+
if(res==null){
|
2121
|
+
return
|
2122
|
+
}
|
2123
|
+
res.write(`event: ${sendObj.event}\n`); // 事件类型
|
2124
|
+
res.write(`id: ${sendObj.id}\n`); // 消息 ID
|
2125
|
+
res.write(`data: ${sendObj.data}\n`); // 消息数据
|
2126
|
+
res.write(`retry: ${sendObj.retry}\n`); // 重连时间
|
2127
|
+
res.write('\n\n'); // 消息结束
|
2128
|
+
}
|
2129
|
+
M["_sse_send"](clientId,sendObj);
|
2130
|
+
})
|
2131
|
+
setInterval(() => {
|
2132
|
+
headBeat();
|
2133
|
+
}, M._sseHeatTime);
|
2134
|
+
|
2135
|
+
let app = function (req, res) {
|
2136
|
+
headBeat();
|
2137
|
+
let clientId=req.params.clientId;
|
2138
|
+
let connectionRes= M["_sse_connection"](clientId);
|
2139
|
+
if(connectionRes==false){
|
2140
|
+
return;
|
2141
|
+
}
|
2142
|
+
M._sseClientMap.set(clientId,{res});
|
2143
|
+
res.writeHead(200, {
|
2144
|
+
'Content-Type': 'text/event-stream',
|
2145
|
+
'Cache-Control': 'no-cache',
|
2146
|
+
'Connection': 'keep-alive',
|
2147
|
+
'Access-Control-Allow-Origin': '*',
|
2148
|
+
});
|
2149
|
+
};
|
2150
|
+
app.send = function (msg,clientId,eventName,id) {
|
2151
|
+
event.emit('sseSendMsg', msg,clientId,eventName,id);
|
2152
|
+
}
|
2153
|
+
|
2154
|
+
app.call = function (eventName,msg,clientId,id) {
|
2155
|
+
event.emit('sseSendMsg', msg,clientId,eventName,id);
|
2156
|
+
}
|
2157
|
+
|
2158
|
+
app.set = function (k, v) {
|
2159
|
+
M["_" + k] = v;
|
2160
|
+
}
|
2161
|
+
|
2162
|
+
app.listen = function (port) {
|
2163
|
+
let serverObj = http.createServer(app).listen(port);
|
2164
|
+
app.serverObj = serverObj;
|
2165
|
+
console.log("SSE Server listen on port:" + port);
|
2166
|
+
return app;
|
2167
|
+
}
|
2168
|
+
return app;
|
2169
|
+
}
|
2170
|
+
|
2171
|
+
/**
|
2172
|
+
* ----------------------服务器端END--------------------------------------------
|
2173
|
+
*/
|
2174
|
+
|
2175
|
+
|
2176
|
+
/**
|
2177
|
+
* ----------------------其他工具函数START--------------------------------------------
|
2178
|
+
*/
|
2179
|
+
M.exec = function (comand) {
|
2180
|
+
var promise = new Promise(function (reslove, reject) {
|
2181
|
+
child_process.exec(comand, function (err, stdout, stderr) {
|
2182
|
+
if (err || stderr) console.error(err, stderr);
|
2183
|
+
reslove(stdout);
|
2184
|
+
});
|
2185
|
+
|
2186
|
+
})
|
2187
|
+
return promise;
|
2188
|
+
}
|
2189
|
+
|
2190
|
+
M.getMyIp = function () {
|
2191
|
+
var interfaces = require('os').networkInterfaces();
|
2192
|
+
for (var devName in interfaces) {
|
2193
|
+
var iface = interfaces[devName];
|
2194
|
+
for (var i = 0; i < iface.length; i++) {
|
2195
|
+
var alias = iface[i];
|
2196
|
+
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
|
2197
|
+
return alias.address;
|
2198
|
+
}
|
2199
|
+
}
|
2200
|
+
}
|
2201
|
+
}
|
2202
|
+
|
2203
|
+
/**
|
2204
|
+
*对象转JSON key不用引号括起来,因兼容性不好,所以去掉
|
2205
|
+
*/
|
2206
|
+
|
2207
|
+
/**
|
2208
|
+
M.JSOM_Stringify=function(obj){
|
2202
2209
|
return JSON.stringify(obj).replace(/"(\w+)"(\s*:\s*)/gis, '$1$2');
|
2203
2210
|
}
|
2204
|
-
|
2205
|
-
|
2206
|
-
|
2207
|
-
|
2208
|
-
|
2209
|
-
|
2210
|
-
|
2211
|
-
|
2212
|
-
|
2213
|
-
|
2214
|
-
|
2211
|
+
*/
|
2212
|
+
|
2213
|
+
M.sleep = function (numberMillis) {
|
2214
|
+
var now = new Date();
|
2215
|
+
var exitTime = now.getTime() + numberMillis;
|
2216
|
+
while (true) {
|
2217
|
+
now = new Date();
|
2218
|
+
if (now.getTime() > exitTime)
|
2219
|
+
return;
|
2220
|
+
}
|
2221
|
+
}
|
2215
2222
|
|
2216
2223
|
M.delayMs=async function (ms){
|
2217
2224
|
return new Promise(r=>{
|
@@ -2221,604 +2228,607 @@ M.delayMs=async function (ms){
|
|
2221
2228
|
})
|
2222
2229
|
}
|
2223
2230
|
|
2224
|
-
|
2225
|
-
|
2226
|
-
|
2227
|
-
|
2228
|
-
|
2229
|
-
|
2230
|
-
|
2231
|
-
|
2232
|
-
|
2233
|
-
|
2234
|
-
|
2235
|
-
|
2236
|
-
|
2237
|
-
|
2238
|
-
|
2239
|
-
|
2240
|
-
|
2241
|
-
|
2242
|
-
|
2243
|
-
|
2244
|
-
|
2245
|
-
|
2246
|
-
|
2247
|
-
|
2248
|
-
|
2249
|
-
|
2250
|
-
|
2251
|
-
|
2252
|
-
|
2253
|
-
|
2254
|
-
|
2255
|
-
|
2256
|
-
|
2257
|
-
|
2258
|
-
|
2259
|
-
|
2260
|
-
|
2261
|
-
|
2262
|
-
|
2263
|
-
|
2264
|
-
|
2265
|
-
|
2266
|
-
|
2267
|
-
|
2268
|
-
|
2269
|
-
|
2270
|
-
|
2271
|
-
|
2272
|
-
|
2273
|
-
|
2274
|
-
|
2275
|
-
|
2276
|
-
|
2277
|
-
|
2278
|
-
|
2279
|
-
|
2280
|
-
|
2281
|
-
|
2282
|
-
|
2283
|
-
|
2284
|
-
|
2285
|
-
|
2286
|
-
|
2287
|
-
|
2288
|
-
|
2289
|
-
|
2290
|
-
|
2291
|
-
|
2292
|
-
|
2293
|
-
|
2294
|
-
|
2295
|
-
|
2296
|
-
|
2297
|
-
|
2298
|
-
|
2299
|
-
|
2300
|
-
|
2301
|
-
|
2302
|
-
|
2303
|
-
|
2304
|
-
|
2305
|
-
|
2306
|
-
|
2307
|
-
|
2308
|
-
|
2309
|
-
|
2310
|
-
|
2311
|
-
|
2312
|
-
|
2313
|
-
|
2314
|
-
|
2315
|
-
|
2316
|
-
|
2317
|
-
|
2318
|
-
|
2319
|
-
|
2320
|
-
|
2321
|
-
|
2322
|
-
|
2323
|
-
|
2324
|
-
|
2325
|
-
|
2326
|
-
|
2327
|
-
|
2328
|
-
|
2329
|
-
|
2330
|
-
|
2331
|
-
|
2332
|
-
|
2333
|
-
|
2334
|
-
|
2335
|
-
|
2336
|
-
|
2337
|
-
|
2338
|
-
|
2339
|
-
|
2340
|
-
|
2341
|
-
|
2342
|
-
|
2343
|
-
|
2344
|
-
|
2345
|
-
|
2346
|
-
|
2347
|
-
|
2348
|
-
|
2349
|
-
|
2350
|
-
|
2351
|
-
|
2352
|
-
|
2353
|
-
|
2354
|
-
|
2355
|
-
|
2356
|
-
|
2357
|
-
|
2358
|
-
|
2359
|
-
|
2360
|
-
|
2361
|
-
|
2362
|
-
|
2363
|
-
|
2364
|
-
|
2365
|
-
|
2366
|
-
|
2367
|
-
|
2368
|
-
|
2369
|
-
|
2370
|
-
|
2371
|
-
|
2372
|
-
|
2373
|
-
|
2374
|
-
|
2375
|
-
|
2376
|
-
|
2377
|
-
|
2378
|
-
|
2379
|
-
|
2380
|
-
|
2381
|
-
|
2382
|
-
|
2383
|
-
|
2384
|
-
|
2385
|
-
|
2386
|
-
|
2387
|
-
|
2388
|
-
|
2389
|
-
|
2390
|
-
|
2391
|
-
|
2392
|
-
|
2393
|
-
|
2394
|
-
|
2395
|
-
|
2396
|
-
|
2397
|
-
|
2398
|
-
|
2399
|
-
|
2400
|
-
|
2401
|
-
|
2402
|
-
|
2403
|
-
|
2404
|
-
|
2405
|
-
|
2406
|
-
|
2407
|
-
|
2408
|
-
|
2409
|
-
|
2410
|
-
|
2411
|
-
|
2412
|
-
|
2413
|
-
|
2414
|
-
|
2415
|
-
|
2416
|
-
|
2417
|
-
|
2418
|
-
|
2419
|
-
|
2420
|
-
|
2421
|
-
|
2422
|
-
|
2423
|
-
|
2424
|
-
|
2425
|
-
|
2426
|
-
|
2427
|
-
|
2428
|
-
|
2429
|
-
|
2430
|
-
|
2431
|
-
|
2432
|
-
|
2433
|
-
|
2434
|
-
|
2435
|
-
|
2436
|
-
|
2437
|
-
|
2438
|
-
|
2439
|
-
|
2440
|
-
|
2441
|
-
|
2442
|
-
|
2443
|
-
|
2444
|
-
|
2445
|
-
|
2446
|
-
|
2447
|
-
|
2448
|
-
|
2449
|
-
|
2450
|
-
|
2451
|
-
|
2452
|
-
|
2453
|
-
|
2454
|
-
|
2455
|
-
|
2456
|
-
|
2457
|
-
|
2458
|
-
|
2459
|
-
|
2460
|
-
|
2461
|
-
|
2462
|
-
|
2463
|
-
|
2464
|
-
|
2465
|
-
|
2466
|
-
|
2467
|
-
|
2468
|
-
|
2469
|
-
|
2470
|
-
|
2471
|
-
|
2472
|
-
|
2473
|
-
|
2474
|
-
|
2475
|
-
|
2476
|
-
|
2477
|
-
|
2478
|
-
|
2479
|
-
|
2480
|
-
|
2481
|
-
|
2482
|
-
|
2483
|
-
|
2484
|
-
|
2485
|
-
|
2486
|
-
|
2487
|
-
|
2488
|
-
|
2489
|
-
|
2490
|
-
|
2491
|
-
|
2492
|
-
|
2493
|
-
|
2494
|
-
|
2495
|
-
|
2496
|
-
|
2497
|
-
|
2498
|
-
|
2499
|
-
|
2500
|
-
|
2501
|
-
|
2502
|
-
|
2503
|
-
|
2504
|
-
|
2505
|
-
|
2506
|
-
|
2507
|
-
|
2508
|
-
|
2509
|
-
|
2510
|
-
|
2511
|
-
|
2512
|
-
|
2513
|
-
|
2514
|
-
|
2515
|
-
|
2516
|
-
|
2517
|
-
|
2518
|
-
|
2519
|
-
|
2520
|
-
|
2521
|
-
|
2522
|
-
|
2523
|
-
|
2524
|
-
|
2525
|
-
|
2526
|
-
|
2527
|
-
|
2528
|
-
|
2529
|
-
|
2530
|
-
|
2531
|
-
|
2532
|
-
|
2533
|
-
|
2534
|
-
|
2535
|
-
|
2536
|
-
|
2537
|
-
|
2538
|
-
|
2539
|
-
|
2540
|
-
|
2541
|
-
|
2542
|
-
|
2543
|
-
|
2544
|
-
|
2545
|
-
|
2546
|
-
|
2547
|
-
|
2548
|
-
|
2549
|
-
|
2550
|
-
|
2551
|
-
|
2552
|
-
|
2553
|
-
|
2554
|
-
|
2555
|
-
|
2556
|
-
|
2557
|
-
|
2558
|
-
|
2559
|
-
|
2560
|
-
|
2561
|
-
|
2562
|
-
|
2563
|
-
|
2564
|
-
|
2565
|
-
|
2566
|
-
|
2567
|
-
|
2568
|
-
|
2569
|
-
|
2570
|
-
|
2571
|
-
|
2572
|
-
|
2573
|
-
|
2574
|
-
|
2575
|
-
|
2576
|
-
|
2577
|
-
|
2578
|
-
|
2579
|
-
|
2580
|
-
|
2581
|
-
|
2582
|
-
|
2583
|
-
|
2584
|
-
|
2585
|
-
|
2586
|
-
|
2587
|
-
|
2588
|
-
|
2589
|
-
|
2590
|
-
|
2591
|
-
|
2592
|
-
|
2593
|
-
|
2594
|
-
|
2595
|
-
|
2596
|
-
|
2597
|
-
|
2598
|
-
|
2599
|
-
|
2600
|
-
|
2601
|
-
|
2602
|
-
|
2603
|
-
|
2604
|
-
|
2605
|
-
|
2606
|
-
|
2607
|
-
|
2608
|
-
|
2609
|
-
|
2610
|
-
|
2611
|
-
|
2612
|
-
|
2613
|
-
|
2614
|
-
|
2615
|
-
|
2616
|
-
|
2617
|
-
|
2618
|
-
|
2619
|
-
|
2620
|
-
|
2621
|
-
|
2622
|
-
|
2623
|
-
|
2624
|
-
|
2625
|
-
|
2626
|
-
|
2627
|
-
|
2628
|
-
|
2629
|
-
|
2630
|
-
|
2631
|
-
|
2632
|
-
|
2633
|
-
|
2634
|
-
|
2635
|
-
|
2636
|
-
|
2637
|
-
|
2638
|
-
|
2639
|
-
|
2640
|
-
|
2641
|
-
|
2642
|
-
|
2643
|
-
|
2644
|
-
|
2645
|
-
|
2646
|
-
|
2647
|
-
|
2648
|
-
|
2649
|
-
|
2650
|
-
|
2651
|
-
|
2652
|
-
|
2653
|
-
|
2654
|
-
|
2655
|
-
|
2656
|
-
|
2657
|
-
|
2658
|
-
|
2659
|
-
|
2660
|
-
|
2661
|
-
|
2662
|
-
|
2663
|
-
|
2664
|
-
|
2665
|
-
|
2666
|
-
|
2667
|
-
|
2668
|
-
|
2669
|
-
|
2670
|
-
|
2671
|
-
|
2672
|
-
|
2673
|
-
|
2674
|
-
|
2675
|
-
|
2676
|
-
|
2677
|
-
|
2678
|
-
|
2679
|
-
|
2680
|
-
|
2681
|
-
|
2682
|
-
|
2683
|
-
|
2684
|
-
|
2685
|
-
|
2686
|
-
|
2687
|
-
|
2688
|
-
|
2689
|
-
|
2690
|
-
|
2691
|
-
|
2692
|
-
|
2693
|
-
|
2694
|
-
|
2695
|
-
|
2696
|
-
|
2697
|
-
|
2698
|
-
|
2699
|
-
|
2700
|
-
|
2701
|
-
|
2702
|
-
|
2703
|
-
|
2704
|
-
|
2705
|
-
|
2706
|
-
|
2707
|
-
|
2708
|
-
|
2709
|
-
|
2710
|
-
|
2711
|
-
|
2712
|
-
|
2713
|
-
|
2714
|
-
|
2715
|
-
|
2716
|
-
|
2717
|
-
|
2718
|
-
|
2719
|
-
|
2720
|
-
|
2721
|
-
|
2722
|
-
|
2723
|
-
|
2724
|
-
|
2725
|
-
|
2726
|
-
|
2727
|
-
|
2728
|
-
|
2729
|
-
|
2730
|
-
|
2731
|
-
|
2732
|
-
|
2733
|
-
|
2734
|
-
|
2735
|
-
|
2736
|
-
|
2737
|
-
|
2738
|
-
|
2739
|
-
|
2740
|
-
|
2741
|
-
|
2742
|
-
|
2743
|
-
|
2744
|
-
|
2745
|
-
|
2746
|
-
|
2747
|
-
|
2748
|
-
|
2749
|
-
|
2750
|
-
|
2751
|
-
|
2752
|
-
|
2753
|
-
|
2754
|
-
|
2755
|
-
|
2756
|
-
|
2757
|
-
|
2758
|
-
|
2759
|
-
|
2760
|
-
|
2761
|
-
|
2762
|
-
|
2763
|
-
|
2764
|
-
|
2765
|
-
|
2766
|
-
|
2767
|
-
|
2768
|
-
|
2769
|
-
|
2770
|
-
|
2771
|
-
|
2772
|
-
|
2773
|
-
|
2774
|
-
|
2775
|
-
|
2776
|
-
|
2777
|
-
|
2778
|
-
|
2779
|
-
|
2780
|
-
|
2781
|
-
|
2782
|
-
|
2783
|
-
|
2784
|
-
|
2785
|
-
|
2786
|
-
|
2787
|
-
|
2788
|
-
|
2789
|
-
|
2790
|
-
|
2791
|
-
|
2792
|
-
|
2793
|
-
|
2794
|
-
|
2795
|
-
|
2796
|
-
|
2797
|
-
|
2798
|
-
|
2799
|
-
|
2800
|
-
|
2801
|
-
|
2802
|
-
|
2803
|
-
|
2804
|
-
|
2805
|
-
|
2806
|
-
|
2807
|
-
|
2808
|
-
|
2809
|
-
|
2810
|
-
|
2811
|
-
|
2812
|
-
|
2813
|
-
|
2814
|
-
|
2815
|
-
|
2816
|
-
|
2817
|
-
|
2818
|
-
|
2819
|
-
|
2820
|
-
|
2821
|
-
|
2822
|
-
|
2823
|
-
|
2824
|
-
|
2231
|
+
/**
|
2232
|
+
* ----------------------其他工具函数END--------------------------------------------
|
2233
|
+
*/
|
2234
|
+
|
2235
|
+
/**
|
2236
|
+
* 静态资源对应表
|
2237
|
+
*/
|
2238
|
+
privateObj.staticMime = {
|
2239
|
+
".323": "text/h323",
|
2240
|
+
".3gp": "video/3gpp",
|
2241
|
+
".aab": "application/x-authoware-bin",
|
2242
|
+
".aam": "application/x-authoware-map",
|
2243
|
+
".aas": "application/x-authoware-seg",
|
2244
|
+
".acx": "application/internet-property-stream",
|
2245
|
+
".ai": "application/postscript",
|
2246
|
+
".aif": "audio/x-aiff",
|
2247
|
+
".aifc": "audio/x-aiff",
|
2248
|
+
".aiff": "audio/x-aiff",
|
2249
|
+
".als": "audio/X-Alpha5",
|
2250
|
+
".amc": "application/x-mpeg",
|
2251
|
+
".ani": "application/octet-stream",
|
2252
|
+
".apk": "application/vnd.android.package-archive",
|
2253
|
+
".asc": "text/plain",
|
2254
|
+
".asd": "application/astound",
|
2255
|
+
".asf": "video/x-ms-asf",
|
2256
|
+
".asn": "application/astound",
|
2257
|
+
".asp": "application/x-asap",
|
2258
|
+
".asr": "video/x-ms-asf",
|
2259
|
+
".asx": "video/x-ms-asf",
|
2260
|
+
".au": "audio/basic",
|
2261
|
+
".avb": "application/octet-stream",
|
2262
|
+
".avi": "video/x-msvideo",
|
2263
|
+
".awb": "audio/amr-wb",
|
2264
|
+
".axs": "application/olescript",
|
2265
|
+
".bas": "text/plain",
|
2266
|
+
".bcpio": "application/x-bcpio",
|
2267
|
+
".bin ": "application/octet-stream",
|
2268
|
+
".bld": "application/bld",
|
2269
|
+
".bld2": "application/bld2",
|
2270
|
+
".bmp": "image/bmp",
|
2271
|
+
".bpk": "application/octet-stream",
|
2272
|
+
".bz2": "application/x-bzip2",
|
2273
|
+
".c": "text/plain",
|
2274
|
+
".cal": "image/x-cals",
|
2275
|
+
".cat": "application/vnd.ms-pkiseccat",
|
2276
|
+
".ccn": "application/x-cnc",
|
2277
|
+
".cco": "application/x-cocoa",
|
2278
|
+
".cdf": "application/x-cdf",
|
2279
|
+
".cer": "application/x-x509-ca-cert",
|
2280
|
+
".cgi": "magnus-internal/cgi",
|
2281
|
+
".chat": "application/x-chat",
|
2282
|
+
".class": "application/octet-stream",
|
2283
|
+
".clp": "application/x-msclip",
|
2284
|
+
".cmx": "image/x-cmx",
|
2285
|
+
".co": "application/x-cult3d-object",
|
2286
|
+
".cod": "image/cis-cod",
|
2287
|
+
".conf": "text/plain",
|
2288
|
+
".cpio": "application/x-cpio",
|
2289
|
+
".cpp": "text/plain",
|
2290
|
+
".cpt": "application/mac-compactpro",
|
2291
|
+
".crd": "application/x-mscardfile",
|
2292
|
+
".crl": "application/pkix-crl",
|
2293
|
+
".crt": "application/x-x509-ca-cert",
|
2294
|
+
".csh": "application/x-csh",
|
2295
|
+
".csm": "chemical/x-csml",
|
2296
|
+
".csml": "chemical/x-csml",
|
2297
|
+
".css": "text/css",
|
2298
|
+
".cur": "application/octet-stream",
|
2299
|
+
".dcm": "x-lml/x-evm",
|
2300
|
+
".dcr": "application/x-director",
|
2301
|
+
".dcx": "image/x-dcx",
|
2302
|
+
".der": "application/x-x509-ca-cert",
|
2303
|
+
".dhtml": "text/html",
|
2304
|
+
".dir": "application/x-director",
|
2305
|
+
".dll": "application/x-msdownload",
|
2306
|
+
".dmg": "application/octet-stream",
|
2307
|
+
".dms": "application/octet-stream",
|
2308
|
+
".doc": "application/msword",
|
2309
|
+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
2310
|
+
".dot": "application/msword",
|
2311
|
+
".dvi": "application/x-dvi",
|
2312
|
+
".dwf": "drawing/x-dwf",
|
2313
|
+
".dwg": "application/x-autocad",
|
2314
|
+
".dxf": "application/x-autocad",
|
2315
|
+
".dxr": "application/x-director",
|
2316
|
+
".ebk": "application/x-expandedbook",
|
2317
|
+
".emb": "chemical/x-embl-dl-nucleotide",
|
2318
|
+
".embl": "chemical/x-embl-dl-nucleotide",
|
2319
|
+
".eps": "application/postscript",
|
2320
|
+
".epub": "application/epub+zip",
|
2321
|
+
".eri": "image/x-eri",
|
2322
|
+
".es": "audio/echospeech",
|
2323
|
+
".esl": "audio/echospeech",
|
2324
|
+
".etc": "application/x-earthtime",
|
2325
|
+
".etx": "text/x-setext",
|
2326
|
+
".evm": "x-lml/x-evm",
|
2327
|
+
".evy": "application/envoy",
|
2328
|
+
".exe": "application/octet-stream",
|
2329
|
+
".fh4": "image/x-freehand",
|
2330
|
+
".fh5": "image/x-freehand",
|
2331
|
+
".fhc": "image/x-freehand",
|
2332
|
+
".fif": "application/fractals",
|
2333
|
+
".flr": "x-world/x-vrml",
|
2334
|
+
".flv": "flv-application/octet-stream",
|
2335
|
+
".fm": "application/x-maker",
|
2336
|
+
".fpx": "image/x-fpx",
|
2337
|
+
".fvi": "video/isivideo",
|
2338
|
+
".gau": "chemical/x-gaussian-input",
|
2339
|
+
".gca": "application/x-gca-compressed",
|
2340
|
+
".gdb": "x-lml/x-gdb",
|
2341
|
+
".gif": "image/gif",
|
2342
|
+
".gps": "application/x-gps",
|
2343
|
+
".gtar": "application/x-gtar",
|
2344
|
+
".gz": "application/x-gzip",
|
2345
|
+
".h": "text/plain",
|
2346
|
+
".hdf": "application/x-hdf",
|
2347
|
+
".hdm": "text/x-hdml",
|
2348
|
+
".hdml": "text/x-hdml",
|
2349
|
+
".hlp": "application/winhlp",
|
2350
|
+
".hqx": "application/mac-binhex40",
|
2351
|
+
".hta": "application/hta",
|
2352
|
+
".htc": "text/x-component",
|
2353
|
+
".htm": "text/html",
|
2354
|
+
".html": "text/html",
|
2355
|
+
".hts": "text/html",
|
2356
|
+
".htt": "text/webviewhtml",
|
2357
|
+
".ice": "x-conference/x-cooltalk",
|
2358
|
+
".ico": "image/x-icon",
|
2359
|
+
".ief": "image/ief",
|
2360
|
+
".ifm": "image/gif",
|
2361
|
+
".ifs": "image/ifs",
|
2362
|
+
".iii": "application/x-iphone",
|
2363
|
+
".imy": "audio/melody",
|
2364
|
+
".ins": "application/x-internet-signup",
|
2365
|
+
".ips": "application/x-ipscript",
|
2366
|
+
".ipx": "application/x-ipix",
|
2367
|
+
".isp": "application/x-internet-signup",
|
2368
|
+
".it": "audio/x-mod",
|
2369
|
+
".itz": "audio/x-mod",
|
2370
|
+
".ivr": "i-world/i-vrml",
|
2371
|
+
".j2k": "image/j2k",
|
2372
|
+
".jad": "text/vnd.sun.j2me.app-descriptor",
|
2373
|
+
".jam": "application/x-jam",
|
2374
|
+
".jar": "application/java-archive",
|
2375
|
+
".java": "text/plain",
|
2376
|
+
".jfif": "image/pipeg",
|
2377
|
+
".jnlp": "application/x-java-jnlp-file",
|
2378
|
+
".jpe": "image/jpeg",
|
2379
|
+
".jpeg": "image/jpeg",
|
2380
|
+
".jpg": "image/jpeg",
|
2381
|
+
".jpz": "image/jpeg",
|
2382
|
+
".js": "application/javascript",
|
2383
|
+
".jsx": "application/javascript",
|
2384
|
+
".woff":" application/x-font-woff",
|
2385
|
+
".woff2":" application/x-font-woff",
|
2386
|
+
".jwc": "application/jwc",
|
2387
|
+
".kjx": "application/x-kjx",
|
2388
|
+
".lak": "x-lml/x-lak",
|
2389
|
+
".latex": "application/x-latex",
|
2390
|
+
".lcc": "application/fastman",
|
2391
|
+
".lcl": "application/x-digitalloca",
|
2392
|
+
".lcr": "application/x-digitalloca",
|
2393
|
+
".lgh": "application/lgh",
|
2394
|
+
".lha": "application/octet-stream",
|
2395
|
+
".lml": "x-lml/x-lml",
|
2396
|
+
".lmlpack": "x-lml/x-lmlpack",
|
2397
|
+
".log": "text/plain",
|
2398
|
+
".lsf": "video/x-la-asf",
|
2399
|
+
".lsx": "video/x-la-asf",
|
2400
|
+
".lzh": "application/octet-stream",
|
2401
|
+
".m13": "application/x-msmediaview",
|
2402
|
+
".m14": "application/x-msmediaview",
|
2403
|
+
".m15": "audio/x-mod",
|
2404
|
+
".m3u": "audio/x-mpegurl",
|
2405
|
+
".m3url": "audio/x-mpegurl",
|
2406
|
+
".m4a": "audio/mp4a-latm",
|
2407
|
+
".m4b": "audio/mp4a-latm",
|
2408
|
+
".m4p": "audio/mp4a-latm",
|
2409
|
+
".m4u": "video/vnd.mpegurl",
|
2410
|
+
".m4v": "video/x-m4v",
|
2411
|
+
".ma1": "audio/ma1",
|
2412
|
+
".ma2": "audio/ma2",
|
2413
|
+
".ma3": "audio/ma3",
|
2414
|
+
".ma5": "audio/ma5",
|
2415
|
+
".man": "application/x-troff-man",
|
2416
|
+
".map": "magnus-internal/imagemap",
|
2417
|
+
".mbd": "application/mbedlet",
|
2418
|
+
".mct": "application/x-mascot",
|
2419
|
+
".mdb": "application/x-msaccess",
|
2420
|
+
".mdz": "audio/x-mod",
|
2421
|
+
".me": "application/x-troff-me",
|
2422
|
+
".mel": "text/x-vmel",
|
2423
|
+
".mht": "message/rfc822",
|
2424
|
+
".mhtml": "message/rfc822",
|
2425
|
+
".mi": "application/x-mif",
|
2426
|
+
".mid": "audio/mid",
|
2427
|
+
".midi": "audio/midi",
|
2428
|
+
".mif": "application/x-mif",
|
2429
|
+
".mil": "image/x-cals",
|
2430
|
+
".mio": "audio/x-mio",
|
2431
|
+
".mmf": "application/x-skt-lbs",
|
2432
|
+
".mng": "video/x-mng",
|
2433
|
+
".mny": "application/x-msmoney",
|
2434
|
+
".moc": "application/x-mocha",
|
2435
|
+
".mocha": "application/x-mocha",
|
2436
|
+
".mod": "audio/x-mod",
|
2437
|
+
".mof": "application/x-yumekara",
|
2438
|
+
".mol": "chemical/x-mdl-molfile",
|
2439
|
+
".mop": "chemical/x-mopac-input",
|
2440
|
+
".mov": "video/quicktime",
|
2441
|
+
".movie": "video/x-sgi-movie",
|
2442
|
+
".mp2": "video/mpeg",
|
2443
|
+
".mp3": "audio/mpeg",
|
2444
|
+
".mp4": "video/mp4",
|
2445
|
+
".mpa": "video/mpeg",
|
2446
|
+
".mpc": "application/vnd.mpohun.certificate",
|
2447
|
+
".mpe": "video/mpeg",
|
2448
|
+
".mpeg": "video/mpeg",
|
2449
|
+
".mpg": "video/mpeg",
|
2450
|
+
".mpg4": "video/mp4",
|
2451
|
+
".mpga": "audio/mpeg",
|
2452
|
+
".mpn": "application/vnd.mophun.application",
|
2453
|
+
".mpp": "application/vnd.ms-project",
|
2454
|
+
".mps": "application/x-mapserver",
|
2455
|
+
".mpv2": "video/mpeg",
|
2456
|
+
".mrl": "text/x-mrml",
|
2457
|
+
".mrm": "application/x-mrm",
|
2458
|
+
".ms": "application/x-troff-ms",
|
2459
|
+
".msg": "application/vnd.ms-outlook",
|
2460
|
+
".mts": "application/metastream",
|
2461
|
+
".mtx": "application/metastream",
|
2462
|
+
".mtz": "application/metastream",
|
2463
|
+
".mvb": "application/x-msmediaview",
|
2464
|
+
".mzv": "application/metastream",
|
2465
|
+
".nar": "application/zip",
|
2466
|
+
".nbmp": "image/nbmp",
|
2467
|
+
".nc": "application/x-netcdf",
|
2468
|
+
".ndb": "x-lml/x-ndb",
|
2469
|
+
".ndwn": "application/ndwn",
|
2470
|
+
".nif": "application/x-nif",
|
2471
|
+
".nmz": "application/x-scream",
|
2472
|
+
".nokia-op-logo": "image/vnd.nok-oplogo-color",
|
2473
|
+
".npx": "application/x-netfpx",
|
2474
|
+
".nsnd": "audio/nsnd",
|
2475
|
+
".nva": "application/x-neva1",
|
2476
|
+
".nws": "message/rfc822",
|
2477
|
+
".oda": "application/oda",
|
2478
|
+
".ogg": "audio/ogg",
|
2479
|
+
".oom": "application/x-AtlasMate-Plugin",
|
2480
|
+
".p10": "application/pkcs10",
|
2481
|
+
".p12": "application/x-pkcs12",
|
2482
|
+
".p7b": "application/x-pkcs7-certificates",
|
2483
|
+
".p7c": "application/x-pkcs7-mime",
|
2484
|
+
".p7m": "application/x-pkcs7-mime",
|
2485
|
+
".p7r": "application/x-pkcs7-certreqresp",
|
2486
|
+
".p7s": "application/x-pkcs7-signature",
|
2487
|
+
".pac": "audio/x-pac",
|
2488
|
+
".pae": "audio/x-epac",
|
2489
|
+
".pan": "application/x-pan",
|
2490
|
+
".pbm": "image/x-portable-bitmap",
|
2491
|
+
".pcx": "image/x-pcx",
|
2492
|
+
".pda": "image/x-pda",
|
2493
|
+
".pdb": "chemical/x-pdb",
|
2494
|
+
".pdf": "application/pdf",
|
2495
|
+
".pfr": "application/font-tdpfr",
|
2496
|
+
".pfx": "application/x-pkcs12",
|
2497
|
+
".pgm": "image/x-portable-graymap",
|
2498
|
+
".pict": "image/x-pict",
|
2499
|
+
".pko": "application/ynd.ms-pkipko",
|
2500
|
+
".pm": "application/x-perl",
|
2501
|
+
".pma": "application/x-perfmon",
|
2502
|
+
".pmc": "application/x-perfmon",
|
2503
|
+
".pmd": "application/x-pmd",
|
2504
|
+
".pml": "application/x-perfmon",
|
2505
|
+
".pmr": "application/x-perfmon",
|
2506
|
+
".pmw": "application/x-perfmon",
|
2507
|
+
".png": "image/png",
|
2508
|
+
".pnm": "image/x-portable-anymap",
|
2509
|
+
".pnz": "image/png",
|
2510
|
+
".pot,": "application/vnd.ms-powerpoint",
|
2511
|
+
".ppm": "image/x-portable-pixmap",
|
2512
|
+
".pps": "application/vnd.ms-powerpoint",
|
2513
|
+
".ppt": "application/vnd.ms-powerpoint",
|
2514
|
+
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
2515
|
+
".pqf": "application/x-cprplayer",
|
2516
|
+
".pqi": "application/cprplayer",
|
2517
|
+
".prc": "application/x-prc",
|
2518
|
+
".prf": "application/pics-rules",
|
2519
|
+
".prop": "text/plain",
|
2520
|
+
".proxy": "application/x-ns-proxy-autoconfig",
|
2521
|
+
".ps": "application/postscript",
|
2522
|
+
".ptlk": "application/listenup",
|
2523
|
+
".pub": "application/x-mspublisher",
|
2524
|
+
".pvx": "video/x-pv-pvx",
|
2525
|
+
".qcp": "audio/vnd.qcelp",
|
2526
|
+
".qt": "video/quicktime",
|
2527
|
+
".qti": "image/x-quicktime",
|
2528
|
+
".qtif": "image/x-quicktime",
|
2529
|
+
".r3t": "text/vnd.rn-realtext3d",
|
2530
|
+
".ra": "audio/x-pn-realaudio",
|
2531
|
+
".ram": "audio/x-pn-realaudio",
|
2532
|
+
".rar": "application/octet-stream",
|
2533
|
+
".ras": "image/x-cmu-raster",
|
2534
|
+
".rc": "text/plain",
|
2535
|
+
".rdf": "application/rdf+xml",
|
2536
|
+
".rf": "image/vnd.rn-realflash",
|
2537
|
+
".rgb": "image/x-rgb",
|
2538
|
+
".rlf": "application/x-richlink",
|
2539
|
+
".rm": "audio/x-pn-realaudio",
|
2540
|
+
".rmf": "audio/x-rmf",
|
2541
|
+
".rmi": "audio/mid",
|
2542
|
+
".rmm": "audio/x-pn-realaudio",
|
2543
|
+
".rmvb": "audio/x-pn-realaudio",
|
2544
|
+
".rnx": "application/vnd.rn-realplayer",
|
2545
|
+
".roff": "application/x-troff",
|
2546
|
+
".rp": "image/vnd.rn-realpix",
|
2547
|
+
".rpm": "audio/x-pn-realaudio-plugin",
|
2548
|
+
".rt": "text/vnd.rn-realtext",
|
2549
|
+
".rte": "x-lml/x-gps",
|
2550
|
+
".rtf": "application/rtf",
|
2551
|
+
".rtg": "application/metastream",
|
2552
|
+
".rtx": "text/richtext",
|
2553
|
+
".rv": "video/vnd.rn-realvideo",
|
2554
|
+
".rwc": "application/x-rogerwilco",
|
2555
|
+
".s3m": "audio/x-mod",
|
2556
|
+
".s3z": "audio/x-mod",
|
2557
|
+
".sca": "application/x-supercard",
|
2558
|
+
".scd": "application/x-msschedule",
|
2559
|
+
".sct": "text/scriptlet",
|
2560
|
+
".sdf": "application/e-score",
|
2561
|
+
".sea": "application/x-stuffit",
|
2562
|
+
".setpay": "application/set-payment-initiation",
|
2563
|
+
".setreg": "application/set-registration-initiation",
|
2564
|
+
".sgm": "text/x-sgml",
|
2565
|
+
".sgml": "text/x-sgml",
|
2566
|
+
".sh": "application/x-sh",
|
2567
|
+
".shar": "application/x-shar",
|
2568
|
+
".shtml": "magnus-internal/parsed-html",
|
2569
|
+
".shw": "application/presentations",
|
2570
|
+
".si6": "image/si6",
|
2571
|
+
".si7": "image/vnd.stiwap.sis",
|
2572
|
+
".si9": "image/vnd.lgtwap.sis",
|
2573
|
+
".sis": "application/vnd.symbian.install",
|
2574
|
+
".sit": "application/x-stuffit",
|
2575
|
+
".skd": "application/x-Koan",
|
2576
|
+
".skm": "application/x-Koan",
|
2577
|
+
".skp": "application/x-Koan",
|
2578
|
+
".skt": "application/x-Koan",
|
2579
|
+
".slc": "application/x-salsa",
|
2580
|
+
".smd": "audio/x-smd",
|
2581
|
+
".smi": "application/smil",
|
2582
|
+
".smil": "application/smil",
|
2583
|
+
".smp": "application/studiom",
|
2584
|
+
".smz": "audio/x-smd",
|
2585
|
+
".snd": "audio/basic",
|
2586
|
+
".spc": "application/x-pkcs7-certificates",
|
2587
|
+
".spl": "application/futuresplash",
|
2588
|
+
".spr": "application/x-sprite",
|
2589
|
+
".sprite": "application/x-sprite",
|
2590
|
+
".sdp": "application/sdp",
|
2591
|
+
".spt": "application/x-spt",
|
2592
|
+
".src": "application/x-wais-source",
|
2593
|
+
".sst": "application/vnd.ms-pkicertstore",
|
2594
|
+
".stk": "application/hyperstudio",
|
2595
|
+
".stl": "application/vnd.ms-pkistl",
|
2596
|
+
".stm": "text/html",
|
2597
|
+
".svg": "image/svg+xml",
|
2598
|
+
".sv4cpio": "application/x-sv4cpio",
|
2599
|
+
".sv4crc": "application/x-sv4crc",
|
2600
|
+
".svf": "image/vnd",
|
2601
|
+
".svh": "image/svh",
|
2602
|
+
".svr": "x-world/x-svr",
|
2603
|
+
".swf": "application/x-shockwave-flash",
|
2604
|
+
".swfl": "application/x-shockwave-flash",
|
2605
|
+
".t": "application/x-troff",
|
2606
|
+
".tad": "application/octet-stream",
|
2607
|
+
".talk": "text/x-speech",
|
2608
|
+
".tar": "application/x-tar",
|
2609
|
+
".taz": "application/x-tar",
|
2610
|
+
".tbp": "application/x-timbuktu",
|
2611
|
+
".tbt": "application/x-timbuktu",
|
2612
|
+
".tcl": "application/x-tcl",
|
2613
|
+
".tex": "application/x-tex",
|
2614
|
+
".texi": "application/x-texinfo",
|
2615
|
+
".texinfo": "application/x-texinfo",
|
2616
|
+
".tgz": "application/x-compressed",
|
2617
|
+
".thm": "application/vnd.eri.thm",
|
2618
|
+
".tif": "image/tiff",
|
2619
|
+
".tiff": "image/tiff",
|
2620
|
+
".tki": "application/x-tkined",
|
2621
|
+
".tkined": "application/x-tkined",
|
2622
|
+
".toc": "application/toc",
|
2623
|
+
".toy": "image/toy",
|
2624
|
+
".tr": "application/x-troff",
|
2625
|
+
".trk": "x-lml/x-gps",
|
2626
|
+
".trm": "application/x-msterminal",
|
2627
|
+
".tsi": "audio/tsplayer",
|
2628
|
+
".tsp": "application/dsptype",
|
2629
|
+
".tsv": "text/tab-separated-values",
|
2630
|
+
".ttf": "application/octet-stream",
|
2631
|
+
".ttz": "application/t-time",
|
2632
|
+
".txt": "text/plain",
|
2633
|
+
".uls": "text/iuls",
|
2634
|
+
".ult": "audio/x-mod",
|
2635
|
+
".ustar": "application/x-ustar",
|
2636
|
+
".uu": "application/x-uuencode",
|
2637
|
+
".uue": "application/x-uuencode",
|
2638
|
+
".vcd": "application/x-cdlink",
|
2639
|
+
".vcf": "text/x-vcard",
|
2640
|
+
".vdo": "video/vdo",
|
2641
|
+
".vib": "audio/vib",
|
2642
|
+
".viv": "video/vivo",
|
2643
|
+
".vivo": "video/vivo",
|
2644
|
+
".vmd": "application/vocaltec-media-desc",
|
2645
|
+
".vmf": "application/vocaltec-media-file",
|
2646
|
+
".vmi": "application/x-dreamcast-vms-info",
|
2647
|
+
".vms": "application/x-dreamcast-vms",
|
2648
|
+
".vox": "audio/voxware",
|
2649
|
+
".vqe": "audio/x-twinvq-plugin",
|
2650
|
+
".vqf": "audio/x-twinvq",
|
2651
|
+
".vql": "audio/x-twinvq",
|
2652
|
+
".vre": "x-world/x-vream",
|
2653
|
+
".vrml": "x-world/x-vrml",
|
2654
|
+
".vrt": "x-world/x-vrt",
|
2655
|
+
".vrw": "x-world/x-vream",
|
2656
|
+
".vts": "workbook/formulaone",
|
2657
|
+
".wav": "audio/x-wav",
|
2658
|
+
".wax": "audio/x-ms-wax",
|
2659
|
+
".wbmp": "image/vnd.wap.wbmp",
|
2660
|
+
".wcm": "application/vnd.ms-works",
|
2661
|
+
".wdb": "application/vnd.ms-works",
|
2662
|
+
".web": "application/vnd.xara",
|
2663
|
+
".wi": "image/wavelet",
|
2664
|
+
".wis": "application/x-InstallShield",
|
2665
|
+
".wks": "application/vnd.ms-works",
|
2666
|
+
".wm": "video/x-ms-wm",
|
2667
|
+
".wma": "audio/x-ms-wma",
|
2668
|
+
".wmd": "application/x-ms-wmd",
|
2669
|
+
".wmf": "application/x-msmetafile",
|
2670
|
+
".wml": "text/vnd.wap.wml",
|
2671
|
+
".wmlc": "application/vnd.wap.wmlc",
|
2672
|
+
".wmls": "text/vnd.wap.wmlscript",
|
2673
|
+
".wmlsc": "application/vnd.wap.wmlscriptc",
|
2674
|
+
".wmlscript": "text/vnd.wap.wmlscript",
|
2675
|
+
".wmv": "audio/x-ms-wmv",
|
2676
|
+
".wmx": "video/x-ms-wmx",
|
2677
|
+
".wmz": "application/x-ms-wmz",
|
2678
|
+
".wpng": "image/x-up-wpng",
|
2679
|
+
".wps": "application/vnd.ms-works",
|
2680
|
+
".wpt": "x-lml/x-gps",
|
2681
|
+
".wri": "application/x-mswrite",
|
2682
|
+
".wrl": "x-world/x-vrml",
|
2683
|
+
".wrz": "x-world/x-vrml",
|
2684
|
+
".ws": "text/vnd.wap.wmlscript",
|
2685
|
+
".wsc": "application/vnd.wap.wmlscriptc",
|
2686
|
+
".wv": "video/wavelet",
|
2687
|
+
".wvx": "video/x-ms-wvx",
|
2688
|
+
".wxl": "application/x-wxl",
|
2689
|
+
".x-gzip": "application/x-gzip",
|
2690
|
+
".xaf": "x-world/x-vrml",
|
2691
|
+
".xar": "application/vnd.xara",
|
2692
|
+
".xbm": "image/x-xbitmap",
|
2693
|
+
".xdm": "application/x-xdma",
|
2694
|
+
".xdma": "application/x-xdma",
|
2695
|
+
".xdw": "application/vnd.fujixerox.docuworks",
|
2696
|
+
".xht": "application/xhtml+xml",
|
2697
|
+
".xhtm": "application/xhtml+xml",
|
2698
|
+
".xhtml": "application/xhtml+xml",
|
2699
|
+
".xla": "application/vnd.ms-excel",
|
2700
|
+
".xlc": "application/vnd.ms-excel",
|
2701
|
+
".xll": "application/x-excel",
|
2702
|
+
".xlm": "application/vnd.ms-excel",
|
2703
|
+
".xls": "application/vnd.ms-excel",
|
2704
|
+
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
2705
|
+
".xlt": "application/vnd.ms-excel",
|
2706
|
+
".xlw": "application/vnd.ms-excel",
|
2707
|
+
".xm": "audio/x-mod",
|
2708
|
+
".xml": "text/plain",
|
2709
|
+
".xml": "application/xml",
|
2710
|
+
".xmz": "audio/x-mod",
|
2711
|
+
".xof": "x-world/x-vrml",
|
2712
|
+
".xpi": "application/x-xpinstall",
|
2713
|
+
".xpm": "image/x-xpixmap",
|
2714
|
+
".xsit": "text/xml",
|
2715
|
+
".xsl": "text/xml",
|
2716
|
+
".xul": "text/xul",
|
2717
|
+
".xwd": "image/x-xwindowdump",
|
2718
|
+
".xyz": "chemical/x-pdb",
|
2719
|
+
".yz1": "application/x-yz1",
|
2720
|
+
".z": "application/x-compress",
|
2721
|
+
".zac": "application/x-zaurus-zac",
|
2722
|
+
".zip": "application/zip",
|
2723
|
+
".json": "application/json",
|
2724
|
+
".go": "application/go",
|
2725
|
+
".properties": "application/properties",
|
2726
|
+
".yaml": "application/yaml",
|
2727
|
+
".bat": "application/bat",
|
2728
|
+
".sh": "application/sh",
|
2729
|
+
".vue": "application/vue",
|
2730
|
+
".less": "application/less",
|
2731
|
+
".sass": "application/sass",
|
2732
|
+
".csv": "application/csv",
|
2733
|
+
".lua": "application/lua",
|
2734
|
+
".apex": "application/apex",
|
2735
|
+
".azcli": "application/azcli",
|
2736
|
+
".clojure": "application/clojure",
|
2737
|
+
".coffee": "application/coffee",
|
2738
|
+
".cs": "application/cs",
|
2739
|
+
".csp": "application/csp",
|
2740
|
+
".dockerfile": "application/dockerfile",
|
2741
|
+
".fsharp": "application/fsharp",
|
2742
|
+
".handlebars": "text/plain",
|
2743
|
+
".ini": "text/plain",
|
2744
|
+
".md": "text/plain",
|
2745
|
+
".perl": "text/plain",
|
2746
|
+
".php": "text/plain",
|
2747
|
+
".py": "text/plain",
|
2748
|
+
".redis": "text/plain",
|
2749
|
+
".conf": "text/conf",
|
2750
|
+
".sql": "text/sql"
|
2751
|
+
}
|
2752
|
+
|
2753
|
+
M.test = function () {
|
2754
|
+
console.log(privateObj.staticMime[".jssson"] || "aa")
|
2755
|
+
}
|
2756
|
+
|
2757
|
+
M.getRemoteCacheByUrl = async function (url) {
|
2758
|
+
if (url in M._globle_cacheMap) {
|
2759
|
+
return M._globle_cacheMap[url];
|
2760
|
+
}
|
2761
|
+
let text = "";
|
2762
|
+
if (url.startsWith("file:")) {
|
2763
|
+
text = M.readFile(url.substring(5));
|
2764
|
+
} else {
|
2765
|
+
text = await M.get(url);
|
2766
|
+
}
|
2767
|
+
console.log("req remote url:", url);
|
2768
|
+
M._globle_cacheMap[url] = text;
|
2769
|
+
return text;
|
2770
|
+
}
|
2771
|
+
|
2772
|
+
M.init = function () {
|
2773
|
+
/***
|
2774
|
+
* 下划线命名转为驼峰命名
|
2775
|
+
*/
|
2776
|
+
String.prototype.underlineToHump = function () {
|
2777
|
+
var re = /_(\w)/g;
|
2778
|
+
str = this.replace(re, function ($0, $1) {
|
2779
|
+
return $1.toUpperCase();
|
2780
|
+
});
|
2781
|
+
return str;
|
2782
|
+
}
|
2783
|
+
|
2784
|
+
/***
|
2785
|
+
* 驼峰命名转下划线
|
2786
|
+
*/
|
2787
|
+
String.prototype.humpToUnderline = function () {
|
2788
|
+
var re = /_(\w)/g;
|
2789
|
+
str = this.replace(/([A-Z])/g, "_$1").toLowerCase();
|
2790
|
+
return str;
|
2791
|
+
}
|
2792
|
+
|
2793
|
+
//首字母变大写
|
2794
|
+
String.prototype.firstChartoUpper = function () {
|
2795
|
+
return this.replace(/^([a-z])/g, function (word) {
|
2796
|
+
return word.replace(word.charAt(0), word.charAt(0).toUpperCase());
|
2797
|
+
});
|
2798
|
+
}
|
2799
|
+
//首字母变小写
|
2800
|
+
String.prototype.firstChartoLower = function () {
|
2801
|
+
return this.replace(/^([A-Z])/g, function (word) {
|
2802
|
+
return word.replace(word.charAt(0), word.charAt(0).toLowerCase());
|
2803
|
+
});
|
2804
|
+
}
|
2805
|
+
//格式化日期
|
2806
|
+
Date.prototype.format = function (fmt) {
|
2807
|
+
var o = {
|
2808
|
+
"M+": this.getMonth() + 1, //月份
|
2809
|
+
"d+": this.getDate(), //日
|
2810
|
+
"h+": this.getHours(), //小时
|
2811
|
+
"m+": this.getMinutes(), //分
|
2812
|
+
"s+": this.getSeconds(), //秒
|
2813
|
+
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
|
2814
|
+
"S": this.getMilliseconds() //毫秒
|
2815
|
+
};
|
2816
|
+
if (/(y+)/.test(fmt)) {
|
2817
|
+
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
|
2818
|
+
}
|
2819
|
+
for (var k in o) {
|
2820
|
+
if (new RegExp("(" + k + ")").test(fmt)) {
|
2821
|
+
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
2822
|
+
}
|
2823
|
+
}
|
2824
|
+
return fmt;
|
2825
|
+
}
|
2826
|
+
|
2827
|
+
|
2828
|
+
}
|
2829
|
+
M.init();
|
2830
|
+
|
2831
|
+
global.M=M;
|
2832
|
+
global.MIO=M.IO;
|
2833
|
+
|
2834
|
+
module.exports = M;
|