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